Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
23 void CFB_Mode::clear()
24 {
25 m_cipher->clear();
26 reset();
27 }
28
29 void CFB_Mode::reset()
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
48 size_t CFB_Mode::update_granularity() const
49 {
50 return feedback();
51 }
52
53 size_t CFB_Mode::minimum_final_size() const
54 {
55 return 0;
56 }
57
58 Key_Length_Specification CFB_Mode::key_spec() const
59 {
60 return cipher().key_spec();
61 }
62
63 size_t CFB_Mode::default_nonce_length() const
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 uint8_t key[], size_t length)
74 {
75 m_cipher->set_key(key, length);
76 }
77
78 void CFB_Mode::start_msg(const uint8_t 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
92 secure_vector<uint8_t>& state = shift_register();
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<uint8_t>& 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
124 secure_vector<uint8_t>& state = shift_register();
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<uint8_t>& buffer, size_t offset)
152 {
153 update(buffer, offset);
154 }
155
156 }
Botan::xor_buf
void xor_buf(T out[], const T in[], size_t length)
Definition: mem_ops.h:115
Botan::BlockCipher::encrypt
void encrypt(const uint8_t in[], uint8_t out[]) const
Definition: block_cipher.h:80
Botan::Invalid_Argument
Definition: exceptn.h:34
Botan::CFB_Mode::update_granularity
size_t update_granularity() const override
Definition: cfb.cpp:48
Botan::CFB_Mode::cipher
const BlockCipher & cipher() const
Definition: cfb.h:43
Botan::CFB_Encryption::finish
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: cfb.cpp:115
Botan::CFB_Mode::key_spec
Key_Length_Specification key_spec() const override
Definition: cfb.cpp:58
Botan::Cipher_Mode::update
void update(secure_vector< uint8_t > &buffer, size_t offset=0)
Definition: cipher_mode.h:81
Botan::Key_Length_Specification
Definition: key_spec.h:18
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
Botan::CFB_Mode::CFB_Mode
CFB_Mode(BlockCipher *cipher, size_t feedback_bits)
Definition: cfb.cpp:14
Botan::CFB_Mode::shift_register
secure_vector< uint8_t > & shift_register()
Definition: cfb.h:47
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::SymmetricAlgorithm::name
virtual std::string name() const =0
Botan::CFB_Mode::output_length
size_t output_length(size_t input_length) const override
Definition: cfb.cpp:43
Botan::CFB_Decryption::finish
void finish(secure_vector< uint8_t > &final_block, size_t offset=0) override
Definition: cfb.cpp:151
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Botan
Definition: alg_id.cpp:13
Botan::CFB_Mode::feedback
size_t feedback() const
Definition: cfb.h:45
Botan::CFB_Mode::default_nonce_length
size_t default_nonce_length() const override
Definition: cfb.cpp:63
Botan::CFB_Decryption::process
size_t process(uint8_t buf[], size_t size) override
Definition: cfb.cpp:120
Botan::CFB_Mode::valid_nonce_length
bool valid_nonce_length(size_t n) const override
Definition: cfb.cpp:68
Botan::CFB_Mode::clear
void clear() override
Definition: cfb.cpp:23
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
Botan::CFB_Mode::reset
void reset() override
Definition: cfb.cpp:29
Botan::CFB_Mode::name
std::string name() const override
Definition: cfb.cpp:35
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:180
Botan::CFB_Mode::keystream_buf
secure_vector< uint8_t > & keystream_buf()
Definition: cfb.h:49
Botan::CFB_Encryption::process
size_t process(uint8_t buf[], size_t size) override
Definition: cfb.cpp:88
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::CFB_Mode::minimum_final_size
size_t minimum_final_size() const override
Definition: cfb.cpp:53
Botan::BlockCipher
Definition: block_cipher.h:19