DEFINITIONS
This source file includes following functions.
- SHA256_Init
- SHA256_Transform
- SHA256_Transform
- SHA256_Update
- SHA256_Final
- SHA256_Equal
- SHA512_Init
- SHA512_Transform
- SHA512_Transform
- SHA512_Update
- SHA512_Last
- SHA512_Final
- SHA512_Equal
- SHA384_Init
- SHA384_Update
- SHA384_Final
- SHA384_Equal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <assert.h>
42 #include "sha2.h"
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 typedef uint8_t sha2_byte;
67 typedef uint32_t sha2_word32;
68 typedef uint64_t sha2_word64;
69
70 #if defined(__GNUC__)
71 #define ULL(number) number##ULL
72 #else
73 #define ULL(number) (uint64_t)(number)
74 #endif
75
76
77
78
79 #define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
80 #define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
81 #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
82
83
84
85 #ifndef WORDS_BIGENDIAN
86 #define REVERSE32(w,x) { \
87 sha2_word32 tmp = (w); \
88 tmp = (tmp >> 16) | (tmp << 16); \
89 (x) = ((tmp & 0xff00ff00UL) >> 8) | ((tmp & 0x00ff00ffUL) << 8); \
90 }
91 #define REVERSE64(w,x) { \
92 sha2_word64 tmp = (w); \
93 tmp = (tmp >> 32) | (tmp << 32); \
94 tmp = ((tmp & ULL(0xff00ff00ff00ff00)) >> 8) | \
95 ((tmp & ULL(0x00ff00ff00ff00ff)) << 8); \
96 (x) = ((tmp & ULL(0xffff0000ffff0000)) >> 16) | \
97 ((tmp & ULL(0x0000ffff0000ffff)) << 16); \
98 }
99 #endif
100
101
102
103
104
105
106 #define ADDINC128(w,n) { \
107 (w)[0] += (sha2_word64)(n); \
108 if ((w)[0] < (n)) { \
109 (w)[1]++; \
110 } \
111 }
112
113
114
115
116
117
118
119
120
121
122 #if !defined(SHA2_USE_MEMSET_MEMCPY) && !defined(SHA2_USE_BZERO_BCOPY)
123
124 #define SHA2_USE_MEMSET_MEMCPY 1
125 #endif
126 #if defined(SHA2_USE_MEMSET_MEMCPY) && defined(SHA2_USE_BZERO_BCOPY)
127
128 #error Define either SHA2_USE_MEMSET_MEMCPY or SHA2_USE_BZERO_BCOPY, not both!
129 #endif
130
131 #ifdef SHA2_USE_MEMSET_MEMCPY
132 #define MEMSET_BZERO(p,l) memset((p), 0, (l))
133 #define MEMCPY_BCOPY(d,s,l) memcpy((d), (s), (l))
134 #endif
135 #ifdef SHA2_USE_BZERO_BCOPY
136 #define MEMSET_BZERO(p,l) bzero((p), (l))
137 #define MEMCPY_BCOPY(d,s,l) bcopy((s), (d), (l))
138 #endif
139
140
141
142
143
144
145
146
147
148
149
150
151 #define R(b,x) ((x) >> (b))
152
153 #define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
154
155 #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
156
157
158 #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
159 #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
160
161
162 #define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
163 #define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
164 #define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
165 #define sigma1_256(x) (S32(17, (x)) ^ S32(19, (x)) ^ R(10, (x)))
166
167
168 #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x)))
169 #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x)))
170 #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ R( 7, (x)))
171 #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ R( 6, (x)))
172
173
174
175
176
177
178 void SHA512_Last(SHA512_CTX*);
179 void SHA256_Transform(SHA256_CTX*, const sha2_word32*);
180 void SHA512_Transform(SHA512_CTX*, const sha2_word64*);
181
182
183
184
185 const static sha2_word32 K256[64] = {
186 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
187 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
188 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
189 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL,
190 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
191 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL,
192 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL,
193 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL,
194 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL,
195 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
196 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL,
197 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL,
198 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL,
199 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL,
200 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
201 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
202 };
203
204
205 const static sha2_word32 sha256_initial_hash_value[8] = {
206 0x6a09e667UL,
207 0xbb67ae85UL,
208 0x3c6ef372UL,
209 0xa54ff53aUL,
210 0x510e527fUL,
211 0x9b05688cUL,
212 0x1f83d9abUL,
213 0x5be0cd19UL
214 };
215
216
217 const static sha2_word64 K512[80] = {
218 ULL(0x428a2f98d728ae22), ULL(0x7137449123ef65cd),
219 ULL(0xb5c0fbcfec4d3b2f), ULL(0xe9b5dba58189dbbc),
220 ULL(0x3956c25bf348b538), ULL(0x59f111f1b605d019),
221 ULL(0x923f82a4af194f9b), ULL(0xab1c5ed5da6d8118),
222 ULL(0xd807aa98a3030242), ULL(0x12835b0145706fbe),
223 ULL(0x243185be4ee4b28c), ULL(0x550c7dc3d5ffb4e2),
224 ULL(0x72be5d74f27b896f), ULL(0x80deb1fe3b1696b1),
225 ULL(0x9bdc06a725c71235), ULL(0xc19bf174cf692694),
226 ULL(0xe49b69c19ef14ad2), ULL(0xefbe4786384f25e3),
227 ULL(0x0fc19dc68b8cd5b5), ULL(0x240ca1cc77ac9c65),
228 ULL(0x2de92c6f592b0275), ULL(0x4a7484aa6ea6e483),
229 ULL(0x5cb0a9dcbd41fbd4), ULL(0x76f988da831153b5),
230 ULL(0x983e5152ee66dfab), ULL(0xa831c66d2db43210),
231 ULL(0xb00327c898fb213f), ULL(0xbf597fc7beef0ee4),
232 ULL(0xc6e00bf33da88fc2), ULL(0xd5a79147930aa725),
233 ULL(0x06ca6351e003826f), ULL(0x142929670a0e6e70),
234 ULL(0x27b70a8546d22ffc), ULL(0x2e1b21385c26c926),
235 ULL(0x4d2c6dfc5ac42aed), ULL(0x53380d139d95b3df),
236 ULL(0x650a73548baf63de), ULL(0x766a0abb3c77b2a8),
237 ULL(0x81c2c92e47edaee6), ULL(0x92722c851482353b),
238 ULL(0xa2bfe8a14cf10364), ULL(0xa81a664bbc423001),
239 ULL(0xc24b8b70d0f89791), ULL(0xc76c51a30654be30),
240 ULL(0xd192e819d6ef5218), ULL(0xd69906245565a910),
241 ULL(0xf40e35855771202a), ULL(0x106aa07032bbd1b8),
242 ULL(0x19a4c116b8d2d0c8), ULL(0x1e376c085141ab53),
243 ULL(0x2748774cdf8eeb99), ULL(0x34b0bcb5e19b48a8),
244 ULL(0x391c0cb3c5c95a63), ULL(0x4ed8aa4ae3418acb),
245 ULL(0x5b9cca4f7763e373), ULL(0x682e6ff3d6b2b8a3),
246 ULL(0x748f82ee5defb2fc), ULL(0x78a5636f43172f60),
247 ULL(0x84c87814a1f0ab72), ULL(0x8cc702081a6439ec),
248 ULL(0x90befffa23631e28), ULL(0xa4506cebde82bde9),
249 ULL(0xbef9a3f7b2c67915), ULL(0xc67178f2e372532b),
250 ULL(0xca273eceea26619c), ULL(0xd186b8c721c0c207),
251 ULL(0xeada7dd6cde0eb1e), ULL(0xf57d4f7fee6ed178),
252 ULL(0x06f067aa72176fba), ULL(0x0a637dc5a2c898a6),
253 ULL(0x113f9804bef90dae), ULL(0x1b710b35131c471b),
254 ULL(0x28db77f523047d84), ULL(0x32caab7b40c72493),
255 ULL(0x3c9ebe0a15c9bebc), ULL(0x431d67c49c100d4c),
256 ULL(0x4cc5d4becb3e42b6), ULL(0x597f299cfc657e2a),
257 ULL(0x5fcb6fab3ad6faec), ULL(0x6c44198c4a475817)
258 };
259
260
261 const static sha2_word64 sha384_initial_hash_value[8] = {
262 ULL(0xcbbb9d5dc1059ed8),
263 ULL(0x629a292a367cd507),
264 ULL(0x9159015a3070dd17),
265 ULL(0x152fecd8f70e5939),
266 ULL(0x67332667ffc00b31),
267 ULL(0x8eb44a8768581511),
268 ULL(0xdb0c2e0d64f98fa7),
269 ULL(0x47b5481dbefa4fa4)
270 };
271
272
273 const static sha2_word64 sha512_initial_hash_value[8] = {
274 ULL(0x6a09e667f3bcc908),
275 ULL(0xbb67ae8584caa73b),
276 ULL(0x3c6ef372fe94f82b),
277 ULL(0xa54ff53a5f1d36f1),
278 ULL(0x510e527fade682d1),
279 ULL(0x9b05688c2b3e6c1f),
280 ULL(0x1f83d9abfb41bd6b),
281 ULL(0x5be0cd19137e2179)
282 };
283
284
285
286 void SHA256_Init(SHA256_CTX* context) {
287 if (context == (SHA256_CTX*)0) {
288 return;
289 }
290 MEMCPY_BCOPY(context->state, sha256_initial_hash_value, SHA256_DIGEST_LENGTH);
291 MEMSET_BZERO(context->buffer, SHA256_BLOCK_LENGTH);
292 context->bitcount = 0;
293 }
294
295 #ifdef SHA2_UNROLL_TRANSFORM
296
297
298
299 #ifndef WORDS_BIGENDIAN
300
301 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
302 REVERSE32(*data++, W256[j]); \
303 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
304 K256[j] + W256[j]; \
305 (d) += T1; \
306 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
307 j++
308
309
310 #else
311
312 #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) \
313 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + \
314 K256[j] + (W256[j] = *data++); \
315 (d) += T1; \
316 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
317 j++
318
319 #endif
320
321 #define ROUND256(a,b,c,d,e,f,g,h) \
322 s0 = W256[(j+1)&0x0f]; \
323 s0 = sigma0_256(s0); \
324 s1 = W256[(j+14)&0x0f]; \
325 s1 = sigma1_256(s1); \
326 T1 = (h) + Sigma1_256(e) + Ch((e), (f), (g)) + K256[j] + \
327 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0); \
328 (d) += T1; \
329 (h) = T1 + Sigma0_256(a) + Maj((a), (b), (c)); \
330 j++
331
332 void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
333 sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
334 sha2_word32 T1, *W256;
335 int j;
336
337 W256 = (sha2_word32*)context->buffer;
338
339
340 a = context->state[0];
341 b = context->state[1];
342 c = context->state[2];
343 d = context->state[3];
344 e = context->state[4];
345 f = context->state[5];
346 g = context->state[6];
347 h = context->state[7];
348
349 j = 0;
350 do {
351
352 ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
353 ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
354 ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
355 ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
356 ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
357 ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
358 ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
359 ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
360 } while (j < 16);
361
362
363 do {
364 ROUND256(a,b,c,d,e,f,g,h);
365 ROUND256(h,a,b,c,d,e,f,g);
366 ROUND256(g,h,a,b,c,d,e,f);
367 ROUND256(f,g,h,a,b,c,d,e);
368 ROUND256(e,f,g,h,a,b,c,d);
369 ROUND256(d,e,f,g,h,a,b,c);
370 ROUND256(c,d,e,f,g,h,a,b);
371 ROUND256(b,c,d,e,f,g,h,a);
372 } while (j < 64);
373
374
375 context->state[0] += a;
376 context->state[1] += b;
377 context->state[2] += c;
378 context->state[3] += d;
379 context->state[4] += e;
380 context->state[5] += f;
381 context->state[6] += g;
382 context->state[7] += h;
383
384
385 a = b = c = d = e = f = g = h = T1 = 0;
386 }
387
388 #else
389
390 void SHA256_Transform(SHA256_CTX* context, const sha2_word32* data) {
391 sha2_word32 a, b, c, d, e, f, g, h, s0, s1;
392 sha2_word32 T1, T2, *W256;
393 int j;
394
395 W256 = (sha2_word32*)context->buffer;
396
397
398 a = context->state[0];
399 b = context->state[1];
400 c = context->state[2];
401 d = context->state[3];
402 e = context->state[4];
403 f = context->state[5];
404 g = context->state[6];
405 h = context->state[7];
406
407 j = 0;
408 do {
409 #ifndef WORDS_BIGENDIAN
410
411 REVERSE32(*data++,W256[j]);
412
413 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + W256[j];
414 #else
415
416 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] + (W256[j] = *data++);
417 #endif
418 T2 = Sigma0_256(a) + Maj(a, b, c);
419 h = g;
420 g = f;
421 f = e;
422 e = d + T1;
423 d = c;
424 c = b;
425 b = a;
426 a = T1 + T2;
427
428 j++;
429 } while (j < 16);
430
431 do {
432
433 s0 = W256[(j+1)&0x0f];
434 s0 = sigma0_256(s0);
435 s1 = W256[(j+14)&0x0f];
436 s1 = sigma1_256(s1);
437
438
439 T1 = h + Sigma1_256(e) + Ch(e, f, g) + K256[j] +
440 (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
441 T2 = Sigma0_256(a) + Maj(a, b, c);
442 h = g;
443 g = f;
444 f = e;
445 e = d + T1;
446 d = c;
447 c = b;
448 b = a;
449 a = T1 + T2;
450
451 j++;
452 } while (j < 64);
453
454
455 context->state[0] += a;
456 context->state[1] += b;
457 context->state[2] += c;
458 context->state[3] += d;
459 context->state[4] += e;
460 context->state[5] += f;
461 context->state[6] += g;
462 context->state[7] += h;
463
464
465 a = b = c = d = e = f = g = h = T1 = T2 = 0;
466 }
467
468 #endif
469
470 void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) {
471 unsigned int freespace, usedspace;
472
473 if (len == 0) {
474
475 return;
476 }
477
478
479 assert(context != NULL && data != NULL);
480
481 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
482 if (usedspace > 0) {
483
484 freespace = SHA256_BLOCK_LENGTH - usedspace;
485
486 if (len >= freespace) {
487
488 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
489 context->bitcount += freespace << 3;
490 len -= freespace;
491 data += freespace;
492 SHA256_Transform(context, (sha2_word32*)context->buffer);
493 } else {
494
495 MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
496 context->bitcount += len << 3;
497
498 usedspace = freespace = 0;
499 return;
500 }
501 }
502 while (len >= SHA256_BLOCK_LENGTH) {
503
504 SHA256_Transform(context, (const sha2_word32*)data);
505 context->bitcount += SHA256_BLOCK_LENGTH << 3;
506 len -= SHA256_BLOCK_LENGTH;
507 data += SHA256_BLOCK_LENGTH;
508 }
509 if (len > 0) {
510
511 MEMCPY_BCOPY(context->buffer, data, len);
512 context->bitcount += len << 3;
513 }
514
515 usedspace = freespace = 0;
516 }
517
518 void SHA256_Final(sha2_byte digest[], SHA256_CTX* context) {
519 sha2_word32 *d = (sha2_word32*)digest;
520 unsigned int usedspace;
521
522
523 assert(context != NULL);
524
525
526 if (digest != (sha2_byte*)0) {
527 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH;
528 #ifndef WORDS_BIGENDIAN
529
530 REVERSE64(context->bitcount,context->bitcount);
531 #endif
532 if (usedspace > 0) {
533
534 context->buffer[usedspace++] = 0x80;
535
536 if (usedspace <= SHA256_SHORT_BLOCK_LENGTH) {
537
538 MEMSET_BZERO(&context->buffer[usedspace], SHA256_SHORT_BLOCK_LENGTH - usedspace);
539 } else {
540 if (usedspace < SHA256_BLOCK_LENGTH) {
541 MEMSET_BZERO(&context->buffer[usedspace], SHA256_BLOCK_LENGTH - usedspace);
542 }
543
544 SHA256_Transform(context, (sha2_word32*)context->buffer);
545
546
547 MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
548 }
549 } else {
550
551 MEMSET_BZERO(context->buffer, SHA256_SHORT_BLOCK_LENGTH);
552
553
554 *context->buffer = 0x80;
555 }
556
557 *(sha2_word64*)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
558
559
560 SHA256_Transform(context, (sha2_word32*)context->buffer);
561
562 #ifndef WORDS_BIGENDIAN
563 {
564
565 int j;
566 for (j = 0; j < 8; j++) {
567 REVERSE32(context->state[j],context->state[j]);
568 *d++ = context->state[j];
569 }
570 }
571 #else
572 MEMCPY_BCOPY(d, context->state, SHA256_DIGEST_LENGTH);
573 #endif
574 }
575
576
577 MEMSET_BZERO(context, sizeof(SHA256_CTX));
578 usedspace = 0;
579 }
580
581 int SHA256_Equal(SHA256_CTX* pctx1, SHA256_CTX* pctx2) {
582 return pctx1->bitcount == pctx2->bitcount
583 && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
584 && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
585 }
586
587
588 void SHA512_Init(SHA512_CTX* context) {
589 if (context == (SHA512_CTX*)0) {
590 return;
591 }
592 MEMCPY_BCOPY(context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH);
593 MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH);
594 context->bitcount[0] = context->bitcount[1] = 0;
595 }
596
597 #ifdef SHA2_UNROLL_TRANSFORM
598
599
600 #ifndef WORDS_BIGENDIAN
601
602 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
603 REVERSE64(*data++, W512[j]); \
604 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
605 K512[j] + W512[j]; \
606 (d) += T1, \
607 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)), \
608 j++
609
610
611 #else
612
613 #define ROUND512_0_TO_15(a,b,c,d,e,f,g,h) \
614 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + \
615 K512[j] + (W512[j] = *data++); \
616 (d) += T1; \
617 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
618 j++
619
620 #endif
621
622 #define ROUND512(a,b,c,d,e,f,g,h) \
623 s0 = W512[(j+1)&0x0f]; \
624 s0 = sigma0_512(s0); \
625 s1 = W512[(j+14)&0x0f]; \
626 s1 = sigma1_512(s1); \
627 T1 = (h) + Sigma1_512(e) + Ch((e), (f), (g)) + K512[j] + \
628 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0); \
629 (d) += T1; \
630 (h) = T1 + Sigma0_512(a) + Maj((a), (b), (c)); \
631 j++
632
633 void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
634 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
635 sha2_word64 T1, *W512 = (sha2_word64*)context->buffer;
636 int j;
637
638
639 a = context->state[0];
640 b = context->state[1];
641 c = context->state[2];
642 d = context->state[3];
643 e = context->state[4];
644 f = context->state[5];
645 g = context->state[6];
646 h = context->state[7];
647
648 j = 0;
649 do {
650 ROUND512_0_TO_15(a,b,c,d,e,f,g,h);
651 ROUND512_0_TO_15(h,a,b,c,d,e,f,g);
652 ROUND512_0_TO_15(g,h,a,b,c,d,e,f);
653 ROUND512_0_TO_15(f,g,h,a,b,c,d,e);
654 ROUND512_0_TO_15(e,f,g,h,a,b,c,d);
655 ROUND512_0_TO_15(d,e,f,g,h,a,b,c);
656 ROUND512_0_TO_15(c,d,e,f,g,h,a,b);
657 ROUND512_0_TO_15(b,c,d,e,f,g,h,a);
658 } while (j < 16);
659
660
661 do {
662 ROUND512(a,b,c,d,e,f,g,h);
663 ROUND512(h,a,b,c,d,e,f,g);
664 ROUND512(g,h,a,b,c,d,e,f);
665 ROUND512(f,g,h,a,b,c,d,e);
666 ROUND512(e,f,g,h,a,b,c,d);
667 ROUND512(d,e,f,g,h,a,b,c);
668 ROUND512(c,d,e,f,g,h,a,b);
669 ROUND512(b,c,d,e,f,g,h,a);
670 } while (j < 80);
671
672
673 context->state[0] += a;
674 context->state[1] += b;
675 context->state[2] += c;
676 context->state[3] += d;
677 context->state[4] += e;
678 context->state[5] += f;
679 context->state[6] += g;
680 context->state[7] += h;
681
682
683 a = b = c = d = e = f = g = h = T1 = 0;
684 }
685
686 #else
687
688 void SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {
689 sha2_word64 a, b, c, d, e, f, g, h, s0, s1;
690 sha2_word64 T1, T2, *W512 = (sha2_word64*)context->buffer;
691 int j;
692
693
694 a = context->state[0];
695 b = context->state[1];
696 c = context->state[2];
697 d = context->state[3];
698 e = context->state[4];
699 f = context->state[5];
700 g = context->state[6];
701 h = context->state[7];
702
703 j = 0;
704 do {
705 #ifndef WORDS_BIGENDIAN
706
707 REVERSE64(*data++, W512[j]);
708
709 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];
710 #else
711
712 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);
713 #endif
714 T2 = Sigma0_512(a) + Maj(a, b, c);
715 h = g;
716 g = f;
717 f = e;
718 e = d + T1;
719 d = c;
720 c = b;
721 b = a;
722 a = T1 + T2;
723
724 j++;
725 } while (j < 16);
726
727 do {
728
729 s0 = W512[(j+1)&0x0f];
730 s0 = sigma0_512(s0);
731 s1 = W512[(j+14)&0x0f];
732 s1 = sigma1_512(s1);
733
734
735 T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +
736 (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);
737 T2 = Sigma0_512(a) + Maj(a, b, c);
738 h = g;
739 g = f;
740 f = e;
741 e = d + T1;
742 d = c;
743 c = b;
744 b = a;
745 a = T1 + T2;
746
747 j++;
748 } while (j < 80);
749
750
751 context->state[0] += a;
752 context->state[1] += b;
753 context->state[2] += c;
754 context->state[3] += d;
755 context->state[4] += e;
756 context->state[5] += f;
757 context->state[6] += g;
758 context->state[7] += h;
759
760
761 a = b = c = d = e = f = g = h = T1 = T2 = 0;
762 }
763
764 #endif
765
766 void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {
767 unsigned int freespace, usedspace;
768
769 if (len == 0) {
770
771 return;
772 }
773
774
775 assert(context != NULL && data != NULL);
776
777 usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
778 if (usedspace > 0) {
779
780 freespace = SHA512_BLOCK_LENGTH - usedspace;
781
782 if (len >= freespace) {
783
784 MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);
785 ADDINC128(context->bitcount, freespace << 3);
786 len -= freespace;
787 data += freespace;
788 SHA512_Transform(context, (const sha2_word64*)context->buffer);
789 } else {
790
791 MEMCPY_BCOPY(&context->buffer[usedspace], data, len);
792 ADDINC128(context->bitcount, len << 3);
793
794 usedspace = freespace = 0;
795 return;
796 }
797 }
798 while (len >= SHA512_BLOCK_LENGTH) {
799
800 SHA512_Transform(context, (const sha2_word64*)data);
801 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
802 len -= SHA512_BLOCK_LENGTH;
803 data += SHA512_BLOCK_LENGTH;
804 }
805 if (len > 0) {
806
807 MEMCPY_BCOPY(context->buffer, data, len);
808 ADDINC128(context->bitcount, len << 3);
809 }
810
811 usedspace = freespace = 0;
812 }
813
814 void SHA512_Last(SHA512_CTX* context) {
815 unsigned int usedspace;
816
817 usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH;
818 #ifndef WORDS_BIGENDIAN
819
820 REVERSE64(context->bitcount[0],context->bitcount[0]);
821 REVERSE64(context->bitcount[1],context->bitcount[1]);
822 #endif
823 if (usedspace > 0) {
824
825 context->buffer[usedspace++] = 0x80;
826
827 if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {
828
829 MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);
830 } else {
831 if (usedspace < SHA512_BLOCK_LENGTH) {
832 MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);
833 }
834
835 SHA512_Transform(context, (const sha2_word64*)context->buffer);
836
837
838 MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);
839 }
840 } else {
841
842 MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);
843
844
845 *context->buffer = 0x80;
846 }
847
848 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
849 *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
850
851
852 SHA512_Transform(context, (const sha2_word64*)context->buffer);
853 }
854
855 void SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {
856 sha2_word64 *d = (sha2_word64*)digest;
857
858
859 assert(context != NULL);
860
861
862 if (digest != (sha2_byte*)0) {
863 SHA512_Last(context);
864
865
866 #ifndef WORDS_BIGENDIAN
867 {
868
869 int j;
870 for (j = 0; j < 8; j++) {
871 REVERSE64(context->state[j],context->state[j]);
872 *d++ = context->state[j];
873 }
874 }
875 #else
876 MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);
877 #endif
878 }
879
880
881 MEMSET_BZERO(context, sizeof(SHA512_CTX));
882 }
883
884 int SHA512_Equal(SHA512_CTX* pctx1, SHA512_CTX* pctx2) {
885 return memcmp(pctx1->bitcount, pctx2->bitcount, sizeof(pctx1->bitcount)) == 0
886 && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
887 && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
888 }
889
890
891 void SHA384_Init(SHA384_CTX* context) {
892 if (context == (SHA384_CTX*)0) {
893 return;
894 }
895 MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);
896 MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);
897 context->bitcount[0] = context->bitcount[1] = 0;
898 }
899
900 void SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {
901 SHA512_Update((SHA512_CTX*)context, data, len);
902 }
903
904 void SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {
905 sha2_word64 *d = (sha2_word64*)digest;
906
907
908 assert(context != NULL);
909
910
911 if (digest != (sha2_byte*)0) {
912 SHA512_Last((SHA512_CTX*)context);
913
914
915 #ifndef WORDS_BIGENDIAN
916 {
917
918 int j;
919 for (j = 0; j < 6; j++) {
920 REVERSE64(context->state[j],context->state[j]);
921 *d++ = context->state[j];
922 }
923 }
924 #else
925 MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);
926 #endif
927 }
928
929
930 MEMSET_BZERO(context, sizeof(SHA384_CTX));
931 }
932
933 int SHA384_Equal(SHA384_CTX* pctx1, SHA384_CTX* pctx2) {
934 return memcmp(pctx1->bitcount, pctx2->bitcount, sizeof(pctx1->bitcount)) == 0
935 && memcmp(pctx1->state, pctx2->state, sizeof(pctx1->state)) == 0
936 && memcmp(pctx1->buffer, pctx2->buffer, sizeof(pctx1->buffer)) == 0;
937 }