base58.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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[] = {
  46. 0, 2, 3, 5, 6, 7, 9, 10, full_encoded_block_size};
  47. const size_t full_block_size =
  48. sizeof(encoded_block_sizes) / sizeof(encoded_block_sizes[0]) - 1;
  49. const size_t addr_checksum_size = 4;
  50. const size_t max_bin_data_size = 72;
  51. const int decoded_block_sizes[] = {0, -1, 1, 2, -1, 3, 4, 5, -1, 6, 7, 8};
  52. #define reverse_alphabet(letter) ((int8_t)b58digits_map[(int)letter])
  53. uint64_t uint_8be_to_64(const uint8_t *data, size_t size) {
  54. assert(1 <= size && size <= sizeof(uint64_t));
  55. uint64_t res = 0;
  56. switch (9 - size) {
  57. case 1:
  58. res |= *data++; /* FALLTHRU */
  59. case 2:
  60. res <<= 8;
  61. res |= *data++; /* FALLTHRU */
  62. case 3:
  63. res <<= 8;
  64. res |= *data++; /* FALLTHRU */
  65. case 4:
  66. res <<= 8;
  67. res |= *data++; /* FALLTHRU */
  68. case 5:
  69. res <<= 8;
  70. res |= *data++; /* FALLTHRU */
  71. case 6:
  72. res <<= 8;
  73. res |= *data++; /* FALLTHRU */
  74. case 7:
  75. res <<= 8;
  76. res |= *data++; /* FALLTHRU */
  77. case 8:
  78. res <<= 8;
  79. res |= *data;
  80. break;
  81. default:
  82. assert(false);
  83. }
  84. return res;
  85. }
  86. void uint_64_to_8be(uint64_t num, size_t size, uint8_t *data) {
  87. assert(1 <= size && size <= sizeof(uint64_t));
  88. #if BYTE_ORDER == LITTLE_ENDIAN
  89. uint64_t num_be = SWAP64(num);
  90. #else
  91. uint64_t num_be = num;
  92. #endif
  93. memcpy(data, (uint8_t *)(&num_be) + sizeof(uint64_t) - size, size);
  94. }
  95. void encode_block(const char *block, size_t size, char *res) {
  96. assert(1 <= size && size <= full_block_size);
  97. uint64_t num = uint_8be_to_64((uint8_t *)(block), size);
  98. int i = ((int)(encoded_block_sizes[size])) - 1;
  99. while (0 <= i) {
  100. uint64_t remainder = num % alphabet_size;
  101. num /= alphabet_size;
  102. res[i] = b58digits_ordered[remainder];
  103. --i;
  104. }
  105. }
  106. bool decode_block(const char *block, size_t size, char *res) {
  107. assert(1 <= size && size <= full_encoded_block_size);
  108. int res_size = decoded_block_sizes[size];
  109. if (res_size <= 0) {
  110. return false; // Invalid block size
  111. }
  112. uint64_t res_num = 0;
  113. uint64_t order = 1;
  114. for (size_t i = size - 1; i < size; --i) {
  115. if (block[i] & 0x80) {
  116. return false; // Invalid symbol
  117. }
  118. int digit = reverse_alphabet(block[i]);
  119. if (digit < 0) {
  120. return false; // Invalid symbol
  121. }
  122. uint64_t product_hi = 0;
  123. uint64_t tmp = res_num + mul128(order, (uint64_t)digit, &product_hi);
  124. if (tmp < res_num || 0 != product_hi) {
  125. return false; // Overflow
  126. }
  127. res_num = tmp;
  128. // The original code comment for the order multiplication says
  129. // "Never overflows, 58^10 < 2^64"
  130. // This is incorrect since it overflows on the 11th iteration
  131. // However, there is no negative impact since the result is unused
  132. order *= alphabet_size;
  133. }
  134. if ((size_t)res_size < full_block_size &&
  135. (UINT64_C(1) << (8 * res_size)) <= res_num)
  136. return false; // Overflow
  137. uint_64_to_8be(res_num, res_size, (uint8_t *)(res));
  138. return true;
  139. }
  140. bool xmr_base58_encode(char *b58, size_t *b58sz, const void *data,
  141. size_t binsz) {
  142. if (binsz == 0) {
  143. if (b58sz) {
  144. *b58sz = 0;
  145. }
  146. return true;
  147. }
  148. const char *data_bin = data;
  149. size_t full_block_count = binsz / full_block_size;
  150. size_t last_block_size = binsz % full_block_size;
  151. size_t res_size = full_block_count * full_encoded_block_size +
  152. encoded_block_sizes[last_block_size];
  153. if (b58sz) {
  154. if (res_size > *b58sz) {
  155. return false;
  156. }
  157. *b58sz = res_size;
  158. }
  159. for (size_t i = 0; i < full_block_count; ++i) {
  160. encode_block(data_bin + i * full_block_size, full_block_size,
  161. b58 + i * full_encoded_block_size);
  162. }
  163. if (0 < last_block_size) {
  164. encode_block(data_bin + full_block_count * full_block_size, last_block_size,
  165. b58 + full_block_count * full_encoded_block_size);
  166. }
  167. return true;
  168. }
  169. bool xmr_base58_decode(const char *b58, size_t b58sz, void *data,
  170. size_t *binsz) {
  171. if (b58sz == 0) {
  172. *binsz = 0;
  173. return true;
  174. }
  175. size_t full_block_count = b58sz / full_encoded_block_size;
  176. size_t last_block_size = b58sz % full_encoded_block_size;
  177. int last_block_decoded_size = decoded_block_sizes[last_block_size];
  178. if (last_block_decoded_size < 0) {
  179. *binsz = 0;
  180. return false; // Invalid enc length
  181. }
  182. size_t data_size =
  183. full_block_count * full_block_size + last_block_decoded_size;
  184. if (*binsz < data_size) {
  185. *binsz = 0;
  186. return false;
  187. }
  188. char *data_bin = data;
  189. for (size_t i = 0; i < full_block_count; ++i) {
  190. if (!decode_block(b58 + i * full_encoded_block_size,
  191. full_encoded_block_size,
  192. data_bin + i * full_block_size)) {
  193. *binsz = 0;
  194. return false;
  195. }
  196. }
  197. if (0 < last_block_size) {
  198. if (!decode_block(b58 + full_block_count * full_encoded_block_size,
  199. last_block_size,
  200. data_bin + full_block_count * full_block_size)) {
  201. *binsz = 0;
  202. return false;
  203. }
  204. }
  205. *binsz = data_size;
  206. return true;
  207. }
  208. int xmr_base58_addr_encode_check(uint64_t tag, const uint8_t *data,
  209. size_t binsz, char *b58, size_t b58sz) {
  210. if (binsz > max_bin_data_size || tag > 127) { // tag varint
  211. return false;
  212. }
  213. size_t b58size = b58sz;
  214. uint8_t buf[(binsz + 1) + HASHER_DIGEST_LENGTH];
  215. memset(buf, 0, sizeof(buf));
  216. uint8_t *hash = buf + binsz + 1;
  217. buf[0] = (uint8_t)tag;
  218. memcpy(buf + 1, data, binsz);
  219. hasher_Raw(HASHER_SHA3K, buf, binsz + 1, hash);
  220. bool r =
  221. xmr_base58_encode(b58, &b58size, buf, binsz + 1 + addr_checksum_size);
  222. return (int)(!r ? 0 : b58size);
  223. }
  224. int xmr_base58_addr_decode_check(const char *addr, size_t sz, uint64_t *tag,
  225. void *data, size_t datalen) {
  226. size_t buflen = 1 + max_bin_data_size + addr_checksum_size;
  227. uint8_t buf[buflen];
  228. memset(buf, 0, sizeof(buf));
  229. uint8_t hash[HASHER_DIGEST_LENGTH] = {0};
  230. if (!xmr_base58_decode(addr, sz, buf, &buflen)) {
  231. return 0;
  232. }
  233. if (buflen <= addr_checksum_size + 1) {
  234. return 0;
  235. }
  236. size_t res_size = buflen - addr_checksum_size - 1;
  237. if (datalen < res_size) {
  238. return 0;
  239. }
  240. hasher_Raw(HASHER_SHA3K, buf, buflen - addr_checksum_size, hash);
  241. if (memcmp(hash, buf + buflen - addr_checksum_size, addr_checksum_size) !=
  242. 0) {
  243. return 0;
  244. }
  245. *tag = buf[0];
  246. if (*tag > 127) {
  247. return false; // varint
  248. }
  249. memcpy(data, buf + 1, res_size);
  250. return (int)res_size;
  251. }
  252. #endif // USE_MONERO