Botan  1.10.9
lookup.cpp
Go to the documentation of this file.
1 /*
2 * Algorithm Retrieval
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/lookup.h>
9 #include <botan/libstate.h>
10 #include <botan/engine.h>
11 
12 namespace Botan {
13 
14 /*
15 * Query if an algorithm exists
16 */
17 bool have_algorithm(const std::string& name)
18  {
20 
21  if(af.prototype_block_cipher(name))
22  return true;
23  if(af.prototype_stream_cipher(name))
24  return true;
25  if(af.prototype_hash_function(name))
26  return true;
27  if(af.prototype_mac(name))
28  return true;
29  return false;
30  }
31 
32 /*
33 * Query the block size of a cipher or hash
34 */
35 size_t block_size_of(const std::string& name)
36  {
38 
39  if(const BlockCipher* cipher = af.prototype_block_cipher(name))
40  return cipher->block_size();
41 
42  if(const HashFunction* hash = af.prototype_hash_function(name))
43  return hash->hash_block_size();
44 
45  throw Algorithm_Not_Found(name);
46  }
47 
48 /*
49 * Query the output_length() of a hash or MAC
50 */
51 size_t output_length_of(const std::string& name)
52  {
54 
55  if(const HashFunction* hash = af.prototype_hash_function(name))
56  return hash->output_length();
57 
58  if(const MessageAuthenticationCode* mac = af.prototype_mac(name))
59  return mac->output_length();
60 
61  throw Algorithm_Not_Found(name);
62  }
63 
64 /*
65 * Query the minimum allowed key length of an algorithm implementation
66 */
67 size_t min_keylength_of(const std::string& name)
68  {
70 
71  if(const BlockCipher* bc = af.prototype_block_cipher(name))
72  return bc->key_spec().minimum_keylength();
73 
74  if(const StreamCipher* sc = af.prototype_stream_cipher(name))
75  return sc->key_spec().minimum_keylength();
76 
77  if(const MessageAuthenticationCode* mac = af.prototype_mac(name))
78  return mac->key_spec().minimum_keylength();
79 
80  throw Algorithm_Not_Found(name);
81  }
82 
83 /*
84 * Query the maximum allowed keylength of an algorithm implementation
85 */
86 size_t max_keylength_of(const std::string& name)
87  {
89 
90  if(const BlockCipher* bc = af.prototype_block_cipher(name))
91  return bc->key_spec().maximum_keylength();
92 
93  if(const StreamCipher* sc = af.prototype_stream_cipher(name))
94  return sc->key_spec().maximum_keylength();
95 
96  if(const MessageAuthenticationCode* mac = af.prototype_mac(name))
97  return mac->key_spec().maximum_keylength();
98 
99  throw Algorithm_Not_Found(name);
100  }
101 
102 /*
103 * Query the number of byte a valid key must be a multiple of
104 */
105 size_t keylength_multiple_of(const std::string& name)
106  {
108 
109  if(const BlockCipher* bc = af.prototype_block_cipher(name))
110  return bc->key_spec().keylength_multiple();
111 
112  if(const StreamCipher* sc = af.prototype_stream_cipher(name))
113  return sc->key_spec().keylength_multiple();
114 
115  if(const MessageAuthenticationCode* mac = af.prototype_mac(name))
116  return mac->key_spec().keylength_multiple();
117 
118  throw Algorithm_Not_Found(name);
119  }
120 
121 /*
122 * Get a cipher object
123 */
124 Keyed_Filter* get_cipher(const std::string& algo_spec,
125  Cipher_Dir direction)
126  {
128 
130 
131  while(Engine* engine = i.next())
132  {
133  if(Keyed_Filter* algo = engine->get_cipher(algo_spec, direction, af))
134  return algo;
135  }
136 
137  throw Algorithm_Not_Found(algo_spec);
138  }
139 
140 /*
141 * Get a cipher object
142 */
143 Keyed_Filter* get_cipher(const std::string& algo_spec,
144  const SymmetricKey& key,
145  const InitializationVector& iv,
146  Cipher_Dir direction)
147  {
148  Keyed_Filter* cipher = get_cipher(algo_spec, direction);
149  cipher->set_key(key);
150 
151  if(iv.length())
152  cipher->set_iv(iv);
153 
154  return cipher;
155  }
156 
157 /*
158 * Get a cipher object
159 */
160 Keyed_Filter* get_cipher(const std::string& algo_spec,
161  const SymmetricKey& key,
162  Cipher_Dir direction)
163  {
164  return get_cipher(algo_spec,
165  key, InitializationVector(), direction);
166  }
167 
168 }
size_t minimum_keylength() const
Definition: key_spec.h:61
const BlockCipher * prototype_block_cipher(const std::string &algo_spec, const std::string &provider="")
virtual void set_iv(const InitializationVector &iv)
Definition: basefilt.cpp:13
size_t length() const
Definition: symkey.h:25
virtual void set_key(const SymmetricKey &key)=0
size_t min_keylength_of(const std::string &name)
Definition: lookup.cpp:67
size_t max_keylength_of(const std::string &name)
Definition: lookup.cpp:86
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
MessageAuthenticationCode * mac
Definition: fpe_fe1.cpp:94
Library_State & global_state()
bool have_algorithm(const std::string &name)
Definition: lookup.cpp:17
size_t maximum_keylength() const
Definition: key_spec.h:69
const MessageAuthenticationCode * prototype_mac(const std::string &algo_spec, const std::string &provider="")
size_t block_size_of(const std::string &name)
Definition: lookup.cpp:35
virtual Key_Length_Specification key_spec() const =0
size_t keylength_multiple_of(const std::string &name)
Definition: lookup.cpp:105
size_t keylength_multiple() const
Definition: key_spec.h:77
Cipher_Dir
Definition: sym_algo.h:87
size_t output_length_of(const std::string &name)
Definition: lookup.cpp:51
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: lookup.cpp:124
virtual size_t output_length() const =0
OctetString InitializationVector
Definition: symkey.h:152
const StreamCipher * prototype_stream_cipher(const std::string &algo_spec, const std::string &provider="")