From d4647b485a4123f0163ae541439af3f21e26b101 Mon Sep 17 00:00:00 2001 From: lloyd Date: Sun, 28 Sep 2008 23:42:02 +0000 Subject: Add modebase module (and add dep in cipher mode impls). Move data_snk.cpp to filters --- src/modes/cbc/modinfo.txt | 5 ++++ src/modes/cfb/modinfo.txt | 5 ++++ src/modes/ctr/modinfo.txt | 5 ++++ src/modes/cts/modinfo.txt | 5 ++++ src/modes/eax/modinfo.txt | 5 ++++ src/modes/ecb/modinfo.txt | 5 ++++ src/modes/modebase/modebase.cpp | 53 +++++++++++++++++++++++++++++++++++++++++ src/modes/modebase/modebase.h | 35 +++++++++++++++++++++++++++ src/modes/ofb/modinfo.txt | 5 ++++ 9 files changed, 123 insertions(+) create mode 100644 src/modes/modebase/modebase.cpp create mode 100644 src/modes/modebase/modebase.h (limited to 'src/modes') diff --git a/src/modes/cbc/modinfo.txt b/src/modes/cbc/modinfo.txt index 4ebc7730a..4c975eb9f 100644 --- a/src/modes/cbc/modinfo.txt +++ b/src/modes/cbc/modinfo.txt @@ -8,3 +8,8 @@ load_on auto cbc.cpp cbc.h + + +modebase + + diff --git a/src/modes/cfb/modinfo.txt b/src/modes/cfb/modinfo.txt index edce3a7e3..0a1af3d15 100644 --- a/src/modes/cfb/modinfo.txt +++ b/src/modes/cfb/modinfo.txt @@ -8,3 +8,8 @@ load_on auto cfb.cpp cfb.h + + +modebase + + diff --git a/src/modes/ctr/modinfo.txt b/src/modes/ctr/modinfo.txt index 912b80f51..8772f178f 100644 --- a/src/modes/ctr/modinfo.txt +++ b/src/modes/ctr/modinfo.txt @@ -8,3 +8,8 @@ load_on auto ctr.cpp ctr.h + + +modebase + + diff --git a/src/modes/cts/modinfo.txt b/src/modes/cts/modinfo.txt index 9f36221b4..83bbd8302 100644 --- a/src/modes/cts/modinfo.txt +++ b/src/modes/cts/modinfo.txt @@ -8,3 +8,8 @@ load_on auto cts.cpp cts.h + + +modebase + + diff --git a/src/modes/eax/modinfo.txt b/src/modes/eax/modinfo.txt index 7d5f7e98c..f31dd378e 100644 --- a/src/modes/eax/modinfo.txt +++ b/src/modes/eax/modinfo.txt @@ -9,3 +9,8 @@ eax.cpp eax.h eax_dec.cpp + + +modebase + + diff --git a/src/modes/ecb/modinfo.txt b/src/modes/ecb/modinfo.txt index fb0c8b7c6..039a83739 100644 --- a/src/modes/ecb/modinfo.txt +++ b/src/modes/ecb/modinfo.txt @@ -8,3 +8,8 @@ load_on auto ecb.cpp ecb.h + + +modebase + + diff --git a/src/modes/modebase/modebase.cpp b/src/modes/modebase/modebase.cpp new file mode 100644 index 000000000..6661d9f33 --- /dev/null +++ b/src/modes/modebase/modebase.cpp @@ -0,0 +1,53 @@ +/************************************************* +* Block Cipher Mode Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include +#include + +namespace Botan { + +/************************************************* +* Block Cipher Mode Constructor * +*************************************************/ +BlockCipherMode::BlockCipherMode(const std::string& cipher_name, + const std::string& cipher_mode_name, + u32bit iv_size, u32bit iv_meth, + u32bit buf_mult) : + BLOCK_SIZE(block_size_of(cipher_name)), BUFFER_SIZE(buf_mult * BLOCK_SIZE), + IV_METHOD(iv_meth), mode_name(cipher_mode_name) + { + base_ptr = cipher = get_block_cipher(cipher_name); + buffer.create(BUFFER_SIZE); + state.create(iv_size); + position = 0; + } + +/************************************************* +* Return the name of this type * +*************************************************/ +std::string BlockCipherMode::name() const + { + return (cipher->name() + "/" + mode_name); + } + +/************************************************* +* Set the IV * +*************************************************/ +void BlockCipherMode::set_iv(const InitializationVector& new_iv) + { + if(new_iv.length() != state.size()) + throw Invalid_IV_Length(name(), new_iv.length()); + + state = new_iv.bits_of(); + buffer.clear(); + position = 0; + + if(IV_METHOD == 1) + cipher->encrypt(state, buffer); + else if(IV_METHOD == 2) + cipher->encrypt(state); + } + +} diff --git a/src/modes/modebase/modebase.h b/src/modes/modebase/modebase.h new file mode 100644 index 000000000..90ab277b2 --- /dev/null +++ b/src/modes/modebase/modebase.h @@ -0,0 +1,35 @@ +/************************************************* +* Block Cipher Mode Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_MODEBASE_H__ +#define BOTAN_MODEBASE_H__ + +#include + +namespace Botan { + +/************************************************* +* Block Cipher Mode * +*************************************************/ +class BOTAN_DLL BlockCipherMode : public Keyed_Filter + { + public: + std::string name() const; + + BlockCipherMode(const std::string&, const std::string&, + u32bit, u32bit = 0, u32bit = 1); + virtual ~BlockCipherMode() { delete cipher; } + protected: + void set_iv(const InitializationVector&); + const u32bit BLOCK_SIZE, BUFFER_SIZE, IV_METHOD; + const std::string mode_name; + BlockCipher* cipher; + SecureVector buffer, state; + u32bit position; + }; + +} + +#endif diff --git a/src/modes/ofb/modinfo.txt b/src/modes/ofb/modinfo.txt index 38a15d0b6..ab4aa823a 100644 --- a/src/modes/ofb/modinfo.txt +++ b/src/modes/ofb/modinfo.txt @@ -8,3 +8,8 @@ load_on auto ofb.cpp ofb.h + + +modebase + + -- cgit v1.2.3