1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 14:28:07 +00:00

Implement ecdsa_get_ethereum_pubkeyhash()

This commit is contained in:
Alex Beregszaszi 2016-05-17 18:12:13 +01:00
parent 7d68a6ee17
commit 1b8e3d557f
3 changed files with 31 additions and 1 deletions

24
ecdsa.c
View File

@ -36,6 +36,9 @@
#include "base58.h"
#include "macros.h"
#include "secp256k1.h"
#if USE_ETHEREUM
#include "sha3.h"
#endif
// Set cp2 = cp1
void point_copy(const curve_point *cp1, curve_point *cp2)
@ -850,6 +853,27 @@ void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash)
MEMSET_BZERO(h, sizeof(h));
}
#if USE_ETHEREUM
int ecdsa_get_ethereum_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash)
{
uint8_t h[65];
SHA3_CTX ctx;
if (!ecdsa_uncompress_pubkey(&secp256k1, pub_key, h)) {
return 0;
}
sha3_256_Init(&ctx);
sha3_Update(&ctx, h + 1, 64);
keccak_Final(&ctx, h);
// least significant 160 bits
memcpy(pubkeyhash, h + 12, 20);
return 1;
}
#endif
void ecdsa_get_address_raw(const uint8_t *pub_key, uint8_t version, uint8_t *addr_raw)
{
addr_raw[0] = version;

View File

@ -66,6 +66,7 @@ int ecdsa_sign_digest(const ecdsa_curve *curve, const uint8_t *priv_key, const u
void ecdsa_get_public_key33(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key);
void ecdsa_get_public_key65(const ecdsa_curve *curve, const uint8_t *priv_key, uint8_t *pub_key);
void ecdsa_get_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash);
int ecdsa_get_ethereum_pubkeyhash(const uint8_t *pub_key, uint8_t *pubkeyhash);
void ecdsa_get_address_raw(const uint8_t *pub_key, uint8_t version, uint8_t *addr_raw);
void ecdsa_get_address(const uint8_t *pub_key, uint8_t version, char *addr, int addrsize);
void ecdsa_get_wif(const uint8_t *priv_key, uint8_t version, char *wif, int wifsize);

View File

@ -56,9 +56,14 @@
#define BIP39_CACHE_SIZE 4
#endif
// support Ethereum operations
#ifndef USE_ETHEREUM
#define USE_ETHEREUM 0
#endif
// support Keccak hashing
#ifndef USE_KECCAK
#define USE_KECCAK 0
#define USE_KECCAK USE_ETHEREUM
#endif
#endif