aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/hash/comb4p
diff options
context:
space:
mode:
authorlloyd <[email protected]>2014-01-10 03:41:59 +0000
committerlloyd <[email protected]>2014-01-10 03:41:59 +0000
commit6894dca64c04936d07048c0e8cbf7e25858548c3 (patch)
tree5d572bfde9fe667dab14e3f04b5285a85d8acd95 /src/lib/hash/comb4p
parent9efa3be92442afb3d0b69890a36c7f122df18eda (diff)
Move lib into src
Diffstat (limited to 'src/lib/hash/comb4p')
-rw-r--r--src/lib/hash/comb4p/comb4p.cpp102
-rw-r--r--src/lib/hash/comb4p/comb4p.h61
-rw-r--r--src/lib/hash/comb4p/info.txt1
3 files changed, 164 insertions, 0 deletions
diff --git a/src/lib/hash/comb4p/comb4p.cpp b/src/lib/hash/comb4p/comb4p.cpp
new file mode 100644
index 000000000..7aec5972e
--- /dev/null
+++ b/src/lib/hash/comb4p/comb4p.cpp
@@ -0,0 +1,102 @@
+/*
+* 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 {
+
+void comb4p_round(secure_vector<byte>& out,
+ const secure_vector<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());
+
+ secure_vector<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) :
+ 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();
+ }
+
+size_t Comb4P::hash_block_size() const
+ {
+ if(hash1->hash_block_size() == hash2->hash_block_size())
+ return hash1->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::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[], size_t length)
+ {
+ hash1->update(input, length);
+ hash2->update(input, length);
+ }
+
+void Comb4P::final_result(byte out[])
+ {
+ secure_vector<byte> h1 = hash1->final();
+ secure_vector<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/lib/hash/comb4p/comb4p.h b/src/lib/hash/comb4p/comb4p.h
new file mode 100644
index 000000000..e0cffc22b
--- /dev/null
+++ b/src/lib/hash/comb4p/comb4p.h
@@ -0,0 +1,61 @@
+/*
+* 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 BOTAN_DLL Comb4P : public HashFunction
+ {
+ public:
+ /**
+ * @param h1 the first hash
+ * @param h2 the second hash
+ */
+ Comb4P(HashFunction* h1, HashFunction* h2);
+
+ Comb4P(const Comb4P&) = delete;
+ Comb4P& operator=(const Comb4P&) = delete;
+
+ ~Comb4P() { delete hash1; delete hash2; }
+
+ size_t hash_block_size() const;
+
+ size_t output_length() const
+ {
+ return hash1->output_length() + hash2->output_length();
+ }
+
+ 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[], size_t length);
+ void final_result(byte out[]);
+
+ HashFunction* hash1;
+ HashFunction* hash2;
+ };
+
+}
+
+#endif
diff --git a/src/lib/hash/comb4p/info.txt b/src/lib/hash/comb4p/info.txt
new file mode 100644
index 000000000..375895610
--- /dev/null
+++ b/src/lib/hash/comb4p/info.txt
@@ -0,0 +1 @@
+define COMB4P 20131128