/src/trezor-firmware/crypto/hasher.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * Copyright (c) 2017 Saleem Rashid |
3 | | * |
4 | | * Permission is hereby granted, free of charge, to any person obtaining |
5 | | * a copy of this software and associated documentation files (the "Software"), |
6 | | * to deal in the Software without restriction, including without limitation |
7 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
8 | | * and/or sell copies of the Software, and to permit persons to whom the |
9 | | * Software is furnished to do so, subject to the following conditions: |
10 | | * |
11 | | * The above copyright notice and this permission notice shall be included |
12 | | * in all copies or substantial portions of the Software. |
13 | | * |
14 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
15 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
17 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES |
18 | | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
19 | | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
20 | | * OTHER DEALINGS IN THE SOFTWARE. |
21 | | */ |
22 | | |
23 | | #include "hasher.h" |
24 | | #include "ripemd160.h" |
25 | | |
26 | | const uint32_t sha256_initial_tapsighash_state[8] = { |
27 | | 0xf504a425UL, 0xd7f8783bUL, 0x1363868aUL, 0xe3e55658UL, |
28 | | 0x6eee945dUL, 0xbc7888ddUL, 0x02a6e2c3UL, 0x1873fe9fUL, |
29 | | }; |
30 | | |
31 | | void hasher_InitParam(Hasher *hasher, HasherType type, const void *param, |
32 | 2.20k | uint32_t param_size) { |
33 | 2.20k | hasher->type = type; |
34 | 2.20k | hasher->param = param; |
35 | 2.20k | hasher->param_size = param_size; |
36 | | |
37 | 2.20k | switch (hasher->type) { |
38 | 1.96k | case HASHER_SHA2: |
39 | 1.96k | case HASHER_SHA2D: |
40 | 1.96k | case HASHER_SHA2_RIPEMD: |
41 | 1.96k | sha256_Init(&hasher->ctx.sha2); |
42 | 1.96k | break; |
43 | 0 | case HASHER_SHA2_TAPSIGHASH: |
44 | 0 | sha256_Init_ex(&hasher->ctx.sha2, sha256_initial_tapsighash_state, 512); |
45 | 0 | break; |
46 | 242 | case HASHER_SHA3: |
47 | 242 | #if USE_KECCAK |
48 | 242 | case HASHER_SHA3K: |
49 | 242 | #endif |
50 | 242 | sha3_256_Init(&hasher->ctx.sha3); |
51 | 242 | break; |
52 | 0 | case HASHER_BLAKE: |
53 | 0 | case HASHER_BLAKED: |
54 | 0 | case HASHER_BLAKE_RIPEMD: |
55 | 0 | blake256_Init(&hasher->ctx.blake); |
56 | 0 | break; |
57 | 0 | case HASHER_GROESTLD_TRUNC: |
58 | 0 | groestl512_Init(&hasher->ctx.groestl); |
59 | 0 | break; |
60 | 0 | case HASHER_BLAKE2B: |
61 | 0 | trezor_blake2b_Init(&hasher->ctx.blake2b, 32); |
62 | 0 | break; |
63 | 0 | case HASHER_BLAKE2B_PERSONAL: |
64 | 0 | trezor_blake2b_InitPersonal(&hasher->ctx.blake2b, 32, hasher->param, |
65 | 0 | hasher->param_size); |
66 | 0 | break; |
67 | 2.20k | } |
68 | 2.20k | } |
69 | | |
70 | 2.20k | void hasher_Init(Hasher *hasher, HasherType type) { |
71 | 2.20k | hasher_InitParam(hasher, type, NULL, 0); |
72 | 2.20k | } |
73 | | |
74 | 0 | void hasher_Reset(Hasher *hasher) { |
75 | 0 | hasher_InitParam(hasher, hasher->type, hasher->param, hasher->param_size); |
76 | 0 | } |
77 | | |
78 | 18.0k | void hasher_Update(Hasher *hasher, const uint8_t *data, size_t length) { |
79 | 18.0k | switch (hasher->type) { |
80 | 11.4k | case HASHER_SHA2: |
81 | 11.4k | case HASHER_SHA2D: |
82 | 11.4k | case HASHER_SHA2_RIPEMD: |
83 | 11.4k | case HASHER_SHA2_TAPSIGHASH: |
84 | 11.4k | sha256_Update(&hasher->ctx.sha2, data, length); |
85 | 11.4k | break; |
86 | 6.65k | case HASHER_SHA3: |
87 | 6.65k | #if USE_KECCAK |
88 | 6.65k | case HASHER_SHA3K: |
89 | 6.65k | #endif |
90 | 6.65k | sha3_Update(&hasher->ctx.sha3, data, length); |
91 | 6.65k | break; |
92 | 0 | case HASHER_BLAKE: |
93 | 0 | case HASHER_BLAKED: |
94 | 0 | case HASHER_BLAKE_RIPEMD: |
95 | 0 | blake256_Update(&hasher->ctx.blake, data, length); |
96 | 0 | break; |
97 | 0 | case HASHER_GROESTLD_TRUNC: |
98 | 0 | groestl512_Update(&hasher->ctx.groestl, data, length); |
99 | 0 | break; |
100 | 0 | case HASHER_BLAKE2B: |
101 | 0 | case HASHER_BLAKE2B_PERSONAL: |
102 | 0 | trezor_blake2b_Update(&hasher->ctx.blake2b, data, length); |
103 | 0 | break; |
104 | 18.0k | } |
105 | 18.0k | } |
106 | | |
107 | 2.20k | void hasher_Final(Hasher *hasher, uint8_t hash[HASHER_DIGEST_LENGTH]) { |
108 | 2.20k | switch (hasher->type) { |
109 | 1.96k | case HASHER_SHA2: |
110 | 1.96k | case HASHER_SHA2_TAPSIGHASH: |
111 | 1.96k | sha256_Final(&hasher->ctx.sha2, hash); |
112 | 1.96k | break; |
113 | 0 | case HASHER_SHA2D: |
114 | 0 | sha256_Final(&hasher->ctx.sha2, hash); |
115 | 0 | hasher_Raw(HASHER_SHA2, hash, HASHER_DIGEST_LENGTH, hash); |
116 | 0 | break; |
117 | 0 | case HASHER_SHA2_RIPEMD: |
118 | 0 | sha256_Final(&hasher->ctx.sha2, hash); |
119 | 0 | ripemd160(hash, HASHER_DIGEST_LENGTH, hash); |
120 | 0 | break; |
121 | 242 | case HASHER_SHA3: |
122 | 242 | sha3_Final(&hasher->ctx.sha3, hash); |
123 | 242 | break; |
124 | 0 | #if USE_KECCAK |
125 | 0 | case HASHER_SHA3K: |
126 | 0 | keccak_Final(&hasher->ctx.sha3, hash); |
127 | 0 | break; |
128 | 0 | #endif |
129 | 0 | case HASHER_BLAKE: |
130 | 0 | blake256_Final(&hasher->ctx.blake, hash); |
131 | 0 | break; |
132 | 0 | case HASHER_BLAKED: |
133 | 0 | blake256_Final(&hasher->ctx.blake, hash); |
134 | 0 | hasher_Raw(HASHER_BLAKE, hash, HASHER_DIGEST_LENGTH, hash); |
135 | 0 | break; |
136 | 0 | case HASHER_BLAKE_RIPEMD: |
137 | 0 | blake256_Final(&hasher->ctx.blake, hash); |
138 | 0 | ripemd160(hash, HASHER_DIGEST_LENGTH, hash); |
139 | 0 | break; |
140 | 0 | case HASHER_GROESTLD_TRUNC: |
141 | 0 | groestl512_DoubleTrunc(&hasher->ctx.groestl, hash); |
142 | 0 | break; |
143 | 0 | case HASHER_BLAKE2B: |
144 | 0 | case HASHER_BLAKE2B_PERSONAL: |
145 | 0 | trezor_blake2b_Final(&hasher->ctx.blake2b, hash, 32); |
146 | 0 | break; |
147 | 2.20k | } |
148 | 2.20k | } |
149 | | |
150 | | void hasher_Raw(HasherType type, const uint8_t *data, size_t length, |
151 | 1.93k | uint8_t hash[HASHER_DIGEST_LENGTH]) { |
152 | 1.93k | Hasher hasher = {0}; |
153 | | |
154 | 1.93k | hasher_Init(&hasher, type); |
155 | 1.93k | hasher_Update(&hasher, data, length); |
156 | 1.93k | hasher_Final(&hasher, hash); |
157 | 1.93k | } |