Botan 3.12.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
84void CFB_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
85 if(!valid_nonce_length(nonce_len)) {
86 throw Invalid_IV_Length(name(), nonce_len);
87 }
88
90
91 if(nonce_len == 0) {
92 if(m_state.empty()) {
93 throw Invalid_State("CFB requires a non-empty initial nonce");
94 }
95 // No reason to encrypt state->keystream_buf, because no change
96 } else {
97 m_state.assign(nonce, nonce + nonce_len);
100 }
101}
102
104 const size_t shift = feedback();
105 const size_t carryover = block_size() - shift;
106
107 if(carryover > 0) {
108 copy_mem(m_state.data(), &m_state[shift], carryover);
109 }
110 copy_mem(&m_state[carryover], m_keystream.data(), shift);
112 m_keystream_pos = 0;
113}
114
115size_t CFB_Encryption::process_msg(uint8_t buf[], size_t sz) {
117 BOTAN_STATE_CHECK(m_state.empty() == false);
118
119 const size_t shift = feedback();
120
121 size_t left = sz;
122
123 if(m_keystream_pos != 0) {
124 const size_t take = std::min<size_t>(left, shift - m_keystream_pos);
125
126 xor_buf(m_keystream.data() + m_keystream_pos, buf, take);
127 copy_mem(buf, m_keystream.data() + m_keystream_pos, take);
128
129 m_keystream_pos += take;
130 left -= take;
131 buf += take;
132
133 if(m_keystream_pos == shift) {
135 }
136 }
137
138 while(left >= shift) {
139 xor_buf(m_keystream.data(), buf, shift);
140 copy_mem(buf, m_keystream.data(), shift);
141
142 left -= shift;
143 buf += shift;
145 }
146
147 if(left > 0) {
148 xor_buf(m_keystream.data(), buf, left);
149 copy_mem(buf, m_keystream.data(), left);
150 m_keystream_pos += left;
151 }
152
153 return sz;
154}
155
156void CFB_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
157 update(buffer, offset);
158}
159
160namespace {
161
162inline void xor_copy(uint8_t buf[], uint8_t key_buf[], size_t len) {
163 for(size_t i = 0; i != len; ++i) {
164 const uint8_t k = key_buf[i];
165 key_buf[i] = buf[i];
166 buf[i] ^= k;
167 }
168}
169
170} // namespace
171
172size_t CFB_Decryption::process_msg(uint8_t buf[], size_t sz) {
174 BOTAN_STATE_CHECK(m_state.empty() == false);
175
176 const size_t shift = feedback();
177
178 size_t left = sz;
179
180 if(m_keystream_pos != 0) {
181 const size_t take = std::min<size_t>(left, shift - m_keystream_pos);
182
183 xor_copy(buf, m_keystream.data() + m_keystream_pos, take);
184
185 m_keystream_pos += take;
186 left -= take;
187 buf += take;
188
189 if(m_keystream_pos == shift) {
191 }
192 }
193
194 while(left >= shift) {
195 xor_copy(buf, m_keystream.data(), shift);
196 left -= shift;
197 buf += shift;
199 }
200
201 if(left > 0) {
202 xor_copy(buf, m_keystream.data(), left);
203 m_keystream_pos += left;
204 }
205
206 return sz;
207}
208
209void CFB_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
210 update(buffer, offset);
211}
212
213} // 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:103
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:145
virtual Key_Length_Specification key_spec() const =0
void zeroise(std::vector< T, Alloc > &vec)
Definition secmem.h:137
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:341
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68