bech32.c (7957B)
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 #include "config.h" 25 #include <assert.h> 26 #include "bech32.h" 27 #include <string.h> 28 29 static uint32_t bech32_polymod_step(uint32_t pre) { 30 uint8_t b = pre >> 25; 31 return ((pre & 0x1FFFFFF) << 5) ^ 32 (-((b >> 0) & 1) & 0x3b6a57b2UL) ^ 33 (-((b >> 1) & 1) & 0x26508e6dUL) ^ 34 (-((b >> 2) & 1) & 0x1ea119faUL) ^ 35 (-((b >> 3) & 1) & 0x3d4233ddUL) ^ 36 (-((b >> 4) & 1) & 0x2a1462b3UL); 37 } 38 39 static uint32_t bech32_final_constant(bech32_encoding enc) { 40 if (enc == BECH32_ENCODING_BECH32) return 1; 41 if (enc == BECH32_ENCODING_BECH32M) return 0x2bc830a3; 42 assert(0); 43 } 44 45 const char bech32_charset[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; 46 47 const int8_t bech32_charset_rev[128] = { 48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 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 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, 52 -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 53 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, 54 -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, 55 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 56 }; 57 58 int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, size_t max_input_len, bech32_encoding enc) { 59 uint32_t chk = 1; 60 size_t i = 0; 61 while (hrp[i] != 0) { 62 int ch = hrp[i]; 63 if (ch < 33 || ch > 126) { 64 return 0; 65 } 66 67 if (ch >= 'A' && ch <= 'Z') return 0; 68 chk = bech32_polymod_step(chk) ^ (ch >> 5); 69 ++i; 70 } 71 if (i + 7 + data_len > max_input_len) return 0; 72 chk = bech32_polymod_step(chk); 73 while (*hrp != 0) { 74 chk = bech32_polymod_step(chk) ^ (*hrp & 0x1f); 75 *(output++) = *(hrp++); 76 } 77 *(output++) = '1'; 78 for (i = 0; i < data_len; ++i) { 79 if (*data >> 5) return 0; 80 chk = bech32_polymod_step(chk) ^ (*data); 81 *(output++) = bech32_charset[*(data++)]; 82 } 83 for (i = 0; i < 6; ++i) { 84 chk = bech32_polymod_step(chk); 85 } 86 chk ^= bech32_final_constant(enc); 87 for (i = 0; i < 6; ++i) { 88 *(output++) = bech32_charset[(chk >> ((5 - i) * 5)) & 0x1f]; 89 } 90 *output = 0; 91 return 1; 92 } 93 94 bech32_encoding bech32_decode_len(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t input_len) { 95 uint32_t chk = 1; 96 size_t i; 97 size_t hrp_len; 98 int have_lower = 0, have_upper = 0; 99 if (input_len < 8) { 100 return BECH32_ENCODING_NONE; 101 } 102 *data_len = 0; 103 while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') { 104 ++(*data_len); 105 } 106 hrp_len = input_len - (1 + *data_len); 107 if (1 + *data_len >= input_len || *data_len < 6) { 108 return BECH32_ENCODING_NONE; 109 } 110 *(data_len) -= 6; 111 for (i = 0; i < hrp_len; ++i) { 112 int ch = input[i]; 113 if (ch < 33 || ch > 126) { 114 return BECH32_ENCODING_NONE; 115 } 116 if (ch >= 'a' && ch <= 'z') { 117 have_lower = 1; 118 } else if (ch >= 'A' && ch <= 'Z') { 119 have_upper = 1; 120 ch = (ch - 'A') + 'a'; 121 } 122 hrp[i] = ch; 123 chk = bech32_polymod_step(chk) ^ (ch >> 5); 124 } 125 hrp[i] = 0; 126 chk = bech32_polymod_step(chk); 127 for (i = 0; i < hrp_len; ++i) { 128 chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f); 129 } 130 ++i; 131 while (i < input_len) { 132 int v = (input[i] & 0x80) ? -1 : bech32_charset_rev[(int)input[i]]; 133 if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1; 134 if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1; 135 if (v == -1) { 136 return BECH32_ENCODING_NONE; 137 } 138 chk = bech32_polymod_step(chk) ^ v; 139 if (i + 6 < input_len) { 140 data[i - (1 + hrp_len)] = v; 141 } 142 ++i; 143 } 144 if (have_lower && have_upper) { 145 return BECH32_ENCODING_NONE; 146 } 147 if (chk == bech32_final_constant(BECH32_ENCODING_BECH32)) { 148 return BECH32_ENCODING_BECH32; 149 } else if (chk == bech32_final_constant(BECH32_ENCODING_BECH32M)) { 150 return BECH32_ENCODING_BECH32M; 151 } else { 152 return BECH32_ENCODING_NONE; 153 } 154 } 155 156 bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t max_input_len) { 157 size_t len = strlen(input); 158 if (len > max_input_len) { 159 return BECH32_ENCODING_NONE; 160 } 161 return bech32_decode_len(hrp, data, data_len, input, len); 162 } 163 164 int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) { 165 uint32_t val = 0; 166 int bits = 0; 167 uint32_t maxv = (((uint32_t)1) << outbits) - 1; 168 while (inlen--) { 169 val = (val << inbits) | *(in++); 170 bits += inbits; 171 while (bits >= outbits) { 172 bits -= outbits; 173 out[(*outlen)++] = (val >> bits) & maxv; 174 } 175 } 176 if (pad) { 177 if (bits) { 178 out[(*outlen)++] = (val << (outbits - bits)) & maxv; 179 } 180 } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) { 181 return 0; 182 } 183 return 1; 184 } 185 186 int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) { 187 uint8_t data[65]; 188 size_t datalen = 0; 189 bech32_encoding enc = BECH32_ENCODING_BECH32; 190 if (witver > 16) return 0; 191 if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0; 192 if (witprog_len < 2 || witprog_len > 40) return 0; 193 if (witver > 0) enc = BECH32_ENCODING_BECH32M; 194 data[0] = witver; 195 bech32_convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1); 196 ++datalen; 197 return bech32_encode(output, hrp, data, datalen, 90, enc); 198 } 199 200 int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) { 201 uint8_t data[84]; 202 char hrp_actual[84]; 203 size_t data_len; 204 bech32_encoding enc = bech32_decode(hrp_actual, data, &data_len, addr, 90); 205 if (enc == BECH32_ENCODING_NONE) return 0; 206 if (data_len == 0 || data_len > 65) return 0; 207 if (strncmp(hrp, hrp_actual, 84) != 0) return 0; 208 if (data[0] > 16) return 0; 209 if (data[0] == 0 && enc != BECH32_ENCODING_BECH32) return 0; 210 if (data[0] > 0 && enc != BECH32_ENCODING_BECH32M) return 0; 211 *witdata_len = 0; 212 if (!bech32_convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0; 213 if (*witdata_len < 2 || *witdata_len > 40) return 0; 214 if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0; 215 *witver = data[0]; 216 return 1; 217 }