1
$\begingroup$

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;
}
$\endgroup$
4
  • $\begingroup$ I'm not sure if this applies, but ECIES by itself provides strong secrecy and non-malleability to the receiver, but I guess you want to authenticate the sender? $\endgroup$
    – SEJPM
    Jun 1, 2015 at 19:31
  • 1
    $\begingroup$ May you please provide the verification routine aswell? $\endgroup$
    – SEJPM
    Jun 1, 2015 at 19:33
  • $\begingroup$ Please also indicate if verification takes place on the same runtime or on another. $\endgroup$
    – Maarten Bodewes
    Jun 1, 2015 at 23:55
  • $\begingroup$ Verify added to the question. Yes, the sign and verify took place at the same runtime although this will not be the common case $\endgroup$
    – Simon
    Jun 2, 2015 at 21:21

0

Browse other questions tagged or ask your own question.