2014-05-22 19:52:04 +00:00
|
|
|
/**
|
2014-12-23 00:17:58 +00:00
|
|
|
* Copyright (c) 2012-2014 Luke Dashjr
|
2014-05-22 19:52:04 +00:00
|
|
|
* Copyright (c) 2013-2014 Pavol Rusnak
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
#include "base58.h"
|
2014-12-23 00:17:58 +00:00
|
|
|
#include <stdbool.h>
|
2019-03-29 14:44:55 +00:00
|
|
|
#include <string.h>
|
2018-01-18 14:18:09 +00:00
|
|
|
#include "memzero.h"
|
2019-03-29 14:44:55 +00:00
|
|
|
#include "ripemd160.h"
|
|
|
|
#include "sha2.h"
|
2014-05-22 19:52:04 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const char b58digits_ordered[] =
|
|
|
|
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
2018-07-05 00:10:34 +00:00
|
|
|
const int8_t b58digits_map[] = {
|
2019-03-29 14:44:55 +00:00
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7,
|
|
|
|
8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18,
|
|
|
|
19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1,
|
|
|
|
-1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48,
|
|
|
|
49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
|
2014-12-23 00:17:58 +00:00
|
|
|
};
|
|
|
|
|
2019-09-29 08:46:21 +00:00
|
|
|
typedef uint64_t b58_maxint_t;
|
|
|
|
typedef uint32_t b58_almostmaxint_t;
|
|
|
|
#define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8)
|
|
|
|
static const b58_almostmaxint_t b58_almostmaxint_mask =
|
|
|
|
((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
bool b58tobin(void *bin, size_t *binszp, const char *b58) {
|
|
|
|
size_t binsz = *binszp;
|
2018-10-23 16:01:44 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (binsz == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-23 16:01:44 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const unsigned char *b58u = (const unsigned char *)b58;
|
|
|
|
unsigned char *binu = bin;
|
2019-09-29 08:46:21 +00:00
|
|
|
size_t outisz =
|
|
|
|
(binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
|
|
|
|
b58_almostmaxint_t outi[outisz];
|
2019-10-02 14:31:36 +00:00
|
|
|
b58_maxint_t t = 0;
|
|
|
|
b58_almostmaxint_t c = 0;
|
|
|
|
size_t i = 0, j = 0;
|
2019-09-29 08:46:21 +00:00
|
|
|
uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
|
|
|
|
b58_almostmaxint_t zeromask =
|
|
|
|
bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
|
2019-03-29 14:44:55 +00:00
|
|
|
unsigned zerocount = 0;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-09-29 08:46:21 +00:00
|
|
|
size_t b58sz = strlen(b58);
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
memzero(outi, sizeof(outi));
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
// Leading zeros, just count
|
|
|
|
for (i = 0; i < b58sz && b58u[i] == '1'; ++i) ++zerocount;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (; i < b58sz; ++i) {
|
|
|
|
if (b58u[i] & 0x80)
|
|
|
|
// High-bit set on invalid digit
|
|
|
|
return false;
|
|
|
|
if (b58digits_map[b58u[i]] == -1)
|
|
|
|
// Invalid base58 digit
|
|
|
|
return false;
|
|
|
|
c = (unsigned)b58digits_map[b58u[i]];
|
|
|
|
for (j = outisz; j--;) {
|
2019-09-29 08:46:21 +00:00
|
|
|
t = ((b58_maxint_t)outi[j]) * 58 + c;
|
|
|
|
c = t >> b58_almostmaxint_bits;
|
|
|
|
outi[j] = t & b58_almostmaxint_mask;
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
|
|
|
if (c)
|
|
|
|
// Output number too big (carry to the next int32)
|
|
|
|
return false;
|
|
|
|
if (outi[0] & zeromask)
|
|
|
|
// Output number too big (last int32 filled too far)
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
j = 0;
|
2019-09-29 08:46:21 +00:00
|
|
|
if (bytesleft) {
|
|
|
|
for (i = bytesleft; i > 0; --i) {
|
|
|
|
*(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff;
|
|
|
|
}
|
|
|
|
++j;
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (; j < outisz; ++j) {
|
2019-09-29 08:46:21 +00:00
|
|
|
for (i = sizeof(*outi); i > 0; --i) {
|
|
|
|
*(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff;
|
|
|
|
}
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
// Count canonical base58 byte count
|
|
|
|
binu = bin;
|
|
|
|
for (i = 0; i < binsz; ++i) {
|
|
|
|
if (binu[i]) {
|
|
|
|
if (zerocount > i) {
|
|
|
|
/* result too large */
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-29 08:46:21 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
--*binszp;
|
|
|
|
}
|
|
|
|
*binszp += zerocount;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return true;
|
2014-12-23 00:17:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
int b58check(const void *bin, size_t binsz, HasherType hasher_type,
|
|
|
|
const char *base58str) {
|
2019-10-02 14:31:36 +00:00
|
|
|
unsigned char buf[32] = {0};
|
2019-03-29 14:44:55 +00:00
|
|
|
const uint8_t *binc = bin;
|
2019-10-02 14:31:36 +00:00
|
|
|
unsigned i = 0;
|
2019-03-29 14:44:55 +00:00
|
|
|
if (binsz < 4) return -4;
|
|
|
|
hasher_Raw(hasher_type, bin, binsz - 4, buf);
|
|
|
|
if (memcmp(&binc[binsz - 4], buf, 4)) return -1;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
// Check number of zeros is correct AFTER verifying checksum (to avoid
|
|
|
|
// possibility of accessing base58str beyond the end)
|
|
|
|
for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) {
|
|
|
|
} // Just finding the end of zeros, nothing to do in loop
|
|
|
|
if (binc[i] == '\0' || base58str[i] == '1') return -3;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return binc[0];
|
2014-12-23 00:17:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
|
|
|
|
const uint8_t *bin = data;
|
2019-10-02 14:31:36 +00:00
|
|
|
int carry = 0;
|
|
|
|
size_t i = 0, j = 0, high = 0, zcount = 0;
|
|
|
|
size_t size = 0;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-09-29 08:46:21 +00:00
|
|
|
while (zcount < binsz && !bin[zcount]) ++zcount;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
size = (binsz - zcount) * 138 / 100 + 1;
|
|
|
|
uint8_t buf[size];
|
|
|
|
memzero(buf, size);
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-09-29 08:46:21 +00:00
|
|
|
for (i = zcount, high = size - 1; i < binsz; ++i, high = j) {
|
2019-03-29 14:44:55 +00:00
|
|
|
for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
|
|
|
|
carry += 256 * buf[j];
|
|
|
|
buf[j] = carry % 58;
|
|
|
|
carry /= 58;
|
2019-09-29 08:46:21 +00:00
|
|
|
if (!j) {
|
|
|
|
// Otherwise j wraps to maxint which is > high
|
|
|
|
break;
|
|
|
|
}
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-09-29 08:46:21 +00:00
|
|
|
for (j = 0; j < size && !buf[j]; ++j)
|
2019-03-29 14:44:55 +00:00
|
|
|
;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (*b58sz <= zcount + size - j) {
|
|
|
|
*b58sz = zcount + size - j + 1;
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (zcount) memset(b58, '1', zcount);
|
2019-09-29 08:46:21 +00:00
|
|
|
for (i = zcount; j < size; ++i, ++j) b58[i] = b58digits_ordered[buf[j]];
|
2019-03-29 14:44:55 +00:00
|
|
|
b58[i] = '\0';
|
|
|
|
*b58sz = i + 1;
|
2014-12-23 00:17:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return true;
|
2014-12-23 00:17:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
int base58_encode_check(const uint8_t *data, int datalen,
|
|
|
|
HasherType hasher_type, char *str, int strsize) {
|
|
|
|
if (datalen > 128) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t buf[datalen + 32];
|
2019-10-04 13:11:46 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2019-03-29 14:44:55 +00:00
|
|
|
uint8_t *hash = buf + datalen;
|
|
|
|
memcpy(buf, data, datalen);
|
|
|
|
hasher_Raw(hasher_type, data, datalen, hash);
|
|
|
|
size_t res = strsize;
|
|
|
|
bool success = b58enc(str, &res, buf, datalen + 4);
|
|
|
|
memzero(buf, sizeof(buf));
|
|
|
|
return success ? res : 0;
|
2014-05-22 19:52:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data,
|
|
|
|
int datalen) {
|
|
|
|
if (datalen > 128) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t d[datalen + 4];
|
2019-10-04 13:11:46 +00:00
|
|
|
memset(d, 0, sizeof(d));
|
2019-03-29 14:44:55 +00:00
|
|
|
size_t res = datalen + 4;
|
|
|
|
if (b58tobin(d, &res, str) != true) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t *nd = d + datalen + 4 - res;
|
|
|
|
if (b58check(nd, res, hasher_type, str) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(data, nd, res - 4);
|
|
|
|
return res - 4;
|
2014-05-22 19:52:04 +00:00
|
|
|
}
|
2016-09-27 20:48:36 +00:00
|
|
|
|
|
|
|
#if USE_GRAPHENE
|
2019-03-29 14:44:55 +00:00
|
|
|
int b58gphcheck(const void *bin, size_t binsz, const char *base58str) {
|
2019-10-02 14:31:36 +00:00
|
|
|
unsigned char buf[32] = {0};
|
2019-03-29 14:44:55 +00:00
|
|
|
const uint8_t *binc = bin;
|
2019-10-02 14:31:36 +00:00
|
|
|
unsigned i = 0;
|
2019-03-29 14:44:55 +00:00
|
|
|
if (binsz < 4) return -4;
|
|
|
|
ripemd160(bin, binsz - 4, buf); // No double SHA256, but a single RIPEMD160
|
|
|
|
if (memcmp(&binc[binsz - 4], buf, 4)) return -1;
|
2016-09-27 20:48:36 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
// Check number of zeros is correct AFTER verifying checksum (to avoid
|
|
|
|
// possibility of accessing base58str beyond the end)
|
|
|
|
for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) {
|
|
|
|
} // Just finding the end of zeros, nothing to do in loop
|
|
|
|
if (binc[i] == '\0' || base58str[i] == '1') return -3;
|
2016-09-27 20:48:36 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return binc[0];
|
2016-09-27 20:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
int base58gph_encode_check(const uint8_t *data, int datalen, char *str,
|
|
|
|
int strsize) {
|
|
|
|
if (datalen > 128) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t buf[datalen + 32];
|
2019-10-04 13:11:46 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2019-03-29 14:44:55 +00:00
|
|
|
uint8_t *hash = buf + datalen;
|
|
|
|
memcpy(buf, data, datalen);
|
|
|
|
ripemd160(data, datalen, hash); // No double SHA256, but a single RIPEMD160
|
|
|
|
size_t res = strsize;
|
|
|
|
bool success = b58enc(str, &res, buf, datalen + 4);
|
|
|
|
memzero(buf, sizeof(buf));
|
|
|
|
return success ? res : 0;
|
2016-09-27 20:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
int base58gph_decode_check(const char *str, uint8_t *data, int datalen) {
|
|
|
|
if (datalen > 128) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t d[datalen + 4];
|
2019-10-04 13:11:46 +00:00
|
|
|
memset(d, 0, sizeof(d));
|
2019-03-29 14:44:55 +00:00
|
|
|
size_t res = datalen + 4;
|
|
|
|
if (b58tobin(d, &res, str) != true) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
uint8_t *nd = d + datalen + 4 - res;
|
|
|
|
if (b58gphcheck(nd, res, str) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(data, nd, res - 4);
|
|
|
|
return res - 4;
|
2016-09-27 20:48:36 +00:00
|
|
|
}
|
|
|
|
#endif
|