Botan: src/libstate/get_enc.cpp Source File

Botan 1.10.17

get_enc.cpp
Go to the documentation of this file.
1 /*
2 * PBKDF/EMSA/EME/KDF/MGF 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/scan_name.h>
11
12 #if defined(BOTAN_HAS_MGF1)
13 #include <botan/mgf1.h>
14 #endif
15
16 #if defined(BOTAN_HAS_EMSA1)
17 #include <botan/emsa1.h>
18 #endif
19
20 #if defined(BOTAN_HAS_EMSA1_BSI)
21 #include <botan/emsa1_bsi.h>
22 #endif
23
24 #if defined(BOTAN_HAS_EMSA2)
25 #include <botan/emsa2.h>
26 #endif
27
28 #if defined(BOTAN_HAS_EMSA3)
29 #include <botan/emsa3.h>
30 #endif
31
32 #if defined(BOTAN_HAS_EMSA4)
33 #include <botan/emsa4.h>
34 #endif
35
36 #if defined(BOTAN_HAS_EMSA_RAW)
37 #include <botan/emsa_raw.h>
38 #endif
39
40 #if defined(BOTAN_HAS_EME1)
41 #include <botan/eme1.h>
42 #endif
43
44 #if defined(BOTAN_HAS_EME_PKCS1v15)
45 #include <botan/eme_pkcs.h>
46 #endif
47
48 #if defined(BOTAN_HAS_KDF1)
49 #include <botan/kdf1.h>
50 #endif
51
52 #if defined(BOTAN_HAS_KDF2)
53 #include <botan/kdf2.h>
54 #endif
55
56 #if defined(BOTAN_HAS_X942_PRF)
57 #include <botan/prf_x942.h>
58 #endif
59
60 #if defined(BOTAN_HAS_SSL_V3_PRF)
61 #include <botan/prf_ssl3.h>
62 #endif
63
64 #if defined(BOTAN_HAS_TLS_V10_PRF)
65 #include <botan/prf_tls.h>
66 #endif
67
68 namespace Botan {
69
70 /*
71 * Get a PBKDF algorithm by name
72 */
73 PBKDF* get_pbkdf(const std::string& algo_spec)
74 {
75 Algorithm_Factory& af = global_state().algorithm_factory();
76
77 if(PBKDF* pbkdf = af.make_pbkdf(algo_spec))
78 return pbkdf;
79
80 throw Algorithm_Not_Found(algo_spec);
81 }
82
83 /*
84 * Get an EMSA by name
85 */
86 EMSA* get_emsa(const std::string& algo_spec)
87 {
88 SCAN_Name request(algo_spec);
89
90 Algorithm_Factory& af = global_state().algorithm_factory();
91
92 #if defined(BOTAN_HAS_EMSA_RAW)
93 if(request.algo_name() == "Raw" && request.arg_count() == 0)
94 return new EMSA_Raw;
95 #endif
96
97 #if defined(BOTAN_HAS_EMSA1)
98 if(request.algo_name() == "EMSA1" && request.arg_count() == 1)
99 return new EMSA1(af.make_hash_function(request.arg(0)));
100 #endif
101
102 #if defined(BOTAN_HAS_EMSA1_BSI)
103 if(request.algo_name() == "EMSA1_BSI" && request.arg_count() == 1)
104 return new EMSA1_BSI(af.make_hash_function(request.arg(0)));
105 #endif
106
107 #if defined(BOTAN_HAS_EMSA2)
108 if(request.algo_name() == "EMSA2" && request.arg_count() == 1)
109 return new EMSA2(af.make_hash_function(request.arg(0)));
110 #endif
111
112 #if defined(BOTAN_HAS_EMSA3)
113 if(request.algo_name() == "EMSA3" && request.arg_count() == 1)
114 {
115 if(request.arg(0) == "Raw")
116 return new EMSA3_Raw;
117 return new EMSA3(af.make_hash_function(request.arg(0)));
118 }
119 #endif
120
121 #if defined(BOTAN_HAS_EMSA4)
122 if(request.algo_name() == "EMSA4" && request.arg_count_between(1, 3))
123 {
124 // 3 args: Hash, MGF, salt size (MGF is hardcoded MGF1 in Botan)
125 if(request.arg_count() == 1)
126 return new EMSA4(af.make_hash_function(request.arg(0)));
127
128 if(request.arg_count() == 2 && request.arg(1) != "MGF1")
129 return new EMSA4(af.make_hash_function(request.arg(0)));
130
131 if(request.arg_count() == 3)
132 return new EMSA4(af.make_hash_function(request.arg(0)),
133 request.arg_as_integer(2, 0));
134 }
135 #endif
136
137 throw Algorithm_Not_Found(algo_spec);
138 }
139
140 /*
141 * Get an EME by name
142 */
143 EME* get_eme(const std::string& algo_spec)
144 {
145 SCAN_Name request(algo_spec);
146
147 Algorithm_Factory& af = global_state().algorithm_factory();
148
149 if(request.algo_name() == "Raw")
150 return 0; // No padding
151
152 #if defined(BOTAN_HAS_EME_PKCS1v15)
153 if(request.algo_name() == "PKCS1v15" && request.arg_count() == 0)
154 return new EME_PKCS1v15;
155 #endif
156
157 #if defined(BOTAN_HAS_EME1)
158 if(request.algo_name() == "EME1" && request.arg_count_between(1, 2))
159 {
160 if(request.arg_count() == 1 ||
161 (request.arg_count() == 2 && request.arg(1) == "MGF1"))
162 {
163 return new EME1(af.make_hash_function(request.arg(0)));
164 }
165 }
166 #endif
167
168 throw Algorithm_Not_Found(algo_spec);
169 }
170
171 /*
172 * Get an KDF by name
173 */
174 KDF* get_kdf(const std::string& algo_spec)
175 {
176 SCAN_Name request(algo_spec);
177
178 Algorithm_Factory& af = global_state().algorithm_factory();
179
180 if(request.algo_name() == "Raw")
181 return 0; // No KDF
182
183 #if defined(BOTAN_HAS_KDF1)
184 if(request.algo_name() == "KDF1" && request.arg_count() == 1)
185 return new KDF1(af.make_hash_function(request.arg(0)));
186 #endif
187
188 #if defined(BOTAN_HAS_KDF2)
189 if(request.algo_name() == "KDF2" && request.arg_count() == 1)
190 return new KDF2(af.make_hash_function(request.arg(0)));
191 #endif
192
193 #if defined(BOTAN_HAS_X942_PRF)
194 if(request.algo_name() == "X9.42-PRF" && request.arg_count() == 1)
195 return new X942_PRF(request.arg(0)); // OID
196 #endif
197
198 #if defined(BOTAN_HAS_TLS_V10_PRF)
199 if(request.algo_name() == "TLS-PRF" && request.arg_count() == 0)
200 return new TLS_PRF;
201 #endif
202
203 #if defined(BOTAN_HAS_SSL_V3_PRF)
204 if(request.algo_name() == "SSL3-PRF" && request.arg_count() == 0)
205 return new SSL3_PRF;
206 #endif
207
208 throw Algorithm_Not_Found(algo_spec);
209 }
210
211 }
Botan::EMSA4
Definition: emsa4.h:20
Botan::get_emsa
EMSA * get_emsa(const std::string &algo_spec)
Definition: get_enc.cpp:86
Botan::TLS_PRF
Definition: prf_tls.h:20
Botan::Algorithm_Factory
Definition: algo_factory.h:34
Botan::SCAN_Name::arg_count
size_t arg_count() const
Definition: scan_name.h:47
Botan::Library_State::algorithm_factory
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
Botan::Algorithm_Factory::make_pbkdf
PBKDF * make_pbkdf(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:298
Botan::KDF1
Definition: kdf1.h:19
Botan::SCAN_Name::arg_as_integer
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:167
Botan::EMSA3
Definition: emsa3.h:21
Botan::EME
Definition: eme.h:19
Botan::Global_State_Management::global_state
Library_State & global_state()
Definition: global_state.cpp:32
Botan::SCAN_Name::arg
std::string arg(size_t i) const
Definition: scan_name.cpp:153
Botan::EMSA3_Raw
Definition: emsa3.h:49
Botan::EMSA2
Definition: emsa2.h:20
Botan::X942_PRF
Definition: prf_x942.h:18
Botan::SSL3_PRF
Definition: prf_ssl3.h:18
Botan::EMSA
Definition: emsa.h:19
Botan
Definition: algo_base.h:14
Botan::get_eme
EME * get_eme(const std::string &algo_spec)
Definition: get_enc.cpp:143
Botan::EME1
Definition: eme1.h:20
Botan::Algorithm_Factory::make_hash_function
HashFunction * make_hash_function(const std::string &algo_spec, const std::string &provider="")
Definition: algo_factory.cpp:274
Botan::SCAN_Name
Definition: scan_name.h:21
Botan::EMSA1_BSI
Definition: emsa1_bsi.h:21
Botan::KDF
Definition: kdf.h:20
Botan::EME_PKCS1v15
Definition: eme_pkcs.h:18
Botan::SCAN_Name::arg_count_between
bool arg_count_between(size_t lower, size_t upper) const
Definition: scan_name.h:54
Botan::EMSA1
Definition: emsa1.h:20
Botan::get_pbkdf
PBKDF * get_pbkdf(const std::string &algo_spec)
Definition: get_enc.cpp:73
Botan::EMSA_Raw
Definition: emsa_raw.h:19
Botan::get_kdf
KDF * get_kdf(const std::string &algo_spec)
Definition: get_enc.cpp:174
Botan::Algorithm_Not_Found
Definition: exceptn.h:109
Botan::SCAN_Name::algo_name
std::string algo_name() const
Definition: scan_name.h:37
Botan::KDF2
Definition: kdf2.h:19
Botan::PBKDF
Definition: pbkdf.h:21