From eb8fd42d6aa3ed267c7444b114e02e64a567ca00 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 28 Sep 2008 20:03:58 +0000 Subject: Modularize MGF1. Make EME1 and EMSA4 depend on it --- src/kdf/mgf1/mgf1.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/kdf/mgf1/mgf1.h | 33 +++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/kdf/mgf1/mgf1.cpp create mode 100644 src/kdf/mgf1/mgf1.h (limited to 'src/kdf/mgf1') diff --git a/src/kdf/mgf1/mgf1.cpp b/src/kdf/mgf1/mgf1.cpp new file mode 100644 index 000000000..c2cda7f4c --- /dev/null +++ b/src/kdf/mgf1/mgf1.cpp @@ -0,0 +1,55 @@ +/************************************************* +* MGF1 Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include +#include +#include +#include +#include + +namespace Botan { + +/************************************************* +* MGF1 Mask Generation Function * +*************************************************/ +void MGF1::mask(const byte in[], u32bit in_len, byte out[], + u32bit out_len) const + { + u32bit counter = 0; + + while(out_len) + { + hash->update(in, in_len); + for(u32bit j = 0; j != 4; ++j) + hash->update(get_byte(j, counter)); + SecureVector buffer = hash->final(); + + u32bit xored = std::min(buffer.size(), out_len); + xor_buf(out, buffer.begin(), xored); + out += xored; + out_len -= xored; + + ++counter; + } + } + +/************************************************* +* MGF1 Constructor * +*************************************************/ +MGF1::MGF1(HashFunction* h) : hash(h) + { + if(!hash) + throw Invalid_Argument("MGF1 given null hash object"); + } + +/************************************************* +* MGF1 Destructor * +*************************************************/ +MGF1::~MGF1() + { + delete hash; + } + +} diff --git a/src/kdf/mgf1/mgf1.h b/src/kdf/mgf1/mgf1.h new file mode 100644 index 000000000..c235821bf --- /dev/null +++ b/src/kdf/mgf1/mgf1.h @@ -0,0 +1,33 @@ +/************************************************* +* MGF1 Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_MGF1_H__ +#define BOTAN_MGF1_H__ + +#include + +namespace Botan { + +/************************************************* +* MGF1 (Mask Generation Function) * +*************************************************/ +class BOTAN_DLL MGF1 : public MGF + { + public: + void mask(const byte[], u32bit, byte[], u32bit) const; + + /** + MGF1 constructor: takes ownership of hash + */ + MGF1(HashFunction* hash); + + ~MGF1(); + private: + HashFunction* hash; + }; + +} + +#endif -- cgit v1.2.3