1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 07:18:09 +00:00

ecdsa_address_decode now needs version

This commit is contained in:
Pavol Rusnak 2016-10-08 18:13:23 +02:00
parent 430a5087c8
commit 8764e26368
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 26 additions and 14 deletions

12
ecdsa.c
View File

@ -930,10 +930,18 @@ void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wif
MEMSET_BZERO(wif_raw, sizeof(wif_raw)); MEMSET_BZERO(wif_raw, sizeof(wif_raw));
} }
int ecdsa_address_decode(const char *addr, uint8_t *out) int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out)
{ {
if (!addr) return 0; if (!addr) return 0;
return base58_decode_check(addr, out, 21) == 21; if (version <= 0xFF) {
return base58_decode_check(addr, out, 21) == 21 && out[0] == (version & 0xFF);
} else if (version <= 0xFFFF) {
return base58_decode_check(addr, out, 22) == 22 && out[0] == ((version >> 8)) && out[1] == (version & 0xFF);
} else if (version <= 0xFFFF) {
return base58_decode_check(addr, out, 23) == 23 && out[0] == ((version >> 16)) && out[1] == ((version >> 8) & 0xFF) && out[2] == (version & 0xFF);
} else {
return base58_decode_check(addr, out, 24) == 24 && out[0] == (version >> 24) && out[1] == ((version >> 16) & 0xFF) && out[2] == ((version >> 8) & 0xFF) && out[3] == (version & 0xFF);
}
} }
void uncompress_coords(const ecdsa_curve *curve, uint8_t odd, const bignum256 *x, bignum256 *y) void uncompress_coords(const ecdsa_curve *curve, uint8_t odd, const bignum256 *x, bignum256 *y)

View File

@ -78,7 +78,7 @@ void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, uint8_t *ad
void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize); void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize);
void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize); void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize);
int ecdsa_address_decode(const char *addr, uint8_t *out); int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out);
int ecdsa_read_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, curve_point *pub); int ecdsa_read_pubkey(const ecdsa_curve *curve, const uint8_t *pub_key, curve_point *pub);
int ecdsa_validate_pubkey(const ecdsa_curve *curve, const curve_point *pub); int ecdsa_validate_pubkey(const ecdsa_curve *curve, const curve_point *pub);
int ecdsa_verify(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len); int ecdsa_verify(const ecdsa_curve *curve, const uint8_t *pub_key, const uint8_t *sig, const uint8_t *msg, uint32_t msg_len);

24
tests.c
View File

@ -2152,44 +2152,48 @@ START_TEST(test_address_decode)
int res; int res;
uint8_t decode[MAX_ADDR_RAW_SIZE]; uint8_t decode[MAX_ADDR_RAW_SIZE];
res = ecdsa_address_decode("1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T", decode); res = ecdsa_address_decode("1JwSSubhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("00c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); ck_assert_mem_eq(decode, fromhex("00c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
res = ecdsa_address_decode("myTPjxggahXyAzuMcYp5JTkbybANyLsYBW", decode); res = ecdsa_address_decode("myTPjxggahXyAzuMcYp5JTkbybANyLsYBW", 111, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("6fc4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); ck_assert_mem_eq(decode, fromhex("6fc4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
res = ecdsa_address_decode("NEWoeZ6gh4CGvRgFAoAGh4hBqpxizGT6gZ", decode); res = ecdsa_address_decode("NEWoeZ6gh4CGvRgFAoAGh4hBqpxizGT6gZ", 52, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("34c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); ck_assert_mem_eq(decode, fromhex("34c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
res = ecdsa_address_decode("LdAPi7uXrLLmeh7u57pzkZc3KovxEDYRJq", decode); res = ecdsa_address_decode("LdAPi7uXrLLmeh7u57pzkZc3KovxEDYRJq", 48, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("30c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21); ck_assert_mem_eq(decode, fromhex("30c4c5d791fcb4654a1ef5e03fe0ad3d9c598f9827"), 21);
res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", decode); res = ecdsa_address_decode("1C7zdTfnkzmr13HfA2vNm5SJYRK6nEKyq8", 0, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("0079fbfc3f34e7745860d76137da68f362380c606c"), 21); ck_assert_mem_eq(decode, fromhex("0079fbfc3f34e7745860d76137da68f362380c606c"), 21);
res = ecdsa_address_decode("mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV", decode); res = ecdsa_address_decode("mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV", 111, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("6f79fbfc3f34e7745860d76137da68f362380c606c"), 21); ck_assert_mem_eq(decode, fromhex("6f79fbfc3f34e7745860d76137da68f362380c606c"), 21);
res = ecdsa_address_decode("N7hMq7AmgNsQXaYARrEwybbDGei9mcPNqr", decode); res = ecdsa_address_decode("N7hMq7AmgNsQXaYARrEwybbDGei9mcPNqr", 52, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("3479fbfc3f34e7745860d76137da68f362380c606c"), 21); ck_assert_mem_eq(decode, fromhex("3479fbfc3f34e7745860d76137da68f362380c606c"), 21);
res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", decode); res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 48, decode);
ck_assert_int_eq(res, 1); ck_assert_int_eq(res, 1);
ck_assert_mem_eq(decode, fromhex("3079fbfc3f34e7745860d76137da68f362380c606c"), 21); ck_assert_mem_eq(decode, fromhex("3079fbfc3f34e7745860d76137da68f362380c606c"), 21);
// invalid char // invalid char
res = ecdsa_address_decode("1JwSSubhmg6i000jtyqhUYYH7bZg3Lfy1T", decode); res = ecdsa_address_decode("1JwSSubhmg6i000jtyqhUYYH7bZg3Lfy1T", 0, decode);
ck_assert_int_eq(res, 0); ck_assert_int_eq(res, 0);
// invalid address // invalid address
res = ecdsa_address_decode("1111Subhmg6iPtRjtyqhUYYH7bZg3Lfy1T", decode); res = ecdsa_address_decode("1111Subhmg6iPtRjtyqhUYYH7bZg3Lfy1T", 0, decode);
ck_assert_int_eq(res, 0);
// invalid version
res = ecdsa_address_decode("LWLwtfycqf1uFqypLAug36W4kdgNwrZdNs", 0, decode);
ck_assert_int_eq(res, 0); ck_assert_int_eq(res, 0);
} }
END_TEST END_TEST

View File

@ -44,7 +44,7 @@ int main(int argc, char **argv)
fprintf(stderr, "\"%s\" is not a valid mnemonic\n", mnemonic); fprintf(stderr, "\"%s\" is not a valid mnemonic\n", mnemonic);
return 2; return 2;
} }
if (!ecdsa_address_decode(address, addr)) { if (!ecdsa_address_decode(address, 0, addr)) {
fprintf(stderr, "\"%s\" is not a valid address\n", address); fprintf(stderr, "\"%s\" is not a valid address\n", address);
return 3; return 3;
} }