diff options
-rw-r--r-- | doc/log.txt | 1 | ||||
-rw-r--r-- | src/engine/def_engine/lookup_hash.cpp | 15 | ||||
-rw-r--r-- | src/hash/comb4p/comb4p.cpp | 104 | ||||
-rw-r--r-- | src/hash/comb4p/comb4p.h | 47 | ||||
-rw-r--r-- | src/hash/comb4p/info.txt | 1 |
5 files changed, 168 insertions, 0 deletions
diff --git a/doc/log.txt b/doc/log.txt index 80b0d88d6..ac37b7e70 100644 --- a/doc/log.txt +++ b/doc/log.txt @@ -1,5 +1,6 @@ * 1.9.7-dev, ????-??-?? + - Add Comb4P hash combiner function * 1.9.6, 2010-04-09 - TLS: Add support for TLS v1.1 diff --git a/src/engine/def_engine/lookup_hash.cpp b/src/engine/def_engine/lookup_hash.cpp index 4ce7915d4..1d96d4f3f 100644 --- a/src/engine/def_engine/lookup_hash.cpp +++ b/src/engine/def_engine/lookup_hash.cpp @@ -83,6 +83,10 @@ #include <botan/par_hash.h> #endif +#if defined(BOTAN_HAS_COMB4P) + #include <botan/comb4p.h> +#endif + namespace Botan { /* @@ -185,6 +189,17 @@ Default_Engine::find_hash(const SCAN_Name& request, return new Whirlpool; #endif +#if defined(BOTAN_HAS_COMB4P) + if(request.algo_name() == "Comb4P" && request.arg_count() == 2) + { + const HashFunction* h1 = af.prototype_hash_function(request.arg(0)); + const HashFunction* h2 = af.prototype_hash_function(request.arg(1)); + + if(h1 && h2) + return new Comb4P(h1->clone(), h2->clone()); + } +#endif + #if defined(BOTAN_HAS_PARALLEL_HASH) if(request.algo_name() == "Parallel") diff --git a/src/hash/comb4p/comb4p.cpp b/src/hash/comb4p/comb4p.cpp new file mode 100644 index 000000000..ff6cb43a9 --- /dev/null +++ b/src/hash/comb4p/comb4p.cpp @@ -0,0 +1,104 @@ +/** +* 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 0 + if(hash1->OUTPUT_LENGTH != hash2->OUTPUT_LENGTH) + throw std::invalid_argument("Comb4P: Incompatible hashes " + + hash1->name() + " and " + + hash2->name()); +#endif + + 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 |