Botan  2.6.0
Crypto and TLS for C++11
p11_mechanism.cpp
Go to the documentation of this file.
1 /*
2 * PKCS#11 Mechanism
3 * (C) 2016 Daniel Neus, Sirrix AG
4 * (C) 2016 Philipp Weber, Sirrix AG
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/internal/p11_mechanism.h>
10 #include <botan/scan_name.h>
11 #include <botan/parsing.h>
12 #include <botan/emsa.h>
13 
14 #include <tuple>
15 
16 namespace Botan {
17 namespace PKCS11 {
18 
19 namespace {
20 using PSS_Params = std::tuple<size_t, MechanismType, MGF>;
21 
22 // maps a PSS mechanism type to the number of bytes used for the salt, the mechanism type of the underlying hash algorithm and the MGF
23 static const std::map<MechanismType, PSS_Params> PssOptions =
24  {
31  };
32 
33 struct MechanismData
34  {
35  explicit MechanismData(MechanismType _type)
36  : type(_type)
37  {}
38 
39  MechanismData(MechanismData const&) = default;
40  MechanismData& operator=(MechanismData const&) = default;
41  virtual ~MechanismData() = default;
42 
43  // the mechanism to perform
45  };
46 
47 struct RSA_SignMechanism final : public MechanismData
48  {
49  explicit RSA_SignMechanism(MechanismType _type)
50  : MechanismData(_type), hash(static_cast<MechanismType>(0)), mgf(static_cast<MGF>(0)), salt_size(0)
51  {
52  auto pss_option = PssOptions.find(type);
53  if(pss_option != PssOptions.end())
54  {
55  hash = std::get<1>(pss_option->second);
56  mgf = std::get<2>(pss_option->second);
57  salt_size = std::get<0>(pss_option->second);
58  }
59  }
60 
61  // hash algorithm used in the PSS encoding; if the signature mechanism does not include message hashing,
62  // then this value must be the mechanism used by the application to generate the message hash;
63  // if the signature mechanism includes hashing, then this value must match the hash algorithm indicated by the signature mechanism
65 
66  // mask generation function to use on the encoded block
68 
69  // length, in bytes, of the salt value used in the PSS encoding; typical values are the length of the message hash and zero
70  size_t salt_size;
71  };
72 
73 // note: when updating this map, update the documentation for `MechanismWrapper::create_rsa_sign_mechanism`
74 static std::map<std::string, RSA_SignMechanism> SignMechanisms =
75  {
76  { "Raw", RSA_SignMechanism(MechanismType::RsaX509) },
77 
78  { "EMSA2(Raw)", RSA_SignMechanism(MechanismType::RsaX931) },
79  { "EMSA2(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaX931) },
80 
81  // RSASSA PKCS#1 v1.5
82  { "EMSA3(Raw)", RSA_SignMechanism(MechanismType::RsaPkcs) },
83  { "EMSA3(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcs) },
84  { "EMSA3(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcs) },
85  { "EMSA3(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcs) },
86  { "EMSA3(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcs) },
87  { "EMSA3(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcs) },
88 
89  // RSASSA PKCS#1 PSS
90  { "EMSA4(Raw)", RSA_SignMechanism(MechanismType::RsaPkcsPss) },
91  { "EMSA4(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcsPss) },
92  { "EMSA4(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcsPss) },
93  { "EMSA4(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss) },
94  { "EMSA4(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss) },
95  { "EMSA4(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss) },
96 
97  { "ISO9796", RSA_SignMechanism(MechanismType::Rsa9796) }
98  };
99 
100 struct RSA_CryptMechanism final : public MechanismData
101  {
102  RSA_CryptMechanism(MechanismType _type, size_t _padding_size, MechanismType _hash, MGF _mgf)
103  : MechanismData(_type), hash(_hash), mgf(_mgf), padding_size(_padding_size)
104  {}
105 
106  RSA_CryptMechanism(MechanismType _type, size_t _padding_size)
107  : RSA_CryptMechanism(_type, _padding_size, static_cast<MechanismType>(0), static_cast<MGF>(0))
108  {}
109 
110  // mechanism ID of the message digest algorithm used to calculate the digest of the encoding parameter
112 
113  // mask generation function to use on the encoded block
114  MGF mgf;
115 
116  // number of bytes required for the padding
117  size_t padding_size;
118  };
119 
120 // note: when updating this map, update the documentation for `MechanismWrapper::create_rsa_crypt_mechanism`
121 static const std::map<std::string, RSA_CryptMechanism> CryptMechanisms =
122  {
123  { "Raw", RSA_CryptMechanism(MechanismType::RsaX509, 0) },
124  { "EME-PKCS1-v1_5", RSA_CryptMechanism(MechanismType::RsaPkcs, 11) },
125  { "OAEP(SHA-1)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 20, MechanismType::Sha1, MGF::Mgf1Sha1) },
126  { "OAEP(SHA-224)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 28, MechanismType::Sha224, MGF::Mgf1Sha224) },
127  { "OAEP(SHA-256)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 32, MechanismType::Sha256, MGF::Mgf1Sha256) },
128  { "OAEP(SHA-384)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 48, MechanismType::Sha384, MGF::Mgf1Sha384) },
129  { "OAEP(SHA-512)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 64, MechanismType::Sha512, MGF::Mgf1Sha512) }
130  };
131 
132 // note: when updating this map, update the documentation for `MechanismWrapper::create_ecdsa_mechanism`
133 static std::map<std::string, MechanismType> EcdsaHash =
134  {
135  { "Raw", MechanismType::Ecdsa },
136  { "SHA-160", MechanismType::EcdsaSha1 },
137  { "SHA-224", MechanismType::EcdsaSha224 },
138  { "SHA-256", MechanismType::EcdsaSha256 },
139  { "SHA-384", MechanismType::EcdsaSha384 },
140  { "SHA-512", MechanismType::EcdsaSha512 }
141  };
142 
143 // note: when updating this map, update the documentation for `MechanismWrapper::create_ecdh_mechanism`
144 static std::map<std::string, KeyDerivation> EcdhHash =
145  {
146  { "Raw", KeyDerivation::Null },
147  { "SHA-160", KeyDerivation::Sha1Kdf },
148  { "SHA-224", KeyDerivation::Sha224Kdf },
149  { "SHA-256", KeyDerivation::Sha256Kdf },
150  { "SHA-384", KeyDerivation::Sha384Kdf },
151  { "SHA-512", KeyDerivation::Sha512Kdf }
152  };
153 }
154 
156  : m_mechanism( { static_cast<CK_MECHANISM_TYPE>(mechanism_type), nullptr, 0 }), m_parameters(nullptr)
157  {}
158 
160  {
161  auto mechanism_info_it = CryptMechanisms.find(padding);
162  if(mechanism_info_it == CryptMechanisms.end())
163  {
164  // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
165  throw Lookup_Error("PKCS#11 RSA encrypt/decrypt does not support EME " + padding);
166  }
167  RSA_CryptMechanism mechanism_info = mechanism_info_it->second;
168 
169  MechanismWrapper mech(mechanism_info.type);
170  if(mechanism_info.type == MechanismType::RsaPkcsOaep)
171  {
172  mech.m_parameters = std::make_shared<MechanismParameters>();
173  mech.m_parameters->oaep_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash);
174  mech.m_parameters->oaep_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf);
175  mech.m_parameters->oaep_params.source = CKZ_DATA_SPECIFIED;
176  mech.m_parameters->oaep_params.pSourceData = nullptr;
177  mech.m_parameters->oaep_params.ulSourceDataLen = 0;
178  mech.m_mechanism.pParameter = mech.m_parameters.get();
179  mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsOaepParams);
180  }
181  mech.m_padding_size = mechanism_info.padding_size;
182  return mech;
183  }
184 
186  {
187  auto mechanism_info_it = SignMechanisms.find(padding);
188  if(mechanism_info_it == SignMechanisms.end())
189  {
190  // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
191  throw Lookup_Error("PKCS#11 RSA sign/verify does not support EMSA " + padding);
192  }
193  RSA_SignMechanism mechanism_info = mechanism_info_it->second;
194 
195  MechanismWrapper mech(mechanism_info.type);
196  if(PssOptions.find(mechanism_info.type) != PssOptions.end())
197  {
198  mech.m_parameters = std::make_shared<MechanismParameters>();
199  mech.m_parameters->pss_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash);
200  mech.m_parameters->pss_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf);
201  mech.m_parameters->pss_params.sLen = mechanism_info.salt_size;
202  mech.m_mechanism.pParameter = mech.m_parameters.get();
203  mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsPssParams);
204  }
205  return mech;
206  }
207 
209  {
210  std::string hash_name = hash;
211 
212  if(hash_name != "Raw")
213  {
214  hash_name = hash_for_emsa(hash);
215  }
216 
217  auto mechanism_type = EcdsaHash.find(hash_name);
218  if(mechanism_type == EcdsaHash.end())
219  {
220  throw Lookup_Error("PKCS#11 ECDSA sign/verify does not support " + hash);
221  }
222  return MechanismWrapper(mechanism_type->second);
223  }
224 
226  {
227  std::vector<std::string> param_parts = split_on(params, ',');
228 
229  if(param_parts.empty() || param_parts.size() > 2)
230  throw Invalid_Argument("PKCS #11 ECDH key derivation bad params " + params);
231 
232  const bool use_cofactor =
233  (param_parts[0] == "Cofactor") ||
234  (param_parts.size() == 2 && param_parts[1] == "Cofactor");
235 
236  std::string kdf_name = (param_parts[0] == "Cofactor" ? param_parts[1] : param_parts[0]);
237  std::string hash = kdf_name;
238 
239  if(kdf_name != "Raw")
240  {
241  SCAN_Name kdf_hash(kdf_name);
242 
243  if(kdf_hash.arg_count() > 0)
244  {
245  hash = kdf_hash.arg(0);
246  }
247  }
248 
249  auto kdf = EcdhHash.find(hash);
250  if(kdf == EcdhHash.end())
251  {
252  throw Lookup_Error("PKCS#11 ECDH key derivation does not support KDF " + kdf_name);
253  }
255  mech.m_parameters = std::make_shared<MechanismParameters>();
256  mech.m_parameters->ecdh_params.kdf = static_cast<CK_EC_KDF_TYPE>(kdf->second);
257  mech.m_mechanism.pParameter = mech.m_parameters.get();
258  mech.m_mechanism.ulParameterLen = sizeof(Ecdh1DeriveParams);
259  return mech;
260  }
261 
262 }
263 }
size_t arg_count() const
Definition: scan_name.h:49
CK_ULONG CK_MECHANISM_TYPE
Definition: pkcs11t.h:583
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:144
size_t salt_size
MechanismType type
static MechanismWrapper create_ecdh_mechanism(const std::string &params)
CK_ULONG ulParameterLen
Definition: pkcs11t.h:986
MechanismWrapper(MechanismType mechanism_type)
std::string arg(size_t i) const
Definition: scan_name.cpp:122
CK_RSA_PKCS_PSS_PARAMS RsaPkcsPssParams
Definition: p11.h:849
std::string hash_for_emsa(const std::string &algo_spec)
Definition: emsa.cpp:168
size_t padding_size
MGF mgf
Definition: alg_id.cpp:13
#define CKZ_DATA_SPECIFIED
Definition: pkcs11t.h:1261
MechanismType
Definition: p11.h:335
CK_RSA_PKCS_OAEP_PARAMS RsaPkcsOaepParams
Definition: p11.h:848
static MechanismWrapper create_rsa_crypt_mechanism(const std::string &padding)
CK_ULONG CK_RSA_PKCS_MGF_TYPE
Definition: pkcs11t.h:1241
static MechanismWrapper create_ecdsa_mechanism(const std::string &hash)
CK_ECDH1_DERIVE_PARAMS Ecdh1DeriveParams
Definition: p11.h:850
CK_VOID_PTR pParameter
Definition: pkcs11t.h:985
static MechanismWrapper create_rsa_sign_mechanism(const std::string &padding)
MechanismType hash
CK_ULONG CK_EC_KDF_TYPE
Definition: pkcs11t.h:1287