base58.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  30. const int8_t b58digits_map[] = {
  31. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  32. -1, -1, -1, -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, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1, -1, 9,
  34. 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1, 22, 23, 24, 25, 26, 27, 28, 29,
  35. 30, 31, 32, -1, -1, -1, -1, -1, -1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44,
  36. 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
  37. };
  38. typedef uint64_t b58_maxint_t;
  39. typedef uint32_t b58_almostmaxint_t;
  40. #define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8)
  41. static const b58_almostmaxint_t b58_almostmaxint_mask =
  42. ((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
  43. // Decodes a null-terminated Base58 string `b58` to binary and writes the result
  44. // at the end of the buffer `bin` of size `*binszp`. On success `*binszp` is set
  45. // to the number of valid bytes at the end of the buffer.
  46. bool b58tobin(void* bin, size_t* binszp, const char* b58) {
  47. size_t binsz = *binszp;
  48. if(binsz == 0) {
  49. return false;
  50. }
  51. const unsigned char* b58u = (const unsigned char*)b58;
  52. unsigned char* binu = bin;
  53. size_t outisz = (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
  54. b58_almostmaxint_t outi[outisz];
  55. b58_maxint_t t = 0;
  56. b58_almostmaxint_t c = 0;
  57. size_t i = 0, j = 0;
  58. uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
  59. b58_almostmaxint_t zeromask = bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
  60. unsigned zerocount = 0;
  61. size_t b58sz = strlen(b58);
  62. memzero(outi, sizeof(outi));
  63. // Leading zeros, just count
  64. for(i = 0; i < b58sz && b58u[i] == '1'; ++i) ++zerocount;
  65. for(; i < b58sz; ++i) {
  66. if(b58u[i] & 0x80)
  67. // High-bit set on invalid digit
  68. return false;
  69. if(b58digits_map[b58u[i]] == -1)
  70. // Invalid base58 digit
  71. return false;
  72. c = (unsigned)b58digits_map[b58u[i]];
  73. for(j = outisz; j--;) {
  74. t = ((b58_maxint_t)outi[j]) * 58 + c;
  75. c = t >> b58_almostmaxint_bits;
  76. outi[j] = t & b58_almostmaxint_mask;
  77. }
  78. if(c)
  79. // Output number too big (carry to the next int32)
  80. return false;
  81. if(outi[0] & zeromask)
  82. // Output number too big (last int32 filled too far)
  83. return false;
  84. }
  85. j = 0;
  86. if(bytesleft) {
  87. for(i = bytesleft; i > 0; --i) {
  88. *(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff;
  89. }
  90. ++j;
  91. }
  92. for(; j < outisz; ++j) {
  93. for(i = sizeof(*outi); i > 0; --i) {
  94. *(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff;
  95. }
  96. }
  97. // locate the most significant byte
  98. binu = bin;
  99. for(i = 0; i < binsz; ++i) {
  100. if(binu[i]) break;
  101. }
  102. // prepend the correct number of null-bytes
  103. if(zerocount > i) {
  104. /* result too large */
  105. return false;
  106. }
  107. *binszp = binsz - i + zerocount;
  108. return true;
  109. }
  110. int b58check(const void* bin, size_t binsz, HasherType hasher_type, const char* base58str) {
  111. unsigned char buf[32] = {0};
  112. const uint8_t* binc = bin;
  113. unsigned i = 0;
  114. if(binsz < 4) return -4;
  115. hasher_Raw(hasher_type, bin, binsz - 4, buf);
  116. if(memcmp(&binc[binsz - 4], buf, 4)) return -1;
  117. // Check number of zeros is correct AFTER verifying checksum (to avoid
  118. // possibility of accessing base58str beyond the end)
  119. for(i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i) {
  120. } // Just finding the end of zeros, nothing to do in loop
  121. if(binc[i] == '\0' || base58str[i] == '1') return -3;
  122. return binc[0];
  123. }
  124. bool b58enc(char* b58, size_t* b58sz, const void* data, size_t binsz) {
  125. const uint8_t* bin = data;
  126. int carry = 0;
  127. size_t i = 0, j = 0, high = 0, zcount = 0;
  128. size_t size = 0;
  129. while(zcount < binsz && !bin[zcount]) ++zcount;
  130. size = (binsz - zcount) * 138 / 100 + 1;
  131. uint8_t buf[size];
  132. memzero(buf, size);
  133. for(i = zcount, high = size - 1; i < binsz; ++i, high = j) {
  134. for(carry = bin[i], j = size - 1; (j > high) || carry; --j) {
  135. carry += 256 * buf[j];
  136. buf[j] = carry % 58;
  137. carry /= 58;
  138. if(!j) {
  139. // Otherwise j wraps to maxint which is > high
  140. break;
  141. }
  142. }
  143. }
  144. for(j = 0; j < size && !buf[j]; ++j)
  145. ;
  146. if(*b58sz <= zcount + size - j) {
  147. *b58sz = zcount + size - j + 1;
  148. return false;
  149. }
  150. if(zcount) memset(b58, '1', zcount);
  151. for(i = zcount; j < size; ++i, ++j) b58[i] = b58digits_ordered[buf[j]];
  152. b58[i] = '\0';
  153. *b58sz = i + 1;
  154. return true;
  155. }
  156. int base58_encode_check(
  157. const uint8_t* data,
  158. int datalen,
  159. HasherType hasher_type,
  160. char* str,
  161. int strsize) {
  162. if(datalen > 128) {
  163. return 0;
  164. }
  165. uint8_t buf[datalen + 32];
  166. memset(buf, 0, sizeof(buf));
  167. uint8_t* hash = buf + datalen;
  168. memcpy(buf, data, datalen);
  169. hasher_Raw(hasher_type, data, datalen, hash);
  170. size_t res = strsize;
  171. bool success = b58enc(str, &res, buf, datalen + 4);
  172. memzero(buf, sizeof(buf));
  173. return success ? res : 0;
  174. }
  175. int base58_decode_check(const char* str, HasherType hasher_type, uint8_t* data, int datalen) {
  176. if(datalen > 128) {
  177. return 0;
  178. }
  179. uint8_t d[datalen + 4];
  180. memset(d, 0, sizeof(d));
  181. size_t res = datalen + 4;
  182. if(b58tobin(d, &res, str) != true) {
  183. return 0;
  184. }
  185. uint8_t* nd = d + datalen + 4 - res;
  186. if(b58check(nd, res, hasher_type, str) < 0) {
  187. return 0;
  188. }
  189. memcpy(data, nd, res - 4);
  190. return res - 4;
  191. }