1

I have generated EC curve keys using openssl and generated the signature using sha256 digest

The data: 265a33bf7a514b6671e6e02aaee2383759348d9f

openssl dgst -sha256 -sign key1.pem data > sig1

The public key file pubkey1.pem

   -----BEGIN PUBLIC KEY-----
    MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAE/2CznS1gXRaO6z8UvF1SOs97Dwp5HUdo
    1Y9OW91lfLl1NA8uXUFY7wJYvTl2dbnuZ1muh7htsxMVgEEbn+XCdQ==
    -----END PUBLIC KEY-----

The signature file base64 encoded

MEUCIQDqUv33+c3svyYOXPVZCYx49TE2Vxq4uP5kSV2ZJ4o/JwIgEqWkxdSMNuQNuzL4KXTEeH/O
ZBFjyErxvHgdHTCjeh0=

Singature verification passed

[bash]$ base64 -d sig1b64  > sig1d
[bash]$ openssl dgst -sha256 -verify pubkey1.pem -signature sig1d 
data
Verified OK

When i verify this using the Java Bouncycastle library it fails to verify the signature, here is the code snippet,

    public boolean verifyMessage (final String param,final String message , final String signature , final String algo ) throws Exception
{

    byte[] content = param.getBytes();
    InputStream is = new ByteArrayInputStream(content);
    InputStreamReader isr = new InputStreamReader(is);

    Reader br = new BufferedReader(isr);
    PEMParser parser = new PEMParser(br);

    Object obj = parser.readObject();
    PublicKey thepubKeyofA = null;

    if (obj instanceof org.bouncycastle.asn1.x509.SubjectPublicKeyInfo) {
        SubjectPublicKeyInfo eckey = (SubjectPublicKeyInfo) obj;
        thepubKeyofA = new JcaPEMKeyConverter().setProvider("BC").getPublicKey(eckey);
        Signature ecdsaVerify = Signature.getInstance("SHA256withECDSA", "BC");
        ecdsaVerify.initVerify(thepubKeyofA);
        ecdsaVerify.update(message.getBytes());
        boolean result = ecdsaVerify.verify(Utils.decodeBASE64(signature));
        return result;

    }
anish
  • 6,884
  • 13
  • 74
  • 140
  • **Check your data.** I can't reproduce your case because `data` plus LF, CRLF, or nothing all fail with openssl. But with a similar signature generated by openssl using my keypair, and the **correct data, your code with a line added** to declare `(bouncy).asn1.x509.SubjectPublicKeyInfo eckey` initted from `getInstance` of the binary pubkey it **verifies true as expected**.with all BCprov versions from 1.50 to 1.61. – dave_thompson_085 Apr 28 '19 at 08:57
  • PS: you don't need Bouncy if you're using Sun/Oracle Java; it has included ECDSA for a decade since j7, and 'X509' pubkey format since the beginning of JCA. I believe the same for IBM, but I don't know about Apple or Android. – dave_thompson_085 Apr 28 '19 at 09:06
  • @dave_thompson_085 Thanks would it possible to share the code snippet for Java – anish Apr 28 '19 at 13:06
  • 1
    (1) I see you now posted the data, and added to the code. If I use your completed code as now posted, with that pubkey and signature, it verifies true if `message` consists of the characters you posted for `data` **followed by a newline character**. Does your `message` have the newline character? (2) Aside: if you have PEM in `String`, you don't need to convert to bytes, create BAIS and then ISR; you can just do `new StringReader(param)` . – dave_thompson_085 Apr 30 '19 at 22:43
  • Yes that the problem the newline echo -n solve the problem – anish May 02 '19 at 05:12

0 Answers0