Botan: src/libstate/lookup.cpp Source File

Botan 1.10.17

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 {
19 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
37 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
53 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
69 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
88 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
107 Algorithm_Factory& af = global_state().algorithm_factory();
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 {
127 Algorithm_Factory& af = global_state().algorithm_factory();
128
129 Algorithm_Factory::Engine_Iterator i(af);
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 }
Botan::Algorithm_Factory
Definition: algo_factory.h:34
Botan::OctetString
Definition: symkey.h:19
Botan::Keyed_Filter
Definition: key_filt.h:20
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
Botan::Key_Length_Specification::maximum_keylength
size_t maximum_keylength() const
Definition: key_spec.h:69
Botan::Algorithm_Factory::prototype_block_cipher
const BlockCipher * prototype_block_cipher(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:193
Botan::MessageAuthenticationCode
Definition: mac.h:20
Botan::Keyed_Filter::set_iv
virtual void set_iv(const InitializationVector &iv)
Definition: basefilt.cpp:13
Botan::Keyed_Filter::set_key
virtual void set_key(const SymmetricKey &key)=0
Botan::min_keylength_of
size_t min_keylength_of(const std::string &name)
Definition: lookup.cpp:67
Botan::max_keylength_of
size_t max_keylength_of(const std::string &name)
Definition: lookup.cpp:86
Botan::Algorithm_Factory::prototype_hash_function
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:215
Botan::Engine
Definition: engine.h:32
Botan::Key_Length_Specification::keylength_multiple
size_t keylength_multiple() const
Definition: key_spec.h:77
Botan::Algorithm_Factory::Engine_Iterator
Definition: algo_factory.h:190
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::StreamCipher
Definition: stream_cipher.h:18
Botan::have_algorithm
bool have_algorithm(const std::string &name)
Definition: lookup.cpp:17
Botan::Algorithm_Factory::prototype_mac
const MessageAuthenticationCode * prototype_mac(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:226
Botan::block_size_of
size_t block_size_of(const std::string &name)
Definition: lookup.cpp:35
Botan
Definition: algo_base.h:14
Botan::OctetString::length
size_t length() const
Definition: symkey.h:25
Botan::HashFunction
Definition: hash.h:20
Botan::SymmetricAlgorithm::key_spec
virtual Key_Length_Specification key_spec() const =0
Botan::keylength_multiple_of
size_t keylength_multiple_of(const std::string &name)
Definition: lookup.cpp:105
Botan::Algorithm_Factory::Engine_Iterator::next
Engine * next()
Definition: algo_factory.h:196
Botan::Cipher_Dir
Cipher_Dir
Definition: sym_algo.h:87
Botan::Algorithm_Not_Found
Definition: exceptn.h:109
Botan::output_length_of
size_t output_length_of(const std::string &name)
Definition: lookup.cpp:51
Botan::get_cipher
Keyed_Filter * get_cipher(const std::string &algo_spec, Cipher_Dir direction)
Definition: lookup.cpp:124
Botan::Key_Length_Specification::minimum_keylength
size_t minimum_keylength() const
Definition: key_spec.h:61
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0
Botan::InitializationVector
OctetString InitializationVector
Definition: symkey.h:152
Botan::Algorithm_Factory::prototype_stream_cipher
const StreamCipher * prototype_stream_cipher(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:204
Botan::BlockCipher
Definition: block_cipher.h:18