aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/hash
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2016-09-17 14:20:03 -0400
committerJack Lloyd <[email protected]>2016-09-17 14:20:03 -0400
commitd734198074d0290631e27d1fb27dce45f0676d58 (patch)
treefaed46aaae836e44e972a4e6d5bdea06b0729034 /src/lib/hash
parent272fcf00572432f64085b10132e364740d7eb093 (diff)
parent2b7f2d52d032ad56526d38e7f65bd966ac59325a (diff)
Merge GH #623 Merge algorithm impl types
If CPU specific optimizations are available they are always used, without requiring the application to use the application registry to ensure they get the optimal type. See also GH #477
Diffstat (limited to 'src/lib/hash')
-rw-r--r--src/lib/hash/hash.cpp9
-rw-r--r--src/lib/hash/hash.h6
-rw-r--r--src/lib/hash/sha1/sha160.cpp11
-rw-r--r--src/lib/hash/sha1/sha160.h33
-rw-r--r--src/lib/hash/sha1_sse2/info.txt2
-rw-r--r--src/lib/hash/sha1_sse2/sha1_sse2.cpp38
-rw-r--r--src/lib/hash/sha1_sse2/sha1_sse2.h29
7 files changed, 53 insertions, 75 deletions
diff --git a/src/lib/hash/hash.cpp b/src/lib/hash/hash.cpp
index 42a7666b6..5a31763d1 100644
--- a/src/lib/hash/hash.cpp
+++ b/src/lib/hash/hash.cpp
@@ -45,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
@@ -155,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/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