base58.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Copyright (c) 2014-2018, The Monero Project
  2. //
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // 1. Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. //
  11. // 2. Redistributions in binary form must reproduce the above copyright notice,
  12. // this list of conditions and the following disclaimer in the documentation
  13. // and/or other materials provided with the distribution.
  14. //
  15. // 3. Neither the name of the copyright holder nor the names of its contributors
  16. // may be used to endorse or promote products derived from this software
  17. // without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  23. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. // POSSIBILITY OF SUCH DAMAGE.
  30. //
  31. // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote
  32. // developers
  33. #if USE_MONERO
  34. #include "base58.h"
  35. #include <assert.h>
  36. #include <stdbool.h>
  37. #include <string.h>
  38. #include <sys/types.h>
  39. #include "../base58.h"
  40. #include "../byte_order.h"
  41. #include "int_util.h"
  42. #include "../sha2.h"
  43. const size_t alphabet_size = 58; // sizeof(b58digits_ordered) - 1;
  44. const size_t full_encoded_block_size = 11;
  45. const size_t encoded_block_sizes[] = {0, 2, 3, 5, 6, 7, 9, 10, full_encoded_block_size};
  46. const size_t full_block_size = sizeof(encoded_block_sizes) / sizeof(encoded_block_sizes[0]) - 1;
  47. const size_t addr_checksum_size = 4;
  48. const size_t max_bin_data_size = 72;
  49. const int decoded_block_sizes[] = {0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8};
  50. #define reverse_alphabet(letter) ((int8_t)b58digits_map[(int)letter])
  51. uint64_t uint_8be_to_64(const uint8_t* data, size_t size) {
  52. assert(1 <= size && size <= sizeof(uint64_t));
  53. uint64_t res = 0;
  54. switch(9 - size) {
  55. case 1:
  56. res |= *data++; /* FALLTHRU */
  57. case 2:
  58. res <<= 8;
  59. res |= *data++; /* FALLTHRU */
  60. case 3:
  61. res <<= 8;
  62. res |= *data++; /* FALLTHRU */
  63. case 4:
  64. res <<= 8;
  65. res |= *data++; /* FALLTHRU */
  66. case 5:
  67. res <<= 8;
  68. res |= *data++; /* FALLTHRU */
  69. case 6:
  70. res <<= 8;
  71. res |= *data++; /* FALLTHRU */
  72. case 7:
  73. res <<= 8;
  74. res |= *data++; /* FALLTHRU */
  75. case 8:
  76. res <<= 8;
  77. res |= *data;
  78. break;
  79. default:
  80. assert(false);
  81. }
  82. return res;
  83. }
  84. void uint_64_to_8be(uint64_t num, size_t size, uint8_t* data) {
  85. assert(1 <= size && size <= sizeof(uint64_t));
  86. #if BYTE_ORDER == LITTLE_ENDIAN
  87. uint64_t num_be = SWAP64(num);
  88. #else
  89. uint64_t num_be = num;
  90. #endif
  91. memcpy(data, (uint8_t*)(&num_be) + sizeof(uint64_t) - size, size);
  92. }
  93. void encode_block(const char* block, size_t size, char* res) {
  94. assert(1 <= size && size <= full_block_size);
  95. uint64_t num = uint_8be_to_64((uint8_t*)(block), size);
  96. int i = ((int)(encoded_block_sizes[size])) - 1;
  97. while(0 <= i) {
  98. uint64_t remainder = num % alphabet_size;
  99. num /= alphabet_size;
  100. res[i] = b58digits_ordered[remainder];
  101. --i;
  102. }
  103. }
  104. bool decode_block(const char* block, size_t size, char* res) {
  105. assert(1 <= size && size <= full_encoded_block_size);
  106. int res_size = decoded_block_sizes[size];
  107. if(res_size <= 0) {
  108. return false; // Invalid block size
  109. }
  110. uint64_t res_num = 0;
  111. uint64_t order = 1;
  112. for(size_t i = size - 1; i < size; --i) {
  113. if(block[i] & 0x80) {
  114. return false; // Invalid symbol
  115. }
  116. int digit = reverse_alphabet(block[i]);
  117. if(digit < 0) {
  118. return false; // Invalid symbol
  119. }
  120. uint64_t product_hi = 0;
  121. uint64_t tmp = res_num + mul128(order, (uint64_t)digit, &product_hi);
  122. if(tmp < res_num || 0 != product_hi) {
  123. return false; // Overflow
  124. }
  125. res_num = tmp;
  126. // The original code comment for the order multiplication says
  127. // "Never overflows, 58^10 < 2^64"
  128. // This is incorrect since it overflows on the 11th iteration
  129. // However, there is no negative impact since the result is unused
  130. order *= alphabet_size;
  131. }
  132. if((size_t)res_size < full_block_size && (UINT64_C(1) << (8 * res_size)) <= res_num)
  133. return false; // Overflow
  134. uint_64_to_8be(res_num, res_size, (uint8_t*)(res));
  135. return true;
  136. }
  137. bool xmr_base58_encode(char* b58, size_t* b58sz, const void* data, size_t binsz) {
  138. if(binsz == 0) {
  139. if(b58sz) {
  140. *b58sz = 0;
  141. }
  142. return true;
  143. }
  144. const char* data_bin = data;
  145. size_t full_block_count = binsz / full_block_size;
  146. size_t last_block_size = binsz % full_block_size;
  147. size_t res_size =
  148. full_block_count * full_encoded_block_size + encoded_block_sizes[last_block_size];
  149. if(b58sz) {
  150. if(res_size > *b58sz) {
  151. return false;
  152. }
  153. *b58sz = res_size;
  154. }
  155. for(size_t i = 0; i < full_block_count; ++i) {
  156. encode_block(
  157. data_bin + i * full_block_size, full_block_size, b58 + i * full_encoded_block_size);
  158. }
  159. if(0 < last_block_size) {
  160. encode_block(
  161. data_bin + full_block_count * full_block_size,
  162. last_block_size,
  163. b58 + full_block_count * full_encoded_block_size);
  164. }
  165. return true;
  166. }
  167. bool xmr_base58_decode(const char* b58, size_t b58sz, void* data, size_t* binsz) {
  168. if(b58sz == 0) {
  169. *binsz = 0;
  170. return true;
  171. }
  172. size_t full_block_count = b58sz / full_encoded_block_size;
  173. size_t last_block_size = b58sz % full_encoded_block_size;
  174. int last_block_decoded_size = decoded_block_sizes[last_block_size];
  175. if(last_block_decoded_size < 0) {
  176. *binsz = 0;
  177. return false; // Invalid enc length
  178. }
  179. size_t data_size = full_block_count * full_block_size + last_block_decoded_size;
  180. if(*binsz < data_size) {
  181. *binsz = 0;
  182. return false;
  183. }
  184. char* data_bin = data;
  185. for(size_t i = 0; i < full_block_count; ++i) {
  186. if(!decode_block(
  187. b58 + i * full_encoded_block_size,
  188. full_encoded_block_size,
  189. data_bin + i * full_block_size)) {
  190. *binsz = 0;
  191. return false;
  192. }
  193. }
  194. if(0 < last_block_size) {
  195. if(!decode_block(
  196. b58 + full_block_count * full_encoded_block_size,
  197. last_block_size,
  198. data_bin + full_block_count * full_block_size)) {
  199. *binsz = 0;
  200. return false;
  201. }
  202. }
  203. *binsz = data_size;
  204. return true;
  205. }
  206. int xmr_base58_addr_encode_check(
  207. uint64_t tag,
  208. const uint8_t* data,
  209. size_t binsz,
  210. char* b58,
  211. size_t b58sz) {
  212. if(binsz > max_bin_data_size || tag > 127) { // tag varint
  213. return false;
  214. }
  215. size_t b58size = b58sz;
  216. uint8_t buf[(binsz + 1) + HASHER_DIGEST_LENGTH];
  217. memset(buf, 0, sizeof(buf));
  218. uint8_t* hash = buf + binsz + 1;
  219. buf[0] = (uint8_t)tag;
  220. memcpy(buf + 1, data, binsz);
  221. hasher_Raw(HASHER_SHA3K, buf, binsz + 1, hash);
  222. bool r = xmr_base58_encode(b58, &b58size, buf, binsz + 1 + addr_checksum_size);
  223. return (int)(!r ? 0 : b58size);
  224. }
  225. int xmr_base58_addr_decode_check(
  226. const char* addr,
  227. size_t sz,
  228. uint64_t* tag,
  229. void* data,
  230. size_t datalen) {
  231. size_t buflen = 1 + max_bin_data_size + addr_checksum_size;
  232. uint8_t buf[buflen];
  233. memset(buf, 0, sizeof(buf));
  234. uint8_t hash[HASHER_DIGEST_LENGTH] = {0};
  235. if(!xmr_base58_decode(addr, sz, buf, &buflen)) {
  236. return 0;
  237. }
  238. if(buflen <= addr_checksum_size + 1) {
  239. return 0;
  240. }
  241. size_t res_size = buflen - addr_checksum_size - 1;
  242. if(datalen < res_size) {
  243. return 0;
  244. }
  245. hasher_Raw(HASHER_SHA3K, buf, buflen - addr_checksum_size, hash);
  246. if(memcmp(hash, buf + buflen - addr_checksum_size, addr_checksum_size) != 0) {
  247. return 0;
  248. }
  249. *tag = buf[0];
  250. if(*tag > 127) {
  251. return false; // varint
  252. }
  253. memcpy(data, buf + 1, res_size);
  254. return (int)res_size;
  255. }
  256. #endif // USE_MONERO