monotone

monotone Mtn Source Tree

Root/botan/get_algo.cpp

1/*************************************************
2* Algorithm Retrieval Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/lookup.h>
7
8namespace Botan {
9
10/*************************************************
11* Get a block cipher by name *
12*************************************************/
13BlockCipher* get_block_cipher(const std::string& name)
14 {
15 const BlockCipher* cipher = retrieve_block_cipher(name);
16 if(cipher)
17 return cipher->clone();
18 throw Algorithm_Not_Found(name);
19 }
20
21/*************************************************
22* Get a stream cipher by name *
23*************************************************/
24StreamCipher* get_stream_cipher(const std::string& name)
25 {
26 const StreamCipher* cipher = retrieve_stream_cipher(name);
27 if(cipher)
28 return cipher->clone();
29 throw Algorithm_Not_Found(name);
30 }
31
32/*************************************************
33* Get a hash function by name *
34*************************************************/
35HashFunction* get_hash(const std::string& name)
36 {
37 const HashFunction* hash = retrieve_hash(name);
38 if(hash)
39 return hash->clone();
40 throw Algorithm_Not_Found(name);
41 }
42
43/*************************************************
44* Get a MAC by name *
45*************************************************/
46MessageAuthenticationCode* get_mac(const std::string& name)
47 {
48 const MessageAuthenticationCode* mac = retrieve_mac(name);
49 if(mac)
50 return mac->clone();
51 throw Algorithm_Not_Found(name);
52 }
53
54/*************************************************
55* Get a S2K algorithm by name *
56*************************************************/
57S2K* get_s2k(const std::string& name)
58 {
59 const S2K* s2k = retrieve_s2k(name);
60 if(s2k)
61 return s2k->clone();
62 throw Algorithm_Not_Found(name);
63 }
64
65/*************************************************
66* Get a block cipher padding method by name *
67*************************************************/
68const BlockCipherModePaddingMethod* get_bc_pad(const std::string& name)
69 {
70 const BlockCipherModePaddingMethod* pad = retrieve_bc_pad(name);
71 if(pad)
72 return pad;
73 throw Algorithm_Not_Found(name);
74 }
75
76/*************************************************
77* Query if an algorithm exists *
78*************************************************/
79bool have_algorithm(const std::string& name)
80 {
81 if(retrieve_block_cipher(name))
82 return true;
83 if(retrieve_stream_cipher(name))
84 return true;
85 if(retrieve_hash(name))
86 return true;
87 if(retrieve_mac(name))
88 return true;
89 return false;
90 }
91
92/*************************************************
93* Query if Botan has the named block cipher *
94*************************************************/
95bool have_block_cipher(const std::string& name)
96 {
97 if(retrieve_block_cipher(name))
98 return true;
99 return false;
100 }
101
102/*************************************************
103* Query if Botan has the named stream cipher *
104*************************************************/
105bool have_stream_cipher(const std::string& name)
106 {
107 if(retrieve_stream_cipher(name))
108 return true;
109 return false;
110 }
111
112/*************************************************
113* Query if Botan has the named hash function *
114*************************************************/
115bool have_hash(const std::string& name)
116 {
117 if(retrieve_hash(name))
118 return true;
119 return false;
120 }
121
122/*************************************************
123* Query if Botan has the named MAC *
124*************************************************/
125bool have_mac(const std::string& name)
126 {
127 if(retrieve_mac(name))
128 return true;
129 return false;
130 }
131
132/*************************************************
133* Query the BLOCK_SIZE of a block cipher *
134*************************************************/
135u32bit block_size_of(const std::string& name)
136 {
137 const BlockCipher* cipher = retrieve_block_cipher(name);
138 if(cipher)
139 return cipher->BLOCK_SIZE;
140 throw Algorithm_Not_Found(name);
141 }
142
143/*************************************************
144* Query the OUTPUT_LENGTH of a hash or MAC *
145*************************************************/
146u32bit output_length_of(const std::string& name)
147 {
148 const HashFunction* hash = retrieve_hash(name);
149 if(hash)
150 return hash->OUTPUT_LENGTH;
151
152 const MessageAuthenticationCode* mac = retrieve_mac(name);
153 if(mac)
154 return mac->OUTPUT_LENGTH;
155
156 throw Algorithm_Not_Found(name);
157 }
158
159/*************************************************
160* Check if a keylength is valid for this algo *
161*************************************************/
162bool valid_keylength_for(u32bit key_len, const std::string& name)
163 {
164 const BlockCipher* bc = retrieve_block_cipher(name);
165 if(bc)
166 return bc->valid_keylength(key_len);
167
168 const StreamCipher* sc = retrieve_stream_cipher(name);
169 if(sc)
170 return sc->valid_keylength(key_len);
171
172 const MessageAuthenticationCode* mac = retrieve_mac(name);
173 if(mac)
174 return mac->valid_keylength(key_len);
175
176 throw Algorithm_Not_Found(name);
177 }
178
179/*************************************************
180* Query the MINIMUM_KEYLENGTH of an algorithm *
181*************************************************/
182u32bit min_keylength_of(const std::string& name)
183 {
184 const BlockCipher* bc = retrieve_block_cipher(name);
185 if(bc)
186 return bc->MINIMUM_KEYLENGTH;
187
188 const StreamCipher* sc = retrieve_stream_cipher(name);
189 if(sc)
190 return sc->MINIMUM_KEYLENGTH;
191
192 const MessageAuthenticationCode* mac = retrieve_mac(name);
193 if(mac)
194 return mac->MINIMUM_KEYLENGTH;
195
196 throw Algorithm_Not_Found(name);
197 }
198
199/*************************************************
200* Query the MAXIMUM_KEYLENGTH of an algorithm *
201*************************************************/
202u32bit max_keylength_of(const std::string& name)
203 {
204 const BlockCipher* bc = retrieve_block_cipher(name);
205 if(bc)
206 return bc->MAXIMUM_KEYLENGTH;
207
208 const StreamCipher* sc = retrieve_stream_cipher(name);
209 if(sc)
210 return sc->MAXIMUM_KEYLENGTH;
211
212 const MessageAuthenticationCode* mac = retrieve_mac(name);
213 if(mac)
214 return mac->MAXIMUM_KEYLENGTH;
215
216 throw Algorithm_Not_Found(name);
217 }
218
219/*************************************************
220* Query the KEYLENGTH_MULTIPLE of an algorithm *
221*************************************************/
222u32bit keylength_multiple_of(const std::string& name)
223 {
224 const BlockCipher* bc = retrieve_block_cipher(name);
225 if(bc)
226 return bc->KEYLENGTH_MULTIPLE;
227
228 const StreamCipher* sc = retrieve_stream_cipher(name);
229 if(sc)
230 return sc->KEYLENGTH_MULTIPLE;
231
232 const MessageAuthenticationCode* mac = retrieve_mac(name);
233 if(mac)
234 return mac->KEYLENGTH_MULTIPLE;
235
236 throw Algorithm_Not_Found(name);
237 }
238
239}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status