Botan
1.11.15
|
00001 /* 00002 * CBC mode 00003 * (C) 1999-2007,2013 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_MODE_CBC_H__ 00009 #define BOTAN_MODE_CBC_H__ 00010 00011 #include <botan/cipher_mode.h> 00012 #include <botan/block_cipher.h> 00013 #include <botan/mode_pad.h> 00014 00015 namespace Botan { 00016 00017 /** 00018 * CBC Mode 00019 */ 00020 class BOTAN_DLL CBC_Mode : public Cipher_Mode 00021 { 00022 public: 00023 std::string name() const override; 00024 00025 size_t update_granularity() const override; 00026 00027 Key_Length_Specification key_spec() const override; 00028 00029 size_t default_nonce_length() const override; 00030 00031 bool valid_nonce_length(size_t n) const override; 00032 00033 void clear() override; 00034 protected: 00035 CBC_Mode(BlockCipher* cipher, BlockCipherModePaddingMethod* padding); 00036 00037 const BlockCipher& cipher() const { return *m_cipher; } 00038 00039 const BlockCipherModePaddingMethod& padding() const 00040 { 00041 BOTAN_ASSERT_NONNULL(m_padding); 00042 return *m_padding; 00043 } 00044 00045 secure_vector<byte>& state() { return m_state; } 00046 00047 byte* state_ptr() { return &m_state[0]; } 00048 00049 private: 00050 secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override; 00051 00052 void key_schedule(const byte key[], size_t length) override; 00053 00054 std::unique_ptr<BlockCipher> m_cipher; 00055 std::unique_ptr<BlockCipherModePaddingMethod> m_padding; 00056 secure_vector<byte> m_state; 00057 }; 00058 00059 /** 00060 * CBC Encryption 00061 */ 00062 class BOTAN_DLL CBC_Encryption : public CBC_Mode 00063 { 00064 public: 00065 CBC_Encryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) : 00066 CBC_Mode(cipher, padding) {} 00067 00068 void update(secure_vector<byte>& blocks, size_t offset = 0) override; 00069 00070 void finish(secure_vector<byte>& final_block, size_t offset = 0) override; 00071 00072 size_t output_length(size_t input_length) const override; 00073 00074 size_t minimum_final_size() const override; 00075 }; 00076 00077 /** 00078 * CBC Encryption with ciphertext stealing (CBC-CS3 variant) 00079 */ 00080 class BOTAN_DLL CTS_Encryption : public CBC_Encryption 00081 { 00082 public: 00083 CTS_Encryption(BlockCipher* cipher) : CBC_Encryption(cipher, nullptr) {} 00084 00085 size_t output_length(size_t input_length) const override; 00086 00087 void finish(secure_vector<byte>& final_block, size_t offset = 0) override; 00088 00089 size_t minimum_final_size() const override; 00090 00091 bool valid_nonce_length(size_t n) const; 00092 }; 00093 00094 /** 00095 * CBC Decryption 00096 */ 00097 class BOTAN_DLL CBC_Decryption : public CBC_Mode 00098 { 00099 public: 00100 CBC_Decryption(BlockCipher* cipher, BlockCipherModePaddingMethod* padding) : 00101 CBC_Mode(cipher, padding), m_tempbuf(update_granularity()) {} 00102 00103 void update(secure_vector<byte>& blocks, size_t offset = 0) override; 00104 00105 void finish(secure_vector<byte>& final_block, size_t offset = 0) override; 00106 00107 size_t output_length(size_t input_length) const override; 00108 00109 size_t minimum_final_size() const override; 00110 private: 00111 secure_vector<byte> m_tempbuf; 00112 }; 00113 00114 /** 00115 * CBC Decryption with ciphertext stealing (CBC-CS3 variant) 00116 */ 00117 class BOTAN_DLL CTS_Decryption : public CBC_Decryption 00118 { 00119 public: 00120 CTS_Decryption(BlockCipher* cipher) : CBC_Decryption(cipher, nullptr) {} 00121 00122 void finish(secure_vector<byte>& final_block, size_t offset = 0) override; 00123 00124 size_t minimum_final_size() const override; 00125 00126 bool valid_nonce_length(size_t n) const; 00127 }; 00128 00129 } 00130 00131 #endif