cash_addr.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* Copyright (c) 2017 Jochen Hoenicke
  2. * based on code Copyright (c) 2017 Peter Wuille
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include "cash_addr.h"
  26. #define MAX_CASHADDR_SIZE 129
  27. #define MAX_BASE32_SIZE 104
  28. #define MAX_DATA_SIZE 65
  29. #define MAX_HRP_SIZE 20
  30. #define CHECKSUM_SIZE 8
  31. uint64_t cashaddr_polymod_step(uint64_t pre) {
  32. uint8_t b = pre >> 35;
  33. return ((pre & 0x7FFFFFFFFULL) << 5) ^ (-((b >> 0) & 1) & 0x98f2bc8e61ULL) ^
  34. (-((b >> 1) & 1) & 0x79b76d99e2ULL) ^
  35. (-((b >> 2) & 1) & 0xf33e5fb3c4ULL) ^
  36. (-((b >> 3) & 1) & 0xae2eabe2a8ULL) ^
  37. (-((b >> 4) & 1) & 0x1e4f43e470ULL);
  38. }
  39. static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
  40. static const int8_t charset_rev[128] = {
  41. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  42. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  43. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, -1, 10, 17, 21, 20, 26, 30, 7,
  44. 5, -1, -1, -1, -1, -1, -1, -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22,
  45. 31, 27, 19, -1, 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1,
  46. -1, -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 1, 0,
  47. 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1};
  48. int cash_encode(char* output, const char* hrp, const uint8_t* data,
  49. size_t data_len) {
  50. uint64_t chk = 1;
  51. size_t i = 0;
  52. while (hrp[i] != 0) {
  53. int ch = hrp[i];
  54. if (ch < 33 || ch > 126) {
  55. return 0;
  56. }
  57. *(output++) = ch;
  58. chk = cashaddr_polymod_step(chk) ^ (ch & 0x1f);
  59. ++i;
  60. }
  61. if (i + 1 + data_len + CHECKSUM_SIZE > MAX_CASHADDR_SIZE) {
  62. return 0;
  63. }
  64. chk = cashaddr_polymod_step(chk);
  65. *(output++) = ':';
  66. for (i = 0; i < data_len; ++i) {
  67. if (*data >> 5) return 0;
  68. chk = cashaddr_polymod_step(chk) ^ (*data);
  69. *(output++) = charset[*(data++)];
  70. }
  71. for (i = 0; i < CHECKSUM_SIZE; ++i) {
  72. chk = cashaddr_polymod_step(chk);
  73. }
  74. chk ^= 1;
  75. for (i = 0; i < CHECKSUM_SIZE; ++i) {
  76. *(output++) = charset[(chk >> ((CHECKSUM_SIZE - 1 - i) * 5)) & 0x1f];
  77. }
  78. *output = 0;
  79. return 1;
  80. }
  81. int cash_decode(char* hrp, uint8_t* data, size_t* data_len, const char* input) {
  82. uint64_t chk = 1;
  83. size_t i = 0;
  84. size_t input_len = strlen(input);
  85. size_t hrp_len = 0;
  86. int have_lower = 0, have_upper = 0;
  87. if (input_len < CHECKSUM_SIZE || input_len > MAX_CASHADDR_SIZE) {
  88. return 0;
  89. }
  90. *data_len = 0;
  91. while (*data_len < input_len && input[(input_len - 1) - *data_len] != ':') {
  92. ++(*data_len);
  93. }
  94. hrp_len = input_len - (1 + *data_len);
  95. if (1 + *data_len >= input_len || hrp_len > MAX_HRP_SIZE ||
  96. *data_len < CHECKSUM_SIZE ||
  97. *data_len > CHECKSUM_SIZE + MAX_BASE32_SIZE) {
  98. return 0;
  99. }
  100. // subtract checksum
  101. *(data_len) -= CHECKSUM_SIZE;
  102. for (i = 0; i < hrp_len; ++i) {
  103. int ch = input[i];
  104. if (ch < 33 || ch > 126) {
  105. return 0;
  106. }
  107. if (ch >= 'a' && ch <= 'z') {
  108. have_lower = 1;
  109. } else if (ch >= 'A' && ch <= 'Z') {
  110. have_upper = 1;
  111. ch = (ch - 'A') + 'a';
  112. }
  113. hrp[i] = ch;
  114. chk = cashaddr_polymod_step(chk) ^ (ch & 0x1f);
  115. }
  116. hrp[i] = 0;
  117. chk = cashaddr_polymod_step(chk);
  118. ++i;
  119. while (i < input_len) {
  120. int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]];
  121. if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
  122. if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
  123. if (v == -1) {
  124. return 0;
  125. }
  126. chk = cashaddr_polymod_step(chk) ^ v;
  127. if (i + CHECKSUM_SIZE < input_len) {
  128. data[i - (1 + hrp_len)] = v;
  129. }
  130. ++i;
  131. }
  132. if (have_lower && have_upper) {
  133. return 0;
  134. }
  135. return chk == 1;
  136. }
  137. static int convert_bits(uint8_t* out, size_t* outlen, int outbits,
  138. const uint8_t* in, size_t inlen, int inbits, int pad) {
  139. uint32_t val = 0;
  140. int bits = 0;
  141. uint32_t maxv = (((uint32_t)1) << outbits) - 1;
  142. while (inlen--) {
  143. val = (val << inbits) | *(in++);
  144. bits += inbits;
  145. while (bits >= outbits) {
  146. bits -= outbits;
  147. out[(*outlen)++] = (val >> bits) & maxv;
  148. }
  149. }
  150. if (pad) {
  151. if (bits) {
  152. out[(*outlen)++] = (val << (outbits - bits)) & maxv;
  153. }
  154. } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) {
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. int cash_addr_encode(char* output, const char* hrp, const uint8_t* data,
  160. size_t data_len) {
  161. uint8_t base32[MAX_BASE32_SIZE] = {0};
  162. size_t base32len = 0;
  163. if (data_len < 2 || data_len > MAX_DATA_SIZE) return 0;
  164. convert_bits(base32, &base32len, 5, data, data_len, 8, 1);
  165. return cash_encode(output, hrp, base32, base32len);
  166. }
  167. int cash_addr_decode(uint8_t* witdata, size_t* witdata_len, const char* hrp,
  168. const char* addr) {
  169. uint8_t data[MAX_BASE32_SIZE] = {0};
  170. char hrp_actual[MAX_HRP_SIZE + 1] = {0};
  171. size_t data_len = 0;
  172. if (!cash_decode(hrp_actual, data, &data_len, addr)) return 0;
  173. if (data_len == 0 || data_len > MAX_BASE32_SIZE) return 0;
  174. if (strncmp(hrp, hrp_actual, MAX_HRP_SIZE + 1) != 0) return 0;
  175. *witdata_len = 0;
  176. if (!convert_bits(witdata, witdata_len, 8, data, data_len, 5, 0)) return 0;
  177. if (*witdata_len < 2 || *witdata_len > MAX_DATA_SIZE) return 0;
  178. return 1;
  179. }