Botan  1.10.9
cts.cpp
Go to the documentation of this file.
1 /*
2 * CTS Mode
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/cts.h>
9 #include <botan/internal/xor_buf.h>
10 #include <algorithm>
11 
12 namespace Botan {
13 
14 /*
15 * CTS Encryption Constructor
16 */
18  cipher(ciph)
19  {
20  buffer.resize(2 * cipher->block_size());
21  state.resize(cipher->block_size());
22  position = 0;
23  }
24 
25 /*
26 * CTS Encryption Constructor
27 */
29  const SymmetricKey& key,
30  const InitializationVector& iv) :
31  cipher(ciph)
32  {
33  buffer.resize(2 * cipher->block_size());
34  state.resize(cipher->block_size());
35  position = 0;
36 
37  set_key(key);
38  set_iv(iv);
39  }
40 
41 /*
42 * Set the IV
43 */
45  {
46  if(!valid_iv_length(iv.length()))
47  throw Invalid_IV_Length(name(), iv.length());
48 
49  state = iv.bits_of();
50  zeroise(buffer);
51  position = 0;
52  }
53 
54 /*
55 * Encrypt a block
56 */
57 void CTS_Encryption::encrypt(const byte block[])
58  {
59  xor_buf(state, block, cipher->block_size());
60  cipher->encrypt(state);
61  send(state, cipher->block_size());
62  }
63 
64 /*
65 * Encrypt in CTS mode
66 */
67 void CTS_Encryption::write(const byte input[], size_t length)
68  {
69  size_t copied = std::min<size_t>(buffer.size() - position, length);
70  buffer.copy(position, input, copied);
71  length -= copied;
72  input += copied;
73  position += copied;
74 
75  if(length == 0) return;
76 
77  encrypt(&buffer[0]);
78  if(length > cipher->block_size())
79  {
80  encrypt(&buffer[cipher->block_size()]);
81  while(length > 2*cipher->block_size())
82  {
83  encrypt(input);
84  length -= cipher->block_size();
85  input += cipher->block_size();
86  }
87  position = 0;
88  }
89  else
90  {
91  copy_mem(&buffer[0], &buffer[cipher->block_size()], cipher->block_size());
92  position = cipher->block_size();
93  }
94  buffer.copy(position, input, length);
95  position += length;
96  }
97 
98 /*
99 * Finish encrypting in CTS mode
100 */
101 void CTS_Encryption::end_msg()
102  {
103  if(position < cipher->block_size() + 1)
104  throw Encoding_Error(name() + ": insufficient data to encrypt");
105 
106  xor_buf(state, buffer, cipher->block_size());
107  cipher->encrypt(state);
108  SecureVector<byte> cn = state;
109  clear_mem(&buffer[position], buffer.size() - position);
110  encrypt(&buffer[cipher->block_size()]);
111  send(cn, position - cipher->block_size());
112  }
113 
114 /*
115 * CTS Decryption Constructor
116 */
118  cipher(ciph)
119  {
120  buffer.resize(2 * cipher->block_size());
121  state.resize(cipher->block_size());
122  temp.resize(cipher->block_size());
123  position = 0;
124  }
125 
126 /*
127 * CTS Decryption Constructor
128 */
130  const SymmetricKey& key,
131  const InitializationVector& iv) :
132  cipher(ciph)
133  {
134  buffer.resize(2 * cipher->block_size());
135  state.resize(cipher->block_size());
136  temp.resize(cipher->block_size());
137  position = 0;
138 
139  set_key(key);
140  set_iv(iv);
141  }
142 
143 /*
144 * Set the IV
145 */
147  {
148  if(!valid_iv_length(iv.length()))
149  throw Invalid_IV_Length(name(), iv.length());
150 
151  state = iv.bits_of();
152  zeroise(buffer);
153  position = 0;
154  }
155 
156 /*
157 * Decrypt a block
158 */
159 void CTS_Decryption::decrypt(const byte block[])
160  {
161  cipher->decrypt(block, &temp[0]);
162  xor_buf(temp, state, cipher->block_size());
163  send(temp, cipher->block_size());
164  state.copy(block, cipher->block_size());
165  }
166 
167 /*
168 * Decrypt in CTS mode
169 */
170 void CTS_Decryption::write(const byte input[], size_t length)
171  {
172  size_t copied = std::min<size_t>(buffer.size() - position, length);
173  buffer.copy(position, input, copied);
174  length -= copied;
175  input += copied;
176  position += copied;
177 
178  if(length == 0) return;
179 
180  decrypt(buffer);
181  if(length > cipher->block_size())
182  {
183  decrypt(&buffer[cipher->block_size()]);
184  while(length > 2*cipher->block_size())
185  {
186  decrypt(input);
187  length -= cipher->block_size();
188  input += cipher->block_size();
189  }
190  position = 0;
191  }
192  else
193  {
194  copy_mem(&buffer[0], &buffer[cipher->block_size()], cipher->block_size());
195  position = cipher->block_size();
196  }
197  buffer.copy(position, input, length);
198  position += length;
199  }
200 
201 /*
202 * Finish decrypting in CTS mode
203 */
204 void CTS_Decryption::end_msg()
205  {
206  cipher->decrypt(buffer, temp);
207  xor_buf(temp, &buffer[cipher->block_size()], position - cipher->block_size());
208 
209  SecureVector<byte> xn = temp;
210 
211  copy_mem(&buffer[position],
212  &xn[position - cipher->block_size()],
213  buffer.size() - position);
214 
215  cipher->decrypt(&buffer[cipher->block_size()], temp);
216  xor_buf(temp, state, cipher->block_size());
217  send(temp, cipher->block_size());
218  send(xn, position - cipher->block_size());
219  }
220 
221 }
void resize(size_t n)
Definition: secmem.h:211
std::string name() const
Definition: cts.h:57
void decrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:57
size_t block_size
Definition: ossl_md.cpp:41
bool valid_iv_length(size_t iv_len) const
Definition: cts.h:66
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:32
void copy(const T in[], size_t n)
Definition: secmem.h:120
size_t length() const
Definition: symkey.h:25
unsigned char byte
Definition: types.h:22
SecureVector< byte > bits_of() const
Definition: symkey.h:30
std::string name() const
Definition: cts.h:22
void send(const byte in[], size_t length)
Definition: filter.cpp:28
void set_iv(const InitializationVector &)
Definition: cts.cpp:44
void set_key(const SymmetricKey &key)
Definition: cts.h:61
void set_iv(const InitializationVector &)
Definition: cts.cpp:146
size_t size() const
Definition: secmem.h:29
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
CTS_Encryption(BlockCipher *cipher)
Definition: cts.cpp:17
CTS_Decryption(BlockCipher *cipher)
Definition: cts.cpp:117
void encrypt(const byte in[], byte out[]) const
Definition: block_cipher.h:47
bool valid_iv_length(size_t iv_len) const
Definition: cts.h:31
void xor_buf(byte out[], const byte in[], size_t length)
Definition: xor_buf.h:21
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
virtual size_t block_size() const =0
void set_key(const SymmetricKey &key)
Definition: cts.h:26