summaryrefslogtreecommitdiff
path: root/openbsd-compat/sha2.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/sha2.c')
-rw-r--r--openbsd-compat/sha2.c334
1 files changed, 220 insertions, 114 deletions
diff --git a/openbsd-compat/sha2.c b/openbsd-compat/sha2.c
index b55ea30ac..e63324c99 100644
--- a/openbsd-compat/sha2.c
+++ b/openbsd-compat/sha2.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sha2.c,v 1.11 2005/08/08 08:05:35 espie Exp */ 1/* $OpenBSD: sha2.c,v 1.28 2019/07/23 12:35:22 dtucker Exp $ */
2 2
3/* 3/*
4 * FILE: sha2.c 4 * FILE: sha2.c
@@ -38,18 +38,14 @@
38 38
39#include "includes.h" 39#include "includes.h"
40 40
41#ifdef WITH_OPENSSL 41#if !defined(HAVE_SHA256UPDATE) || !defined(HAVE_SHA384UPDATE) || \
42# include <openssl/opensslv.h> 42 !defined(HAVE_SHA512UPDATE)
43# if !defined(HAVE_EVP_SHA256) && (OPENSSL_VERSION_NUMBER >= 0x00907000L)
44# define _NEED_SHA2 1
45# endif
46#else
47# define _NEED_SHA2 1
48#endif
49 43
50#if defined(_NEED_SHA2) && !defined(HAVE_SHA256_UPDATE) 44/* no-op out, similar to DEF_WEAK but only needed here */
45#define MAKE_CLONE(x, y) void __ssh_compat_make_clone_##x_##y(void);
51 46
52#include <string.h> 47#include <string.h>
48#include <sha2.h>
53 49
54/* 50/*
55 * UNROLLED TRANSFORM LOOP NOTE: 51 * UNROLLED TRANSFORM LOOP NOTE:
@@ -64,8 +60,13 @@
64 * #define SHA2_UNROLL_TRANSFORM 60 * #define SHA2_UNROLL_TRANSFORM
65 * 61 *
66 */ 62 */
63#ifndef SHA2_SMALL
64#if defined(__amd64__) || defined(__i386__)
65#define SHA2_UNROLL_TRANSFORM
66#endif
67#endif
67 68
68/*** SHA-256/384/512 Machine Architecture Definitions *****************/ 69/*** SHA-224/256/384/512 Machine Architecture Definitions *****************/
69/* 70/*
70 * BYTE_ORDER NOTE: 71 * BYTE_ORDER NOTE:
71 * 72 *
@@ -98,8 +99,9 @@
98#endif 99#endif
99 100
100 101
101/*** SHA-256/384/512 Various Length Definitions ***********************/ 102/*** SHA-224/256/384/512 Various Length Definitions ***********************/
102/* NOTE: Most of these are in sha2.h */ 103/* NOTE: Most of these are in sha2.h */
104#define SHA224_SHORT_BLOCK_LENGTH (SHA224_BLOCK_LENGTH - 8)
103#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8) 105#define SHA256_SHORT_BLOCK_LENGTH (SHA256_BLOCK_LENGTH - 8)
104#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16) 106#define SHA384_SHORT_BLOCK_LENGTH (SHA384_BLOCK_LENGTH - 16)
105#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) 107#define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16)
@@ -152,22 +154,22 @@
152 * Bit shifting and rotation (used by the six SHA-XYZ logical functions: 154 * Bit shifting and rotation (used by the six SHA-XYZ logical functions:
153 * 155 *
154 * NOTE: The naming of R and S appears backwards here (R is a SHIFT and 156 * NOTE: The naming of R and S appears backwards here (R is a SHIFT and
155 * S is a ROTATION) because the SHA-256/384/512 description document 157 * S is a ROTATION) because the SHA-224/256/384/512 description document
156 * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this 158 * (see http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf) uses this
157 * same "backwards" definition. 159 * same "backwards" definition.
158 */ 160 */
159/* Shift-right (used in SHA-256, SHA-384, and SHA-512): */ 161/* Shift-right (used in SHA-224, SHA-256, SHA-384, and SHA-512): */
160#define R(b,x) ((x) >> (b)) 162#define R(b,x) ((x) >> (b))
161/* 32-bit Rotate-right (used in SHA-256): */ 163/* 32-bit Rotate-right (used in SHA-224 and SHA-256): */
162#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b)))) 164#define S32(b,x) (((x) >> (b)) | ((x) << (32 - (b))))
163/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */ 165/* 64-bit Rotate-right (used in SHA-384 and SHA-512): */
164#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) 166#define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b))))
165 167
166/* Two of six logical functions used in SHA-256, SHA-384, and SHA-512: */ 168/* Two of six logical functions used in SHA-224, SHA-256, SHA-384, and SHA-512: */
167#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) 169#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
168#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 170#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
169 171
170/* Four of six logical functions used in SHA-256: */ 172/* Four of six logical functions used in SHA-224 and SHA-256: */
171#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x))) 173#define Sigma0_256(x) (S32(2, (x)) ^ S32(13, (x)) ^ S32(22, (x)))
172#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x))) 174#define Sigma1_256(x) (S32(6, (x)) ^ S32(11, (x)) ^ S32(25, (x)))
173#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x))) 175#define sigma0_256(x) (S32(7, (x)) ^ S32(18, (x)) ^ R(3 , (x)))
@@ -181,8 +183,8 @@
181 183
182 184
183/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/ 185/*** SHA-XYZ INITIAL HASH VALUES AND CONSTANTS ************************/
184/* Hash constant words K for SHA-256: */ 186/* Hash constant words K for SHA-224 and SHA-256: */
185const static u_int32_t K256[64] = { 187static const u_int32_t K256[64] = {
186 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 188 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL,
187 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 189 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL,
188 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 190 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL,
@@ -202,7 +204,7 @@ const static u_int32_t K256[64] = {
202}; 204};
203 205
204/* Initial hash value H for SHA-256: */ 206/* Initial hash value H for SHA-256: */
205const static u_int32_t sha256_initial_hash_value[8] = { 207static const u_int32_t sha256_initial_hash_value[8] = {
206 0x6a09e667UL, 208 0x6a09e667UL,
207 0xbb67ae85UL, 209 0xbb67ae85UL,
208 0x3c6ef372UL, 210 0x3c6ef372UL,
@@ -214,7 +216,7 @@ const static u_int32_t sha256_initial_hash_value[8] = {
214}; 216};
215 217
216/* Hash constant words K for SHA-384 and SHA-512: */ 218/* Hash constant words K for SHA-384 and SHA-512: */
217const static u_int64_t K512[80] = { 219static const u_int64_t K512[80] = {
218 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 220 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
219 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 221 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
220 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 222 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
@@ -257,8 +259,35 @@ const static u_int64_t K512[80] = {
257 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL 259 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
258}; 260};
259 261
262/* Initial hash value H for SHA-512 */
263static const u_int64_t sha512_initial_hash_value[8] = {
264 0x6a09e667f3bcc908ULL,
265 0xbb67ae8584caa73bULL,
266 0x3c6ef372fe94f82bULL,
267 0xa54ff53a5f1d36f1ULL,
268 0x510e527fade682d1ULL,
269 0x9b05688c2b3e6c1fULL,
270 0x1f83d9abfb41bd6bULL,
271 0x5be0cd19137e2179ULL
272};
273
274#if !defined(SHA2_SMALL)
275#if 0
276/* Initial hash value H for SHA-224: */
277static const u_int32_t sha224_initial_hash_value[8] = {
278 0xc1059ed8UL,
279 0x367cd507UL,
280 0x3070dd17UL,
281 0xf70e5939UL,
282 0xffc00b31UL,
283 0x68581511UL,
284 0x64f98fa7UL,
285 0xbefa4fa4UL
286};
287#endif /* 0 */
288
260/* Initial hash value H for SHA-384 */ 289/* Initial hash value H for SHA-384 */
261const static u_int64_t sha384_initial_hash_value[8] = { 290static const u_int64_t sha384_initial_hash_value[8] = {
262 0xcbbb9d5dc1059ed8ULL, 291 0xcbbb9d5dc1059ed8ULL,
263 0x629a292a367cd507ULL, 292 0x629a292a367cd507ULL,
264 0x9159015a3070dd17ULL, 293 0x9159015a3070dd17ULL,
@@ -269,30 +298,67 @@ const static u_int64_t sha384_initial_hash_value[8] = {
269 0x47b5481dbefa4fa4ULL 298 0x47b5481dbefa4fa4ULL
270}; 299};
271 300
272/* Initial hash value H for SHA-512 */ 301#if 0
273const static u_int64_t sha512_initial_hash_value[8] = { 302/* Initial hash value H for SHA-512-256 */
274 0x6a09e667f3bcc908ULL, 303static const u_int64_t sha512_256_initial_hash_value[8] = {
275 0xbb67ae8584caa73bULL, 304 0x22312194fc2bf72cULL,
276 0x3c6ef372fe94f82bULL, 305 0x9f555fa3c84c64c2ULL,
277 0xa54ff53a5f1d36f1ULL, 306 0x2393b86b6f53b151ULL,
278 0x510e527fade682d1ULL, 307 0x963877195940eabdULL,
279 0x9b05688c2b3e6c1fULL, 308 0x96283ee2a88effe3ULL,
280 0x1f83d9abfb41bd6bULL, 309 0xbe5e1e2553863992ULL,
281 0x5be0cd19137e2179ULL 310 0x2b0199fc2c85b8aaULL,
311 0x0eb72ddc81c52ca2ULL
282}; 312};
283 313
314/*** SHA-224: *********************************************************/
315void
316SHA224Init(SHA2_CTX *context)
317{
318 memcpy(context->state.st32, sha224_initial_hash_value,
319 sizeof(sha224_initial_hash_value));
320 memset(context->buffer, 0, sizeof(context->buffer));
321 context->bitcount[0] = 0;
322}
323DEF_WEAK(SHA224Init);
324
325MAKE_CLONE(SHA224Transform, SHA256Transform);
326MAKE_CLONE(SHA224Update, SHA256Update);
327MAKE_CLONE(SHA224Pad, SHA256Pad);
328DEF_WEAK(SHA224Transform);
329DEF_WEAK(SHA224Update);
330DEF_WEAK(SHA224Pad);
331
332void
333SHA224Final(u_int8_t digest[SHA224_DIGEST_LENGTH], SHA2_CTX *context)
334{
335 SHA224Pad(context);
336
337#if BYTE_ORDER == LITTLE_ENDIAN
338 int i;
339
340 /* Convert TO host byte order */
341 for (i = 0; i < 7; i++)
342 BE_32_TO_8(digest + i * 4, context->state.st32[i]);
343#else
344 memcpy(digest, context->state.st32, SHA224_DIGEST_LENGTH);
345#endif
346 explicit_bzero(context, sizeof(*context));
347}
348DEF_WEAK(SHA224Final);
349#endif /* !defined(SHA2_SMALL) */
350#endif /* 0 */
284 351
285/*** SHA-256: *********************************************************/ 352/*** SHA-256: *********************************************************/
286void 353void
287SHA256_Init(SHA256_CTX *context) 354SHA256Init(SHA2_CTX *context)
288{ 355{
289 if (context == NULL) 356 memcpy(context->state.st32, sha256_initial_hash_value,
290 return;
291 memcpy(context->state, sha256_initial_hash_value,
292 sizeof(sha256_initial_hash_value)); 357 sizeof(sha256_initial_hash_value));
293 memset(context->buffer, 0, sizeof(context->buffer)); 358 memset(context->buffer, 0, sizeof(context->buffer));
294 context->bitcount = 0; 359 context->bitcount[0] = 0;
295} 360}
361DEF_WEAK(SHA256Init);
296 362
297#ifdef SHA2_UNROLL_TRANSFORM 363#ifdef SHA2_UNROLL_TRANSFORM
298 364
@@ -320,7 +386,7 @@ SHA256_Init(SHA256_CTX *context)
320} while(0) 386} while(0)
321 387
322void 388void
323SHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH]) 389SHA256Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH])
324{ 390{
325 u_int32_t a, b, c, d, e, f, g, h, s0, s1; 391 u_int32_t a, b, c, d, e, f, g, h, s0, s1;
326 u_int32_t T1, W256[16]; 392 u_int32_t T1, W256[16];
@@ -378,7 +444,7 @@ SHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH])
378#else /* SHA2_UNROLL_TRANSFORM */ 444#else /* SHA2_UNROLL_TRANSFORM */
379 445
380void 446void
381SHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH]) 447SHA256Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH])
382{ 448{
383 u_int32_t a, b, c, d, e, f, g, h, s0, s1; 449 u_int32_t a, b, c, d, e, f, g, h, s0, s1;
384 u_int32_t T1, T2, W256[16]; 450 u_int32_t T1, T2, W256[16];
@@ -451,17 +517,18 @@ SHA256_Transform(u_int32_t state[8], const u_int8_t data[SHA256_BLOCK_LENGTH])
451} 517}
452 518
453#endif /* SHA2_UNROLL_TRANSFORM */ 519#endif /* SHA2_UNROLL_TRANSFORM */
520DEF_WEAK(SHA256Transform);
454 521
455void 522void
456SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len) 523SHA256Update(SHA2_CTX *context, const u_int8_t *data, size_t len)
457{ 524{
458 size_t freespace, usedspace; 525 u_int64_t freespace, usedspace;
459 526
460 /* Calling with no data is valid (we do nothing) */ 527 /* Calling with no data is valid (we do nothing) */
461 if (len == 0) 528 if (len == 0)
462 return; 529 return;
463 530
464 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; 531 usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
465 if (usedspace > 0) { 532 if (usedspace > 0) {
466 /* Calculate how much free space is available in the buffer */ 533 /* Calculate how much free space is available in the buffer */
467 freespace = SHA256_BLOCK_LENGTH - usedspace; 534 freespace = SHA256_BLOCK_LENGTH - usedspace;
@@ -469,14 +536,14 @@ SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len)
469 if (len >= freespace) { 536 if (len >= freespace) {
470 /* Fill the buffer completely and process it */ 537 /* Fill the buffer completely and process it */
471 memcpy(&context->buffer[usedspace], data, freespace); 538 memcpy(&context->buffer[usedspace], data, freespace);
472 context->bitcount += freespace << 3; 539 context->bitcount[0] += freespace << 3;
473 len -= freespace; 540 len -= freespace;
474 data += freespace; 541 data += freespace;
475 SHA256_Transform(context->state, context->buffer); 542 SHA256Transform(context->state.st32, context->buffer);
476 } else { 543 } else {
477 /* The buffer is not yet full */ 544 /* The buffer is not yet full */
478 memcpy(&context->buffer[usedspace], data, len); 545 memcpy(&context->buffer[usedspace], data, len);
479 context->bitcount += len << 3; 546 context->bitcount[0] += (u_int64_t)len << 3;
480 /* Clean up: */ 547 /* Clean up: */
481 usedspace = freespace = 0; 548 usedspace = freespace = 0;
482 return; 549 return;
@@ -484,26 +551,27 @@ SHA256_Update(SHA256_CTX *context, const u_int8_t *data, size_t len)
484 } 551 }
485 while (len >= SHA256_BLOCK_LENGTH) { 552 while (len >= SHA256_BLOCK_LENGTH) {
486 /* Process as many complete blocks as we can */ 553 /* Process as many complete blocks as we can */
487 SHA256_Transform(context->state, data); 554 SHA256Transform(context->state.st32, data);
488 context->bitcount += SHA256_BLOCK_LENGTH << 3; 555 context->bitcount[0] += SHA256_BLOCK_LENGTH << 3;
489 len -= SHA256_BLOCK_LENGTH; 556 len -= SHA256_BLOCK_LENGTH;
490 data += SHA256_BLOCK_LENGTH; 557 data += SHA256_BLOCK_LENGTH;
491 } 558 }
492 if (len > 0) { 559 if (len > 0) {
493 /* There's left-overs, so save 'em */ 560 /* There's left-overs, so save 'em */
494 memcpy(context->buffer, data, len); 561 memcpy(context->buffer, data, len);
495 context->bitcount += len << 3; 562 context->bitcount[0] += len << 3;
496 } 563 }
497 /* Clean up: */ 564 /* Clean up: */
498 usedspace = freespace = 0; 565 usedspace = freespace = 0;
499} 566}
567DEF_WEAK(SHA256Update);
500 568
501void 569void
502SHA256_Pad(SHA256_CTX *context) 570SHA256Pad(SHA2_CTX *context)
503{ 571{
504 unsigned int usedspace; 572 unsigned int usedspace;
505 573
506 usedspace = (context->bitcount >> 3) % SHA256_BLOCK_LENGTH; 574 usedspace = (context->bitcount[0] >> 3) % SHA256_BLOCK_LENGTH;
507 if (usedspace > 0) { 575 if (usedspace > 0) {
508 /* Begin padding with a 1 bit: */ 576 /* Begin padding with a 1 bit: */
509 context->buffer[usedspace++] = 0x80; 577 context->buffer[usedspace++] = 0x80;
@@ -518,7 +586,7 @@ SHA256_Pad(SHA256_CTX *context)
518 SHA256_BLOCK_LENGTH - usedspace); 586 SHA256_BLOCK_LENGTH - usedspace);
519 } 587 }
520 /* Do second-to-last transform: */ 588 /* Do second-to-last transform: */
521 SHA256_Transform(context->state, context->buffer); 589 SHA256Transform(context->state.st32, context->buffer);
522 590
523 /* Prepare for last transform: */ 591 /* Prepare for last transform: */
524 memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH); 592 memset(context->buffer, 0, SHA256_SHORT_BLOCK_LENGTH);
@@ -532,47 +600,45 @@ SHA256_Pad(SHA256_CTX *context)
532 } 600 }
533 /* Store the length of input data (in bits) in big endian format: */ 601 /* Store the length of input data (in bits) in big endian format: */
534 BE_64_TO_8(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], 602 BE_64_TO_8(&context->buffer[SHA256_SHORT_BLOCK_LENGTH],
535 context->bitcount); 603 context->bitcount[0]);
536 604
537 /* Final transform: */ 605 /* Final transform: */
538 SHA256_Transform(context->state, context->buffer); 606 SHA256Transform(context->state.st32, context->buffer);
539 607
540 /* Clean up: */ 608 /* Clean up: */
541 usedspace = 0; 609 usedspace = 0;
542} 610}
611DEF_WEAK(SHA256Pad);
543 612
544void 613void
545SHA256_Final(u_int8_t digest[SHA256_DIGEST_LENGTH], SHA256_CTX *context) 614SHA256Final(u_int8_t digest[SHA256_DIGEST_LENGTH], SHA2_CTX *context)
546{ 615{
547 SHA256_Pad(context); 616 SHA256Pad(context);
548 617
549 /* If no digest buffer is passed, we don't bother doing this: */
550 if (digest != NULL) {
551#if BYTE_ORDER == LITTLE_ENDIAN 618#if BYTE_ORDER == LITTLE_ENDIAN
552 int i; 619 int i;
553 620
554 /* Convert TO host byte order */ 621 /* Convert TO host byte order */
555 for (i = 0; i < 8; i++) 622 for (i = 0; i < 8; i++)
556 BE_32_TO_8(digest + i * 4, context->state[i]); 623 BE_32_TO_8(digest + i * 4, context->state.st32[i]);
557#else 624#else
558 memcpy(digest, context->state, SHA256_DIGEST_LENGTH); 625 memcpy(digest, context->state.st32, SHA256_DIGEST_LENGTH);
559#endif 626#endif
560 memset(context, 0, sizeof(*context)); 627 explicit_bzero(context, sizeof(*context));
561 }
562} 628}
629DEF_WEAK(SHA256Final);
563 630
564 631
565/*** SHA-512: *********************************************************/ 632/*** SHA-512: *********************************************************/
566void 633void
567SHA512_Init(SHA512_CTX *context) 634SHA512Init(SHA2_CTX *context)
568{ 635{
569 if (context == NULL) 636 memcpy(context->state.st64, sha512_initial_hash_value,
570 return;
571 memcpy(context->state, sha512_initial_hash_value,
572 sizeof(sha512_initial_hash_value)); 637 sizeof(sha512_initial_hash_value));
573 memset(context->buffer, 0, sizeof(context->buffer)); 638 memset(context->buffer, 0, sizeof(context->buffer));
574 context->bitcount[0] = context->bitcount[1] = 0; 639 context->bitcount[0] = context->bitcount[1] = 0;
575} 640}
641DEF_WEAK(SHA512Init);
576 642
577#ifdef SHA2_UNROLL_TRANSFORM 643#ifdef SHA2_UNROLL_TRANSFORM
578 644
@@ -601,7 +667,7 @@ SHA512_Init(SHA512_CTX *context)
601} while(0) 667} while(0)
602 668
603void 669void
604SHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) 670SHA512Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
605{ 671{
606 u_int64_t a, b, c, d, e, f, g, h, s0, s1; 672 u_int64_t a, b, c, d, e, f, g, h, s0, s1;
607 u_int64_t T1, W512[16]; 673 u_int64_t T1, W512[16];
@@ -659,7 +725,7 @@ SHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
659#else /* SHA2_UNROLL_TRANSFORM */ 725#else /* SHA2_UNROLL_TRANSFORM */
660 726
661void 727void
662SHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) 728SHA512Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
663{ 729{
664 u_int64_t a, b, c, d, e, f, g, h, s0, s1; 730 u_int64_t a, b, c, d, e, f, g, h, s0, s1;
665 u_int64_t T1, T2, W512[16]; 731 u_int64_t T1, T2, W512[16];
@@ -732,9 +798,10 @@ SHA512_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
732} 798}
733 799
734#endif /* SHA2_UNROLL_TRANSFORM */ 800#endif /* SHA2_UNROLL_TRANSFORM */
801DEF_WEAK(SHA512Transform);
735 802
736void 803void
737SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) 804SHA512Update(SHA2_CTX *context, const u_int8_t *data, size_t len)
738{ 805{
739 size_t freespace, usedspace; 806 size_t freespace, usedspace;
740 807
@@ -753,7 +820,7 @@ SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len)
753 ADDINC128(context->bitcount, freespace << 3); 820 ADDINC128(context->bitcount, freespace << 3);
754 len -= freespace; 821 len -= freespace;
755 data += freespace; 822 data += freespace;
756 SHA512_Transform(context->state, context->buffer); 823 SHA512Transform(context->state.st64, context->buffer);
757 } else { 824 } else {
758 /* The buffer is not yet full */ 825 /* The buffer is not yet full */
759 memcpy(&context->buffer[usedspace], data, len); 826 memcpy(&context->buffer[usedspace], data, len);
@@ -765,7 +832,7 @@ SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len)
765 } 832 }
766 while (len >= SHA512_BLOCK_LENGTH) { 833 while (len >= SHA512_BLOCK_LENGTH) {
767 /* Process as many complete blocks as we can */ 834 /* Process as many complete blocks as we can */
768 SHA512_Transform(context->state, data); 835 SHA512Transform(context->state.st64, data);
769 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); 836 ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);
770 len -= SHA512_BLOCK_LENGTH; 837 len -= SHA512_BLOCK_LENGTH;
771 data += SHA512_BLOCK_LENGTH; 838 data += SHA512_BLOCK_LENGTH;
@@ -778,9 +845,10 @@ SHA512_Update(SHA512_CTX *context, const u_int8_t *data, size_t len)
778 /* Clean up: */ 845 /* Clean up: */
779 usedspace = freespace = 0; 846 usedspace = freespace = 0;
780} 847}
848DEF_WEAK(SHA512Update);
781 849
782void 850void
783SHA512_Pad(SHA512_CTX *context) 851SHA512Pad(SHA2_CTX *context)
784{ 852{
785 unsigned int usedspace; 853 unsigned int usedspace;
786 854
@@ -797,7 +865,7 @@ SHA512_Pad(SHA512_CTX *context)
797 memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace); 865 memset(&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace);
798 } 866 }
799 /* Do second-to-last transform: */ 867 /* Do second-to-last transform: */
800 SHA512_Transform(context->state, context->buffer); 868 SHA512Transform(context->state.st64, context->buffer);
801 869
802 /* And set-up for the last transform: */ 870 /* And set-up for the last transform: */
803 memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2); 871 memset(context->buffer, 0, SHA512_BLOCK_LENGTH - 2);
@@ -816,89 +884,127 @@ SHA512_Pad(SHA512_CTX *context)
816 context->bitcount[0]); 884 context->bitcount[0]);
817 885
818 /* Final transform: */ 886 /* Final transform: */
819 SHA512_Transform(context->state, context->buffer); 887 SHA512Transform(context->state.st64, context->buffer);
820 888
821 /* Clean up: */ 889 /* Clean up: */
822 usedspace = 0; 890 usedspace = 0;
823} 891}
892DEF_WEAK(SHA512Pad);
824 893
825void 894void
826SHA512_Final(u_int8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context) 895SHA512Final(u_int8_t digest[SHA512_DIGEST_LENGTH], SHA2_CTX *context)
827{ 896{
828 SHA512_Pad(context); 897 SHA512Pad(context);
829 898
830 /* If no digest buffer is passed, we don't bother doing this: */
831 if (digest != NULL) {
832#if BYTE_ORDER == LITTLE_ENDIAN 899#if BYTE_ORDER == LITTLE_ENDIAN
833 int i; 900 int i;
834 901
835 /* Convert TO host byte order */ 902 /* Convert TO host byte order */
836 for (i = 0; i < 8; i++) 903 for (i = 0; i < 8; i++)
837 BE_64_TO_8(digest + i * 8, context->state[i]); 904 BE_64_TO_8(digest + i * 8, context->state.st64[i]);
838#else 905#else
839 memcpy(digest, context->state, SHA512_DIGEST_LENGTH); 906 memcpy(digest, context->state.st64, SHA512_DIGEST_LENGTH);
840#endif 907#endif
841 memset(context, 0, sizeof(*context)); 908 explicit_bzero(context, sizeof(*context));
842 }
843} 909}
910DEF_WEAK(SHA512Final);
844 911
912#if !defined(SHA2_SMALL)
845 913
846/*** SHA-384: *********************************************************/ 914/*** SHA-384: *********************************************************/
847void 915void
848SHA384_Init(SHA384_CTX *context) 916SHA384Init(SHA2_CTX *context)
849{ 917{
850 if (context == NULL) 918 memcpy(context->state.st64, sha384_initial_hash_value,
851 return;
852 memcpy(context->state, sha384_initial_hash_value,
853 sizeof(sha384_initial_hash_value)); 919 sizeof(sha384_initial_hash_value));
854 memset(context->buffer, 0, sizeof(context->buffer)); 920 memset(context->buffer, 0, sizeof(context->buffer));
855 context->bitcount[0] = context->bitcount[1] = 0; 921 context->bitcount[0] = context->bitcount[1] = 0;
856} 922}
923DEF_WEAK(SHA384Init);
857 924
858#if 0 925MAKE_CLONE(SHA384Transform, SHA512Transform);
859__weak_alias(SHA384_Transform, SHA512_Transform); 926MAKE_CLONE(SHA384Update, SHA512Update);
860__weak_alias(SHA384_Update, SHA512_Update); 927MAKE_CLONE(SHA384Pad, SHA512Pad);
861__weak_alias(SHA384_Pad, SHA512_Pad); 928DEF_WEAK(SHA384Transform);
862#endif 929DEF_WEAK(SHA384Update);
930DEF_WEAK(SHA384Pad);
863 931
932/* Equivalent of MAKE_CLONE (which is a no-op) for SHA384 funcs */
864void 933void
865SHA384_Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH]) 934SHA384Transform(u_int64_t state[8], const u_int8_t data[SHA512_BLOCK_LENGTH])
866{ 935{
867 return SHA512_Transform(state, data); 936 SHA512Transform(state, data);
868} 937}
869 938
870void 939void
871SHA384_Update(SHA512_CTX *context, const u_int8_t *data, size_t len) 940SHA384Update(SHA2_CTX *context, const u_int8_t *data, size_t len)
872{ 941{
873 SHA512_Update(context, data, len); 942 SHA512Update(context, data, len);
874} 943}
875 944
876void 945void
877SHA384_Pad(SHA512_CTX *context) 946SHA384Pad(SHA2_CTX *context)
878{ 947{
879 SHA512_Pad(context); 948 SHA512Pad(context);
880} 949}
881 950
882void 951void
883SHA384_Final(u_int8_t digest[SHA384_DIGEST_LENGTH], SHA384_CTX *context) 952SHA384Final(u_int8_t digest[SHA384_DIGEST_LENGTH], SHA2_CTX *context)
884{ 953{
885 SHA384_Pad(context); 954 SHA384Pad(context);
886 955
887 /* If no digest buffer is passed, we don't bother doing this: */
888 if (digest != NULL) {
889#if BYTE_ORDER == LITTLE_ENDIAN 956#if BYTE_ORDER == LITTLE_ENDIAN
890 int i; 957 int i;
891 958
892 /* Convert TO host byte order */ 959 /* Convert TO host byte order */
893 for (i = 0; i < 6; i++) 960 for (i = 0; i < 6; i++)
894 BE_64_TO_8(digest + i * 8, context->state[i]); 961 BE_64_TO_8(digest + i * 8, context->state.st64[i]);
895#else 962#else
896 memcpy(digest, context->state, SHA384_DIGEST_LENGTH); 963 memcpy(digest, context->state.st64, SHA384_DIGEST_LENGTH);
897#endif 964#endif
898 } 965 /* Zero out state data */
966 explicit_bzero(context, sizeof(*context));
967}
968DEF_WEAK(SHA384Final);
969
970#if 0
971/*** SHA-512/256: *********************************************************/
972void
973SHA512_256Init(SHA2_CTX *context)
974{
975 memcpy(context->state.st64, sha512_256_initial_hash_value,
976 sizeof(sha512_256_initial_hash_value));
977 memset(context->buffer, 0, sizeof(context->buffer));
978 context->bitcount[0] = context->bitcount[1] = 0;
979}
980DEF_WEAK(SHA512_256Init);
899 981
982MAKE_CLONE(SHA512_256Transform, SHA512Transform);
983MAKE_CLONE(SHA512_256Update, SHA512Update);
984MAKE_CLONE(SHA512_256Pad, SHA512Pad);
985DEF_WEAK(SHA512_256Transform);
986DEF_WEAK(SHA512_256Update);
987DEF_WEAK(SHA512_256Pad);
988
989void
990SHA512_256Final(u_int8_t digest[SHA512_256_DIGEST_LENGTH], SHA2_CTX *context)
991{
992 SHA512_256Pad(context);
993
994#if BYTE_ORDER == LITTLE_ENDIAN
995 int i;
996
997 /* Convert TO host byte order */
998 for (i = 0; i < 4; i++)
999 BE_64_TO_8(digest + i * 8, context->state.st64[i]);
1000#else
1001 memcpy(digest, context->state.st64, SHA512_256_DIGEST_LENGTH);
1002#endif
900 /* Zero out state data */ 1003 /* Zero out state data */
901 memset(context, 0, sizeof(*context)); 1004 explicit_bzero(context, sizeof(*context));
902} 1005}
1006DEF_WEAK(SHA512_256Final);
1007#endif /* !defined(SHA2_SMALL) */
1008#endif /* 0 */
903 1009
904#endif /* defined(_NEED_SHA2) && !defined(HAVE_SHA256_UPDATE) */ 1010#endif /* HAVE_SHA{256,384,512}UPDATE */