diff options
author | lloyd <[email protected]> | 2010-06-28 15:33:58 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-06-28 15:33:58 +0000 |
commit | 06cea953d28c7b372ccdb2765c57774e2799dd77 (patch) | |
tree | 8e51c688f6aec190a0f1d4ed5f700015dbb13c2a /src/hash/sha2 | |
parent | 420bb86ee0722cb214378611d8b0ceffedfc5eba (diff) |
For the SHA-2 classes, don't use inheritence to share a handful of
things, just share the compression function via an anon namespace
member, and replicate the simple stuff like copy_out.
Diffstat (limited to 'src/hash/sha2')
-rw-r--r-- | src/hash/sha2/sha2_32.cpp | 52 | ||||
-rw-r--r-- | src/hash/sha2/sha2_32.h | 47 | ||||
-rw-r--r-- | src/hash/sha2/sha2_64.cpp | 50 | ||||
-rw-r--r-- | src/hash/sha2/sha2_64.h | 42 |
4 files changed, 106 insertions, 85 deletions
diff --git a/src/hash/sha2/sha2_32.cpp b/src/hash/sha2/sha2_32.cpp index 91375df04..4315e10d6 100644 --- a/src/hash/sha2/sha2_32.cpp +++ b/src/hash/sha2/sha2_32.cpp @@ -1,6 +1,6 @@ /* * SHA-{224,256} -* (C) 1999-2008 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * 2007 FlexSecure GmbH * * Distributed under the terms of the Botan license @@ -43,12 +43,12 @@ inline void F1(u32bit A, u32bit B, u32bit C, u32bit& D, H += rho(A, 2, 13, 22) + ((A & B) | ((A | B) & C)); } -} - /* -* SHA-256 Compression Function +* SHA-224 / SHA-256 compression function */ -void SHA_224_256_BASE::compress_n(const byte input[], u32bit blocks) +void sha2_32_compress(MemoryRegion<u32bit>& W, + MemoryRegion<u32bit>& 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], @@ -152,26 +152,27 @@ void SHA_224_256_BASE::compress_n(const byte input[], u32bit blocks) G = (digest[6] += G); H = (digest[7] += H); - input += HASH_BLOCK_SIZE; + input += 64; } } +} + /* -* Copy out the digest +* SHA-224 compression function */ -void SHA_224_256_BASE::copy_out(byte output[]) +void SHA_224::compress_n(const byte input[], u32bit blocks) { - for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) - store_be(digest[j/4], output + j); + sha2_32_compress(W, digest, input, blocks); } /* -* Clear memory of sensitive data +* Copy out the digest */ -void SHA_224_256_BASE::clear() +void SHA_224::copy_out(byte output[]) { - MDx_HashFunction::clear(); - W.clear(); + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 4) + store_be(digest[j/4], output + j); } /* @@ -179,7 +180,8 @@ void SHA_224_256_BASE::clear() */ void SHA_224::clear() { - SHA_224_256_BASE::clear(); + MDx_HashFunction::clear(); + W.clear(); digest[0] = 0xC1059ED8; digest[1] = 0x367CD507; digest[2] = 0x3070DD17; @@ -191,11 +193,29 @@ void SHA_224::clear() } /* +* 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() { - SHA_224_256_BASE::clear(); + MDx_HashFunction::clear(); + W.clear(); digest[0] = 0x6A09E667; digest[1] = 0xBB67AE85; digest[2] = 0x3C6EF372; diff --git a/src/hash/sha2/sha2_32.h b/src/hash/sha2/sha2_32.h index e8e60d07c..71f0cff4b 100644 --- a/src/hash/sha2/sha2_32.h +++ b/src/hash/sha2/sha2_32.h @@ -1,61 +1,52 @@ /* * SHA-{224,256} -* (C) 1999-2008 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * 2007 FlexSecure GmbH * * Distributed under the terms of the Botan license */ -#ifndef BOTAN_SHA_256_H__ -#define BOTAN_SHA_256_H__ +#ifndef BOTAN_SHA_224_256_H__ +#define BOTAN_SHA_224_256_H__ #include <botan/mdx_hash.h> namespace Botan { /** -* Base class for the 32-bit SHA-2 hashes (SHA-224 and SHA-256) -*/ -class BOTAN_DLL SHA_224_256_BASE : public MDx_HashFunction - { - protected: - void clear(); - - /** - * @param out output size in bytes - */ - SHA_224_256_BASE(u32bit out) : - MDx_HashFunction(out, 64, true, true) { clear(); } - - SecureVector<u32bit, 64> W; - SecureVector<u32bit, 8> digest; - private: - void compress_n(const byte[], u32bit blocks); - void copy_out(byte[]); - }; - -/** * SHA-224 */ -class BOTAN_DLL SHA_224 : public SHA_224_256_BASE +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() : SHA_224_256_BASE(28) { clear(); } + SHA_224() : MDx_HashFunction(28, 64, true, true) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector<u32bit, 64> W; + SecureVector<u32bit, 8> digest; }; /** * SHA-256 */ -class BOTAN_DLL SHA_256 : public SHA_224_256_BASE +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() : SHA_224_256_BASE(32) { clear (); } + SHA_256() : MDx_HashFunction(32, 64, true, true) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector<u32bit, 64> W; + SecureVector<u32bit, 8> digest; }; } diff --git a/src/hash/sha2/sha2_64.cpp b/src/hash/sha2/sha2_64.cpp index 3e7c0e228..10fe81a5e 100644 --- a/src/hash/sha2/sha2_64.cpp +++ b/src/hash/sha2/sha2_64.cpp @@ -1,6 +1,6 @@ /* * SHA-{384,512} -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -42,12 +42,12 @@ 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 SHA_384_512_BASE::compress_n(const byte input[], u32bit blocks) +void sha2_64_compress(MemoryRegion<u64bit>& W, + MemoryRegion<u64bit>& 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], @@ -159,26 +159,27 @@ void SHA_384_512_BASE::compress_n(const byte input[], u32bit blocks) G = (digest[6] += G); H = (digest[7] += H); - input += HASH_BLOCK_SIZE; + input += 128; } } +} + /* -* Copy out the digest +* SHA-384 compression function */ -void SHA_384_512_BASE::copy_out(byte output[]) +void SHA_384::compress_n(const byte input[], u32bit blocks) { - for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) - store_be(digest[j/8], output + j); + sha2_64_compress(W, digest, input, blocks); } /* -* Clear memory of sensitive data +* Copy out the digest */ -void SHA_384_512_BASE::clear() +void SHA_384::copy_out(byte output[]) { - MDx_HashFunction::clear(); - W.clear(); + for(u32bit j = 0; j != OUTPUT_LENGTH; j += 8) + store_be(digest[j/8], output + j); } /* @@ -186,7 +187,8 @@ void SHA_384_512_BASE::clear() */ void SHA_384::clear() { - SHA_384_512_BASE::clear(); + MDx_HashFunction::clear(); + W.clear(); digest[0] = 0xCBBB9D5DC1059ED8; digest[1] = 0x629A292A367CD507; digest[2] = 0x9159015A3070DD17; @@ -198,11 +200,29 @@ void SHA_384::clear() } /* +* 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() { - SHA_384_512_BASE::clear(); + MDx_HashFunction::clear(); + W.clear(); digest[0] = 0x6A09E667F3BCC908; digest[1] = 0xBB67AE8584CAA73B; digest[2] = 0x3C6EF372FE94F82B; diff --git a/src/hash/sha2/sha2_64.h b/src/hash/sha2/sha2_64.h index bf87eb62d..e8112595e 100644 --- a/src/hash/sha2/sha2_64.h +++ b/src/hash/sha2/sha2_64.h @@ -1,6 +1,6 @@ /* * SHA-{384,512} -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2010 Jack Lloyd * * Distributed under the terms of the Botan license */ @@ -13,49 +13,39 @@ namespace Botan { /** -* Base class for the 64-bit SHA-2 hashes (SHA-384 and SHA-512) +* SHA-384 */ -class BOTAN_DLL SHA_384_512_BASE : public MDx_HashFunction +class BOTAN_DLL SHA_384 : public MDx_HashFunction { - protected: + public: void clear(); - - /** - * @param out output size in bytes - */ - SHA_384_512_BASE(u32bit out) : - MDx_HashFunction(out, 128, true, true, 16) {} - - SecureVector<u64bit, 8> digest; + std::string name() const { return "SHA-384"; } + HashFunction* clone() const { return new SHA_384; } + SHA_384() : MDx_HashFunction(48, 128, true, true, 16) { clear(); } private: void compress_n(const byte[], u32bit blocks); void copy_out(byte[]); SecureVector<u64bit, 80> W; - }; - -/** -* SHA-384 -*/ -class BOTAN_DLL SHA_384 : public SHA_384_512_BASE - { - public: - void clear(); - std::string name() const { return "SHA-384"; } - HashFunction* clone() const { return new SHA_384; } - SHA_384() : SHA_384_512_BASE(48) { clear(); } + SecureVector<u64bit, 8> digest; }; /** * SHA-512 */ -class BOTAN_DLL SHA_512 : public SHA_384_512_BASE +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() : SHA_384_512_BASE(64) { clear(); } + SHA_512() : MDx_HashFunction(64, 128, true, true, 16) { clear(); } + private: + void compress_n(const byte[], u32bit blocks); + void copy_out(byte[]); + + SecureVector<u64bit, 80> W; + SecureVector<u64bit, 8> digest; }; } |