Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Classes | Public Member Functions | Static Public Member Functions | List of all members
Botan::PKCS11::MechanismWrapper Class Referencefinal

`#include <p11_mechanism.h>`

## Classes
union | MechanismParameters
| Holds the mechanism parameters for OEAP, PSS and ECDH. More...

## Public Member Functions
Mechanism * | data () 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 (const std::string &params)
static MechanismWrapper | create_ecdsa_mechanism (const std::string &hash)
static MechanismWrapper | create_rsa_crypt_mechanism (const std::string &padding)
static MechanismWrapper | create_rsa_sign_mechanism (const std::string &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 26 of file p11_mechanism.h.

## Constructor & Destructor Documentation

Botan::PKCS11::MechanismWrapper::MechanismWrapper | ( | MechanismType | mechanism_type | ) |
| explicit

Parameters

  mechanism_type | the CK_MECHANISM_TYPE for the `mechanism` field of the CK_MECHANISM struct

Definition at line 154 of file p11_mechanism.cpp.

Referenced by create_ecdsa_mechanism().

155 : m_mechanism( { static_cast<CK_MECHANISM_TYPE>(mechanism_type), nullptr, 0 }), m_parameters(nullptr)
156 {}
CK_MECHANISM_TYPE
CK_ULONG CK_MECHANISM_TYPE
Definition: pkcs11t.h:583

## Member Function Documentation

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_ecdh_mechanism | ( | const std::string & | params | ) |
| static

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

Parameters

  params | specifies the key derivation function to use. Supported KDFs are Raw and SHA-160 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 224 of file p11_mechanism.cpp.

References Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_count(), Botan::PKCS11::Ecdh1CofactorDerive, Botan::PKCS11::Ecdh1Derive, hash, CK_MECHANISM::pParameter, Botan::split_on(), and CK_MECHANISM::ulParameterLen.

225 {
226 std::vector<std::string> param_parts = split_on(params, ',');
227
228 if(param_parts.empty() || param_parts.size() > 2)
229 throw Invalid_Argument("PKCS #11 ECDH key derivation bad params " + params);
230
231 const bool use_cofactor =
232 (param_parts[0] == "Cofactor") ||
233 (param_parts.size() == 2 && param_parts[1] == "Cofactor");
234
235 std::string kdf_name = (param_parts[0] == "Cofactor" ? param_parts[1] : param_parts[0]);
236 std::string hash = kdf_name;
237
238 if(kdf_name != "Raw")
239 {
240 SCAN_Name kdf_hash(kdf_name);
241
242 if(kdf_hash.arg_count() > 0)
243 {
244 hash = kdf_hash.arg(0);
245 }
246 }
247
248 auto kdf = EcdhHash.find(hash);
249 if(kdf == EcdhHash.end())
250 {
251 throw Lookup_Error("PKCS#11 ECDH key derivation does not support KDF " + kdf_name);
252 }
253 MechanismWrapper mech(use_cofactor ? MechanismType::Ecdh1CofactorDerive : MechanismType::Ecdh1Derive);
254 mech.m_parameters = std::make_shared<MechanismParameters>();
255 mech.m_parameters->ecdh_params.kdf = static_cast<CK_EC_KDF_TYPE>(kdf->second);
256 mech.m_mechanism.pParameter = mech.m_parameters.get();
257 mech.m_mechanism.ulParameterLen = sizeof(Ecdh1DeriveParams);
258 return mech;
259 }
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:138
Botan::PKCS11::MechanismWrapper::MechanismWrapper
MechanismWrapper(MechanismType mechanism_type)
Definition: p11_mechanism.cpp:154
Botan::PKCS11::MechanismType::Ecdh1CofactorDerive
Botan::PKCS11::MechanismType::Ecdh1Derive
Botan::PKCS11::Ecdh1DeriveParams
CK_ECDH1_DERIVE_PARAMS Ecdh1DeriveParams
Definition: p11.h:850
hash
MechanismType hash
Definition: p11_mechanism.cpp:63
CK_EC_KDF_TYPE
CK_ULONG CK_EC_KDF_TYPE
Definition: pkcs11t.h:1287

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_ecdsa_mechanism | ( | const std::string & | hash | ) |
| static

Creates the CK_MECHANISM data for ECDSA signature/verification

Parameters

  hash | the hash algorithm used to hash the data to sign. supported hash functions are Raw and SHA-160 to SHA-512

Definition at line 207 of file p11_mechanism.cpp.

References hash, Botan::hash_for_emsa(), and MechanismWrapper().

208 {
209 std::string hash_name = hash;
210
211 if(hash_name != "Raw")
212 {
213 hash_name = hash_for_emsa(hash);
214 }
215
216 auto mechanism_type = EcdsaHash.find(hash_name);
217 if(mechanism_type == EcdsaHash.end())
218 {
219 throw Lookup_Error("PKCS#11 ECDSA sign/verify does not support " + hash);
220 }
221 return MechanismWrapper(mechanism_type->second);
222 }
Botan::hash_for_emsa
std::string hash_for_emsa(const std::string &algo_spec)
Definition: emsa.cpp:142
Botan::PKCS11::MechanismWrapper::MechanismWrapper
MechanismWrapper(MechanismType mechanism_type)
Definition: p11_mechanism.cpp:154
hash
MechanismType hash
Definition: p11_mechanism.cpp:63

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_rsa_crypt_mechanism | ( | const std::string & | padding | ) |
| static

Creates the CK_MECHANISM data for RSA encryption/decryption

Parameters

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

Definition at line 158 of file p11_mechanism.cpp.

References CKZ_DATA_SPECIFIED, CK_MECHANISM::pParameter, Botan::PKCS11::RsaPkcsOaep, and CK_MECHANISM::ulParameterLen.

159 {
160 auto mechanism_info_it = CryptMechanisms.find(padding);
161 if(mechanism_info_it == CryptMechanisms.end())
162 {
163 // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
164 throw Lookup_Error("PKCS#11 RSA encrypt/decrypt does not support EME " + padding);
165 }
166 RSA_CryptMechanism mechanism_info = mechanism_info_it->second;
167
168 MechanismWrapper mech(mechanism_info.type);
169 if(mechanism_info.type == MechanismType::RsaPkcsOaep)
170 {
171 mech.m_parameters = std::make_shared<MechanismParameters>();
172 mech.m_parameters->oaep_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash);
173 mech.m_parameters->oaep_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf);
174 mech.m_parameters->oaep_params.source = CKZ_DATA_SPECIFIED;
175 mech.m_parameters->oaep_params.pSourceData = nullptr;
176 mech.m_parameters->oaep_params.ulSourceDataLen = 0;
177 mech.m_mechanism.pParameter = mech.m_parameters.get();
178 mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsOaepParams);
179 }
180 mech.m_padding_size = mechanism_info.padding_size;
181 return mech;
182 }
CK_MECHANISM_TYPE
CK_ULONG CK_MECHANISM_TYPE
Definition: pkcs11t.h:583
Botan::PKCS11::MechanismWrapper::MechanismWrapper
MechanismWrapper(MechanismType mechanism_type)
Definition: p11_mechanism.cpp:154
CKZ_DATA_SPECIFIED
#define CKZ_DATA_SPECIFIED
Definition: pkcs11t.h:1261
Botan::PKCS11::MechanismType::RsaPkcsOaep
Botan::PKCS11::RsaPkcsOaepParams
CK_RSA_PKCS_OAEP_PARAMS RsaPkcsOaepParams
Definition: p11.h:848
CK_RSA_PKCS_MGF_TYPE
CK_ULONG CK_RSA_PKCS_MGF_TYPE
Definition: pkcs11t.h:1241

MechanismWrapper Botan::PKCS11::MechanismWrapper::create_rsa_sign_mechanism | ( | const std::string & | padding | ) |
| static

Creates the CK_MECHANISM data for RSA signature/verification

Parameters

  padding | supported 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 184 of file p11_mechanism.cpp.

References CK_MECHANISM::pParameter, and CK_MECHANISM::ulParameterLen.

185 {
186 auto mechanism_info_it = SignMechanisms.find(padding);
187 if(mechanism_info_it == SignMechanisms.end())
188 {
189 // at this point it would be possible to support additional configurations that are not predefined above by parsing `padding`
190 throw Lookup_Error("PKCS#11 RSA sign/verify does not support EMSA " + padding);
191 }
192 RSA_SignMechanism mechanism_info = mechanism_info_it->second;
193
194 MechanismWrapper mech(mechanism_info.type);
195 if(PssOptions.find(mechanism_info.type) != PssOptions.end())
196 {
197 mech.m_parameters = std::make_shared<MechanismParameters>();
198 mech.m_parameters->pss_params.hashAlg = static_cast<CK_MECHANISM_TYPE>(mechanism_info.hash);
199 mech.m_parameters->pss_params.mgf = static_cast<CK_RSA_PKCS_MGF_TYPE>(mechanism_info.mgf);
200 mech.m_parameters->pss_params.sLen = mechanism_info.salt_size;
201 mech.m_mechanism.pParameter = mech.m_parameters.get();
202 mech.m_mechanism.ulParameterLen = sizeof(RsaPkcsPssParams);
203 }
204 return mech;
205 }
CK_MECHANISM_TYPE
CK_ULONG CK_MECHANISM_TYPE
Definition: pkcs11t.h:583
Botan::PKCS11::MechanismWrapper::MechanismWrapper
MechanismWrapper(MechanismType mechanism_type)
Definition: p11_mechanism.cpp:154
Botan::PKCS11::RsaPkcsPssParams
CK_RSA_PKCS_PSS_PARAMS RsaPkcsPssParams
Definition: p11.h:849
CK_RSA_PKCS_MGF_TYPE
CK_ULONG CK_RSA_PKCS_MGF_TYPE
Definition: pkcs11t.h:1241

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 84 of file p11_mechanism.h.

85 {
86 return const_cast<Mechanism*>(&m_mechanism);
87 }
Botan::PKCS11::ObjectClass::Mechanism

size_t Botan::PKCS11::MechanismWrapper::padding_size | ( |  | ) | const
| inline

Returns
  the size of the padding in bytes (for encryption/decryption)

Definition at line 90 of file p11_mechanism.h.

91 {
92 return m_padding_size;
93 }

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_key | key of the other party
  other_key_len | size of the key of the other party in bytes

Definition at line 77 of file p11_mechanism.h.

78 {
79 m_parameters->ecdh_params.pPublicData = const_cast<uint8_t*>(other_key);
80 m_parameters->ecdh_params.ulPublicDataLen = other_key_len;
81 }

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

  salt | the salt
  salt_len | size of the salt in bytes

Definition at line 66 of file p11_mechanism.h.

67 {
68 m_parameters->ecdh_params.pSharedData = const_cast<uint8_t*>(salt);
69 m_parameters->ecdh_params.ulSharedDataLen = salt_len;
70 }

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

- src/lib/prov/pkcs11/p11_mechanism.h
- src/lib/prov/pkcs11/p11_mechanism.cpp