From c6e7d376b71471af4667ddb285a7109cc25c41c4 Mon Sep 17 00:00:00 2001 From: Jochen Hoenicke Date: Wed, 27 Apr 2016 23:10:35 +0200 Subject: [PATCH] Handle b58 address with shorter lengths --- base58.c | 14 ++++++-------- bip32.c | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/base58.c b/base58.c index d8252aefc7..5355fcb1ee 100644 --- a/base58.c +++ b/base58.c @@ -42,7 +42,7 @@ static const int8_t b58digits_map[] = { bool b58tobin(void *bin, size_t *binszp, const char *b58) { size_t binsz = *binszp; - const unsigned char *b58u = (void*)b58; + const unsigned char *b58u = (const unsigned char*)b58; unsigned char *binu = bin; size_t outisz = (binsz + 3) / 4; uint32_t outi[outisz]; @@ -59,7 +59,7 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58) memset(outi, 0, outisz * sizeof(*outi)); // Leading zeros, just count - for (i = 0; i < b58sz && !b58digits_map[b58u[i]]; ++i) + for (i = 0; i < b58sz && b58u[i] == '1'; ++i) ++zerocount; for ( ; i < b58sz; ++i) @@ -210,12 +210,10 @@ int base58_decode_check(const char *str, uint8_t *data, int datalen) if (b58tobin(d, &res, str) != true) { return 0; } - if (res != (size_t)datalen + 4) { + uint8_t *nd = d + datalen + 4 - res; + if (b58check(nd, res, str) < 0) { return 0; } - if (b58check(d, res, str) < 0) { - return 0; - } - memcpy(data, d, datalen); - return datalen; + memcpy(data, nd, res - 4); + return res - 4; } diff --git a/bip32.c b/bip32.c index 064a5e0c1c..8ecfc6f733 100644 --- a/bip32.c +++ b/bip32.c @@ -400,7 +400,7 @@ int hdnode_deserialize(const char *str, HDNode *node) { uint8_t node_data[78]; memset(node, 0, sizeof(HDNode)); - if (!base58_decode_check(str, node_data, sizeof(node_data))) { + if (base58_decode_check(str, node_data, sizeof(node_data)) != sizeof(node_data)) { return -1; } node->curve = get_curve_by_name(SECP256K1_NAME);