bech32.c (7736B)
1 /* Stolen from https://github.com/sipa/bech32/blob/master/ref/c/segwit_addr.c, 2 * with only the two ' > 90' checks hoisted, and more internals exposed */ 3 4 /* Copyright (c) 2017, 2021 Pieter Wuille 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "config.h" 26 #include <assert.h> 27 #include <string.h> 28 #include "bech32.h" 29 30 static uint32_t bech32_polymod_step(uint32_t pre) { 31 uint8_t b = pre >> 25; 32 return ((pre & 0x1FFFFFF) << 5) ^ 33 (-((b >> 0) & 1) & 0x3b6a57b2UL) ^ 34 (-((b >> 1) & 1) & 0x26508e6dUL) ^ 35 (-((b >> 2) & 1) & 0x1ea119faUL) ^ 36 (-((b >> 3) & 1) & 0x3d4233ddUL) ^ 37 (-((b >> 4) & 1) & 0x2a1462b3UL); 38 } 39 40 static uint32_t bech32_final_constant(bech32_encoding enc) { 41 if (enc == BECH32_ENCODING_BECH32) return 1; 42 if (enc == BECH32_ENCODING_BECH32M) return 0x2bc830a3; 43 assert(0); 44 } 45 46 const char bech32_charset[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; 47 48 const int8_t bech32_charset_rev[128] = { 49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 52 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, 53 -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 54 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, 55 -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 56 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 57 }; 58 59 int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, size_t max_input_len, bech32_encoding enc) { 60 uint32_t chk = 1; 61 size_t i = 0; 62 while (hrp[i] != 0) { 63 int ch = hrp[i]; 64 if (ch < 33 || ch > 126) { 65 return 0; 66 } 67 68 if (ch >= 'A' && ch <= 'Z') return 0; 69 chk = bech32_polymod_step(chk) ^ (ch >> 5); 70 ++i; 71 } 72 if (i + 7 + data_len > max_input_len) return 0; 73 chk = bech32_polymod_step(chk); 74 while (*hrp != 0) { 75 chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f); 76 *(output++) = *(hrp++); 77 } 78 *(output++) = '1'; 79 for (i = 0; i < data_len; ++i) { 80 if (*data >> 5) return 0; 81 chk = bech32_polymod_step(chk) ^ (*data); 82 *(output++) = bech32_charset[*(data++)]; 83 } 84 for (i = 0; i < 6; ++i) { 85 chk = bech32_polymod_step(chk); 86 } 87 chk ^= bech32_final_constant(enc); 88 for (i = 0; i < 6; ++i) { 89 *(output++) = bech32_charset[(chk >> ((5 - i) * 5)) & 0x1f]; 90 } 91 *output = 0; 92 return 1; 93 } 94 95 bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t max_input_len) { 96 uint32_t chk = 1; 97 size_t i; 98 size_t input_len = strlen(input); 99 size_t hrp_len; 100 int have_lower = 0, have_upper = 0; 101 if (input_len < 8 || input_len > max_input_len) { 102 return BECH32_ENCODING_NONE; 103 } 104 *data_len = 0; 105 while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') { 106 ++(*data_len); 107 } 108 hrp_len = input_len - (1 + *data_len); 109 if (1 + *data_len >= input_len || *data_len < 6) { 110 return BECH32_ENCODING_NONE; 111 } 112 *(data_len) -= 6; 113 for (i = 0; i < hrp_len; ++i) { 114 int ch = input[i]; 115 if (ch < 33 || ch > 126) { 116 return BECH32_ENCODING_NONE; 117 } 118 if (ch >= 'a' && ch <= 'z') { 119 have_lower = 1; 120 } else if (ch >= 'A' && ch <= 'Z') { 121 have_upper = 1; 122 ch = (ch - 'A') + 'a'; 123 } 124 hrp[i] = ch; 125 chk = bech32_polymod_step(chk) ^ (ch >> 5); 126 } 127 hrp[i] = 0; 128 chk = bech32_polymod_step(chk); 129 for (i = 0; i < hrp_len; ++i) { 130 chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f); 131 } 132 ++i; 133 while (i < input_len) { 134 int v = (input[i] & 0x80) ? -1 : bech32_charset_rev[(int)input[i]]; 135 if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1; 136 if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1; 137 if (v == -1) { 138 return BECH32_ENCODING_NONE; 139 } 140 chk = bech32_polymod_step(chk) ^ v; 141 if (i + 6 < input_len) { 142 data[i - (1 + hrp_len)] = v; 143 } 144 ++i; 145 } 146 if (have_lower && have_upper) { 147 return BECH32_ENCODING_NONE; 148 } 149 if (chk == bech32_final_constant(BECH32_ENCODING_BECH32)) { 150 return BECH32_ENCODING_BECH32; 151 } else if (chk == bech32_final_constant(BECH32_ENCODING_BECH32M)) { 152 return BECH32_ENCODING_BECH32M; 153 } else { 154 return BECH32_ENCODING_NONE; 155 } 156 } 157 158 int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) { 159 uint32_t val = 0; 160 int bits = 0; 161 uint32_t maxv = (((uint32_t)1) << outbits) - 1; 162 while (inlen--) { 163 val = (val << inbits) | *(in++); 164 bits += inbits; 165 while (bits >= outbits) { 166 bits -= outbits; 167 out[(*outlen)++] = (val >> bits) & maxv; 168 } 169 } 170 if (pad) { 171 if (bits) { 172 out[(*outlen)++] = (val << (outbits - bits)) & maxv; 173 } 174 } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) { 175 return 0; 176 } 177 return 1; 178 } 179 180 int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) { 181 uint8_t data[65]; 182 size_t datalen = 0; 183 bech32_encoding enc = BECH32_ENCODING_BECH32; 184 if (witver > 16) return 0; 185 if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0; 186 if (witprog_len < 2 || witprog_len > 40) return 0; 187 if (witver > 0) enc = BECH32_ENCODING_BECH32M; 188 data[0] = witver; 189 bech32_convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1); 190 ++datalen; 191 return bech32_encode(output, hrp, data, datalen, 90, enc); 192 } 193 194 int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) { 195 uint8_t data[84]; 196 char hrp_actual[84]; 197 size_t data_len; 198 bech32_encoding enc = bech32_decode(hrp_actual, data, &data_len, addr, 90); 199 if (enc == BECH32_ENCODING_NONE) return 0; 200 if (data_len == 0 || data_len > 65) return 0; 201 if (strncmp(hrp, hrp_actual, 84) != 0) return 0; 202 if (data[0] > 16) return 0; 203 if (data[0] == 0 && enc != BECH32_ENCODING_BECH32) return 0; 204 if (data[0] > 0 && enc != BECH32_ENCODING_BECH32M) return 0; 205 *witdata_len = 0; 206 if (!bech32_convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0; 207 if (*witdata_len < 2 || *witdata_len > 40) return 0; 208 if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0; 209 *witver = data[0]; 210 return 1; 211 }