Botan  2.6.0
Crypto and TLS for C++11
cfb.h
Go to the documentation of this file.
1 /*
2 * CFB mode
3 * (C) 1999-2007,2013 Jack Lloyd
4 * (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #ifndef BOTAN_MODE_CFB_H_
10 #define BOTAN_MODE_CFB_H_
11 
12 #include <botan/cipher_mode.h>
13 #include <botan/block_cipher.h>
14 
15 namespace Botan {
16 
17 /**
18 * CFB Mode
19 */
21  {
22  public:
23  std::string name() const override final;
24 
25  size_t update_granularity() const override final;
26 
27  size_t minimum_final_size() const override final;
28 
29  Key_Length_Specification key_spec() const override final;
30 
31  size_t output_length(size_t input_length) const override final;
32 
33  size_t default_nonce_length() const override final;
34 
35  bool valid_nonce_length(size_t n) const override final;
36 
37  void clear() override final;
38 
39  void reset() override final;
40  protected:
41  CFB_Mode(BlockCipher* cipher, size_t feedback_bits);
42 
43  void shift_register();
44 
45  size_t feedback() const { return m_feedback_bytes; }
46  const BlockCipher& cipher() const { return *m_cipher; }
47  size_t block_size() const { return m_block_size; }
48 
51  size_t m_keystream_pos = 0;
52 
53  private:
54  void start_msg(const uint8_t nonce[], size_t nonce_len) override;
55  void key_schedule(const uint8_t key[], size_t length) override;
56 
57  std::unique_ptr<BlockCipher> m_cipher;
58  const size_t m_block_size;
59  const size_t m_feedback_bytes;
60  };
61 
62 /**
63 * CFB Encryption
64 */
65 class BOTAN_PUBLIC_API(2,0) CFB_Encryption final : public CFB_Mode
66  {
67  public:
68  /**
69  * If feedback_bits is zero, cipher->block_size() bytes will be used.
70  * @param cipher block cipher to use
71  * @param feedback_bits number of bits fed back into the shift register,
72  * must be a multiple of 8
73  */
74  CFB_Encryption(BlockCipher* cipher, size_t feedback_bits) :
75  CFB_Mode(cipher, feedback_bits) {}
76 
77  size_t process(uint8_t buf[], size_t size) override;
78 
79  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
80  };
81 
82 /**
83 * CFB Decryption
84 */
85 class BOTAN_PUBLIC_API(2,0) CFB_Decryption final : public CFB_Mode
86  {
87  public:
88  /**
89  * If feedback_bits is zero, cipher->block_size() bytes will be used.
90  * @param cipher block cipher to use
91  * @param feedback_bits number of bits fed back into the shift register,
92  * must be a multiple of 8
93  */
94  CFB_Decryption(BlockCipher* cipher, size_t feedback_bits) :
95  CFB_Mode(cipher, feedback_bits) {}
96 
97  size_t process(uint8_t buf[], size_t size) override;
98 
99  void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
100  };
101 
102 }
103 
104 #endif
secure_vector< uint8_t > m_keystream
Definition: cfb.h:50
size_t feedback() const
Definition: cfb.h:45
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
CFB_Encryption(BlockCipher *cipher, size_t feedback_bits)
Definition: cfb.h:74
const BlockCipher & cipher() const
Definition: cfb.h:46
secure_vector< uint8_t > m_state
Definition: cfb.h:49
Definition: alg_id.cpp:13
size_t block_size() const
Definition: cfb.h:47
CFB_Decryption(BlockCipher *cipher, size_t feedback_bits)
Definition: cfb.h:94
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88