1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 21:22:10 +00:00

hasher: don't hardcore zcash stuff into hasher

This commit is contained in:
Pavol Rusnak 2019-01-31 19:05:53 +01:00
parent b7e99aa76c
commit 8c2bac9594
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 18 additions and 32 deletions

View File

@ -23,8 +23,9 @@
#include "hasher.h"
#include "ripemd160.h"
void hasher_Init(Hasher *hasher, HasherType type) {
void hasher_InitParam(Hasher *hasher, HasherType type, const void *param) {
hasher->type = type;
hasher->param = param;
switch (hasher->type) {
case HASHER_SHA2:
@ -46,26 +47,17 @@ void hasher_Init(Hasher *hasher, HasherType type) {
case HASHER_GROESTLD_TRUNC:
groestl512_Init(&hasher->ctx.groestl);
break;
case HASHER_OVERWINTER_PREVOUTS:
blake2b_InitPersonal(&hasher->ctx.blake2b, 32, "ZcashPrevoutHash", 16);
case HASHER_BLAKE2B:
blake2b_Init(&hasher->ctx.blake2b, 32);
break;
case HASHER_OVERWINTER_SEQUENCE:
blake2b_InitPersonal(&hasher->ctx.blake2b, 32, "ZcashSequencHash", 16);
break;
case HASHER_OVERWINTER_OUTPUTS:
blake2b_InitPersonal(&hasher->ctx.blake2b, 32, "ZcashOutputsHash", 16);
break;
case HASHER_OVERWINTER_PREIMAGE:
blake2b_InitPersonal(&hasher->ctx.blake2b, 32, "ZcashSigHash\x19\x1b\xa8\x5b", 16); // BRANCH_ID = 0x5ba81b19 / Overwinter
break;
case HASHER_SAPLING_PREIMAGE:
blake2b_InitPersonal(&hasher->ctx.blake2b, 32, "ZcashSigHash\xbb\x09\xb8\x76", 16); // BRANCH_ID = 0x76b809bb / Sapling
case HASHER_BLAKE2B_PERSONAL:
blake2b_InitPersonal(&hasher->ctx.blake2b, 32, hasher->param, 16);
break;
}
}
void hasher_Reset(Hasher *hasher) {
hasher_Init(hasher, hasher->type);
hasher_InitParam(hasher, hasher->type, hasher->param);
}
void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length) {
@ -89,11 +81,8 @@ void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length) {
case HASHER_GROESTLD_TRUNC:
groestl512_Update(&hasher->ctx.groestl, data, length);
break;
case HASHER_OVERWINTER_PREVOUTS:
case HASHER_OVERWINTER_SEQUENCE:
case HASHER_OVERWINTER_OUTPUTS:
case HASHER_OVERWINTER_PREIMAGE:
case HASHER_SAPLING_PREIMAGE:
case HASHER_BLAKE2B:
case HASHER_BLAKE2B_PERSONAL:
blake2b_Update(&hasher->ctx.blake2b, data, length);
break;
}
@ -134,11 +123,8 @@ void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) {
case HASHER_GROESTLD_TRUNC:
groestl512_DoubleTrunc(&hasher->ctx.groestl, hash);
break;
case HASHER_OVERWINTER_PREVOUTS:
case HASHER_OVERWINTER_SEQUENCE:
case HASHER_OVERWINTER_OUTPUTS:
case HASHER_OVERWINTER_PREIMAGE:
case HASHER_SAPLING_PREIMAGE:
case HASHER_BLAKE2B:
case HASHER_BLAKE2B_PERSONAL:
blake2b_Final(&hasher->ctx.blake2b, hash, 32);
break;
}

View File

@ -50,11 +50,8 @@ typedef enum {
HASHER_GROESTLD_TRUNC, /* Double Groestl512 hasher truncated to 256 bits */
HASHER_OVERWINTER_PREVOUTS,
HASHER_OVERWINTER_SEQUENCE,
HASHER_OVERWINTER_OUTPUTS,
HASHER_OVERWINTER_PREIMAGE,
HASHER_SAPLING_PREIMAGE,
HASHER_BLAKE2B,
HASHER_BLAKE2B_PERSONAL,
} HasherType;
typedef struct {
@ -65,11 +62,14 @@ typedef struct {
SHA3_CTX sha3; // for HASHER_SHA3{,K}
BLAKE256_CTX blake; // for HASHER_BLAKE{,D}
GROESTL512_CTX groestl; // for HASHER_GROESTLD_TRUNC
BLAKE2B_CTX blake2b; // for HASHER_OVERWINTER_*, HASHER_SAPLING_*
BLAKE2B_CTX blake2b; // for HASHER_BLAKE2B{,_PERSONAL}
} ctx;
const void *param;
} Hasher;
void hasher_Init(Hasher *hasher, HasherType type);
void hasher_InitParam(Hasher *hasher, HasherType type, const void *param);
inline void hasher_Init(Hasher *hasher, HasherType type) { hasher_InitParam(hasher, type, NULL); }
void hasher_Reset(Hasher *hasher);
void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length);
void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]);