Search code examples
javacryptographydigital-signaturebouncycastleelliptic-curve

ECC ASN1 signature verification failure


I have 48 bytes ECC secp192r1 signature which worked in other environment:

byte[] signature = new byte[]{(byte)0x08, (byte)0x33, (byte)0x6B, (byte)0x27, (byte)0xBC, (byte)0x29, (byte)0x64, (byte)0x36, (byte)0x70, (byte)0x08, (byte)0x97, (byte)0x4F, (byte)0xA8, (byte)0xD7, (byte)0x1F, (byte)0x4D, (byte)0x05, (byte)0xF5, (byte)0xB2, (byte)0x0F, (byte)0x15, (byte)0x5D, (byte)0x68, (byte)0x61, (byte)0xB3, (byte)0x2B, (byte)0x0E, (byte)0xA9, (byte)0xFB, (byte)0x37, (byte)0xF1, (byte)0xD4, (byte)0x70, (byte)0xEA, (byte)0x2B, (byte)0xCA, (byte)0x53, (byte)0x9D, (byte)0x11, (byte)0xE7, (byte)0x26, (byte)0x37, (byte)0x92, (byte)0x73, (byte)0xDE, (byte)0x95, (byte)0x6C, (byte)0x4A};

It is encoded to ASN1 format ~54 bytes length with:

    byte[] sig1 = Arrays.copyOfRange(signature, 0, 24);
    byte[] sig2 = Arrays.copyOfRange(signature, 24, 48);

    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(new BigInteger(sig1)));
    v.add(new ASN1Integer(new BigInteger(sig2)));

    byte[] javaSig = new DERSequence(v).getEncoded();

However, when I try to verify data, it fails. Is it correct way to encode signature to ASN1?

Also there is the same problem with signature decoding from ASN1 to 48 bytes format:

    ASN1InputStream input = new ASN1InputStream(signed);
    ASN1Primitive item = input.readObject();

    ASN1Sequence s = (ASN1Sequence)item;
    BigInteger[] items = new BigInteger[2];

    items[0] = ((ASN1Integer)s.getObjectAt(0)).getValue();
    items[1] = ((ASN1Integer)s.getObjectAt(1)).getValue();

    byte[] itBytes0 = items[0].toByteArray();
    byte[] itBytes1 = items[1].toByteArray();

Solution

  • Switched from ECDSA to NONEwithECDSA:

    Signature sig = Signature.getInstance("NONEwithECDSA", "BC");