【发布时间】:2023-03-06 12:08:01
【问题描述】:
我正在用 java 为与 ikev2 协议相关的程序编写测试工具。作为其中的一部分,我需要能够计算 ECDSA 签名(特别是使用 NIST P-256 曲线)。
RFC 4754 描述了 IKEv2 中 ECDSA 的使用,并提供了一组测试向量(包括我需要的 p256 曲线)。
我正在尝试使用以下代码通过 java 的 ECDSA 签名实现运行 ECDSA-256 测试向量值(RFC 中的Section 8.1):
//"abc" for the input
byte[] input = { 0x61, 0x62, 0x63 };
//Ugly way of getting the ECParameterSpec for the P-256 curve by name as opposed to specifying all the parameters manually.
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec kpgparams = new ECGenParameterSpec("secp256r1");
kpg.initialize(kpgparams);
ECParameterSpec params = ((ECPublicKey) kpg.generateKeyPair().getPublic()).getParams();
//Create the static private key W from the Test Vector
ECPrivateKeySpec static_privates = new ECPrivateKeySpec(new BigInteger("DC51D3866A15BACDE33D96F992FCA99DA7E6EF0934E7097559C27F1614C88A7F", 16), params);
KeyFactory kf = KeyFactory.getInstance("EC");
ECPrivateKey spriv = (ECPrivateKey) kf.generatePrivate(static_privates);
//Perfrom ECDSA signature of the data with SHA-256 as the hash algorithm
Signature dsa = Signature.getInstance("SHA256withECDSA");
dsa.initSign(spriv);
dsa.update(input);
byte[] output = dsa.sign();
System.out.println("Result: " + new BigInteger(1, output).toString(16));
结果应该是:
CB28E099 9B9C7715 FD0A80D8 E47A7707 9716CBBF 917DD72E 97566EA1 C066957C 86FA3BB4 E26CAD5B F90B7F81 899256CE 7594BB1E A0C89212 748BFF3B 3D5B0315
相反,我得到:
30460221 00DD9131 EDEB5FD C5E718DF C8A7AB2D 5532B85B 7D4C012A E5A4E90C 3B824AB5 D7022100 9A8A2B12 9E10AFF 7066FF79 89AA73D5 BA37C868 5.36517 216S43 FFA876D7 P>
我知道长度差异是由于 Java ASN.1 对签名进行编码。但是,其余部分完全错误,我不知道为什么。
任何帮助或建议将不胜感激!
P.S 我不是 ECDSA 或 Java 加密专家,所以这可能是我犯的一个愚蠢的错误
【问题讨论】:
标签: java cryptography elliptic-curve ecdsa