diff options
author | lloyd <[email protected]> | 2008-09-28 18:15:52 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-28 18:15:52 +0000 |
commit | 983a8ecb42e844f89466d0ae52bba591d4fc4275 (patch) | |
tree | de01378ee9be0ba8bec28bc08d3bde6e422ff898 /src | |
parent | d59f6a2c9e797ee22c21b2d85c662e0fe8d1cf35 (diff) |
Modularize cipher modes
Diffstat (limited to 'src')
-rw-r--r-- | src/cbc.cpp | 157 | ||||
-rw-r--r-- | src/cfb.cpp | 142 | ||||
-rw-r--r-- | src/ctr.cpp | 74 | ||||
-rw-r--r-- | src/cts.cpp | 175 | ||||
-rw-r--r-- | src/def_mode.cpp | 68 | ||||
-rw-r--r-- | src/eax.cpp | 194 | ||||
-rw-r--r-- | src/eax_dec.cpp | 126 | ||||
-rw-r--r-- | src/ecb.cpp | 152 | ||||
-rw-r--r-- | src/ofb.cpp | 65 |
9 files changed, 61 insertions, 1092 deletions
diff --git a/src/cbc.cpp b/src/cbc.cpp deleted file mode 100644 index 9ad598bed..000000000 --- a/src/cbc.cpp +++ /dev/null @@ -1,157 +0,0 @@ -/************************************************* -* CBC Mode Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/cbc.h> -#include <botan/lookup.h> -#include <botan/xor_buf.h> -#include <algorithm> - -namespace Botan { - -/************************************************* -* CBC Encryption Constructor * -*************************************************/ -CBC_Encryption::CBC_Encryption(const std::string& cipher_name, - const std::string& padding_name) : - BlockCipherMode(cipher_name, "CBC", block_size_of(cipher_name)), - padder(get_bc_pad(padding_name)) - { - if(!padder->valid_blocksize(BLOCK_SIZE)) - throw Invalid_Block_Size(name(), padder->name()); - } - -/************************************************* -* CBC Encryption Constructor * -*************************************************/ -CBC_Encryption::CBC_Encryption(const std::string& cipher_name, - const std::string& padding_name, - const SymmetricKey& key, - const InitializationVector& iv) : - BlockCipherMode(cipher_name, "CBC", block_size_of(cipher_name)), - padder(get_bc_pad(padding_name)) - { - if(!padder->valid_blocksize(BLOCK_SIZE)) - throw Invalid_Block_Size(name(), padder->name()); - set_key(key); - set_iv(iv); - } - -/************************************************* -* Encrypt in CBC mode * -*************************************************/ -void CBC_Encryption::write(const byte input[], u32bit length) - { - while(length) - { - u32bit xored = std::min(BLOCK_SIZE - position, length); - xor_buf(state + position, input, xored); - input += xored; - length -= xored; - position += xored; - if(position == BLOCK_SIZE) - { - cipher->encrypt(state); - send(state, BLOCK_SIZE); - position = 0; - } - } - } - -/************************************************* -* Finish encrypting in CBC mode * -*************************************************/ -void CBC_Encryption::end_msg() - { - SecureVector<byte> padding(BLOCK_SIZE); - padder->pad(padding, padding.size(), position); - write(padding, padder->pad_bytes(BLOCK_SIZE, position)); - if(position != 0) - throw Exception(name() + ": Did not pad to full blocksize"); - } - -/************************************************* -* Return a CBC mode name * -*************************************************/ -std::string CBC_Encryption::name() const - { - return (cipher->name() + "/" + mode_name + "/" + padder->name()); - } - -/************************************************* -* CBC Decryption Constructor * -*************************************************/ -CBC_Decryption::CBC_Decryption(const std::string& cipher_name, - const std::string& padding_name) : - BlockCipherMode(cipher_name, "CBC", block_size_of(cipher_name)), - padder(get_bc_pad(padding_name)) - { - if(!padder->valid_blocksize(BLOCK_SIZE)) - throw Invalid_Block_Size(name(), padder->name()); - temp.create(BLOCK_SIZE); - } - -/************************************************* -* CBC Decryption Constructor * -*************************************************/ -CBC_Decryption::CBC_Decryption(const std::string& cipher_name, - const std::string& padding_name, - const SymmetricKey& key, - const InitializationVector& iv) : - BlockCipherMode(cipher_name, "CBC", block_size_of(cipher_name)), - padder(get_bc_pad(padding_name)) - { - if(!padder->valid_blocksize(BLOCK_SIZE)) - throw Invalid_Block_Size(name(), padder->name()); - temp.create(BLOCK_SIZE); - set_key(key); - set_iv(iv); - } - -/************************************************* -* Decrypt in CBC mode * -*************************************************/ -void CBC_Decryption::write(const byte input[], u32bit length) - { - while(length) - { - if(position == BLOCK_SIZE) - { - cipher->decrypt(buffer, temp); - xor_buf(temp, state, BLOCK_SIZE); - send(temp, BLOCK_SIZE); - state = buffer; - position = 0; - } - u32bit added = std::min(BLOCK_SIZE - position, length); - buffer.copy(position, input, added); - input += added; - length -= added; - position += added; - } - } - -/************************************************* -* Finish decrypting in CBC mode * -*************************************************/ -void CBC_Decryption::end_msg() - { - if(position != BLOCK_SIZE) - throw Decoding_Error(name()); - cipher->decrypt(buffer, temp); - xor_buf(temp, state, BLOCK_SIZE); - send(temp, padder->unpad(temp, BLOCK_SIZE)); - state = buffer; - position = 0; - } - -/************************************************* -* Return a CBC mode name * -*************************************************/ -std::string CBC_Decryption::name() const - { - return (cipher->name() + "/" + mode_name + "/" + padder->name()); - } - -} diff --git a/src/cfb.cpp b/src/cfb.cpp deleted file mode 100644 index dbfbff6ae..000000000 --- a/src/cfb.cpp +++ /dev/null @@ -1,142 +0,0 @@ -/************************************************* -* CFB Mode Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/cfb.h> -#include <botan/lookup.h> -#include <botan/parsing.h> -#include <botan/xor_buf.h> -#include <algorithm> - -namespace Botan { - -namespace { - -/************************************************* -* Check the feedback size * -*************************************************/ -void check_feedback(u32bit BLOCK_SIZE, u32bit FEEDBACK_SIZE, u32bit bits, - const std::string& name) - { - if(FEEDBACK_SIZE == 0 || FEEDBACK_SIZE > BLOCK_SIZE || bits % 8 != 0) - throw Invalid_Argument(name + ": Invalid feedback size " + - to_string(bits)); - } - -} - -/************************************************* -* CFB Encryption Constructor * -*************************************************/ -CFB_Encryption::CFB_Encryption(const std::string& cipher_name, - u32bit fback_bits) : - BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1), - FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE) - { - check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); - } - -/************************************************* -* CFB Encryption Constructor * -*************************************************/ -CFB_Encryption::CFB_Encryption(const std::string& cipher_name, - const SymmetricKey& key, - const InitializationVector& iv, - u32bit fback_bits) : - BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1), - FEEDBACK_SIZE(fback_bits ? fback_bits / 8: BLOCK_SIZE) - { - check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); - set_key(key); - set_iv(iv); - } - -/************************************************* -* Encrypt data in CFB mode * -*************************************************/ -void CFB_Encryption::write(const byte input[], u32bit length) - { - while(length) - { - u32bit xored = std::min(FEEDBACK_SIZE - position, length); - xor_buf(buffer + position, input, xored); - send(buffer + position, xored); - input += xored; - length -= xored; - position += xored; - if(position == FEEDBACK_SIZE) - feedback(); - } - } - -/************************************************* -* Do the feedback * -*************************************************/ -void CFB_Encryption::feedback() - { - for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j) - state[j] = state[j + FEEDBACK_SIZE]; - state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE); - cipher->encrypt(state, buffer); - position = 0; - } - -/************************************************* -* CFB Decryption Constructor * -*************************************************/ -CFB_Decryption::CFB_Decryption(const std::string& cipher_name, - u32bit fback_bits) : - BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1), - FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE) - { - check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); - } - -/************************************************* -* CFB Decryption Constructor * -*************************************************/ -CFB_Decryption::CFB_Decryption(const std::string& cipher_name, - const SymmetricKey& key, - const InitializationVector& iv, - u32bit fback_bits) : - BlockCipherMode(cipher_name, "CFB", block_size_of(cipher_name), 1), - FEEDBACK_SIZE(fback_bits ? fback_bits / 8 : BLOCK_SIZE) - { - check_feedback(BLOCK_SIZE, FEEDBACK_SIZE, fback_bits, name()); - set_key(key); - set_iv(iv); - } - -/************************************************* -* Decrypt data in CFB mode * -*************************************************/ -void CFB_Decryption::write(const byte input[], u32bit length) - { - while(length) - { - u32bit xored = std::min(FEEDBACK_SIZE - position, length); - xor_buf(buffer + position, input, xored); - send(buffer + position, xored); - buffer.copy(position, input, xored); - input += xored; - length -= xored; - position += xored; - if(position == FEEDBACK_SIZE) - feedback(); - } - } - -/************************************************* -* Do the feedback * -*************************************************/ -void CFB_Decryption::feedback() - { - for(u32bit j = 0; j != BLOCK_SIZE - FEEDBACK_SIZE; ++j) - state[j] = state[j + FEEDBACK_SIZE]; - state.copy(BLOCK_SIZE - FEEDBACK_SIZE, buffer, FEEDBACK_SIZE); - cipher->encrypt(state, buffer); - position = 0; - } - -} diff --git a/src/ctr.cpp b/src/ctr.cpp deleted file mode 100644 index 8b8c5f35f..000000000 --- a/src/ctr.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/************************************************* -* 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/cts.cpp b/src/cts.cpp deleted file mode 100644 index 8af775713..000000000 --- a/src/cts.cpp +++ /dev/null @@ -1,175 +0,0 @@ -/************************************************* -* CTS Mode Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/cts.h> -#include <botan/lookup.h> -#include <botan/xor_buf.h> -#include <algorithm> - -namespace Botan { - -/************************************************* -* CTS Encryption Constructor * -*************************************************/ -CTS_Encryption::CTS_Encryption(const std::string& cipher_name) : - BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2) - { - } - -/************************************************* -* CTS Encryption Constructor * -*************************************************/ -CTS_Encryption::CTS_Encryption(const std::string& cipher_name, - const SymmetricKey& key, - const InitializationVector& iv) : - BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2) - { - set_key(key); - set_iv(iv); - } - -/************************************************* -* Encrypt a block * -*************************************************/ -void CTS_Encryption::encrypt(const byte block[]) - { - xor_buf(state, block, BLOCK_SIZE); - cipher->encrypt(state); - send(state, BLOCK_SIZE); - } - -/************************************************* -* Encrypt in CTS mode * -*************************************************/ -void CTS_Encryption::write(const byte input[], u32bit length) - { - u32bit copied = std::min(BUFFER_SIZE - position, length); - buffer.copy(position, input, copied); - length -= copied; - input += copied; - position += copied; - - if(length == 0) return; - - encrypt(buffer); - if(length > BLOCK_SIZE) - { - encrypt(buffer + BLOCK_SIZE); - while(length > 2*BLOCK_SIZE) - { - encrypt(input); - length -= BLOCK_SIZE; - input += BLOCK_SIZE; - } - position = 0; - } - else - { - copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE); - position = BLOCK_SIZE; - } - buffer.copy(position, input, length); - position += length; - } - -/************************************************* -* Finish encrypting in CTS mode * -*************************************************/ -void CTS_Encryption::end_msg() - { - if(position < BLOCK_SIZE + 1) - throw Exception("CTS_Encryption: insufficient data to encrypt"); - xor_buf(state, buffer, BLOCK_SIZE); - cipher->encrypt(state); - SecureVector<byte> cn = state; - clear_mem(buffer + position, BUFFER_SIZE - position); - encrypt(buffer + BLOCK_SIZE); - send(cn, position - BLOCK_SIZE); - } - -/************************************************* -* CTS Decryption Constructor * -*************************************************/ -CTS_Decryption::CTS_Decryption(const std::string& cipher_name) : - BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2) - { - temp.create(BLOCK_SIZE); - } - -/************************************************* -* CTS Decryption Constructor * -*************************************************/ -CTS_Decryption::CTS_Decryption(const std::string& cipher_name, - const SymmetricKey& key, - const InitializationVector& iv) : - BlockCipherMode(cipher_name, "CTS", block_size_of(cipher_name), 0, 2) - { - temp.create(BLOCK_SIZE); - set_key(key); - set_iv(iv); - } - -/************************************************* -* Decrypt a block * -*************************************************/ -void CTS_Decryption::decrypt(const byte block[]) - { - cipher->decrypt(block, temp); - xor_buf(temp, state, BLOCK_SIZE); - send(temp, BLOCK_SIZE); - state.copy(block, BLOCK_SIZE); - } - -/************************************************* -* Decrypt in CTS mode * -*************************************************/ -void CTS_Decryption::write(const byte input[], u32bit length) - { - u32bit copied = std::min(BUFFER_SIZE - position, length); - buffer.copy(position, input, copied); - length -= copied; - input += copied; - position += copied; - - if(length == 0) return; - - decrypt(buffer); - if(length > BLOCK_SIZE) - { - decrypt(buffer + BLOCK_SIZE); - while(length > 2*BLOCK_SIZE) - { - decrypt(input); - length -= BLOCK_SIZE; - input += BLOCK_SIZE; - } - position = 0; - } - else - { - copy_mem(buffer.begin(), buffer + BLOCK_SIZE, BLOCK_SIZE); - position = BLOCK_SIZE; - } - buffer.copy(position, input, length); - position += length; - } - -/************************************************* -* Finish decrypting in CTS mode * -*************************************************/ -void CTS_Decryption::end_msg() - { - cipher->decrypt(buffer, temp); - xor_buf(temp, buffer + BLOCK_SIZE, position - BLOCK_SIZE); - SecureVector<byte> xn = temp; - copy_mem(buffer + position, xn + (position - BLOCK_SIZE), - BUFFER_SIZE - position); - cipher->decrypt(buffer + BLOCK_SIZE, temp); - xor_buf(temp, state, BLOCK_SIZE); - send(temp, BLOCK_SIZE); - send(xn, position - BLOCK_SIZE); - } - -} diff --git a/src/def_mode.cpp b/src/def_mode.cpp index 89600f695..0645364a0 100644 --- a/src/def_mode.cpp +++ b/src/def_mode.cpp @@ -7,13 +7,34 @@ #include <botan/parsing.h> #include <botan/filters.h> #include <botan/lookup.h> -#include <botan/ecb.h> -#include <botan/cbc.h> -#include <botan/cts.h> -#include <botan/cfb.h> -#include <botan/ofb.h> -#include <botan/ctr.h> -#include <botan/eax.h> + +#ifdef BOTAN_HAS_ECB + #include <botan/ecb.h> +#endif + +#ifdef BOTAN_HAS_CBC + #include <botan/cbc.h> +#endif + +#ifdef BOTAN_HAS_CTS + #include <botan/cts.h> +#endif + +#ifdef BOTAN_HAS_CFB + #include <botan/cfb.h> +#endif + +#ifdef BOTAN_HAS_OFB + #include <botan/ofb.h> +#endif + +#ifdef BOTAN_HAS_CTR + #include <botan/ctr.h> +#endif + +#ifdef BOTAN_HAS_EAX + #include <botan/eax.h> +#endif namespace Botan { @@ -68,46 +89,79 @@ Keyed_Filter* Default_Engine::get_cipher(const std::string& algo_spec, throw Invalid_Algorithm_Name(algo_spec); if(mode == "OFB") + { +#ifdef BOTAN_HAS_OFB return new OFB(cipher); +#else + return 0; +#endif + } else if(mode == "CTR-BE") + { +#ifdef BOTAN_HAS_CTR return new CTR_BE(cipher); +#else + return 0; +#endif + } else if(mode == "ECB" || mode == "CBC" || mode == "CTS" || mode == "CFB" || mode == "EAX") { if(mode == "ECB") { +#ifdef BOTAN_HAS_ECB if(direction == ENCRYPTION) return new ECB_Encryption(cipher, padding); else return new ECB_Decryption(cipher, padding); +#else + return 0; +#endif } else if(mode == "CFB") { +#ifdef BOTAN_HAS_CFB if(direction == ENCRYPTION) return new CFB_Encryption(cipher, bits); else return new CFB_Decryption(cipher, bits); +#else + return 0; +#endif } else if(mode == "CBC") { if(padding == "CTS") { +#ifdef BOTAN_HAS_CTS if(direction == ENCRYPTION) return new CTS_Encryption(cipher); else return new CTS_Decryption(cipher); +#else + return 0; +#endif } + +#ifdef BOTAN_HAS_CBC if(direction == ENCRYPTION) return new CBC_Encryption(cipher, padding); else return new CBC_Decryption(cipher, padding); +#else + return 0; +#endif } else if(mode == "EAX") { +#ifdef BOTAN_HAS_EAX if(direction == ENCRYPTION) return new EAX_Encryption(cipher, bits); else return new EAX_Decryption(cipher, bits); +#else + return 0; +#endif } else throw Internal_Error("get_mode: " + cipher + "/" diff --git a/src/eax.cpp b/src/eax.cpp deleted file mode 100644 index f246a9dea..000000000 --- a/src/eax.cpp +++ /dev/null @@ -1,194 +0,0 @@ -/************************************************* -* EAX Mode Encryption Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/eax.h> -#include <botan/lookup.h> -#include <botan/xor_buf.h> -#include <botan/parsing.h> -#include <algorithm> - -namespace Botan { - -namespace { - -/************************************************* -* EAX MAC-based PRF * -*************************************************/ -SecureVector<byte> eax_prf(byte tag, u32bit BLOCK_SIZE, - MessageAuthenticationCode* mac, - const byte in[], u32bit length) - { - for(u32bit j = 0; j != BLOCK_SIZE - 1; ++j) - mac->update(0); - mac->update(tag); - mac->update(in, length); - return mac->final(); - } - -} - -/************************************************* -* EAX_Base Constructor * -*************************************************/ -EAX_Base::EAX_Base(const std::string& cipher_name, - u32bit tag_size) : - TAG_SIZE(tag_size ? tag_size / 8 : block_size_of(cipher_name)), - BLOCK_SIZE(block_size_of(cipher_name)) - { - const std::string mac_name = "CMAC(" + cipher_name + ")"; - - cipher = get_block_cipher(cipher_name); - mac = get_mac(mac_name); - - if(tag_size % 8 != 0 || TAG_SIZE == 0 || TAG_SIZE > mac->OUTPUT_LENGTH) - throw Invalid_Argument(name() + ": Bad tag size " + to_string(tag_size)); - - state.create(BLOCK_SIZE); - buffer.create(BLOCK_SIZE); - position = 0; - } - -/************************************************* -* Check if a keylength is valid for EAX * -*************************************************/ -bool EAX_Base::valid_keylength(u32bit n) const - { - if(!cipher->valid_keylength(n)) - return false; - if(!mac->valid_keylength(n)) - return false; - return true; - } - -/************************************************* -* Set the EAX key * -*************************************************/ -void EAX_Base::set_key(const SymmetricKey& key) - { - cipher->set_key(key); - mac->set_key(key); - header_mac = eax_prf(1, BLOCK_SIZE, mac, 0, 0); - } - -/************************************************* -* Do setup at the start of each message * -*************************************************/ -void EAX_Base::start_msg() - { - for(u32bit j = 0; j != BLOCK_SIZE - 1; ++j) - mac->update(0); - mac->update(2); - } - -/************************************************* -* Set the EAX nonce * -*************************************************/ -void EAX_Base::set_iv(const InitializationVector& iv) - { - nonce_mac = eax_prf(0, BLOCK_SIZE, mac, iv.begin(), iv.length()); - state = nonce_mac; - cipher->encrypt(state, buffer); - } - -/************************************************* -* Set the EAX header * -*************************************************/ -void EAX_Base::set_header(const byte header[], u32bit length) - { - header_mac = eax_prf(1, BLOCK_SIZE, mac, header, length); - } - -/************************************************* -* Return the name of this cipher mode * -*************************************************/ -std::string EAX_Base::name() const - { - return (cipher->name() + "/EAX"); - } - -/************************************************* -* Increment the counter and update the buffer * -*************************************************/ -void EAX_Base::increment_counter() - { - for(s32bit j = BLOCK_SIZE - 1; j >= 0; --j) - if(++state[j]) - break; - cipher->encrypt(state, buffer); - position = 0; - } - -/************************************************* -* EAX_Encryption Constructor * -*************************************************/ -EAX_Encryption::EAX_Encryption(const std::string& cipher_name, - u32bit tag_size) : - EAX_Base(cipher_name, tag_size) - { - } - -/************************************************* -* EAX_Encryption Constructor * -*************************************************/ -EAX_Encryption::EAX_Encryption(const std::string& cipher_name, - const SymmetricKey& key, - const InitializationVector& iv, - u32bit tag_size) : - EAX_Base(cipher_name, tag_size) - { - set_key(key); - set_iv(iv); - } - -/************************************************* -* Encrypt in EAX mode * -*************************************************/ -void EAX_Encryption::write(const byte input[], u32bit length) - { - u32bit copied = std::min(BLOCK_SIZE - position, length); - xor_buf(buffer + position, input, copied); - send(buffer + position, copied); - mac->update(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); - mac->update(buffer, BLOCK_SIZE); - - input += BLOCK_SIZE; - length -= BLOCK_SIZE; - increment_counter(); - } - - xor_buf(buffer + position, input, length); - send(buffer + position, length); - mac->update(buffer + position, length); - position += length; - } - -/************************************************* -* Finish encrypting in EAX mode * -*************************************************/ -void EAX_Encryption::end_msg() - { - SecureVector<byte> data_mac = mac->final(); - xor_buf(data_mac, nonce_mac, data_mac.size()); - xor_buf(data_mac, header_mac, data_mac.size()); - - send(data_mac, TAG_SIZE); - - state.clear(); - buffer.clear(); - position = 0; - } - -} diff --git a/src/eax_dec.cpp b/src/eax_dec.cpp deleted file mode 100644 index 70cdd9863..000000000 --- a/src/eax_dec.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/************************************************* -* EAX Mode Encryption Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/eax.h> -#include <botan/lookup.h> -#include <botan/xor_buf.h> -#include <botan/parsing.h> -#include <algorithm> - -namespace Botan { - -/************************************************* -* EAX_Decryption Constructor * -*************************************************/ -EAX_Decryption::EAX_Decryption(const std::string& cipher_name, - u32bit tag_size) : - EAX_Base(cipher_name, tag_size) - { - queue.create(2*TAG_SIZE + DEFAULT_BUFFERSIZE); - queue_start = queue_end = 0; - } - -/************************************************* -* EAX_Decryption Constructor * -*************************************************/ -EAX_Decryption::EAX_Decryption(const std::string& cipher_name, - const SymmetricKey& key, - const InitializationVector& iv, - u32bit tag_size) : - EAX_Base(cipher_name, tag_size) - { - set_key(key); - set_iv(iv); - queue.create(2*TAG_SIZE + DEFAULT_BUFFERSIZE); - queue_start = queue_end = 0; - } - -/************************************************* -* Decrypt in EAX mode * -*************************************************/ -void EAX_Decryption::write(const byte input[], u32bit length) - { - while(length) - { - const u32bit copied = std::min(length, queue.size() - queue_end); - - queue.copy(queue_end, input, copied); - input += copied; - length -= copied; - queue_end += copied; - - SecureVector<byte> block_buf(cipher->BLOCK_SIZE); - while((queue_end - queue_start) > TAG_SIZE) - { - u32bit removed = (queue_end - queue_start) - TAG_SIZE; - do_write(queue + queue_start, removed); - queue_start += removed; - } - - if(queue_start + TAG_SIZE == queue_end && - queue_start >= queue.size() / 2) - { - SecureVector<byte> queue_data(TAG_SIZE); - queue_data.copy(queue + queue_start, TAG_SIZE); - queue.copy(queue_data, TAG_SIZE); - queue_start = 0; - queue_end = TAG_SIZE; - } - } - } - -/************************************************* -* Decrypt in EAX mode * -*************************************************/ -void EAX_Decryption::do_write(const byte input[], u32bit length) - { - mac->update(input, 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; - } - -/************************************************* -* Finish decrypting in EAX mode * -*************************************************/ -void EAX_Decryption::end_msg() - { - if((queue_end - queue_start) != TAG_SIZE) - throw Integrity_Failure(name() + ": Message authentication failure"); - - SecureVector<byte> data_mac = mac->final(); - - for(u32bit j = 0; j != TAG_SIZE; ++j) - if(queue[queue_start+j] != (data_mac[j] ^ nonce_mac[j] ^ header_mac[j])) - throw Integrity_Failure(name() + ": Message authentication failure"); - - state.clear(); - buffer.clear(); - position = 0; - queue_start = queue_end = 0; - } - -} diff --git a/src/ecb.cpp b/src/ecb.cpp deleted file mode 100644 index 8effac1d5..000000000 --- a/src/ecb.cpp +++ /dev/null @@ -1,152 +0,0 @@ -/************************************************* -* ECB Mode Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/ecb.h> -#include <botan/lookup.h> - -namespace Botan { - -/************************************************* -* ECB Constructor * -*************************************************/ -ECB::ECB(const std::string& cipher_name, const std::string& padding_name) : - BlockCipherMode(cipher_name, "ECB", 0), padder(get_bc_pad(padding_name)) - { - } - -/************************************************* -* Verify the IV is not set * -*************************************************/ -bool ECB::valid_iv_size(u32bit iv_size) const - { - if(iv_size == 0) - return true; - return false; - } - -/************************************************* -* Return an ECB mode name * -*************************************************/ -std::string ECB::name() const - { - return (cipher->name() + "/" + mode_name + "/" + padder->name()); - } - -/************************************************* -* ECB Encryption Constructor * -*************************************************/ -ECB_Encryption::ECB_Encryption(const std::string& cipher_name, - const std::string& padding_name) : - ECB(cipher_name, padding_name) - { - } - -/************************************************* -* ECB Encryption Constructor * -*************************************************/ -ECB_Encryption::ECB_Encryption(const std::string& cipher_name, - const std::string& padding_name, - const SymmetricKey& key) : - ECB(cipher_name, padding_name) - { - set_key(key); - } - -/************************************************* -* Encrypt in ECB mode * -*************************************************/ -void ECB_Encryption::write(const byte input[], u32bit length) - { - buffer.copy(position, input, length); - if(position + length >= BLOCK_SIZE) - { - cipher->encrypt(buffer); - send(buffer, BLOCK_SIZE); - input += (BLOCK_SIZE - position); - length -= (BLOCK_SIZE - position); - while(length >= BLOCK_SIZE) - { - cipher->encrypt(input, buffer); - send(buffer, BLOCK_SIZE); - input += BLOCK_SIZE; - length -= BLOCK_SIZE; - } - buffer.copy(input, length); - position = 0; - } - position += length; - } - -/************************************************* -* Finish encrypting in ECB mode * -*************************************************/ -void ECB_Encryption::end_msg() - { - SecureVector<byte> padding(BLOCK_SIZE); - padder->pad(padding, padding.size(), position); - write(padding, padder->pad_bytes(BLOCK_SIZE, position)); - if(position != 0) - throw Encoding_Error(name() + ": Did not pad to full blocksize"); - } - -/************************************************* -* ECB Decryption Constructor * -*************************************************/ -ECB_Decryption::ECB_Decryption(const std::string& cipher_name, - const std::string& padding_name) : - ECB(cipher_name, padding_name) - { - } - -/************************************************* -* ECB Decryption Constructor * -*************************************************/ -ECB_Decryption::ECB_Decryption(const std::string& cipher_name, - const std::string& padding_name, - const SymmetricKey& key) : - ECB(cipher_name, padding_name) - { - set_key(key); - } - -/************************************************* -* Decrypt in ECB mode * -*************************************************/ -void ECB_Decryption::write(const byte input[], u32bit length) - { - buffer.copy(position, input, length); - if(position + length > BLOCK_SIZE) - { - cipher->decrypt(buffer); - send(buffer, BLOCK_SIZE); - input += (BLOCK_SIZE - position); - length -= (BLOCK_SIZE - position); - while(length > BLOCK_SIZE) - { - cipher->decrypt(input, buffer); - send(buffer, BLOCK_SIZE); - input += BLOCK_SIZE; - length -= BLOCK_SIZE; - } - buffer.copy(input, length); - position = 0; - } - position += length; - } - -/************************************************* -* Finish decrypting in ECB mode * -*************************************************/ -void ECB_Decryption::end_msg() - { - if(position != BLOCK_SIZE) - throw Decoding_Error(name()); - cipher->decrypt(buffer); - send(buffer, padder->unpad(buffer, BLOCK_SIZE)); - state = buffer; - position = 0; - } - -} diff --git a/src/ofb.cpp b/src/ofb.cpp deleted file mode 100644 index db254d329..000000000 --- a/src/ofb.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/************************************************* -* OFB Mode Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/ofb.h> -#include <botan/lookup.h> -#include <botan/xor_buf.h> -#include <algorithm> - -namespace Botan { - -/************************************************* -* OFB Constructor * -*************************************************/ -OFB::OFB(const std::string& cipher_name) : - BlockCipherMode(cipher_name, "OFB", block_size_of(cipher_name), 2) - { - } - -/************************************************* -* OFB Constructor * -*************************************************/ -OFB::OFB(const std::string& cipher_name, const SymmetricKey& key, - const InitializationVector& iv) : - BlockCipherMode(cipher_name, "OFB", block_size_of(cipher_name), 2) - { - set_key(key); - set_iv(iv); - } - -/************************************************* -* OFB Encryption/Decryption * -*************************************************/ -void OFB::write(const byte input[], u32bit length) - { - u32bit copied = std::min(BLOCK_SIZE - position, length); - xor_buf(buffer, input, state + position, copied); - send(buffer, copied); - input += copied; - length -= copied; - position += copied; - - if(position == BLOCK_SIZE) - { - cipher->encrypt(state); - position = 0; - } - - while(length >= BLOCK_SIZE) - { - xor_buf(buffer, input, state, BLOCK_SIZE); - send(buffer, BLOCK_SIZE); - - input += BLOCK_SIZE; - length -= BLOCK_SIZE; - cipher->encrypt(state); - } - - xor_buf(buffer, input, state + position, length); - send(buffer, length); - position += length; - } - -} |