Search code examples
copensslecdsa

How can I use keys from OpenSSL for ECDSA (prime256v1) with easy-ecc?


I am trying to use OpenSSL generated keys with Kenneth MacKay's easy-ecc library but I am struggling.

To test, I've generated keys and dumped them:

$ openssl ec -in ec_private.pem -text -noout
read EC key
Private-Key: (256 bit)
priv:
    45:b7:1a:09:1c:2a:1c:78:f8:80:da:94:44:0f:0f:
    3f:f6:a8:93:2d:07:43:c8:2a:9c:58:25:49:1e:a5:
    b2:68
pub: 
    04:ae:ef:df:ff:85:ee:59:00:fd:43:52:10:14:77:
    23:c8:e3:ba:00:4d:82:4d:41:15:a4:c5:09:ec:96:
    ef:50:73:79:9d:a7:a6:1a:88:a9:b2:26:ac:1e:03:
    50:02:0d:93:47:83:9d:eb:f9:e7:d3:dd:7d:59:04:
    95:a8:fd:bb:cf
ASN1 OID: prime256v1
NIST CURVE: P-256

Imported them into a small harness based on a test in the pr using curve secp256r1,

#include <stdio.h>
#include <string.h>
#include "ecc.h"

int main() {

    uint8_t privatek[] = { 
        0x45, 0xb7, 0x1a, 0x09, 0x1c, 0x2a, 0x1c, 0x78, 
        0xf8, 0x80, 0xda, 0x94, 0x44, 0x0f, 0x0f, 0x3f, 
        0xf6, 0xa8, 0x93, 0x2d, 0x07, 0x43, 0xc8, 0x2a, 
        0x9c, 0x58, 0x25, 0x49, 0x1e, 0xa5, 0xb2, 0x68
    };

    uint8_t publick[] = {
        0xae, 0xef, 0xdf, 0xff, 0x85, 0xee, 0x59, 0x00, 
        0xfd, 0x43, 0x52, 0x10, 0x14, 0x77, 0x23, 0xc8, 
        0xe3, 0xba, 0x00, 0x4d, 0x82, 0x4d, 0x41, 0x15, 
        0xa4, 0xc5, 0x09, 0xec, 0x96, 0xef, 0x50, 0x73, 
        0x79, 0x9d, 0xa7, 0xa6, 0x1a, 0x88, 0xa9, 0xb2, 
        0x26, 0xac, 0x1e, 0x03, 0x50, 0x02, 0x0d, 0x93, 
        0x47, 0x83, 0x9d, 0xeb, 0xf9, 0xe7, 0xd3, 0xdd, 
        0x7d, 0x59, 0x04, 0x95, 0xa8, 0xfd, 0xbb, 0xcf
    };

  uint8_t hash[ECC_BYTES];
  uint8_t sig[ECC_BYTES * 2];

  memcpy(hash, publick, ECC_BYTES);

  if (!ecdsa_sign(privatek, hash, sig)) {
      printf("ECC_sign() failed\n");
  }

  if (!ecdsa_verify(publick, hash, sig)) {
      printf("ECC_verify() failed\n");
  }  
}

but this never passes. I am not sure if I am doing something silly while importing the keys, or something else but this is not passing.

Any ideas as to what I am doing wrong?


Solution

  • I guess I should have waited an hour before posting that question. easy-ecc works with compressed points. I dumped a compressed public key and used that with success.

    There is also a fork by arekinath that supports uncompressed points.

    Now if only I could get my micro-ecc code working...