Botan: src/filters/modes/cfb/cfb.cpp Source File

Botan 1.10.16

cfb.cpp
Go to the documentation of this file.
1 /*
2 * CFB Mode
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/cfb.h>
9 #include <botan/parsing.h>
10 #include <botan/internal/xor_buf.h>
11 #include <algorithm>
12
13 namespace Botan {
14
15 /*
16 * CFB Encryption Constructor
17 */
18 CFB_Encryption::CFB_Encryption(BlockCipher* ciph, size_t fback_bits)
19 {
20 cipher = ciph;
21 feedback = fback_bits ? fback_bits / 8: cipher->block_size();
22
23 buffer.resize(cipher->block_size());
24 state.resize(cipher->block_size());
25 position = 0;
26
27 if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
28 throw Invalid_Argument("CFB_Encryption: Invalid feedback size " +
29 to_string(fback_bits));
30 }
31
32 /*
33 * CFB Encryption Constructor
34 */
35 CFB_Encryption::CFB_Encryption(BlockCipher* ciph,
36 const SymmetricKey& key,
37 const InitializationVector& iv,
38 size_t fback_bits)
39 {
40 cipher = ciph;
41 feedback = fback_bits ? fback_bits / 8: cipher->block_size();
42
43 buffer.resize(cipher->block_size());
44 state.resize(cipher->block_size());
45 position = 0;
46
47 if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
48 throw Invalid_Argument("CFB_Encryption: Invalid feedback size " +
49 to_string(fback_bits));
50
51 set_key(key);
52 set_iv(iv);
53 }
54
55 void CFB_Encryption::set_iv(const InitializationVector& iv)
56 {
57 if(!valid_iv_length(iv.length()))
58 throw Invalid_IV_Length(name(), iv.length());
59
60 state = iv.bits_of();
61 zeroise(buffer);
62 position = 0;
63
64 cipher->encrypt(state, buffer);
65 }
66
67 /*
68 * Encrypt data in CFB mode
69 */
70 void CFB_Encryption::write(const byte input[], size_t length)
71 {
72 while(length)
73 {
74 size_t xored = std::min(feedback - position, length);
75 xor_buf(&buffer[position], input, xored);
76 send(&buffer[position], xored);
77 input += xored;
78 length -= xored;
79 position += xored;
80
81 if(position == feedback)
82 {
83 for(size_t j = 0; j != cipher->block_size() - feedback; ++j)
84 state[j] = state[j + feedback];
85 state.copy(cipher->block_size() - feedback, buffer, feedback);
86 cipher->encrypt(state, buffer);
87 position = 0;
88 }
89 }
90 }
91
92 /*
93 * CFB Decryption Constructor
94 */
95 CFB_Decryption::CFB_Decryption(BlockCipher* ciph, size_t fback_bits)
96 {
97 cipher = ciph;
98 feedback = fback_bits ? fback_bits / 8: cipher->block_size();
99
100 buffer.resize(cipher->block_size());
101 state.resize(cipher->block_size());
102 position = 0;
103
104 if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
105 throw Invalid_Argument("CFB_Decryption: Invalid feedback size " +
106 to_string(fback_bits));
107 }
108
109 /*
110 * CFB Decryption Constructor
111 */
112 CFB_Decryption::CFB_Decryption(BlockCipher* ciph,
113 const SymmetricKey& key,
114 const InitializationVector& iv,
115 size_t fback_bits)
116 {
117 cipher = ciph;
118 feedback = fback_bits ? fback_bits / 8: cipher->block_size();
119
120 buffer.resize(cipher->block_size());
121 state.resize(cipher->block_size());
122 position = 0;
123
124 if(feedback == 0 || fback_bits % 8 != 0 || feedback > cipher->block_size())
125 throw Invalid_Argument("CFB_Decryption: Invalid feedback size " +
126 to_string(fback_bits));
127
128 set_key(key);
129 set_iv(iv);
130 }
131
132 void CFB_Decryption::set_iv(const InitializationVector& iv)
133 {
134 if(!valid_iv_length(iv.length()))
135 throw Invalid_IV_Length(name(), iv.length());
136
137 state = iv.bits_of();
138 zeroise(buffer);
139 position = 0;
140
141 cipher->encrypt(state, buffer);
142 }
143
144 /*
145 * Decrypt data in CFB mode
146 */
147 void CFB_Decryption::write(const byte input[], size_t length)
148 {
149 while(length)
150 {
151 size_t xored = std::min(feedback - position, length);
152 xor_buf(&buffer[position], input, xored);
153 send(&buffer[position], xored);
154 buffer.copy(position, input, xored);
155 input += xored;
156 length -= xored;
157 position += xored;
158 if(position == feedback)
159 {
160 for(size_t j = 0; j != cipher->block_size() - feedback; ++j)
161 state[j] = state[j + feedback];
162 state.copy(cipher->block_size() - feedback, buffer, feedback);
163 cipher->encrypt(state, buffer);
164 position = 0;
165 }
166 }
167 }
168
169 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
Botan::OctetString
Definition: symkey.h:19
Botan::CFB_Encryption::name
std::string name() const
Definition: cfb.h:22
Botan::CFB_Decryption::CFB_Decryption
CFB_Decryption(BlockCipher *cipher, size_t feedback=0)
Definition: cfb.cpp:95
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::Invalid_IV_Length
Definition: exceptn.h:78
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:120
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::CFB_Encryption::set_iv
void set_iv(const InitializationVector &)
Definition: cfb.cpp:55
Botan::Filter::send
void send(const byte in[], size_t length)
Definition: filter.cpp:28
Botan::CFB_Encryption::CFB_Encryption
CFB_Encryption(BlockCipher *cipher, size_t feedback=0)
Definition: cfb.cpp:18
Botan::BlockCipher::encrypt
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
Botan
Definition: algo_base.h:14
Botan::OctetString::length
size_t length() const
Definition: symkey.h:25
Botan::CFB_Decryption::set_iv
void set_iv(const InitializationVector &)
Definition: cfb.cpp:132
Botan::CFB_Encryption::valid_iv_length
bool valid_iv_length(size_t iv_len) const
Definition: cfb.h:31
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::CFB_Encryption::set_key
void set_key(const SymmetricKey &key)
Definition: cfb.h:26
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::OctetString::bits_of
SecureVector< byte > bits_of() const
Definition: symkey.h:30
Botan::xor_buf
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
Botan::BlockCipher::block_size
virtual size_t block_size() const =0
Botan::BlockCipher
Definition: block_cipher.h:18