aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRené Korthaus <[email protected]>2016-11-14 15:59:22 +0100
committerRené Korthaus <[email protected]>2016-11-14 16:20:06 +0100
commit7acbc219afdf72578bc158ac223f1147a6b43d6f (patch)
tree66875a2b0ef8f4d7929a5433e6e44ac218af7646
parent37c1a62525c74461693789f983a41c80697ff4a3 (diff)
Add ESP block cipher padding from RFC 4303
ESP padding is one of the recommended padding methods from BSI-TR-02102-1.
-rw-r--r--src/lib/modes/mode_pad/mode_pad.cpp43
-rw-r--r--src/lib/modes/mode_pad/mode_pad.h20
-rw-r--r--src/tests/data/pad.vec21
3 files changed, 83 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
diff --git a/src/tests/data/pad.vec b/src/tests/data/pad.vec
index 3c47959c9..edd0892fc 100644
--- a/src/tests/data/pad.vec
+++ b/src/tests/data/pad.vec
@@ -80,4 +80,25 @@ Blocksize = 8
In = FFFFFFFFFFFFFFFFFF
Out = FFFFFFFFFFFFFFFFFF00000000000007
+Blocksize = 8
+
+[ESP]
+In = FFFFFF
+Out = FFFFFF0102030405060708090A0B0C0D
+Blocksize = 16
+
+In = FFFFFFFF
+Out = FFFFFFFF0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C
+Blocksize = 32
+
+In = FFFFFFFFFFFF
+Out = FFFFFFFFFFFF0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A
+Blocksize = 64
+
+In = FFFFFFFFFFFFFFFF
+Out = FFFFFFFFFFFFFFFF0102030405060708
+Blocksize = 8
+
+In = FFFFFFFFFFFFFFFFFF
+Out = FFFFFFFFFFFFFFFFFF01020304050607
Blocksize = 8 \ No newline at end of file