Commit 92a64fd5 authored by filatov's avatar filatov

fix BN serialization padding (thanks Michal Kazmierowski)

parent e5925bfe
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include <openssl/ecdsa.h> #include <openssl/ecdsa.h>
#include <string.h> #include <string.h>
#define FIELD_SIZE 32
#define ARRAYSIZE(A) (sizeof(A)/sizeof(A[0])) #define ARRAYSIZE(A) (sizeof(A)/sizeof(A[0]))
static char * _bin2hex(char * hex, int hlen, const char * bin, int blen); static char * _bin2hex(char * hex, int hlen, const char * bin, int blen);
...@@ -172,23 +174,25 @@ int ecc_api_key_public(void* key, char * px, char * py) ...@@ -172,23 +174,25 @@ int ecc_api_key_public(void* key, char * px, char * py)
BIGNUM x, y; BIGNUM x, y;
int bcount = -1; int bcount = -1;
ecgroup = EC_KEY_get0_group(eckey); if( key && px && py ) {
ecpoint = EC_KEY_get0_public_key(eckey); ecgroup = EC_KEY_get0_group(eckey);
ecpoint = EC_KEY_get0_public_key(eckey);
//fill public key data //fill public key data
BN_init(&x); BN_init(&y); BN_init(&x); BN_init(&y);
if (EC_POINT_get_affine_coordinates_GFp(ecgroup, ecpoint, &x, &y, NULL)){ if (EC_POINT_get_affine_coordinates_GFp(ecgroup, ecpoint, &x, &y, NULL)){
bcount = BN_num_bytes(&x); bcount = BN_num_bytes(&x);
if (px){ for(; bcount < FIELD_SIZE; bcount++)
*(px++) = 0; // add padding with zeros
BN_bn2bin(&x, (unsigned char*)px); BN_bn2bin(&x, (unsigned char*)px);
}
bcount = BN_num_bytes(&y);
bcount = BN_num_bytes(&y); for(; bcount < FIELD_SIZE; bcount++)
if (py){ *(py++) = 0; // add padding with zeros
BN_bn2bin(&y, (unsigned char*)py); BN_bn2bin(&y, (unsigned char*)py);
} }
BN_clear_free(&x); BN_clear_free(&y);
} }
BN_clear_free(&x); BN_clear_free(&y);
return bcount; return bcount;
} }
...@@ -446,12 +450,16 @@ int ecc_sign(void * key, const char * data, int length, char ** psig, int max ...@@ -446,12 +450,16 @@ int ecc_sign(void * key, const char * data, int length, char ** psig, int max
ecdsa = ECDSA_do_sign(hash, 32, eckey); ecdsa = ECDSA_do_sign(hash, 32, eckey);
EC_KEY_free(eckey); EC_KEY_free(eckey);
if (ecdsa){ if (ecdsa){
int bcount; int i, bcount;
*(sig++) = 0; // ECC_POINT type (x_coordinate_only) *(sig++) = 0; // ECC_POINT type (x_coordinate_only)
bcount = BN_num_bytes(ecdsa->r); bcount = BN_num_bytes(ecdsa->r);
for(i=bcount; i < FIELD_SIZE; i++)
*(sig++) = 0; // add padding with zeros
BN_bn2bin(ecdsa->r, sig); BN_bn2bin(ecdsa->r, sig);
sig += bcount; sig += bcount;
bcount = BN_num_bytes(ecdsa->s); bcount = BN_num_bytes(ecdsa->s);
for(i=bcount; i < FIELD_SIZE; i++)
*(sig++) = 0; // add padding with zeros
BN_bn2bin(ecdsa->s, sig); BN_bn2bin(ecdsa->s, sig);
sig += bcount; sig += bcount;
ECDSA_SIG_free(ecdsa); ECDSA_SIG_free(ecdsa);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment