diff options
Diffstat (limited to 'src/lib/hash')
-rw-r--r-- | src/lib/hash/has160/has160.cpp | 162 | ||||
-rw-r--r-- | src/lib/hash/has160/has160.h | 39 | ||||
-rw-r--r-- | src/lib/hash/has160/info.txt | 5 | ||||
-rw-r--r-- | src/lib/hash/hash.cpp | 33 | ||||
-rw-r--r-- | src/lib/hash/hash.h | 6 | ||||
-rw-r--r-- | src/lib/hash/keccak/keccak.cpp | 9 | ||||
-rw-r--r-- | src/lib/hash/keccak/keccak.h | 6 | ||||
-rw-r--r-- | src/lib/hash/md2/info.txt | 1 | ||||
-rw-r--r-- | src/lib/hash/md2/md2.cpp | 112 | ||||
-rw-r--r-- | src/lib/hash/md2/md2.h | 41 | ||||
-rw-r--r-- | src/lib/hash/rmd128/info.txt | 5 | ||||
-rw-r--r-- | src/lib/hash/rmd128/rmd128.cpp | 173 | ||||
-rw-r--r-- | src/lib/hash/rmd128/rmd128.h | 38 | ||||
-rw-r--r-- | src/lib/hash/sha1/sha160.cpp | 11 | ||||
-rw-r--r-- | src/lib/hash/sha1/sha160.h | 33 | ||||
-rw-r--r-- | src/lib/hash/sha1_sse2/info.txt | 2 | ||||
-rw-r--r-- | src/lib/hash/sha1_sse2/sha1_sse2.cpp | 38 | ||||
-rw-r--r-- | src/lib/hash/sha1_sse2/sha1_sse2.h | 29 |
18 files changed, 62 insertions, 681 deletions
diff --git a/src/lib/hash/has160/has160.cpp b/src/lib/hash/has160/has160.cpp deleted file mode 100644 index 114b5f8b5..000000000 --- a/src/lib/hash/has160/has160.cpp +++ /dev/null @@ -1,162 +0,0 @@ -/* -* HAS-160 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/has160.h> - -namespace Botan { - -namespace HAS_160_F { - -/* -* HAS-160 F1 Function -*/ -inline void F1(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, - u32bit msg, u32bit rot) - { - E += rotate_left(A, rot) + (D ^ (B & (C ^ D))) + msg; - B = rotate_left(B, 10); - } - -/* -* HAS-160 F2 Function -*/ -inline void F2(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, - u32bit msg, u32bit rot) - { - E += rotate_left(A, rot) + (B ^ C ^ D) + msg + 0x5A827999; - B = rotate_left(B, 17); - } - -/* -* HAS-160 F3 Function -*/ -inline void F3(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, - u32bit msg, u32bit rot) - { - E += rotate_left(A, rot) + (C ^ (B | ~D)) + msg + 0x6ED9EBA1; - B = rotate_left(B, 25); - } - -/* -* HAS-160 F4 Function -*/ -inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, - u32bit msg, u32bit rot) - { - E += rotate_left(A, rot) + (B ^ C ^ D) + msg + 0x8F1BBCDC; - B = rotate_left(B, 30); - } - -} - -/* -* HAS-160 Compression Function -*/ -void HAS_160::compress_n(const byte input[], size_t blocks) - { - using namespace HAS_160_F; - - u32bit A = m_digest[0], B = m_digest[1], C = m_digest[2], - D = m_digest[3], E = m_digest[4]; - - for(size_t i = 0; i != blocks; ++i) - { - load_le(m_X.data(), input, 16); - - m_X[16] = m_X[ 0] ^ m_X[ 1] ^ m_X[ 2] ^ m_X[ 3]; - m_X[17] = m_X[ 4] ^ m_X[ 5] ^ m_X[ 6] ^ m_X[ 7]; - m_X[18] = m_X[ 8] ^ m_X[ 9] ^ m_X[10] ^ m_X[11]; - m_X[19] = m_X[12] ^ m_X[13] ^ m_X[14] ^ m_X[15]; - F1(A,B,C,D,E,m_X[18], 5); F1(E,A,B,C,D,m_X[ 0],11); - F1(D,E,A,B,C,m_X[ 1], 7); F1(C,D,E,A,B,m_X[ 2],15); - F1(B,C,D,E,A,m_X[ 3], 6); F1(A,B,C,D,E,m_X[19],13); - F1(E,A,B,C,D,m_X[ 4], 8); F1(D,E,A,B,C,m_X[ 5],14); - F1(C,D,E,A,B,m_X[ 6], 7); F1(B,C,D,E,A,m_X[ 7],12); - F1(A,B,C,D,E,m_X[16], 9); F1(E,A,B,C,D,m_X[ 8],11); - F1(D,E,A,B,C,m_X[ 9], 8); F1(C,D,E,A,B,m_X[10],15); - F1(B,C,D,E,A,m_X[11], 6); F1(A,B,C,D,E,m_X[17],12); - F1(E,A,B,C,D,m_X[12], 9); F1(D,E,A,B,C,m_X[13],14); - F1(C,D,E,A,B,m_X[14], 5); F1(B,C,D,E,A,m_X[15],13); - - m_X[16] = m_X[ 3] ^ m_X[ 6] ^ m_X[ 9] ^ m_X[12]; - m_X[17] = m_X[ 2] ^ m_X[ 5] ^ m_X[ 8] ^ m_X[15]; - m_X[18] = m_X[ 1] ^ m_X[ 4] ^ m_X[11] ^ m_X[14]; - m_X[19] = m_X[ 0] ^ m_X[ 7] ^ m_X[10] ^ m_X[13]; - F2(A,B,C,D,E,m_X[18], 5); F2(E,A,B,C,D,m_X[ 3],11); - F2(D,E,A,B,C,m_X[ 6], 7); F2(C,D,E,A,B,m_X[ 9],15); - F2(B,C,D,E,A,m_X[12], 6); F2(A,B,C,D,E,m_X[19],13); - F2(E,A,B,C,D,m_X[15], 8); F2(D,E,A,B,C,m_X[ 2],14); - F2(C,D,E,A,B,m_X[ 5], 7); F2(B,C,D,E,A,m_X[ 8],12); - F2(A,B,C,D,E,m_X[16], 9); F2(E,A,B,C,D,m_X[11],11); - F2(D,E,A,B,C,m_X[14], 8); F2(C,D,E,A,B,m_X[ 1],15); - F2(B,C,D,E,A,m_X[ 4], 6); F2(A,B,C,D,E,m_X[17],12); - F2(E,A,B,C,D,m_X[ 7], 9); F2(D,E,A,B,C,m_X[10],14); - F2(C,D,E,A,B,m_X[13], 5); F2(B,C,D,E,A,m_X[ 0],13); - - m_X[16] = m_X[ 5] ^ m_X[ 7] ^ m_X[12] ^ m_X[14]; - m_X[17] = m_X[ 0] ^ m_X[ 2] ^ m_X[ 9] ^ m_X[11]; - m_X[18] = m_X[ 4] ^ m_X[ 6] ^ m_X[13] ^ m_X[15]; - m_X[19] = m_X[ 1] ^ m_X[ 3] ^ m_X[ 8] ^ m_X[10]; - F3(A,B,C,D,E,m_X[18], 5); F3(E,A,B,C,D,m_X[12],11); - F3(D,E,A,B,C,m_X[ 5], 7); F3(C,D,E,A,B,m_X[14],15); - F3(B,C,D,E,A,m_X[ 7], 6); F3(A,B,C,D,E,m_X[19],13); - F3(E,A,B,C,D,m_X[ 0], 8); F3(D,E,A,B,C,m_X[ 9],14); - F3(C,D,E,A,B,m_X[ 2], 7); F3(B,C,D,E,A,m_X[11],12); - F3(A,B,C,D,E,m_X[16], 9); F3(E,A,B,C,D,m_X[ 4],11); - F3(D,E,A,B,C,m_X[13], 8); F3(C,D,E,A,B,m_X[ 6],15); - F3(B,C,D,E,A,m_X[15], 6); F3(A,B,C,D,E,m_X[17],12); - F3(E,A,B,C,D,m_X[ 8], 9); F3(D,E,A,B,C,m_X[ 1],14); - F3(C,D,E,A,B,m_X[10], 5); F3(B,C,D,E,A,m_X[ 3],13); - - m_X[16] = m_X[ 2] ^ m_X[ 7] ^ m_X[ 8] ^ m_X[13]; - m_X[17] = m_X[ 3] ^ m_X[ 4] ^ m_X[ 9] ^ m_X[14]; - m_X[18] = m_X[ 0] ^ m_X[ 5] ^ m_X[10] ^ m_X[15]; - m_X[19] = m_X[ 1] ^ m_X[ 6] ^ m_X[11] ^ m_X[12]; - F4(A,B,C,D,E,m_X[18], 5); F4(E,A,B,C,D,m_X[ 7],11); - F4(D,E,A,B,C,m_X[ 2], 7); F4(C,D,E,A,B,m_X[13],15); - F4(B,C,D,E,A,m_X[ 8], 6); F4(A,B,C,D,E,m_X[19],13); - F4(E,A,B,C,D,m_X[ 3], 8); F4(D,E,A,B,C,m_X[14],14); - F4(C,D,E,A,B,m_X[ 9], 7); F4(B,C,D,E,A,m_X[ 4],12); - F4(A,B,C,D,E,m_X[16], 9); F4(E,A,B,C,D,m_X[15],11); - F4(D,E,A,B,C,m_X[10], 8); F4(C,D,E,A,B,m_X[ 5],15); - F4(B,C,D,E,A,m_X[ 0], 6); F4(A,B,C,D,E,m_X[17],12); - F4(E,A,B,C,D,m_X[11], 9); F4(D,E,A,B,C,m_X[ 6],14); - F4(C,D,E,A,B,m_X[ 1], 5); F4(B,C,D,E,A,m_X[12],13); - - A = (m_digest[0] += A); - B = (m_digest[1] += B); - C = (m_digest[2] += C); - D = (m_digest[3] += D); - E = (m_digest[4] += E); - - input += hash_block_size(); - } - } - -/* -* Copy out the digest -*/ -void HAS_160::copy_out(byte output[]) - { - copy_out_vec_le(output, output_length(), m_digest); - } - -/* -* Clear memory of sensitive data -*/ -void HAS_160::clear() - { - MDx_HashFunction::clear(); - zeroise(m_X); - m_digest[0] = 0x67452301; - m_digest[1] = 0xEFCDAB89; - m_digest[2] = 0x98BADCFE; - m_digest[3] = 0x10325476; - m_digest[4] = 0xC3D2E1F0; - } - -} diff --git a/src/lib/hash/has160/has160.h b/src/lib/hash/has160/has160.h deleted file mode 100644 index 7ef090eb7..000000000 --- a/src/lib/hash/has160/has160.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -* HAS-160 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_HAS_160_H__ -#define BOTAN_HAS_160_H__ - -#include <botan/mdx_hash.h> - -namespace Botan { - -/** -* HAS-160, a Korean hash function standardized in -* TTAS.KO-12.0011/R1. Used in conjunction with KCDSA -*/ -class BOTAN_DLL HAS_160 final : public MDx_HashFunction - { - public: - std::string name() const override { return "HAS-160"; } - size_t output_length() const override { return 20; } - HashFunction* clone() const override { return new HAS_160; } - - void clear() override; - - HAS_160() : MDx_HashFunction(64, false, true), m_X(20), m_digest(5) - { clear(); } - private: - void compress_n(const byte[], size_t blocks) override; - void copy_out(byte[]) override; - - secure_vector<u32bit> m_X, m_digest; - }; - -} - -#endif diff --git a/src/lib/hash/has160/info.txt b/src/lib/hash/has160/info.txt deleted file mode 100644 index cf403dad5..000000000 --- a/src/lib/hash/has160/info.txt +++ /dev/null @@ -1,5 +0,0 @@ -define HAS_160 20131128 - -<requires> -mdx_hash -</requires> diff --git a/src/lib/hash/hash.cpp b/src/lib/hash/hash.cpp index 9a15c7998..5a31763d1 100644 --- a/src/lib/hash/hash.cpp +++ b/src/lib/hash/hash.cpp @@ -25,18 +25,10 @@ #include <botan/gost_3411.h> #endif -#if defined(BOTAN_HAS_HAS_160) - #include <botan/has160.h> -#endif - #if defined(BOTAN_HAS_KECCAK) #include <botan/keccak.h> #endif -#if defined(BOTAN_HAS_MD2) - #include <botan/md2.h> -#endif - #if defined(BOTAN_HAS_MD4) #include <botan/md4.h> #endif @@ -45,10 +37,6 @@ #include <botan/md5.h> #endif -#if defined(BOTAN_HAS_RIPEMD_128) - #include <botan/rmd128.h> -#endif - #if defined(BOTAN_HAS_RIPEMD_160) #include <botan/rmd160.h> #endif @@ -57,10 +45,6 @@ #include <botan/sha160.h> #endif -#if defined(BOTAN_HAS_SHA1_SSE2) - #include <botan/sha1_sse2.h> -#endif - #if defined(BOTAN_HAS_SHA2_32) #include <botan/sha2_32.h> #endif @@ -147,18 +131,10 @@ BOTAN_REGISTER_NAMED_T(HashFunction, "Parallel", Parallel, Parallel::make); BOTAN_REGISTER_HASH_NAMED_NOARGS(GOST_34_11, "GOST-R-34.11-94"); #endif -#if defined(BOTAN_HAS_HAS_160) -BOTAN_REGISTER_HASH_NAMED_NOARGS(HAS_160, "HAS-160"); -#endif - #if defined(BOTAN_HAS_KECCAK) BOTAN_REGISTER_HASH_NAMED_1LEN(Keccak_1600, "Keccak-1600", 512); #endif -#if defined(BOTAN_HAS_MD2) -BOTAN_REGISTER_HASH_NOARGS(MD2); -#endif - #if defined(BOTAN_HAS_MD4) BOTAN_REGISTER_HASH_NOARGS(MD4); #endif @@ -167,10 +143,6 @@ BOTAN_REGISTER_HASH_NOARGS(MD4); BOTAN_REGISTER_HASH_NOARGS(MD5); #endif -#if defined(BOTAN_HAS_RIPEMD_128) -BOTAN_REGISTER_HASH_NAMED_NOARGS(RIPEMD_128, "RIPEMD-128"); -#endif - #if defined(BOTAN_HAS_RIPEMD_160) BOTAN_REGISTER_HASH_NAMED_NOARGS(RIPEMD_160, "RIPEMD-160"); #endif @@ -179,11 +151,6 @@ BOTAN_REGISTER_HASH_NAMED_NOARGS(RIPEMD_160, "RIPEMD-160"); BOTAN_REGISTER_HASH_NAMED_NOARGS(SHA_160, "SHA-160"); #endif -#if defined(BOTAN_HAS_SHA1_SSE2) -BOTAN_REGISTER_HASH_NOARGS_IF(CPUID::has_sse2(), SHA_160_SSE2, "SHA-160", - "sse2", BOTAN_SIMD_ALGORITHM_PRIO); -#endif - #if defined(BOTAN_HAS_SHA2_32) BOTAN_REGISTER_HASH_NAMED_NOARGS(SHA_224, "SHA-224"); BOTAN_REGISTER_HASH_NAMED_NOARGS(SHA_256, "SHA-256"); diff --git a/src/lib/hash/hash.h b/src/lib/hash/hash.h index ac1c22a65..08b5d5243 100644 --- a/src/lib/hash/hash.h +++ b/src/lib/hash/hash.h @@ -40,6 +40,12 @@ class BOTAN_DLL HashFunction : public Buffered_Computation */ virtual HashFunction* clone() const = 0; + /** + * @return provider information about this implementation. Default is "base", + * might also return "sse2", "avx2", "openssl", or some other arbitrary string. + */ + virtual std::string provider() const { return "base"; } + HashFunction(); virtual ~HashFunction(); diff --git a/src/lib/hash/keccak/keccak.cpp b/src/lib/hash/keccak/keccak.cpp index 44297dcef..e8bda068d 100644 --- a/src/lib/hash/keccak/keccak.cpp +++ b/src/lib/hash/keccak/keccak.cpp @@ -11,9 +11,8 @@ namespace Botan { -namespace { - -void keccak_f_1600(u64bit A[25]) +//static +void Keccak_1600::permute(u64bit A[25]) { static const u64bit RC[24] = { 0x0000000000000001, 0x0000000000008082, 0x800000000000808A, @@ -96,8 +95,6 @@ void keccak_f_1600(u64bit A[25]) } } -} - Keccak_1600::Keccak_1600(size_t output_bits) : m_output_bits(output_bits), m_bitrate(1600 - 2*output_bits), @@ -167,7 +164,7 @@ void Keccak_1600::add_data(const byte input[], size_t length) if(m_S_pos == m_bitrate / 8) { - keccak_f_1600(m_S.data()); + Keccak_1600::permute(m_S.data()); m_S_pos = 0; } } diff --git a/src/lib/hash/keccak/keccak.h b/src/lib/hash/keccak/keccak.h index a73595d6a..a66142d8c 100644 --- a/src/lib/hash/keccak/keccak.h +++ b/src/lib/hash/keccak/keccak.h @@ -33,6 +33,12 @@ class BOTAN_DLL Keccak_1600 final : public HashFunction HashFunction* clone() const override; std::string name() const override; void clear() override; + + /** + * The bare Keccak-1600 permutation + */ + static void permute(u64bit A[25]); + private: void add_data(const byte input[], size_t length) override; void final_result(byte out[]) override; diff --git a/src/lib/hash/md2/info.txt b/src/lib/hash/md2/info.txt deleted file mode 100644 index 8ea7dc393..000000000 --- a/src/lib/hash/md2/info.txt +++ /dev/null @@ -1 +0,0 @@ -define MD2 20131128 diff --git a/src/lib/hash/md2/md2.cpp b/src/lib/hash/md2/md2.cpp deleted file mode 100644 index f5e222610..000000000 --- a/src/lib/hash/md2/md2.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* -* MD2 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/md2.h> - -namespace Botan { - -/** -* MD2 Compression Function -*/ -void MD2::hash(const byte input[]) - { - static const byte SBOX[256] = { - 0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, - 0xEC, 0xF0, 0x06, 0x13, 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, - 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA, 0x1E, 0x9B, 0x57, 0x3C, - 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, - 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, - 0xBB, 0x2F, 0xEE, 0x7A, 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, - 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21, 0x80, 0x7F, 0x5D, 0x9A, - 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, - 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, - 0xAC, 0x56, 0xAA, 0xC6, 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, - 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 0x45, 0x9D, 0x70, 0x59, - 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02, - 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, - 0x34, 0x40, 0x7E, 0x0F, 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, - 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 0x2C, 0x53, 0x0D, 0x6E, - 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52, - 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, - 0x0C, 0xBD, 0xB1, 0x4A, 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, - 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39, 0xF2, 0xEF, 0xB7, 0x0E, - 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, - 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, - 0x9F, 0x11, 0x83, 0x14 }; - - buffer_insert(m_X, 16, input, hash_block_size()); - xor_buf(&m_X[32], m_X.data(), &m_X[16], hash_block_size()); - byte T = 0; - - for(size_t i = 0; i != 18; ++i) - { - for(size_t k = 0; k != 48; k += 8) - { - T = m_X[k ] ^= SBOX[T]; T = m_X[k+1] ^= SBOX[T]; - T = m_X[k+2] ^= SBOX[T]; T = m_X[k+3] ^= SBOX[T]; - T = m_X[k+4] ^= SBOX[T]; T = m_X[k+5] ^= SBOX[T]; - T = m_X[k+6] ^= SBOX[T]; T = m_X[k+7] ^= SBOX[T]; - } - - T += static_cast<byte>(i); - } - - T = m_checksum[15]; - for(size_t i = 0; i != hash_block_size(); ++i) - T = m_checksum[i] ^= SBOX[input[i] ^ T]; - } - -/** -* Update the hash -*/ -void MD2::add_data(const byte input[], size_t length) - { - buffer_insert(m_buffer, m_position, input, length); - - if(m_position + length >= hash_block_size()) - { - hash(m_buffer.data()); - input += (hash_block_size() - m_position); - length -= (hash_block_size() - m_position); - while(length >= hash_block_size()) - { - hash(input); - input += hash_block_size(); - length -= hash_block_size(); - } - copy_mem(m_buffer.data(), input, length); - m_position = 0; - } - m_position += length; - } - -/** -* Finalize a MD2 Hash -*/ -void MD2::final_result(byte output[]) - { - for(size_t i = m_position; i != hash_block_size(); ++i) - m_buffer[i] = static_cast<byte>(hash_block_size() - m_position); - - hash(m_buffer.data()); - hash(m_checksum.data()); - copy_mem(output, m_X.data(), output_length()); - clear(); - } - -/** -* Clear memory of sensitive data -*/ -void MD2::clear() - { - zeroise(m_X); - zeroise(m_checksum); - zeroise(m_buffer); - m_position = 0; - } - -} diff --git a/src/lib/hash/md2/md2.h b/src/lib/hash/md2/md2.h deleted file mode 100644 index 58629495a..000000000 --- a/src/lib/hash/md2/md2.h +++ /dev/null @@ -1,41 +0,0 @@ -/* -* MD2 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_MD2_H__ -#define BOTAN_MD2_H__ - -#include <botan/hash.h> - -namespace Botan { - -/** -* MD2 -*/ -class BOTAN_DLL MD2 final : public HashFunction - { - public: - std::string name() const override { return "MD2"; } - size_t output_length() const override { return 16; } - size_t hash_block_size() const override { return 16; } - HashFunction* clone() const override { return new MD2; } - - void clear() override; - - MD2() : m_X(48), m_checksum(16), m_buffer(16), m_position(0) - { clear(); } - private: - void add_data(const byte[], size_t) override; - void hash(const byte[]); - void final_result(byte[]) override; - - secure_vector<byte> m_X, m_checksum, m_buffer; - size_t m_position; - }; - -} - -#endif diff --git a/src/lib/hash/rmd128/info.txt b/src/lib/hash/rmd128/info.txt deleted file mode 100644 index 7d2a4eacd..000000000 --- a/src/lib/hash/rmd128/info.txt +++ /dev/null @@ -1,5 +0,0 @@ -define RIPEMD_128 20131128 - -<requires> -mdx_hash -</requires> diff --git a/src/lib/hash/rmd128/rmd128.cpp b/src/lib/hash/rmd128/rmd128.cpp deleted file mode 100644 index e520fa0c3..000000000 --- a/src/lib/hash/rmd128/rmd128.cpp +++ /dev/null @@ -1,173 +0,0 @@ -/* -* RIPEMD-128 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/rmd128.h> - -namespace Botan { - -namespace RIPEMD_128_F { - -/* -* RIPEMD-128 F1 Function -*/ -inline void F1(u32bit& A, u32bit B, u32bit C, u32bit D, - u32bit msg, u32bit shift) - { - A += (B ^ C ^ D) + msg; - A = rotate_left(A, shift); - } - -/* -* RIPEMD-128 F2 Function -*/ -inline void F2(u32bit& A, u32bit B, u32bit C, u32bit D, - u32bit msg, u32bit shift, u32bit magic) - { - A += (D ^ (B & (C ^ D))) + msg + magic; - A = rotate_left(A, shift); - } - -/* -* RIPEMD-128 F3 Function -*/ -inline void F3(u32bit& A, u32bit B, u32bit C, u32bit D, - u32bit msg, u32bit shift, u32bit magic) - { - A += (D ^ (B | ~C)) + msg + magic; - A = rotate_left(A, shift); - } - -/* -* RIPEMD-128 F4 Function -*/ -inline void F4(u32bit& A, u32bit B, u32bit C, u32bit D, - u32bit msg, u32bit shift, u32bit magic) - { - A += (C ^ (D & (B ^ C))) + msg + magic; - A = rotate_left(A, shift); - } - -} - -/* -* RIPEMD-128 Compression Function -*/ -void RIPEMD_128::compress_n(const byte input[], size_t blocks) - { - using namespace RIPEMD_128_F; - - const u32bit MAGIC2 = 0x5A827999, MAGIC3 = 0x6ED9EBA1, - MAGIC4 = 0x8F1BBCDC, MAGIC5 = 0x50A28BE6, - MAGIC6 = 0x5C4DD124, MAGIC7 = 0x6D703EF3; - - for(size_t i = 0; i != blocks; ++i) - { - load_le(m_M.data(), input, m_M.size()); - - u32bit A1 = m_digest[0], A2 = A1, B1 = m_digest[1], B2 = B1, - C1 = m_digest[2], C2 = C1, D1 = m_digest[3], D2 = D1; - - F1(A1,B1,C1,D1,m_M[ 0],11 ); F4(A2,B2,C2,D2,m_M[ 5], 8,MAGIC5); - F1(D1,A1,B1,C1,m_M[ 1],14 ); F4(D2,A2,B2,C2,m_M[14], 9,MAGIC5); - F1(C1,D1,A1,B1,m_M[ 2],15 ); F4(C2,D2,A2,B2,m_M[ 7], 9,MAGIC5); - F1(B1,C1,D1,A1,m_M[ 3],12 ); F4(B2,C2,D2,A2,m_M[ 0],11,MAGIC5); - F1(A1,B1,C1,D1,m_M[ 4], 5 ); F4(A2,B2,C2,D2,m_M[ 9],13,MAGIC5); - F1(D1,A1,B1,C1,m_M[ 5], 8 ); F4(D2,A2,B2,C2,m_M[ 2],15,MAGIC5); - F1(C1,D1,A1,B1,m_M[ 6], 7 ); F4(C2,D2,A2,B2,m_M[11],15,MAGIC5); - F1(B1,C1,D1,A1,m_M[ 7], 9 ); F4(B2,C2,D2,A2,m_M[ 4], 5,MAGIC5); - F1(A1,B1,C1,D1,m_M[ 8],11 ); F4(A2,B2,C2,D2,m_M[13], 7,MAGIC5); - F1(D1,A1,B1,C1,m_M[ 9],13 ); F4(D2,A2,B2,C2,m_M[ 6], 7,MAGIC5); - F1(C1,D1,A1,B1,m_M[10],14 ); F4(C2,D2,A2,B2,m_M[15], 8,MAGIC5); - F1(B1,C1,D1,A1,m_M[11],15 ); F4(B2,C2,D2,A2,m_M[ 8],11,MAGIC5); - F1(A1,B1,C1,D1,m_M[12], 6 ); F4(A2,B2,C2,D2,m_M[ 1],14,MAGIC5); - F1(D1,A1,B1,C1,m_M[13], 7 ); F4(D2,A2,B2,C2,m_M[10],14,MAGIC5); - F1(C1,D1,A1,B1,m_M[14], 9 ); F4(C2,D2,A2,B2,m_M[ 3],12,MAGIC5); - F1(B1,C1,D1,A1,m_M[15], 8 ); F4(B2,C2,D2,A2,m_M[12], 6,MAGIC5); - - F2(A1,B1,C1,D1,m_M[ 7], 7,MAGIC2); F3(A2,B2,C2,D2,m_M[ 6], 9,MAGIC6); - F2(D1,A1,B1,C1,m_M[ 4], 6,MAGIC2); F3(D2,A2,B2,C2,m_M[11],13,MAGIC6); - F2(C1,D1,A1,B1,m_M[13], 8,MAGIC2); F3(C2,D2,A2,B2,m_M[ 3],15,MAGIC6); - F2(B1,C1,D1,A1,m_M[ 1],13,MAGIC2); F3(B2,C2,D2,A2,m_M[ 7], 7,MAGIC6); - F2(A1,B1,C1,D1,m_M[10],11,MAGIC2); F3(A2,B2,C2,D2,m_M[ 0],12,MAGIC6); - F2(D1,A1,B1,C1,m_M[ 6], 9,MAGIC2); F3(D2,A2,B2,C2,m_M[13], 8,MAGIC6); - F2(C1,D1,A1,B1,m_M[15], 7,MAGIC2); F3(C2,D2,A2,B2,m_M[ 5], 9,MAGIC6); - F2(B1,C1,D1,A1,m_M[ 3],15,MAGIC2); F3(B2,C2,D2,A2,m_M[10],11,MAGIC6); - F2(A1,B1,C1,D1,m_M[12], 7,MAGIC2); F3(A2,B2,C2,D2,m_M[14], 7,MAGIC6); - F2(D1,A1,B1,C1,m_M[ 0],12,MAGIC2); F3(D2,A2,B2,C2,m_M[15], 7,MAGIC6); - F2(C1,D1,A1,B1,m_M[ 9],15,MAGIC2); F3(C2,D2,A2,B2,m_M[ 8],12,MAGIC6); - F2(B1,C1,D1,A1,m_M[ 5], 9,MAGIC2); F3(B2,C2,D2,A2,m_M[12], 7,MAGIC6); - F2(A1,B1,C1,D1,m_M[ 2],11,MAGIC2); F3(A2,B2,C2,D2,m_M[ 4], 6,MAGIC6); - F2(D1,A1,B1,C1,m_M[14], 7,MAGIC2); F3(D2,A2,B2,C2,m_M[ 9],15,MAGIC6); - F2(C1,D1,A1,B1,m_M[11],13,MAGIC2); F3(C2,D2,A2,B2,m_M[ 1],13,MAGIC6); - F2(B1,C1,D1,A1,m_M[ 8],12,MAGIC2); F3(B2,C2,D2,A2,m_M[ 2],11,MAGIC6); - - F3(A1,B1,C1,D1,m_M[ 3],11,MAGIC3); F2(A2,B2,C2,D2,m_M[15], 9,MAGIC7); - F3(D1,A1,B1,C1,m_M[10],13,MAGIC3); F2(D2,A2,B2,C2,m_M[ 5], 7,MAGIC7); - F3(C1,D1,A1,B1,m_M[14], 6,MAGIC3); F2(C2,D2,A2,B2,m_M[ 1],15,MAGIC7); - F3(B1,C1,D1,A1,m_M[ 4], 7,MAGIC3); F2(B2,C2,D2,A2,m_M[ 3],11,MAGIC7); - F3(A1,B1,C1,D1,m_M[ 9],14,MAGIC3); F2(A2,B2,C2,D2,m_M[ 7], 8,MAGIC7); - F3(D1,A1,B1,C1,m_M[15], 9,MAGIC3); F2(D2,A2,B2,C2,m_M[14], 6,MAGIC7); - F3(C1,D1,A1,B1,m_M[ 8],13,MAGIC3); F2(C2,D2,A2,B2,m_M[ 6], 6,MAGIC7); - F3(B1,C1,D1,A1,m_M[ 1],15,MAGIC3); F2(B2,C2,D2,A2,m_M[ 9],14,MAGIC7); - F3(A1,B1,C1,D1,m_M[ 2],14,MAGIC3); F2(A2,B2,C2,D2,m_M[11],12,MAGIC7); - F3(D1,A1,B1,C1,m_M[ 7], 8,MAGIC3); F2(D2,A2,B2,C2,m_M[ 8],13,MAGIC7); - F3(C1,D1,A1,B1,m_M[ 0],13,MAGIC3); F2(C2,D2,A2,B2,m_M[12], 5,MAGIC7); - F3(B1,C1,D1,A1,m_M[ 6], 6,MAGIC3); F2(B2,C2,D2,A2,m_M[ 2],14,MAGIC7); - F3(A1,B1,C1,D1,m_M[13], 5,MAGIC3); F2(A2,B2,C2,D2,m_M[10],13,MAGIC7); - F3(D1,A1,B1,C1,m_M[11],12,MAGIC3); F2(D2,A2,B2,C2,m_M[ 0],13,MAGIC7); - F3(C1,D1,A1,B1,m_M[ 5], 7,MAGIC3); F2(C2,D2,A2,B2,m_M[ 4], 7,MAGIC7); - F3(B1,C1,D1,A1,m_M[12], 5,MAGIC3); F2(B2,C2,D2,A2,m_M[13], 5,MAGIC7); - - F4(A1,B1,C1,D1,m_M[ 1],11,MAGIC4); F1(A2,B2,C2,D2,m_M[ 8],15 ); - F4(D1,A1,B1,C1,m_M[ 9],12,MAGIC4); F1(D2,A2,B2,C2,m_M[ 6], 5 ); - F4(C1,D1,A1,B1,m_M[11],14,MAGIC4); F1(C2,D2,A2,B2,m_M[ 4], 8 ); - F4(B1,C1,D1,A1,m_M[10],15,MAGIC4); F1(B2,C2,D2,A2,m_M[ 1],11 ); - F4(A1,B1,C1,D1,m_M[ 0],14,MAGIC4); F1(A2,B2,C2,D2,m_M[ 3],14 ); - F4(D1,A1,B1,C1,m_M[ 8],15,MAGIC4); F1(D2,A2,B2,C2,m_M[11],14 ); - F4(C1,D1,A1,B1,m_M[12], 9,MAGIC4); F1(C2,D2,A2,B2,m_M[15], 6 ); - F4(B1,C1,D1,A1,m_M[ 4], 8,MAGIC4); F1(B2,C2,D2,A2,m_M[ 0],14 ); - F4(A1,B1,C1,D1,m_M[13], 9,MAGIC4); F1(A2,B2,C2,D2,m_M[ 5], 6 ); - F4(D1,A1,B1,C1,m_M[ 3],14,MAGIC4); F1(D2,A2,B2,C2,m_M[12], 9 ); - F4(C1,D1,A1,B1,m_M[ 7], 5,MAGIC4); F1(C2,D2,A2,B2,m_M[ 2],12 ); - F4(B1,C1,D1,A1,m_M[15], 6,MAGIC4); F1(B2,C2,D2,A2,m_M[13], 9 ); - F4(A1,B1,C1,D1,m_M[14], 8,MAGIC4); F1(A2,B2,C2,D2,m_M[ 9],12 ); - F4(D1,A1,B1,C1,m_M[ 5], 6,MAGIC4); F1(D2,A2,B2,C2,m_M[ 7], 5 ); - F4(C1,D1,A1,B1,m_M[ 6], 5,MAGIC4); F1(C2,D2,A2,B2,m_M[10],15 ); - F4(B1,C1,D1,A1,m_M[ 2],12,MAGIC4); F1(B2,C2,D2,A2,m_M[14], 8 ); - - D2 = m_digest[1] + C1 + D2; - m_digest[1] = m_digest[2] + D1 + A2; - m_digest[2] = m_digest[3] + A1 + B2; - m_digest[3] = m_digest[0] + B1 + C2; - m_digest[0] = D2; - - input += hash_block_size(); - } - } - -/* -* Copy out the digest -*/ -void RIPEMD_128::copy_out(byte output[]) - { - copy_out_vec_le(output, output_length(), m_digest); - } - -/* -* Clear memory of sensitive data -*/ -void RIPEMD_128::clear() - { - MDx_HashFunction::clear(); - zeroise(m_M); - m_digest[0] = 0x67452301; - m_digest[1] = 0xEFCDAB89; - m_digest[2] = 0x98BADCFE; - m_digest[3] = 0x10325476; - } - -} diff --git a/src/lib/hash/rmd128/rmd128.h b/src/lib/hash/rmd128/rmd128.h deleted file mode 100644 index ba36ab902..000000000 --- a/src/lib/hash/rmd128/rmd128.h +++ /dev/null @@ -1,38 +0,0 @@ -/* -* RIPEMD-128 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_RIPEMD_128_H__ -#define BOTAN_RIPEMD_128_H__ - -#include <botan/mdx_hash.h> - -namespace Botan { - -/** -* RIPEMD-128 -*/ -class BOTAN_DLL RIPEMD_128 final : public MDx_HashFunction - { - public: - std::string name() const override { return "RIPEMD-128"; } - size_t output_length() const override { return 16; } - HashFunction* clone() const override { return new RIPEMD_128; } - - void clear() override; - - RIPEMD_128() : MDx_HashFunction(64, false, true), m_M(16), m_digest(4) - { clear(); } - private: - void compress_n(const byte[], size_t blocks) override; - void copy_out(byte[]) override; - - secure_vector<u32bit> m_M, m_digest; - }; - -} - -#endif diff --git a/src/lib/hash/sha1/sha160.cpp b/src/lib/hash/sha1/sha160.cpp index 21e87465a..87738fb00 100644 --- a/src/lib/hash/sha1/sha160.cpp +++ b/src/lib/hash/sha1/sha160.cpp @@ -6,6 +6,7 @@ */ #include <botan/sha160.h> +#include <botan/cpuid.h> namespace Botan { @@ -60,9 +61,19 @@ void SHA_160::compress_n(const byte input[], size_t blocks) { using namespace SHA1_F; +#if defined(BOTAN_HAS_SHA1_SSE2) + if(CPUID::has_sse2()) + { + return sse2_compress_n(m_digest, input, blocks); + } + +#endif + u32bit A = m_digest[0], B = m_digest[1], C = m_digest[2], D = m_digest[3], E = m_digest[4]; + m_W.resize(80); + for(size_t i = 0; i != blocks; ++i) { load_be(m_W.data(), input, 16); diff --git a/src/lib/hash/sha1/sha160.h b/src/lib/hash/sha1/sha160.h index b4a161c14..d7860834f 100644 --- a/src/lib/hash/sha1/sha160.h +++ b/src/lib/hash/sha1/sha160.h @@ -1,6 +1,6 @@ /* * SHA-160 -* (C) 1999-2007 Jack Lloyd +* (C) 1999-2007,2016 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -15,7 +15,7 @@ namespace Botan { /** * NIST's SHA-160 */ -class BOTAN_DLL SHA_160 : public MDx_HashFunction +class BOTAN_DLL SHA_160 final : public MDx_HashFunction { public: std::string name() const override { return "SHA-160"; } @@ -24,37 +24,36 @@ class BOTAN_DLL SHA_160 : public MDx_HashFunction void clear() override; - SHA_160() : MDx_HashFunction(64, true, true), m_digest(5), m_W(80) - { - clear(); - } - protected: - /** - * Set a custom size for the W array. Normally 80, but some - * subclasses need slightly more for best performance/internal - * constraints - * @param W_size how big to make W - */ - explicit SHA_160(size_t W_size) : - MDx_HashFunction(64, true, true), m_digest(5), m_W(W_size) + SHA_160() : MDx_HashFunction(64, true, true), m_digest(5) { clear(); } + private: void compress_n(const byte[], size_t blocks) override; + +#if defined(BOTAN_HAS_SHA1_SSE2) + static void sse2_compress_n(secure_vector<u32bit>& digest, + const byte blocks[], + size_t block_count); +#endif + + void copy_out(byte[]) override; /** - * The digest value, exposed for use by subclasses (asm, SSE2) + * The digest value */ secure_vector<u32bit> m_digest; /** - * The message buffer, exposed for use by subclasses (asm, SSE2) + * The message buffer */ secure_vector<u32bit> m_W; }; +typedef SHA_160 SHA_1; + } #endif diff --git a/src/lib/hash/sha1_sse2/info.txt b/src/lib/hash/sha1_sse2/info.txt index 78f5540e7..e352364ec 100644 --- a/src/lib/hash/sha1_sse2/info.txt +++ b/src/lib/hash/sha1_sse2/info.txt @@ -1,4 +1,4 @@ -define SHA1_SSE2 20131128 +define SHA1_SSE2 20160803 need_isa sse2 diff --git a/src/lib/hash/sha1_sse2/sha1_sse2.cpp b/src/lib/hash/sha1_sse2/sha1_sse2.cpp index 14ad88bc4..2ece541b0 100644 --- a/src/lib/hash/sha1_sse2/sha1_sse2.cpp +++ b/src/lib/hash/sha1_sse2/sha1_sse2.cpp @@ -7,8 +7,7 @@ * Botan is released under the Simplified BSD License (see license.txt) */ -#include <botan/sha1_sse2.h> -#include <botan/cpuid.h> +#include <botan/sha160.h> #include <emmintrin.h> namespace Botan { @@ -152,7 +151,8 @@ inline void F4(u32bit A, u32bit& B, u32bit C, u32bit D, u32bit& E, u32bit msg) /* * SHA-160 Compression Function using SSE for message expansion */ -void SHA_160_SSE2::compress_n(const byte input_bytes[], size_t blocks) +//static +void SHA_160::sse2_compress_n(secure_vector<uint32_t>& digest, const byte input[], size_t blocks) { using namespace SHA1_SSE2_F; @@ -161,13 +161,13 @@ void SHA_160_SSE2::compress_n(const byte input_bytes[], size_t blocks) const __m128i K40_59 = _mm_set1_epi32(0x8F1BBCDC); const __m128i K60_79 = _mm_set1_epi32(0xCA62C1D6); - u32bit A = m_digest[0], - B = m_digest[1], - C = m_digest[2], - D = m_digest[3], - E = m_digest[4]; + u32bit A = digest[0], + B = digest[1], + C = digest[2], + D = digest[3], + E = digest[4]; - const __m128i* input = reinterpret_cast<const __m128i*>(input_bytes); + const __m128i* input_mm = reinterpret_cast<const __m128i*>(input); for(size_t i = 0; i != blocks; ++i) { @@ -178,16 +178,16 @@ void SHA_160_SSE2::compress_n(const byte input_bytes[], size_t blocks) v4si P0, P1, P2, P3; - __m128i W0 = _mm_loadu_si128(&input[0]); + __m128i W0 = _mm_loadu_si128(&input_mm[0]); prep00_15(P0, W0); - __m128i W1 = _mm_loadu_si128(&input[1]); + __m128i W1 = _mm_loadu_si128(&input_mm[1]); prep00_15(P1, W1); - __m128i W2 = _mm_loadu_si128(&input[2]); + __m128i W2 = _mm_loadu_si128(&input_mm[2]); prep00_15(P2, W2); - __m128i W3 = _mm_loadu_si128(&input[3]); + __m128i W3 = _mm_loadu_si128(&input_mm[3]); prep00_15(P3, W3); /* @@ -316,13 +316,13 @@ void SHA_160_SSE2::compress_n(const byte input_bytes[], size_t blocks) F4(C, D, E, A, B, GET_P_32(P3, 2)); F4(B, C, D, E, A, GET_P_32(P3, 3)); - A = (m_digest[0] += A); - B = (m_digest[1] += B); - C = (m_digest[2] += C); - D = (m_digest[3] += D); - E = (m_digest[4] += E); + A = (digest[0] += A); + B = (digest[1] += B); + C = (digest[2] += C); + D = (digest[3] += D); + E = (digest[4] += E); - input += (hash_block_size() / 16); + input_mm += (64 / 16); } #undef GET_P_32 diff --git a/src/lib/hash/sha1_sse2/sha1_sse2.h b/src/lib/hash/sha1_sse2/sha1_sse2.h deleted file mode 100644 index a38600762..000000000 --- a/src/lib/hash/sha1_sse2/sha1_sse2.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -* SHA-160 -* (C) 1999-2007 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_SHA_160_SSE2_H__ -#define BOTAN_SHA_160_SSE2_H__ - -#include <botan/sha160.h> - -namespace Botan { - -/** -* SHA-160 using SSE2 for the message expansion -*/ -class BOTAN_DLL SHA_160_SSE2 final : public SHA_160 - { - public: - HashFunction* clone() const override { return new SHA_160_SSE2; } - SHA_160_SSE2() : SHA_160(0) {} // no W needed - private: - void compress_n(const byte[], size_t blocks) override; - }; - -} - -#endif |