diff options
Diffstat (limited to 'old/botan/src/modes/mode_pad')
-rw-r--r-- | old/botan/src/modes/mode_pad/info.txt | 10 | ||||
-rw-r--r-- | old/botan/src/modes/mode_pad/mode_pad.cpp | 128 | ||||
-rw-r--r-- | old/botan/src/modes/mode_pad/mode_pad.h | 120 |
3 files changed, 258 insertions, 0 deletions
diff --git a/old/botan/src/modes/mode_pad/info.txt b/old/botan/src/modes/mode_pad/info.txt new file mode 100644 index 0000000..f22cf74 --- /dev/null +++ b/old/botan/src/modes/mode_pad/info.txt @@ -0,0 +1,10 @@ +realname "Cipher Mode Padding Method" + +define CIPHER_MODE_PADDING + +load_on auto + +<add> +mode_pad.cpp +mode_pad.h +</add> diff --git a/old/botan/src/modes/mode_pad/mode_pad.cpp b/old/botan/src/modes/mode_pad/mode_pad.cpp new file mode 100644 index 0000000..b8badd7 --- /dev/null +++ b/old/botan/src/modes/mode_pad/mode_pad.cpp @@ -0,0 +1,128 @@ +/* +* 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> +#include <botan/util.h> + +namespace Botan { + +/* +* Default amount of padding +*/ +u32bit BlockCipherModePaddingMethod::pad_bytes(u32bit bs, u32bit pos) const + { + return (bs - pos); + } + +/* +* Pad with PKCS #7 Method +*/ +void PKCS7_Padding::pad(byte block[], u32bit size, u32bit position) const + { + for(u32bit j = 0; j != size; ++j) + block[j] = (size-position); + } + +/* +* Unpad with PKCS #7 Method +*/ +u32bit PKCS7_Padding::unpad(const byte block[], u32bit size) const + { + u32bit position = block[size-1]; + if(position > size) + throw Decoding_Error(name()); + for(u32bit j = size-position; j != size-1; ++j) + if(block[j] != position) + throw Decoding_Error(name()); + return (size-position); + } + +/* +* Query if the size is valid for this method +*/ +bool PKCS7_Padding::valid_blocksize(u32bit 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[], u32bit size, u32bit position) const + { + for(u32bit j = 0; j != size-position; ++j) + block[j] = 0; + block[size-position-1] = (size-position); + } + +/* +* Unpad with ANSI X9.23 Method +*/ +u32bit ANSI_X923_Padding::unpad(const byte block[], u32bit size) const + { + u32bit position = block[size-1]; + if(position > size) + throw Decoding_Error(name()); + for(u32bit 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(u32bit size) const + { + if(size > 0 && size < 256) + return true; + else + return false; + } + +/* +* Pad with One and Zeros Method +*/ +void OneAndZeros_Padding::pad(byte block[], u32bit size, u32bit) const + { + block[0] = 0x80; + for(u32bit j = 1; j != size; ++j) + block[j] = 0x00; + } + +/* +* Unpad with One and Zeros Method +*/ +u32bit OneAndZeros_Padding::unpad(const byte block[], u32bit 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(u32bit size) const + { + if(size) return true; + else return false; + } + +} diff --git a/old/botan/src/modes/mode_pad/mode_pad.h b/old/botan/src/modes/mode_pad/mode_pad.h new file mode 100644 index 0000000..a486d3c --- /dev/null +++ b/old/botan/src/modes/mode_pad/mode_pad.h @@ -0,0 +1,120 @@ +/** +* CBC Padding Methods +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_CBC_PADDING_H__ +#define BOTAN_CBC_PADDING_H__ + +#include <botan/types.h> +#include <string> + +namespace Botan { + +/** +* Block Cipher Mode Padding Method +* This class is pretty limited, it cannot deal well with +* randomized padding methods, or any padding method that +* wants to add more than one block. For instance, it should +* be possible to define cipher text stealing mode as simply +* a padding mode for CBC, which happens to consume the last +* two block (and requires use of the block cipher). +*/ +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[], + u32bit size, + u32bit current_position) const = 0; + + /** + * @param block the last block + * @param size the of the block + */ + virtual u32bit unpad(const byte block[], + u32bit size) const = 0; + + /** + * @param block_size of the cipher + * @param position in the current block + * @return number of padding bytes that will be appended + */ + virtual u32bit pad_bytes(u32bit block_size, + u32bit position) const; + + /** + * @param block_size of the cipher + * @return valid block size for this padding mode + */ + virtual bool valid_blocksize(u32bit block_size) const = 0; + + /** + * @return name of the mode + */ + virtual std::string name() const = 0; + + /** + * virtual destructor + */ + virtual ~BlockCipherModePaddingMethod() {} + }; + +/** +* PKCS#7 Padding +*/ +class BOTAN_DLL PKCS7_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "PKCS7"; } + }; + +/** +* ANSI X9.23 Padding +*/ +class BOTAN_DLL ANSI_X923_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "X9.23"; } + }; + +/** +* One And Zeros Padding +*/ +class BOTAN_DLL OneAndZeros_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const; + u32bit unpad(const byte[], u32bit) const; + bool valid_blocksize(u32bit) const; + std::string name() const { return "OneAndZeros"; } + }; + +/** +* Null Padding +*/ +class BOTAN_DLL Null_Padding : public BlockCipherModePaddingMethod + { + public: + void pad(byte[], u32bit, u32bit) const { return; } + u32bit unpad(const byte[], u32bit size) const { return size; } + u32bit pad_bytes(u32bit, u32bit) const { return 0; } + bool valid_blocksize(u32bit) const { return true; } + std::string name() const { return "NoPadding"; } + }; + +} + +#endif |