From 63bc6e8971f66233f4e05bb8d7f7db9afd7ba7f4 Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 30 Sep 2010 18:42:42 +0000 Subject: Split SHA-2 into 32 and 64 bit versions; they are totally independent of each other anyway. --- src/engine/core_engine/lookup_hash.cpp | 9 +- src/hash/sha2/info.txt | 5 - src/hash/sha2/sha2_32.cpp | 229 -------------------------------- src/hash/sha2/sha2_32.h | 56 -------- src/hash/sha2/sha2_64.cpp | 236 --------------------------------- src/hash/sha2/sha2_64.h | 54 -------- src/hash/sha2_32/info.txt | 5 + src/hash/sha2_32/sha2_32.cpp | 229 ++++++++++++++++++++++++++++++++ src/hash/sha2_32/sha2_32.h | 56 ++++++++ src/hash/sha2_64/info.txt | 5 + src/hash/sha2_64/sha2_64.cpp | 236 +++++++++++++++++++++++++++++++++ src/hash/sha2_64/sha2_64.h | 54 ++++++++ 12 files changed, 592 insertions(+), 582 deletions(-) delete mode 100644 src/hash/sha2/info.txt delete mode 100644 src/hash/sha2/sha2_32.cpp delete mode 100644 src/hash/sha2/sha2_32.h delete mode 100644 src/hash/sha2/sha2_64.cpp delete mode 100644 src/hash/sha2/sha2_64.h create mode 100644 src/hash/sha2_32/info.txt create mode 100644 src/hash/sha2_32/sha2_32.cpp create mode 100644 src/hash/sha2_32/sha2_32.h create mode 100644 src/hash/sha2_64/info.txt create mode 100644 src/hash/sha2_64/sha2_64.cpp create mode 100644 src/hash/sha2_64/sha2_64.h diff --git a/src/engine/core_engine/lookup_hash.cpp b/src/engine/core_engine/lookup_hash.cpp index 1524015dd..705b16b87 100644 --- a/src/engine/core_engine/lookup_hash.cpp +++ b/src/engine/core_engine/lookup_hash.cpp @@ -58,8 +58,10 @@ #include #endif -#if defined(BOTAN_HAS_SHA2) +#if defined(BOTAN_HAS_SHA2_32) #include + +#if defined(BOTAN_HAS_SHA2_64) #include #endif @@ -151,11 +153,14 @@ HashFunction* Core_Engine::find_hash(const SCAN_Name& request, return new SHA_160; #endif -#if defined(BOTAN_HAS_SHA2) +#if defined(BOTAN_HAS_SHA2_32) if(request.algo_name() == "SHA-224") return new SHA_224; if(request.algo_name() == "SHA-256") return new SHA_256; +#endif + +#if defined(BOTAN_HAS_SHA2_64) if(request.algo_name() == "SHA-384") return new SHA_384; if(request.algo_name() == "SHA-512") diff --git a/src/hash/sha2/info.txt b/src/hash/sha2/info.txt deleted file mode 100644 index e12f2b694..000000000 --- a/src/hash/sha2/info.txt +++ /dev/null @@ -1,5 +0,0 @@ -define SHA2 - - -mdx_hash - diff --git a/src/hash/sha2/sha2_32.cpp b/src/hash/sha2/sha2_32.cpp deleted file mode 100644 index acd06061e..000000000 --- a/src/hash/sha2/sha2_32.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/* -* SHA-{224,256} -* (C) 1999-2010 Jack Lloyd -* 2007 FlexSecure GmbH -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include - -namespace Botan { - -namespace { - -/* -* SHA-256 Rho Function -*/ -inline u32bit rho(u32bit X, u32bit rot1, u32bit rot2, u32bit rot3) - { - return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ - rotate_right(X, rot3)); - } - -/* -* SHA-256 Sigma Function -*/ -inline u32bit sigma(u32bit X, u32bit rot1, u32bit rot2, u32bit shift) - { - return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); - } - -/* -* SHA-256 F1 Function -*/ -inline void F1(u32bit A, u32bit B, u32bit C, u32bit& D, - u32bit E, u32bit F, u32bit G, u32bit& H, - u32bit msg, u32bit magic) - { - H += magic + rho(E, 6, 11, 25) + ((E & F) ^ (~E & G)) + msg; - D += H; - H += rho(A, 2, 13, 22) + ((A & B) | ((A | B) & C)); - } - -/* -* SHA-224 / SHA-256 compression function -*/ -void sha2_32_compress(MemoryRegion& W, - MemoryRegion& digest, - const byte input[], u32bit blocks) - { - u32bit A = digest[0], B = digest[1], C = digest[2], - D = digest[3], E = digest[4], F = digest[5], - G = digest[6], H = digest[7]; - - for(u32bit i = 0; i != blocks; ++i) - { - load_be(&W[0], input, 16); - - for(u32bit j = 16; j != 64; j += 8) - { - W[j ] = sigma(W[j- 2], 17, 19, 10) + W[j-7] + - sigma(W[j-15], 7, 18, 3) + W[j-16]; - W[j+1] = sigma(W[j- 1], 17, 19, 10) + W[j-6] + - sigma(W[j-14], 7, 18, 3) + W[j-15]; - W[j+2] = sigma(W[j ], 17, 19, 10) + W[j-5] + - sigma(W[j-13], 7, 18, 3) + W[j-14]; - W[j+3] = sigma(W[j+ 1], 17, 19, 10) + W[j-4] + - sigma(W[j-12], 7, 18, 3) + W[j-13]; - W[j+4] = sigma(W[j+ 2], 17, 19, 10) + W[j-3] + - sigma(W[j-11], 7, 18, 3) + W[j-12]; - W[j+5] = sigma(W[j+ 3], 17, 19, 10) + W[j-2] + - sigma(W[j-10], 7, 18, 3) + W[j-11]; - W[j+6] = sigma(W[j+ 4], 17, 19, 10) + W[j-1] + - sigma(W[j- 9], 7, 18, 3) + W[j-10]; - W[j+7] = sigma(W[j+ 5], 17, 19, 10) + W[j ] + - sigma(W[j- 8], 7, 18, 3) + W[j- 9]; - } - - F1(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98); - F1(H, A, B, C, D, E, F, G, W[ 1], 0x71374491); - F1(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF); - F1(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5); - F1(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B); - F1(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1); - F1(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4); - F1(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5); - F1(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98); - F1(H, A, B, C, D, E, F, G, W[ 9], 0x12835B01); - F1(G, H, A, B, C, D, E, F, W[10], 0x243185BE); - F1(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3); - F1(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74); - F1(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE); - F1(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7); - F1(B, C, D, E, F, G, H, A, W[15], 0xC19BF174); - F1(A, B, C, D, E, F, G, H, W[16], 0xE49B69C1); - F1(H, A, B, C, D, E, F, G, W[17], 0xEFBE4786); - F1(G, H, A, B, C, D, E, F, W[18], 0x0FC19DC6); - F1(F, G, H, A, B, C, D, E, W[19], 0x240CA1CC); - F1(E, F, G, H, A, B, C, D, W[20], 0x2DE92C6F); - F1(D, E, F, G, H, A, B, C, W[21], 0x4A7484AA); - F1(C, D, E, F, G, H, A, B, W[22], 0x5CB0A9DC); - F1(B, C, D, E, F, G, H, A, W[23], 0x76F988DA); - F1(A, B, C, D, E, F, G, H, W[24], 0x983E5152); - F1(H, A, B, C, D, E, F, G, W[25], 0xA831C66D); - F1(G, H, A, B, C, D, E, F, W[26], 0xB00327C8); - F1(F, G, H, A, B, C, D, E, W[27], 0xBF597FC7); - F1(E, F, G, H, A, B, C, D, W[28], 0xC6E00BF3); - F1(D, E, F, G, H, A, B, C, W[29], 0xD5A79147); - F1(C, D, E, F, G, H, A, B, W[30], 0x06CA6351); - F1(B, C, D, E, F, G, H, A, W[31], 0x14292967); - F1(A, B, C, D, E, F, G, H, W[32], 0x27B70A85); - F1(H, A, B, C, D, E, F, G, W[33], 0x2E1B2138); - F1(G, H, A, B, C, D, E, F, W[34], 0x4D2C6DFC); - F1(F, G, H, A, B, C, D, E, W[35], 0x53380D13); - F1(E, F, G, H, A, B, C, D, W[36], 0x650A7354); - F1(D, E, F, G, H, A, B, C, W[37], 0x766A0ABB); - F1(C, D, E, F, G, H, A, B, W[38], 0x81C2C92E); - F1(B, C, D, E, F, G, H, A, W[39], 0x92722C85); - F1(A, B, C, D, E, F, G, H, W[40], 0xA2BFE8A1); - F1(H, A, B, C, D, E, F, G, W[41], 0xA81A664B); - F1(G, H, A, B, C, D, E, F, W[42], 0xC24B8B70); - F1(F, G, H, A, B, C, D, E, W[43], 0xC76C51A3); - F1(E, F, G, H, A, B, C, D, W[44], 0xD192E819); - F1(D, E, F, G, H, A, B, C, W[45], 0xD6990624); - F1(C, D, E, F, G, H, A, B, W[46], 0xF40E3585); - F1(B, C, D, E, F, G, H, A, W[47], 0x106AA070); - F1(A, B, C, D, E, F, G, H, W[48], 0x19A4C116); - F1(H, A, B, C, D, E, F, G, W[49], 0x1E376C08); - F1(G, H, A, B, C, D, E, F, W[50], 0x2748774C); - F1(F, G, H, A, B, C, D, E, W[51], 0x34B0BCB5); - F1(E, F, G, H, A, B, C, D, W[52], 0x391C0CB3); - F1(D, E, F, G, H, A, B, C, W[53], 0x4ED8AA4A); - F1(C, D, E, F, G, H, A, B, W[54], 0x5B9CCA4F); - F1(B, C, D, E, F, G, H, A, W[55], 0x682E6FF3); - F1(A, B, C, D, E, F, G, H, W[56], 0x748F82EE); - F1(H, A, B, C, D, E, F, G, W[57], 0x78A5636F); - F1(G, H, A, B, C, D, E, F, W[58], 0x84C87814); - F1(F, G, H, A, B, C, D, E, W[59], 0x8CC70208); - F1(E, F, G, H, A, B, C, D, W[60], 0x90BEFFFA); - F1(D, E, F, G, H, A, B, C, W[61], 0xA4506CEB); - F1(C, D, E, F, G, H, A, B, W[62], 0xBEF9A3F7); - F1(B, C, D, E, F, G, H, A, W[63], 0xC67178F2); - - A = (digest[0] += A); - B = (digest[1] += B); - C = (digest[2] += C); - D = (digest[3] += D); - E = (digest[4] += E); - F = (digest[5] += F); - G = (digest[6] += G); - H = (digest[7] += H); - - input += 64; - } - } - -} - -/* -* SHA-224 compression function -*/ -void SHA_224::compress_n(const byte input[], u32bit blocks) - { - sha2_32_compress(W, digest, input, blocks); - } - -/* -* Copy out the digest -*/ -void SHA_224::copy_out(byte output[]) - { - for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) - store_be(digest[j/4], output + j); - } - -/* -* Clear memory of sensitive data -*/ -void SHA_224::clear() - { - MDx_HashFunction::clear(); - zeroise(W); - digest[0] = 0xC1059ED8; - digest[1] = 0x367CD507; - digest[2] = 0x3070DD17; - digest[3] = 0xF70E5939; - digest[4] = 0xFFC00B31; - digest[5] = 0x68581511; - digest[6] = 0x64F98FA7; - digest[7] = 0xBEFA4FA4; - } - -/* -* SHA-256 compression function -*/ -void SHA_256::compress_n(const byte input[], u32bit blocks) - { - sha2_32_compress(W, digest, input, blocks); - } - -/* -* Copy out the digest -*/ -void SHA_256::copy_out(byte output[]) - { - for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) - store_be(digest[j/4], output + j); - } - -/* -* Clear memory of sensitive data -*/ -void SHA_256::clear() - { - MDx_HashFunction::clear(); - zeroise(W); - digest[0] = 0x6A09E667; - digest[1] = 0xBB67AE85; - digest[2] = 0x3C6EF372; - digest[3] = 0xA54FF53A; - digest[4] = 0x510E527F; - digest[5] = 0x9B05688C; - digest[6] = 0x1F83D9AB; - digest[7] = 0x5BE0CD19; - } - -} diff --git a/src/hash/sha2/sha2_32.h b/src/hash/sha2/sha2_32.h deleted file mode 100644 index a3e3a6f19..000000000 --- a/src/hash/sha2/sha2_32.h +++ /dev/null @@ -1,56 +0,0 @@ -/* -* SHA-{224,256} -* (C) 1999-2010 Jack Lloyd -* 2007 FlexSecure GmbH -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_SHA_224_256_H__ -#define BOTAN_SHA_224_256_H__ - -#include - -namespace Botan { - -/** -* SHA-224 -*/ -class BOTAN_DLL SHA_224 : public MDx_HashFunction - { - public: - void clear(); - std::string name() const { return "SHA-224"; } - HashFunction* clone() const { return new SHA_224; } - - SHA_224() : MDx_HashFunction(28, 64, true, true), W(64), digest(8) - { clear(); } - private: - void compress_n(const byte[], u32bit blocks); - void copy_out(byte[]); - - SecureVector W, digest; - }; - -/** -* SHA-256 -*/ -class BOTAN_DLL SHA_256 : public MDx_HashFunction - { - public: - void clear(); - std::string name() const { return "SHA-256"; } - HashFunction* clone() const { return new SHA_256; } - - SHA_256() : MDx_HashFunction(32, 64, true, true), W(64), digest(8) - { clear(); } - private: - void compress_n(const byte[], u32bit blocks); - void copy_out(byte[]); - - SecureVector W, digest; - }; - -} - -#endif diff --git a/src/hash/sha2/sha2_64.cpp b/src/hash/sha2/sha2_64.cpp deleted file mode 100644 index 5ca78173c..000000000 --- a/src/hash/sha2/sha2_64.cpp +++ /dev/null @@ -1,236 +0,0 @@ -/* -* SHA-{384,512} -* (C) 1999-2010 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include - -namespace Botan { - -namespace { - -/* -* SHA-{384,512} Rho Function -*/ -inline u64bit rho(u64bit X, u32bit rot1, u32bit rot2, u32bit rot3) - { - return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ - rotate_right(X, rot3)); - } - -/* -* SHA-{384,512} F1 Function -*/ -inline void F1(u64bit A, u64bit B, u64bit C, u64bit& D, - u64bit E, u64bit F, u64bit G, u64bit& H, - u64bit msg, u64bit magic) - { - magic += rho(E, 14, 18, 41) + ((E & F) ^ (~E & G)) + msg; - D += magic + H; - H += magic + rho(A, 28, 34, 39) + ((A & B) ^ (A & C) ^ (B & C)); - } - -/* -* SHA-{384,512} Sigma Function -*/ -inline u64bit sigma(u64bit X, u32bit rot1, u32bit rot2, u32bit shift) - { - return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); - } - -/* -* SHA-{384,512} Compression Function -*/ -void sha2_64_compress(MemoryRegion& W, - MemoryRegion& digest, - const byte input[], u32bit blocks) - { - u64bit A = digest[0], B = digest[1], C = digest[2], - D = digest[3], E = digest[4], F = digest[5], - G = digest[6], H = digest[7]; - - for(u32bit i = 0; i != blocks; ++i) - { - load_be(&W[0], input, 16); - - for(u32bit j = 16; j != 80; j += 8) - { - W[j ] = sigma(W[j-2], 19, 61, 6) + W[j-7] + sigma(W[j-15], 1, 8, 7) + W[j-16]; - W[j+1] = sigma(W[j-1], 19, 61, 6) + W[j-6] + sigma(W[j-14], 1, 8, 7) + W[j-15]; - W[j+2] = sigma(W[j ], 19, 61, 6) + W[j-5] + sigma(W[j-13], 1, 8, 7) + W[j-14]; - W[j+3] = sigma(W[j+1], 19, 61, 6) + W[j-4] + sigma(W[j-12], 1, 8, 7) + W[j-13]; - W[j+4] = sigma(W[j+2], 19, 61, 6) + W[j-3] + sigma(W[j-11], 1, 8, 7) + W[j-12]; - W[j+5] = sigma(W[j+3], 19, 61, 6) + W[j-2] + sigma(W[j-10], 1, 8, 7) + W[j-11]; - W[j+6] = sigma(W[j+4], 19, 61, 6) + W[j-1] + sigma(W[j- 9], 1, 8, 7) + W[j-10]; - W[j+7] = sigma(W[j+5], 19, 61, 6) + W[j ] + sigma(W[j- 8], 1, 8, 7) + W[j- 9]; - } - - F1(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98D728AE22); - F1(H, A, B, C, D, E, F, G, W[ 1], 0x7137449123EF65CD); - F1(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCFEC4D3B2F); - F1(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA58189DBBC); - F1(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25BF348B538); - F1(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1B605D019); - F1(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4AF194F9B); - F1(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5DA6D8118); - F1(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98A3030242); - F1(H, A, B, C, D, E, F, G, W[ 9], 0x12835B0145706FBE); - F1(G, H, A, B, C, D, E, F, W[10], 0x243185BE4EE4B28C); - F1(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3D5FFB4E2); - F1(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74F27B896F); - F1(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE3B1696B1); - F1(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A725C71235); - F1(B, C, D, E, F, G, H, A, W[15], 0xC19BF174CF692694); - F1(A, B, C, D, E, F, G, H, W[16], 0xE49B69C19EF14AD2); - F1(H, A, B, C, D, E, F, G, W[17], 0xEFBE4786384F25E3); - F1(G, H, A, B, C, D, E, F, W[18], 0x0FC19DC68B8CD5B5); - F1(F, G, H, A, B, C, D, E, W[19], 0x240CA1CC77AC9C65); - F1(E, F, G, H, A, B, C, D, W[20], 0x2DE92C6F592B0275); - F1(D, E, F, G, H, A, B, C, W[21], 0x4A7484AA6EA6E483); - F1(C, D, E, F, G, H, A, B, W[22], 0x5CB0A9DCBD41FBD4); - F1(B, C, D, E, F, G, H, A, W[23], 0x76F988DA831153B5); - F1(A, B, C, D, E, F, G, H, W[24], 0x983E5152EE66DFAB); - F1(H, A, B, C, D, E, F, G, W[25], 0xA831C66D2DB43210); - F1(G, H, A, B, C, D, E, F, W[26], 0xB00327C898FB213F); - F1(F, G, H, A, B, C, D, E, W[27], 0xBF597FC7BEEF0EE4); - F1(E, F, G, H, A, B, C, D, W[28], 0xC6E00BF33DA88FC2); - F1(D, E, F, G, H, A, B, C, W[29], 0xD5A79147930AA725); - F1(C, D, E, F, G, H, A, B, W[30], 0x06CA6351E003826F); - F1(B, C, D, E, F, G, H, A, W[31], 0x142929670A0E6E70); - F1(A, B, C, D, E, F, G, H, W[32], 0x27B70A8546D22FFC); - F1(H, A, B, C, D, E, F, G, W[33], 0x2E1B21385C26C926); - F1(G, H, A, B, C, D, E, F, W[34], 0x4D2C6DFC5AC42AED); - F1(F, G, H, A, B, C, D, E, W[35], 0x53380D139D95B3DF); - F1(E, F, G, H, A, B, C, D, W[36], 0x650A73548BAF63DE); - F1(D, E, F, G, H, A, B, C, W[37], 0x766A0ABB3C77B2A8); - F1(C, D, E, F, G, H, A, B, W[38], 0x81C2C92E47EDAEE6); - F1(B, C, D, E, F, G, H, A, W[39], 0x92722C851482353B); - F1(A, B, C, D, E, F, G, H, W[40], 0xA2BFE8A14CF10364); - F1(H, A, B, C, D, E, F, G, W[41], 0xA81A664BBC423001); - F1(G, H, A, B, C, D, E, F, W[42], 0xC24B8B70D0F89791); - F1(F, G, H, A, B, C, D, E, W[43], 0xC76C51A30654BE30); - F1(E, F, G, H, A, B, C, D, W[44], 0xD192E819D6EF5218); - F1(D, E, F, G, H, A, B, C, W[45], 0xD69906245565A910); - F1(C, D, E, F, G, H, A, B, W[46], 0xF40E35855771202A); - F1(B, C, D, E, F, G, H, A, W[47], 0x106AA07032BBD1B8); - F1(A, B, C, D, E, F, G, H, W[48], 0x19A4C116B8D2D0C8); - F1(H, A, B, C, D, E, F, G, W[49], 0x1E376C085141AB53); - F1(G, H, A, B, C, D, E, F, W[50], 0x2748774CDF8EEB99); - F1(F, G, H, A, B, C, D, E, W[51], 0x34B0BCB5E19B48A8); - F1(E, F, G, H, A, B, C, D, W[52], 0x391C0CB3C5C95A63); - F1(D, E, F, G, H, A, B, C, W[53], 0x4ED8AA4AE3418ACB); - F1(C, D, E, F, G, H, A, B, W[54], 0x5B9CCA4F7763E373); - F1(B, C, D, E, F, G, H, A, W[55], 0x682E6FF3D6B2B8A3); - F1(A, B, C, D, E, F, G, H, W[56], 0x748F82EE5DEFB2FC); - F1(H, A, B, C, D, E, F, G, W[57], 0x78A5636F43172F60); - F1(G, H, A, B, C, D, E, F, W[58], 0x84C87814A1F0AB72); - F1(F, G, H, A, B, C, D, E, W[59], 0x8CC702081A6439EC); - F1(E, F, G, H, A, B, C, D, W[60], 0x90BEFFFA23631E28); - F1(D, E, F, G, H, A, B, C, W[61], 0xA4506CEBDE82BDE9); - F1(C, D, E, F, G, H, A, B, W[62], 0xBEF9A3F7B2C67915); - F1(B, C, D, E, F, G, H, A, W[63], 0xC67178F2E372532B); - F1(A, B, C, D, E, F, G, H, W[64], 0xCA273ECEEA26619C); - F1(H, A, B, C, D, E, F, G, W[65], 0xD186B8C721C0C207); - F1(G, H, A, B, C, D, E, F, W[66], 0xEADA7DD6CDE0EB1E); - F1(F, G, H, A, B, C, D, E, W[67], 0xF57D4F7FEE6ED178); - F1(E, F, G, H, A, B, C, D, W[68], 0x06F067AA72176FBA); - F1(D, E, F, G, H, A, B, C, W[69], 0x0A637DC5A2C898A6); - F1(C, D, E, F, G, H, A, B, W[70], 0x113F9804BEF90DAE); - F1(B, C, D, E, F, G, H, A, W[71], 0x1B710B35131C471B); - F1(A, B, C, D, E, F, G, H, W[72], 0x28DB77F523047D84); - F1(H, A, B, C, D, E, F, G, W[73], 0x32CAAB7B40C72493); - F1(G, H, A, B, C, D, E, F, W[74], 0x3C9EBE0A15C9BEBC); - F1(F, G, H, A, B, C, D, E, W[75], 0x431D67C49C100D4C); - F1(E, F, G, H, A, B, C, D, W[76], 0x4CC5D4BECB3E42B6); - F1(D, E, F, G, H, A, B, C, W[77], 0x597F299CFC657E2A); - F1(C, D, E, F, G, H, A, B, W[78], 0x5FCB6FAB3AD6FAEC); - F1(B, C, D, E, F, G, H, A, W[79], 0x6C44198C4A475817); - - A = (digest[0] += A); - B = (digest[1] += B); - C = (digest[2] += C); - D = (digest[3] += D); - E = (digest[4] += E); - F = (digest[5] += F); - G = (digest[6] += G); - H = (digest[7] += H); - - input += 128; - } - } - -} - -/* -* SHA-384 compression function -*/ -void SHA_384::compress_n(const byte input[], u32bit blocks) - { - sha2_64_compress(W, digest, input, blocks); - } - -/* -* Copy out the digest -*/ -void SHA_384::copy_out(byte output[]) - { - for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) - store_be(digest[j/8], output + j); - } - -/* -* Clear memory of sensitive data -*/ -void SHA_384::clear() - { - MDx_HashFunction::clear(); - zeroise(W); - digest[0] = 0xCBBB9D5DC1059ED8; - digest[1] = 0x629A292A367CD507; - digest[2] = 0x9159015A3070DD17; - digest[3] = 0x152FECD8F70E5939; - digest[4] = 0x67332667FFC00B31; - digest[5] = 0x8EB44A8768581511; - digest[6] = 0xDB0C2E0D64F98FA7; - digest[7] = 0x47B5481DBEFA4FA4; - } - -/* -* SHA-512 compression function -*/ -void SHA_512::compress_n(const byte input[], u32bit blocks) - { - sha2_64_compress(W, digest, input, blocks); - } - -/* -* Copy out the digest -*/ -void SHA_512::copy_out(byte output[]) - { - for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) - store_be(digest[j/8], output + j); - } - -/* -* Clear memory of sensitive data -*/ -void SHA_512::clear() - { - MDx_HashFunction::clear(); - zeroise(W); - digest[0] = 0x6A09E667F3BCC908; - digest[1] = 0xBB67AE8584CAA73B; - digest[2] = 0x3C6EF372FE94F82B; - digest[3] = 0xA54FF53A5F1D36F1; - digest[4] = 0x510E527FADE682D1; - digest[5] = 0x9B05688C2B3E6C1F; - digest[6] = 0x1F83D9ABFB41BD6B; - digest[7] = 0x5BE0CD19137E2179; - } - -} diff --git a/src/hash/sha2/sha2_64.h b/src/hash/sha2/sha2_64.h deleted file mode 100644 index 726712221..000000000 --- a/src/hash/sha2/sha2_64.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -* SHA-{384,512} -* (C) 1999-2010 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_SHA_64BIT_H__ -#define BOTAN_SHA_64BIT_H__ - -#include - -namespace Botan { - -/** -* SHA-384 -*/ -class BOTAN_DLL SHA_384 : public MDx_HashFunction - { - public: - void clear(); - std::string name() const { return "SHA-384"; } - HashFunction* clone() const { return new SHA_384; } - - SHA_384() : MDx_HashFunction(48, 128, true, true, 16), W(80), digest(8) - { clear(); } - private: - void compress_n(const byte[], u32bit blocks); - void copy_out(byte[]); - - SecureVector W, digest; - }; - -/** -* SHA-512 -*/ -class BOTAN_DLL SHA_512 : public MDx_HashFunction - { - public: - void clear(); - std::string name() const { return "SHA-512"; } - HashFunction* clone() const { return new SHA_512; } - SHA_512() : MDx_HashFunction(64, 128, true, true, 16), W(80), digest(8) - { clear(); } - private: - void compress_n(const byte[], u32bit blocks); - void copy_out(byte[]); - - SecureVector W, digest; - }; - -} - -#endif diff --git a/src/hash/sha2_32/info.txt b/src/hash/sha2_32/info.txt new file mode 100644 index 000000000..2f0b8a7ee --- /dev/null +++ b/src/hash/sha2_32/info.txt @@ -0,0 +1,5 @@ +define SHA2_32 + + +mdx_hash + diff --git a/src/hash/sha2_32/sha2_32.cpp b/src/hash/sha2_32/sha2_32.cpp new file mode 100644 index 000000000..acd06061e --- /dev/null +++ b/src/hash/sha2_32/sha2_32.cpp @@ -0,0 +1,229 @@ +/* +* SHA-{224,256} +* (C) 1999-2010 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include + +namespace Botan { + +namespace { + +/* +* SHA-256 Rho Function +*/ +inline u32bit rho(u32bit X, u32bit rot1, u32bit rot2, u32bit rot3) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ + rotate_right(X, rot3)); + } + +/* +* SHA-256 Sigma Function +*/ +inline u32bit sigma(u32bit X, u32bit rot1, u32bit rot2, u32bit shift) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); + } + +/* +* SHA-256 F1 Function +*/ +inline void F1(u32bit A, u32bit B, u32bit C, u32bit& D, + u32bit E, u32bit F, u32bit G, u32bit& H, + u32bit msg, u32bit magic) + { + H += magic + rho(E, 6, 11, 25) + ((E & F) ^ (~E & G)) + msg; + D += H; + H += rho(A, 2, 13, 22) + ((A & B) | ((A | B) & C)); + } + +/* +* SHA-224 / SHA-256 compression function +*/ +void sha2_32_compress(MemoryRegion& W, + MemoryRegion& digest, + const byte input[], u32bit blocks) + { + u32bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4], F = digest[5], + G = digest[6], H = digest[7]; + + for(u32bit i = 0; i != blocks; ++i) + { + load_be(&W[0], input, 16); + + for(u32bit j = 16; j != 64; j += 8) + { + W[j ] = sigma(W[j- 2], 17, 19, 10) + W[j-7] + + sigma(W[j-15], 7, 18, 3) + W[j-16]; + W[j+1] = sigma(W[j- 1], 17, 19, 10) + W[j-6] + + sigma(W[j-14], 7, 18, 3) + W[j-15]; + W[j+2] = sigma(W[j ], 17, 19, 10) + W[j-5] + + sigma(W[j-13], 7, 18, 3) + W[j-14]; + W[j+3] = sigma(W[j+ 1], 17, 19, 10) + W[j-4] + + sigma(W[j-12], 7, 18, 3) + W[j-13]; + W[j+4] = sigma(W[j+ 2], 17, 19, 10) + W[j-3] + + sigma(W[j-11], 7, 18, 3) + W[j-12]; + W[j+5] = sigma(W[j+ 3], 17, 19, 10) + W[j-2] + + sigma(W[j-10], 7, 18, 3) + W[j-11]; + W[j+6] = sigma(W[j+ 4], 17, 19, 10) + W[j-1] + + sigma(W[j- 9], 7, 18, 3) + W[j-10]; + W[j+7] = sigma(W[j+ 5], 17, 19, 10) + W[j ] + + sigma(W[j- 8], 7, 18, 3) + W[j- 9]; + } + + F1(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98); + F1(H, A, B, C, D, E, F, G, W[ 1], 0x71374491); + F1(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF); + F1(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5); + F1(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B); + F1(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1); + F1(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4); + F1(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5); + F1(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98); + F1(H, A, B, C, D, E, F, G, W[ 9], 0x12835B01); + F1(G, H, A, B, C, D, E, F, W[10], 0x243185BE); + F1(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3); + F1(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74); + F1(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE); + F1(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7); + F1(B, C, D, E, F, G, H, A, W[15], 0xC19BF174); + F1(A, B, C, D, E, F, G, H, W[16], 0xE49B69C1); + F1(H, A, B, C, D, E, F, G, W[17], 0xEFBE4786); + F1(G, H, A, B, C, D, E, F, W[18], 0x0FC19DC6); + F1(F, G, H, A, B, C, D, E, W[19], 0x240CA1CC); + F1(E, F, G, H, A, B, C, D, W[20], 0x2DE92C6F); + F1(D, E, F, G, H, A, B, C, W[21], 0x4A7484AA); + F1(C, D, E, F, G, H, A, B, W[22], 0x5CB0A9DC); + F1(B, C, D, E, F, G, H, A, W[23], 0x76F988DA); + F1(A, B, C, D, E, F, G, H, W[24], 0x983E5152); + F1(H, A, B, C, D, E, F, G, W[25], 0xA831C66D); + F1(G, H, A, B, C, D, E, F, W[26], 0xB00327C8); + F1(F, G, H, A, B, C, D, E, W[27], 0xBF597FC7); + F1(E, F, G, H, A, B, C, D, W[28], 0xC6E00BF3); + F1(D, E, F, G, H, A, B, C, W[29], 0xD5A79147); + F1(C, D, E, F, G, H, A, B, W[30], 0x06CA6351); + F1(B, C, D, E, F, G, H, A, W[31], 0x14292967); + F1(A, B, C, D, E, F, G, H, W[32], 0x27B70A85); + F1(H, A, B, C, D, E, F, G, W[33], 0x2E1B2138); + F1(G, H, A, B, C, D, E, F, W[34], 0x4D2C6DFC); + F1(F, G, H, A, B, C, D, E, W[35], 0x53380D13); + F1(E, F, G, H, A, B, C, D, W[36], 0x650A7354); + F1(D, E, F, G, H, A, B, C, W[37], 0x766A0ABB); + F1(C, D, E, F, G, H, A, B, W[38], 0x81C2C92E); + F1(B, C, D, E, F, G, H, A, W[39], 0x92722C85); + F1(A, B, C, D, E, F, G, H, W[40], 0xA2BFE8A1); + F1(H, A, B, C, D, E, F, G, W[41], 0xA81A664B); + F1(G, H, A, B, C, D, E, F, W[42], 0xC24B8B70); + F1(F, G, H, A, B, C, D, E, W[43], 0xC76C51A3); + F1(E, F, G, H, A, B, C, D, W[44], 0xD192E819); + F1(D, E, F, G, H, A, B, C, W[45], 0xD6990624); + F1(C, D, E, F, G, H, A, B, W[46], 0xF40E3585); + F1(B, C, D, E, F, G, H, A, W[47], 0x106AA070); + F1(A, B, C, D, E, F, G, H, W[48], 0x19A4C116); + F1(H, A, B, C, D, E, F, G, W[49], 0x1E376C08); + F1(G, H, A, B, C, D, E, F, W[50], 0x2748774C); + F1(F, G, H, A, B, C, D, E, W[51], 0x34B0BCB5); + F1(E, F, G, H, A, B, C, D, W[52], 0x391C0CB3); + F1(D, E, F, G, H, A, B, C, W[53], 0x4ED8AA4A); + F1(C, D, E, F, G, H, A, B, W[54], 0x5B9CCA4F); + F1(B, C, D, E, F, G, H, A, W[55], 0x682E6FF3); + F1(A, B, C, D, E, F, G, H, W[56], 0x748F82EE); + F1(H, A, B, C, D, E, F, G, W[57], 0x78A5636F); + F1(G, H, A, B, C, D, E, F, W[58], 0x84C87814); + F1(F, G, H, A, B, C, D, E, W[59], 0x8CC70208); + F1(E, F, G, H, A, B, C, D, W[60], 0x90BEFFFA); + F1(D, E, F, G, H, A, B, C, W[61], 0xA4506CEB); + F1(C, D, E, F, G, H, A, B, W[62], 0xBEF9A3F7); + F1(B, C, D, E, F, G, H, A, W[63], 0xC67178F2); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + E = (digest[4] += E); + F = (digest[5] += F); + G = (digest[6] += G); + H = (digest[7] += H); + + input += 64; + } + } + +} + +/* +* SHA-224 compression function +*/ +void SHA_224::compress_n(const byte input[], u32bit blocks) + { + sha2_32_compress(W, digest, input, blocks); + } + +/* +* Copy out the digest +*/ +void SHA_224::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_be(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_224::clear() + { + MDx_HashFunction::clear(); + zeroise(W); + digest[0] = 0xC1059ED8; + digest[1] = 0x367CD507; + digest[2] = 0x3070DD17; + digest[3] = 0xF70E5939; + digest[4] = 0xFFC00B31; + digest[5] = 0x68581511; + digest[6] = 0x64F98FA7; + digest[7] = 0xBEFA4FA4; + } + +/* +* SHA-256 compression function +*/ +void SHA_256::compress_n(const byte input[], u32bit blocks) + { + sha2_32_compress(W, digest, input, blocks); + } + +/* +* Copy out the digest +*/ +void SHA_256::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_be(digest[j/4], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_256::clear() + { + MDx_HashFunction::clear(); + zeroise(W); + digest[0] = 0x6A09E667; + digest[1] = 0xBB67AE85; + digest[2] = 0x3C6EF372; + digest[3] = 0xA54FF53A; + digest[4] = 0x510E527F; + digest[5] = 0x9B05688C; + digest[6] = 0x1F83D9AB; + digest[7] = 0x5BE0CD19; + } + +} diff --git a/src/hash/sha2_32/sha2_32.h b/src/hash/sha2_32/sha2_32.h new file mode 100644 index 000000000..a3e3a6f19 --- /dev/null +++ b/src/hash/sha2_32/sha2_32.h @@ -0,0 +1,56 @@ +/* +* SHA-{224,256} +* (C) 1999-2010 Jack Lloyd +* 2007 FlexSecure GmbH +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_224_256_H__ +#define BOTAN_SHA_224_256_H__ + +#include + +namespace Botan { + +/** +* SHA-224 +*/ +class BOTAN_DLL SHA_224 : public MDx_HashFunction + { + public: + void clear(); + std::string name() const { return "SHA-224"; } + HashFunction* clone() const { return new SHA_224; } + + SHA_224() : MDx_HashFunction(28, 64, true, true), W(64), digest(8) + { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector W, digest; + }; + +/** +* SHA-256 +*/ +class BOTAN_DLL SHA_256 : public MDx_HashFunction + { + public: + void clear(); + std::string name() const { return "SHA-256"; } + HashFunction* clone() const { return new SHA_256; } + + SHA_256() : MDx_HashFunction(32, 64, true, true), W(64), digest(8) + { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector W, digest; + }; + +} + +#endif diff --git a/src/hash/sha2_64/info.txt b/src/hash/sha2_64/info.txt new file mode 100644 index 000000000..02e9971ce --- /dev/null +++ b/src/hash/sha2_64/info.txt @@ -0,0 +1,5 @@ +define SHA2_64 + + +mdx_hash + diff --git a/src/hash/sha2_64/sha2_64.cpp b/src/hash/sha2_64/sha2_64.cpp new file mode 100644 index 000000000..5ca78173c --- /dev/null +++ b/src/hash/sha2_64/sha2_64.cpp @@ -0,0 +1,236 @@ +/* +* SHA-{384,512} +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include + +namespace Botan { + +namespace { + +/* +* SHA-{384,512} Rho Function +*/ +inline u64bit rho(u64bit X, u32bit rot1, u32bit rot2, u32bit rot3) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ + rotate_right(X, rot3)); + } + +/* +* SHA-{384,512} F1 Function +*/ +inline void F1(u64bit A, u64bit B, u64bit C, u64bit& D, + u64bit E, u64bit F, u64bit G, u64bit& H, + u64bit msg, u64bit magic) + { + magic += rho(E, 14, 18, 41) + ((E & F) ^ (~E & G)) + msg; + D += magic + H; + H += magic + rho(A, 28, 34, 39) + ((A & B) ^ (A & C) ^ (B & C)); + } + +/* +* SHA-{384,512} Sigma Function +*/ +inline u64bit sigma(u64bit X, u32bit rot1, u32bit rot2, u32bit shift) + { + return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift)); + } + +/* +* SHA-{384,512} Compression Function +*/ +void sha2_64_compress(MemoryRegion& W, + MemoryRegion& digest, + const byte input[], u32bit blocks) + { + u64bit A = digest[0], B = digest[1], C = digest[2], + D = digest[3], E = digest[4], F = digest[5], + G = digest[6], H = digest[7]; + + for(u32bit i = 0; i != blocks; ++i) + { + load_be(&W[0], input, 16); + + for(u32bit j = 16; j != 80; j += 8) + { + W[j ] = sigma(W[j-2], 19, 61, 6) + W[j-7] + sigma(W[j-15], 1, 8, 7) + W[j-16]; + W[j+1] = sigma(W[j-1], 19, 61, 6) + W[j-6] + sigma(W[j-14], 1, 8, 7) + W[j-15]; + W[j+2] = sigma(W[j ], 19, 61, 6) + W[j-5] + sigma(W[j-13], 1, 8, 7) + W[j-14]; + W[j+3] = sigma(W[j+1], 19, 61, 6) + W[j-4] + sigma(W[j-12], 1, 8, 7) + W[j-13]; + W[j+4] = sigma(W[j+2], 19, 61, 6) + W[j-3] + sigma(W[j-11], 1, 8, 7) + W[j-12]; + W[j+5] = sigma(W[j+3], 19, 61, 6) + W[j-2] + sigma(W[j-10], 1, 8, 7) + W[j-11]; + W[j+6] = sigma(W[j+4], 19, 61, 6) + W[j-1] + sigma(W[j- 9], 1, 8, 7) + W[j-10]; + W[j+7] = sigma(W[j+5], 19, 61, 6) + W[j ] + sigma(W[j- 8], 1, 8, 7) + W[j- 9]; + } + + F1(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98D728AE22); + F1(H, A, B, C, D, E, F, G, W[ 1], 0x7137449123EF65CD); + F1(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCFEC4D3B2F); + F1(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA58189DBBC); + F1(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25BF348B538); + F1(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1B605D019); + F1(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4AF194F9B); + F1(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5DA6D8118); + F1(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98A3030242); + F1(H, A, B, C, D, E, F, G, W[ 9], 0x12835B0145706FBE); + F1(G, H, A, B, C, D, E, F, W[10], 0x243185BE4EE4B28C); + F1(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3D5FFB4E2); + F1(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74F27B896F); + F1(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE3B1696B1); + F1(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A725C71235); + F1(B, C, D, E, F, G, H, A, W[15], 0xC19BF174CF692694); + F1(A, B, C, D, E, F, G, H, W[16], 0xE49B69C19EF14AD2); + F1(H, A, B, C, D, E, F, G, W[17], 0xEFBE4786384F25E3); + F1(G, H, A, B, C, D, E, F, W[18], 0x0FC19DC68B8CD5B5); + F1(F, G, H, A, B, C, D, E, W[19], 0x240CA1CC77AC9C65); + F1(E, F, G, H, A, B, C, D, W[20], 0x2DE92C6F592B0275); + F1(D, E, F, G, H, A, B, C, W[21], 0x4A7484AA6EA6E483); + F1(C, D, E, F, G, H, A, B, W[22], 0x5CB0A9DCBD41FBD4); + F1(B, C, D, E, F, G, H, A, W[23], 0x76F988DA831153B5); + F1(A, B, C, D, E, F, G, H, W[24], 0x983E5152EE66DFAB); + F1(H, A, B, C, D, E, F, G, W[25], 0xA831C66D2DB43210); + F1(G, H, A, B, C, D, E, F, W[26], 0xB00327C898FB213F); + F1(F, G, H, A, B, C, D, E, W[27], 0xBF597FC7BEEF0EE4); + F1(E, F, G, H, A, B, C, D, W[28], 0xC6E00BF33DA88FC2); + F1(D, E, F, G, H, A, B, C, W[29], 0xD5A79147930AA725); + F1(C, D, E, F, G, H, A, B, W[30], 0x06CA6351E003826F); + F1(B, C, D, E, F, G, H, A, W[31], 0x142929670A0E6E70); + F1(A, B, C, D, E, F, G, H, W[32], 0x27B70A8546D22FFC); + F1(H, A, B, C, D, E, F, G, W[33], 0x2E1B21385C26C926); + F1(G, H, A, B, C, D, E, F, W[34], 0x4D2C6DFC5AC42AED); + F1(F, G, H, A, B, C, D, E, W[35], 0x53380D139D95B3DF); + F1(E, F, G, H, A, B, C, D, W[36], 0x650A73548BAF63DE); + F1(D, E, F, G, H, A, B, C, W[37], 0x766A0ABB3C77B2A8); + F1(C, D, E, F, G, H, A, B, W[38], 0x81C2C92E47EDAEE6); + F1(B, C, D, E, F, G, H, A, W[39], 0x92722C851482353B); + F1(A, B, C, D, E, F, G, H, W[40], 0xA2BFE8A14CF10364); + F1(H, A, B, C, D, E, F, G, W[41], 0xA81A664BBC423001); + F1(G, H, A, B, C, D, E, F, W[42], 0xC24B8B70D0F89791); + F1(F, G, H, A, B, C, D, E, W[43], 0xC76C51A30654BE30); + F1(E, F, G, H, A, B, C, D, W[44], 0xD192E819D6EF5218); + F1(D, E, F, G, H, A, B, C, W[45], 0xD69906245565A910); + F1(C, D, E, F, G, H, A, B, W[46], 0xF40E35855771202A); + F1(B, C, D, E, F, G, H, A, W[47], 0x106AA07032BBD1B8); + F1(A, B, C, D, E, F, G, H, W[48], 0x19A4C116B8D2D0C8); + F1(H, A, B, C, D, E, F, G, W[49], 0x1E376C085141AB53); + F1(G, H, A, B, C, D, E, F, W[50], 0x2748774CDF8EEB99); + F1(F, G, H, A, B, C, D, E, W[51], 0x34B0BCB5E19B48A8); + F1(E, F, G, H, A, B, C, D, W[52], 0x391C0CB3C5C95A63); + F1(D, E, F, G, H, A, B, C, W[53], 0x4ED8AA4AE3418ACB); + F1(C, D, E, F, G, H, A, B, W[54], 0x5B9CCA4F7763E373); + F1(B, C, D, E, F, G, H, A, W[55], 0x682E6FF3D6B2B8A3); + F1(A, B, C, D, E, F, G, H, W[56], 0x748F82EE5DEFB2FC); + F1(H, A, B, C, D, E, F, G, W[57], 0x78A5636F43172F60); + F1(G, H, A, B, C, D, E, F, W[58], 0x84C87814A1F0AB72); + F1(F, G, H, A, B, C, D, E, W[59], 0x8CC702081A6439EC); + F1(E, F, G, H, A, B, C, D, W[60], 0x90BEFFFA23631E28); + F1(D, E, F, G, H, A, B, C, W[61], 0xA4506CEBDE82BDE9); + F1(C, D, E, F, G, H, A, B, W[62], 0xBEF9A3F7B2C67915); + F1(B, C, D, E, F, G, H, A, W[63], 0xC67178F2E372532B); + F1(A, B, C, D, E, F, G, H, W[64], 0xCA273ECEEA26619C); + F1(H, A, B, C, D, E, F, G, W[65], 0xD186B8C721C0C207); + F1(G, H, A, B, C, D, E, F, W[66], 0xEADA7DD6CDE0EB1E); + F1(F, G, H, A, B, C, D, E, W[67], 0xF57D4F7FEE6ED178); + F1(E, F, G, H, A, B, C, D, W[68], 0x06F067AA72176FBA); + F1(D, E, F, G, H, A, B, C, W[69], 0x0A637DC5A2C898A6); + F1(C, D, E, F, G, H, A, B, W[70], 0x113F9804BEF90DAE); + F1(B, C, D, E, F, G, H, A, W[71], 0x1B710B35131C471B); + F1(A, B, C, D, E, F, G, H, W[72], 0x28DB77F523047D84); + F1(H, A, B, C, D, E, F, G, W[73], 0x32CAAB7B40C72493); + F1(G, H, A, B, C, D, E, F, W[74], 0x3C9EBE0A15C9BEBC); + F1(F, G, H, A, B, C, D, E, W[75], 0x431D67C49C100D4C); + F1(E, F, G, H, A, B, C, D, W[76], 0x4CC5D4BECB3E42B6); + F1(D, E, F, G, H, A, B, C, W[77], 0x597F299CFC657E2A); + F1(C, D, E, F, G, H, A, B, W[78], 0x5FCB6FAB3AD6FAEC); + F1(B, C, D, E, F, G, H, A, W[79], 0x6C44198C4A475817); + + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + E = (digest[4] += E); + F = (digest[5] += F); + G = (digest[6] += G); + H = (digest[7] += H); + + input += 128; + } + } + +} + +/* +* SHA-384 compression function +*/ +void SHA_384::compress_n(const byte input[], u32bit blocks) + { + sha2_64_compress(W, digest, input, blocks); + } + +/* +* Copy out the digest +*/ +void SHA_384::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) + store_be(digest[j/8], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_384::clear() + { + MDx_HashFunction::clear(); + zeroise(W); + digest[0] = 0xCBBB9D5DC1059ED8; + digest[1] = 0x629A292A367CD507; + digest[2] = 0x9159015A3070DD17; + digest[3] = 0x152FECD8F70E5939; + digest[4] = 0x67332667FFC00B31; + digest[5] = 0x8EB44A8768581511; + digest[6] = 0xDB0C2E0D64F98FA7; + digest[7] = 0x47B5481DBEFA4FA4; + } + +/* +* SHA-512 compression function +*/ +void SHA_512::compress_n(const byte input[], u32bit blocks) + { + sha2_64_compress(W, digest, input, blocks); + } + +/* +* Copy out the digest +*/ +void SHA_512::copy_out(byte output[]) + { + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) + store_be(digest[j/8], output + j); + } + +/* +* Clear memory of sensitive data +*/ +void SHA_512::clear() + { + MDx_HashFunction::clear(); + zeroise(W); + digest[0] = 0x6A09E667F3BCC908; + digest[1] = 0xBB67AE8584CAA73B; + digest[2] = 0x3C6EF372FE94F82B; + digest[3] = 0xA54FF53A5F1D36F1; + digest[4] = 0x510E527FADE682D1; + digest[5] = 0x9B05688C2B3E6C1F; + digest[6] = 0x1F83D9ABFB41BD6B; + digest[7] = 0x5BE0CD19137E2179; + } + +} diff --git a/src/hash/sha2_64/sha2_64.h b/src/hash/sha2_64/sha2_64.h new file mode 100644 index 000000000..726712221 --- /dev/null +++ b/src/hash/sha2_64/sha2_64.h @@ -0,0 +1,54 @@ +/* +* SHA-{384,512} +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_SHA_64BIT_H__ +#define BOTAN_SHA_64BIT_H__ + +#include + +namespace Botan { + +/** +* SHA-384 +*/ +class BOTAN_DLL SHA_384 : public MDx_HashFunction + { + public: + void clear(); + std::string name() const { return "SHA-384"; } + HashFunction* clone() const { return new SHA_384; } + + SHA_384() : MDx_HashFunction(48, 128, true, true, 16), W(80), digest(8) + { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector W, digest; + }; + +/** +* SHA-512 +*/ +class BOTAN_DLL SHA_512 : public MDx_HashFunction + { + public: + void clear(); + std::string name() const { return "SHA-512"; } + HashFunction* clone() const { return new SHA_512; } + SHA_512() : MDx_HashFunction(64, 128, true, true, 16), W(80), digest(8) + { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector W, digest; + }; + +} + +#endif -- cgit v1.2.3