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

add overwinter hashers

This commit is contained in:
Pavol Rusnak 2018-06-12 19:04:28 +02:00
parent afbe3e7551
commit dba2361728
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 37 additions and 11 deletions

View File

@ -37,6 +37,18 @@ void hasher_Init(Hasher *hasher, HasherType type) {
case HASHER_GROESTLD_TRUNC: case HASHER_GROESTLD_TRUNC:
groestl512_Init(&hasher->ctx.groestl); groestl512_Init(&hasher->ctx.groestl);
break; break;
case HASHER_OVERWINTER_PREVOUTS:
blake2b_InitKey(&hasher->ctx.blake2b, 32, "ZcashPrevoutHash", 16);
break;
case HASHER_OVERWINTER_SEQUENCE:
blake2b_InitKey(&hasher->ctx.blake2b, 32, "ZcashSequencHash", 16);
break;
case HASHER_OVERWINTER_OUTPUTS:
blake2b_InitKey(&hasher->ctx.blake2b, 32, "ZcashOutputsHash", 16);
break;
case HASHER_OVERWINTER_PREIMAGE:
blake2b_InitKey(&hasher->ctx.blake2b, 32, "ZcashSigHash\x19\x1b\xa8\x5b", 16); // BRANCH_ID = 0x5ba81b19
break;
} }
} }
@ -57,32 +69,39 @@ void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length) {
case HASHER_GROESTLD_TRUNC: case HASHER_GROESTLD_TRUNC:
groestl512_Update(&hasher->ctx.groestl, data, length); groestl512_Update(&hasher->ctx.groestl, data, length);
break; break;
case HASHER_OVERWINTER_PREVOUTS:
case HASHER_OVERWINTER_SEQUENCE:
case HASHER_OVERWINTER_OUTPUTS:
case HASHER_OVERWINTER_PREIMAGE:
blake2b_Update(&hasher->ctx.blake2b, data, length);
break;
} }
} }
void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) { void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) {
switch (hasher->type) { switch (hasher->type) {
case HASHER_SHA2: case HASHER_SHA2:
case HASHER_SHA2D:
sha256_Final(&hasher->ctx.sha2, hash); sha256_Final(&hasher->ctx.sha2, hash);
break; break;
case HASHER_SHA2D:
sha256_Final(&hasher->ctx.sha2, hash);
hasher_Raw(HASHER_SHA2, hash, HASHER_DIGEST_LENGTH, hash);
break;
case HASHER_BLAKE: case HASHER_BLAKE:
blake256_Final(&hasher->ctx.blake, hash);
break;
case HASHER_BLAKED: case HASHER_BLAKED:
blake256_Final(&hasher->ctx.blake, hash); blake256_Final(&hasher->ctx.blake, hash);
hasher_Raw(HASHER_BLAKE, hash, HASHER_DIGEST_LENGTH, hash);
break; break;
case HASHER_GROESTLD_TRUNC: case HASHER_GROESTLD_TRUNC:
groestl512_DoubleTrunc(&hasher->ctx.groestl, hash); groestl512_DoubleTrunc(&hasher->ctx.groestl, hash);
return;
}
switch (hasher->type) {
case HASHER_SHA2D:
hasher_Raw(HASHER_SHA2, hash, HASHER_DIGEST_LENGTH, hash);
break; break;
case HASHER_BLAKED: case HASHER_OVERWINTER_PREVOUTS:
hasher_Raw(HASHER_BLAKE, hash, HASHER_DIGEST_LENGTH, hash); case HASHER_OVERWINTER_SEQUENCE:
break; case HASHER_OVERWINTER_OUTPUTS:
default: case HASHER_OVERWINTER_PREIMAGE:
blake2b_Final(&hasher->ctx.blake2b, hash, 32);
break; break;
} }
} }

View File

@ -29,6 +29,7 @@
#include "sha2.h" #include "sha2.h"
#include "blake256.h" #include "blake256.h"
#include "groestl.h" #include "groestl.h"
#include "blake2b.h"
#define HASHER_DIGEST_LENGTH 32 #define HASHER_DIGEST_LENGTH 32
@ -40,6 +41,11 @@ typedef enum {
HASHER_BLAKED, HASHER_BLAKED,
HASHER_GROESTLD_TRUNC, /* Double Groestl512 hasher truncated to 256 bits */ HASHER_GROESTLD_TRUNC, /* Double Groestl512 hasher truncated to 256 bits */
HASHER_OVERWINTER_PREVOUTS,
HASHER_OVERWINTER_SEQUENCE,
HASHER_OVERWINTER_OUTPUTS,
HASHER_OVERWINTER_PREIMAGE,
} HasherType; } HasherType;
typedef struct { typedef struct {
@ -49,6 +55,7 @@ typedef struct {
SHA256_CTX sha2; SHA256_CTX sha2;
BLAKE256_CTX blake; BLAKE256_CTX blake;
GROESTL512_CTX groestl; GROESTL512_CTX groestl;
BLAKE2B_CTX blake2b;
} ctx; } ctx;
} Hasher; } Hasher;