I need a fixed length of 64 byte ECDSA signature with NIST P-256 curve.
The implementation should use JCE.
The following code sample can generate a signature and verify it.
Provider provSign = new SunEC();
Provider provVerify = new SunEC();
KeyPairGenerator kg = KeyPairGenerator.getInstance("EC", provSign);
ECGenParameterSpec ecParam = new ECGenParameterSpec("secp256r1");
kg.initialize(ecParam);
KeyPair keyPair = kg.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
try
{
KeyFactory kf = KeyFactory.getInstance("EC", provSign);
ECPublicKeySpec publicKeySpec = kf.getKeySpec(keyPair.getPublic(), ECPublicKeySpec.class);
kf = KeyFactory.getInstance("EC", provVerify);
publicKey = (PublicKey)kf.generatePublic(publicKeySpec);
}
catch (InvalidKeySpecException ex)
{
ex.printStackTrace();
}
Signature sig = Signature.getInstance("SHA256withECDSA", provSign);
Signature ver = Signature.getInstance("SHA256withECDSA", provVerify);
byte[] data = new byte[64];
sig.initSign(privateKey);
sig.update(data);
byte [] sign = sig.sign();
ver.initVerify(publicKey);
ver.update(data);
if (ver.verify(sign) == false)
{
throw new Exception("Signature Verification failed");
}
The problem is that the character is somehow encoded (I think in DER format) and lasts from 70 to 72 bytes, but I need a signature of 64 bytes (unregistered / unprocessed).
What I tried: Convert to a fixed length of 64 bytes Signature
DerInputStream derInputStream = new DerInputStream(sign);
DerValue[] values = derInputStream.getSequence(2);
byte[] random = values[0].getPositiveBigInteger().toByteArray();
byte[] signature = values[1].getPositiveBigInteger().toByteArray();
byte[] tokenSignature = new byte[64];
System.arraycopy(random, random.length > 32 ? 1 : 0, tokenSignature, random.length < 32 ? 1 : 0,
random.length > 32 ? 32 : random.length);
System.arraycopy(signature, signature.length > 32 ? 1 : 0, tokenSignature, signature.length < 32 ? 33 : 32,
signature.length > 32 ? 32 : signature.length);
System.out.println("Full Signature length: "+tokenSignature.length+" r length: "+random.length+" s length"+signature.length);
How to check a 64-byte token signature now? I do not know how to convert a 64-byte Signature token back to the desired format
ver.initVerify(publicKey);
ver.update(data);
if (ver.verify(???) == false)
{
throw new Exception("Signature Verification failed");
}
64- BouncyCastle ECDSASigner. ECDSASigner, SignatureSpi , , JCE.