From 0e60ba54b77615e6c687b6838ee744afa5668aa7 Mon Sep 17 00:00:00 2001 From: Saleem Rashid Date: Sat, 9 Dec 2017 16:38:27 +0000 Subject: [PATCH] crypto: Use Hasher for message signing --- firmware/crypto.c | 37 ++++++++++++++++--------------------- vendor/trezor-crypto | 2 +- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/firmware/crypto.c b/firmware/crypto.c index 17b67a468a..924e5766a3 100644 --- a/firmware/crypto.c +++ b/firmware/crypto.c @@ -112,18 +112,22 @@ int gpgMessageSign(HDNode *node, const uint8_t *message, size_t message_len, uin } } -int cryptoMessageSign(const CoinInfo *coin, HDNode *node, InputScriptType script_type, const uint8_t *message, size_t message_len, uint8_t *signature) -{ - SHA256_CTX ctx; - sha256_Init(&ctx); - sha256_Update(&ctx, (const uint8_t *)coin->signed_message_header, strlen(coin->signed_message_header)); +static void cryptoMessageHash(const CoinInfo *coin, const uint8_t *message, size_t message_len, uint8_t hash[HASHER_DIGEST_LENGTH]) { + Hasher hasher; + hasher_Init(&hasher, coin->hasher_type); + hasher_Update(&hasher, (const uint8_t *)coin->signed_message_header, strlen(coin->signed_message_header)); uint8_t varint[5]; uint32_t l = ser_length(message_len, varint); - sha256_Update(&ctx, varint, l); - sha256_Update(&ctx, message, message_len); - uint8_t hash[32]; - sha256_Final(&ctx, hash); - sha256_Raw(hash, 32, hash); + hasher_Update(&hasher, varint, l); + hasher_Update(&hasher, message, message_len); + hasher_Double(&hasher, hash); +} + +int cryptoMessageSign(const CoinInfo *coin, HDNode *node, InputScriptType script_type, const uint8_t *message, size_t message_len, uint8_t *signature) +{ + uint8_t hash[HASHER_DIGEST_LENGTH]; + cryptoMessageHash(coin, message, message_len, hash); + uint8_t pby; int result = hdnode_sign_digest(node, hash, signature + 1, &pby, NULL); if (result == 0) { @@ -152,17 +156,8 @@ int cryptoMessageVerify(const CoinInfo *coin, const uint8_t *message, size_t mes return 1; } - // calculate hash - SHA256_CTX ctx; - sha256_Init(&ctx); - sha256_Update(&ctx, (const uint8_t *)coin->signed_message_header, strlen(coin->signed_message_header)); - uint8_t varint[5]; - uint32_t l = ser_length(message_len, varint); - sha256_Update(&ctx, varint, l); - sha256_Update(&ctx, message, message_len); - uint8_t hash[32]; - sha256_Final(&ctx, hash); - sha256_Raw(hash, 32, hash); + uint8_t hash[HASHER_DIGEST_LENGTH]; + cryptoMessageHash(coin, message, message_len, hash); uint8_t recid = (signature[0] - 27) % 4; bool compressed = signature[0] >= 31; diff --git a/vendor/trezor-crypto b/vendor/trezor-crypto index 95a522bf1a..764cc4c6e8 160000 --- a/vendor/trezor-crypto +++ b/vendor/trezor-crypto @@ -1 +1 @@ -Subproject commit 95a522bf1a453880050521661258d7943e966d1f +Subproject commit 764cc4c6e8ef32e7e1a77f0496ae090f11a36def