diff options
author | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 19:29:24 +0000 |
commit | 9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch) | |
tree | fe5e8ae9813b853549558b59833022e87e83981b /src/modes/ctr | |
parent | 9822a701516396b7de4e41339faecd48ff8dc8ff (diff) |
Move all modules into src/ directory
Diffstat (limited to 'src/modes/ctr')
-rw-r--r-- | src/modes/ctr/ctr.cpp | 74 | ||||
-rw-r--r-- | src/modes/ctr/ctr.h | 29 | ||||
-rw-r--r-- | src/modes/ctr/modinfo.txt | 10 |
3 files changed, 113 insertions, 0 deletions
diff --git a/src/modes/ctr/ctr.cpp b/src/modes/ctr/ctr.cpp new file mode 100644 index 000000000..8b8c5f35f --- /dev/null +++ b/src/modes/ctr/ctr.cpp @@ -0,0 +1,74 @@ +/************************************************* +* CTR Mode Source File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#include <botan/ctr.h> +#include <botan/lookup.h> +#include <botan/xor_buf.h> +#include <algorithm> + +namespace Botan { + +/************************************************* +* CTR-BE Constructor * +*************************************************/ +CTR_BE::CTR_BE(const std::string& cipher_name) : + BlockCipherMode(cipher_name, "CTR-BE", block_size_of(cipher_name), 1) + { + } + +/************************************************* +* CTR-BE Constructor * +*************************************************/ +CTR_BE::CTR_BE(const std::string& cipher_name, const SymmetricKey& key, + const InitializationVector& iv) : + BlockCipherMode(cipher_name, "CTR-BE", block_size_of(cipher_name), 1) + { + set_key(key); + set_iv(iv); + } + +/************************************************* +* CTR-BE Encryption/Decryption * +*************************************************/ +void CTR_BE::write(const byte input[], u32bit length) + { + u32bit copied = std::min(BLOCK_SIZE - position, length); + xor_buf(buffer + position, input, copied); + send(buffer + position, copied); + input += copied; + length -= copied; + position += copied; + + if(position == BLOCK_SIZE) + increment_counter(); + + while(length >= BLOCK_SIZE) + { + xor_buf(buffer, input, BLOCK_SIZE); + send(buffer, BLOCK_SIZE); + + input += BLOCK_SIZE; + length -= BLOCK_SIZE; + increment_counter(); + } + + xor_buf(buffer + position, input, length); + send(buffer + position, length); + position += length; + } + +/************************************************* +* Increment the counter and update the buffer * +*************************************************/ +void CTR_BE::increment_counter() + { + for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j) + if(++state[j]) + break; + cipher->encrypt(state, buffer); + position = 0; + } + +} diff --git a/src/modes/ctr/ctr.h b/src/modes/ctr/ctr.h new file mode 100644 index 000000000..c3217a5d1 --- /dev/null +++ b/src/modes/ctr/ctr.h @@ -0,0 +1,29 @@ +/************************************************* +* CTR Mode Header File * +* (C) 1999-2007 Jack Lloyd * +*************************************************/ + +#ifndef BOTAN_CTR_H__ +#define BOTAN_CTR_H__ + +#include <botan/modebase.h> + +namespace Botan { + +/************************************************* +* CTR-BE Mode * +*************************************************/ +class BOTAN_DLL CTR_BE : public BlockCipherMode + { + public: + CTR_BE(const std::string&); + CTR_BE(const std::string&, + const SymmetricKey&, const InitializationVector&); + private: + void write(const byte[], u32bit); + void increment_counter(); + }; + +} + +#endif diff --git a/src/modes/ctr/modinfo.txt b/src/modes/ctr/modinfo.txt new file mode 100644 index 000000000..912b80f51 --- /dev/null +++ b/src/modes/ctr/modinfo.txt @@ -0,0 +1,10 @@ +realname "CTR block cipher mode" + +define CTR + +load_on auto + +<add> +ctr.cpp +ctr.h +</add> |