aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstate/lookup.cpp
blob: d16364cdc2a2ded8268800977bb2baf480ca97b8 (plain)
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
/*************************************************
* Algorithm Retrieval Source File                *
* (C) 1999-2007 Jack Lloyd                       *
*************************************************/

#include <botan/lookup.h>
#include <botan/libstate.h>
#include <botan/engine.h>

namespace Botan {

/**
* Acquire a block cipher
*/
const BlockCipher* retrieve_block_cipher(Library_State& libstate,
                                         const std::string& algo_spec)
   {
   return libstate.algo_factory().prototype_block_cipher(algo_spec);
   }

/**
* Get a block cipher by name
*/
BlockCipher* get_block_cipher(const std::string& algo_spec)
   {
   return global_state().algo_factory().make_block_cipher(algo_spec);
   }

/**
* Acquire a stream cipher
*/
const StreamCipher* retrieve_stream_cipher(Library_State& libstate,
                                           const std::string& algo_spec)
   {
   return libstate.algo_factory().prototype_stream_cipher(algo_spec);
   }

/**
* Get a stream cipher by name
*/
StreamCipher* get_stream_cipher(const std::string& algo_spec)
   {
   return global_state().algo_factory().make_stream_cipher(algo_spec);
   }

/**
* Acquire a hash function
*/
const HashFunction* retrieve_hash(Library_State& libstate,
                                  const std::string& algo_spec)
   {
   return libstate.algo_factory().prototype_hash_function(algo_spec);
   }

/**
* Get a hash function by name
*/
HashFunction* get_hash(const std::string& algo_spec)
   {
   return global_state().algo_factory().make_hash_function(algo_spec);
   }

/**
* Query if Botan has the named hash function
*/
bool have_hash(const std::string& algo_spec)
   {
   return global_state().algo_factory().prototype_hash_function(algo_spec);
   }

/**
* Acquire an authentication code
*/
const MessageAuthenticationCode* retrieve_mac(Library_State& libstate,
                                              const std::string& algo_spec)
   {
   return libstate.algo_factory().prototype_mac(algo_spec);
   }

/**
* Get a MAC by name
*/
MessageAuthenticationCode* get_mac(const std::string& algo_spec)
   {
   return global_state().algo_factory().make_mac(algo_spec);
   }

/**
* Query if Botan has the named MAC
*/
bool have_mac(const std::string& algo_spec)
   {
   return global_state().algo_factory().prototype_mac(algo_spec);
   }

/**
* Query if an algorithm exists
*/
bool have_algorithm(const std::string& name)
   {
   if(retrieve_block_cipher(global_state(), name))
      return true;
   if(retrieve_stream_cipher(global_state(), name))
      return true;
   if(retrieve_hash(global_state(), name))
      return true;
   if(retrieve_mac(global_state(), name))
      return true;
   return false;
   }

/**
* Query if Botan has the named block cipher
*/
bool have_block_cipher(const std::string& name)
   {
   return (retrieve_block_cipher(global_state(), name) != 0);
   }

/**
* Query if Botan has the named stream cipher
*/
bool have_stream_cipher(const std::string& name)
   {
   return (retrieve_stream_cipher(global_state(), name) != 0);
   }

/**
* Query the block size of a cipher or hash
*/
u32bit block_size_of(const std::string& name)
   {
   const BlockCipher* cipher = retrieve_block_cipher(global_state(), name);
   if(cipher)
      return cipher->BLOCK_SIZE;

   const HashFunction* hash = retrieve_hash(global_state(), name);
   if(hash)
      return hash->HASH_BLOCK_SIZE;

   throw Algorithm_Not_Found(name);
   }

/**
* Query the OUTPUT_LENGTH of a hash or MAC
*/
u32bit output_length_of(const std::string& name)
   {
   const HashFunction* hash = retrieve_hash(global_state(), name);
   if(hash)
      return hash->OUTPUT_LENGTH;

   const MessageAuthenticationCode* mac = retrieve_mac(global_state(), name);
   if(mac)
      return mac->OUTPUT_LENGTH;

   throw Algorithm_Not_Found(name);
   }

/**
* Check if a keylength is valid for this algo
*/
bool valid_keylength_for(u32bit key_len, const std::string& name)
   {
   const BlockCipher* bc = retrieve_block_cipher(global_state(), name);
   if(bc)
      return bc->valid_keylength(key_len);

   const StreamCipher* sc = retrieve_stream_cipher(global_state(), name);
   if(sc)
      return sc->valid_keylength(key_len);

   const MessageAuthenticationCode* mac = retrieve_mac(global_state(), name);
   if(mac)
      return mac->valid_keylength(key_len);

   throw Algorithm_Not_Found(name);
   }

/**
* Query the MINIMUM_KEYLENGTH of an algorithm
*/
u32bit min_keylength_of(const std::string& name)
   {
   const BlockCipher* bc = retrieve_block_cipher(global_state(), name);
   if(bc)
      return bc->MINIMUM_KEYLENGTH;

   const StreamCipher* sc = retrieve_stream_cipher(global_state(), name);
   if(sc)
      return sc->MINIMUM_KEYLENGTH;

   const MessageAuthenticationCode* mac = retrieve_mac(global_state(), name);
   if(mac)
      return mac->MINIMUM_KEYLENGTH;

   throw Algorithm_Not_Found(name);
   }

/**
* Query the MAXIMUM_KEYLENGTH of an algorithm
*/
u32bit max_keylength_of(const std::string& name)
   {
   const BlockCipher* bc = retrieve_block_cipher(global_state(), name);
   if(bc)
      return bc->MAXIMUM_KEYLENGTH;

   const StreamCipher* sc = retrieve_stream_cipher(global_state(), name);
   if(sc)
      return sc->MAXIMUM_KEYLENGTH;

   const MessageAuthenticationCode* mac = retrieve_mac(global_state(), name);
   if(mac)
      return mac->MAXIMUM_KEYLENGTH;

   throw Algorithm_Not_Found(name);
   }

/**
* Query the KEYLENGTH_MULTIPLE of an algorithm
*/
u32bit keylength_multiple_of(const std::string& name)
   {
   const BlockCipher* bc = retrieve_block_cipher(global_state(), name);
   if(bc)
      return bc->KEYLENGTH_MULTIPLE;

   const StreamCipher* sc = retrieve_stream_cipher(global_state(), name);
   if(sc)
      return sc->KEYLENGTH_MULTIPLE;

   const MessageAuthenticationCode* mac = retrieve_mac(global_state(), name);
   if(mac)
      return mac->KEYLENGTH_MULTIPLE;

   throw Algorithm_Not_Found(name);
   }

/**
* Get a cipher object
*/
Keyed_Filter* get_cipher(const std::string& algo_spec,
                         Cipher_Dir direction)
   {
   Algorithm_Factory::Engine_Iterator i(global_state().algo_factory());

   while(Engine* engine = i.next())
      {
      Keyed_Filter* algo = engine->get_cipher(algo_spec, direction);
      if(algo)
         return algo;
      }

   throw Algorithm_Not_Found(algo_spec);
   }

/**
* Get a cipher object
*/
Keyed_Filter* get_cipher(const std::string& algo_spec,
                         const SymmetricKey& key,
                         const InitializationVector& iv,
                         Cipher_Dir direction)
   {
   Keyed_Filter* cipher = get_cipher(algo_spec, direction);
   cipher->set_key(key);

   if(iv.length())
      cipher->set_iv(iv);

   return cipher;
   }

/**
* Get a cipher object
*/
Keyed_Filter* get_cipher(const std::string& algo_spec,
                         const SymmetricKey& key,
                         Cipher_Dir direction)
   {
   return get_cipher(algo_spec,
                     key, InitializationVector(), direction);
   }

}