Botan 2.19.5 Crypto and TLS for C&

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 {
25 { MechanismType::RsaPkcsPss, PSS_Params(0, MechanismType::Sha1, MGF::Mgf1Sha1) },
26 { MechanismType::Sha1RsaPkcsPss, PSS_Params(20, MechanismType::Sha1, MGF::Mgf1Sha1) },
27 { MechanismType::Sha224RsaPkcsPss, PSS_Params(28, MechanismType::Sha224, MGF::Mgf1Sha224) },
28 { MechanismType::Sha256RsaPkcsPss, PSS_Params(32, MechanismType::Sha256, MGF::Mgf1Sha256) },
29 { MechanismType::Sha384RsaPkcsPss, PSS_Params(48, MechanismType::Sha384, MGF::Mgf1Sha384) },
30 { MechanismType::Sha512RsaPkcsPss, PSS_Params(64, MechanismType::Sha512, MGF::Mgf1Sha512) }
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() {}
42
43 // the mechanism to perform
44 MechanismType type;
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
64 MechanismType hash;
65
66 // mask generation function to use on the encoded block
67 MGF mgf;
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 { "EMSA_PKCS1(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcs) },
90 { "EMSA_PKCS1(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcs) },
91 { "EMSA_PKCS1(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcs) },
92 { "EMSA_PKCS1(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcs) },
93 { "EMSA_PKCS1(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcs) },
94
95 // RSASSA PKCS#1 PSS
96 { "EMSA4(Raw)", RSA_SignMechanism(MechanismType::RsaPkcsPss) },
97 { "EMSA4(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcsPss) },
98 { "EMSA4(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcsPss) },
99
100 { "EMSA4(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss) },
101 { "EMSA4(SHA-256,MGF1,32)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss) },
102 { "PSSR(SHA-256,MGF1,32)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss) },
103
104 { "EMSA4(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss) },
105 { "EMSA4(SHA-384,MGF1,48)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss) },
106 { "PSSR(SHA-384,MGF1,48)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss) },
107
108 { "EMSA4(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss) },
109 { "EMSA4(SHA-512,MGF1,64)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss) },
110 { "PSSR(SHA-512,MGF1,64)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss) },
111
112 { "ISO9796", RSA_SignMechanism(MechanismType::Rsa9796) }
113 };
114
115 struct RSA_CryptMechanism final : public MechanismData
116 {
117 RSA_CryptMechanism(MechanismType _type, size_t _padding_size, MechanismType _hash, MGF _mgf)
118 : MechanismData(_type), hash(_hash), mgf(_mgf), padding_size(_padding_size)
119 {}
120
121 RSA_CryptMechanism(MechanismType _type, size_t _padding_size)
122 : RSA_CryptMechanism(_type, _padding_size, static_cast<MechanismType>(0), static_cast<MGF>(0))
123 {}
124
125 // mechanism ID of the message digest algorithm used to calculate the digest of the encoding parameter
126 MechanismType hash;
127
128 // mask generation function to use on the encoded block
129 MGF mgf;
130
131 // number of bytes required for the padding
132 size_t padding_size;
133 };
134
135// note: when updating this map, update the documentation for `MechanismWrapper::create_rsa_crypt_mechanism`
136 static const std::map<std::string, RSA_CryptMechanism> CryptMechanisms =
137 {
138 { "Raw", RSA_CryptMechanism(MechanismType::RsaX509, 0) },
139 { "EME-PKCS1-v1_5", RSA_CryptMechanism(MechanismType::RsaPkcs, 11) },
140 { "OAEP(SHA-1)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 20, MechanismType::Sha1, MGF::Mgf1Sha1) },
141 { "OAEP(SHA-224)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 28, MechanismType::Sha224, MGF::Mgf1Sha224) },
142 { "OAEP(SHA-256)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 32, MechanismType::Sha256, MGF::Mgf1Sha256) },
143 { "OAEP(SHA-384)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 48, MechanismType::Sha384, MGF::Mgf1Sha384) },
144 { "OAEP(SHA-512)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 64, MechanismType::Sha512, MGF::Mgf1Sha512) }
145 };
146
147// note: when updating this map, update the documentation for `MechanismWrapper::create_ecdsa_mechanism`
148 static std::map<std::string, MechanismType> EcdsaHash =
149 {
150 { "Raw", MechanismType::Ecdsa },
151 { "SHA-160", MechanismType::EcdsaSha1 },
152 { "SHA-224", MechanismType::EcdsaSha224 },
153 { "SHA-256", MechanismType::EcdsaSha256 },
154 { "SHA-384", MechanismType::EcdsaSha384 },
155 { "SHA-512", MechanismType::EcdsaSha512 }
156 };
157
158// note: when updating this map, update the documentation for `MechanismWrapper::create_ecdh_mechanism`
159 static std::map<std::string, KeyDerivation> EcdhHash =
160 {
161 { "Raw", KeyDerivation::Null },
162 { "SHA-160", KeyDerivation::Sha1Kdf },
163 { "SHA-224", KeyDerivation::Sha224Kdf },
164 { "SHA-256", KeyDerivation::Sha256Kdf },
165 { "SHA-384", KeyDerivation::Sha384Kdf },
166 { "SHA-512", KeyDerivation::Sha512Kdf }
167 };
168}
169
170 MechanismWrapper::MechanismWrapper(MechanismType mechanism_type)
171 : m_mechanism( { static_cast<CK_MECHANISM_TYPE>(mechanism_type), nullptr, 0 }), m_parameters(nullptr)
172 {}
173
174 MechanismWrapper MechanismWrapper::create_rsa_crypt_mechanism(const std::string& padding)
175 {
176 auto mechanism_info_it = CryptMechanisms.find(padding);
177 if(mechanism_info_it == CryptMechanisms.end())
178 {
179 // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
180 throw Lookup_Error("PKCS#11 RSA encrypt/decrypt does not support EME " + padding);
181 }
182 RSA_CryptMechanism mechanism_info = mechanism_info_it->second;
183
184 MechanismWrapper mech(mechanism_info.type);
185 if(mechanism_info.type == MechanismType::RsaPkcsOaep)
186 {
187 mech.m_parameters = std::make_shared<MechanismParameters>();
188 mech.m_parameters->oaep_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash);
189 mech.m_parameters->oaep_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf);
190 mech.m_parameters->oaep_params.source = CKZ_DATA_SPECIFIED;
191 mech.m_parameters->oaep_params.pSourceData = nullptr;
192 mech.m_parameters->oaep_params.ulSourceDataLen = 0;
193 mech.m_mechanism.pParameter = mech.m_parameters.get();
194 mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsOaepParams);
195 }
196 mech.m_padding_size = mechanism_info.padding_size;
197 return mech;
198 }
199
200 MechanismWrapper MechanismWrapper::create_rsa_sign_mechanism(const std::string& padding)
201 {
202 auto mechanism_info_it = SignMechanisms.find(padding);
203 if(mechanism_info_it == SignMechanisms.end())
204 {
205 // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
206 throw Lookup_Error("PKCS#11 RSA sign/verify does not support EMSA " + padding);
207 }
208 RSA_SignMechanism mechanism_info = mechanism_info_it->second;
209
210 MechanismWrapper mech(mechanism_info.type);
211 if(PssOptions.find(mechanism_info.type) != PssOptions.end())
212 {
213 mech.m_parameters = std::make_shared<MechanismParameters>();
214 mech.m_parameters->pss_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash);
215 mech.m_parameters->pss_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf);
216 mech.m_parameters->pss_params.sLen = static_cast<Ulong>(mechanism_info.salt_size);
217 mech.m_mechanism.pParameter = mech.m_parameters.get();
218 mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsPssParams);
219 }
220 return mech;
221 }
222
223 MechanismWrapper MechanismWrapper::create_ecdsa_mechanism(const std::string& hash)
224 {
225 std::string hash_name = hash;
226
227 if(hash_name != "Raw")
228 {
229 hash_name = hash_for_emsa(hash);
230 }
231
232 auto mechanism_type = EcdsaHash.find(hash_name);
233 if(mechanism_type == EcdsaHash.end())
234 {
235 throw Lookup_Error("PKCS#11 ECDSA sign/verify does not support " + hash);
236 }
237 return MechanismWrapper(mechanism_type->second);
238 }
239
240 MechanismWrapper MechanismWrapper::create_ecdh_mechanism(const std::string& params)
241 {
242 std::vector<std::string> param_parts = split_on(params, ',');
243
244 if(param_parts.empty() || param_parts.size() > 2)
245 throw Invalid_Argument("PKCS #11 ECDH key derivation bad params " + params);
246
247 const bool use_cofactor =
248 (param_parts[0] == "Cofactor") ||
249 (param_parts.size() == 2 && param_parts[1] == "Cofactor");
250
251 std::string kdf_name = (param_parts[0] == "Cofactor" ? param_parts[1] : param_parts[0]);
252 std::string hash = kdf_name;
253
254 if(kdf_name != "Raw")
255 {
256 SCAN_Name kdf_hash(kdf_name);
257
258 if(kdf_hash.arg_count() > 0)
259 {
260 hash = kdf_hash.arg(0);
261 }
262 }
263
264 auto kdf = EcdhHash.find(hash);
265 if(kdf == EcdhHash.end())
266 {
267 throw Lookup_Error("PKCS#11 ECDH key derivation does not support KDF " + kdf_name);
268 }
269 MechanismWrapper mech(use_cofactor ? MechanismType::Ecdh1CofactorDerive : MechanismType::Ecdh1Derive);
270 mech.m_parameters = std::make_shared<MechanismParameters>();
271 mech.m_parameters->ecdh_params.kdf = static_cast<CK_EC_KDF_TYPE>(kdf->second);
272 mech.m_mechanism.pParameter = mech.m_parameters.get();
273 mech.m_mechanism.ulParameterLen = sizeof(Ecdh1DeriveParams);
274 return mech;
275 }
276
277}
278}
Botan::Invalid_Argument
Definition: exceptn.h:137
Botan::Lookup_Error
Definition: exceptn.h:248
Botan::PKCS11::MechanismWrapper
Definition: p11_mechanism.h:27
Botan::PKCS11::MechanismWrapper::create_ecdh_mechanism
static MechanismWrapper create_ecdh_mechanism(const std::string &params)
Definition: p11_mechanism.cpp:240
Botan::PKCS11::MechanismWrapper::create_rsa_sign_mechanism
static MechanismWrapper create_rsa_sign_mechanism(const std::string &padding)
Definition: p11_mechanism.cpp:200
Botan::PKCS11::MechanismWrapper::create_ecdsa_mechanism
static MechanismWrapper create_ecdsa_mechanism(const std::string &hash)
Definition: p11_mechanism.cpp:223
Botan::PKCS11::MechanismWrapper::create_rsa_crypt_mechanism
static MechanismWrapper create_rsa_crypt_mechanism(const std::string &padding)
Definition: p11_mechanism.cpp:174
Botan::PKCS11::MechanismWrapper::MechanismWrapper
MechanismWrapper(MechanismType mechanism_type)
Definition: p11_mechanism.cpp:170
Botan::SCAN_Name
Definition: scan_name.h:24
Botan::SCAN_Name::arg
std::string arg(size_t i) const
Definition: scan_name.cpp:127
Botan::SCAN_Name::arg_count
size_t arg_count() const
Definition: scan_name.h:56
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
Botan::PKCS11::MGF
MGF
Definition: p11.h:273
Botan::PKCS11::MGF::Mgf1Sha1
@ Mgf1Sha1
Botan::PKCS11::MGF::Mgf1Sha384
@ Mgf1Sha384
Botan::PKCS11::MGF::Mgf1Sha512
@ Mgf1Sha512
Botan::PKCS11::MGF::Mgf1Sha256
@ Mgf1Sha256
Botan::PKCS11::MGF::Mgf1Sha224
@ Mgf1Sha224
Botan::PKCS11::RsaPkcsOaepParams
CK_RSA_PKCS_OAEP_PARAMS RsaPkcsOaepParams
Definition: p11.h:850
Botan::PKCS11::KeyDerivation::Sha512Kdf
@ Sha512Kdf
Botan::PKCS11::KeyDerivation::Sha224Kdf
@ Sha224Kdf
Botan::PKCS11::KeyDerivation::Sha1Kdf
@ Sha1Kdf
Botan::PKCS11::KeyDerivation::Sha256Kdf
@ Sha256Kdf
Botan::PKCS11::KeyDerivation::Sha384Kdf
@ Sha384Kdf
Botan::PKCS11::KeyDerivation::Null
@ Null
Botan::PKCS11::RsaPkcsPssParams
CK_RSA_PKCS_PSS_PARAMS RsaPkcsPssParams
Definition: p11.h:851
Botan::PKCS11::MechanismType
MechanismType
Definition: p11.h:338
Botan::PKCS11::MechanismType::Sha512RsaPkcsPss
@ Sha512RsaPkcsPss
Botan::PKCS11::MechanismType::RsaPkcsOaep
@ RsaPkcsOaep
Botan::PKCS11::MechanismType::EcdsaSha256
@ EcdsaSha256
Botan::PKCS11::MechanismType::Ecdh1Derive
@ Ecdh1Derive
Botan::PKCS11::MechanismType::Sha1RsaPkcs
@ Sha1RsaPkcs
Botan::PKCS11::MechanismType::Sha256
@ Sha256
Botan::PKCS11::MechanismType::Rsa9796
@ Rsa9796
Botan::PKCS11::MechanismType::Sha224
@ Sha224
Botan::PKCS11::MechanismType::EcdsaSha1
@ EcdsaSha1
Botan::PKCS11::MechanismType::RsaX509
@ RsaX509
Botan::PKCS11::MechanismType::RsaPkcsPss
@ RsaPkcsPss
Botan::PKCS11::MechanismType::Sha384
@ Sha384
Botan::PKCS11::MechanismType::Sha1RsaPkcsPss
@ Sha1RsaPkcsPss
Botan::PKCS11::MechanismType::Ecdh1CofactorDerive
@ Ecdh1CofactorDerive
Botan::PKCS11::MechanismType::Ecdsa
@ Ecdsa
Botan::PKCS11::MechanismType::Sha224RsaPkcs
@ Sha224RsaPkcs
Botan::PKCS11::MechanismType::EcdsaSha384
@ EcdsaSha384
Botan::PKCS11::MechanismType::EcdsaSha512
@ EcdsaSha512
Botan::PKCS11::MechanismType::Sha1
@ Sha1
Botan::PKCS11::MechanismType::Sha1RsaX931
@ Sha1RsaX931
Botan::PKCS11::MechanismType::Sha512RsaPkcs
@ Sha512RsaPkcs
Botan::PKCS11::MechanismType::RsaX931
@ RsaX931
Botan::PKCS11::MechanismType::EcdsaSha224
@ EcdsaSha224
Botan::PKCS11::MechanismType::Sha384RsaPkcsPss
@ Sha384RsaPkcsPss
Botan::PKCS11::MechanismType::Sha256RsaPkcs
@ Sha256RsaPkcs
Botan::PKCS11::MechanismType::Sha224RsaPkcsPss
@ Sha224RsaPkcsPss
Botan::PKCS11::MechanismType::RsaPkcs
@ RsaPkcs
Botan::PKCS11::MechanismType::Sha256RsaPkcsPss
@ Sha256RsaPkcsPss
Botan::PKCS11::MechanismType::Sha512
@ Sha512
Botan::PKCS11::MechanismType::Sha384RsaPkcs
@ Sha384RsaPkcs
Botan::PKCS11::Ecdh1DeriveParams
CK_ECDH1_DERIVE_PARAMS Ecdh1DeriveParams
Definition: p11.h:852
Botan::PKCS11::Ulong
CK_ULONG Ulong
Definition: p11.h:838
Botan
Definition: alg_id.cpp:13
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:148
Botan::hash_for_emsa
std::string hash_for_emsa(const std::string &algo_spec)
Definition: emsa.cpp:189
mgf
MGF mgf
Definition: p11_mechanism.cpp:67
hash
MechanismType hash
Definition: p11_mechanism.cpp:64
type
MechanismType type
Definition: p11_mechanism.cpp:44
padding_size
size_t padding_size
Definition: p11_mechanism.cpp:132
salt_size
size_t salt_size
Definition: p11_mechanism.cpp:70
CK_EC_KDF_TYPE
CK_ULONG CK_EC_KDF_TYPE
Definition: pkcs11t.h:1287
CKZ_DATA_SPECIFIED
#define CKZ_DATA_SPECIFIED
Definition: pkcs11t.h:1261
CK_RSA_PKCS_MGF_TYPE
CK_ULONG CK_RSA_PKCS_MGF_TYPE
Definition: pkcs11t.h:1241
CK_MECHANISM_TYPE
CK_ULONG CK_MECHANISM_TYPE
Definition: pkcs11t.h:583
CK_MECHANISM::pParameter
CK_VOID_PTR pParameter
Definition: pkcs11t.h:985
CK_MECHANISM::ulParameterLen
CK_ULONG ulParameterLen
Definition: pkcs11t.h:986