base58.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Copyright (c) 2012-2014 Luke Dashjr
  3. * Copyright (c) 2013-2014 Pavol Rusnak
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  19. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "base58.h"
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include "memzero.h"
  27. #include "ripemd160.h"
  28. #include "sha2.h"
  29. const char b58digits_ordered[] =
  30. "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  31. const int8_t b58digits_map[] = {
  32. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  33. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  34. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7,
  35. 8, -1, -1, -1, -1, -1, -1, -1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18,
  36. 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1,
  37. -1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46, 47, 48,
  38. 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
  39. };
  40. typedef uint64_t b58_maxint_t;
  41. typedef uint32_t b58_almostmaxint_t;
  42. #define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8)
  43. static const b58_almostmaxint_t b58_almostmaxint_mask =
  44. ((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
  45. // Decodes a null-terminated Base58 string `b58` to binary and writes the result
  46. // at the end of the buffer `bin` of size `*binszp`. On success `*binszp` is set
  47. // to the number of valid bytes at the end of the buffer.
  48. bool b58tobin(void *bin, size_t *binszp, const char *b58) {
  49. size_t binsz = *binszp;
  50. if (binsz == 0) {
  51. return false;
  52. }
  53. const unsigned char *b58u = (const unsigned char *)b58;
  54. unsigned char *binu = bin;
  55. size_t outisz =
  56. (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
  57. b58_almostmaxint_t outi[outisz];
  58. b58_maxint_t t = 0;
  59. b58_almostmaxint_t c = 0;
  60. size_t i = 0, j = 0;
  61. uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
  62. b58_almostmaxint_t zeromask =
  63. bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
  64. unsigned zerocount = 0;
  65. size_t b58sz = strlen(b58);
  66. memzero(outi, sizeof(outi));
  67. // Leading zeros, just count
  68. for (i = 0; i < b58sz && b58u[i] == '1'; ++i) ++zerocount;
  69. for (; i < b58sz; ++i) {
  70. if (b58u[i] & 0x80)
  71. // High-bit set on invalid digit
  72. return false;
  73. if (b58digits_map[b58u[i]] == -1)
  74. // Invalid base58 digit
  75. return false;
  76. c = (unsigned)b58digits_map[b58u[i]];
  77. for (j = outisz; j--;) {
  78. t = ((b58_maxint_t)outi[j]) * 58 + c;
  79. c = t >> b58_almostmaxint_bits;
  80. outi[j] = t & b58_almostmaxint_mask;
  81. }
  82. if (c)
  83. // Output number too big (carry to the next int32)
  84. return false;
  85. if (outi[0] & zeromask)
  86. // Output number too big (last int32 filled too far)
  87. return false;
  88. }
  89. j = 0;
  90. if (bytesleft) {
  91. for (i = bytesleft; i > 0; --i) {
  92. *(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff;
  93. }
  94. ++j;
  95. }
  96. for (; j < outisz; ++j) {
  97. for (i = sizeof(*outi); i > 0; --i) {
  98. *(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff;
  99. }
  100. }
  101. // locate the most significant byte
  102. binu = bin;
  103. for (i = 0; i < binsz; ++i) {
  104. if (binu[i]) break;
  105. }
  106. // prepend the correct number of null-bytes
  107. if (zerocount > i) {
  108. /* result too large */
  109. return false;
  110. }
  111. *binszp = binsz - i + zerocount;
  112. return true;
  113. }
  114. int b58check(const void *bin, size_t binsz, HasherType hasher_type,
  115. const char *base58str) {
  116. unsigned char buf[32] = {0};
  117. const uint8_t *binc = bin;
  118. unsigned i = 0;
  119. if (binsz < 4) return -4;
  120. hasher_Raw(hasher_type, bin, binsz - 4, buf);
  121. if (memcmp(&binc[binsz - 4], buf, 4)) return -1;
  122. // Check number of zeros is correct AFTER verifying checksum (to avoid
  123. // possibility of accessing base58str beyond the end)
  124. for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) {
  125. } // Just finding the end of zeros, nothing to do in loop
  126. if (binc[i] == '\0' || base58str[i] == '1') return -3;
  127. return binc[0];
  128. }
  129. bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz) {
  130. const uint8_t *bin = data;
  131. int carry = 0;
  132. size_t i = 0, j = 0, high = 0, zcount = 0;
  133. size_t size = 0;
  134. while (zcount < binsz && !bin[zcount]) ++zcount;
  135. size = (binsz - zcount) * 138 / 100 + 1;
  136. uint8_t buf[size];
  137. memzero(buf, size);
  138. for (i = zcount, high = size - 1; i < binsz; ++i, high = j) {
  139. for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
  140. carry += 256 * buf[j];
  141. buf[j] = carry % 58;
  142. carry /= 58;
  143. if (!j) {
  144. // Otherwise j wraps to maxint which is > high
  145. break;
  146. }
  147. }
  148. }
  149. for (j = 0; j < size && !buf[j]; ++j)
  150. ;
  151. if (*b58sz <= zcount + size - j) {
  152. *b58sz = zcount + size - j + 1;
  153. return false;
  154. }
  155. if (zcount) memset(b58, '1', zcount);
  156. for (i = zcount; j < size; ++i, ++j) b58[i] = b58digits_ordered[buf[j]];
  157. b58[i] = '\0';
  158. *b58sz = i + 1;
  159. return true;
  160. }
  161. int base58_encode_check(const uint8_t *data, int datalen,
  162. HasherType hasher_type, char *str, int strsize) {
  163. if (datalen > 128) {
  164. return 0;
  165. }
  166. uint8_t buf[datalen + 32];
  167. memset(buf, 0, sizeof(buf));
  168. uint8_t *hash = buf + datalen;
  169. memcpy(buf, data, datalen);
  170. hasher_Raw(hasher_type, data, datalen, hash);
  171. size_t res = strsize;
  172. bool success = b58enc(str, &res, buf, datalen + 4);
  173. memzero(buf, sizeof(buf));
  174. return success ? res : 0;
  175. }
  176. int base58_decode_check(const char *str, HasherType hasher_type, uint8_t *data,
  177. int datalen) {
  178. if (datalen > 128) {
  179. return 0;
  180. }
  181. uint8_t d[datalen + 4];
  182. memset(d, 0, sizeof(d));
  183. size_t res = datalen + 4;
  184. if (b58tobin(d, &res, str) != true) {
  185. return 0;
  186. }
  187. uint8_t *nd = d + datalen + 4 - res;
  188. if (b58check(nd, res, hasher_type, str) < 0) {
  189. return 0;
  190. }
  191. memcpy(data, nd, res - 4);
  192. return res - 4;
  193. }