~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/base.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

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