/src/relic/src/md/sha384-512.c
Line | Count | Source |
1 | | /*************************** sha384-512.c ***************************/ |
2 | | /********************* See RFC 4634 for details *********************/ |
3 | | /* |
4 | | * Description: |
5 | | * This file implements the Secure Hash Signature Standard |
6 | | * algorithms as defined in the National Institute of Standards |
7 | | * and Technology Federal Information Processing Standards |
8 | | * Publication (FIPS PUB) 180-1 published on April 17, 1995, 180-2 |
9 | | * published on August 1, 2002, and the FIPS PUB 180-2 Change |
10 | | * Notice published on February 28, 2004. |
11 | | * |
12 | | * A combined document showing all algorithms is available at |
13 | | * http://csrc.nist.gov/publications/fips/ |
14 | | * fips180-2/fips180-2withchangenotice.pdf |
15 | | * |
16 | | * The SHA-384 and SHA-512 algorithms produce 384-bit and 512-bit |
17 | | * message digests for a given data stream. It should take about |
18 | | * 2**n steps to find a message with the same digest as a given |
19 | | * message and 2**(n/2) to find any two messages with the same |
20 | | * digest, when n is the digest size in bits. Therefore, this |
21 | | * algorithm can serve as a means of providing a |
22 | | * "fingerprint" for a message. |
23 | | * |
24 | | * Portability Issues: |
25 | | * SHA-384 and SHA-512 are defined in terms of 64-bit "words", |
26 | | * but if USE_32BIT_ONLY is #defined, this code is implemented in |
27 | | * terms of 32-bit "words". This code uses <stdint.h> (included |
28 | | * via "sha.h") to define the 64, 32 and 8 bit unsigned integer |
29 | | * types. If your C compiler does not support 64 bit unsigned |
30 | | * integers, and you do not #define USE_32BIT_ONLY, this code is |
31 | | * not appropriate. |
32 | | * |
33 | | * Caveats: |
34 | | * SHA-384 and SHA-512 are designed to work with messages less |
35 | | * than 2^128 bits long. This implementation uses |
36 | | * SHA384/512Input() to hash the bits that are a multiple of the |
37 | | * size of an 8-bit character, and then uses SHA384/256FinalBits() |
38 | | * to hash the final few bits of the input. |
39 | | * |
40 | | */ |
41 | | |
42 | | #include "sha.h" |
43 | | |
44 | | #ifdef USE_32BIT_ONLY |
45 | | /* |
46 | | * Define 64-bit arithmetic in terms of 32-bit arithmetic. |
47 | | * Each 64-bit number is represented in a 2-word array. |
48 | | * All macros are defined such that the result is the last parameter. |
49 | | */ |
50 | | |
51 | | /* |
52 | | * Define shift, rotate left and rotate right functions |
53 | | */ |
54 | 126M | #define SHA512_SHR(bits, word, ret) ( \ |
55 | 126M | /* (((uint64_t)((word))) >> (bits)) */ \ |
56 | 126M | (ret)[0] = (((bits) < 32) && ((bits) >= 0)) ? \ |
57 | 126M | ((word)[0] >> (bits)) : 0, \ |
58 | 126M | (ret)[1] = ((bits) > 32) ? ((word)[0] >> ((bits) - 32)) : \ |
59 | 126M | ((bits) == 32) ? (word)[0] : \ |
60 | 81.8M | ((bits) >= 0) ? \ |
61 | 81.8M | (((word)[0] << (32 - (bits))) | \ |
62 | 81.8M | ((word)[1] >> (bits))) : 0 ) |
63 | | |
64 | 107M | #define SHA512_SHL(bits, word, ret) ( \ |
65 | 107M | /* (((uint64_t)(word)) << (bits)) */ \ |
66 | 107M | (ret)[0] = ((bits) > 32) ? ((word)[1] << ((bits) - 32)) : \ |
67 | 107M | ((bits) == 32) ? (word)[1] : \ |
68 | 44.4M | ((bits) >= 0) ? \ |
69 | 44.4M | (((word)[0] << (bits)) | \ |
70 | 44.4M | ((word)[1] >> (32 - (bits)))) : \ |
71 | 44.4M | 0, \ |
72 | 107M | (ret)[1] = (((bits) < 32) && ((bits) >= 0)) ? \ |
73 | 107M | ((word)[1] << (bits)) : 0 ) |
74 | | |
75 | | /* |
76 | | * Define 64-bit OR |
77 | | */ |
78 | 107M | #define SHA512_OR(word1, word2, ret) ( \ |
79 | 107M | (ret)[0] = (word1)[0] | (word2)[0], \ |
80 | 107M | (ret)[1] = (word1)[1] | (word2)[1] ) |
81 | | |
82 | | /* |
83 | | * Define 64-bit XOR |
84 | | */ |
85 | 119M | #define SHA512_XOR(word1, word2, ret) ( \ |
86 | 119M | (ret)[0] = (word1)[0] ^ (word2)[0], \ |
87 | 119M | (ret)[1] = (word1)[1] ^ (word2)[1] ) |
88 | | |
89 | | /* |
90 | | * Define 64-bit AND |
91 | | */ |
92 | 58.4M | #define SHA512_AND(word1, word2, ret) ( \ |
93 | 58.4M | (ret)[0] = (word1)[0] & (word2)[0], \ |
94 | 58.4M | (ret)[1] = (word1)[1] & (word2)[1] ) |
95 | | |
96 | | /* |
97 | | * Define 64-bit TILDA |
98 | | */ |
99 | | #define SHA512_TILDA(word, ret) \ |
100 | 11.6M | ( (ret)[0] = ~(word)[0], (ret)[1] = ~(word)[1] ) |
101 | | |
102 | | /* |
103 | | * Define 64-bit ADD |
104 | | */ |
105 | 109M | #define SHA512_ADD(word1, word2, ret) ( \ |
106 | 109M | (ret)[1] = (word1)[1], (ret)[1] += (word2)[1], \ |
107 | 109M | (ret)[0] = (word1)[0] + (word2)[0] + ((ret)[1] < (word1)[1]) ) |
108 | | |
109 | | /* |
110 | | * Add the 4word value in word2 to word1. |
111 | | */ |
112 | 18.6M | #define SHA512_ADDTO4(word1, word2) ( \ |
113 | 18.6M | ADDTO4_temp = (word1)[3], \ |
114 | 18.6M | (word1)[3] += (word2)[3], \ |
115 | 18.6M | ADDTO4_temp2 = (word1)[2], \ |
116 | 18.6M | (word1)[2] += (word2)[2] + ((word1)[3] < ADDTO4_temp), \ |
117 | 18.6M | ADDTO4_temp = (word1)[1], \ |
118 | 18.6M | (word1)[1] += (word2)[1] + ((word1)[2] < ADDTO4_temp2), \ |
119 | 18.6M | (word1)[0] += (word2)[0] + ((word1)[1] < ADDTO4_temp) ) |
120 | | |
121 | | /* |
122 | | * Add the 2word value in word2 to word1. |
123 | | */ |
124 | 1.16M | #define SHA512_ADDTO2(word1, word2) ( \ |
125 | 1.16M | ADDTO2_temp = (word1)[1], \ |
126 | 1.16M | (word1)[1] += (word2)[1], \ |
127 | 1.16M | (word1)[0] += (word2)[0] + ((word1)[1] < ADDTO2_temp) ) |
128 | | |
129 | | /* |
130 | | * SHA rotate ((word >> bits) | (word << (64-bits))) |
131 | | */ |
132 | 107M | #define SHA512_ROTR(bits, word, ret) ( \ |
133 | 107M | SHA512_SHR((bits), (word), ROTR_temp1), \ |
134 | 107M | SHA512_SHL(64-(bits), (word), ROTR_temp2), \ |
135 | 107M | SHA512_OR(ROTR_temp1, ROTR_temp2, (ret)) ) |
136 | | |
137 | | /* |
138 | | * Define the SHA SIGMA and sigma macros |
139 | | * SHA512_ROTR(28,word) ^ SHA512_ROTR(34,word) ^ SHA512_ROTR(39,word) |
140 | | */ |
141 | 11.6M | #define SHA512_SIGMA0(word, ret) ( \ |
142 | 11.6M | SHA512_ROTR(28, (word), SIGMA0_temp1), \ |
143 | 11.6M | SHA512_ROTR(34, (word), SIGMA0_temp2), \ |
144 | 11.6M | SHA512_ROTR(39, (word), SIGMA0_temp3), \ |
145 | 11.6M | SHA512_XOR(SIGMA0_temp2, SIGMA0_temp3, SIGMA0_temp4), \ |
146 | 11.6M | SHA512_XOR(SIGMA0_temp1, SIGMA0_temp4, (ret)) ) |
147 | | |
148 | | /* |
149 | | * SHA512_ROTR(14,word) ^ SHA512_ROTR(18,word) ^ SHA512_ROTR(41,word) |
150 | | */ |
151 | 11.6M | #define SHA512_SIGMA1(word, ret) ( \ |
152 | 11.6M | SHA512_ROTR(14, (word), SIGMA1_temp1), \ |
153 | 11.6M | SHA512_ROTR(18, (word), SIGMA1_temp2), \ |
154 | 11.6M | SHA512_ROTR(41, (word), SIGMA1_temp3), \ |
155 | 11.6M | SHA512_XOR(SIGMA1_temp2, SIGMA1_temp3, SIGMA1_temp4), \ |
156 | 11.6M | SHA512_XOR(SIGMA1_temp1, SIGMA1_temp4, (ret)) ) |
157 | | |
158 | | /* |
159 | | * (SHA512_ROTR( 1,word) ^ SHA512_ROTR( 8,word) ^ SHA512_SHR( 7,word)) |
160 | | */ |
161 | 9.35M | #define SHA512_sigma0(word, ret) ( \ |
162 | 9.35M | SHA512_ROTR( 1, (word), sigma0_temp1), \ |
163 | 9.35M | SHA512_ROTR( 8, (word), sigma0_temp2), \ |
164 | 9.35M | SHA512_SHR( 7, (word), sigma0_temp3), \ |
165 | 9.35M | SHA512_XOR(sigma0_temp2, sigma0_temp3, sigma0_temp4), \ |
166 | 9.35M | SHA512_XOR(sigma0_temp1, sigma0_temp4, (ret)) ) |
167 | | |
168 | | /* |
169 | | * (SHA512_ROTR(19,word) ^ SHA512_ROTR(61,word) ^ SHA512_SHR( 6,word)) |
170 | | */ |
171 | 9.35M | #define SHA512_sigma1(word, ret) ( \ |
172 | 9.35M | SHA512_ROTR(19, (word), sigma1_temp1), \ |
173 | 9.35M | SHA512_ROTR(61, (word), sigma1_temp2), \ |
174 | 9.35M | SHA512_SHR( 6, (word), sigma1_temp3), \ |
175 | 9.35M | SHA512_XOR(sigma1_temp2, sigma1_temp3, sigma1_temp4), \ |
176 | 9.35M | SHA512_XOR(sigma1_temp1, sigma1_temp4, (ret)) ) |
177 | | |
178 | | #undef SHA_Ch |
179 | | #undef SHA_Maj |
180 | | |
181 | | #ifndef USE_MODIFIED_MACROS |
182 | | /* |
183 | | * These definitions are the ones used in FIPS-180-2, section 4.1.3 |
184 | | * Ch(x,y,z) ((x & y) ^ (~x & z)) |
185 | | */ |
186 | 11.6M | #define SHA_Ch(x, y, z, ret) ( \ |
187 | 11.6M | SHA512_AND(x, y, Ch_temp1), \ |
188 | 11.6M | SHA512_TILDA(x, Ch_temp2), \ |
189 | 11.6M | SHA512_AND(Ch_temp2, z, Ch_temp3), \ |
190 | 11.6M | SHA512_XOR(Ch_temp1, Ch_temp3, (ret)) ) |
191 | | /* |
192 | | * Maj(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z))) |
193 | | */ |
194 | 11.6M | #define SHA_Maj(x, y, z, ret) ( \ |
195 | 11.6M | SHA512_AND(x, y, Maj_temp1), \ |
196 | 11.6M | SHA512_AND(x, z, Maj_temp2), \ |
197 | 11.6M | SHA512_AND(y, z, Maj_temp3), \ |
198 | 11.6M | SHA512_XOR(Maj_temp2, Maj_temp3, Maj_temp4), \ |
199 | 11.6M | SHA512_XOR(Maj_temp1, Maj_temp4, (ret)) ) |
200 | | #else /* !USE_32BIT_ONLY */ |
201 | | /* |
202 | | * These definitions are potentially faster equivalents for the ones |
203 | | * used in FIPS-180-2, section 4.1.3. |
204 | | * ((x & y) ^ (~x & z)) becomes |
205 | | * ((x & (y ^ z)) ^ z) |
206 | | */ |
207 | | #define SHA_Ch(x, y, z, ret) ( \ |
208 | | (ret)[0] = (((x)[0] & ((y)[0] ^ (z)[0])) ^ (z)[0]), \ |
209 | | (ret)[1] = (((x)[1] & ((y)[1] ^ (z)[1])) ^ (z)[1]) ) |
210 | | |
211 | | /* |
212 | | * ((x & y) ^ (x & z) ^ (y & z)) becomes |
213 | | * ((x & (y | z)) | (y & z)) |
214 | | */ |
215 | | #define SHA_Maj(x, y, z, ret) ( \ |
216 | | ret[0] = (((x)[0] & ((y)[0] | (z)[0])) | ((y)[0] & (z)[0])), \ |
217 | | ret[1] = (((x)[1] & ((y)[1] | (z)[1])) | ((y)[1] & (z)[1])) ) |
218 | | #endif /* USE_MODIFIED_MACROS */ |
219 | | |
220 | | /* |
221 | | * add "length" to the length |
222 | | */ |
223 | 37.2M | #define SHA384_512AddLength(context, length) ( \ |
224 | 37.2M | addTemp[3] = (length), SHA512_ADDTO4((context)->Length, addTemp), \ |
225 | 37.2M | (context)->Corrupted = (((context)->Length[3] == 0) && \ |
226 | 18.6M | ((context)->Length[2] == 0) && ((context)->Length[1] == 0) && \ |
227 | 18.6M | ((context)->Length[0] < 8)) ? 1 : 0 ) |
228 | | |
229 | | /* Local Function Prototypes */ |
230 | | static void SHA384_512Finalize(SHA512Context *context, |
231 | | uint8_t Pad_Byte); |
232 | | static void SHA384_512PadMessage(SHA512Context *context, |
233 | | uint8_t Pad_Byte); |
234 | | static void SHA384_512ProcessMessageBlock(SHA512Context *context); |
235 | | static int SHA384_512Reset(SHA512Context *context, uint32_t H0[]); |
236 | | static int SHA384_512ResultN( SHA512Context *context, |
237 | | uint8_t Message_Digest[], int HashSize); |
238 | | |
239 | | /* Initial Hash Values: FIPS-180-2 sections 5.3.3 and 5.3.4 */ |
240 | | static uint32_t SHA384_H0[SHA512HashSize/4] = { |
241 | | 0xCBBB9D5D, 0xC1059ED8, 0x629A292A, 0x367CD507, 0x9159015A, |
242 | | 0x3070DD17, 0x152FECD8, 0xF70E5939, 0x67332667, 0xFFC00B31, |
243 | | 0x8EB44A87, 0x68581511, 0xDB0C2E0D, 0x64F98FA7, 0x47B5481D, |
244 | | 0xBEFA4FA4 |
245 | | }; |
246 | | |
247 | | static uint32_t SHA512_H0[SHA512HashSize/4] = { |
248 | | 0x6A09E667, 0xF3BCC908, 0xBB67AE85, 0x84CAA73B, 0x3C6EF372, |
249 | | 0xFE94F82B, 0xA54FF53A, 0x5F1D36F1, 0x510E527F, 0xADE682D1, |
250 | | 0x9B05688C, 0x2B3E6C1F, 0x1F83D9AB, 0xFB41BD6B, 0x5BE0CD19, |
251 | | 0x137E2179 |
252 | | }; |
253 | | |
254 | | #else /* !USE_32BIT_ONLY */ |
255 | | |
256 | | /* Define the SHA shift, rotate left and rotate right macro */ |
257 | | #define SHA512_SHR(bits,word) (((uint64_t)(word)) >> (bits)) |
258 | | #define SHA512_ROTR(bits,word) ((((uint64_t)(word)) >> (bits)) | \ |
259 | | (((uint64_t)(word)) << (64-(bits)))) |
260 | | |
261 | | /* Define the SHA SIGMA and sigma macros */ |
262 | | #define SHA512_SIGMA0(word) \ |
263 | | (SHA512_ROTR(28,word) ^ SHA512_ROTR(34,word) ^ SHA512_ROTR(39,word)) |
264 | | #define SHA512_SIGMA1(word) \ |
265 | | (SHA512_ROTR(14,word) ^ SHA512_ROTR(18,word) ^ SHA512_ROTR(41,word)) |
266 | | #define SHA512_sigma0(word) \ |
267 | | (SHA512_ROTR( 1,word) ^ SHA512_ROTR( 8,word) ^ SHA512_SHR( 7,word)) |
268 | | #define SHA512_sigma1(word) \ |
269 | | (SHA512_ROTR(19,word) ^ SHA512_ROTR(61,word) ^ SHA512_SHR( 6,word)) |
270 | | |
271 | | /* |
272 | | * add "length" to the length |
273 | | */ |
274 | | #define SHA384_512AddLength(context, length) \ |
275 | | (addTemp = context->Length_Low, context->Corrupted = \ |
276 | | ((context->Length_Low += length) < addTemp) && \ |
277 | | (++context->Length_High == 0) ? 1 : 0) |
278 | | |
279 | | /* Local Function Prototypes */ |
280 | | static void SHA384_512Finalize(SHA512Context *context, |
281 | | uint8_t Pad_Byte); |
282 | | static void SHA384_512PadMessage(SHA512Context *context, |
283 | | uint8_t Pad_Byte); |
284 | | static void SHA384_512ProcessMessageBlock(SHA512Context *context); |
285 | | static int SHA384_512Reset(SHA512Context *context, uint64_t H0[]); |
286 | | static int SHA384_512ResultN(SHA512Context *context, |
287 | | uint8_t Message_Digest[], int HashSize); |
288 | | |
289 | | /* Initial Hash Values: FIPS-180-2 sections 5.3.3 and 5.3.4 */ |
290 | | static uint64_t SHA384_H0[] = { |
291 | | 0xCBBB9D5DC1059ED8ll, 0x629A292A367CD507ll, 0x9159015A3070DD17ll, |
292 | | 0x152FECD8F70E5939ll, 0x67332667FFC00B31ll, 0x8EB44A8768581511ll, |
293 | | 0xDB0C2E0D64F98FA7ll, 0x47B5481DBEFA4FA4ll |
294 | | }; |
295 | | static uint64_t SHA512_H0[] = { |
296 | | 0x6A09E667F3BCC908ll, 0xBB67AE8584CAA73Bll, 0x3C6EF372FE94F82Bll, |
297 | | 0xA54FF53A5F1D36F1ll, 0x510E527FADE682D1ll, 0x9B05688C2B3E6C1Fll, |
298 | | 0x1F83D9ABFB41BD6Bll, 0x5BE0CD19137E2179ll |
299 | | }; |
300 | | |
301 | | #endif /* USE_32BIT_ONLY */ |
302 | | |
303 | | /* |
304 | | * SHA384Reset |
305 | | * |
306 | | * Description: |
307 | | * This function will initialize the SHA384Context in preparation |
308 | | * for computing a new SHA384 message digest. |
309 | | * |
310 | | * Parameters: |
311 | | * context: [in/out] |
312 | | * The context to reset. |
313 | | * |
314 | | * Returns: |
315 | | * sha Error Code. |
316 | | * |
317 | | */ |
318 | | int SHA384Reset(SHA384Context *context) |
319 | 432 | { |
320 | 432 | return SHA384_512Reset(context, SHA384_H0); |
321 | 432 | } |
322 | | |
323 | | /* |
324 | | * SHA384Input |
325 | | * |
326 | | * Description: |
327 | | * This function accepts an array of octets as the next portion |
328 | | * of the message. |
329 | | * |
330 | | * Parameters: |
331 | | * context: [in/out] |
332 | | * The SHA context to update |
333 | | * message_array: [in] |
334 | | * An array of characters representing the next portion of |
335 | | * the message. |
336 | | * length: [in] |
337 | | * The length of the message in message_array |
338 | | * |
339 | | * Returns: |
340 | | * sha Error Code. |
341 | | * |
342 | | */ |
343 | | int SHA384Input(SHA384Context *context, |
344 | | const uint8_t *message_array, unsigned int length) |
345 | 432 | { |
346 | 432 | return SHA512Input(context, message_array, length); |
347 | 432 | } |
348 | | |
349 | | /* |
350 | | * SHA384FinalBits |
351 | | * |
352 | | * Description: |
353 | | * This function will add in any final bits of the message. |
354 | | * |
355 | | * Parameters: |
356 | | * context: [in/out] |
357 | | * The SHA context to update |
358 | | * message_bits: [in] |
359 | | * The final bits of the message, in the upper portion of the |
360 | | * byte. (Use 0b###00000 instead of 0b00000### to input the |
361 | | * three bits ###.) |
362 | | * length: [in] |
363 | | * The number of bits in message_bits, between 1 and 7. |
364 | | * |
365 | | * Returns: |
366 | | * sha Error Code. |
367 | | * |
368 | | */ |
369 | | int SHA384FinalBits(SHA384Context *context, |
370 | | const uint8_t message_bits, unsigned int length) |
371 | 0 | { |
372 | 0 | return SHA512FinalBits(context, message_bits, length); |
373 | 0 | } |
374 | | |
375 | | /* |
376 | | * SHA384Result |
377 | | * |
378 | | * Description: |
379 | | * This function will return the 384-bit message |
380 | | * digest into the Message_Digest array provided by the caller. |
381 | | * NOTE: The first octet of hash is stored in the 0th element, |
382 | | * the last octet of hash in the 48th element. |
383 | | * |
384 | | * Parameters: |
385 | | * context: [in/out] |
386 | | * The context to use to calculate the SHA hash. |
387 | | * Message_Digest: [out] |
388 | | * Where the digest is returned. |
389 | | * |
390 | | * Returns: |
391 | | * sha Error Code. |
392 | | * |
393 | | */ |
394 | | int SHA384Result(SHA384Context *context, uint8_t Message_Digest[SHA384HashSize]) |
395 | 432 | { |
396 | 432 | return SHA384_512ResultN(context, Message_Digest, SHA384HashSize); |
397 | 432 | } |
398 | | |
399 | | /* |
400 | | * SHA512Reset |
401 | | * |
402 | | * Description: |
403 | | * This function will initialize the SHA512Context in preparation |
404 | | * for computing a new SHA512 message digest. |
405 | | * |
406 | | * Parameters: |
407 | | * context: [in/out] |
408 | | * The context to reset. |
409 | | * |
410 | | * Returns: |
411 | | * sha Error Code. |
412 | | * |
413 | | */ |
414 | | int SHA512Reset(SHA512Context *context) |
415 | 354 | { |
416 | 354 | return SHA384_512Reset(context, SHA512_H0); |
417 | 354 | } |
418 | | |
419 | | /* |
420 | | * SHA512Input |
421 | | * |
422 | | * Description: |
423 | | * This function accepts an array of octets as the next portion |
424 | | * of the message. |
425 | | * |
426 | | * Parameters: |
427 | | * context: [in/out] |
428 | | * The SHA context to update |
429 | | * message_array: [in] |
430 | | * An array of characters representing the next portion of |
431 | | * the message. |
432 | | * length: [in] |
433 | | * The length of the message in message_array |
434 | | * |
435 | | * Returns: |
436 | | * sha Error Code. |
437 | | * |
438 | | */ |
439 | | int SHA512Input(SHA512Context *context, |
440 | | const uint8_t *message_array, |
441 | | unsigned int length) |
442 | 786 | { |
443 | 786 | if (!length) |
444 | 134 | return shaSuccess; |
445 | | |
446 | 652 | if (!context || !message_array) |
447 | 0 | return shaNull; |
448 | | |
449 | 652 | if (context->Computed) { |
450 | 0 | context->Corrupted = shaStateError; |
451 | 0 | return shaStateError; |
452 | 0 | } |
453 | | |
454 | 652 | if (context->Corrupted) |
455 | 0 | return context->Corrupted; |
456 | | |
457 | 18.6M | while (length-- && !context->Corrupted) { |
458 | 18.6M | context->Message_Block[context->Message_Block_Index++] = |
459 | 18.6M | (uint8_t)(*message_array & 0xFF); |
460 | | |
461 | 18.6M | #ifdef USE_32BIT_ONLY |
462 | 18.6M | uint32_t ADDTO4_temp, ADDTO4_temp2, addTemp[4] = { 0, 0, 0, 0 }; |
463 | | #else |
464 | | uint64_t addTemp; |
465 | | #endif |
466 | 18.6M | if (!SHA384_512AddLength(context, 8) && |
467 | 18.6M | (context->Message_Block_Index == SHA512_Message_Block_Size)) |
468 | 145k | SHA384_512ProcessMessageBlock(context); |
469 | | |
470 | 18.6M | message_array++; |
471 | 18.6M | } |
472 | | |
473 | 652 | return shaSuccess; |
474 | 652 | } |
475 | | |
476 | | /* |
477 | | * SHA512FinalBits |
478 | | * |
479 | | * Description: |
480 | | * This function will add in any final bits of the message. |
481 | | * |
482 | | * Parameters: |
483 | | * context: [in/out] |
484 | | * The SHA context to update |
485 | | * message_bits: [in] |
486 | | * The final bits of the message, in the upper portion of the |
487 | | * byte. (Use 0b###00000 instead of 0b00000### to input the |
488 | | * three bits ###.) |
489 | | * length: [in] |
490 | | * The number of bits in message_bits, between 1 and 7. |
491 | | * |
492 | | * Returns: |
493 | | * sha Error Code. |
494 | | * |
495 | | */ |
496 | | int SHA512FinalBits(SHA512Context *context, |
497 | | const uint8_t message_bits, unsigned int length) |
498 | 0 | { |
499 | 0 | uint8_t masks[8] = { |
500 | 0 | /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, |
501 | 0 | /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0, |
502 | 0 | /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8, |
503 | 0 | /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE |
504 | 0 | }; |
505 | 0 | uint8_t markbit[8] = { |
506 | 0 | /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, |
507 | 0 | /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10, |
508 | 0 | /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04, |
509 | 0 | /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01 |
510 | 0 | }; |
511 | |
|
512 | 0 | if (!length) |
513 | 0 | return shaSuccess; |
514 | | |
515 | 0 | if (!context) |
516 | 0 | return shaNull; |
517 | | |
518 | 0 | if ((context->Computed) || (length >= 8) || (length == 0)) { |
519 | 0 | context->Corrupted = shaStateError; |
520 | 0 | return shaStateError; |
521 | 0 | } |
522 | | |
523 | 0 | if (context->Corrupted) |
524 | 0 | return context->Corrupted; |
525 | | |
526 | 0 | #ifdef USE_32BIT_ONLY |
527 | 0 | uint32_t ADDTO4_temp, ADDTO4_temp2, addTemp[4] = { 0, 0, 0, 0 }; |
528 | | #else |
529 | | uint64_t addTemp; |
530 | | #endif |
531 | 0 | SHA384_512AddLength(context, length); |
532 | 0 | SHA384_512Finalize(context, (uint8_t) |
533 | 0 | ((message_bits & masks[length]) | markbit[length])); |
534 | |
|
535 | 0 | return shaSuccess; |
536 | 0 | } |
537 | | |
538 | | /* |
539 | | * SHA384_512Finalize |
540 | | * |
541 | | * Description: |
542 | | * This helper function finishes off the digest calculations. |
543 | | * |
544 | | * Parameters: |
545 | | * context: [in/out] |
546 | | * The SHA context to update |
547 | | * Pad_Byte: [in] |
548 | | * The last byte to add to the digest before the 0-padding |
549 | | * and length. This will contain the last bits of the message |
550 | | * followed by another single bit. If the message was an |
551 | | * exact multiple of 8-bits long, Pad_Byte will be 0x80. |
552 | | * |
553 | | * Returns: |
554 | | * sha Error Code. |
555 | | * |
556 | | */ |
557 | | static void SHA384_512Finalize(SHA512Context *context, |
558 | | uint8_t Pad_Byte) |
559 | 786 | { |
560 | 786 | int_least16_t i; |
561 | 786 | SHA384_512PadMessage(context, Pad_Byte); |
562 | | /* message may be sensitive, clear it out */ |
563 | 101k | for (i = 0; i < SHA512_Message_Block_Size; ++i) |
564 | 100k | context->Message_Block[i] = 0; |
565 | 786 | #ifdef USE_32BIT_ONLY /* and clear length */ |
566 | 786 | context->Length[0] = context->Length[1] = 0; |
567 | 786 | context->Length[2] = context->Length[3] = 0; |
568 | | #else /* !USE_32BIT_ONLY */ |
569 | | context->Length_Low = 0; |
570 | | context->Length_High = 0; |
571 | | #endif /* USE_32BIT_ONLY */ |
572 | 786 | context->Computed = 1; |
573 | 786 | } |
574 | | |
575 | | /* |
576 | | * SHA512Result |
577 | | * |
578 | | * Description: |
579 | | * This function will return the 512-bit message |
580 | | * digest into the Message_Digest array provided by the caller. |
581 | | * NOTE: The first octet of hash is stored in the 0th element, |
582 | | * the last octet of hash in the 64th element. |
583 | | * |
584 | | * Parameters: |
585 | | * context: [in/out] |
586 | | * The context to use to calculate the SHA hash. |
587 | | * Message_Digest: [out] |
588 | | * Where the digest is returned. |
589 | | * |
590 | | * Returns: |
591 | | * sha Error Code. |
592 | | * |
593 | | */ |
594 | | int SHA512Result(SHA512Context *context, uint8_t Message_Digest[SHA512HashSize]) |
595 | 354 | { |
596 | 354 | return SHA384_512ResultN(context, Message_Digest, SHA512HashSize); |
597 | 354 | } |
598 | | |
599 | | /* |
600 | | * SHA384_512PadMessage |
601 | | * |
602 | | * Description: |
603 | | * According to the standard, the message must be padded to an |
604 | | * even 1024 bits. The first padding bit must be a '1'. The |
605 | | * last 128 bits represent the length of the original message. |
606 | | * All bits in between should be 0. This helper function will |
607 | | * pad the message according to those rules by filling the |
608 | | * Message_Block array accordingly. When it returns, it can be |
609 | | * assumed that the message digest has been computed. |
610 | | * |
611 | | * Parameters: |
612 | | * context: [in/out] |
613 | | * The context to pad |
614 | | * Pad_Byte: [in] |
615 | | * The last byte to add to the digest before the 0-padding |
616 | | * and length. This will contain the last bits of the message |
617 | | * followed by another single bit. If the message was an |
618 | | * exact multiple of 8-bits long, Pad_Byte will be 0x80. |
619 | | * |
620 | | * Returns: |
621 | | * Nothing. |
622 | | * |
623 | | */ |
624 | | static void SHA384_512PadMessage(SHA512Context *context, |
625 | | uint8_t Pad_Byte) |
626 | 786 | { |
627 | | /* |
628 | | * Check to see if the current message block is too small to hold |
629 | | * the initial padding bits and length. If so, we will pad the |
630 | | * block, process it, and then continue padding into a second |
631 | | * block. |
632 | | */ |
633 | 786 | if (context->Message_Block_Index >= (SHA512_Message_Block_Size-16)) { |
634 | 89 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte; |
635 | 746 | while (context->Message_Block_Index < SHA512_Message_Block_Size) |
636 | 657 | context->Message_Block[context->Message_Block_Index++] = 0; |
637 | 89 | SHA384_512ProcessMessageBlock(context); |
638 | 89 | } else |
639 | 697 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte; |
640 | | |
641 | 64.1k | while (context->Message_Block_Index < (SHA512_Message_Block_Size-16)) |
642 | 63.3k | context->Message_Block[context->Message_Block_Index++] = 0; |
643 | | |
644 | | /* |
645 | | * Store the message length as the last 16 octets |
646 | | */ |
647 | 786 | #ifdef USE_32BIT_ONLY |
648 | 786 | context->Message_Block[112] = (uint8_t)(context->Length[0] >> 24); |
649 | 786 | context->Message_Block[113] = (uint8_t)(context->Length[0] >> 16); |
650 | 786 | context->Message_Block[114] = (uint8_t)(context->Length[0] >> 8); |
651 | 786 | context->Message_Block[115] = (uint8_t)(context->Length[0]); |
652 | 786 | context->Message_Block[116] = (uint8_t)(context->Length[1] >> 24); |
653 | 786 | context->Message_Block[117] = (uint8_t)(context->Length[1] >> 16); |
654 | 786 | context->Message_Block[118] = (uint8_t)(context->Length[1] >> 8); |
655 | 786 | context->Message_Block[119] = (uint8_t)(context->Length[1]); |
656 | | |
657 | 786 | context->Message_Block[120] = (uint8_t)(context->Length[2] >> 24); |
658 | 786 | context->Message_Block[121] = (uint8_t)(context->Length[2] >> 16); |
659 | 786 | context->Message_Block[122] = (uint8_t)(context->Length[2] >> 8); |
660 | 786 | context->Message_Block[123] = (uint8_t)(context->Length[2]); |
661 | 786 | context->Message_Block[124] = (uint8_t)(context->Length[3] >> 24); |
662 | 786 | context->Message_Block[125] = (uint8_t)(context->Length[3] >> 16); |
663 | 786 | context->Message_Block[126] = (uint8_t)(context->Length[3] >> 8); |
664 | 786 | context->Message_Block[127] = (uint8_t)(context->Length[3]); |
665 | | #else /* !USE_32BIT_ONLY */ |
666 | | context->Message_Block[112] = (uint8_t)(context->Length_High >> 56); |
667 | | context->Message_Block[113] = (uint8_t)(context->Length_High >> 48); |
668 | | context->Message_Block[114] = (uint8_t)(context->Length_High >> 40); |
669 | | context->Message_Block[115] = (uint8_t)(context->Length_High >> 32); |
670 | | context->Message_Block[116] = (uint8_t)(context->Length_High >> 24); |
671 | | context->Message_Block[117] = (uint8_t)(context->Length_High >> 16); |
672 | | context->Message_Block[118] = (uint8_t)(context->Length_High >> 8); |
673 | | context->Message_Block[119] = (uint8_t)(context->Length_High); |
674 | | |
675 | | context->Message_Block[120] = (uint8_t)(context->Length_Low >> 56); |
676 | | context->Message_Block[121] = (uint8_t)(context->Length_Low >> 48); |
677 | | context->Message_Block[122] = (uint8_t)(context->Length_Low >> 40); |
678 | | context->Message_Block[123] = (uint8_t)(context->Length_Low >> 32); |
679 | | context->Message_Block[124] = (uint8_t)(context->Length_Low >> 24); |
680 | | context->Message_Block[125] = (uint8_t)(context->Length_Low >> 16); |
681 | | context->Message_Block[126] = (uint8_t)(context->Length_Low >> 8); |
682 | | context->Message_Block[127] = (uint8_t)(context->Length_Low); |
683 | | #endif /* USE_32BIT_ONLY */ |
684 | 786 | SHA384_512ProcessMessageBlock(context); |
685 | 786 | } |
686 | | |
687 | | /* |
688 | | * SHA384_512ProcessMessageBlock |
689 | | * |
690 | | * Description: |
691 | | * This helper function will process the next 1024 bits of the |
692 | | * message stored in the Message_Block array. |
693 | | * |
694 | | * Parameters: |
695 | | * context: [in/out] |
696 | | * The SHA context to update |
697 | | * |
698 | | * Returns: |
699 | | * Nothing. |
700 | | * |
701 | | * Comments: |
702 | | * Many of the variable names in this code, especially the |
703 | | * single character names, were used because those were the |
704 | | * names used in the publication. |
705 | | * |
706 | | * |
707 | | */ |
708 | | static void SHA384_512ProcessMessageBlock(SHA512Context *context) |
709 | 146k | { |
710 | | /* Constants defined in FIPS-180-2, section 4.2.3 */ |
711 | 146k | #ifdef USE_32BIT_ONLY |
712 | 146k | static const uint32_t K[80*2] = { |
713 | 146k | 0x428A2F98, 0xD728AE22, 0x71374491, 0x23EF65CD, 0xB5C0FBCF, |
714 | 146k | 0xEC4D3B2F, 0xE9B5DBA5, 0x8189DBBC, 0x3956C25B, 0xF348B538, |
715 | 146k | 0x59F111F1, 0xB605D019, 0x923F82A4, 0xAF194F9B, 0xAB1C5ED5, |
716 | 146k | 0xDA6D8118, 0xD807AA98, 0xA3030242, 0x12835B01, 0x45706FBE, |
717 | 146k | 0x243185BE, 0x4EE4B28C, 0x550C7DC3, 0xD5FFB4E2, 0x72BE5D74, |
718 | 146k | 0xF27B896F, 0x80DEB1FE, 0x3B1696B1, 0x9BDC06A7, 0x25C71235, |
719 | 146k | 0xC19BF174, 0xCF692694, 0xE49B69C1, 0x9EF14AD2, 0xEFBE4786, |
720 | 146k | 0x384F25E3, 0x0FC19DC6, 0x8B8CD5B5, 0x240CA1CC, 0x77AC9C65, |
721 | 146k | 0x2DE92C6F, 0x592B0275, 0x4A7484AA, 0x6EA6E483, 0x5CB0A9DC, |
722 | 146k | 0xBD41FBD4, 0x76F988DA, 0x831153B5, 0x983E5152, 0xEE66DFAB, |
723 | 146k | 0xA831C66D, 0x2DB43210, 0xB00327C8, 0x98FB213F, 0xBF597FC7, |
724 | 146k | 0xBEEF0EE4, 0xC6E00BF3, 0x3DA88FC2, 0xD5A79147, 0x930AA725, |
725 | 146k | 0x06CA6351, 0xE003826F, 0x14292967, 0x0A0E6E70, 0x27B70A85, |
726 | 146k | 0x46D22FFC, 0x2E1B2138, 0x5C26C926, 0x4D2C6DFC, 0x5AC42AED, |
727 | 146k | 0x53380D13, 0x9D95B3DF, 0x650A7354, 0x8BAF63DE, 0x766A0ABB, |
728 | 146k | 0x3C77B2A8, 0x81C2C92E, 0x47EDAEE6, 0x92722C85, 0x1482353B, |
729 | 146k | 0xA2BFE8A1, 0x4CF10364, 0xA81A664B, 0xBC423001, 0xC24B8B70, |
730 | 146k | 0xD0F89791, 0xC76C51A3, 0x0654BE30, 0xD192E819, 0xD6EF5218, |
731 | 146k | 0xD6990624, 0x5565A910, 0xF40E3585, 0x5771202A, 0x106AA070, |
732 | 146k | 0x32BBD1B8, 0x19A4C116, 0xB8D2D0C8, 0x1E376C08, 0x5141AB53, |
733 | 146k | 0x2748774C, 0xDF8EEB99, 0x34B0BCB5, 0xE19B48A8, 0x391C0CB3, |
734 | 146k | 0xC5C95A63, 0x4ED8AA4A, 0xE3418ACB, 0x5B9CCA4F, 0x7763E373, |
735 | 146k | 0x682E6FF3, 0xD6B2B8A3, 0x748F82EE, 0x5DEFB2FC, 0x78A5636F, |
736 | 146k | 0x43172F60, 0x84C87814, 0xA1F0AB72, 0x8CC70208, 0x1A6439EC, |
737 | 146k | 0x90BEFFFA, 0x23631E28, 0xA4506CEB, 0xDE82BDE9, 0xBEF9A3F7, |
738 | 146k | 0xB2C67915, 0xC67178F2, 0xE372532B, 0xCA273ECE, 0xEA26619C, |
739 | 146k | 0xD186B8C7, 0x21C0C207, 0xEADA7DD6, 0xCDE0EB1E, 0xF57D4F7F, |
740 | 146k | 0xEE6ED178, 0x06F067AA, 0x72176FBA, 0x0A637DC5, 0xA2C898A6, |
741 | 146k | 0x113F9804, 0xBEF90DAE, 0x1B710B35, 0x131C471B, 0x28DB77F5, |
742 | 146k | 0x23047D84, 0x32CAAB7B, 0x40C72493, 0x3C9EBE0A, 0x15C9BEBC, |
743 | 146k | 0x431D67C4, 0x9C100D4C, 0x4CC5D4BE, 0xCB3E42B6, 0x597F299C, |
744 | 146k | 0xFC657E2A, 0x5FCB6FAB, 0x3AD6FAEC, 0x6C44198C, 0x4A475817 |
745 | 146k | }; |
746 | 146k | int t, t2, t8; /* Loop counter */ |
747 | 146k | uint32_t temp1[2], temp2[2], /* Temporary word values */ |
748 | 146k | temp3[2], temp4[2], temp5[2]; |
749 | 146k | uint32_t W[2*80]; /* Word sequence */ |
750 | 146k | uint32_t A[2], B[2], C[2], D[2], /* Word buffers */ |
751 | 146k | E[2], F[2], G[2], H[2]; |
752 | 146k | uint32_t ROTR_temp1[2], ROTR_temp2[2]; |
753 | 146k | uint32_t sigma0_temp1[2], sigma0_temp2[2], |
754 | 146k | sigma0_temp3[2], sigma0_temp4[2]; |
755 | 146k | uint32_t sigma1_temp1[2], sigma1_temp2[2], |
756 | 146k | sigma1_temp3[2], sigma1_temp4[2]; |
757 | 146k | uint32_t SIGMA1_temp1[2], SIGMA1_temp2[2], SIGMA1_temp3[2], SIGMA1_temp4[2]; |
758 | 146k | uint32_t SIGMA0_temp1[2], SIGMA0_temp2[2], SIGMA0_temp3[2], SIGMA0_temp4[2]; |
759 | 146k | #ifndef USE_MODIFIED_MACROS |
760 | 146k | uint32_t Ch_temp1[2], Ch_temp2[2], Ch_temp3[2]; |
761 | 146k | uint32_t Maj_temp1[2], Maj_temp2[2], Maj_temp3[2], Maj_temp4[2]; |
762 | 146k | #endif |
763 | | |
764 | | /* Initialize the first 16 words in the array W */ |
765 | 2.48M | for (t = t2 = t8 = 0; t < 16; t++, t8 += 8) { |
766 | 2.33M | W[t2++] = ((((uint32_t)context->Message_Block[t8 ])) << 24) | |
767 | 2.33M | ((((uint32_t)context->Message_Block[t8 + 1])) << 16) | |
768 | 2.33M | ((((uint32_t)context->Message_Block[t8 + 2])) << 8) | |
769 | 2.33M | ((((uint32_t)context->Message_Block[t8 + 3]))); |
770 | 2.33M | W[t2++] = ((((uint32_t)context->Message_Block[t8 + 4])) << 24) | |
771 | 2.33M | ((((uint32_t)context->Message_Block[t8 + 5])) << 16) | |
772 | 2.33M | ((((uint32_t)context->Message_Block[t8 + 6])) << 8) | |
773 | 2.33M | ((((uint32_t)context->Message_Block[t8 + 7]))); |
774 | 2.33M | } |
775 | | |
776 | 9.50M | for (t = 16; t < 80; t++, t2 += 2) { |
777 | | /* W[t] = SHA512_sigma1(W[t-2]) + W[t-7] + |
778 | | SHA512_sigma0(W[t-15]) + W[t-16]; */ |
779 | 9.35M | uint32_t *Wt2 = &W[t2-2*2]; |
780 | 9.35M | uint32_t *Wt7 = &W[t2-7*2]; |
781 | 9.35M | uint32_t *Wt15 = &W[t2-15*2]; |
782 | 9.35M | uint32_t *Wt16 = &W[t2-16*2]; |
783 | 9.35M | SHA512_sigma1(Wt2, temp1); |
784 | 9.35M | SHA512_ADD(temp1, Wt7, temp2); |
785 | 9.35M | SHA512_sigma0(Wt15, temp1); |
786 | 9.35M | SHA512_ADD(temp1, Wt16, temp3); |
787 | 9.35M | SHA512_ADD(temp2, temp3, &W[t2]); |
788 | 9.35M | } |
789 | | |
790 | 146k | A[0] = context->Intermediate_Hash[0]; |
791 | 146k | A[1] = context->Intermediate_Hash[1]; |
792 | 146k | B[0] = context->Intermediate_Hash[2]; |
793 | 146k | B[1] = context->Intermediate_Hash[3]; |
794 | 146k | C[0] = context->Intermediate_Hash[4]; |
795 | 146k | C[1] = context->Intermediate_Hash[5]; |
796 | 146k | D[0] = context->Intermediate_Hash[6]; |
797 | 146k | D[1] = context->Intermediate_Hash[7]; |
798 | 146k | E[0] = context->Intermediate_Hash[8]; |
799 | 146k | E[1] = context->Intermediate_Hash[9]; |
800 | 146k | F[0] = context->Intermediate_Hash[10]; |
801 | 146k | F[1] = context->Intermediate_Hash[11]; |
802 | 146k | G[0] = context->Intermediate_Hash[12]; |
803 | 146k | G[1] = context->Intermediate_Hash[13]; |
804 | 146k | H[0] = context->Intermediate_Hash[14]; |
805 | 146k | H[1] = context->Intermediate_Hash[15]; |
806 | | |
807 | 11.8M | for (t = t2 = 0; t < 80; t++, t2 += 2) { |
808 | | /* |
809 | | * temp1 = H + SHA512_SIGMA1(E) + SHA_Ch(E,F,G) + K[t] + W[t]; |
810 | | */ |
811 | 11.6M | SHA512_SIGMA1(E,temp1); |
812 | 11.6M | SHA512_ADD(H, temp1, temp2); |
813 | 11.6M | SHA_Ch(E,F,G,temp3); |
814 | 11.6M | SHA512_ADD(temp2, temp3, temp4); |
815 | 11.6M | SHA512_ADD(&K[t2], &W[t2], temp5); |
816 | 11.6M | SHA512_ADD(temp4, temp5, temp1); |
817 | | /* |
818 | | * temp2 = SHA512_SIGMA0(A) + SHA_Maj(A,B,C); |
819 | | */ |
820 | 11.6M | SHA512_SIGMA0(A,temp3); |
821 | 11.6M | SHA_Maj(A,B,C,temp4); |
822 | 11.6M | SHA512_ADD(temp3, temp4, temp2); |
823 | 11.6M | H[0] = G[0]; H[1] = G[1]; |
824 | 11.6M | G[0] = F[0]; G[1] = F[1]; |
825 | 11.6M | F[0] = E[0]; F[1] = E[1]; |
826 | 11.6M | SHA512_ADD(D, temp1, E); |
827 | 11.6M | D[0] = C[0]; D[1] = C[1]; |
828 | 11.6M | C[0] = B[0]; C[1] = B[1]; |
829 | 11.6M | B[0] = A[0]; B[1] = A[1]; |
830 | 11.6M | SHA512_ADD(temp1, temp2, A); |
831 | 11.6M | } |
832 | | |
833 | 146k | uint32_t ADDTO2_temp; |
834 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[0], A); |
835 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[2], B); |
836 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[4], C); |
837 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[6], D); |
838 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[8], E); |
839 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[10], F); |
840 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[12], G); |
841 | 146k | SHA512_ADDTO2(&context->Intermediate_Hash[14], H); |
842 | | |
843 | | #else /* !USE_32BIT_ONLY */ |
844 | | static const uint64_t K[80] = { |
845 | | 0x428A2F98D728AE22ll, 0x7137449123EF65CDll, 0xB5C0FBCFEC4D3B2Fll, |
846 | | 0xE9B5DBA58189DBBCll, 0x3956C25BF348B538ll, 0x59F111F1B605D019ll, |
847 | | 0x923F82A4AF194F9Bll, 0xAB1C5ED5DA6D8118ll, 0xD807AA98A3030242ll, |
848 | | 0x12835B0145706FBEll, 0x243185BE4EE4B28Cll, 0x550C7DC3D5FFB4E2ll, |
849 | | 0x72BE5D74F27B896Fll, 0x80DEB1FE3B1696B1ll, 0x9BDC06A725C71235ll, |
850 | | 0xC19BF174CF692694ll, 0xE49B69C19EF14AD2ll, 0xEFBE4786384F25E3ll, |
851 | | 0x0FC19DC68B8CD5B5ll, 0x240CA1CC77AC9C65ll, 0x2DE92C6F592B0275ll, |
852 | | 0x4A7484AA6EA6E483ll, 0x5CB0A9DCBD41FBD4ll, 0x76F988DA831153B5ll, |
853 | | 0x983E5152EE66DFABll, 0xA831C66D2DB43210ll, 0xB00327C898FB213Fll, |
854 | | 0xBF597FC7BEEF0EE4ll, 0xC6E00BF33DA88FC2ll, 0xD5A79147930AA725ll, |
855 | | 0x06CA6351E003826Fll, 0x142929670A0E6E70ll, 0x27B70A8546D22FFCll, |
856 | | 0x2E1B21385C26C926ll, 0x4D2C6DFC5AC42AEDll, 0x53380D139D95B3DFll, |
857 | | 0x650A73548BAF63DEll, 0x766A0ABB3C77B2A8ll, 0x81C2C92E47EDAEE6ll, |
858 | | 0x92722C851482353Bll, 0xA2BFE8A14CF10364ll, 0xA81A664BBC423001ll, |
859 | | 0xC24B8B70D0F89791ll, 0xC76C51A30654BE30ll, 0xD192E819D6EF5218ll, |
860 | | 0xD69906245565A910ll, 0xF40E35855771202All, 0x106AA07032BBD1B8ll, |
861 | | 0x19A4C116B8D2D0C8ll, 0x1E376C085141AB53ll, 0x2748774CDF8EEB99ll, |
862 | | 0x34B0BCB5E19B48A8ll, 0x391C0CB3C5C95A63ll, 0x4ED8AA4AE3418ACBll, |
863 | | 0x5B9CCA4F7763E373ll, 0x682E6FF3D6B2B8A3ll, 0x748F82EE5DEFB2FCll, |
864 | | 0x78A5636F43172F60ll, 0x84C87814A1F0AB72ll, 0x8CC702081A6439ECll, |
865 | | 0x90BEFFFA23631E28ll, 0xA4506CEBDE82BDE9ll, 0xBEF9A3F7B2C67915ll, |
866 | | 0xC67178F2E372532Bll, 0xCA273ECEEA26619Cll, 0xD186B8C721C0C207ll, |
867 | | 0xEADA7DD6CDE0EB1Ell, 0xF57D4F7FEE6ED178ll, 0x06F067AA72176FBAll, |
868 | | 0x0A637DC5A2C898A6ll, 0x113F9804BEF90DAEll, 0x1B710B35131C471Bll, |
869 | | 0x28DB77F523047D84ll, 0x32CAAB7B40C72493ll, 0x3C9EBE0A15C9BEBCll, |
870 | | 0x431D67C49C100D4Cll, 0x4CC5D4BECB3E42B6ll, 0x597F299CFC657E2All, |
871 | | 0x5FCB6FAB3AD6FAECll, 0x6C44198C4A475817ll |
872 | | }; |
873 | | int t, t8; /* Loop counter */ |
874 | | uint64_t temp1, temp2; /* Temporary word value */ |
875 | | uint64_t W[80]; /* Word sequence */ |
876 | | uint64_t A, B, C, D, E, F, G, H; /* Word buffers */ |
877 | | |
878 | | /* |
879 | | * Initialize the first 16 words in the array W |
880 | | */ |
881 | | for (t = t8 = 0; t < 16; t++, t8 += 8) |
882 | | W[t] = ((uint64_t)(context->Message_Block[t8 ]) << 56) | |
883 | | ((uint64_t)(context->Message_Block[t8 + 1]) << 48) | |
884 | | ((uint64_t)(context->Message_Block[t8 + 2]) << 40) | |
885 | | ((uint64_t)(context->Message_Block[t8 + 3]) << 32) | |
886 | | ((uint64_t)(context->Message_Block[t8 + 4]) << 24) | |
887 | | ((uint64_t)(context->Message_Block[t8 + 5]) << 16) | |
888 | | ((uint64_t)(context->Message_Block[t8 + 6]) << 8) | |
889 | | ((uint64_t)(context->Message_Block[t8 + 7])); |
890 | | |
891 | | for (t = 16; t < 80; t++) |
892 | | W[t] = SHA512_sigma1(W[t-2]) + W[t-7] + |
893 | | SHA512_sigma0(W[t-15]) + W[t-16]; |
894 | | |
895 | | A = context->Intermediate_Hash[0]; |
896 | | B = context->Intermediate_Hash[1]; |
897 | | C = context->Intermediate_Hash[2]; |
898 | | D = context->Intermediate_Hash[3]; |
899 | | E = context->Intermediate_Hash[4]; |
900 | | F = context->Intermediate_Hash[5]; |
901 | | G = context->Intermediate_Hash[6]; |
902 | | H = context->Intermediate_Hash[7]; |
903 | | |
904 | | for (t = 0; t < 80; t++) { |
905 | | #ifdef USE_32BIT_ONLY |
906 | | uint32_t ROTR_temp1[2], ROTR_temp2[2]; |
907 | | uint32_t SIGMA0_temp1[2], SIGMA0_temp2[2], SIGMA0_temp3[2], SIGMA0_temp4[2]; |
908 | | #endif |
909 | | temp1 = H + SHA512_SIGMA1(E) + SHA_Ch(E,F,G) + K[t] + W[t]; |
910 | | temp2 = SHA512_SIGMA0(A) + SHA_Maj(A,B,C); |
911 | | H = G; |
912 | | G = F; |
913 | | F = E; |
914 | | E = D + temp1; |
915 | | D = C; |
916 | | C = B; |
917 | | B = A; |
918 | | A = temp1 + temp2; |
919 | | } |
920 | | |
921 | | context->Intermediate_Hash[0] += A; |
922 | | context->Intermediate_Hash[1] += B; |
923 | | context->Intermediate_Hash[2] += C; |
924 | | context->Intermediate_Hash[3] += D; |
925 | | context->Intermediate_Hash[4] += E; |
926 | | context->Intermediate_Hash[5] += F; |
927 | | context->Intermediate_Hash[6] += G; |
928 | | context->Intermediate_Hash[7] += H; |
929 | | #endif /* USE_32BIT_ONLY */ |
930 | | |
931 | 146k | context->Message_Block_Index = 0; |
932 | 146k | } |
933 | | |
934 | | /* |
935 | | * SHA384_512Reset |
936 | | * |
937 | | * Description: |
938 | | * This helper function will initialize the SHA512Context in |
939 | | * preparation for computing a new SHA384 or SHA512 message |
940 | | * digest. |
941 | | * |
942 | | * Parameters: |
943 | | * context: [in/out] |
944 | | * The context to reset. |
945 | | * H0 |
946 | | * The initial hash value to use. |
947 | | * |
948 | | * Returns: |
949 | | * sha Error Code. |
950 | | * |
951 | | */ |
952 | | #ifdef USE_32BIT_ONLY |
953 | | static int SHA384_512Reset(SHA512Context *context, uint32_t H0[]) |
954 | | #else /* !USE_32BIT_ONLY */ |
955 | | static int SHA384_512Reset(SHA512Context *context, uint64_t H0[]) |
956 | | #endif /* USE_32BIT_ONLY */ |
957 | 786 | { |
958 | 786 | int i; |
959 | 786 | if (!context) |
960 | 0 | return shaNull; |
961 | | |
962 | 786 | context->Message_Block_Index = 0; |
963 | | |
964 | 786 | #ifdef USE_32BIT_ONLY |
965 | 786 | context->Length[0] = context->Length[1] = 0; |
966 | 786 | context->Length[2] = context->Length[3] = 0; |
967 | | |
968 | 13.3k | for (i = 0; i < SHA512HashSize/4; i++) |
969 | 12.5k | context->Intermediate_Hash[i] = H0[i]; |
970 | | #else /* !USE_32BIT_ONLY */ |
971 | | context->Length_High = context->Length_Low = 0; |
972 | | |
973 | | for (i = 0; i < SHA512HashSize/8; i++) |
974 | | context->Intermediate_Hash[i] = H0[i]; |
975 | | #endif /* USE_32BIT_ONLY */ |
976 | | |
977 | 786 | context->Computed = 0; |
978 | 786 | context->Corrupted = 0; |
979 | | |
980 | 786 | return shaSuccess; |
981 | 786 | } |
982 | | |
983 | | /* |
984 | | * SHA384_512ResultN |
985 | | * |
986 | | * Description: |
987 | | * This helper function will return the 384-bit or 512-bit message |
988 | | * digest into the Message_Digest array provided by the caller. |
989 | | * NOTE: The first octet of hash is stored in the 0th element, |
990 | | * the last octet of hash in the 48th/64th element. |
991 | | * |
992 | | * Parameters: |
993 | | * context: [in/out] |
994 | | * The context to use to calculate the SHA hash. |
995 | | * Message_Digest: [out] |
996 | | * Where the digest is returned. |
997 | | * HashSize: [in] |
998 | | * The size of the hash, either 48 or 64. |
999 | | * |
1000 | | * Returns: |
1001 | | * sha Error Code. |
1002 | | * |
1003 | | */ |
1004 | | static int SHA384_512ResultN(SHA512Context *context, |
1005 | | uint8_t Message_Digest[], int HashSize) |
1006 | 786 | { |
1007 | 786 | int i; |
1008 | | |
1009 | 786 | #ifdef USE_32BIT_ONLY |
1010 | 786 | int i2; |
1011 | 786 | #endif /* USE_32BIT_ONLY */ |
1012 | | |
1013 | 786 | if (!context || !Message_Digest) |
1014 | 0 | return shaNull; |
1015 | | |
1016 | 786 | if (context->Corrupted) |
1017 | 0 | return context->Corrupted; |
1018 | | |
1019 | 786 | if (!context->Computed) |
1020 | 786 | SHA384_512Finalize(context, 0x80); |
1021 | | |
1022 | 786 | #ifdef USE_32BIT_ONLY |
1023 | 6.21k | for (i = i2 = 0; i < HashSize; ) { |
1024 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2]>>24); |
1025 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2]>>16); |
1026 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2]>>8); |
1027 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2++]); |
1028 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2]>>24); |
1029 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2]>>16); |
1030 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2]>>8); |
1031 | 5.42k | Message_Digest[i++]=(uint8_t)(context->Intermediate_Hash[i2++]); |
1032 | 5.42k | } |
1033 | | #else /* !USE_32BIT_ONLY */ |
1034 | | for (i = 0; i < HashSize; ++i) |
1035 | | Message_Digest[i] = (uint8_t) |
1036 | | (context->Intermediate_Hash[i>>3] >> 8 * ( 7 - ( i % 8 ) )); |
1037 | | #endif /* USE_32BIT_ONLY */ |
1038 | | |
1039 | 786 | return shaSuccess; |
1040 | 786 | } |