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
|
/*************************************************
* 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>
#include <botan/sym_algo.h>
namespace Botan {
/*************************************************
* Constants *
*************************************************/
static const u32bit DEFAULT_BUFFERSIZE = BOTAN_DEFAULT_BUFFER_SIZE;
/**
* 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 block_size,
u32bit key_min,
u32bit key_max = 0,
u32bit key_mod = 1) :
SymmetricAlgorithm(key_min, key_max, key_mod),
BLOCK_SIZE(block_size) {}
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 key_min, u32bit key_max = 0,
u32bit key_mod = 1,
u32bit iv_len = 0) :
SymmetricAlgorithm(key_min, key_max, key_mod),
IV_LENGTH(iv_len) {}
virtual ~StreamCipher() {}
private:
virtual void cipher(const byte[], byte[], u32bit) = 0;
};
}
#endif
|