Botan 1.10.17
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
13namespace Botan {
14
15/*
16* CFB Encryption Constructor
17*/
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*/
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
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*/
70void 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*/
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*/
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
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*/
147void 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}
void encrypt(const byte in[], byte out[]) const
virtual size_t block_size() const =0
CFB_Decryption(BlockCipher *cipher, size_t feedback=0)
Definition cfb.cpp:95
std::string name() const
Definition cfb.h:56
bool valid_iv_length(size_t iv_len) const
Definition cfb.h:65
void set_iv(const InitializationVector &)
Definition cfb.cpp:132
void set_key(const SymmetricKey &key)
Definition cfb.h:60
std::string name() const
Definition cfb.h:22
void set_key(const SymmetricKey &key)
Definition cfb.h:26
bool valid_iv_length(size_t iv_len) const
Definition cfb.h:31
CFB_Encryption(BlockCipher *cipher, size_t feedback=0)
Definition cfb.cpp:18
void set_iv(const InitializationVector &)
Definition cfb.cpp:55
void send(const byte in[], size_t length)
Definition filter.cpp:28
void copy(const T in[], size_t n)
Definition secmem.h:126
SecureVector< byte > bits_of() const
Definition symkey.h:30
size_t length() const
Definition symkey.h:25
OctetString SymmetricKey
Definition symkey.h:147
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428
std::string to_string(u64bit n, size_t min_len)
Definition parsing.cpp:42
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
OctetString InitializationVector
Definition symkey.h:152
Invalid_IV_Length(const std::string &mode, size_t bad_len)
Definition exceptn.h:80