/src/botan/build/include/botan/internal/cfb.h
Line | Count | Source |
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 | | */ |
20 | | class CFB_Mode : public Cipher_Mode |
21 | | { |
22 | | public: |
23 | | std::string name() const override final; |
24 | | |
25 | | size_t update_granularity() const override final; |
26 | | |
27 | | size_t ideal_granularity() const override final; |
28 | | |
29 | | size_t minimum_final_size() const override final; |
30 | | |
31 | | Key_Length_Specification key_spec() const override final; |
32 | | |
33 | | size_t output_length(size_t input_length) const override final; |
34 | | |
35 | | size_t default_nonce_length() const override final; |
36 | | |
37 | | bool valid_nonce_length(size_t n) const override final; |
38 | | |
39 | | void clear() override final; |
40 | | |
41 | | void reset() override final; |
42 | | |
43 | | bool has_keying_material() const override final; |
44 | | protected: |
45 | | CFB_Mode(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits); |
46 | | |
47 | | void shift_register(); |
48 | | |
49 | 6.96k | size_t feedback() const { return m_feedback_bytes; } |
50 | 6.73k | const BlockCipher& cipher() const { return *m_cipher; } |
51 | 4.18k | size_t block_size() const { return m_block_size; } |
52 | | |
53 | | secure_vector<uint8_t> m_state; |
54 | | secure_vector<uint8_t> m_keystream; |
55 | | size_t m_keystream_pos = 0; |
56 | | |
57 | | private: |
58 | | void start_msg(const uint8_t nonce[], size_t nonce_len) override; |
59 | | void key_schedule(const uint8_t key[], size_t length) override; |
60 | | |
61 | | std::unique_ptr<BlockCipher> m_cipher; |
62 | | const size_t m_block_size; |
63 | | const size_t m_feedback_bytes; |
64 | | }; |
65 | | |
66 | | /** |
67 | | * CFB Encryption |
68 | | */ |
69 | | class CFB_Encryption final : public CFB_Mode |
70 | | { |
71 | | public: |
72 | | /** |
73 | | * If feedback_bits is zero, cipher->block_size() bytes will be used. |
74 | | * @param cipher block cipher to use |
75 | | * @param feedback_bits number of bits fed back into the shift register, |
76 | | * must be a multiple of 8 |
77 | | */ |
78 | | CFB_Encryption(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits) : |
79 | 719 | CFB_Mode(std::move(cipher), feedback_bits) {} |
80 | | |
81 | | size_t process(uint8_t buf[], size_t size) override; |
82 | | |
83 | | void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
84 | | }; |
85 | | |
86 | | /** |
87 | | * CFB Decryption |
88 | | */ |
89 | | class CFB_Decryption final : public CFB_Mode |
90 | | { |
91 | | public: |
92 | | /** |
93 | | * If feedback_bits is zero, cipher->block_size() bytes will be used. |
94 | | * @param cipher block cipher to use |
95 | | * @param feedback_bits number of bits fed back into the shift register, |
96 | | * must be a multiple of 8 |
97 | | */ |
98 | | CFB_Decryption(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits) : |
99 | 765 | CFB_Mode(std::move(cipher), feedback_bits) {} |
100 | | |
101 | | size_t process(uint8_t buf[], size_t size) override; |
102 | | |
103 | | void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
104 | | }; |
105 | | |
106 | | } |
107 | | |
108 | | #endif |