diff options
author | René Korthaus <[email protected]> | 2016-11-14 15:59:22 +0100 |
---|---|---|
committer | René Korthaus <[email protected]> | 2016-11-14 16:20:06 +0100 |
commit | 7acbc219afdf72578bc158ac223f1147a6b43d6f (patch) | |
tree | 66875a2b0ef8f4d7929a5433e6e44ac218af7646 /src/lib/modes | |
parent | 37c1a62525c74461693789f983a41c80697ff4a3 (diff) |
Add ESP block cipher padding from RFC 4303
ESP padding is one of the recommended padding
methods from BSI-TR-02102-1.
Diffstat (limited to 'src/lib/modes')
-rw-r--r-- | src/lib/modes/mode_pad/mode_pad.cpp | 43 | ||||
-rw-r--r-- | src/lib/modes/mode_pad/mode_pad.h | 20 |
2 files changed, 62 insertions, 1 deletions
diff --git a/src/lib/modes/mode_pad/mode_pad.cpp b/src/lib/modes/mode_pad/mode_pad.cpp index 7b4546c86..6550edef2 100644 --- a/src/lib/modes/mode_pad/mode_pad.cpp +++ b/src/lib/modes/mode_pad/mode_pad.cpp @@ -1,6 +1,7 @@ /* * CBC Padding Methods * (C) 1999-2007,2013 Jack Lloyd +* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -27,6 +28,9 @@ BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec) if(algo_spec == "X9.23") return new ANSI_X923_Padding; + if(algo_spec == "ESP") + return new ESP_Padding; + return nullptr; } @@ -121,5 +125,44 @@ size_t OneAndZeros_Padding::unpad(const byte block[], size_t size) const return (size-1); } +/* +* Pad with ESP Padding Method +*/ +void ESP_Padding::add_padding(secure_vector<byte>& buffer, + size_t last_byte_pos, + size_t block_size) const + { + byte pad_value = 0x01; + + for(size_t i = last_byte_pos; i < block_size; ++i) + { + buffer.push_back(pad_value++); + } + } + +/* +* Unpad with ESP Padding Method +*/ +size_t ESP_Padding::unpad(const byte block[], size_t size) const + { + while(size) + { + if(block[size-1] == 0x01) + { + break; + } + if(block[size-1] == 0x00) + { + throw Decoding_Error(name()); + } + size--; + } + if(!size) + { + throw Decoding_Error(name()); + } + return (size-1); + } + } diff --git a/src/lib/modes/mode_pad/mode_pad.h b/src/lib/modes/mode_pad/mode_pad.h index d52fbd7b6..af528a926 100644 --- a/src/lib/modes/mode_pad/mode_pad.h +++ b/src/lib/modes/mode_pad/mode_pad.h @@ -1,6 +1,7 @@ /* * ECB/CBC Padding Methods * (C) 1999-2008,2013 Jack Lloyd +* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -96,7 +97,7 @@ class BOTAN_DLL ANSI_X923_Padding final : public BlockCipherModePaddingMethod }; /** -* One And Zeros Padding +* One And Zeros Padding (ISO/IEC 7816-4) */ class BOTAN_DLL OneAndZeros_Padding final : public BlockCipherModePaddingMethod { @@ -113,6 +114,23 @@ class BOTAN_DLL OneAndZeros_Padding final : public BlockCipherModePaddingMethod }; /** +* ESP Padding (RFC 4304) +*/ +class BOTAN_DLL ESP_Padding final : public BlockCipherModePaddingMethod + { + public: + 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 override; + + bool valid_blocksize(size_t bs) const override { return (bs > 0); } + + std::string name() const override { return "ESP"; } + }; + +/** * Null Padding */ class BOTAN_DLL Null_Padding final : public BlockCipherModePaddingMethod |