1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/*************************************************
* Base Classes Header File *
* (C) 1999-2007 Jack Lloyd *
*************************************************/
#ifndef BOTAN_BASE_H__
#define BOTAN_BASE_H__
#include <botan/exceptn.h>
#include <botan/symkey.h>
namespace Botan {
/*************************************************
* Constants *
*************************************************/
static const u32bit DEFAULT_BUFFERSIZE = BOTAN_DEFAULT_BUFFER_SIZE;
/**
* This class represents a symmetric algorithm object.
*/
class BOTAN_DLL SymmetricAlgorithm
{
public:
/**
* The maximum allowed key length.
*/
const u32bit MAXIMUM_KEYLENGTH;
/**
* The minimal allowed key length.
*/
const u32bit MINIMUM_KEYLENGTH;
/**
* A valid keylength is a multiple of this value.
*/
const u32bit KEYLENGTH_MULTIPLE;
/**
* The name of the algorithm.
* @return the name of the algorithm
*/
virtual std::string name() const = 0;
/**
* Set the symmetric key of this object.
* @param key the SymmetricKey to be set.
*/
void set_key(const SymmetricKey& key) throw(Invalid_Key_Length);
/**
* Set the symmetric key of this object.
* @param key the to be set as a byte array.
* @param the length of the byte array.
*/
void set_key(const byte key[], u32bit length) throw(Invalid_Key_Length);
/**
* Check whether a given key length is valid for this algorithm.
* @param length the key length to be checked.
* @return true if the key length is valid.
*/
bool valid_keylength(u32bit length) const;
/**
* Construct a SymmetricAlgorithm.
* @param key_min the minimum allowed key length
* @param key_max the maximum allowed key length
* @param key_mod any valid key length must be a multiple of this value
*/
SymmetricAlgorithm(u32bit key_min, u32bit key_max, u32bit key_mod);
virtual ~SymmetricAlgorithm() {}
private:
virtual void key(const byte[], u32bit) = 0;
};
/**
* This class represents a block cipher object.
*/
class BOTAN_DLL BlockCipher : public SymmetricAlgorithm
{
public:
/**
* The block size of this algorithm.
*/
const u32bit BLOCK_SIZE;
/**
* Encrypt a block.
* @param in The plaintext block to be encrypted as a byte array.
* Must be of length BLOCK_SIZE.
* @param out The byte array designated to hold the encrypted block.
* Must be of length BLOCK_SIZE.
*/
void encrypt(const byte in[], byte out[]) const { enc(in, out); }
/**
* Decrypt a block.
* @param in The ciphertext block to be decypted as a byte array.
* Must be of length BLOCK_SIZE.
* @param out The byte array designated to hold the decrypted block.
* Must be of length BLOCK_SIZE.
*/
void decrypt(const byte in[], byte out[]) const { dec(in, out); }
/**
* Encrypt a block.
* @param in The plaintext block to be encrypted as a byte array.
* Must be of length BLOCK_SIZE. Will hold the result when the function
* has finished.
*/
void encrypt(byte block[]) const { enc(block, block); }
/**
* Decrypt a block.
* @param in The ciphertext block to be decrypted as a byte array.
* Must be of length BLOCK_SIZE. Will hold the result when the function
* has finished.
*/
void decrypt(byte block[]) const { dec(block, block); }
/**
* Get a new object representing the same algorithm as *this
*/
virtual BlockCipher* clone() const = 0;
/**
* Zeroize internal state
*/
virtual void clear() throw() = 0;
BlockCipher(u32bit, u32bit, u32bit = 0, u32bit = 1);
virtual ~BlockCipher() {}
private:
virtual void enc(const byte[], byte[]) const = 0;
virtual void dec(const byte[], byte[]) const = 0;
};
/*************************************************
* Stream Cipher *
*************************************************/
class BOTAN_DLL StreamCipher : public SymmetricAlgorithm
{
public:
const u32bit IV_LENGTH;
/**
* Encrypt a message.
* @param i the plaintext
* @param o the byte array to hold the output, i.e. the ciphertext
* @param len the length of both i and o
*/
void encrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); }
/**
* Decrypt a message.
* @param i the ciphertext to decrypt
* @param o the byte array to hold the output, i.e. the plaintext
* @param len the length of both i and o
*/
void decrypt(const byte i[], byte o[], u32bit len) { cipher(i, o, len); }
/**
* Encrypt a message.
* @param in the plaintext as input, after the function has
* returned it will hold the ciphertext
* @param len the length of in
*/
void encrypt(byte in[], u32bit len) { cipher(in, in, len); }
/**
* Decrypt a message.
* @param in the ciphertext as input, after the function has
* returned it will hold the plaintext
* @param len the length of in
*/
void decrypt(byte in[], u32bit len) { cipher(in, in, len); }
/**
* Resync the cipher using the IV
* @param iv the initialization vector
* @param iv_len the length of the IV in bytes
*/
virtual void resync(const byte iv[], u32bit iv_len);
/**
* Seek ahead in the stream.
* @param len the length to seek ahead.
*/
virtual void seek(u32bit len);
/**
* Get a new object representing the same algorithm as *this
*/
virtual StreamCipher* clone() const = 0;
/**
* Zeroize internal state
*/
virtual void clear() throw() = 0;
StreamCipher(u32bit, u32bit = 0, u32bit = 1, u32bit = 0);
virtual ~StreamCipher() {}
private:
virtual void cipher(const byte[], byte[], u32bit) = 0;
};
/**
* This class represents any kind of computation which
* uses an internal state,
* such as hash functions.
*/
class BOTAN_DLL BufferedComputation
{
public:
/**
* The length of the output of this function in bytes.
*/
const u32bit OUTPUT_LENGTH;
/**
* Add new input to process.
* @param in the input to process as a byte array
* @param the length of the byte array
*/
void update(const byte in[], u32bit length);
/**
* Add new input to process.
* @param in the input to process as a MemoryRegion
*/
void update(const MemoryRegion<byte>& in);
/**
* Add new input to process.
* @param in the input to process as a std::string. Will be interpreted
* as a byte array based on
* the strings encoding.
*/
void update(const std::string&);
/**
* Process a single byte.
* @param in the byte to process
*/
void update(byte in);
/**
* Complete the computation and retrieve the
* final result.
* @param out The byte array to be filled with the result.
* Must be of length OUTPUT_LENGTH.
*/
void final(byte out[]) { final_result(out); }
/**
* Complete the computation and retrieve the
* final result.
* @return a SecureVector holding the result
*/
SecureVector<byte> final();
/**
* Update and finalize computation. Does the same as calling update()
* and final() consecutively.
* @param in the input to process as a byte array
* @param length the length of the byte array
* @result the result of the call to final()
*/
SecureVector<byte> process(const byte in[], u32bit length);
/**
* Update and finalize computation. Does the same as calling update()
* and final() consecutively.
* @param in the input to process
* @result the result of the call to final()
*/
SecureVector<byte> process(const MemoryRegion<byte>& in);
/**
* Update and finalize computation. Does the same as calling update()
* and final() consecutively.
* @param in the input to process as a string
* @result the result of the call to final()
*/
SecureVector<byte> process(const std::string& in);
BufferedComputation(u32bit);
virtual ~BufferedComputation() {}
private:
BufferedComputation& operator=(const BufferedComputation&);
virtual void add_data(const byte[], u32bit) = 0;
virtual void final_result(byte[]) = 0;
};
/**
* This class represents Message Authentication Code (MAC) objects.
*/
class BOTAN_DLL MessageAuthenticationCode : public BufferedComputation,
public SymmetricAlgorithm
{
public:
/**
* Verify a MAC.
* @param in the MAC to verify as a byte array
* @param length the length of the byte array
* @return true if the MAC is valid, false otherwise
*/
virtual bool verify_mac(const byte[], u32bit);
/**
* Get a new object representing the same algorithm as *this
*/
virtual MessageAuthenticationCode* clone() const = 0;
/**
* Get the name of this algorithm.
* @return the name of this algorithm
*/
virtual std::string name() const = 0;
/**
* Reset the internal state of this object.
*/
virtual void clear() throw() = 0;
MessageAuthenticationCode(u32bit, u32bit, u32bit = 0, u32bit = 1);
virtual ~MessageAuthenticationCode() {}
};
}
#endif
|