bech32.c (8323B)
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, int max_hrp_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 // Maximum amount of text content is buffer length - 1 byte, to account for the null-terminator 108 int max_hrp_content_len = max_hrp_len - 1; 109 if (hrp_len > max_hrp_content_len) 110 return BECH32_ENCODING_NONE; 111 if (1 + *data_len >= input_len || *data_len < 6) { 112 return BECH32_ENCODING_NONE; 113 } 114 *(data_len) -= 6; 115 for (i = 0; i < hrp_len; ++i) { 116 int ch = input[i]; 117 if (ch < 33 || ch > 126) { 118 return BECH32_ENCODING_NONE; 119 } 120 if (ch >= 'a' && ch <= 'z') { 121 have_lower = 1; 122 } else if (ch >= 'A' && ch <= 'Z') { 123 have_upper = 1; 124 ch = (ch - 'A') + 'a'; 125 } 126 hrp[i] = ch; 127 chk = bech32_polymod_step(chk) ^ (ch >> 5); 128 } 129 hrp[i] = 0; 130 chk = bech32_polymod_step(chk); 131 for (i = 0; i < hrp_len; ++i) { 132 chk = bech32_polymod_step(chk) ^ (input[i] & 0x1f); 133 } 134 ++i; 135 while (i < input_len) { 136 int v = (input[i] & 0x80) ? -1 : bech32_charset_rev[(int)input[i]]; 137 if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1; 138 if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1; 139 if (v == -1) { 140 return BECH32_ENCODING_NONE; 141 } 142 chk = bech32_polymod_step(chk) ^ v; 143 if (i + 6 < input_len) { 144 data[i - (1 + hrp_len)] = v; 145 } 146 ++i; 147 } 148 if (have_lower && have_upper) { 149 return BECH32_ENCODING_NONE; 150 } 151 if (chk == bech32_final_constant(BECH32_ENCODING_BECH32)) { 152 return BECH32_ENCODING_BECH32; 153 } else if (chk == bech32_final_constant(BECH32_ENCODING_BECH32M)) { 154 return BECH32_ENCODING_BECH32M; 155 } else { 156 return BECH32_ENCODING_NONE; 157 } 158 } 159 160 bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t max_input_len) { 161 size_t len = strlen(input); 162 if (len > max_input_len) { 163 return BECH32_ENCODING_NONE; 164 } 165 static const int MAX_PREFIX = 10; // 9 bytes for the text, 1 byte for the null terminator 166 return bech32_decode_len(hrp, data, data_len, input, len, MAX_PREFIX); 167 } 168 169 int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) { 170 uint32_t val = 0; 171 int bits = 0; 172 uint32_t maxv = (((uint32_t)1) << outbits) - 1; 173 *outlen = 0; 174 while (inlen--) { 175 val = (val << inbits) | *(in++); 176 bits += inbits; 177 while (bits >= outbits) { 178 bits -= outbits; 179 out[(*outlen)++] = (val >> bits) & maxv; 180 } 181 } 182 if (pad) { 183 if (bits) { 184 out[(*outlen)++] = (val << (outbits - bits)) & maxv; 185 } 186 } else if (((val << (outbits - bits)) & maxv) || bits >= inbits) { 187 return 0; 188 } 189 return 1; 190 } 191 192 int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) { 193 uint8_t data[65]; 194 size_t datalen = 0; 195 bech32_encoding enc = BECH32_ENCODING_BECH32; 196 if (witver > 16) return 0; 197 if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0; 198 if (witprog_len < 2 || witprog_len > 40) return 0; 199 if (witver > 0) enc = BECH32_ENCODING_BECH32M; 200 data[0] = witver; 201 bech32_convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1); 202 ++datalen; 203 return bech32_encode(output, hrp, data, datalen, 90, enc); 204 } 205 206 int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) { 207 uint8_t data[84]; 208 char hrp_actual[84]; 209 size_t data_len; 210 bech32_encoding enc = bech32_decode(hrp_actual, data, &data_len, addr, 90); 211 if (enc == BECH32_ENCODING_NONE) return 0; 212 if (data_len == 0 || data_len > 65) return 0; 213 if (strncmp(hrp, hrp_actual, 84) != 0) return 0; 214 if (data[0] > 16) return 0; 215 if (data[0] == 0 && enc != BECH32_ENCODING_BECH32) return 0; 216 if (data[0] > 0 && enc != BECH32_ENCODING_BECH32M) return 0; 217 *witdata_len = 0; 218 if (!bech32_convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0; 219 if (*witdata_len < 2 || *witdata_len > 40) return 0; 220 if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0; 221 *witver = data[0]; 222 return 1; 223 }