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

crypto: Use Hasher for message signing

This commit is contained in:
Saleem Rashid 2017-12-09 16:38:27 +00:00 committed by Pavol Rusnak
parent a1e911aa4c
commit 0e60ba54b7
2 changed files with 17 additions and 22 deletions

View File

@ -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) static void cryptoMessageHash(const CoinInfo *coin, const uint8_t *message, size_t message_len, uint8_t hash[HASHER_DIGEST_LENGTH]) {
{ Hasher hasher;
SHA256_CTX ctx; hasher_Init(&hasher, coin->hasher_type);
sha256_Init(&ctx); hasher_Update(&hasher, (const uint8_t *)coin->signed_message_header, strlen(coin->signed_message_header));
sha256_Update(&ctx, (const uint8_t *)coin->signed_message_header, strlen(coin->signed_message_header));
uint8_t varint[5]; uint8_t varint[5];
uint32_t l = ser_length(message_len, varint); uint32_t l = ser_length(message_len, varint);
sha256_Update(&ctx, varint, l); hasher_Update(&hasher, varint, l);
sha256_Update(&ctx, message, message_len); hasher_Update(&hasher, message, message_len);
uint8_t hash[32]; hasher_Double(&hasher, hash);
sha256_Final(&ctx, hash); }
sha256_Raw(hash, 32, 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; uint8_t pby;
int result = hdnode_sign_digest(node, hash, signature + 1, &pby, NULL); int result = hdnode_sign_digest(node, hash, signature + 1, &pby, NULL);
if (result == 0) { if (result == 0) {
@ -152,17 +156,8 @@ int cryptoMessageVerify(const CoinInfo *coin, const uint8_t *message, size_t mes
return 1; return 1;
} }
// calculate hash uint8_t hash[HASHER_DIGEST_LENGTH];
SHA256_CTX ctx; cryptoMessageHash(coin, message, message_len, hash);
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 recid = (signature[0] - 27) % 4; uint8_t recid = (signature[0] - 27) % 4;
bool compressed = signature[0] >= 31; bool compressed = signature[0] >= 31;

@ -1 +1 @@
Subproject commit 95a522bf1a453880050521661258d7943e966d1f Subproject commit 764cc4c6e8ef32e7e1a77f0496ae090f11a36def