From ca46c7c591f7c118fedd04f6883867882ca331b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Fri, 11 Jul 2014 00:12:33 +0000 Subject: [PATCH] Fully separate the SHA-384 and SHA-512 code. --- lib/digest/sha384.c | 44 ++++++++++---------------------------------- lib/digest/sha512.c | 45 ++++++++++++--------------------------------- 2 files changed, 22 insertions(+), 67 deletions(-) diff --git a/lib/digest/sha384.c b/lib/digest/sha384.c index b51359a..208b7ac 100644 --- a/lib/digest/sha384.c +++ b/lib/digest/sha384.c @@ -41,8 +41,6 @@ #include -static int is384 = 1; - /* * 64-bit integer manipulation macros (big endian) */ @@ -132,30 +130,14 @@ void sha384_init( sha384_ctx *ctx ) ctx->total[0] = 0; ctx->total[1] = 0; - if( is384 == 0 ) - { - /* SHA-384 */ - ctx->state[0] = UL64(0x6A09E667F3BCC908); - ctx->state[1] = UL64(0xBB67AE8584CAA73B); - ctx->state[2] = UL64(0x3C6EF372FE94F82B); - ctx->state[3] = UL64(0xA54FF53A5F1D36F1); - ctx->state[4] = UL64(0x510E527FADE682D1); - ctx->state[5] = UL64(0x9B05688C2B3E6C1F); - ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); - ctx->state[7] = UL64(0x5BE0CD19137E2179); - } - else - { - /* SHA-384 */ - ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); - ctx->state[1] = UL64(0x629A292A367CD507); - ctx->state[2] = UL64(0x9159015A3070DD17); - ctx->state[3] = UL64(0x152FECD8F70E5939); - ctx->state[4] = UL64(0x67332667FFC00B31); - ctx->state[5] = UL64(0x8EB44A8768581511); - ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); - ctx->state[7] = UL64(0x47B5481DBEFA4FA4); - } + ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); + ctx->state[1] = UL64(0x629A292A367CD507); + ctx->state[2] = UL64(0x9159015A3070DD17); + ctx->state[3] = UL64(0x152FECD8F70E5939); + ctx->state[4] = UL64(0x67332667FFC00B31); + ctx->state[5] = UL64(0x8EB44A8768581511); + ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); + ctx->state[7] = UL64(0x47B5481DBEFA4FA4); } static void sha384_process( sha384_ctx *ctx, const unsigned char *data ) @@ -310,12 +292,6 @@ void sha384_final( sha384_ctx *ctx, unsigned char output[64] ) PUT_UINT64_BE( ctx->state[3], output, 24 ); PUT_UINT64_BE( ctx->state[4], output, 32 ); PUT_UINT64_BE( ctx->state[5], output, 40 ); - - if( is384 == 0 ) - { - PUT_UINT64_BE( ctx->state[6], output, 48 ); - PUT_UINT64_BE( ctx->state[7], output, 56 ); - } } /* @@ -344,7 +320,7 @@ void sha384_hmac_init( sha384_ctx *ctx, unsigned char *key, int keylen ) if( keylen > 128 ) { sha384_complete( key, keylen, sum ); - keylen = ( is384 ) ? 48 : 64; + keylen = SHA384_DIGEST_LEN; key = sum; } @@ -380,7 +356,7 @@ void sha384_hmac_final( sha384_ctx *ctx, unsigned char output[64] ) int hlen; unsigned char tmpbuf[64]; - hlen = ( is384 == 0 ) ? 64 : 48; + hlen = SHA384_DIGEST_LEN; sha384_final( ctx, tmpbuf ); sha384_init( ctx ); diff --git a/lib/digest/sha512.c b/lib/digest/sha512.c index 7d49416..beac0b2 100644 --- a/lib/digest/sha512.c +++ b/lib/digest/sha512.c @@ -41,8 +41,6 @@ #include -static int is384 = 0; - /* * 64-bit integer manipulation macros (big endian) */ @@ -132,30 +130,14 @@ void sha512_init( sha512_ctx *ctx ) ctx->total[0] = 0; ctx->total[1] = 0; - if( is384 == 0 ) - { - /* SHA-512 */ - ctx->state[0] = UL64(0x6A09E667F3BCC908); - ctx->state[1] = UL64(0xBB67AE8584CAA73B); - ctx->state[2] = UL64(0x3C6EF372FE94F82B); - ctx->state[3] = UL64(0xA54FF53A5F1D36F1); - ctx->state[4] = UL64(0x510E527FADE682D1); - ctx->state[5] = UL64(0x9B05688C2B3E6C1F); - ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); - ctx->state[7] = UL64(0x5BE0CD19137E2179); - } - else - { - /* SHA-384 */ - ctx->state[0] = UL64(0xCBBB9D5DC1059ED8); - ctx->state[1] = UL64(0x629A292A367CD507); - ctx->state[2] = UL64(0x9159015A3070DD17); - ctx->state[3] = UL64(0x152FECD8F70E5939); - ctx->state[4] = UL64(0x67332667FFC00B31); - ctx->state[5] = UL64(0x8EB44A8768581511); - ctx->state[6] = UL64(0xDB0C2E0D64F98FA7); - ctx->state[7] = UL64(0x47B5481DBEFA4FA4); - } + ctx->state[0] = UL64(0x6A09E667F3BCC908); + ctx->state[1] = UL64(0xBB67AE8584CAA73B); + ctx->state[2] = UL64(0x3C6EF372FE94F82B); + ctx->state[3] = UL64(0xA54FF53A5F1D36F1); + ctx->state[4] = UL64(0x510E527FADE682D1); + ctx->state[5] = UL64(0x9B05688C2B3E6C1F); + ctx->state[6] = UL64(0x1F83D9ABFB41BD6B); + ctx->state[7] = UL64(0x5BE0CD19137E2179); } static void sha512_process( sha512_ctx *ctx, const unsigned char *data ) @@ -311,11 +293,8 @@ void sha512_final( sha512_ctx *ctx, unsigned char output[64] ) PUT_UINT64_BE( ctx->state[4], output, 32 ); PUT_UINT64_BE( ctx->state[5], output, 40 ); - if( is384 == 0 ) - { - PUT_UINT64_BE( ctx->state[6], output, 48 ); - PUT_UINT64_BE( ctx->state[7], output, 56 ); - } + PUT_UINT64_BE( ctx->state[6], output, 48 ); + PUT_UINT64_BE( ctx->state[7], output, 56 ); } /* @@ -344,7 +323,7 @@ void sha512_hmac_init( sha512_ctx *ctx, unsigned char *key, int keylen ) if( keylen > 128 ) { sha512_complete( key, keylen, sum ); - keylen = ( is384 ) ? 48 : 64; + keylen = SHA512_DIGEST_LEN; key = sum; } @@ -380,7 +359,7 @@ void sha512_hmac_final( sha512_ctx *ctx, unsigned char output[64] ) int hlen; unsigned char tmpbuf[64]; - hlen = ( is384 == 0 ) ? 64 : 48; + hlen = SHA512_DIGEST_LEN; sha512_final( ctx, tmpbuf ); sha512_init( ctx );