Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm guessing that each time you run your program, you get a different signature value for the same plaintext (to-be-signed) input. </p> <p>ECDSA specifies that a random ephemeral ECDSA private key be generated per signature. To that end, <code>Signature.getInstance("SHA256withECDSA")</code> doesn't let you specify an ephemeral key (this is a good thing, to prevent many a self shot in the foot!). Instead, it gets its own SecureRandom instance that will make your output nondeterministic. </p> <p>This probably means you can't use JCE (<code>Signature.getInstance()</code>) for test vector validation.</p> <p>What you could do is extend <code>SecureRandom</code> in a way that it returns deterministic data. Obviously you shouldn't use this in a real deployment:</p> <pre><code>public class FixedSecureRandom extends SecureRandom { private static boolean debug = false; private static final long serialVersionUID = 1L; public FixedSecureRandom() { } private int nextBytesIndex = 0; private byte[] nextBytesValues = null; public void setBytes(byte[] values) { this.nextBytesValues = values; } public void nextBytes(byte[] b) { if (nextBytesValues==null) { super.nextBytes(b); } else if (nextBytesValues.length==0) { super.nextBytes(b); } else { for (int i=0; i&lt;b.length; i++) { b[i] = nextBytesValues[nextBytesIndex]; nextBytesIndex = (nextBytesIndex + 1) % nextBytesValues.length; } } } } </code></pre> <p>Phew. Ok now you have a SecureRandom class that returns you some number of known bytes, then falls back to a real SecureRandom after that. I'll say it again (excuse the shouting) - DO NOT USE THIS IN PRODUCTION!</p> <p>Next you'll need to use a ECDSA implementation that lets you specify your own SecureRandom. You can use BouncyCastle's <code>ECDSASigner</code> for this purpose. Except here you're going to give it your own bootlegged FixedSecureRandom, so that when it calls <code>secureRandom.getBytes()</code>, it gets the bytes you want it to. This lets you control the ephemeral key to match that specified in the test vectors. You may need to massage the actual bytes (eg. add zero pre-padding) to match what <code>ECDSASigner</code> is going to request.</p> <pre><code>ECPrivateKeyParameters ecPriv = ...; // this is the user's EC private key (not ephemeral) FixedSecureRandom fsr_k = new FixedSecureRandom(); fsr_k.setBytes(tempKeyK); ECDSASigner signer = new ECDSASigner(); ParametersWithRandom ecdsaprivrand = new ParametersWithRandom(ecPriv, fsr_k); signer.init(true, ecdsaprivrand); </code></pre> <p>Note that BC's <code>ECDSASigner</code> implements only the EC signature part, not the hashing. You'll still need to do your own hashing (assuming your input data is in <code>data</code>):</p> <pre><code>Digest md = new SHA256Digest() md.reset(); md.update(data, 0, data.length); byte[] hash = new byte[md.getDigestSize()]; md.doFinal(hash, 0); </code></pre> <p>before you create the ECDSA signature:</p> <pre><code>BigInteger[] sig = signer.generateSignature(hash); </code></pre> <p>Finally, this <code>BigInteger[]</code> (should be length==2) are the (r,s) values. You'll need to ASN.1 DER-encode it, which should give you the <strike>droids</strike> bytes you're looking for.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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