From 570e081546f4ff2c44e3fde80ba2ba47c89a2a12 Mon Sep 17 00:00:00 2001 From: samr7 Date: Sat, 6 Aug 2011 13:52:47 -0700 Subject: [PATCH] Fix a serious bug in the private key encoder. --- pattern.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pattern.c b/pattern.c index 87c2315..e5653cd 100644 --- a/pattern.c +++ b/pattern.c @@ -124,9 +124,13 @@ vg_encode_privkey(EC_KEY *pkey, int addrtype, char *result) bn = EC_KEY_get0_private_key(pkey); eckey_buf[0] = addrtype; - nbytes = BN_bn2bin(bn, &eckey_buf[1]); + nbytes = BN_num_bytes(bn); + assert(nbytes <= 32); + if (nbytes < 32) + memset(eckey_buf + 1, 0, 32 - nbytes); + BN_bn2bin(bn, &eckey_buf[33 - nbytes]); - encode_b58_check(eckey_buf, nbytes + 1, result); + encode_b58_check(eckey_buf, 33, result); }