Botan  1.11.34
Crypto and TLS for C++11
cfb.cpp
Go to the documentation of this file.
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 #include <botan/cfb.h>
10 #include <botan/parsing.h>
11 
12 namespace Botan {
13 
14 CFB_Mode::CFB_Mode(BlockCipher* cipher, size_t feedback_bits) :
15  m_cipher(cipher),
16  m_feedback_bytes(feedback_bits ? feedback_bits / 8 : cipher->block_size())
17  {
18  if(feedback_bits % 8 || feedback() > cipher->block_size())
19  throw Invalid_Argument(name() + ": feedback bits " +
20  std::to_string(feedback_bits) + " not supported");
21  }
22 
24  {
25  m_cipher->clear();
26  reset();
27  }
28 
30  {
31  m_shift_register.clear();
32  m_keystream_buf.clear();
33  }
34 
35 std::string CFB_Mode::name() const
36  {
37  if(feedback() == cipher().block_size())
38  return cipher().name() + "/CFB";
39  else
40  return cipher().name() + "/CFB(" + std::to_string(feedback()*8) + ")";
41  }
42 
43 size_t CFB_Mode::output_length(size_t input_length) const
44  {
45  return input_length;
46  }
47 
49  {
50  return feedback();
51  }
52 
54  {
55  return 0;
56  }
57 
59  {
60  return cipher().key_spec();
61  }
62 
64  {
65  return cipher().block_size();
66  }
67 
68 bool CFB_Mode::valid_nonce_length(size_t n) const
69  {
70  return (n == cipher().block_size());
71  }
72 
73 void CFB_Mode::key_schedule(const byte key[], size_t length)
74  {
75  m_cipher->set_key(key, length);
76  }
77 
78 void CFB_Mode::start_msg(const byte nonce[], size_t nonce_len)
79  {
80  if(!valid_nonce_length(nonce_len))
81  throw Invalid_IV_Length(name(), nonce_len);
82 
83  m_shift_register.assign(nonce, nonce + nonce_len);
84  m_keystream_buf.resize(m_shift_register.size());
85  cipher().encrypt(m_shift_register, m_keystream_buf);
86  }
87 
88 size_t CFB_Encryption::process(uint8_t buf[], size_t sz)
89  {
90  const size_t BS = cipher().block_size();
91 
93  const size_t shift = feedback();
94  size_t left = sz;
95 
96  while(left)
97  {
98  const size_t took = std::min(shift, left);
99  xor_buf(buf, &keystream_buf()[0], took);
100 
101  // Assumes feedback-sized block except for last input
102  if (BS - shift > 0)
103  {
104  copy_mem(state.data(), &state[shift], BS - shift);
105  }
106  copy_mem(&state[BS-shift], buf, took);
107  cipher().encrypt(state, keystream_buf());
108 
109  buf += took;
110  left -= took;
111  }
112  return sz;
113  }
114 
115 void CFB_Encryption::finish(secure_vector<byte>& buffer, size_t offset)
116  {
117  update(buffer, offset);
118  }
119 
120 size_t CFB_Decryption::process(uint8_t buf[], size_t sz)
121  {
122  const size_t BS = cipher().block_size();
123 
125  const size_t shift = feedback();
126  size_t left = sz;
127 
128  while(left)
129  {
130  const size_t took = std::min(shift, left);
131 
132  // first update shift register with ciphertext
133  if (BS - shift > 0)
134  {
135  copy_mem(state.data(), &state[shift], BS - shift);
136  }
137  copy_mem(&state[BS-shift], buf, took);
138 
139  // then decrypt
140  xor_buf(buf, &keystream_buf()[0], took);
141 
142  // then update keystream
143  cipher().encrypt(state, keystream_buf());
144 
145  buf += took;
146  left -= took;
147  }
148  return sz;
149  }
150 
151 void CFB_Decryption::finish(secure_vector<byte>& buffer, size_t offset)
152  {
153  update(buffer, offset);
154  }
155 
156 }
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
size_t update_granularity() const override
Definition: cfb.cpp:48
void update(secure_vector< byte > &buffer, size_t offset=0)
Definition: cipher_mode.h:81
secure_vector< byte > & shift_register()
Definition: cfb.h:47
size_t feedback() const
Definition: cfb.h:45
Key_Length_Specification key_spec() const override
Definition: cfb.cpp:58
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
CFB_Mode(BlockCipher *cipher, size_t feedback_bits)
Definition: cfb.cpp:14
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:113
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:80
virtual std::string name() const =0
void finish(secure_vector< byte > &final_block, size_t offset=0) override
Definition: cfb.cpp:115
size_t output_length(size_t input_length) const override
Definition: cfb.cpp:43
secure_vector< byte > & keystream_buf()
Definition: cfb.h:49
const BlockCipher & cipher() const
Definition: cfb.h:43
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Definition: alg_id.cpp:13
size_t default_nonce_length() const override
Definition: cfb.cpp:63
size_t process(uint8_t buf[], size_t size) override
Definition: cfb.cpp:120
bool valid_nonce_length(size_t n) const override
Definition: cfb.cpp:68
void clear() override
Definition: cfb.cpp:23
virtual Key_Length_Specification key_spec() const =0
void reset() override
Definition: cfb.cpp:29
std::string name() const override
Definition: cfb.cpp:35
T min(T a, T b)
Definition: ct_utils.h:180
size_t process(uint8_t buf[], size_t size) override
Definition: cfb.cpp:88
virtual size_t block_size() const =0
void finish(secure_vector< byte > &final_block, size_t offset=0) override
Definition: cfb.cpp:151
size_t minimum_final_size() const override
Definition: cfb.cpp:53
std::uint8_t byte
Definition: types.h:31