Botan  1.11.15
src/lib/modes/cfb/cfb.h
Go to the documentation of this file.
00001 /*
00002 * CFB 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_CFB_H__
00009 #define BOTAN_MODE_CFB_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 * CFB Mode
00019 */
00020 class BOTAN_DLL CFB_Mode : public Cipher_Mode
00021    {
00022    public:
00023       std::string name() const override;
00024 
00025       size_t update_granularity() const override;
00026 
00027       size_t minimum_final_size() const override;
00028 
00029       Key_Length_Specification key_spec() const override;
00030 
00031       size_t output_length(size_t input_length) const override;
00032 
00033       size_t default_nonce_length() const override;
00034 
00035       bool valid_nonce_length(size_t n) const override;
00036 
00037       void clear() override;
00038    protected:
00039       CFB_Mode(BlockCipher* cipher, size_t feedback_bits);
00040 
00041       const BlockCipher& cipher() const { return *m_cipher; }
00042 
00043       size_t feedback() const { return m_feedback_bytes; }
00044 
00045       secure_vector<byte>& shift_register() { return m_shift_register; }
00046 
00047       secure_vector<byte>& keystream_buf() { return m_keystream_buf; }
00048 
00049    private:
00050       secure_vector<byte> start_raw(const byte nonce[], size_t nonce_len) override;
00051       void key_schedule(const byte key[], size_t length) override;
00052 
00053       std::unique_ptr<BlockCipher> m_cipher;
00054       secure_vector<byte> m_shift_register;
00055       secure_vector<byte> m_keystream_buf;
00056       size_t m_feedback_bytes;
00057    };
00058 
00059 /**
00060 * CFB Encryption
00061 */
00062 class BOTAN_DLL CFB_Encryption : public CFB_Mode
00063    {
00064    public:
00065       CFB_Encryption(BlockCipher* cipher, size_t feedback_bits) :
00066          CFB_Mode(cipher, feedback_bits) {}
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 
00073 /**
00074 * CFB Decryption
00075 */
00076 class BOTAN_DLL CFB_Decryption : public CFB_Mode
00077    {
00078    public:
00079       CFB_Decryption(BlockCipher* cipher, size_t feedback_bits) :
00080          CFB_Mode(cipher, feedback_bits) {}
00081 
00082       void update(secure_vector<byte>& blocks, size_t offset = 0) override;
00083 
00084       void finish(secure_vector<byte>& final_block, size_t offset = 0) override;
00085    };
00086 
00087 }
00088 
00089 #endif