1
/*************************************************
2
* Base Classes Source File *
3
* (C) 1999-2007 The Botan Project *
4
*************************************************/
6
#include <botan/base.h>
7
#include <botan/version.h>
8
#include <botan/parsing.h>
9
#include <botan/util.h>
10
#include <botan/config.h>
14
/*************************************************
15
* SymmetricAlgorithm Constructor *
16
*************************************************/
17
SymmetricAlgorithm::SymmetricAlgorithm(u32bit key_min, u32bit key_max,
19
MAXIMUM_KEYLENGTH(key_max ? key_max : key_min),
20
MINIMUM_KEYLENGTH(key_min),
21
KEYLENGTH_MULTIPLE(key_mod)
25
/*************************************************
26
* Query if the keylength is valid *
27
*************************************************/
28
bool SymmetricAlgorithm::valid_keylength(u32bit length) const
30
return ((length >= MINIMUM_KEYLENGTH) &&
31
(length <= MAXIMUM_KEYLENGTH) &&
32
(length % KEYLENGTH_MULTIPLE == 0));
35
/*************************************************
37
*************************************************/
38
void SymmetricAlgorithm::set_key(const SymmetricKey& algo_key)
39
throw(Invalid_Key_Length)
41
set_key(algo_key.begin(), algo_key.length());
44
/*************************************************
46
*************************************************/
47
void SymmetricAlgorithm::set_key(const byte algo_key[], u32bit length)
48
throw(Invalid_Key_Length)
50
if(!valid_keylength(length))
51
throw Invalid_Key_Length(name(), length);
52
key(algo_key, length);
55
/*************************************************
56
* BlockCipher Constructor *
57
*************************************************/
58
BlockCipher::BlockCipher(u32bit block, u32bit key_min, u32bit key_max,
60
SymmetricAlgorithm(key_min, key_max, key_mod),
65
/*************************************************
66
* StreamCipher Constructor *
67
*************************************************/
68
StreamCipher::StreamCipher(u32bit key_min, u32bit key_max, u32bit key_mod,
70
SymmetricAlgorithm(key_min, key_max, key_mod), IV_LENGTH(iv_len)
74
/*************************************************
75
* BufferedComputation Constructor *
76
*************************************************/
77
BufferedComputation::BufferedComputation(u32bit olen) : OUTPUT_LENGTH(olen)
81
/*************************************************
82
* HashFunction Constructor *
83
*************************************************/
84
HashFunction::HashFunction(u32bit hlen, u32bit blen) :
85
BufferedComputation(hlen), HASH_BLOCK_SIZE(blen)
89
/*************************************************
90
* MessageAuthenticationCode Constructor *
91
*************************************************/
92
MessageAuthenticationCode::MessageAuthenticationCode(u32bit mlen,
96
BufferedComputation(mlen),
97
SymmetricAlgorithm(key_min, key_max, key_mod)
101
/*************************************************
102
* Default MAC verification operation *
103
*************************************************/
104
bool MessageAuthenticationCode::verify_mac(const byte mac[], u32bit length)
106
SecureVector<byte> our_mac = final();
107
if(our_mac.size() != length)
109
for(u32bit j = 0; j != length; ++j)
110
if(mac[j] != our_mac[j])
115
/*************************************************
116
* Default StreamCipher Resync Operation *
117
*************************************************/
118
void StreamCipher::resync(const byte[], u32bit length)
121
throw Exception("The stream cipher " + name() +
122
" does not support resyncronization");
125
/*************************************************
126
* Default StreamCipher Seek Operation *
127
*************************************************/
128
void StreamCipher::seek(u32bit)
130
throw Exception("The stream cipher " + name() + " does not support seek()");
133
/*************************************************
135
*************************************************/
136
void BufferedComputation::update(const byte in[], u32bit n)
141
/*************************************************
143
*************************************************/
144
void BufferedComputation::update(const MemoryRegion<byte>& in)
146
add_data(in, in.size());
149
/*************************************************
151
*************************************************/
152
void BufferedComputation::update(const std::string& str)
154
update((const byte*)str.c_str(), str.size());
157
/*************************************************
159
*************************************************/
160
void BufferedComputation::update(byte in)
165
/*************************************************
167
*************************************************/
168
SecureVector<byte> BufferedComputation::final()
170
SecureVector<byte> output(OUTPUT_LENGTH);
171
final_result(output);
175
/*************************************************
177
*************************************************/
178
SecureVector<byte> BufferedComputation::process(const byte in[], u32bit len)
184
/*************************************************
186
*************************************************/
187
SecureVector<byte> BufferedComputation::process(const MemoryRegion<byte>& in)
189
update(in, in.size());
193
/*************************************************
195
*************************************************/
196
SecureVector<byte> BufferedComputation::process(const std::string& in)
202
/*************************************************
203
* Default fast poll for EntropySources *
204
*************************************************/
205
u32bit EntropySource::fast_poll(byte buf[], u32bit len)
207
return slow_poll(buf, len);
210
/*************************************************
211
* Add entropy to internal state *
212
*************************************************/
213
void RandomNumberGenerator::add_entropy(const byte random[], u32bit length)
215
add_randomness(random, length);
218
/*************************************************
219
* Add entropy to internal state *
220
*************************************************/
221
u32bit RandomNumberGenerator::add_entropy(EntropySource& source,
224
std::string poll_type;
226
poll_type = "rng/slow_poll_request";
228
poll_type = "rng/fast_poll_request";
230
u32bit poll_for = global_config().option_as_u32bit(poll_type);
232
SecureVector<byte> buffer(poll_for ? poll_for : 256);
234
u32bit bytes_gathered = 0;
237
bytes_gathered = source.slow_poll(buffer, buffer.size());
239
bytes_gathered = source.fast_poll(buffer, buffer.size());
241
add_entropy(buffer, bytes_gathered);
243
return entropy_estimate(buffer, bytes_gathered);
246
/*************************************************
247
* Return the version as a string *
248
*************************************************/
249
std::string version_string()
251
return "Botan " + to_string(version_major()) + "." +
252
to_string(version_minor()) + "." +
253
to_string(version_patch());
256
/*************************************************
257
* Return parts of the version as integers *
258
*************************************************/
259
u32bit version_major() { return BOTAN_VERSION_MAJOR; }
260
u32bit version_minor() { return BOTAN_VERSION_MINOR; }
261
u32bit version_patch() { return BOTAN_VERSION_PATCH; }