/src/nss-nspr/nss/lib/freebl/sha3.c
Line | Count | Source (jump to first uncovered line) |
1 | | |
2 | | #ifdef FREEBL_NO_DEPEND |
3 | | #include "stubs.h" |
4 | | #endif |
5 | | |
6 | | #include "prtypes.h" /* for PRUintXX */ |
7 | | #include "secport.h" /* for PORT_XXX */ |
8 | | #include "blapi.h" |
9 | | #include "blapii.h" |
10 | | #include "blapit.h" |
11 | | #include "secerr.h" |
12 | | #include "Hacl_Hash_SHA3.h" |
13 | | |
14 | | struct SHA3ContextStr { |
15 | | Hacl_Streaming_Keccak_state *st; |
16 | | }; |
17 | | |
18 | | SHA3_224Context * |
19 | | SHA3_224_NewContext() |
20 | 0 | { |
21 | 0 | SHA3_224Context *ctx = PORT_New(SHA3_224Context); |
22 | 0 | ctx->st = Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_SHA3_224); |
23 | 0 | return ctx; |
24 | 0 | } |
25 | | |
26 | | SHA3_256Context * |
27 | | SHA3_256_NewContext() |
28 | 0 | { |
29 | 0 | SHA3_256Context *ctx = PORT_New(SHA3_256Context); |
30 | 0 | ctx->st = Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_SHA3_256); |
31 | 0 | return ctx; |
32 | 0 | } |
33 | | |
34 | | SHA3_384Context * |
35 | | SHA3_384_NewContext() |
36 | 0 | { |
37 | 0 | SHA3_384Context *ctx = PORT_New(SHA3_384Context); |
38 | 0 | ctx->st = Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_SHA3_384); |
39 | 0 | return ctx; |
40 | 0 | } |
41 | | |
42 | | SHA3_512Context * |
43 | | SHA3_512_NewContext() |
44 | 0 | { |
45 | 0 | SHA3_512Context *ctx = PORT_New(SHA3_512Context); |
46 | 0 | ctx->st = Hacl_Streaming_Keccak_malloc(Spec_Hash_Definitions_SHA3_512); |
47 | 0 | return ctx; |
48 | 0 | } |
49 | | |
50 | | void |
51 | | SHA3_224_DestroyContext(SHA3_224Context *ctx, PRBool freeit) |
52 | 0 | { |
53 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
54 | 0 | if (freeit) { |
55 | 0 | Hacl_Streaming_Keccak_free(ctx->st); |
56 | 0 | PORT_Free(ctx); |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | void |
61 | | SHA3_256_DestroyContext(SHA3_256Context *ctx, PRBool freeit) |
62 | 0 | { |
63 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
64 | 0 | if (freeit) { |
65 | 0 | Hacl_Streaming_Keccak_free(ctx->st); |
66 | 0 | PORT_Free(ctx); |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | void |
71 | | SHA3_384_DestroyContext(SHA3_384Context *ctx, PRBool freeit) |
72 | 0 | { |
73 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
74 | 0 | if (freeit) { |
75 | 0 | Hacl_Streaming_Keccak_free(ctx->st); |
76 | 0 | PORT_Free(ctx); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | void |
81 | | SHA3_512_DestroyContext(SHA3_512Context *ctx, PRBool freeit) |
82 | 0 | { |
83 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
84 | 0 | if (freeit) { |
85 | 0 | Hacl_Streaming_Keccak_free(ctx->st); |
86 | 0 | PORT_Free(ctx); |
87 | 0 | } |
88 | 0 | } |
89 | | |
90 | | unsigned int |
91 | | SHA3_224_FlattenSize(SHA3_224Context *ctx) |
92 | 0 | { |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | | unsigned int |
97 | | SHA3_256_FlattenSize(SHA3_256Context *ctx) |
98 | 0 | { |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | | unsigned int |
103 | | SHA3_384_FlattenSize(SHA3_384Context *ctx) |
104 | 0 | { |
105 | 0 | return 0; |
106 | 0 | } |
107 | | |
108 | | unsigned int |
109 | | SHA3_512_FlattenSize(SHA3_512Context *ctx) |
110 | 0 | { |
111 | 0 | return 0; |
112 | 0 | } |
113 | | |
114 | | void |
115 | | SHA3_224_Begin(SHA3_224Context *ctx) |
116 | 0 | { |
117 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
118 | 0 | } |
119 | | |
120 | | void |
121 | | SHA3_256_Begin(SHA3_256Context *ctx) |
122 | 0 | { |
123 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
124 | 0 | } |
125 | | |
126 | | void |
127 | | SHA3_384_Begin(SHA3_384Context *ctx) |
128 | 0 | { |
129 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
130 | 0 | } |
131 | | |
132 | | void |
133 | | SHA3_512_Begin(SHA3_512Context *ctx) |
134 | 0 | { |
135 | 0 | Hacl_Streaming_Keccak_reset(ctx->st); |
136 | 0 | } |
137 | | |
138 | | void |
139 | | SHA3_224_Update(SHA3_224Context *ctx, const unsigned char *input, |
140 | | unsigned int inputLen) |
141 | 0 | { |
142 | 0 | Hacl_Streaming_Keccak_update(ctx->st, (uint8_t *)input, inputLen); |
143 | 0 | } |
144 | | |
145 | | void |
146 | | SHA3_256_Update(SHA3_256Context *ctx, const unsigned char *input, |
147 | | unsigned int inputLen) |
148 | 0 | { |
149 | 0 | Hacl_Streaming_Keccak_update(ctx->st, (uint8_t *)input, inputLen); |
150 | 0 | } |
151 | | |
152 | | void |
153 | | SHA3_384_Update(SHA3_384Context *ctx, const unsigned char *input, |
154 | | unsigned int inputLen) |
155 | 0 | { |
156 | 0 | Hacl_Streaming_Keccak_update(ctx->st, (uint8_t *)input, inputLen); |
157 | 0 | } |
158 | | |
159 | | void |
160 | | SHA3_512_Update(SHA3_512Context *ctx, const unsigned char *input, |
161 | | unsigned int inputLen) |
162 | 0 | { |
163 | 0 | Hacl_Streaming_Keccak_update(ctx->st, (uint8_t *)input, inputLen); |
164 | 0 | } |
165 | | |
166 | | void |
167 | | SHA3_224_End(SHA3_224Context *ctx, unsigned char *digest, |
168 | | unsigned int *digestLen, unsigned int maxDigestLen) |
169 | 0 | { |
170 | 0 | uint8_t sha3_digest[SHA3_224_LENGTH] = { 0 }; |
171 | 0 | Hacl_Streaming_Keccak_finish(ctx->st, sha3_digest); |
172 | |
|
173 | 0 | unsigned int len = PR_MIN(SHA3_224_LENGTH, maxDigestLen); |
174 | 0 | memcpy(digest, sha3_digest, len); |
175 | 0 | if (digestLen) |
176 | 0 | *digestLen = len; |
177 | 0 | } |
178 | | |
179 | | void |
180 | | SHA3_256_End(SHA3_256Context *ctx, unsigned char *digest, |
181 | | unsigned int *digestLen, unsigned int maxDigestLen) |
182 | 0 | { |
183 | 0 | uint8_t sha3_digest[SHA3_256_LENGTH] = { 0 }; |
184 | 0 | Hacl_Streaming_Keccak_finish(ctx->st, sha3_digest); |
185 | |
|
186 | 0 | unsigned int len = PR_MIN(SHA3_256_LENGTH, maxDigestLen); |
187 | 0 | memcpy(digest, sha3_digest, len); |
188 | 0 | if (digestLen) |
189 | 0 | *digestLen = len; |
190 | 0 | } |
191 | | |
192 | | void |
193 | | SHA3_384_End(SHA3_384Context *ctx, unsigned char *digest, |
194 | | unsigned int *digestLen, unsigned int maxDigestLen) |
195 | 0 | { |
196 | 0 | uint8_t sha3_digest[SHA3_384_LENGTH] = { 0 }; |
197 | 0 | Hacl_Streaming_Keccak_finish(ctx->st, sha3_digest); |
198 | |
|
199 | 0 | unsigned int len = PR_MIN(SHA3_384_LENGTH, maxDigestLen); |
200 | 0 | memcpy(digest, sha3_digest, len); |
201 | 0 | if (digestLen) |
202 | 0 | *digestLen = len; |
203 | 0 | } |
204 | | |
205 | | void |
206 | | SHA3_512_End(SHA3_512Context *ctx, unsigned char *digest, |
207 | | unsigned int *digestLen, unsigned int maxDigestLen) |
208 | 0 | { |
209 | 0 | uint8_t sha3_digest[SHA3_512_LENGTH] = { 0 }; |
210 | 0 | Hacl_Streaming_Keccak_finish(ctx->st, sha3_digest); |
211 | |
|
212 | 0 | unsigned int len = PR_MIN(SHA3_512_LENGTH, maxDigestLen); |
213 | 0 | memcpy(digest, sha3_digest, len); |
214 | 0 | if (digestLen) |
215 | 0 | *digestLen = len; |
216 | 0 | } |
217 | | |
218 | | SECStatus |
219 | | SHA3_224_HashBuf(unsigned char *dest, const unsigned char *src, |
220 | | PRUint32 src_length) |
221 | 0 | { |
222 | 0 | SHA3_224Context *ctx = SHA3_224_NewContext(); |
223 | 0 | SHA3_224_Begin(ctx); |
224 | 0 | SHA3_224_Update(ctx, src, src_length); |
225 | 0 | SHA3_224_End(ctx, dest, NULL, SHA3_224_LENGTH); |
226 | 0 | SHA3_224_DestroyContext(ctx, true); |
227 | 0 | return SECSuccess; |
228 | 0 | } |
229 | | |
230 | | SECStatus |
231 | | SHA3_256_HashBuf(unsigned char *dest, const unsigned char *src, |
232 | | PRUint32 src_length) |
233 | 0 | { |
234 | 0 | SHA3_256Context *ctx = SHA3_256_NewContext(); |
235 | 0 | SHA3_256_Begin(ctx); |
236 | 0 | SHA3_256_Update(ctx, src, src_length); |
237 | 0 | SHA3_256_End(ctx, dest, NULL, SHA3_256_LENGTH); |
238 | 0 | SHA3_256_DestroyContext(ctx, true); |
239 | 0 | return SECSuccess; |
240 | 0 | } |
241 | | |
242 | | SECStatus |
243 | | SHA3_384_HashBuf(unsigned char *dest, const unsigned char *src, |
244 | | PRUint32 src_length) |
245 | 0 | { |
246 | 0 | SHA3_384Context *ctx = SHA3_384_NewContext(); |
247 | 0 | SHA3_384_Begin(ctx); |
248 | 0 | SHA3_384_Update(ctx, src, src_length); |
249 | 0 | SHA3_384_End(ctx, dest, NULL, SHA3_384_LENGTH); |
250 | 0 | SHA3_384_DestroyContext(ctx, true); |
251 | 0 | return SECSuccess; |
252 | 0 | } |
253 | | |
254 | | SECStatus |
255 | | SHA3_512_HashBuf(unsigned char *dest, const unsigned char *src, |
256 | | PRUint32 src_length) |
257 | 0 | { |
258 | 0 | SHA3_512Context *ctx = SHA3_512_NewContext(); |
259 | 0 | SHA3_512_Begin(ctx); |
260 | 0 | SHA3_512_Update(ctx, src, src_length); |
261 | 0 | SHA3_512_End(ctx, dest, NULL, SHA3_512_LENGTH); |
262 | 0 | SHA3_512_DestroyContext(ctx, true); |
263 | 0 | return SECSuccess; |
264 | 0 | } |
265 | | |
266 | | SECStatus |
267 | | SHA3_224_Hash(unsigned char *dest, const char *src) |
268 | 0 | { |
269 | 0 | return SHA3_224_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); |
270 | 0 | } |
271 | | |
272 | | SECStatus |
273 | | SHA3_256_Hash(unsigned char *dest, const char *src) |
274 | 0 | { |
275 | 0 | return SHA3_256_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); |
276 | 0 | } |
277 | | |
278 | | SECStatus |
279 | | SHA3_384_Hash(unsigned char *dest, const char *src) |
280 | 0 | { |
281 | 0 | return SHA3_384_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); |
282 | 0 | } |
283 | | |
284 | | SECStatus |
285 | | SHA3_512_Hash(unsigned char *dest, const char *src) |
286 | 0 | { |
287 | 0 | return SHA3_512_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); |
288 | 0 | } |