Commit 92a64fd5 authored by filatov's avatar filatov
Browse files

fix BN serialization padding (thanks Michal Kazmierowski)

parent e5925bfe
......@@ -19,6 +19,8 @@
#include <openssl/ecdsa.h>
#include <string.h>
#define FIELD_SIZE 32
#define ARRAYSIZE(A) (sizeof(A)/sizeof(A[0]))
static char * _bin2hex(char * hex, int hlen, const char * bin, int blen);
......@@ -172,6 +174,7 @@ int ecc_api_key_public(void* key, char * px, char * py)
BIGNUM x, y;
int bcount = -1;
if( key && px && py ) {
ecgroup = EC_KEY_get0_group(eckey);
ecpoint = EC_KEY_get0_public_key(eckey);
......@@ -179,16 +182,17 @@ int ecc_api_key_public(void* key, char * px, char * py)
BN_init(&x); BN_init(&y);
if (EC_POINT_get_affine_coordinates_GFp(ecgroup, ecpoint, &x, &y, NULL)){
bcount = BN_num_bytes(&x);
if (px){
for(; bcount < FIELD_SIZE; bcount++)
*(px++) = 0; // add padding with zeros
BN_bn2bin(&x, (unsigned char*)px);
}
bcount = BN_num_bytes(&y);
if (py){
for(; bcount < FIELD_SIZE; bcount++)
*(py++) = 0; // add padding with zeros
BN_bn2bin(&y, (unsigned char*)py);
}
}
BN_clear_free(&x); BN_clear_free(&y);
}
return bcount;
}
......@@ -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);
EC_KEY_free(eckey);
if (ecdsa){
int bcount;
int i, bcount;
*(sig++) = 0; // ECC_POINT type (x_coordinate_only)
bcount = BN_num_bytes(ecdsa->r);
for(i=bcount; i < FIELD_SIZE; i++)
*(sig++) = 0; // add padding with zeros
BN_bn2bin(ecdsa->r, sig);
sig += bcount;
bcount = BN_num_bytes(ecdsa->s);
for(i=bcount; i < FIELD_SIZE; i++)
*(sig++) = 0; // add padding with zeros
BN_bn2bin(ecdsa->s, sig);
sig += bcount;
ECDSA_SIG_free(ecdsa);
......
Supports Markdown
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