From 58bacd23fdf9ba9bc7e84856598cac01566b60f2 Mon Sep 17 00:00:00 2001 From: W0nda <46626766+W0nda@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:25:10 +0100 Subject: [PATCH] Fix #22140 - Add bech32 encoding/decoding ##crypto --- dist/plugins-cfg/plugins.def.cfg | 1 + libr/crypto/meson.build | 1 + libr/crypto/p/bech32.mk | 11 ++ libr/crypto/p/crypto_bech32.c | 213 +++++++++++++++++++++++++++++ libr/crypto/p/crypto_bech32.h | 18 +++ libr/include/r_crypto.h | 2 + libr/main/rahash2.c | 4 +- test/db/cmd/hash | 2 +- test/db/tools/rahash2 | 1 + test/unit/test_bech32.c | 225 +++++++++++++++++++++++++++++++ 10 files changed, 475 insertions(+), 3 deletions(-) create mode 100644 libr/crypto/p/bech32.mk create mode 100644 libr/crypto/p/crypto_bech32.c create mode 100644 libr/crypto/p/crypto_bech32.h create mode 100644 test/unit/test_bech32.c diff --git a/dist/plugins-cfg/plugins.def.cfg b/dist/plugins-cfg/plugins.def.cfg index a21e27512b..6366237430 100644 --- a/dist/plugins-cfg/plugins.def.cfg +++ b/dist/plugins-cfg/plugins.def.cfg @@ -177,6 +177,7 @@ crypto.aes_cbc crypto.aes_wrap crypto.base64 crypto.base91 +crypto.bech32 crypto.blowfish crypto.cps2 crypto.des diff --git a/libr/crypto/meson.build b/libr/crypto/meson.build index da16053316..92656f30a2 100644 --- a/libr/crypto/meson.build +++ b/libr/crypto/meson.build @@ -5,6 +5,7 @@ r_crypto_sources = files( 'p/crypto_aes_algo.c', 'p/crypto_aes_cbc.c', 'p/crypto_aes_wrap.c', + 'p/crypto_bech32.c', 'p/crypto_base64.c', 'p/crypto_base91.c', 'p/crypto_blowfish.c', diff --git a/libr/crypto/p/bech32.mk b/libr/crypto/p/bech32.mk new file mode 100644 index 0000000000..6574e1d567 --- /dev/null +++ b/libr/crypto/p/bech32.mk @@ -0,0 +1,11 @@ +OBJ_BECH32=crypto_bech32.o + +STATIC_OBJ+=${OBJ_BECH32} +TARGET_BECH32=crypto_bech32.${EXT_SO} + +ALL_TARGETS+=${TARGET_BECH32} +DEPFLAGS=-L.. -lr_crypto -I../../../include + +${TARGET_BECH32}: ${OBJ_BECH32} + ${CC} $(call libname,crypto_bech32) $(DEPFLAGS) \ + ${LDFLAGS} ${CFLAGS} -o ${TARGET_BECH32} ${OBJ_BECH32} diff --git a/libr/crypto/p/crypto_bech32.c b/libr/crypto/p/crypto_bech32.c new file mode 100644 index 0000000000..e820cd4f82 --- /dev/null +++ b/libr/crypto/p/crypto_bech32.c @@ -0,0 +1,213 @@ +/* radare - BSD-3-Clause - Copyright (c) 2017, 2021, 2024 - Pieter Wuille, W0nda */ + +#include + +typedef enum { + BECH32_ENCODING_NONE, + BECH32_ENCODING_BECH32, + BECH32_ENCODING_BECH32M +} bech32_encoding; + +static uint32_t bech32_polymod_step(ut32 pre) { + uint8_t b = pre >> 25; + return ((pre & 0x1FFFFFF) << 5) ^ + (-((b >> 0) & 1) & 0x3b6a57b2UL) ^ + (-((b >> 1) & 1) & 0x26508e6dUL) ^ + (-((b >> 2) & 1) & 0x1ea119faUL) ^ + (-((b >> 3) & 1) & 0x3d4233ddUL) ^ + (-((b >> 4) & 1) & 0x2a1462b3UL); +} + +static uint32_t bech32_final_constant(bech32_encoding enc) { + R_RETURN_VAL_IF_FAIL (enc == BECH32_ENCODING_BECH32 || enc == BECH32_ENCODING_BECH32M, 1); + if (enc == BECH32_ENCODING_BECH32) { + return 1; + } + if (enc == BECH32_ENCODING_BECH32M) { + return 1; + } + return -1; +} + +static const char charset[] = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; + +static const int8_t charset_rev[128] = { + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, + -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, + 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, + -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, + 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 +}; + +static int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc) { + uint32_t chk = 1; + size_t i = 0; + while (hrp[i] != 0) { + int ch = hrp[i]; + if (ch < 33 || ch > 126) { + return 0; + } + if (ch >= 'A' && ch <= 'Z') { + return 0; + } + chk = bech32_polymod_step (chk) ^ (ch >> 5); + i++; + } + if (i + 7 + data_len > 90) { + return 0; + } + chk = bech32_polymod_step (chk); + while (*hrp != 0) { + chk = bech32_polymod_step (chk) ^ (*hrp & 0x1f); + *(output++) = *(hrp++); + } + *(output++) = '1'; + for (i = 0; i < data_len; i++) { + if (*data >> 5) { + return 0; + } + chk = bech32_polymod_step (chk) ^ (*data); + *(output++) = charset[*(data++)]; + } + for (i = 0; i < 6; i++) { + chk = bech32_polymod_step (chk); + } + chk ^= bech32_final_constant (enc); + for (i = 0; i < 6; i++) { + *(output++) = charset[(chk >> ((5 - i) * 5)) & 0x1f]; + } + *output = 0; + return 1; +} + +static bech32_encoding bech32_decode(char *hrp, uint8_t *data, int data_len, const char *input) { + uint32_t chk = 1; + size_t i, input_len = strlen (input); + int have_lower = 0, have_upper = 0; + if (input_len < 8 || input_len > 90) { + return BECH32_ENCODING_NONE; + } + data_len = 0; + while (data_len < input_len && input[(input_len - 1) - data_len] != '1') { + (data_len)++; + } + size_t hrp_len = input_len - (1 + data_len); + if (1 + data_len >= input_len || data_len < 6) { + return BECH32_ENCODING_NONE; + } + data_len -= 6; + for (i = 0; i < hrp_len; i++) { + int ch = input[i]; + if (ch < 33 || ch > 126) { + return BECH32_ENCODING_NONE; + } + if (ch >= 'a' && ch <= 'z') { + have_lower = 1; + } else if (ch >= 'A' && ch <= 'Z') { + have_upper = 1; + ch = (ch - 'A') + 'a'; + } + hrp[i] = ch; + chk = bech32_polymod_step (chk) ^ (ch >> 5); + } + hrp[i] = 0; + chk = bech32_polymod_step (chk); + for (i = 0; i < hrp_len; i++) { + chk = bech32_polymod_step (chk) ^ (input[i] & 0x1f); + } + i++; + while (i < input_len) { + int v = (input[i] & 0x80)? -1: charset_rev[(int)input[i]]; + if (input[i] >= 'a' && input[i] <= 'z') + have_lower = 1; + if (input[i] >= 'A' && input[i] <= 'Z') + have_upper = 1; + if (v == -1) { + return BECH32_ENCODING_NONE; + } + chk = bech32_polymod_step (chk) ^ v; + if (i + 6 < input_len) { + data[i - (1 + hrp_len)] = v; + } + i++; + } + if (have_lower && have_upper) { + return BECH32_ENCODING_NONE; + } + if (chk == bech32_final_constant (BECH32_ENCODING_BECH32)) { + return BECH32_ENCODING_BECH32; // wtf? + if (chk == bech32_final_constant (BECH32_ENCODING_BECH32M)) { + return BECH32_ENCODING_BECH32M; + } + return BECH32_ENCODING_NONE; + } + return BECH32_ENCODING_NONE; +} + +static bool bech32_set_key(RCryptoJob *cj, const ut8 *key, int keylen, int mode, int direction) { + cj->key_len = keylen; + memcpy (cj->key, key, keylen); + cj->dir = direction; + return true; +} + +static int bech32_get_key_size(RCryptoJob *cj) { + return cj->key_len; +} + +static bool bech32_check(const char *algo) { + return !strcmp (algo, "bech32"); +} + +static bool update(RCryptoJob *cj, const ut8 *buf, int len) { + const int enc = BECH32_ENCODING_BECH32; + char *hrp = malloc (cj->key_len + 1); // HRP need to be null-terminated + if (!hrp) { + return false; + } + hrp[cj->key_len] = 0; + memcpy (hrp, cj->key, cj->key_len); + char *in_out = r_str_ndup ((const char *)buf, len); + char *data = r_str_ndup ((const char *)buf, len); + switch (cj->dir) { + case R_CRYPTO_DIR_ENCRYPT: + bech32_encode (in_out, hrp, buf, len, enc); + break; + case R_CRYPTO_DIR_DECRYPT: + bech32_decode (hrp, (ut8*)data, len, in_out); + break; + default: + R_LOG_ERROR ("Choose decrypt or encrypt"); + break; + } + return true; +} + +static bool end(RCryptoJob *cj, const ut8 *buf, int len) { + return update (cj, buf, len); +} + +RCryptoPlugin r_crypto_plugin_bech32 = { + .meta = { + .name = "bech32", + .author = "W0nda", + .license = "BSD-3-Clause", + }, + .type = R_CRYPTO_TYPE_ENCODER, + .set_key = bech32_set_key, + .get_key_size = bech32_get_key_size, + .check = bech32_check, + .update = update, + .end = end +}; + +#ifndef R2_PLUGIN_INCORE +R_API RLibStruct radare_plugin = { + .type = R_LIB_TYPE_CRYPTO, + .data = &r_crypto_plugin_bech32, + .version = R2_VERSION +}; +#endif diff --git a/libr/crypto/p/crypto_bech32.h b/libr/crypto/p/crypto_bech32.h new file mode 100644 index 0000000000..a681a2be82 --- /dev/null +++ b/libr/crypto/p/crypto_bech32.h @@ -0,0 +1,18 @@ +#ifndef R2_BECH32_H +#define R2_BECH32_H + +#ifdef __cplusplus +extern "C" { +#endif + +#if R2_USE_NEW_ABI + +R_API int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len, bech32_encoding enc); +R_API bech32_encoding bech32_decode(char *hrp, uint8_t *data, size_t *data_len, const char *input); +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libr/include/r_crypto.h b/libr/include/r_crypto.h index 26132d3976..086c2b42ce 100644 --- a/libr/include/r_crypto.h +++ b/libr/include/r_crypto.h @@ -143,6 +143,7 @@ extern RCryptoPlugin r_crypto_plugin_sm4; extern RCryptoPlugin r_crypto_plugin_aes_wrap; extern RCryptoPlugin r_crypto_plugin_ed25519; extern RCryptoPlugin r_crypto_plugin_entropy; +extern RCryptoPlugin r_crypto_plugin_bech32; #define R_CRYPTO_NONE 0ULL #define R_CRYPTO_RC2 1ULL @@ -160,6 +161,7 @@ extern RCryptoPlugin r_crypto_plugin_entropy; #define R_CRYPTO_XOR 1ULL<<12 #define R_CRYPTO_SERPENT 1ULL<<13 #define R_CRYPTO_SM4_ECB 1ULL << 14 +#define R_CRYPTO_BECH32 1ULL << 15 #define R_CRYPTO_ALL 0xFFFF #define R_CODEC_NONE 0ULL diff --git a/libr/main/rahash2.c b/libr/main/rahash2.c index 7e5476e73e..d36c0046ba 100644 --- a/libr/main/rahash2.c +++ b/libr/main/rahash2.c @@ -403,7 +403,7 @@ static int encrypt_or_decrypt(RahashOptions *ro, const char *hashstr, int hashst const int direction = ro->direction; const char *algo = ro->algorithm; // TODO: generalise this for all non key encoding/decoding. - bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo); + bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo) || !strcmp ("bech32", algo); if (no_key_mode || ro->s.len > 0) { RCrypto *cry = r_crypto_new (); RCryptoJob *cj = r_crypto_use (cry, algo); @@ -444,7 +444,7 @@ static int encrypt_or_decrypt_file(RahashOptions *ro, const char *filename, cons const int direction = ro->direction; const char *algo = ro->algorithm; // TODO: generalise this for all non key encoding/decoding. aka crypto vs encoder plugins after moving all those hash algos to crypto plugins - bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo); + bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo) || !strcmp ("punycode", algo) || !strcmp ("bech32", algo); if (no_key_mode || ro->s.len > 0) { RCrypto *cry = r_crypto_new (); RCryptoJob *cj = r_crypto_use (cry, algo); diff --git a/test/db/cmd/hash b/test/db/cmd/hash index 0e8b333d6e..2b66f613eb 100644 --- a/test/db/cmd/hash +++ b/test/db/cmd/hash @@ -117,6 +117,6 @@ CMDS=< +#include "minunit.h" + +#include +#include +#include + +static const char *valid_checksum_bech32[] = { + "A12UEL5L", + "a12uel5l", + "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", + "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", + "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", + "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", + "?1ezyfcl", +}; + +static const char *valid_checksum_bech32m[] = { + "A1LQFN3A", + "a1lqfn3a", + "an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6", + "abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx", + "11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8", + "split1checkupstagehandshakeupstreamerranterredcaperredlc445v", + "?1v759aa", +}; + +static const char *invalid_checksum_bech32[] = { + " 1nwldj5", + "\x7f" + "1axkwrx", + "\x80" + "1eym55h", + "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", + "pzry9x0s0muk", + "1pzry9x0s0muk", + "x1b4n0q5v", + "li1dgmt3", + "de1lg7wt\xff", + "A1G7SGD8", + "10a06t8", + "1qzzfhee", +}; + +static const char *invalid_checksum_bech32m[] = { + " 1xj0phk", + "\x7F" + "1g6xzxy", + "\x80" + "1vctc34", + "an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4", + "qyrz8wqd2c9m", + "1qyrz8wqd2c9m", + "y1b0jsk6g", + "lt1igcx5c0", + "in1muywd", + "mm1crxm3i", + "au1s5cgom", + "M1VUXWEZ", + "16plkw9", + "1p2gdwpf", +}; + +struct valid_address_data { + const char *address; + size_t scriptPubKeyLen; + const uint8_t scriptPubKey[42]; +}; + +struct invalid_address_data { + const char *hrp; + int version; + size_t program_length; +}; + +static struct valid_address_data valid_address[] = { + { "BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4", + 22, { 0x00, 0x14, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6 } }, + { "tb1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q0sl5k7", + 34, { 0x00, 0x20, 0x18, 0x63, 0x14, 0x3c, 0x14, 0xc5, 0x16, 0x68, 0x04, 0xbd, 0x19, 0x20, 0x33, 0x56, 0xda, 0x13, 0x6c, 0x98, 0x56, 0x78, 0xcd, 0x4d, 0x27, 0xa1, 0xb8, 0xc6, 0x32, 0x96, 0x04, 0x90, 0x32, 0x62 } }, + { "bc1pw508d6qejxtdg4y5r3zarvary0c5xw7kw508d6qejxtdg4y5r3zarvary0c5xw7kt5nd6y", + 42, { 0x51, 0x28, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23, 0xf1, 0x43, 0x3b, 0xd6 } }, + { "BC1SW50QGDZ25J", + 4, { 0x60, 0x02, 0x75, 0x1e } }, + { "bc1zw508d6qejxtdg4y5r3zarvaryvaxxpcs", + 18, { 0x52, 0x10, 0x75, 0x1e, 0x76, 0xe8, 0x19, 0x91, 0x96, 0xd4, 0x54, 0x94, 0x1c, 0x45, 0xd1, 0xb3, 0xa3, 0x23 } }, + { "tb1qqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesrxh6hy", + 34, { 0x00, 0x20, 0x00, 0x00, 0x00, 0xc4, 0xa5, 0xca, 0xd4, 0x62, 0x21, 0xb2, 0xa1, 0x87, 0x90, 0x5e, 0x52, 0x66, 0x36, 0x2b, 0x99, 0xd5, 0xe9, 0x1c, 0x6c, 0xe2, 0x4d, 0x16, 0x5d, 0xab, 0x93, 0xe8, 0x64, 0x33 } }, + { "tb1pqqqqp399et2xygdj5xreqhjjvcmzhxw4aywxecjdzew6hylgvsesf3hn0c", + 34, { 0x51, 0x20, 0x00, 0x00, 0x00, 0xc4, 0xa5, 0xca, 0xd4, 0x62, 0x21, 0xb2, 0xa1, 0x87, 0x90, 0x5e, 0x52, 0x66, 0x36, 0x2b, 0x99, 0xd5, 0xe9, 0x1c, 0x6c, 0xe2, 0x4d, 0x16, 0x5d, 0xab, 0x93, 0xe8, 0x64, 0x33 } }, + { "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqzk5jj0", + 34, { 0x51, 0x20, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98 } }, +}; + +static const char *invalid_address[] = { + "tc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq5zuyut", + "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqh2y7hd", + "tb1z0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vqglt7rf", + "BC1S0XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ54WELL", + "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kemeawh", + "tb1q0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq24jc47", + "bc1p38j9r5y49hruaue7wxjce0updqjuyyx0kh56v8s25huc6995vvpql3jow4", + "BC130XLXVLHEMJA6C4DQV22UAPCTQUPFHLXM9H8Z3K2E72Q4K9HCZ7VQ7ZWS8R", + "bc1pw5dgrnzv", + "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v8n0nx0muaewav253zgeav", + "BC1QR508D6QEJXTDG4Y5R3ZARVARYV98GJ9P", + "tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vq47Zagq", + "bc1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7v07qwwzcrf", + "tb1p0xlxvlhemja6c4dqv22uapctqupfhlxm9h8z3k2e72q4k9hcz7vpggkg4j", + "bc1gmk9yu", +}; + +static struct invalid_address_data invalid_address_enc[] = { + { "BC", 0, 20 }, + { "bc", 0, 21 }, + { "bc", 17, 32 }, + { "bc", 1, 1 }, + { "bc", 16, 41 }, +}; + +static void segwit_scriptpubkey(uint8_t *scriptpubkey, size_t *scriptpubkeylen, int witver, const uint8_t *witprog, size_t witprog_len) { + scriptpubkey[0] = witver? (0x50 + witver): 0; + scriptpubkey[1] = witprog_len; + memcpy (scriptpubkey + 2, witprog, witprog_len); + *scriptpubkeylen = witprog_len + 2; +} + +int my_strncasecmp (const char *s1, const char *s2, size_t n) { + size_t i = 0; + while (i < n) { + char c1 = s1[i]; + char c2 = s2[i]; + if (c1 >= 'A' && c1 <= 'Z') + c1 = (c1 - 'A') + 'a'; + if (c2 >= 'A' && c2 <= 'Z') + c2 = (c2 - 'A') + 'a'; + if (c1 < c2) + return -1; + if (c1 > c2) + return 1; + if (c1 == 0) + return 0; + ++i; + } + return 0; +} + +static void test_crypto_bech32_encode(void) { + uint8_t data[82]; + char rebuild[92]; + char hrp[84]; + size_t data_len; + int i; + for (i = 0; i < sizeof (valid_checksum_bech32) / sizeof (valid_checksum_bech32[0]); i++) { + bech32_decode (hrp, data, &data_len, valid_checksum_bech32[i]); + if (bech32_encode (rebuild, hrp, data, data_len, BECH32_ENCODING_BECH32)) { + mu_assert_eq (my_strncasecmp (rebuild, valid_checksum_bech32m[i], 92), 0, "bech32_encode"); + mu_end; + } + } +} + +void test_crypto_bech32m_encode (void) { + uint8_t data[82]; + char rebuild[92]; + char hrp[84]; + size_t data_len; + int i; + for (i = 0; i < sizeof (valid_checksum_bech32m) / sizeof (valid_checksum_bech32m[0]); i++) { + bech32_decode (hrp, data, &data_len, valid_checksum_bech32m[i]); + if (bech32_encode (rebuild, hrp, data, data_len, BECH32_ENCODING_BECH32M)) { + mu_assert_eq (my_strncasecmp (rebuild, valid_checksum_bech32m[i], 92), 0, "bech32m_encode"); + mu_end; + } + } +} + +int test_crypto_bech32_decode (void) { + uint8_t data[82]; + char hrp[84]; + size_t data_len; + int i; + for (i = 0; i < sizeof (valid_checksum_bech32) / sizeof (valid_checksum_bech32[0]); i++) { + mu_assert_eq (bech32_decode (hrp, data, &data_len, valid_checksum_bech32[i]), BECH32_ENCODING_BECH32, "bech32_dec_valid_checksum"); + mu_end; + } + for (i = 0; i < sizeof (invalid_checksum_bech32) / sizeof (invalid_checksum_bech32[0]); i++) { + mu_assert_neq (bech32_decode (hrp, data, &data_len, invalid_checksum_bech32[i]), BECH32_ENCODING_BECH32, "bech32_dec_invalid_checksum"); + mu_end; + } +} + +int test_crypto_bech32m_decode (void) { + uint8_t data[82]; + char hrp[84]; + size_t data_len; + int i; + for (i = 0; i < sizeof (valid_checksum_bech32m) / sizeof (valid_checksum_bech32m[0]); i++) { + mu_assert_eq (bech32_decode (hrp, data, &data_len, valid_checksum_bech32m[i]), BECH32_ENCODING_BECH32M, "bech32m_dec_valid_checksum"); + mu_end + } + for (i = 0; i < sizeof (invalid_checksum_bech32m) / sizeof (invalid_checksum_bech32m[0]); i++) { + mu_assert_neq (bech32_decode (hrp, data, &data_len, invalid_checksum_bech32m[i]), BECH32_ENCODING_BECH32M, "bech32m_dec_invalid_checksum"); + mu_end; + } +} + +int all_tests (void) { + mu_run_test (test_crypto_bech32_encode); + mu_run_test (test_crypto_bech32m_encode); + mu_run_test (test_crypto_bech32_decode); + mu_run_test (test_crypto_bech32m_decode); + return tests_passed != tests_run; +} + +int main () { + return all_tests (); +}; +#else +int main () { + return 0; +}; +#endif