From c0181b1aec2d4235d9684635a732cdca3355ae99 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Mon, 10 Oct 2016 11:26:52 +0200 Subject: [PATCH] extract address related stuff into trezor-crypto --- firmware/Makefile | 1 + firmware/coins.c | 41 ++++++----------------------------------- firmware/coins.h | 5 +---- firmware/crypto.c | 5 +++-- firmware/transaction.c | 9 +++++---- vendor/trezor-crypto | 2 +- 6 files changed, 17 insertions(+), 46 deletions(-) diff --git a/firmware/Makefile b/firmware/Makefile index 845d3c02c..9c60e6522 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -21,6 +21,7 @@ OBJS += ethereum.o OBJS += debug.o +OBJS += ../vendor/trezor-crypto/address.o OBJS += ../vendor/trezor-crypto/bignum.o OBJS += ../vendor/trezor-crypto/ecdsa.o OBJS += ../vendor/trezor-crypto/curves.o diff --git a/firmware/coins.c b/firmware/coins.c index b9eb7468b..485377073 100644 --- a/firmware/coins.c +++ b/firmware/coins.c @@ -19,6 +19,7 @@ #include #include "coins.h" +#include "address.h" // filled CoinType Protobuf structure defined in https://github.com/trezor/trezor-common/blob/master/protob/types.proto#L133 // address types > 0xFF represent a two-byte prefix in big-endian order @@ -68,51 +69,21 @@ const CoinType *coinByAddressType(uint32_t address_type) return 0; } -size_t prefixBytesByAddressType(uint32_t address_type) +bool coinExtractAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type) { - if (address_type <= 0xFF) return 1; - if (address_type <= 0xFFFF) return 2; - if (address_type <= 0xFFFFFF) return 3; - return 4; -} - -bool addressHasExpectedPrefix(const uint8_t *addr, uint32_t address_type) -{ - if (address_type <= 0xFF) { - return address_type == (uint32_t)(addr[0]); - } - if (address_type <= 0xFFFF) { - return address_type == ((uint32_t)(addr[0] << 8) | (uint32_t)(addr[1])); - } - if (address_type <= 0xFFFFFF) { - return address_type == ((uint32_t)(addr[0] << 16) | (uint32_t)(addr[1] << 8) | (uint32_t)(addr[2])); - } - return address_type == ((uint32_t)(addr[0] << 24) | (uint32_t)(addr[1] << 16) | (uint32_t)(addr[2] << 8) | (uint32_t)(addr[3])); -} - -void writeAddressPrefix(uint8_t *addr, uint32_t address_type) -{ - if (address_type > 0xFFFFFF) *(addr++) = address_type >> 24; - if (address_type > 0xFFFF) *(addr++) = (address_type >> 16) & 0xFF; - if (address_type > 0xFF) *(addr++) = (address_type >> 8) & 0xFF; - *(addr++) = address_type & 0xFF; -} - -bool getAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type) -{ - if (coin->has_address_type && addressHasExpectedPrefix(addr, coin->address_type)) { + if (coin->has_address_type && address_check_prefix(addr, coin->address_type)) { *address_type = coin->address_type; return true; } - if (coin->has_address_type_p2sh && addressHasExpectedPrefix(addr, coin->address_type_p2sh)) { + if (coin->has_address_type_p2sh && address_check_prefix(addr, coin->address_type_p2sh)) { *address_type = coin->address_type_p2sh; return true; } - if (coin->has_address_type_p2wpkh && addressHasExpectedPrefix(addr, coin->address_type_p2wpkh)) { + if (coin->has_address_type_p2wpkh && address_check_prefix(addr, coin->address_type_p2wpkh)) { *address_type = coin->address_type_p2wpkh; return true; } - if (coin->has_address_type_p2wsh && addressHasExpectedPrefix(addr, coin->address_type_p2wsh)) { + if (coin->has_address_type_p2wsh && address_check_prefix(addr, coin->address_type_p2wsh)) { *address_type = coin->address_type_p2wsh; return true; } diff --git a/firmware/coins.h b/firmware/coins.h index 5acd61e52..69cb002fe 100644 --- a/firmware/coins.h +++ b/firmware/coins.h @@ -29,9 +29,6 @@ extern const CoinType coins[COINS_COUNT]; const CoinType *coinByShortcut(const char *shortcut); const CoinType *coinByName(const char *name); const CoinType *coinByAddressType(uint32_t address_type); -size_t prefixBytesByAddressType(uint32_t address_type); -bool addressHasExpectedPrefix(const uint8_t *addr, uint32_t address_type); -void writeAddressPrefix(uint8_t *addr, uint32_t address_type); -bool getAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type); +bool coinExtractAddressType(const CoinType *coin, const uint8_t *addr, uint32_t *address_type); #endif diff --git a/firmware/crypto.c b/firmware/crypto.c index de0e8e91c..1728793b8 100644 --- a/firmware/crypto.c +++ b/firmware/crypto.c @@ -27,6 +27,7 @@ #include "layout.h" #include "curves.h" #include "secp256k1.h" +#include "address.h" #include "macros.h" #include "coins.h" @@ -173,11 +174,11 @@ int cryptoMessageVerify(const CoinType *coin, const uint8_t *message, size_t mes } // check if the address is correct uint32_t address_type; - if (!getAddressType(coin, address_raw, &address_type)) { + if (!coinExtractAddressType(coin, address_raw, &address_type)) { return 2; } ecdsa_get_address_raw(pubkey, address_type, addr_raw); - if (memcmp(addr_raw, address_raw, prefixBytesByAddressType(address_type) + 20) != 0) { + if (memcmp(addr_raw, address_raw, address_prefix_bytes_len(address_type) + 20) != 0) { return 2; } return 0; diff --git a/firmware/transaction.c b/firmware/transaction.c index 54801a376..2513e5869 100644 --- a/firmware/transaction.c +++ b/firmware/transaction.c @@ -28,6 +28,7 @@ #include "crypto.h" #include "ripemd160.h" #include "base58.h" +#include "address.h" #include "messages.pb.h" #include "types.pb.h" @@ -91,7 +92,7 @@ int compile_output(const CoinType *coin, const HDNode *root, TxOutputType *in, T out->script_pubkey.bytes[0] = 0x76; // OP_DUP out->script_pubkey.bytes[1] = 0xA9; // OP_HASH_160 out->script_pubkey.bytes[2] = 0x14; // pushing 20 bytes - memcpy(out->script_pubkey.bytes + 3, addr_raw + prefixBytesByAddressType(coin->address_type), 20); + memcpy(out->script_pubkey.bytes + 3, addr_raw + address_prefix_bytes_len(coin->address_type), 20); out->script_pubkey.bytes[23] = 0x88; // OP_EQUALVERIFY out->script_pubkey.bytes[24] = 0xAC; // OP_CHECKSIG out->script_pubkey.size = 25; @@ -110,7 +111,7 @@ int compile_output(const CoinType *coin, const HDNode *root, TxOutputType *in, T } out->script_pubkey.bytes[0] = 0xA9; // OP_HASH_160 out->script_pubkey.bytes[1] = 0x14; // pushing 20 bytes - memcpy(out->script_pubkey.bytes + 2, addr_raw + prefixBytesByAddressType(coin->address_type_p2sh), 20); + memcpy(out->script_pubkey.bytes + 2, addr_raw + address_prefix_bytes_len(coin->address_type_p2sh), 20); out->script_pubkey.bytes[22] = 0x87; // OP_EQUAL out->script_pubkey.size = 23; return 23; @@ -118,14 +119,14 @@ int compile_output(const CoinType *coin, const HDNode *root, TxOutputType *in, T if (in->script_type == OutputScriptType_PAYTOMULTISIG) { uint8_t buf[32]; - size_t prefix_bytes = prefixBytesByAddressType(coin->address_type_p2sh); + size_t prefix_bytes = address_prefix_bytes_len(coin->address_type_p2sh); if (!in->has_multisig) { return 0; } if (compile_script_multisig_hash(&(in->multisig), buf) == 0) { return 0; } - writeAddressPrefix(addr_raw, coin->address_type_p2sh); + address_write_prefix_bytes(coin->address_type_p2sh, addr_raw); ripemd160(buf, 32, addr_raw + prefix_bytes); if (needs_confirm) { base58_encode_check(addr_raw, prefix_bytes + 20, in->address, sizeof(in->address)); diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto index ad73c0d4e..b05776be7 160000 --- a/vendor/trezor-crypto +++ b/vendor/trezor-crypto @@ -1 +1 @@ -Subproject commit ad73c0d4e73fe138ebbbc39d6a335167ba7c9923 +Subproject commit b05776be77168738d94ef9963019abb4d80a5356