Botan  1.10.9
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  {
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 
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 
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 
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 }
std::string arg(size_t i) const
Definition: scan_name.cpp:153
EMSA * get_emsa(const std::string &algo_spec)
Definition: get_enc.cpp:86
PBKDF * make_pbkdf(const std::string &algo_spec, const std::string &provider="")
size_t arg_count() const
Definition: scan_name.h:47
Algorithm_Factory & algorithm_factory() const
Definition: libstate.cpp:173
std::string algo_name() const
Definition: scan_name.h:37
Definition: eme.h:19
Library_State & global_state()
bool arg_count_between(size_t lower, size_t upper) const
Definition: scan_name.h:54
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:167
EME * get_eme(const std::string &algo_spec)
Definition: get_enc.cpp:143
HashFunction * make_hash_function(const std::string &algo_spec, const std::string &provider="")
Definition: kdf.h:20
PBKDF * get_pbkdf(const std::string &algo_spec)
Definition: get_enc.cpp:73
KDF * get_kdf(const std::string &algo_spec)
Definition: get_enc.cpp:174