/src/botan/build/include/internal/botan/internal/cbc.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * CBC 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_CBC_H_ |
10 | | #define BOTAN_MODE_CBC_H_ |
11 | | |
12 | | #include <botan/block_cipher.h> |
13 | | #include <botan/cipher_mode.h> |
14 | | #include <botan/internal/mode_pad.h> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | /** |
19 | | * CBC Mode |
20 | | */ |
21 | | class CBC_Mode : public Cipher_Mode { |
22 | | public: |
23 | | std::string name() const final; |
24 | | |
25 | | size_t update_granularity() const final; |
26 | | |
27 | | size_t ideal_granularity() const final; |
28 | | |
29 | | Key_Length_Specification key_spec() const final; |
30 | | |
31 | | size_t default_nonce_length() const final; |
32 | | |
33 | | bool valid_nonce_length(size_t n) const override; |
34 | | |
35 | | void clear() final; |
36 | | |
37 | | void reset() override; |
38 | | |
39 | | bool has_keying_material() const final; |
40 | | |
41 | | protected: |
42 | | CBC_Mode(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding); |
43 | | |
44 | 583 | const BlockCipher& cipher() const { return *m_cipher; } |
45 | | |
46 | 0 | const BlockCipherModePaddingMethod& padding() const { |
47 | 0 | BOTAN_ASSERT_NONNULL(m_padding); |
48 | 0 | return *m_padding; |
49 | 0 | } |
50 | | |
51 | 138 | size_t block_size() const { return m_block_size; } |
52 | | |
53 | 91 | secure_vector<uint8_t>& state() { return m_state; } |
54 | | |
55 | 692 | uint8_t* state_ptr() { return m_state.data(); } |
56 | | |
57 | | private: |
58 | | void start_msg(const uint8_t nonce[], size_t nonce_len) override; |
59 | | |
60 | | void key_schedule(std::span<const uint8_t> key) override; |
61 | | |
62 | | std::unique_ptr<BlockCipher> m_cipher; |
63 | | std::unique_ptr<BlockCipherModePaddingMethod> m_padding; |
64 | | secure_vector<uint8_t> m_state; |
65 | | size_t m_block_size; |
66 | | }; |
67 | | |
68 | | /** |
69 | | * CBC Encryption |
70 | | */ |
71 | | class CBC_Encryption : public CBC_Mode { |
72 | | public: |
73 | | /** |
74 | | * @param cipher block cipher to use |
75 | | * @param padding padding method to use |
76 | | */ |
77 | | CBC_Encryption(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding) : |
78 | 13 | CBC_Mode(std::move(cipher), std::move(padding)) {} |
79 | | |
80 | | size_t output_length(size_t input_length) const override; |
81 | | |
82 | | size_t minimum_final_size() const override; |
83 | | |
84 | | private: |
85 | | size_t process_msg(uint8_t buf[], size_t size) override; |
86 | | void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
87 | | }; |
88 | | |
89 | | /** |
90 | | * CBC Encryption with ciphertext stealing (CBC-CS3 variant) |
91 | | */ |
92 | | class CTS_Encryption final : public CBC_Encryption { |
93 | | public: |
94 | | /** |
95 | | * @param cipher block cipher to use |
96 | | */ |
97 | 0 | explicit CTS_Encryption(std::unique_ptr<BlockCipher> cipher) : CBC_Encryption(std::move(cipher), nullptr) {} |
98 | | |
99 | | size_t output_length(size_t input_length) const override; |
100 | | |
101 | | size_t minimum_final_size() const override; |
102 | | |
103 | | bool valid_nonce_length(size_t n) const override; |
104 | | |
105 | | private: |
106 | | void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
107 | | }; |
108 | | |
109 | | /** |
110 | | * CBC Decryption |
111 | | */ |
112 | | class CBC_Decryption : public CBC_Mode { |
113 | | public: |
114 | | /** |
115 | | * @param cipher block cipher to use |
116 | | * @param padding padding method to use |
117 | | */ |
118 | | CBC_Decryption(std::unique_ptr<BlockCipher> cipher, std::unique_ptr<BlockCipherModePaddingMethod> padding) : |
119 | 75 | CBC_Mode(std::move(cipher), std::move(padding)), m_tempbuf(ideal_granularity()) {} |
120 | | |
121 | | size_t output_length(size_t input_length) const override; |
122 | | |
123 | | size_t minimum_final_size() const override; |
124 | | |
125 | | void reset() override; |
126 | | |
127 | | private: |
128 | | size_t process_msg(uint8_t buf[], size_t size) override; |
129 | | void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
130 | | |
131 | | secure_vector<uint8_t> m_tempbuf; |
132 | | }; |
133 | | |
134 | | /** |
135 | | * CBC Decryption with ciphertext stealing (CBC-CS3 variant) |
136 | | */ |
137 | | class CTS_Decryption final : public CBC_Decryption { |
138 | | public: |
139 | | /** |
140 | | * @param cipher block cipher to use |
141 | | */ |
142 | 0 | explicit CTS_Decryption(std::unique_ptr<BlockCipher> cipher) : CBC_Decryption(std::move(cipher), nullptr) {} |
143 | | |
144 | | size_t minimum_final_size() const override; |
145 | | |
146 | | bool valid_nonce_length(size_t n) const override; |
147 | | |
148 | | private: |
149 | | void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
150 | | }; |
151 | | |
152 | | } // namespace Botan |
153 | | |
154 | | #endif |