From 03c681ec497662ca520691aabcab708b3fb4e6dc Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sun, 21 Oct 2018 19:16:15 +0200 Subject: [PATCH] embed/extmod/modtrezorcrypto: introduce CANONICAL_SIG_ETHEREUM constant --- .../modtrezorcrypto-secp256k1.h | 19 +++++++++++++------ src/apps/ethereum/sign_message.py | 2 +- src/apps/ethereum/sign_tx.py | 2 +- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/embed/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h b/embed/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h index 5a1bc6de79..f9eda27956 100644 --- a/embed/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +++ b/embed/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h @@ -62,13 +62,17 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_publickey(size_t n_args, const mp_obj } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_publickey_obj, 1, 2, mod_trezorcrypto_secp256k1_publickey); -static int ethereum_is_canonic(uint8_t v, uint8_t signature[64]) +static int ethereum_is_canonical(uint8_t v, uint8_t signature[64]) { - (void) signature; + (void)signature; return (v & 2) == 0; } -/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True, ethereum_canonical: bool = False) -> bytes: +enum { + CANONICAL_SIG_ETHEREUM = 1, +}; + +/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True, canonical: int = None) -> bytes: /// ''' /// Uses secret key to produce the signature of the digest. /// ''' @@ -77,10 +81,12 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_sign(size_t n_args, const mp_obj_t *a mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ); mp_get_buffer_raise(args[1], &dig, MP_BUFFER_READ); bool compressed = (n_args < 3) || (args[2] == mp_const_true); - bool ethereum_canonical = (n_args > 3) && (args[3] == mp_const_true); + mp_int_t canonical = (n_args > 3) ? mp_obj_get_int(args[3]) : 0; int (*is_canonical)(uint8_t by, uint8_t sig[64]) = NULL; - if (ethereum_canonical) { - is_canonical = ethereum_is_canonic; + switch (canonical) { + case CANONICAL_SIG_ETHEREUM: + is_canonical = ethereum_is_canonical; + break; } if (sk.len != 32) { mp_raise_ValueError("Invalid length of secret key"); @@ -186,6 +192,7 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_secp256k1_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_obj) }, { MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_recover_obj) }, { MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_multiply_obj) }, + { MP_ROM_QSTR(MP_QSTR_CANONICAL_SIG_ETHEREUM), MP_OBJ_NEW_SMALL_INT(CANONICAL_SIG_ETHEREUM) }, }; STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_secp256k1_globals, mod_trezorcrypto_secp256k1_globals_table); diff --git a/src/apps/ethereum/sign_message.py b/src/apps/ethereum/sign_message.py index 349b297c0e..e4bc7d80fa 100644 --- a/src/apps/ethereum/sign_message.py +++ b/src/apps/ethereum/sign_message.py @@ -24,7 +24,7 @@ async def sign_message(ctx, msg): address_n = msg.address_n or () node = await seed.derive_node(ctx, address_n) - signature = secp256k1.sign(node.private_key(), message_digest(msg.message), False) + signature = secp256k1.sign(node.private_key(), message_digest(msg.message), False, secp256k1.CANONICAL_SIG_ETHEREUM) sig = EthereumMessageSignature() sig.address = node.ethereum_pubkeyhash() diff --git a/src/apps/ethereum/sign_tx.py b/src/apps/ethereum/sign_tx.py index de42dabf95..28650ce011 100644 --- a/src/apps/ethereum/sign_tx.py +++ b/src/apps/ethereum/sign_tx.py @@ -131,7 +131,7 @@ async def send_signature(ctx, msg: EthereumSignTx, digest): address_n = msg.address_n or () node = await seed.derive_node(ctx, address_n) - signature = secp256k1.sign(node.private_key(), digest, False, True) + signature = secp256k1.sign(node.private_key(), digest, False, secp256k1.CANONICAL_SIG_ETHEREUM) req = EthereumTxRequest() req.signature_v = signature[0]