Botan 3.13.0
Crypto and TLS for C&
cfb.cpp
Go to the documentation of this file.
1/*
2* CFB Mode
3* (C) 1999-2007,2013,2017 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#include <botan/internal/cfb.h>
10
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/fmt.h>
14
15namespace Botan {
16
17CFB_Mode::CFB_Mode(std::unique_ptr<BlockCipher> cipher, size_t feedback_bits) :
18 m_cipher(std::move(cipher)),
19 m_block_size(m_cipher->block_size()),
20 m_feedback_bytes(feedback_bits != 0 ? feedback_bits / 8 : m_block_size) {
21 if(feedback_bits % 8 != 0 || feedback() > m_block_size) {
22 throw Invalid_Argument(fmt("{} does not support feedback bits of {}", name(), feedback_bits));
23 }
24}
25
27 m_cipher->clear();
28 m_keystream.clear();
29 reset();
30}
31
33 m_state.clear();
36}
37
38std::string CFB_Mode::name() const {
39 if(feedback() == cipher().block_size()) {
40 return fmt("{}/CFB", cipher().name());
41 } else {
42 return fmt("{}/CFB({})", cipher().name(), feedback() * 8);
43 }
44}
45
46size_t CFB_Mode::output_length(size_t input_length) const {
47 return input_length;
48}
49
51 return feedback();
52}
53
55 // Multiplier here is arbitrary
56 return 16 * feedback();
57}
58
60 return 0;
61}
62
66
68 return block_size();
69}
70
71bool CFB_Mode::valid_nonce_length(size_t n) const {
72 return (n == 0 || n == block_size());
73}
74
76 return m_cipher->has_keying_material();
77}
78
79void CFB_Mode::key_schedule(std::span<const uint8_t> key) {
80 m_cipher->set_key(key);
81 m_keystream.resize(m_cipher->block_size());
82
83 // Drop the IV/feedback register and keystream from any prior message;
84 // they were computed under the previous key.
85 reset();
86}
87
88void CFB_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
89 if(!valid_nonce_length(nonce_len)) {
90 throw Invalid_IV_Length(name(), nonce_len);
91 }
92
94
95 if(nonce_len == 0) {
96 if(m_state.empty()) {
97 throw Invalid_State("CFB requires a non-empty initial nonce");
98 }
99 // No reason to encrypt state->keystream_buf, because no change
100 } else {
101 m_state.assign(nonce, nonce + nonce_len);
103 m_keystream_pos = 0;
104 }
105}
106
108 const size_t shift = feedback();
109 const size_t carryover = block_size() - shift;
110
111 if(carryover > 0) {
112 copy_mem(m_state.data(), &m_state[shift], carryover);
113 }
114 copy_mem(&m_state[carryover], m_keystream.data(), shift);
116 m_keystream_pos = 0;
117}
118
119size_t CFB_Encryption::process_msg(uint8_t buf[], size_t sz) {
121 BOTAN_STATE_CHECK(m_state.empty() == false);
122
123 const size_t shift = feedback();
124
125 size_t left = sz;
126
127 if(m_keystream_pos != 0) {
128 const size_t take = std::min<size_t>(left, shift - m_keystream_pos);
129
130 xor_buf(m_keystream.data() + m_keystream_pos, buf, take);
131 copy_mem(buf, m_keystream.data() + m_keystream_pos, take);
132
133 m_keystream_pos += take;
134 left -= take;
135 buf += take;
136
137 if(m_keystream_pos == shift) {
139 }
140 }
141
142 while(left >= shift) {
143 xor_buf(m_keystream.data(), buf, shift);
144 copy_mem(buf, m_keystream.data(), shift);
145
146 left -= shift;
147 buf += shift;
149 }
150
151 if(left > 0) {
152 xor_buf(m_keystream.data(), buf, left);
153 copy_mem(buf, m_keystream.data(), left);
154 m_keystream_pos += left;
155 }
156
157 return sz;
158}
159
160void CFB_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
161 update(buffer, offset);
162}
163
164namespace {
165
166inline void xor_copy(uint8_t buf[], uint8_t key_buf[], size_t len) {
167 for(size_t i = 0; i != len; ++i) {
168 const uint8_t k = key_buf[i];
169 key_buf[i] = buf[i];
170 buf[i] ^= k;
171 }
172}
173
174} // namespace
175
176size_t CFB_Decryption::process_msg(uint8_t buf[], size_t sz) {
178 BOTAN_STATE_CHECK(m_state.empty() == false);
179
180 const size_t shift = feedback();
181
182 size_t left = sz;
183
184 if(m_keystream_pos != 0) {
185 const size_t take = std::min<size_t>(left, shift - m_keystream_pos);
186
187 xor_copy(buf, m_keystream.data() + m_keystream_pos, take);
188
189 m_keystream_pos += take;
190 left -= take;
191 buf += take;
192
193 if(m_keystream_pos == shift) {
195 }
196 }
197
198 while(left >= shift) {
199 xor_copy(buf, m_keystream.data(), shift);
200 left -= shift;
201 buf += shift;
203 }
204
205 if(left > 0) {
206 xor_copy(buf, m_keystream.data(), left);
207 m_keystream_pos += left;
208 }
209
210 return sz;
211}
212
213void CFB_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
214 update(buffer, offset);
215}
216
217} // namespace Botan
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
void encrypt(const uint8_t in[], uint8_t out[]) const
size_t ideal_granularity() const final
Definition cfb.cpp:54
size_t block_size() const
Definition cfb.h:53
secure_vector< uint8_t > m_keystream
Definition cfb.h:56
std::string name() const final
Definition cfb.cpp:38
CFB_Mode(std::unique_ptr< BlockCipher > cipher, size_t feedback_bits)
Definition cfb.cpp:17
void shift_register()
Definition cfb.cpp:107
Key_Length_Specification key_spec() const final
Definition cfb.cpp:63
size_t default_nonce_length() const final
Definition cfb.cpp:67
size_t feedback() const
Definition cfb.h:49
bool has_keying_material() const final
Definition cfb.cpp:75
size_t output_length(size_t input_length) const final
Definition cfb.cpp:46
secure_vector< uint8_t > m_state
Definition cfb.h:55
void reset() final
Definition cfb.cpp:32
void clear() final
Definition cfb.cpp:26
const BlockCipher & cipher() const
Definition cfb.h:51
size_t m_keystream_pos
Definition cfb.h:57
size_t update_granularity() const final
Definition cfb.cpp:50
bool valid_nonce_length(size_t n) const final
Definition cfb.cpp:71
size_t minimum_final_size() const final
Definition cfb.cpp:59
void update(T &buffer, size_t offset=0)
void assert_key_material_set() const
Definition sym_algo.h:180
virtual Key_Length_Specification key_spec() const =0
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:241
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:403
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128