cash_addr.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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) ^ (-((b >> 2) & 1) & 0xf33e5fb3c4ULL) ^
  35. (-((b >> 3) & 1) & 0xae2eabe2a8ULL) ^ (-((b >> 4) & 1) & 0x1e4f43e470ULL);
  36. }
  37. static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
  38. static const int8_t charset_rev[128] = {
  39. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  40. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  41. -1, -1, -1, -1, 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, -1, 29,
  42. -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 1, 0, 3, 16, 11, 28, 12, 14,
  43. 6, 4, 2, -1, -1, -1, -1, -1, -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27,
  44. 19, -1, 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1};
  45. int cash_encode(char* output, const char* hrp, const uint8_t* data, size_t data_len) {
  46. uint64_t chk = 1;
  47. size_t i = 0;
  48. while(hrp[i] != 0) {
  49. int ch = hrp[i];
  50. if(ch < 33 || ch > 126) {
  51. return 0;
  52. }
  53. *(output++) = ch;
  54. chk = cashaddr_polymod_step(chk) ^ (ch & 0x1f);
  55. ++i;
  56. }
  57. if(i + 1 + data_len + CHECKSUM_SIZE > MAX_CASHADDR_SIZE) {
  58. return 0;
  59. }
  60. chk = cashaddr_polymod_step(chk);
  61. *(output++) = ':';
  62. for(i = 0; i < data_len; ++i) {
  63. if(*data >> 5) return 0;
  64. chk = cashaddr_polymod_step(chk) ^ (*data);
  65. *(output++) = charset[*(data++)];
  66. }
  67. for(i = 0; i < CHECKSUM_SIZE; ++i) {
  68. chk = cashaddr_polymod_step(chk);
  69. }
  70. chk ^= 1;
  71. for(i = 0; i < CHECKSUM_SIZE; ++i) {
  72. *(output++) = charset[(chk >> ((CHECKSUM_SIZE - 1 - i) * 5)) & 0x1f];
  73. }
  74. *output = 0;
  75. return 1;
  76. }
  77. int cash_decode(char* hrp, uint8_t* data, size_t* data_len, const char* input) {
  78. uint64_t chk = 1;
  79. size_t i = 0;
  80. size_t input_len = strlen(input);
  81. size_t hrp_len = 0;
  82. int have_lower = 0, have_upper = 0;
  83. if(input_len < CHECKSUM_SIZE || input_len > MAX_CASHADDR_SIZE) {
  84. return 0;
  85. }
  86. *data_len = 0;
  87. while(*data_len < input_len && input[(input_len - 1) - *data_len] != ':') {
  88. ++(*data_len);
  89. }
  90. hrp_len = input_len - (1 + *data_len);
  91. if(1 + *data_len >= input_len || hrp_len > MAX_HRP_SIZE || *data_len < CHECKSUM_SIZE ||
  92. *data_len > CHECKSUM_SIZE + MAX_BASE32_SIZE) {
  93. return 0;
  94. }
  95. // subtract checksum
  96. *(data_len) -= CHECKSUM_SIZE;
  97. for(i = 0; i < hrp_len; ++i) {
  98. int ch = input[i];
  99. if(ch < 33 || ch > 126) {
  100. return 0;
  101. }
  102. if(ch >= 'a' && ch <= 'z') {
  103. have_lower = 1;
  104. } else if(ch >= 'A' && ch <= 'Z') {
  105. have_upper = 1;
  106. ch = (ch - 'A') + 'a';
  107. }
  108. hrp[i] = ch;
  109. chk = cashaddr_polymod_step(chk) ^ (ch & 0x1f);
  110. }
  111. hrp[i] = 0;
  112. chk = cashaddr_polymod_step(chk);
  113. ++i;
  114. while(i < input_len) {
  115. int v = (input[i] & 0x80) ? -1 : charset_rev[(int)input[i]];
  116. if(input[i] >= 'a' && input[i] <= 'z') have_lower = 1;
  117. if(input[i] >= 'A' && input[i] <= 'Z') have_upper = 1;
  118. if(v == -1) {
  119. return 0;
  120. }
  121. chk = cashaddr_polymod_step(chk) ^ v;
  122. if(i + CHECKSUM_SIZE < input_len) {
  123. data[i - (1 + hrp_len)] = v;
  124. }
  125. ++i;
  126. }
  127. if(have_lower && have_upper) {
  128. return 0;
  129. }
  130. return chk == 1;
  131. }
  132. static int convert_bits(
  133. uint8_t* out,
  134. size_t* outlen,
  135. int outbits,
  136. const uint8_t* in,
  137. size_t inlen,
  138. int inbits,
  139. int pad) {
  140. uint32_t val = 0;
  141. int bits = 0;
  142. uint32_t maxv = (((uint32_t)1) << outbits) - 1;
  143. while(inlen--) {
  144. val = (val << inbits) | *(in++);
  145. bits += inbits;
  146. while(bits >= outbits) {
  147. bits -= outbits;
  148. out[(*outlen)++] = (val >> bits) & maxv;
  149. }
  150. }
  151. if(pad) {
  152. if(bits) {
  153. out[(*outlen)++] = (val << (outbits - bits)) & maxv;
  154. }
  155. } else if(((val << (outbits - bits)) & maxv) || bits >= inbits) {
  156. return 0;
  157. }
  158. return 1;
  159. }
  160. int cash_addr_encode(char* output, const char* hrp, const uint8_t* data, 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, const char* addr) {
  168. uint8_t data[MAX_BASE32_SIZE] = {0};
  169. char hrp_actual[MAX_HRP_SIZE + 1] = {0};
  170. size_t data_len = 0;
  171. if(!cash_decode(hrp_actual, data, &data_len, addr)) return 0;
  172. if(data_len == 0 || data_len > MAX_BASE32_SIZE) return 0;
  173. if(strncmp(hrp, hrp_actual, MAX_HRP_SIZE + 1) != 0) return 0;
  174. *witdata_len = 0;
  175. if(!convert_bits(witdata, witdata_len, 8, data, data_len, 5, 0)) return 0;
  176. if(*witdata_len < 2 || *witdata_len > MAX_DATA_SIZE) return 0;
  177. return 1;
  178. }