diff options
author | lloyd <[email protected]> | 2013-08-15 21:01:49 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2013-08-15 21:01:49 +0000 |
commit | 178fe54130c3582e5a292bac063f53a77dc4c4df (patch) | |
tree | cd45fbf943939862d84778761d080763fb653d0b /src | |
parent | 7328d76b78c3c923c11cc01aa5cf8e5498ea02ff (diff) |
Simplify handling of CBC/ECB padding and move source to src/modes
Diffstat (limited to 'src')
-rw-r--r-- | src/filters/modes/mode_pad/mode_pad.cpp | 135 | ||||
-rw-r--r-- | src/modes/cbc/cbc.cpp | 20 | ||||
-rw-r--r-- | src/modes/ecb/ecb.cpp | 20 | ||||
-rw-r--r-- | src/modes/mode_pad/info.txt (renamed from src/filters/modes/mode_pad/info.txt) | 0 | ||||
-rw-r--r-- | src/modes/mode_pad/mode_pad.cpp | 103 | ||||
-rw-r--r-- | src/modes/mode_pad/mode_pad.h (renamed from src/filters/modes/mode_pad/mode_pad.h) | 62 |
6 files changed, 141 insertions, 199 deletions
diff --git a/src/filters/modes/mode_pad/mode_pad.cpp b/src/filters/modes/mode_pad/mode_pad.cpp deleted file mode 100644 index 728aba540..000000000 --- a/src/filters/modes/mode_pad/mode_pad.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* -* CBC Padding Methods -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include <botan/mode_pad.h> -#include <botan/exceptn.h> - -namespace Botan { - -/* -* Default amount of padding -*/ -size_t BlockCipherModePaddingMethod::pad_bytes(size_t bs, size_t pos) const - { - return (bs - pos); - } - -/* -* Pad with PKCS #7 Method -*/ -void PKCS7_Padding::pad(byte block[], size_t size, size_t position) const - { - const size_t bytes_remaining = size - position; - const byte pad_value = static_cast<byte>(bytes_remaining); - - BOTAN_ASSERT_EQUAL(pad_value, bytes_remaining, - "PKCS7 pad values match bytes remaining"); - - for(size_t j = 0; j != size; ++j) - block[j] = pad_value; - } - -/* -* Unpad with PKCS #7 Method -*/ -size_t PKCS7_Padding::unpad(const byte block[], size_t size) const - { - size_t position = block[size-1]; - - if(position > size) - throw Decoding_Error("Bad padding in " + name()); - - for(size_t j = size-position; j != size-1; ++j) - if(block[j] != position) - throw Decoding_Error("Bad padding in " + name()); - - return (size-position); - } - -/* -* Query if the size is valid for this method -*/ -bool PKCS7_Padding::valid_blocksize(size_t size) const - { - if(size > 0 && size < 256) - return true; - else - return false; - } - -/* -* Pad with ANSI X9.23 Method -*/ -void ANSI_X923_Padding::pad(byte block[], size_t size, size_t position) const - { - for(size_t j = 0; j != size-position; ++j) - block[j] = 0; - block[size-position-1] = static_cast<byte>(size-position); - } - -/* -* Unpad with ANSI X9.23 Method -*/ -size_t ANSI_X923_Padding::unpad(const byte block[], size_t size) const - { - size_t position = block[size-1]; - if(position > size) - throw Decoding_Error(name()); - for(size_t j = size-position; j != size-1; ++j) - if(block[j] != 0) - throw Decoding_Error(name()); - return (size-position); - } - -/* -* Query if the size is valid for this method -*/ -bool ANSI_X923_Padding::valid_blocksize(size_t size) const - { - if(size > 0 && size < 256) - return true; - else - return false; - } - -/* -* Pad with One and Zeros Method -*/ -void OneAndZeros_Padding::pad(byte block[], size_t size, size_t) const - { - block[0] = 0x80; - for(size_t j = 1; j != size; ++j) - block[j] = 0x00; - } - -/* -* Unpad with One and Zeros Method -*/ -size_t OneAndZeros_Padding::unpad(const byte block[], size_t size) const - { - while(size) - { - if(block[size-1] == 0x80) - break; - if(block[size-1] != 0x00) - throw Decoding_Error(name()); - size--; - } - if(!size) - throw Decoding_Error(name()); - return (size-1); - } - -/* -* Query if the size is valid for this method -*/ -bool OneAndZeros_Padding::valid_blocksize(size_t size) const - { - return (size > 0); - } - -} diff --git a/src/modes/cbc/cbc.cpp b/src/modes/cbc/cbc.cpp index 96e5be6e2..822ad35ba 100644 --- a/src/modes/cbc/cbc.cpp +++ b/src/modes/cbc/cbc.cpp @@ -10,9 +10,6 @@ #include <botan/internal/xor_buf.h> #include <botan/internal/rounding.h> -#include <iostream> -#include <botan/hex.h> - namespace Botan { CBC_Mode::CBC_Mode(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) : @@ -126,24 +123,11 @@ void CBC_Encryption::finish(secure_vector<byte>& buffer, size_t offset) const size_t bytes_in_final_block = sz % BS; - const size_t pad_bytes = padding().pad_bytes(BS, bytes_in_final_block); + padding().add_padding(buffer, bytes_in_final_block, BS); - if((pad_bytes + bytes_in_final_block) % BS) + if(buffer.size() % BS) throw std::runtime_error("Did not pad to full block size in " + name()); -#if 0 - const size_t pad_offset = buffer.size(); - //buffer.resize(checked_add(buffer.size() + pad_bytes)); - buffer.resize(buffer.size() + pad_bytes); - - padder().pad(&buffer[pad_offset], BS, bytes_in_final_block); -#else - std::vector<byte> pad(BS); - padding().pad(&pad[0], BS, bytes_in_final_block); - - buffer.insert(buffer.end(), pad.begin(), pad.begin() + pad_bytes); -#endif - update(buffer, offset); } diff --git a/src/modes/ecb/ecb.cpp b/src/modes/ecb/ecb.cpp index 8abf76433..1b73da64f 100644 --- a/src/modes/ecb/ecb.cpp +++ b/src/modes/ecb/ecb.cpp @@ -57,7 +57,7 @@ void ECB_Mode::key_schedule(const byte key[], size_t length) m_cipher->set_key(key, length); } -secure_vector<byte> ECB_Mode::start(const byte nonce[], size_t nonce_len) +secure_vector<byte> ECB_Mode::start(const byte[], size_t nonce_len) { if(!valid_nonce_length(nonce_len)) throw Invalid_IV_Length(name(), nonce_len); @@ -93,30 +93,16 @@ void ECB_Encryption::finish(secure_vector<byte>& buffer, size_t offset) { BOTAN_ASSERT(buffer.size() >= offset, "Offset is sane"); const size_t sz = buffer.size() - offset; - //byte* buf = &buffer[offset]; const size_t BS = cipher().block_size(); const size_t bytes_in_final_block = sz % BS; - const size_t pad_bytes = padding().pad_bytes(BS, bytes_in_final_block); + padding().add_padding(buffer, bytes_in_final_block, BS); - if((pad_bytes + bytes_in_final_block) % BS) + if(buffer.size() % BS) throw std::runtime_error("Did not pad to full block size in " + name()); -#if 0 - const size_t pad_offset = buffer.size(); - //buffer.resize(checked_add(buffer.size() + pad_bytes)); - buffer.resize(buffer.size() + pad_bytes); - - padder().pad(&buffer[pad_offset], BS, bytes_in_final_block); -#else - std::vector<byte> pad(BS); - padding().pad(&pad[0], BS, bytes_in_final_block); - - buffer.insert(buffer.end(), pad.begin(), pad.begin() + pad_bytes); -#endif - update(buffer, offset); } diff --git a/src/filters/modes/mode_pad/info.txt b/src/modes/mode_pad/info.txt index e97ea03a5..e97ea03a5 100644 --- a/src/filters/modes/mode_pad/info.txt +++ b/src/modes/mode_pad/info.txt diff --git a/src/modes/mode_pad/mode_pad.cpp b/src/modes/mode_pad/mode_pad.cpp new file mode 100644 index 000000000..918964c74 --- /dev/null +++ b/src/modes/mode_pad/mode_pad.cpp @@ -0,0 +1,103 @@ +/* +* CBC Padding Methods +* (C) 1999-2007,2013 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include <botan/mode_pad.h> +#include <botan/exceptn.h> + +namespace Botan { + +/* +* Pad with PKCS #7 Method +*/ +void PKCS7_Padding::add_padding(secure_vector<byte>& buffer, + size_t last_byte_pos, + size_t block_size) const + { + const byte pad_value = block_size - last_byte_pos; + + for(size_t i = 0; i != pad_value; ++i) + buffer.push_back(pad_value); + } + +/* +* Unpad with PKCS #7 Method +*/ +size_t PKCS7_Padding::unpad(const byte block[], size_t size) const + { + size_t position = block[size-1]; + + if(position > size) + throw Decoding_Error("Bad padding in " + name()); + + for(size_t j = size-position; j != size-1; ++j) + if(block[j] != position) + throw Decoding_Error("Bad padding in " + name()); + + return (size-position); + } + +/* +* Pad with ANSI X9.23 Method +*/ +void ANSI_X923_Padding::add_padding(secure_vector<byte>& buffer, + size_t last_byte_pos, + size_t block_size) const + { + const byte pad_value = block_size - last_byte_pos; + + for(size_t i = last_byte_pos; i < block_size; ++i) + buffer.push_back(0); + buffer.push_back(pad_value); + } + +/* +* Unpad with ANSI X9.23 Method +*/ +size_t ANSI_X923_Padding::unpad(const byte block[], size_t size) const + { + size_t position = block[size-1]; + if(position > size) + throw Decoding_Error(name()); + for(size_t j = size-position; j != size-1; ++j) + if(block[j] != 0) + throw Decoding_Error(name()); + return (size-position); + } + +/* +* Pad with One and Zeros Method +*/ +void OneAndZeros_Padding::add_padding(secure_vector<byte>& buffer, + size_t last_byte_pos, + size_t block_size) const + { + buffer.push_back(0x80); + + for(size_t i = last_byte_pos + 1; i % block_size; ++i) + buffer.push_back(0x00); + } + +/* +* Unpad with One and Zeros Method +*/ +size_t OneAndZeros_Padding::unpad(const byte block[], size_t size) const + { + while(size) + { + if(block[size-1] == 0x80) + break; + if(block[size-1] != 0x00) + throw Decoding_Error(name()); + size--; + } + if(!size) + throw Decoding_Error(name()); + return (size-1); + } + + +} diff --git a/src/filters/modes/mode_pad/mode_pad.h b/src/modes/mode_pad/mode_pad.h index 48cda02f4..e7c38a196 100644 --- a/src/filters/modes/mode_pad/mode_pad.h +++ b/src/modes/mode_pad/mode_pad.h @@ -1,14 +1,14 @@ /* -* CBC Padding Methods -* (C) 1999-2008 Jack Lloyd +* ECB/CBC Padding Methods +* (C) 1999-2008,2013 Jack Lloyd * * Distributed under the terms of the Botan license */ -#ifndef BOTAN_CBC_PADDING_H__ -#define BOTAN_CBC_PADDING_H__ +#ifndef BOTAN_MODE_PADDING_H__ +#define BOTAN_MODE_PADDING_H__ -#include <botan/types.h> +#include <botan/secmem.h> #include <string> namespace Botan { @@ -25,14 +25,9 @@ namespace Botan { class BOTAN_DLL BlockCipherModePaddingMethod { public: - /** - * @param block output buffer - * @param size of the block - * @param current_position in the last block - */ - virtual void pad(byte block[], - size_t size, - size_t current_position) const = 0; + virtual void add_padding(secure_vector<byte>& buffer, + size_t final_block_bytes, + size_t block_size) const = 0; /** * @param block the last block @@ -43,14 +38,6 @@ class BOTAN_DLL BlockCipherModePaddingMethod /** * @param block_size of the cipher - * @param position in the current block - * @return number of padding bytes that will be appended - */ - virtual size_t pad_bytes(size_t block_size, - size_t position) const; - - /** - * @param block_size of the cipher * @return valid block size for this padding mode */ virtual bool valid_blocksize(size_t block_size) const = 0; @@ -72,9 +59,14 @@ class BOTAN_DLL BlockCipherModePaddingMethod class BOTAN_DLL PKCS7_Padding : public BlockCipherModePaddingMethod { public: - void pad(byte[], size_t, size_t) const; + void add_padding(secure_vector<byte>& buffer, + size_t final_block_bytes, + size_t block_size) const override; + size_t unpad(const byte[], size_t) const; - bool valid_blocksize(size_t) const; + + bool valid_blocksize(size_t bs) const { return (bs > 0 && bs < 256); } + std::string name() const { return "PKCS7"; } }; @@ -84,9 +76,14 @@ class BOTAN_DLL PKCS7_Padding : public BlockCipherModePaddingMethod class BOTAN_DLL ANSI_X923_Padding : public BlockCipherModePaddingMethod { public: - void pad(byte[], size_t, size_t) const; + void add_padding(secure_vector<byte>& buffer, + size_t final_block_bytes, + size_t block_size) const override; + size_t unpad(const byte[], size_t) const; - bool valid_blocksize(size_t) const; + + bool valid_blocksize(size_t bs) const { return (bs > 0 && bs < 256); } + std::string name() const { return "X9.23"; } }; @@ -96,9 +93,14 @@ class BOTAN_DLL ANSI_X923_Padding : public BlockCipherModePaddingMethod class BOTAN_DLL OneAndZeros_Padding : public BlockCipherModePaddingMethod { public: - void pad(byte[], size_t, size_t) const; + void add_padding(secure_vector<byte>& buffer, + size_t final_block_bytes, + size_t block_size) const override; + size_t unpad(const byte[], size_t) const; - bool valid_blocksize(size_t) const; + + bool valid_blocksize(size_t bs) const { return (bs > 0); } + std::string name() const { return "OneAndZeros"; } }; @@ -108,10 +110,12 @@ class BOTAN_DLL OneAndZeros_Padding : public BlockCipherModePaddingMethod class BOTAN_DLL Null_Padding : public BlockCipherModePaddingMethod { public: - void pad(byte[], size_t, size_t) const { return; } + void add_padding(secure_vector<byte>&, size_t, size_t) const override {} + size_t unpad(const byte[], size_t size) const { return size; } - size_t pad_bytes(size_t, size_t) const { return 0; } + bool valid_blocksize(size_t) const { return true; } + std::string name() const { return "NoPadding"; } }; |