monotone

monotone Mtn Source Tree

Root/botan/base.cpp

1/*************************************************
2* Base Classes Source File *
3* (C) 1999-2007 The Botan Project *
4*************************************************/
5
6#include <botan/base.h>
7#include <botan/version.h>
8#include <botan/util.h>
9#include <botan/config.h>
10
11namespace Botan {
12
13/*************************************************
14* SymmetricAlgorithm Constructor *
15*************************************************/
16SymmetricAlgorithm::SymmetricAlgorithm(u32bit key_min, u32bit key_max,
17 u32bit key_mod) :
18 MAXIMUM_KEYLENGTH(key_max ? key_max : key_min),
19 MINIMUM_KEYLENGTH(key_min),
20 KEYLENGTH_MULTIPLE(key_mod)
21 {
22 }
23
24/*************************************************
25* Query if the keylength is valid *
26*************************************************/
27bool SymmetricAlgorithm::valid_keylength(u32bit length) const
28 {
29 return ((length >= MINIMUM_KEYLENGTH) &&
30 (length <= MAXIMUM_KEYLENGTH) &&
31 (length % KEYLENGTH_MULTIPLE == 0));
32 }
33
34/*************************************************
35* Set the key *
36*************************************************/
37void SymmetricAlgorithm::set_key(const SymmetricKey& algo_key)
38 throw(Invalid_Key_Length)
39 {
40 set_key(algo_key.begin(), algo_key.length());
41 }
42
43/*************************************************
44* Set the key *
45*************************************************/
46void SymmetricAlgorithm::set_key(const byte algo_key[], u32bit length)
47 throw(Invalid_Key_Length)
48 {
49 if(!valid_keylength(length))
50 throw Invalid_Key_Length(name(), length);
51 key(algo_key, length);
52 }
53
54/*************************************************
55* BlockCipher Constructor *
56*************************************************/
57BlockCipher::BlockCipher(u32bit block, u32bit key_min, u32bit key_max,
58 u32bit key_mod) :
59 SymmetricAlgorithm(key_min, key_max, key_mod),
60 BLOCK_SIZE(block)
61 {
62 }
63
64/*************************************************
65* StreamCipher Constructor *
66*************************************************/
67StreamCipher::StreamCipher(u32bit key_min, u32bit key_max, u32bit key_mod,
68 u32bit iv_len) :
69 SymmetricAlgorithm(key_min, key_max, key_mod), IV_LENGTH(iv_len)
70 {
71 }
72
73/*************************************************
74* BufferedComputation Constructor *
75*************************************************/
76BufferedComputation::BufferedComputation(u32bit olen) : OUTPUT_LENGTH(olen)
77 {
78 }
79
80/*************************************************
81* HashFunction Constructor *
82*************************************************/
83HashFunction::HashFunction(u32bit hlen, u32bit blen) :
84 BufferedComputation(hlen), HASH_BLOCK_SIZE(blen)
85 {
86 }
87
88/*************************************************
89* MessageAuthenticationCode Constructor *
90*************************************************/
91MessageAuthenticationCode::MessageAuthenticationCode(u32bit mlen,
92 u32bit key_min,
93 u32bit key_max,
94 u32bit key_mod) :
95 BufferedComputation(mlen),
96 SymmetricAlgorithm(key_min, key_max, key_mod)
97 {
98 }
99
100/*************************************************
101* Default MAC verification operation *
102*************************************************/
103bool MessageAuthenticationCode::verify_mac(const byte mac[], u32bit length)
104 {
105 SecureVector<byte> our_mac = final();
106 if(our_mac.size() != length)
107 return false;
108 for(u32bit j = 0; j != length; ++j)
109 if(mac[j] != our_mac[j])
110 return false;
111 return true;
112 }
113
114/*************************************************
115* Default StreamCipher Resync Operation *
116*************************************************/
117void StreamCipher::resync(const byte[], u32bit length)
118 {
119 if(length)
120 throw Exception("The stream cipher " + name() +
121 " does not support resyncronization");
122 }
123
124/*************************************************
125* Default StreamCipher Seek Operation *
126*************************************************/
127void StreamCipher::seek(u32bit)
128 {
129 throw Exception("The stream cipher " + name() + " does not support seek()");
130 }
131
132/*************************************************
133* Hashing/MACing *
134*************************************************/
135void BufferedComputation::update(const byte in[], u32bit n)
136 {
137 add_data(in, n);
138 }
139
140/*************************************************
141* Hashing/MACing *
142*************************************************/
143void BufferedComputation::update(const MemoryRegion<byte>& in)
144 {
145 add_data(in, in.size());
146 }
147
148/*************************************************
149* Hashing/MACing *
150*************************************************/
151void BufferedComputation::update(const std::string& str)
152 {
153 update(reinterpret_cast<const byte*>(str.data()), str.size());
154 }
155
156/*************************************************
157* Hashing/MACing *
158*************************************************/
159void BufferedComputation::update(byte in)
160 {
161 update(&in, 1);
162 }
163
164/*************************************************
165* Hashing/MACing *
166*************************************************/
167SecureVector<byte> BufferedComputation::final()
168 {
169 SecureVector<byte> output(OUTPUT_LENGTH);
170 final_result(output);
171 return output;
172 }
173
174/*************************************************
175* Hashing/MACing *
176*************************************************/
177SecureVector<byte> BufferedComputation::process(const byte in[], u32bit len)
178 {
179 update(in, len);
180 return final();
181 }
182
183/*************************************************
184* Hashing/MACing *
185*************************************************/
186SecureVector<byte> BufferedComputation::process(const MemoryRegion<byte>& in)
187 {
188 update(in, in.size());
189 return final();
190 }
191
192/*************************************************
193* Hashing/MACing *
194*************************************************/
195SecureVector<byte> BufferedComputation::process(const std::string& in)
196 {
197 update(in);
198 return final();
199 }
200
201/*************************************************
202* Default fast poll for EntropySources *
203*************************************************/
204u32bit EntropySource::fast_poll(byte buf[], u32bit len)
205 {
206 return slow_poll(buf, len);
207 }
208
209/*************************************************
210* Add entropy to internal state *
211*************************************************/
212void RandomNumberGenerator::add_entropy(const byte random[], u32bit length)
213 {
214 add_randomness(random, length);
215 }
216
217/*************************************************
218* Add entropy to internal state *
219*************************************************/
220u32bit RandomNumberGenerator::add_entropy(EntropySource& source,
221 bool slow_poll)
222 {
223 std::string poll_type;
224 if(slow_poll)
225 poll_type = "rng/slow_poll_request";
226 else
227 poll_type = "rng/fast_poll_request";
228
229 u32bit poll_for = global_config().option_as_u32bit(poll_type);
230
231 SecureVector<byte> buffer(poll_for ? poll_for : 256);
232
233 u32bit bytes_gathered = 0;
234
235 if(slow_poll)
236 bytes_gathered = source.slow_poll(buffer, buffer.size());
237 else
238 bytes_gathered = source.fast_poll(buffer, buffer.size());
239
240 add_entropy(buffer, bytes_gathered);
241
242 return entropy_estimate(buffer, bytes_gathered);
243 }
244
245}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status