Botan 3.4.0
Crypto and TLS for C&
p11_rsa.h
Go to the documentation of this file.
1/*
2* PKCS#11 RSA
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_RSA_H_
10#define BOTAN_P11_RSA_H_
11
12#include <botan/bigint.h>
13#include <botan/p11_object.h>
14#include <botan/p11_types.h>
15#include <botan/pk_keys.h>
16
17#if defined(BOTAN_HAS_RSA)
18 #include <botan/rsa.h>
19 #include <utility>
20
21namespace Botan::PKCS11 {
22
23/// Properties for generating a PKCS#11 RSA public key
24class BOTAN_PUBLIC_API(2, 0) RSA_PublicKeyGenerationProperties final : public PublicKeyProperties {
25 public:
26 /// @param bits length in bits of modulus n
27 explicit RSA_PublicKeyGenerationProperties(Ulong bits);
28
29 /// @param pub_exponent public exponent e
30 inline void set_pub_exponent(const BigInt& pub_exponent = BigInt::from_word(0x10001)) {
31 add_binary(AttributeType::PublicExponent, BigInt::encode(pub_exponent));
32 }
33
34 ~RSA_PublicKeyGenerationProperties() override = default;
35};
36
37/// Properties for importing a PKCS#11 RSA public key
38class BOTAN_PUBLIC_API(2, 0) RSA_PublicKeyImportProperties final : public PublicKeyProperties {
39 public:
40 /// @param modulus modulus n
41 /// @param pub_exponent public exponent e
42 RSA_PublicKeyImportProperties(const BigInt& modulus, const BigInt& pub_exponent);
43
44 /// @return the modulus
45 inline const BigInt& modulus() const { return m_modulus; }
46
47 /// @return the public exponent
48 inline const BigInt& pub_exponent() const { return m_pub_exponent; }
49
50 ~RSA_PublicKeyImportProperties() override = default;
51
52 private:
53 const BigInt m_modulus;
54 const BigInt m_pub_exponent;
55};
56
57/// Represents a PKCS#11 RSA public key
58class BOTAN_PUBLIC_API(2, 0) PKCS11_RSA_PublicKey : public Object,
59 public RSA_PublicKey {
60 public:
61 static const ObjectClass Class = ObjectClass::PublicKey;
62
63 /**
64 * Creates a PKCS11_RSA_PublicKey object from an existing PKCS#11 RSA public key
65 * @param session the session to use
66 * @param handle the handle of the RSA public key
67 */
68 PKCS11_RSA_PublicKey(Session& session, ObjectHandle handle);
69
70 /**
71 * Imports a RSA public key
72 * @param session the session to use
73 * @param pubkey_props the attributes of the public key
74 */
75 PKCS11_RSA_PublicKey(Session& session, const RSA_PublicKeyImportProperties& pubkey_props);
76
77 /**
78 * @throws Not_Implemented
79 */
80 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator&) const final {
81 throw Not_Implemented("Cannot generate a new PKCS#11 RSA keypair from this public key");
82 }
83
84 std::unique_ptr<PK_Ops::Encryption> create_encryption_op(RandomNumberGenerator& rng,
85 std::string_view params,
86 std::string_view provider) const override;
87
88 std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
89 std::string_view provider) const override;
90};
91
92/// Properties for importing a PKCS#11 RSA private key
93class BOTAN_PUBLIC_API(2, 0) RSA_PrivateKeyImportProperties final : public PrivateKeyProperties {
94 public:
95 /**
96 * @param modulus modulus n
97 * @param priv_exponent private exponent d
98 */
99 RSA_PrivateKeyImportProperties(const BigInt& modulus, const BigInt& priv_exponent);
100
101 /// @param pub_exponent public exponent e
102 inline void set_pub_exponent(const BigInt& pub_exponent) {
103 add_binary(AttributeType::PublicExponent, BigInt::encode(pub_exponent));
104 }
105
106 /// @param prime1 prime p
107 inline void set_prime_1(const BigInt& prime1) { add_binary(AttributeType::Prime1, BigInt::encode(prime1)); }
108
109 /// @param prime2 prime q
110 inline void set_prime_2(const BigInt& prime2) { add_binary(AttributeType::Prime2, BigInt::encode(prime2)); }
111
112 /// @param exp1 private exponent d modulo p-1
113 inline void set_exponent_1(const BigInt& exp1) { add_binary(AttributeType::Exponent1, BigInt::encode(exp1)); }
114
115 /// @param exp2 private exponent d modulo q-1
116 inline void set_exponent_2(const BigInt& exp2) { add_binary(AttributeType::Exponent2, BigInt::encode(exp2)); }
117
118 /// @param coeff CRT coefficient q^-1 mod p
119 inline void set_coefficient(const BigInt& coeff) {
120 add_binary(AttributeType::Coefficient, BigInt::encode(coeff));
121 }
122
123 /// @return the modulus
124 inline const BigInt& modulus() const { return m_modulus; }
125
126 /// @return the private exponent
127 inline const BigInt& priv_exponent() const { return m_priv_exponent; }
128
129 ~RSA_PrivateKeyImportProperties() override = default;
130
131 private:
132 const BigInt m_modulus;
133 const BigInt m_priv_exponent;
134};
135
136/// Properties for generating a PKCS#11 RSA private key
137class BOTAN_PUBLIC_API(2, 0) RSA_PrivateKeyGenerationProperties final : public PrivateKeyProperties {
138 public:
139 RSA_PrivateKeyGenerationProperties() : PrivateKeyProperties(KeyType::Rsa) {}
140
141 ~RSA_PrivateKeyGenerationProperties() override = default;
142};
143
144/// Represents a PKCS#11 RSA private key
145
148
149class BOTAN_PUBLIC_API(2, 0) PKCS11_RSA_PrivateKey final : public Object,
150 public Private_Key,
151 public RSA_PublicKey {
152 public:
153 static const ObjectClass Class = ObjectClass::PrivateKey;
154
155 /// Creates a PKCS11_RSA_PrivateKey object from an existing PKCS#11 RSA private key
156 PKCS11_RSA_PrivateKey(Session& session, ObjectHandle handle);
157
158 /**
159 * Imports a RSA private key
160 * @param session the session to use
161 * @param priv_key_props the properties of the RSA private key
162 */
163 PKCS11_RSA_PrivateKey(Session& session, const RSA_PrivateKeyImportProperties& priv_key_props);
164
165 /**
166 * Generates a PKCS#11 RSA private key
167 * @param session the session to use
168 * @param bits length in bits of modulus n
169 * @param priv_key_props the properties of the RSA private key
170 * @note no persistent public key object will be created
171 */
172 PKCS11_RSA_PrivateKey(Session& session, uint32_t bits, const RSA_PrivateKeyGenerationProperties& priv_key_props);
173
174 /// @return the exported RSA private key
175 RSA_PrivateKey export_key() const;
176
177 /**
178 * If enabled, the PKCS#11 module gets to perform the raw RSA decryption
179 * using a blinded ciphertext. The EME unpadding is performed in software.
180 * This essenially hides the plaintext value from the PKCS#11 module.
181 *
182 * @param software_padding if true, perform the unpadding in software
183 */
184 void set_use_software_padding(bool software_padding) { m_use_software_padding = software_padding; }
185
186 bool uses_software_padding() const { return m_use_software_padding; }
187
188 secure_vector<uint8_t> private_key_bits() const override;
189
190 std::unique_ptr<Public_Key> public_key() const override;
191
192 std::unique_ptr<PK_Ops::Decryption> create_decryption_op(RandomNumberGenerator& rng,
193 std::string_view params,
194 std::string_view provider) const override;
195
196 std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
197 std::string_view params,
198 std::string_view provider) const override;
199
200 private:
201 bool m_use_software_padding = false;
202};
203
205
206using PKCS11_RSA_KeyPair = std::pair<PKCS11_RSA_PublicKey, PKCS11_RSA_PrivateKey>;
207
208/**
209* RSA key pair generation
210* @param session the session that should be used for the key generation
211* @param pub_props properties of the public key
212* @param priv_props properties of the private key
213*/
215PKCS11_RSA_KeyPair generate_rsa_keypair(Session& session,
216 const RSA_PublicKeyGenerationProperties& pub_props,
217 const RSA_PrivateKeyGenerationProperties& priv_props);
218} // namespace Botan::PKCS11
219#endif
220
221#endif
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition compiler.h:191
#define BOTAN_DIAGNOSTIC_PUSH
Definition compiler.h:188
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition compiler.h:190
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31