mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-13 19:18:56 +00:00
Multi-byte prefix cleanup
use the functions from address.c in ecdsa.c to avoid duplicated code.
This commit is contained in:
parent
b55473a01e
commit
9443aefa9a
80
ecdsa.c
80
ecdsa.c
@ -27,6 +27,7 @@
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "address.h"
|
||||
#include "bignum.h"
|
||||
#include "rand.h"
|
||||
#include "sha2.h"
|
||||
@ -881,40 +882,17 @@ void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash)
|
||||
|
||||
void ecdsa_get_address_raw(const uint8_t *pub_key, uint32_t version, uint8_t *addr_raw)
|
||||
{
|
||||
if (version <= 0xFF) {
|
||||
addr_raw[0] = version;
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + 1);
|
||||
} else if (version <= 0xFFFF) {
|
||||
addr_raw[0] = version >> 8;
|
||||
addr_raw[1] = version & 0xFF;
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + 2);
|
||||
} else if (version <= 0xFFFFFF) {
|
||||
addr_raw[0] = version >> 16;
|
||||
addr_raw[1] = (version >> 8) & 0xFF;
|
||||
addr_raw[2] = version & 0xFF;
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + 3);
|
||||
} else {
|
||||
addr_raw[0] = version >> 24;
|
||||
addr_raw[1] = (version >> 16) & 0xFF;
|
||||
addr_raw[2] = (version >> 8) & 0xFF;
|
||||
addr_raw[3] = version & 0xFF;
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + 4);
|
||||
}
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
address_write_prefix_bytes(version, addr_raw);
|
||||
ecdsa_get_pubkeyhash(pub_key, addr_raw + prefix_len);
|
||||
}
|
||||
|
||||
void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int addrsize)
|
||||
{
|
||||
uint8_t raw[MAX_ADDR_RAW_SIZE];
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
ecdsa_get_address_raw(pub_key, version, raw);
|
||||
if (version <= 0xFF) {
|
||||
base58_encode_check(raw, 21, addr, addrsize);
|
||||
} else if (version <= 0xFFFF) {
|
||||
base58_encode_check(raw, 22, addr, addrsize);
|
||||
} else if (version <= 0xFFFFFF) {
|
||||
base58_encode_check(raw, 23, addr, addrsize);
|
||||
} else {
|
||||
base58_encode_check(raw, 24, addr, addrsize);
|
||||
}
|
||||
base58_encode_check(raw, 20 + prefix_len, addr, addrsize);
|
||||
// not as important to clear this one, but we might as well
|
||||
MEMSET_BZERO(raw, sizeof(raw));
|
||||
}
|
||||
@ -922,35 +900,11 @@ void ecdsa_get_address(const uint8_t *pub_key, uint32_t version, char *addr, int
|
||||
void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wifsize)
|
||||
{
|
||||
uint8_t wif_raw[MAX_WIF_RAW_SIZE];
|
||||
|
||||
if (version <= 0xFF) {
|
||||
wif_raw[0] = version;
|
||||
memcpy(wif_raw + 1, priv_key, 32);
|
||||
wif_raw[33] = 0x01;
|
||||
base58_encode_check(wif_raw, 1 + 32 + 1, wif, wifsize);
|
||||
} else if (version <= 0xFFFF) {
|
||||
wif_raw[0] = version >> 8;
|
||||
wif_raw[1] = version & 0xFF;
|
||||
memcpy(wif_raw + 2, priv_key, 32);
|
||||
wif_raw[34] = 0x01;
|
||||
base58_encode_check(wif_raw, 2 + 32 + 1, wif, wifsize);
|
||||
} else if (version <= 0xFFFFFF) {
|
||||
wif_raw[0] = version >> 16;
|
||||
wif_raw[1] = (version >> 8) & 0xFF;
|
||||
wif_raw[2] = version & 0xFF;
|
||||
memcpy(wif_raw + 3, priv_key, 32);
|
||||
wif_raw[35] = 0x01;
|
||||
base58_encode_check(wif_raw, 3 + 32 + 1, wif, wifsize);
|
||||
} else {
|
||||
wif_raw[0] = version >> 24;
|
||||
wif_raw[1] = (version >> 16) & 0xFF;
|
||||
wif_raw[2] = (version >> 8) & 0xFF;
|
||||
wif_raw[3] = version & 0xFF;
|
||||
memcpy(wif_raw + 4, priv_key, 32);
|
||||
wif_raw[36] = 0x01;
|
||||
base58_encode_check(wif_raw, 4 + 32 + 1, wif, wifsize);
|
||||
}
|
||||
|
||||
size_t prefix_len = address_prefix_bytes_len(version);
|
||||
address_write_prefix_bytes(version, wif_raw);
|
||||
memcpy(wif_raw + prefix_len, priv_key, 32);
|
||||
wif_raw[prefix_len + 32] = 0x01;
|
||||
base58_encode_check(wif_raw, prefix_len + 32 + 1, wif, wifsize);
|
||||
// private keys running around our stack can cause trouble
|
||||
MEMSET_BZERO(wif_raw, sizeof(wif_raw));
|
||||
}
|
||||
@ -958,15 +912,9 @@ void ecdsa_get_wif(const uint8_t *priv_key, uint32_t version, char *wif, int wif
|
||||
int ecdsa_address_decode(const char *addr, uint32_t version, uint8_t *out)
|
||||
{
|
||||
if (!addr) return 0;
|
||||
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) & 0xFF) && out[1] == (version & 0xFF);
|
||||
} else if (version <= 0xFFFFFF) {
|
||||
return base58_decode_check(addr, out, 23) == 23 && out[0] == ((version >> 16) & 0xFF) && 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);
|
||||
}
|
||||
int prefix_len = address_prefix_bytes_len(version);
|
||||
return base58_decode_check(addr, out, 20 + prefix_len) == 20 + prefix_len
|
||||
&& address_check_prefix(out, version);
|
||||
}
|
||||
|
||||
void uncompress_coords(const ecdsa_curve *curve, uint8_t odd, const bignum256 *x, bignum256 *y)
|
||||
|
Loading…
Reference in New Issue
Block a user