aboutsummaryrefslogtreecommitdiffstats
path: root/src/hash
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-04-28 22:31:20 +0000
committerlloyd <[email protected]>2010-04-28 22:31:20 +0000
commit0f47cb60e703ca2de56286f07b4c9d91c7bba071 (patch)
tree5304fd84516c79f55e213755b2b2b99e6e65c9e0 /src/hash
parent6eec50d372143afcb3188f21d0991ace3e0d5e9e (diff)
parent50fc7b15553d888d95bee72972e53eae27a82c1f (diff)
propagate from branch 'net.randombit.botan' (head a5f25a3b954f24c5d07fa0dab6c4d76f63767165)
to branch 'net.randombit.botan.c++0x' (head a365694b70b4b84ca713272d56d496acca351cb5)
Diffstat (limited to 'src/hash')
-rw-r--r--src/hash/comb4p/comb4p.cpp105
-rw-r--r--src/hash/comb4p/comb4p.h47
-rw-r--r--src/hash/comb4p/info.txt1
-rw-r--r--src/hash/sha1_sse2/sha1_sse2.cpp2
4 files changed, 154 insertions, 1 deletions
diff --git a/src/hash/comb4p/comb4p.cpp b/src/hash/comb4p/comb4p.cpp
new file mode 100644
index 000000000..6ae36b9d3
--- /dev/null
+++ b/src/hash/comb4p/comb4p.cpp
@@ -0,0 +1,105 @@
+/**
+* Comb4P hash combiner
+* (C) 2010 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/comb4p.h>
+#include <botan/internal/xor_buf.h>
+#include <stdexcept>
+
+namespace Botan {
+
+namespace {
+
+u32bit comb4p_block_size(const HashFunction* h1,
+ const HashFunction* h2)
+ {
+ if(h1->HASH_BLOCK_SIZE == h2->HASH_BLOCK_SIZE)
+ return h1->HASH_BLOCK_SIZE;
+
+ /*
+ * Return LCM of the block sizes? This would probably be OK for
+ * HMAC, which is the main thing relying on knowing the block size.
+ */
+ return 0;
+ }
+
+void comb4p_round(MemoryRegion<byte>& out,
+ const MemoryRegion<byte>& in,
+ byte round_no,
+ HashFunction* h1,
+ HashFunction* h2)
+ {
+ h1->update(round_no);
+ h2->update(round_no);
+
+ h1->update(&in[0], in.size());
+ h2->update(&in[0], in.size());
+
+ SecureVector<byte> h_buf = h1->final();
+ xor_buf(&out[0], &h_buf[0], std::min(out.size(), h_buf.size()));
+
+ h_buf = h2->final();
+ xor_buf(&out[0], &h_buf[0], std::min(out.size(), h_buf.size()));
+ }
+
+}
+
+Comb4P::Comb4P(HashFunction* h1, HashFunction* h2) :
+ HashFunction(h1->OUTPUT_LENGTH + h2->OUTPUT_LENGTH,
+ comb4p_block_size(h1, h2)),
+ hash1(h1), hash2(h2)
+ {
+ if(hash1->name() == hash2->name())
+ throw std::invalid_argument("Comb4P: Must use two distinct hashes");
+
+ if(hash1->OUTPUT_LENGTH != hash2->OUTPUT_LENGTH)
+ throw std::invalid_argument("Comb4P: Incompatible hashes " +
+ hash1->name() + " and " +
+ hash2->name());
+
+ clear();
+ }
+
+void Comb4P::clear()
+ {
+ hash1->clear();
+ hash2->clear();
+
+ // Prep for processing next message, if any
+ hash1->update(0);
+ hash2->update(0);
+ }
+
+void Comb4P::add_data(const byte input[], u32bit length)
+ {
+ hash1->update(input, length);
+ hash2->update(input, length);
+ }
+
+void Comb4P::final_result(byte out[])
+ {
+ SecureVector<byte> h1 = hash1->final();
+ SecureVector<byte> h2 = hash2->final();
+
+ // First round
+ xor_buf(&h1[0], &h2[0], std::min(h1.size(), h2.size()));
+
+ // Second round
+ comb4p_round(h2, h1, 1, hash1, hash2);
+
+ // Third round
+ comb4p_round(h1, h2, 2, hash1, hash2);
+
+ copy_mem(out , &h1[0], h1.size());
+ copy_mem(out + h1.size(), &h2[0], h2.size());
+
+ // Prep for processing next message, if any
+ hash1->update(0);
+ hash2->update(0);
+ }
+
+}
+
diff --git a/src/hash/comb4p/comb4p.h b/src/hash/comb4p/comb4p.h
new file mode 100644
index 000000000..ce66bb9c9
--- /dev/null
+++ b/src/hash/comb4p/comb4p.h
@@ -0,0 +1,47 @@
+/**
+* Comb4P hash combiner
+* (C) 2010 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_COMB4P_H__
+#define BOTAN_COMB4P_H__
+
+#include <botan/hash.h>
+
+namespace Botan {
+
+/**
+* Combines two hash functions using a Feistel scheme. Described in
+* "On the Security of Hash Function Combiners", Anja Lehmann
+*/
+class Comb4P : public HashFunction
+ {
+ public:
+ Comb4P(HashFunction* h1, HashFunction* h2);
+
+ ~Comb4P() { delete hash1; delete hash2; }
+
+ HashFunction* clone() const
+ {
+ return new Comb4P(hash1->clone(), hash2->clone());
+ }
+
+ std::string name() const
+ {
+ return "Comb4P(" + hash1->name() + "," + hash2->name() + ")";
+ }
+
+ void clear();
+ private:
+ void add_data(const byte input[], u32bit length);
+ void final_result(byte out[]);
+
+ HashFunction* hash1;
+ HashFunction* hash2;
+ };
+
+}
+
+#endif
diff --git a/src/hash/comb4p/info.txt b/src/hash/comb4p/info.txt
new file mode 100644
index 000000000..9ccf0223a
--- /dev/null
+++ b/src/hash/comb4p/info.txt
@@ -0,0 +1 @@
+define COMB4P
diff --git a/src/hash/sha1_sse2/sha1_sse2.cpp b/src/hash/sha1_sse2/sha1_sse2.cpp
index 9267689e7..00a0752f6 100644
--- a/src/hash/sha1_sse2/sha1_sse2.cpp
+++ b/src/hash/sha1_sse2/sha1_sse2.cpp
@@ -159,7 +159,7 @@ void SHA_160_SSE2::compress_n(const byte input_bytes[], u32bit blocks)
u32bit A = digest[0], B = digest[1], C = digest[2],
D = digest[3], E = digest[4];
- const __m128i* input = (const __m128i *)input_bytes;
+ const __m128i* input = reinterpret_cast<const __m128i*>(input_bytes);
for(u32bit i = 0; i != blocks; ++i)
{