EC 公钥是否有标准化的固定长度编码?

发布于 2024-11-19 15:54:26 字数 114 浏览 3 评论 0原文

我想知道是否有(并且我希望有)针对素数域(192、224、256)上的每种曲线类型的 ECDH(椭圆曲线 Diffie-Hellman)和 ECDSA(椭圆曲线数字签名算法)的公钥大小标准、 384 和 521)。

I was wondering if there was (and I hope there is) a standard for public key size for ECDH (Elliptic Curve Diffie-Hellman) and ECDSA (Elliptic Curve Digital Signature Algorithm) for every curve type over prime fields (192, 224, 256, 384 and 521).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-11-26 15:54:26

如果您使用“命名曲线”之一,则公钥大小是固定的,并且取决于基础曲线的“字段大小”。

压缩与未压缩表示

公钥大小进一步取决于是否使用“未压缩”表示或“压缩”表示。在未压缩形式中,公钥大小等于字段大小(以字节为单位)的两倍 + 1,在压缩形式中,它是字段大小 + 1。因此,如果您的曲线定义在 secp256r1 (也称为 NIST P-256X9.62 prime256v1),则字段大小为 256 位或 32 字节。因此,公钥在未压缩形式下的长度恰好为 65 字节 (32*2 +1),在压缩形式下的长度为 33 字节 (32 +1)。

未压缩的形式由 0x04(类似于 DER OCTET STRING 标记)和x 坐标的大端二进制表示加上公共点 y 坐标的二进制表示的串联。

GF(p) 情况

如果基础字段是 GF(p),其中 p 是一个大素数(在 P-256 的情况下,是 256 位素数),则 x 和 y 可以被视为来自 [0, p-1]。它们以通常的方式编码为
((log2(p)+1)/8) 字节整数,如有必要,MSB 用零填充。

GF(2^m) 情况

对于 GF(2^m) x 和 y 可以被认为是带有系数的多项式 a_0 + a_1x + a_2x^2 + ... + a_{m-1}x^{m-1} a_i 等于 0 或 1。它们的二进制表示形式只是系数的串联。

进一步阅读

确切的详细信息可以在 SEC1v2 中找到。 (特别是第 10 页和第 11 页的2.3.3 椭圆曲线点到八位字节字符串转换部分。)

If you use one of the "named curves" then the public key size is fixed and dependent on the "field size" of your underlying curve.

Compressed vs. uncompressed representation

Public key sizes further depend on whether the "uncompressed" representation or the "compressed" representation is used. In the uncompressed form, the public key size is equal to two times the field size (in bytes) + 1, in the compressed form it is field size + 1. So if your curve is defined on secp256r1 (also called NIST P-256 or X9.62 prime256v1), then the field size is 256 bits or 32 bytes. And therefore the public key would be exactly 65 bytes (32*2 +1) long in the uncompressed form and 33 bytes (32 +1) long in the compressed form.

The uncompressed form consists of an 0x04 (in analogy to the DER OCTET STRING tag) plus the concatenation of the big-endian binary representation of the x coordinate plus the binary representation of the y coordinate of the public point.

GF(p) case

If the underlying field is GF(p) where p is a a big prime (in the case of P-256, a 256-bit prime), then x and y can be thought of as elements from [0, p-1]. They are encoded in the usual way as
((log2(p)+1)/8)-byte integers, with the MSBs padded with zero if necessary.

GF(2^m) case

For GF(2^m) x and y can be thought of as polynomials a_0 + a_1x + a_2x^2 + ... + a_{m-1}x^{m-1} with coefficients a_i equal to either 0 or 1. Their binary representation is simply the concatenation of the coefficients.

Further reading

The exact details can be found in SEC1v2. (Especially section 2.3.3 Elliptic-Curve-Point-to-Octet-String Conversion on pages 10 and 11.)

╰つ倒转 2024-11-26 15:54:26

我一直在寻找答案,并想用 Java 来分享我的答案。我的任务是从 X509Certificate(正确的网站)获取密钥大小

方法 #1 - 实际计算:(

ECPublicKeyImpl ecPublicKey = (ECPublicKeyImpl) certificate.getPublicKey();
int publicKeyLength = (ecPublicKey.getEncodedPublicValue().length - 1) / 2 * 8;

可能会添加验证第一个字节是否为 0x04)

方法 #2 - 从一些“内部”中提取:

ECParameterSpec spec = ecPublicKey.getParams();
AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("EC");
algorithmParameters.init(spec);
Provider provider = algorithmParameters.getProvider();
provider.get("KeyPairGenerator.EC KeySize");

I was looking for answer quite long and wanted to share mine in Java. My task was to get key size from X509Certificate (website to be correct)

Method #1 - actually calculating:

ECPublicKeyImpl ecPublicKey = (ECPublicKeyImpl) certificate.getPublicKey();
int publicKeyLength = (ecPublicKey.getEncodedPublicValue().length - 1) / 2 * 8;

(Verification if first byte is 0x04 might be added)

Method #2 - extracting from some "internals":

ECParameterSpec spec = ecPublicKey.getParams();
AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance("EC");
algorithmParameters.init(spec);
Provider provider = algorithmParameters.getProvider();
provider.get("KeyPairGenerator.EC KeySize");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文