From 9bcfe627321ddc81691b835dffaa6324ac4684a4 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 28 Sep 2008 19:29:24 +0000 Subject: Move all modules into src/ directory --- src/pk_pad/emsa2/emsa2.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++ src/pk_pad/emsa2/emsa2.h | 38 ++++++++++++++++ src/pk_pad/emsa2/modinfo.txt | 10 ++++ 3 files changed, 154 insertions(+) create mode 100644 src/pk_pad/emsa2/emsa2.cpp create mode 100644 src/pk_pad/emsa2/emsa2.h create mode 100644 src/pk_pad/emsa2/modinfo.txt (limited to 'src/pk_pad/emsa2') diff --git a/src/pk_pad/emsa2/emsa2.cpp b/src/pk_pad/emsa2/emsa2.cpp new file mode 100644 index 000000000..24292d390 --- /dev/null +++ b/src/pk_pad/emsa2/emsa2.cpp @@ -0,0 +1,106 @@ +/************************************************* +* EMSA2 Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include +#include +#include + +namespace Botan { + +namespace { + +/************************************************* +* EMSA2 Encode Operation * +*************************************************/ +SecureVector emsa2_encoding(const MemoryRegion& msg, + u32bit output_bits, + const MemoryRegion& empty_hash, + byte hash_id) + { + const u32bit HASH_SIZE = empty_hash.size(); + + u32bit output_length = (output_bits + 1) / 8; + + if(msg.size() != HASH_SIZE) + throw Encoding_Error("EMSA2::encoding_of: Bad input length"); + if(output_length < HASH_SIZE + 4) + throw Encoding_Error("EMSA2::encoding_of: Output length is too small"); + + bool empty = true; + for(u32bit j = 0; j != HASH_SIZE; ++j) + if(empty_hash[j] != msg[j]) + empty = false; + + SecureVector output(output_length); + + output[0] = (empty ? 0x4B : 0x6B); + output[output_length - 3 - HASH_SIZE] = 0xBA; + set_mem(output + 1, output_length - 4 - HASH_SIZE, 0xBB); + output.copy(output_length - (HASH_SIZE + 2), msg, msg.size()); + output[output_length-2] = hash_id; + output[output_length-1] = 0xCC; + + return output; + } + +} + +/************************************************* +* EMSA2 Update Operation * +*************************************************/ +void EMSA2::update(const byte input[], u32bit length) + { + hash->update(input, length); + } + +/************************************************* +* Return the raw (unencoded) data * +*************************************************/ +SecureVector EMSA2::raw_data() + { + return hash->final(); + } + +/************************************************* +* EMSA2 Encode Operation * +*************************************************/ +SecureVector EMSA2::encoding_of(const MemoryRegion& msg, + u32bit output_bits, + RandomNumberGenerator&) + { + return emsa2_encoding(msg, output_bits, empty_hash, hash_id); + } + +/************************************************* +* EMSA2 Verify Operation * +*************************************************/ +bool EMSA2::verify(const MemoryRegion& coded, + const MemoryRegion& raw, + u32bit key_bits) throw() + { + try + { + return (coded == emsa2_encoding(raw, key_bits, + empty_hash, hash_id)); + } + catch(...) + { + return false; + } + } + +/************************************************* +* EMSA2 Constructor * +*************************************************/ +EMSA2::EMSA2(const std::string& hash_name) + { + hash_id = ieee1363_hash_id(hash_name); + if(hash_id == 0) + throw Encoding_Error("EMSA2 cannot be used with " + hash->name()); + hash = get_hash(hash_name); + empty_hash = hash->final(); + } + +} diff --git a/src/pk_pad/emsa2/emsa2.h b/src/pk_pad/emsa2/emsa2.h new file mode 100644 index 000000000..5db9593f8 --- /dev/null +++ b/src/pk_pad/emsa2/emsa2.h @@ -0,0 +1,38 @@ +/************************************************* +* EMSA2 Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_EMSA2_H__ +#define BOTAN_EMSA2_H__ + +#include + +namespace Botan { + +/************************************************* +* EMSA2 * +*************************************************/ +class BOTAN_DLL EMSA2 : public EMSA + { + public: + EMSA2(const std::string&); + ~EMSA2() { delete hash; } + private: + void update(const byte[], u32bit); + SecureVector raw_data(); + + SecureVector encoding_of(const MemoryRegion&, u32bit, + RandomNumberGenerator& rng); + + bool verify(const MemoryRegion&, const MemoryRegion&, + u32bit) throw(); + + SecureVector empty_hash; + HashFunction* hash; + byte hash_id; + }; + +} + +#endif diff --git a/src/pk_pad/emsa2/modinfo.txt b/src/pk_pad/emsa2/modinfo.txt new file mode 100644 index 000000000..8d14da548 --- /dev/null +++ b/src/pk_pad/emsa2/modinfo.txt @@ -0,0 +1,10 @@ +realname "EMSA2" + +define EMSA2 + +load_on auto + + +emsa2.h +emsa2.cpp + -- cgit v1.2.3