Botan  1.11.15
src/lib/modes/mode_pad/mode_pad.h
Go to the documentation of this file.
00001 /*
00002 * ECB/CBC Padding Methods
00003 * (C) 1999-2008,2013 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_MODE_PADDING_H__
00009 #define BOTAN_MODE_PADDING_H__
00010 
00011 #include <botan/secmem.h>
00012 #include <string>
00013 
00014 namespace Botan {
00015 
00016 /**
00017 * Block Cipher Mode Padding Method
00018 * This class is pretty limited, it cannot deal well with
00019 * randomized padding methods, or any padding method that
00020 * wants to add more than one block. For instance, it should
00021 * be possible to define cipher text stealing mode as simply
00022 * a padding mode for CBC, which happens to consume the last
00023 * two block (and requires use of the block cipher).
00024 */
00025 class BOTAN_DLL BlockCipherModePaddingMethod
00026    {
00027    public:
00028       virtual void add_padding(secure_vector<byte>& buffer,
00029                                size_t final_block_bytes,
00030                                size_t block_size) const = 0;
00031 
00032       /**
00033       * @param block the last block
00034       * @param size the of the block
00035       */
00036       virtual size_t unpad(const byte block[],
00037                            size_t size) const = 0;
00038 
00039       /**
00040       * @param block_size of the cipher
00041       * @return valid block size for this padding mode
00042       */
00043       virtual bool valid_blocksize(size_t block_size) const = 0;
00044 
00045       /**
00046       * @return name of the mode
00047       */
00048       virtual std::string name() const = 0;
00049 
00050       /**
00051       * virtual destructor
00052       */
00053       virtual ~BlockCipherModePaddingMethod() {}
00054    };
00055 
00056 /**
00057 * PKCS#7 Padding
00058 */
00059 class BOTAN_DLL PKCS7_Padding : public BlockCipherModePaddingMethod
00060    {
00061    public:
00062       void add_padding(secure_vector<byte>& buffer,
00063                        size_t final_block_bytes,
00064                        size_t block_size) const override;
00065 
00066       size_t unpad(const byte[], size_t) const;
00067 
00068       bool valid_blocksize(size_t bs) const { return (bs > 0 && bs < 256); }
00069 
00070       std::string name() const { return "PKCS7"; }
00071    };
00072 
00073 /**
00074 * ANSI X9.23 Padding
00075 */
00076 class BOTAN_DLL ANSI_X923_Padding : public BlockCipherModePaddingMethod
00077    {
00078    public:
00079       void add_padding(secure_vector<byte>& buffer,
00080                        size_t final_block_bytes,
00081                        size_t block_size) const override;
00082 
00083       size_t unpad(const byte[], size_t) const;
00084 
00085       bool valid_blocksize(size_t bs) const { return (bs > 0 && bs < 256); }
00086 
00087       std::string name() const { return "X9.23"; }
00088    };
00089 
00090 /**
00091 * One And Zeros Padding
00092 */
00093 class BOTAN_DLL OneAndZeros_Padding : public BlockCipherModePaddingMethod
00094    {
00095    public:
00096       void add_padding(secure_vector<byte>& buffer,
00097                        size_t final_block_bytes,
00098                        size_t block_size) const override;
00099 
00100       size_t unpad(const byte[], size_t) const;
00101 
00102       bool valid_blocksize(size_t bs) const { return (bs > 0); }
00103 
00104       std::string name() const { return "OneAndZeros"; }
00105    };
00106 
00107 /**
00108 * Null Padding
00109 */
00110 class BOTAN_DLL Null_Padding : public BlockCipherModePaddingMethod
00111    {
00112    public:
00113       void add_padding(secure_vector<byte>&, size_t, size_t) const override {}
00114 
00115       size_t unpad(const byte[], size_t size) const { return size; }
00116 
00117       bool valid_blocksize(size_t) const { return true; }
00118 
00119       std::string name() const { return "NoPadding"; }
00120    };
00121 
00122 BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
00123 
00124 }
00125 
00126 #endif