Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating ECDSA signature in Java as per an RFC test vector
    primarykey
    data
    text
    <p>I am writing a test harness in java for a program relating to the ikev2 protocol. As part of this i need to be able to calculate an ECDSA signature (specifically using the NIST P-256 curve).</p> <p><a href="http://tools.ietf.org/html/rfc4754" rel="nofollow">RFC 4754</a> Describes the the use of ECDSA in IKEv2 and helpfully provides a set of test vectors (Including for the p256 curve that i need).</p> <p>I am trying to run the ECDSA-256 Test Vector values (<a href="http://tools.ietf.org/html/rfc4754#section-8.1" rel="nofollow">Section 8.1</a> in the RFC) through java's ECDSA signature implementation using the following code:</p> <pre><code>//"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)); </code></pre> <p>The result should be:</p> <blockquote> <p>CB28E099 9B9C7715 FD0A80D8 E47A7707 9716CBBF 917DD72E 97566EA1 C066957C 86FA3BB4 E26CAD5B F90B7F81 899256CE 7594BB1E A0C89212 748BFF3B 3D5B0315</p> </blockquote> <p>Instead I get:</p> <blockquote> <p>30460221 00dd9131 edeb5efd c5e718df c8a7ab2d 5532b85b 7d4c012a e5a4e90c 3b824ab5 d7022100 9a8a2b12 9e10a2ff 7066ff79 89aa73d5 ba37c868 5ec36517 216e2e43 ffa876d7</p> </blockquote> <p>I know that the length difference is due to Java ASN.1 Encoding the signature. However, the rest of it is completely wrong and I'm stumped as to why.</p> <p>Any help or advice would be greatly appreciated!</p> <p>P.S I am not a ECDSA or Java crypto expert so it is probably a stupid mistake I am making</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload