1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 15:28:10 +00:00

fix(crypto): refactor ethereum_address_checksum to prepend "0x" (#2029)

[no changelog]
This commit is contained in:
Pavol Rusnak 2022-01-12 21:00:54 +01:00 committed by GitHub
parent 8bcd52fc1b
commit 2880a2fa33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 49 deletions

View File

@ -60,11 +60,13 @@ bool address_check_prefix(const uint8_t *addr, uint32_t address_type) {
void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60, void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
uint64_t chain_id) { uint64_t chain_id) {
const char *hex = "0123456789abcdef"; const char *hex = "0123456789abcdef";
address[0] = '0';
address[1] = 'x';
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
address[i * 2] = hex[(addr[i] >> 4) & 0xF]; address[2 + i * 2] = hex[(addr[i] >> 4) & 0xF];
address[i * 2 + 1] = hex[addr[i] & 0xF]; address[2 + i * 2 + 1] = hex[addr[i] & 0xF];
} }
address[40] = 0; address[42] = 0;
SHA3_CTX ctx = {0}; SHA3_CTX ctx = {0};
uint8_t hash[32] = {0}; uint8_t hash[32] = {0};
@ -75,16 +77,17 @@ void ethereum_address_checksum(const uint8_t *addr, char *address, bool rskip60,
prefix, sizeof(prefix)); prefix, sizeof(prefix));
keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size); keccak_Update(&ctx, (const uint8_t *)prefix, prefix_size);
} }
keccak_Update(&ctx, (const uint8_t *)address, 40); keccak_Update(&ctx, (const uint8_t *)(address + 2), 40);
keccak_Final(&ctx, hash); keccak_Final(&ctx, hash);
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
if (hash[i] & 0x80 && address[i * 2] >= 'a' && address[i * 2] <= 'f') { if ((hash[i] & 0x80) && address[2 + i * 2] >= 'a' &&
address[i * 2] -= 0x20; address[2 + i * 2] <= 'f') {
address[2 + i * 2] -= 0x20;
} }
if (hash[i] & 0x08 && address[i * 2 + 1] >= 'a' && if ((hash[i] & 0x08) && address[2 + i * 2 + 1] >= 'a' &&
address[i * 2 + 1] <= 'f') { address[2 + i * 2 + 1] <= 'f') {
address[i * 2 + 1] -= 0x20; address[2 + i * 2 + 1] -= 0x20;
} }
} }
} }

View File

@ -7322,25 +7322,25 @@ START_TEST(test_ethereum_pubkeyhash) {
END_TEST END_TEST
START_TEST(test_ethereum_address) { START_TEST(test_ethereum_address) {
static const char *vectors[] = {"52908400098527886E0F7030069857D2E4169EE7", static const char *vectors[] = {"0x52908400098527886E0F7030069857D2E4169EE7",
"8617E340B3D01FA5F11F306F4090FD50E238070D", "0x8617E340B3D01FA5F11F306F4090FD50E238070D",
"de709f2102306220921060314715629080e2fb77", "0xde709f2102306220921060314715629080e2fb77",
"27b1fdb04752bbc536007a920d24acb045561c26", "0x27b1fdb04752bbc536007a920d24acb045561c26",
"5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed", "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
"fB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
"dbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB", "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
"D1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
"5A4EAB120fB44eb6684E5e32785702FF45ea344D", "0x5A4EAB120fB44eb6684E5e32785702FF45ea344D",
"5be4BDC48CeF65dbCbCaD5218B1A7D37F58A0741", "0x5be4BDC48CeF65dbCbCaD5218B1A7D37F58A0741",
"a7dD84573f5ffF821baf2205745f768F8edCDD58", "0xa7dD84573f5ffF821baf2205745f768F8edCDD58",
"027a49d11d118c0060746F1990273FcB8c2fC196", "0x027a49d11d118c0060746F1990273FcB8c2fC196",
"CD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
0}; 0};
uint8_t addr[20]; uint8_t addr[20];
char address[41]; char address[43];
const char **vec = vectors; const char **vec = vectors;
while (*vec) { while (*vec) {
memcpy(addr, fromhex(*vec), 20); memcpy(addr, fromhex(*vec + 2), 20);
ethereum_address_checksum(addr, address, false, 0); ethereum_address_checksum(addr, address, false, 0);
ck_assert_str_eq(address, *vec); ck_assert_str_eq(address, *vec);
vec++; vec++;
@ -7352,29 +7352,29 @@ END_TEST
// https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md // https://github.com/rsksmart/RSKIPs/blob/master/IPs/RSKIP60.md
START_TEST(test_rsk_address) { START_TEST(test_rsk_address) {
uint8_t addr[20]; uint8_t addr[20];
char address[41]; char address[43];
static const char *rskip60_chain30[] = { static const char *rskip60_chain30[] = {
"5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD", "0x5aaEB6053f3e94c9b9a09f33669435E7ef1bEAeD",
"Fb6916095cA1Df60bb79ce92cE3EA74c37c5d359", "0xFb6916095cA1Df60bb79ce92cE3EA74c37c5d359",
"DBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB", "0xDBF03B407c01E7CD3cBea99509D93F8Dddc8C6FB",
"D1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB", 0}; "0xD1220A0Cf47c7B9BE7a2e6ba89F429762E7B9adB", 0};
const char **vec = rskip60_chain30; const char **vec = rskip60_chain30;
while (*vec) { while (*vec) {
memcpy(addr, fromhex(*vec), 20); memcpy(addr, fromhex(*vec + 2), 20);
ethereum_address_checksum(addr, address, true, 30); ethereum_address_checksum(addr, address, true, 30);
ck_assert_str_eq(address, *vec); ck_assert_str_eq(address, *vec);
vec++; vec++;
} }
static const char *rskip60_chain31[] = { static const char *rskip60_chain31[] = {
"5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd", "0x5aAeb6053F3e94c9b9A09F33669435E7EF1BEaEd",
"Fb6916095CA1dF60bb79CE92ce3Ea74C37c5D359", "0xFb6916095CA1dF60bb79CE92ce3Ea74C37c5D359",
"dbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB", "0xdbF03B407C01E7cd3cbEa99509D93f8dDDc8C6fB",
"d1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB", 0}; "0xd1220a0CF47c7B9Be7A2E6Ba89f429762E7b9adB", 0};
vec = rskip60_chain31; vec = rskip60_chain31;
while (*vec) { while (*vec) {
memcpy(addr, fromhex(*vec), 20); memcpy(addr, fromhex(*vec + 2), 20);
ethereum_address_checksum(addr, address, true, 31); ethereum_address_checksum(addr, address, true, 31);
ck_assert_str_eq(address, *vec); ck_assert_str_eq(address, *vec);
vec++; vec++;

View File

@ -354,12 +354,12 @@ static void layoutEthereumConfirmTx(const uint8_t *to, uint32_t to_len,
ethereumFormatAmount(&val, token, amount, sizeof(amount)); ethereumFormatAmount(&val, token, amount, sizeof(amount));
} }
char _to1[] = "to 0x__________"; char _to1[] = "to ____________";
char _to2[] = "_______________"; char _to2[] = "_______________";
char _to3[] = "_______________?"; char _to3[] = "_______________?";
if (to_len) { if (to_len) {
char to_str[41] = {0}; char to_str[43] = {0};
bool rskip60 = false; bool rskip60 = false;
// constants from trezor-common/defs/ethereum/networks.json // constants from trezor-common/defs/ethereum/networks.json
@ -373,9 +373,9 @@ static void layoutEthereumConfirmTx(const uint8_t *to, uint32_t to_len,
} }
ethereum_address_checksum(to, to_str, rskip60, chain_id); ethereum_address_checksum(to, to_str, rskip60, chain_id);
memcpy(_to1 + 5, to_str, 10); memcpy(_to1 + 3, to_str, 12);
memcpy(_to2, to_str + 10, 15); memcpy(_to2, to_str + 12, 15);
memcpy(_to3, to_str + 25, 15); memcpy(_to3, to_str + 27, 15);
} else { } else {
strlcpy(_to1, _("to new contract?"), sizeof(_to1)); strlcpy(_to1, _("to new contract?"), sizeof(_to1));
strlcpy(_to2, "", sizeof(_to2)); strlcpy(_to2, "", sizeof(_to2));

View File

@ -126,9 +126,7 @@ void fsm_msgEthereumGetAddress(const EthereumGetAddress *msg) {
} }
resp->has_address = true; resp->has_address = true;
resp->address[0] = '0'; ethereum_address_checksum(pubkeyhash, resp->address, rskip60, chain_id);
resp->address[1] = 'x';
ethereum_address_checksum(pubkeyhash, resp->address + 2, rskip60, chain_id);
// ethereum_address_checksum adds trailing zero // ethereum_address_checksum adds trailing zero
if (msg->has_show_display && msg->show_display) { if (msg->has_show_display && msg->show_display) {
@ -161,9 +159,7 @@ void fsm_msgEthereumSignMessage(const EthereumSignMessage *msg) {
return; return;
} }
resp->address[0] = '0'; ethereum_address_checksum(pubkeyhash, resp->address, false, 0);
resp->address[1] = 'x';
ethereum_address_checksum(pubkeyhash, resp->address + 2, false, 0);
// ethereum_address_checksum adds trailing zero // ethereum_address_checksum adds trailing zero
layoutVerifyAddress(NULL, resp->address); layoutVerifyAddress(NULL, resp->address);
@ -243,9 +239,7 @@ void fsm_msgEthereumSignTypedHash(const EthereumSignTypedHash *msg) {
return; return;
} }
resp->address[0] = '0'; ethereum_address_checksum(pubkeyhash, resp->address, false, 0);
resp->address[1] = 'x';
ethereum_address_checksum(pubkeyhash, resp->address + 2, false, 0);
// ethereum_address_checksum adds trailing zero // ethereum_address_checksum adds trailing zero
layoutVerifyAddress(NULL, resp->address); layoutVerifyAddress(NULL, resp->address);