monotone

monotone Mtn Source Tree

Root/botan/get_algo.cpp

1/*************************************************
2* Algorithm Retrieval Source File *
3* (C) 1999-2006 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 return (retrieve_block_cipher(name) != 0);
98 }
99
100/*************************************************
101* Query if Botan has the named stream cipher *
102*************************************************/
103bool have_stream_cipher(const std::string& name)
104 {
105 return (retrieve_stream_cipher(name) != 0);
106 }
107
108/*************************************************
109* Query if Botan has the named hash function *
110*************************************************/
111bool have_hash(const std::string& name)
112 {
113 return (retrieve_hash(name) != 0);
114 }
115
116/*************************************************
117* Query if Botan has the named MAC *
118*************************************************/
119bool have_mac(const std::string& name)
120 {
121 return (retrieve_mac(name) != 0);
122 }
123
124/*************************************************
125* Query the block size of a cipher or hash *
126*************************************************/
127u32bit block_size_of(const std::string& name)
128 {
129 const BlockCipher* cipher = retrieve_block_cipher(name);
130 if(cipher)
131 return cipher->BLOCK_SIZE;
132
133 const HashFunction* hash = retrieve_hash(name);
134 if(hash)
135 return hash->HASH_BLOCK_SIZE;
136
137 throw Algorithm_Not_Found(name);
138 }
139
140/*************************************************
141* Query the OUTPUT_LENGTH of a hash or MAC *
142*************************************************/
143u32bit output_length_of(const std::string& name)
144 {
145 const HashFunction* hash = retrieve_hash(name);
146 if(hash)
147 return hash->OUTPUT_LENGTH;
148
149 const MessageAuthenticationCode* mac = retrieve_mac(name);
150 if(mac)
151 return mac->OUTPUT_LENGTH;
152
153 throw Algorithm_Not_Found(name);
154 }
155
156/*************************************************
157* Check if a keylength is valid for this algo *
158*************************************************/
159bool valid_keylength_for(u32bit key_len, const std::string& name)
160 {
161 const BlockCipher* bc = retrieve_block_cipher(name);
162 if(bc)
163 return bc->valid_keylength(key_len);
164
165 const StreamCipher* sc = retrieve_stream_cipher(name);
166 if(sc)
167 return sc->valid_keylength(key_len);
168
169 const MessageAuthenticationCode* mac = retrieve_mac(name);
170 if(mac)
171 return mac->valid_keylength(key_len);
172
173 throw Algorithm_Not_Found(name);
174 }
175
176/*************************************************
177* Query the MINIMUM_KEYLENGTH of an algorithm *
178*************************************************/
179u32bit min_keylength_of(const std::string& name)
180 {
181 const BlockCipher* bc = retrieve_block_cipher(name);
182 if(bc)
183 return bc->MINIMUM_KEYLENGTH;
184
185 const StreamCipher* sc = retrieve_stream_cipher(name);
186 if(sc)
187 return sc->MINIMUM_KEYLENGTH;
188
189 const MessageAuthenticationCode* mac = retrieve_mac(name);
190 if(mac)
191 return mac->MINIMUM_KEYLENGTH;
192
193 throw Algorithm_Not_Found(name);
194 }
195
196/*************************************************
197* Query the MAXIMUM_KEYLENGTH of an algorithm *
198*************************************************/
199u32bit max_keylength_of(const std::string& name)
200 {
201 const BlockCipher* bc = retrieve_block_cipher(name);
202 if(bc)
203 return bc->MAXIMUM_KEYLENGTH;
204
205 const StreamCipher* sc = retrieve_stream_cipher(name);
206 if(sc)
207 return sc->MAXIMUM_KEYLENGTH;
208
209 const MessageAuthenticationCode* mac = retrieve_mac(name);
210 if(mac)
211 return mac->MAXIMUM_KEYLENGTH;
212
213 throw Algorithm_Not_Found(name);
214 }
215
216/*************************************************
217* Query the KEYLENGTH_MULTIPLE of an algorithm *
218*************************************************/
219u32bit keylength_multiple_of(const std::string& name)
220 {
221 const BlockCipher* bc = retrieve_block_cipher(name);
222 if(bc)
223 return bc->KEYLENGTH_MULTIPLE;
224
225 const StreamCipher* sc = retrieve_stream_cipher(name);
226 if(sc)
227 return sc->KEYLENGTH_MULTIPLE;
228
229 const MessageAuthenticationCode* mac = retrieve_mac(name);
230 if(mac)
231 return mac->KEYLENGTH_MULTIPLE;
232
233 throw Algorithm_Not_Found(name);
234 }
235
236}

Archive Download this file

Branches

Tags

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