Botan  2.1.0
Crypto and TLS for C++11
emsa.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/emsa.h>
8 #include <botan/scan_name.h>
9 
10 #if defined(BOTAN_HAS_EMSA1)
11  #include <botan/emsa1.h>
12 #endif
13 
14 #if defined(BOTAN_HAS_EMSA_X931)
15  #include <botan/emsa_x931.h>
16 #endif
17 
18 #if defined(BOTAN_HAS_EMSA_PKCS1)
19  #include <botan/emsa_pkcs1.h>
20 #endif
21 
22 #if defined(BOTAN_HAS_EMSA_PSSR)
23  #include <botan/pssr.h>
24 #endif
25 
26 #if defined(BOTAN_HAS_EMSA_RAW)
27  #include <botan/emsa_raw.h>
28 #endif
29 
30 #if defined(BOTAN_HAS_ISO_9796)
31  #include <botan/iso9796.h>
32 #endif
33 
34 namespace Botan {
35 
36 EMSA* get_emsa(const std::string& algo_spec)
37  {
38  SCAN_Name req(algo_spec);
39 
40 #if defined(BOTAN_HAS_EMSA1)
41  if(req.algo_name() == "EMSA1" && req.arg_count() == 1)
42  {
43  if(auto hash = HashFunction::create(req.arg(0)))
44  return new EMSA1(hash.release());
45  }
46 #endif
47 
48 #if defined(BOTAN_HAS_EMSA_PKCS1)
49  if(req.algo_name() == "EMSA_PKCS1" ||
50  req.algo_name() == "EMSA-PKCS1-v1_5" ||
51  req.algo_name() == "EMSA3")
52  {
53  if(req.arg_count() == 1)
54  {
55  if(req.arg(0) == "Raw")
56  {
57  return new EMSA_PKCS1v15_Raw;
58  }
59  else
60  {
61  if(auto hash = HashFunction::create(req.arg(0)))
62  {
63  return new EMSA_PKCS1v15(hash.release());
64  }
65  }
66  }
67  }
68 #endif
69 
70 #if defined(BOTAN_HAS_EMSA_PSSR)
71  if(req.algo_name() == "PSSR" ||
72  req.algo_name() == "EMSA-PSS" ||
73  req.algo_name() == "PSS-MGF1" ||
74  req.algo_name() == "EMSA4")
75  {
76  if(req.arg_count_between(1, 3))
77  {
78  if(req.arg(1, "MGF1") != "MGF1")
79  return nullptr; // not supported
80 
81  if(auto h = HashFunction::create(req.arg(0)))
82  {
83  const size_t salt_size = req.arg_as_integer(2, h->output_length());
84  return new PSSR(h.release(), salt_size);
85  }
86  }
87  }
88 #endif
89 
90 #if defined(BOTAN_HAS_ISO_9796)
91  if(req.algo_name() == "ISO_9796_DS2")
92  {
93  if(req.arg_count_between(1, 3))
94  {
95  if(auto h = HashFunction::create(req.arg(0)))
96  {
97  const size_t salt_size = req.arg_as_integer(2, h->output_length());
98  const bool implicit = req.arg(1, "exp") == "imp";
99  return new ISO_9796_DS2(h.release(), implicit, salt_size);
100  }
101  }
102  }
103  //ISO-9796-2 DS 3 is deterministic and DS2 without a salt
104  if(req.algo_name() == "ISO_9796_DS3")
105  {
106  if(req.arg_count_between(1, 2))
107  {
108  if(auto h = HashFunction::create(req.arg(0)))
109  {
110  const bool implicit = req.arg(1, "exp") == "imp";
111  return new ISO_9796_DS3(h.release(), implicit);
112  }
113  }
114  }
115 #endif
116 
117 #if defined(BOTAN_HAS_EMSA_X931)
118  if(req.algo_name() == "EMSA_X931" ||
119  req.algo_name() == "EMSA2" ||
120  req.algo_name() == "X9.31")
121  {
122  if(req.arg_count() == 1)
123  {
124  if(auto hash = HashFunction::create(req.arg(0)))
125  {
126  return new EMSA_X931(hash.release());
127  }
128  }
129  }
130 #endif
131 
132 #if defined(BOTAN_HAS_EMSA_RAW)
133  if(req.algo_name() == "Raw" && req.arg_count() == 0)
134  {
135  return new EMSA_Raw;
136  }
137 #endif
138 
139  throw Algorithm_Not_Found(algo_spec);
140  }
141 
142 std::string hash_for_emsa(const std::string& algo_spec)
143  {
144  SCAN_Name emsa_name(algo_spec);
145 
146  if(emsa_name.arg_count() > 0)
147  {
148  const std::string pos_hash = emsa_name.arg(0);
149  return pos_hash;
150  }
151 
152  return "SHA-512"; // safe default if nothing we understand
153  }
154 
155 }
156 
157 
std::string arg(size_t i) const
Definition: scan_name.cpp:122
std::string hash_for_emsa(const std::string &algo_spec)
Definition: emsa.cpp:142
EMSA * get_emsa(const std::string &algo_spec)
Definition: emsa.cpp:36
size_t arg_count() const
Definition: scan_name.h:50
size_t salt_size
bool arg_count_between(size_t lower, size_t upper) const
Definition: scan_name.h:57
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
size_t arg_as_integer(size_t i, size_t def_value) const
Definition: scan_name.cpp:137
Definition: alg_id.cpp:13
const std::string & algo_name() const
Definition: scan_name.h:45
MechanismType hash