Botan 3.14.1
Crypto and TLS for C&
p11_mechanism.h
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#ifndef BOTAN_P11_MECHANISM_H_
10#define BOTAN_P11_MECHANISM_H_
11
12#include <botan/p11.h>
13
14#include <cstring>
15#include <memory>
16#include <string_view>
17
18namespace Botan {
19
22
23} // namespace Botan
24
25namespace Botan::PKCS11 {
26
27/**
28* Simple class to build and hold the data for a CK_MECHANISM struct
29* for RSA (encryption/decryption, signature/verification)
30* and EC (ECDSA signature/verification, ECDH key derivation).
31*/
33 public:
34 /// @param mechanism_type the CK_MECHANISM_TYPE for the `mechanism` field of the CK_MECHANISM struct
36
37 /**
38 * Creates the CK_MECHANISM data for RSA encryption/decryption
39 * @param options supported paddings are Raw (X.509), PKCS1v15 (PKCS#1 v1.5) and OAEP (PKCS#1 OAEP);
40 * for OAEP the hash (SHA-1, SHA-224, SHA-256, SHA-384 or SHA-512), an optional distinct MGF1 hash,
41 * and an optional label (the context) are used
42 */
44
45 /**
46 * Creates the CK_MECHANISM data for RSA signature/verification
47 * @param options supported paddings are Raw (X.509), PKCS1v15 (PKCS#1 v1.5), PSS (PKCS#1 PSS),
48 * X9.31 (ANSI X9.31) and ISO9796 (ISO/IEC 9796)
49 */
51
52 /**
53 * Creates the CK_MECHANISM data for ECDSA signature/verification
54 * @param hash the hash algorithm used to hash the data to sign.
55 * supported hash functions are Raw and SHA-1 to SHA-512
56 */
57 static MechanismWrapper create_ecdsa_mechanism(std::string_view hash);
58
59 /**
60 * Creates the CK_MECHANISM data for ECDH key derivation (CKM_ECDH1_DERIVE or CKM_ECDH1_COFACTOR_DERIVE)
61 * @param params specifies the key derivation function to use.
62 * Supported KDFs are Raw and SHA-1 to SHA-512.
63 * Params can also include the string "Cofactor" if the cofactor
64 * key derivation mechanism should be used, for example "SHA-512,Cofactor"
65 */
66 static MechanismWrapper create_ecdh_mechanism(std::string_view params);
67
68 /**
69 * Sets the salt for the ECDH mechanism parameters.
70 * @param salt the salt
71 * @param salt_len size of the salt in bytes
72 */
73 inline void set_ecdh_salt(const uint8_t salt[], size_t salt_len) {
74 m_parameters->ecdh_params.pSharedData = const_cast<uint8_t*>(salt);
75 m_parameters->ecdh_params.ulSharedDataLen = checked_ulong_cast(salt_len);
76 }
77
78 /**
79 * Sets the public key of the other party for the ECDH mechanism parameters.
80 * @param other_key key of the other party
81 * @param other_key_len size of the key of the other party in bytes
82 */
83 inline void set_ecdh_other_key(const uint8_t other_key[], size_t other_key_len) {
84 m_parameters->ecdh_params.pPublicData = const_cast<uint8_t*>(other_key);
85 m_parameters->ecdh_params.ulPublicDataLen = checked_ulong_cast(other_key_len);
86 }
87
88 /// @return a pointer to the CK_MECHANISM struct that can be passed to the cryptoki functions
89 inline Mechanism* data() const { return const_cast<Mechanism*>(&m_mechanism); }
90
91 inline MechanismType mechanism_type() const { return static_cast<MechanismType>(m_mechanism.mechanism); }
92
93 /// @return the size of the padding in bytes (for encryption/decryption)
94 inline size_t padding_size() const { return m_padding_size; }
95
96 /// @return the KDF type for an ECDH mechanism
97 inline KeyDerivation ecdh_kdf() const { return static_cast<KeyDerivation>(m_parameters->ecdh_params.kdf); }
98
99 /// Holds the mechanism parameters for OAEP, PSS and ECDH
100 ///
101 /// TODO(Botan4) use a std::variant here
102 /// TODO(Botan4) make this union decl private
110
111 private:
112 Mechanism m_mechanism;
113 std::shared_ptr<MechanismParameters> m_parameters;
114 // Storage for the OAEP label referenced from oaep_params.pSourceData
115 std::shared_ptr<const std::vector<uint8_t>> m_oaep_label;
116 size_t m_padding_size = 0;
117};
118
119} // namespace Botan::PKCS11
120
121#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
void set_ecdh_salt(const uint8_t salt[], size_t salt_len)
static MechanismWrapper create_rsa_sign_mechanism(const PK_Signature_Options_Reader &options)
KeyDerivation ecdh_kdf() const
static MechanismWrapper create_ecdh_mechanism(std::string_view params)
MechanismType mechanism_type() const
static MechanismWrapper create_ecdsa_mechanism(std::string_view hash)
MechanismWrapper(MechanismType mechanism_type)
static MechanismWrapper create_rsa_crypt_mechanism(const PK_Encryption_Options_Reader &options)
void set_ecdh_other_key(const uint8_t other_key[], size_t other_key_len)
CK_RSA_PKCS_OAEP_PARAMS RsaPkcsOaepParams
Definition p11.h:1216
Ulong checked_ulong_cast(size_t v)
Definition p11.h:1228
CK_MECHANISM Mechanism
Definition p11.h:1207
CK_ECDH1_DERIVE_PARAMS Ecdh1DeriveParams
Definition p11.h:1218
CK_RSA_PKCS_PSS_PARAMS RsaPkcsPssParams
Definition p11.h:1217