/src/botan/build/include/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/cipher_mode.h> |
13 | | #include <botan/block_cipher.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 | | { |
23 | | public: |
24 | | std::string name() const override; |
25 | | |
26 | | size_t update_granularity() const override; |
27 | | |
28 | | Key_Length_Specification key_spec() const override; |
29 | | |
30 | | size_t default_nonce_length() const override; |
31 | | |
32 | | bool valid_nonce_length(size_t n) const override; |
33 | | |
34 | | void clear() override; |
35 | | |
36 | | void reset() override; |
37 | | |
38 | | protected: |
39 | | CBC_Mode(std::unique_ptr<BlockCipher> cipher, |
40 | | std::unique_ptr<BlockCipherModePaddingMethod> padding); |
41 | | |
42 | 2.82k | const BlockCipher& cipher() const { return *m_cipher; } |
43 | | |
44 | | const BlockCipherModePaddingMethod& padding() const |
45 | 0 | { |
46 | 0 | BOTAN_ASSERT_NONNULL(m_padding); |
47 | 0 | return *m_padding; |
48 | 0 | } |
49 | | |
50 | 490 | size_t block_size() const { return m_block_size; } |
51 | | |
52 | 407 | secure_vector<uint8_t>& state() { return m_state; } |
53 | | |
54 | 3.39k | uint8_t* state_ptr() { return m_state.data(); } |
55 | | |
56 | | private: |
57 | | void start_msg(const uint8_t nonce[], size_t nonce_len) override; |
58 | | |
59 | | void key_schedule(const uint8_t key[], size_t length) override; |
60 | | |
61 | | std::unique_ptr<BlockCipher> m_cipher; |
62 | | std::unique_ptr<BlockCipherModePaddingMethod> m_padding; |
63 | | secure_vector<uint8_t> m_state; |
64 | | size_t m_block_size; |
65 | | }; |
66 | | |
67 | | /** |
68 | | * CBC Encryption |
69 | | */ |
70 | | class CBC_Encryption : public CBC_Mode |
71 | | { |
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, |
78 | | std::unique_ptr<BlockCipherModePaddingMethod> padding) : |
79 | 132 | CBC_Mode(std::move(cipher), std::move(padding)) {} |
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 | | size_t output_length(size_t input_length) const override; |
86 | | |
87 | | size_t minimum_final_size() const override; |
88 | | }; |
89 | | |
90 | | /** |
91 | | * CBC Encryption with ciphertext stealing (CBC-CS3 variant) |
92 | | */ |
93 | | class CTS_Encryption final : public CBC_Encryption |
94 | | { |
95 | | public: |
96 | | /** |
97 | | * @param cipher block cipher to use |
98 | | */ |
99 | | explicit CTS_Encryption(std::unique_ptr<BlockCipher> cipher) : |
100 | | CBC_Encryption(std::move(cipher), nullptr) |
101 | 0 | {} |
102 | | |
103 | | size_t output_length(size_t input_length) const override; |
104 | | |
105 | | void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
106 | | |
107 | | size_t minimum_final_size() const override; |
108 | | |
109 | | bool valid_nonce_length(size_t n) const override; |
110 | | }; |
111 | | |
112 | | /** |
113 | | * CBC Decryption |
114 | | */ |
115 | | class CBC_Decryption : public CBC_Mode |
116 | | { |
117 | | public: |
118 | | /** |
119 | | * @param cipher block cipher to use |
120 | | * @param padding padding method to use |
121 | | */ |
122 | | CBC_Decryption(std::unique_ptr<BlockCipher> cipher, |
123 | | std::unique_ptr<BlockCipherModePaddingMethod> padding) : |
124 | | CBC_Mode(std::move(cipher), std::move(padding)), |
125 | | m_tempbuf(update_granularity()) |
126 | 241 | {} |
127 | | |
128 | | size_t process(uint8_t buf[], size_t size) override; |
129 | | |
130 | | void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
131 | | |
132 | | size_t output_length(size_t input_length) const override; |
133 | | |
134 | | size_t minimum_final_size() const override; |
135 | | |
136 | | void reset() override; |
137 | | |
138 | | private: |
139 | | secure_vector<uint8_t> m_tempbuf; |
140 | | }; |
141 | | |
142 | | /** |
143 | | * CBC Decryption with ciphertext stealing (CBC-CS3 variant) |
144 | | */ |
145 | | class CTS_Decryption final : public CBC_Decryption |
146 | | { |
147 | | public: |
148 | | /** |
149 | | * @param cipher block cipher to use |
150 | | */ |
151 | | explicit CTS_Decryption(std::unique_ptr<BlockCipher> cipher) : |
152 | | CBC_Decryption(std::move(cipher), nullptr) |
153 | 0 | {} |
154 | | |
155 | | void finish(secure_vector<uint8_t>& final_block, size_t offset = 0) override; |
156 | | |
157 | | size_t minimum_final_size() const override; |
158 | | |
159 | | bool valid_nonce_length(size_t n) const override; |
160 | | }; |
161 | | |
162 | | } |
163 | | |
164 | | #endif |