Botan  1.11.10
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 * Get a PBKDF algorithm by name
16 */
17 PBKDF* get_pbkdf(const std::string& algo_spec)
18  {
20 
21  if(PBKDF* pbkdf = af.make_pbkdf(algo_spec))
22  return pbkdf;
23 
24  throw Algorithm_Not_Found(algo_spec);
25  }
26 
27 /*
28 * Query if an algorithm exists
29 */
30 bool have_algorithm(const std::string& name)
31  {
33 
34  if(af.prototype_block_cipher(name))
35  return true;
36  if(af.prototype_stream_cipher(name))
37  return true;
38  if(af.prototype_hash_function(name))
39  return true;
40  if(af.prototype_mac(name))
41  return true;
42  return false;
43  }
44 
45 /*
46 * Query the block size of a cipher or hash
47 */
48 size_t block_size_of(const std::string& name)
49  {
51 
52  if(const BlockCipher* cipher = af.prototype_block_cipher(name))
53  return cipher->block_size();
54 
55  if(const HashFunction* hash = af.prototype_hash_function(name))
56  return hash->hash_block_size();
57 
58  throw Algorithm_Not_Found(name);
59  }
60 
61 /*
62 * Query the output_length() of a hash or MAC
63 */
64 size_t output_length_of(const std::string& name)
65  {
67 
68  if(const HashFunction* hash = af.prototype_hash_function(name))
69  return hash->output_length();
70 
71  if(const MessageAuthenticationCode* mac = af.prototype_mac(name))
72  return mac->output_length();
73 
74  throw Algorithm_Not_Found(name);
75  }
76 
77 /*
78 * Get a cipher object
79 */
80 Keyed_Filter* get_cipher(const std::string& algo_spec,
81  Cipher_Dir direction)
82  {
84 
86 
87  while(Engine* engine = i.next())
88  {
89  if(Keyed_Filter* algo = engine->get_cipher(algo_spec, direction, af))
90  return algo;
91  }
92 
93  throw Algorithm_Not_Found(algo_spec);
94  }
95 
96 /*
97 * Get a cipher object
98 */
99 Keyed_Filter* get_cipher(const std::string& algo_spec,
100  const SymmetricKey& key,
101  const InitializationVector& iv,
102  Cipher_Dir direction)
103  {
104  Keyed_Filter* cipher = get_cipher(algo_spec, direction);
105  cipher->set_key(key);
106 
107  if(iv.length())
108  cipher->set_iv(iv);
109 
110  return cipher;
111  }
112 
113 /*
114 * Get a cipher object
115 */
116 Keyed_Filter* get_cipher(const std::string& algo_spec,
117  const SymmetricKey& key,
118  Cipher_Dir direction)
119  {
120  return get_cipher(algo_spec,
121  key, InitializationVector(), direction);
122  }
123 
124 }
std::unique_ptr< MessageAuthenticationCode > mac
Definition: fpe_fe1.cpp:89
PBKDF * make_pbkdf(const std::string &algo_spec, const std::string &provider="")
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
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:46
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
Library_State & global_state()
bool have_algorithm(const std::string &name)
Definition: lookup.cpp:30
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:48
PBKDF * get_pbkdf(const std::string &algo_spec)
Definition: lookup.cpp:17
Cipher_Dir
Definition: types.h:46
size_t output_length_of(const std::string &name)
Definition: lookup.cpp:64
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: lookup.cpp:80
OctetString InitializationVector
Definition: symkey.h:139
const StreamCipher * prototype_stream_cipher(const std::string &algo_spec, const std::string &provider="")