Botan 1.10.17
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
12namespace 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*/
57void 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*/
67void 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*/
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*/
159void 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*/
170void 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*/
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 encrypt(const byte in[], byte out[]) const
void decrypt(const byte in[], byte out[]) const
virtual size_t block_size() const =0
void set_key(const SymmetricKey &key)
Definition cts.h:61
std::string name() const
Definition cts.h:57
CTS_Decryption(BlockCipher *cipher)
Definition cts.cpp:117
void set_iv(const InitializationVector &)
Definition cts.cpp:146
bool valid_iv_length(size_t iv_len) const
Definition cts.h:66
std::string name() const
Definition cts.h:22
bool valid_iv_length(size_t iv_len) const
Definition cts.h:31
void set_iv(const InitializationVector &)
Definition cts.cpp:44
void set_key(const SymmetricKey &key)
Definition cts.h:26
CTS_Encryption(BlockCipher *cipher)
Definition cts.cpp:17
void send(const byte in[], size_t length)
Definition filter.cpp:28
virtual void end_msg()
Definition filter.h:44
size_t size() const
Definition secmem.h:29
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
void xor_buf(byte out[], const byte in[], size_t length)
Definition xor_buf.h:21
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
OctetString InitializationVector
Definition symkey.h:152
void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:32
Encoding_Error(const std::string &name)
Definition exceptn.h:131
Invalid_IV_Length(const std::string &mode, size_t bad_len)
Definition exceptn.h:80