Botan 3.13.0
Crypto and TLS for C&
Botan::PKCS11::MechanismWrapper Class Referencefinal

#include <p11_mechanism.h>

Classes

union  MechanismParameters

Public Member Functions

Mechanismdata () const
KeyDerivation ecdh_kdf () const
MechanismType mechanism_type () const
 MechanismWrapper (MechanismType mechanism_type)
size_t padding_size () const
void set_ecdh_other_key (const uint8_t other_key[], size_t other_key_len)
void set_ecdh_salt (const uint8_t salt[], size_t salt_len)

Static Public Member Functions

static MechanismWrapper create_ecdh_mechanism (std::string_view params)
static MechanismWrapper create_ecdsa_mechanism (std::string_view hash)
static MechanismWrapper create_rsa_crypt_mechanism (std::string_view padding)
static MechanismWrapper create_rsa_sign_mechanism (std::string_view padding)

Detailed Description

Simple class to build and hold the data for a CK_MECHANISM struct for RSA (encryption/decryption, signature/verification) and EC (ECDSA signature/verification, ECDH key derivation).

Definition at line 25 of file p11_mechanism.h.

Constructor & Destructor Documentation

◆ MechanismWrapper()

Botan::PKCS11::MechanismWrapper::MechanismWrapper ( MechanismType mechanism_type)
explicit
Parameters
mechanism_typethe CK_MECHANISM_TYPE for the mechanism field of the CK_MECHANISM struct

Definition at line 116 of file p11_mechanism.cpp.

116 :
117 m_mechanism({static_cast<CK_MECHANISM_TYPE>(mechanism_type), nullptr, 0}), m_parameters(nullptr) {}
MechanismType mechanism_type() const
CK_ULONG CK_MECHANISM_TYPE
Definition pkcs11.h:59

References mechanism_type().

Referenced by create_ecdh_mechanism(), create_ecdsa_mechanism(), create_rsa_crypt_mechanism(), and create_rsa_sign_mechanism().

Member Function Documentation

◆ create_ecdh_mechanism()

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_ecdh_mechanism ( std::string_view params)
static

Creates the CK_MECHANISM data for ECDH key derivation (CKM_ECDH1_DERIVE or CKM_ECDH1_COFACTOR_DERIVE)

Parameters
paramsspecifies the key derivation function to use. Supported KDFs are Raw and SHA-1 to SHA-512. Params can also include the string "Cofactor" if the cofactor key derivation mechanism should be used, for example "SHA-512,Cofactor"

Definition at line 277 of file p11_mechanism.cpp.

277 {
278 // note: when updating this map, update the documentation for `MechanismWrapper::create_ecdh_mechanism`
279 static const std::map<std::string_view, KeyDerivation> EcdhHash = {{"Raw", KeyDerivation::Null},
280 {"SHA-1", KeyDerivation::Sha1Kdf},
281 {"SHA-224", KeyDerivation::Sha224Kdf},
282 {"SHA-256", KeyDerivation::Sha256Kdf},
283 {"SHA-384", KeyDerivation::Sha384Kdf},
284 {"SHA-512", KeyDerivation::Sha512Kdf}};
285
286 const std::vector<std::string> param_parts = split_on(params, ',');
287
288 if(param_parts.empty() || param_parts.size() > 2) {
289 throw Invalid_Argument(fmt("PKCS #11 ECDH key derivation bad params {}", params));
290 }
291
292 // TODO(Botan4) remove this cofactor nonsense
293 bool use_cofactor = false;
294 std::string kdf_name;
295 for(const auto& part : param_parts) {
296 if(part == "Cofactor") {
297 if(use_cofactor) {
298 throw Invalid_Argument(fmt("PKCS #11 ECDH key derivation bad params {}", params));
299 }
300 use_cofactor = true;
301 } else {
302 if(!kdf_name.empty()) {
303 throw Invalid_Argument(fmt("PKCS #11 ECDH key derivation bad params {}", params));
304 }
305 kdf_name = part;
306 }
307 }
308
309 if(kdf_name.empty()) {
310 throw Invalid_Argument(fmt("PKCS #11 ECDH key derivation bad params {}", params));
311 }
312
313 auto kdf = EcdhHash.find(kdf_name);
314 if(kdf == EcdhHash.end()) {
315 throw Lookup_Error("PKCS#11 ECDH key derivation does not support KDF " + kdf_name);
316 }
318 mech.m_parameters = std::make_shared<MechanismParameters>();
319 mech.m_parameters->ecdh_params.kdf = static_cast<CK_EC_KDF_TYPE>(kdf->second);
320 mech.m_mechanism.pParameter = mech.m_parameters.get();
321 mech.m_mechanism.ulParameterLen = sizeof(Ecdh1DeriveParams);
322 return mech;
323}
MechanismWrapper(MechanismType mechanism_type)
CK_ECDH1_DERIVE_PARAMS Ecdh1DeriveParams
Definition p11.h:1218
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:141
CK_ULONG CK_EC_KDF_TYPE
Definition pkcs11.h:47

References Botan::PKCS11::Ecdh1CofactorDerive, Botan::PKCS11::Ecdh1Derive, Botan::fmt(), MechanismWrapper(), Botan::PKCS11::Null, CK_MECHANISM::pParameter, Botan::PKCS11::Sha1Kdf, Botan::PKCS11::Sha224Kdf, Botan::PKCS11::Sha256Kdf, Botan::PKCS11::Sha384Kdf, Botan::PKCS11::Sha512Kdf, Botan::split_on(), and CK_MECHANISM::ulParameterLen.

◆ create_ecdsa_mechanism()

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_ecdsa_mechanism ( std::string_view hash)
static

Creates the CK_MECHANISM data for ECDSA signature/verification

Parameters
Hashesthe hash algorithm used to hash the data to sign. supported hash functions are Raw and SHA-1 to SHA-512

Definition at line 250 of file p11_mechanism.cpp.

250 {
251 // note: when updating this map, update the documentation for `MechanismWrapper::create_ecdsa_mechanism`
252 static const std::map<std::string_view, MechanismType> EcdsaHash = {{"Raw", MechanismType::Ecdsa},
253 {"SHA-1", MechanismType::EcdsaSha1},
254 {"SHA-224", MechanismType::EcdsaSha224},
255 {"SHA-256", MechanismType::EcdsaSha256},
256 {"SHA-384", MechanismType::EcdsaSha384},
257 {"SHA-512", MechanismType::EcdsaSha512}};
258
259 const std::string hash_spec(hash_spec_view);
260 auto mechanism = EcdsaHash.find(hash_spec);
261 if(mechanism != EcdsaHash.end()) {
262 return MechanismWrapper(mechanism->second);
263 }
264
265 const SCAN_Name req(hash_spec);
266
267 if(req.algo_name() == "EMSA1" && req.arg_count() == 1) {
268 mechanism = EcdsaHash.find(req.arg(0));
269 if(mechanism != EcdsaHash.end()) {
270 return MechanismWrapper(mechanism->second);
271 }
272 }
273
274 throw Lookup_Error(fmt("PKCS #11 ECDSA sign/verify does not support {}", hash_spec));
275}

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_count(), Botan::PKCS11::Ecdsa, Botan::PKCS11::EcdsaSha1, Botan::PKCS11::EcdsaSha224, Botan::PKCS11::EcdsaSha256, Botan::PKCS11::EcdsaSha384, Botan::PKCS11::EcdsaSha512, Botan::fmt(), and MechanismWrapper().

◆ create_rsa_crypt_mechanism()

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_rsa_crypt_mechanism ( std::string_view padding)
static

Creates the CK_MECHANISM data for RSA encryption/decryption

Parameters
paddingsupported paddings are Raw (X.509), EME-PKCS1-v1_5 (PKCS#1 v1.5) and OAEP (PKCS#1 OAEP)

Definition at line 119 of file p11_mechanism.cpp.

119 {
120 // note: when updating this map, update the documentation for `MechanismWrapper::create_rsa_crypt_mechanism`
121 static const std::map<std::string_view, RSA_CryptMechanism> CryptMechanisms = {
122 {"Raw", RSA_CryptMechanism(MechanismType::RsaX509, 0)},
123 // TODO(Botan4) Remove this
124 {"EME-PKCS1-v1_5", RSA_CryptMechanism(MechanismType::RsaPkcs, 11)},
125 {"PKCS1v15", RSA_CryptMechanism(MechanismType::RsaPkcs, 11)},
126 {"OAEP(SHA-1)", RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 20, MechanismType::Sha1, MGF::Mgf1Sha1)},
127 {"OAEP(SHA-224)",
128 RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 28, MechanismType::Sha224, MGF::Mgf1Sha224)},
129 {"OAEP(SHA-256)",
130 RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 32, MechanismType::Sha256, MGF::Mgf1Sha256)},
131 {"OAEP(SHA-384)",
132 RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 48, MechanismType::Sha384, MGF::Mgf1Sha384)},
133 {"OAEP(SHA-512)",
134 RSA_CryptMechanism(MechanismType::RsaPkcsOaep, 2 + 2 * 64, MechanismType::Sha512, MGF::Mgf1Sha512)}};
135
136 auto mechanism_info_it = CryptMechanisms.find(padding);
137 if(mechanism_info_it == CryptMechanisms.end()) {
138 // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
139 throw Lookup_Error(fmt("PKCS#11 RSA encrypt/decrypt does not support padding with '{}'", padding));
140 }
141 const RSA_CryptMechanism mechanism_info = mechanism_info_it->second;
142
143 MechanismWrapper mech(mechanism_info.type());
144 if(mechanism_info.type() == MechanismType::RsaPkcsOaep) {
145 mech.m_parameters = std::make_shared<MechanismParameters>();
146 mech.m_parameters->oaep_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash());
147 mech.m_parameters->oaep_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf());
148 mech.m_parameters->oaep_params.source = CKZ_DATA_SPECIFIED;
149 mech.m_parameters->oaep_params.pSourceData = nullptr;
150 mech.m_parameters->oaep_params.ulSourceDataLen = 0;
151 mech.m_mechanism.pParameter = mech.m_parameters.get();
152 mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsOaepParams);
153 }
154 mech.m_padding_size = mechanism_info.padding_size();
155 return mech;
156}
CK_RSA_PKCS_OAEP_PARAMS RsaPkcsOaepParams
Definition p11.h:1216
#define CKZ_DATA_SPECIFIED
Definition pkcs11.h:1229
CK_ULONG CK_RSA_PKCS_MGF_TYPE
Definition pkcs11.h:71

References CKZ_DATA_SPECIFIED, Botan::fmt(), MechanismWrapper(), Botan::PKCS11::Mgf1Sha1, Botan::PKCS11::Mgf1Sha224, Botan::PKCS11::Mgf1Sha256, Botan::PKCS11::Mgf1Sha384, Botan::PKCS11::Mgf1Sha512, CK_MECHANISM::pParameter, Botan::PKCS11::RsaPkcs, Botan::PKCS11::RsaPkcsOaep, Botan::PKCS11::RsaX509, Botan::PKCS11::Sha1, Botan::PKCS11::Sha224, Botan::PKCS11::Sha256, Botan::PKCS11::Sha384, Botan::PKCS11::Sha512, and CK_MECHANISM::ulParameterLen.

◆ create_rsa_sign_mechanism()

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_rsa_sign_mechanism ( std::string_view padding)
static

Creates the CK_MECHANISM data for RSA signature/verification

Parameters
paddingsupported paddings are Raw (X.509), EMSA3 (PKCS#1 v1.5), EMSA4 (PKCS#1 PSS), EMSA2 (ANSI X9.31) and ISO9796 (ISO/IEC 9796)

Definition at line 158 of file p11_mechanism.cpp.

158 {
159 // note: when updating this map, update the documentation for `MechanismWrapper::create_rsa_sign_mechanism`
160 static const std::map<std::string_view, RSA_SignMechanism> SignMechanisms = {
161 {"Raw", RSA_SignMechanism(MechanismType::RsaX509)},
162
163 // X9.31
164 {"X9.31(Raw)", RSA_SignMechanism(MechanismType::RsaX931)},
165 {"X9.31(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaX931)},
166
167 // RSASSA PKCS#1 v1.5
168 {"PKCS1v15(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcs)},
169 {"PKCS1v15(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcs)},
170 {"PKCS1v15(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcs)},
171 {"PKCS1v15(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcs)},
172 {"PKCS1v15(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcs)},
173
174 // PSS PKCS#1 v2.0
175 {"PSS(Raw)", RSA_SignMechanism(MechanismType::RsaPkcsPss)},
176
177 {"PSS(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcsPss)},
178 {"PSS(SHA-1,MGF1,20)", RSA_SignMechanism(MechanismType::Sha1RsaPkcsPss)},
179
180 {"PSS(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcsPss)},
181 {"PSS(SHA-224,MGF1,28)", RSA_SignMechanism(MechanismType::Sha224RsaPkcsPss)},
182
183 {"PSS(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss)},
184 {"PSS(SHA-256,MGF1,32)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss)},
185
186 {"PSS(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss)},
187 {"PSS(SHA-384,MGF1,48)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss)},
188
189 {"PSS(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss)},
190 {"PSS(SHA-512,MGF1,64)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss)},
191
192 // ISO 9796 - this is the obsolete and insecure DS1 scheme, not the PSS-based DS2/DS3
193 // TODO(Botan4) remove this
194 {"ISO9796", RSA_SignMechanism(MechanismType::Rsa9796)},
195
196 // Deprecated aliases
197 // TODO(Botan4) remove these
198 {"EMSA2(Raw)", RSA_SignMechanism(MechanismType::RsaX931)},
199 {"EMSA2(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaX931)},
200
201 {"EMSA3(Raw)", RSA_SignMechanism(MechanismType::RsaPkcs)},
202 {"EMSA3(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcs)},
203 {"EMSA3(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcs)},
204 {"EMSA3(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcs)},
205 {"EMSA3(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcs)},
206 {"EMSA3(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcs)},
207
208 {"EMSA_PKCS1(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcs)},
209 {"EMSA_PKCS1(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcs)},
210 {"EMSA_PKCS1(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcs)},
211 {"EMSA_PKCS1(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcs)},
212 {"EMSA_PKCS1(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcs)},
213
214 {"EMSA4(Raw)", RSA_SignMechanism(MechanismType::RsaPkcsPss)},
215 {"EMSA4(SHA-1)", RSA_SignMechanism(MechanismType::Sha1RsaPkcsPss)},
216 {"EMSA4(SHA-224)", RSA_SignMechanism(MechanismType::Sha224RsaPkcsPss)},
217
218 {"EMSA4(SHA-256)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss)},
219 {"EMSA4(SHA-256,MGF1,32)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss)},
220 {"PSSR(SHA-256,MGF1,32)", RSA_SignMechanism(MechanismType::Sha256RsaPkcsPss)},
221
222 {"EMSA4(SHA-384)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss)},
223 {"EMSA4(SHA-384,MGF1,48)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss)},
224 {"PSSR(SHA-384,MGF1,48)", RSA_SignMechanism(MechanismType::Sha384RsaPkcsPss)},
225
226 {"EMSA4(SHA-512)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss)},
227 {"EMSA4(SHA-512,MGF1,64)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss)},
228 {"PSSR(SHA-512,MGF1,64)", RSA_SignMechanism(MechanismType::Sha512RsaPkcsPss)},
229 };
230
231 auto mechanism_info_it = SignMechanisms.find(padding);
232 if(mechanism_info_it == SignMechanisms.end()) {
233 // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
234 throw Lookup_Error(fmt("PKCS#11 RSA sign/verify does not support padding with '{}'", padding));
235 }
236 const RSA_SignMechanism mechanism_info = mechanism_info_it->second;
237
238 MechanismWrapper mech(mechanism_info.type());
239 if(PssOptions().contains(mechanism_info.type())) {
240 mech.m_parameters = std::make_shared<MechanismParameters>();
241 mech.m_parameters->pss_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash());
242 mech.m_parameters->pss_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf());
243 mech.m_parameters->pss_params.sLen = checked_ulong_cast(mechanism_info.salt_size());
244 mech.m_mechanism.pParameter = mech.m_parameters.get();
245 mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsPssParams);
246 }
247 return mech;
248}
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
CK_RSA_PKCS_PSS_PARAMS RsaPkcsPssParams
Definition p11.h:1217

References Botan::PKCS11::checked_ulong_cast(), Botan::fmt(), MechanismWrapper(), CK_MECHANISM::pParameter, Botan::PKCS11::Rsa9796, Botan::PKCS11::RsaPkcs, Botan::PKCS11::RsaPkcsPss, Botan::PKCS11::RsaX509, Botan::PKCS11::RsaX931, Botan::PKCS11::Sha1RsaPkcs, Botan::PKCS11::Sha1RsaPkcsPss, Botan::PKCS11::Sha1RsaX931, Botan::PKCS11::Sha224RsaPkcs, Botan::PKCS11::Sha224RsaPkcsPss, Botan::PKCS11::Sha256RsaPkcs, Botan::PKCS11::Sha256RsaPkcsPss, Botan::PKCS11::Sha384RsaPkcs, Botan::PKCS11::Sha384RsaPkcsPss, Botan::PKCS11::Sha512RsaPkcs, Botan::PKCS11::Sha512RsaPkcsPss, and CK_MECHANISM::ulParameterLen.

◆ data()

Mechanism * Botan::PKCS11::MechanismWrapper::data ( ) const
inline
Returns
a pointer to the CK_MECHANISM struct that can be passed to the cryptoki functions

Definition at line 80 of file p11_mechanism.h.

80{ return const_cast<Mechanism*>(&m_mechanism); }
CK_MECHANISM Mechanism
Definition p11.h:1207

◆ ecdh_kdf()

KeyDerivation Botan::PKCS11::MechanismWrapper::ecdh_kdf ( ) const
inline
Returns
the KDF type for an ECDH mechanism

Definition at line 88 of file p11_mechanism.h.

88{ return static_cast<KeyDerivation>(m_parameters->ecdh_params.kdf); }

◆ mechanism_type()

MechanismType Botan::PKCS11::MechanismWrapper::mechanism_type ( ) const
inline

Definition at line 82 of file p11_mechanism.h.

82{ return static_cast<MechanismType>(m_mechanism.mechanism); }

Referenced by MechanismWrapper().

◆ padding_size()

size_t Botan::PKCS11::MechanismWrapper::padding_size ( ) const
inline
Returns
the size of the padding in bytes (for encryption/decryption)

Definition at line 85 of file p11_mechanism.h.

85{ return m_padding_size; }

◆ set_ecdh_other_key()

void Botan::PKCS11::MechanismWrapper::set_ecdh_other_key ( const uint8_t other_key[],
size_t other_key_len )
inline

Sets the public key of the other party for the ECDH mechanism parameters.

Parameters
other_keykey of the other party
other_key_lensize of the key of the other party in bytes

Definition at line 74 of file p11_mechanism.h.

74 {
75 m_parameters->ecdh_params.pPublicData = const_cast<uint8_t*>(other_key);
76 m_parameters->ecdh_params.ulPublicDataLen = checked_ulong_cast(other_key_len);
77 }

References Botan::PKCS11::checked_ulong_cast().

◆ set_ecdh_salt()

void Botan::PKCS11::MechanismWrapper::set_ecdh_salt ( const uint8_t salt[],
size_t salt_len )
inline

Sets the salt for the ECDH mechanism parameters.

Parameters
saltthe salt
salt_lensize of the salt in bytes

Definition at line 64 of file p11_mechanism.h.

64 {
65 m_parameters->ecdh_params.pSharedData = const_cast<uint8_t*>(salt);
66 m_parameters->ecdh_params.ulSharedDataLen = checked_ulong_cast(salt_len);
67 }

References Botan::PKCS11::checked_ulong_cast().


The documentation for this class was generated from the following files: