I am using BouncyCastle to perform ECDH and then encrypt the data between the mobile (Android) and my hardware with ECIES. It all works fine. When I try to sign a byte array and send it to the hardware, the hardware fails to verify. My code to sign is as follows. You can see in the code below that I use NONEWithECDSA but I have tried also SHA256WithECDSA and SHA1WithECDSA.
public static byte[] sign(Context context, byte[] s) throws InvalidKeyException, NoSuchAlgorithmException, UnsupportedEncodingException, SignatureException
{
/*
* Create a Signature object and initialize it with the private key
*/
byte[] strByte;
Signature dsa = Signature.getInstance("NONEwithECDSA");
PrivateKey privateKey = SecUtils.getPrivateKey();
dsa.initSign(privateKey);
if (s == null)
strByte = "This is string to sign".getBytes();
else
strByte = s;
dsa.update(strByte);
/*
* Now that all the data to be signed has been read in, generate a
* signature for it
*/
byte[] realSig = dsa.sign();
return realSig;
//(new BigInteger(1, realSig).toString(16));
}
Verify
public static boolean verify(Context context, PublicKey publicKey, byte[] baSource, byte[] baSignature) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException
{
Signature ecdsaVerify;
byte [] baText;
PublicKey pk = SecUtils.getPublicKey(SecUtils.publicType.ENUM_REMOTE);
ecdsaVerify = Signature.getInstance("NONEwithECDSA");
ecdsaVerify.initVerify(pk);
if (baSource == null)
baText = "This is string to sign".getBytes();
else
baText = baSource;
ecdsaVerify.update(baText);
boolean result = ecdsaVerify.verify(baSignature);
return result;
}