9#include <botan/p11_rsa.h>
11#include <botan/pk_keys.h>
13#if defined(BOTAN_HAS_RSA)
15 #include <botan/pubkey.h>
16 #include <botan/rng.h>
17 #include <botan/internal/blinding.h>
18 #include <botan/internal/p11_mechanism.h>
19 #include <botan/internal/pk_ops_impl.h>
23RSA_PublicKeyImportProperties::RSA_PublicKeyImportProperties(
const BigInt& modulus,
const BigInt& pub_exponent) :
24 PublicKeyProperties(
KeyType::
Rsa), m_modulus(modulus), m_pub_exponent(pub_exponent) {
25 add_binary(AttributeType::Modulus, m_modulus.serialize());
26 add_binary(AttributeType::PublicExponent, m_pub_exponent.serialize());
29RSA_PublicKeyGenerationProperties::RSA_PublicKeyGenerationProperties(Ulong bits) : PublicKeyProperties(
KeyType::
Rsa) {
30 add_numeric(AttributeType::ModulusBits, bits);
33PKCS11_RSA_PublicKey::PKCS11_RSA_PublicKey(Session& session, ObjectHandle handle) :
34 Object(session, handle),
38PKCS11_RSA_PublicKey::PKCS11_RSA_PublicKey(Session& session,
const RSA_PublicKeyImportProperties& pubkey_props) :
39 Object(session, pubkey_props), RSA_PublicKey(pubkey_props.modulus(), pubkey_props.pub_exponent()) {}
41RSA_PrivateKeyImportProperties::RSA_PrivateKeyImportProperties(
const BigInt& modulus,
const BigInt& priv_exponent) :
42 PrivateKeyProperties(
KeyType::
Rsa), m_modulus(modulus), m_priv_exponent(priv_exponent) {
43 add_binary(AttributeType::Modulus, m_modulus.serialize());
44 add_binary(AttributeType::PrivateExponent, m_priv_exponent.serialize());
47PKCS11_RSA_PrivateKey::PKCS11_RSA_PrivateKey(Session& session, ObjectHandle handle) :
48 Object(session, handle),
52PKCS11_RSA_PrivateKey::PKCS11_RSA_PrivateKey(Session& session,
const RSA_PrivateKeyImportProperties& priv_key_props) :
53 Object(session, priv_key_props),
56PKCS11_RSA_PrivateKey::PKCS11_RSA_PrivateKey(Session& session,
58 const RSA_PrivateKeyGenerationProperties& priv_key_props) :
59 Object(session), RSA_PublicKey() {
60 RSA_PublicKeyGenerationProperties pub_key_props(bits);
61 pub_key_props.set_encrypt(
true);
62 pub_key_props.set_verify(
true);
63 pub_key_props.set_token(
false);
68 session.module()->C_GenerateKeyPair(session.handle(),
71 static_cast<Ulong>(pub_key_props.count()),
72 priv_key_props.data(),
73 static_cast<Ulong>(priv_key_props.count()),
77 this->reset_handle(priv_key_handle);
79 BigInt n = BigInt::from_bytes(get_attribute_value(AttributeType::Modulus));
80 BigInt e = BigInt::from_bytes(get_attribute_value(AttributeType::PublicExponent));
81 RSA_PublicKey::init(std::move(n), std::move(e));
84RSA_PrivateKey PKCS11_RSA_PrivateKey::export_key()
const {
85 auto p = get_attribute_value(AttributeType::Prime1);
86 auto q = get_attribute_value(AttributeType::Prime2);
87 auto e = get_attribute_value(AttributeType::PublicExponent);
88 auto d = get_attribute_value(AttributeType::PrivateExponent);
89 auto n = get_attribute_value(AttributeType::Modulus);
91 return RSA_PrivateKey(BigInt::from_bytes(p),
92 BigInt::from_bytes(q),
93 BigInt::from_bytes(e),
94 BigInt::from_bytes(d),
95 BigInt::from_bytes(n));
98std::unique_ptr<Public_Key> PKCS11_RSA_PrivateKey::public_key()
const {
99 return std::make_unique<RSA_PublicKey>(BigInt::from_bytes(get_attribute_value(AttributeType::Modulus)),
100 BigInt::from_bytes(get_attribute_value(AttributeType::PublicExponent)));
103secure_vector<uint8_t> PKCS11_RSA_PrivateKey::private_key_bits()
const {
104 return export_key().private_key_bits();
110class PKCS11_RSA_Decryption_Operation
final :
public PK_Ops::Decryption {
112 PKCS11_RSA_Decryption_Operation(
const PKCS11_RSA_PrivateKey& key,
113 std::string_view padding,
114 RandomNumberGenerator& rng) :
116 m_mechanism(MechanismWrapper::create_rsa_crypt_mechanism(padding)),
120 [this](const BigInt& k) {
return power_mod(k, m_key.get_e(), m_key.get_n()); },
121 [
this](
const BigInt& k) {
return inverse_mod(k, m_key.get_n()); }) {
122 m_bits = m_key.get_n().bits() - 1;
125 size_t plaintext_length(
size_t )
const override {
return m_key.get_n().bytes(); }
127 secure_vector<uint8_t>
decrypt(uint8_t& valid_mask, std::span<const uint8_t> ctext)
override {
129 m_key.module()->C_DecryptInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
131 std::vector<uint8_t> encrypted_data(ctext.begin(), ctext.end());
133 const size_t modulus_bytes = (m_key.get_n().bits() + 7) / 8;
136 const bool use_blinding = !m_mechanism.padding_size();
139 const BigInt blinded = m_blinder.blind(BigInt::from_bytes(encrypted_data));
141 encrypted_data = blinded.serialize(modulus_bytes);
144 secure_vector<uint8_t> decrypted_data;
145 m_key.module()->C_Decrypt(m_key.session().handle(), encrypted_data, decrypted_data);
149 const BigInt unblinded = m_blinder.unblind(BigInt::from_bytes(decrypted_data));
150 decrypted_data.resize(modulus_bytes);
151 unblinded.serialize_to(decrypted_data);
155 return decrypted_data;
159 const PKCS11_RSA_PrivateKey& m_key;
160 MechanismWrapper m_mechanism;
167class PKCS11_RSA_Decryption_Operation_Software_EME
final :
public PK_Ops::Decryption_with_EME {
169 PKCS11_RSA_Decryption_Operation_Software_EME(
const PKCS11_RSA_PrivateKey& key,
170 std::string_view padding,
171 RandomNumberGenerator& rng) :
172 PK_Ops::Decryption_with_EME(padding), m_raw_decryptor(key, rng,
"Raw") {}
174 size_t plaintext_length(
size_t ctext_len)
const override {
return m_raw_decryptor.plaintext_length(ctext_len); }
176 secure_vector<uint8_t> raw_decrypt(std::span<const uint8_t> input)
override {
177 return m_raw_decryptor.decrypt(input);
181 PK_Decryptor_EME m_raw_decryptor;
186class PKCS11_RSA_Encryption_Operation
final :
public PK_Ops::Encryption {
188 PKCS11_RSA_Encryption_Operation(
const PKCS11_RSA_PublicKey& key, std::string_view padding) :
189 m_key(key), m_mechanism(MechanismWrapper::create_rsa_crypt_mechanism(padding)) {
190 m_bits = 8 * (key.get_n().bytes() - m_mechanism.padding_size()) - 1;
193 size_t ciphertext_length(
size_t )
const override {
return m_key.get_n().bytes(); }
195 size_t max_input_bits()
const override {
return m_bits; }
197 std::vector<uint8_t>
encrypt(std::span<const uint8_t> input, RandomNumberGenerator& )
override {
198 m_key.module()->C_EncryptInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
200 std::vector<uint8_t> encrypted_data;
201 m_key.module()->C_Encrypt(
202 m_key.session().handle(), secure_vector<uint8_t>(input.begin(), input.end()), encrypted_data);
203 return encrypted_data;
207 const PKCS11_RSA_PublicKey& m_key;
208 MechanismWrapper m_mechanism;
212class PKCS11_RSA_Signature_Operation
final :
public PK_Ops::Signature {
214 PKCS11_RSA_Signature_Operation(
const PKCS11_RSA_PrivateKey& key, std::string_view padding) :
215 m_key(key), m_mechanism(MechanismWrapper::create_rsa_sign_mechanism(padding)) {}
217 size_t signature_length()
const override {
return m_key.get_n().bytes(); }
219 void update(std::span<const uint8_t> input)
override {
222 m_key.module()->C_SignInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
223 m_initialized =
true;
224 m_first_message.assign(input.begin(), input.end());
228 if(!m_first_message.empty()) {
230 m_key.module()->C_SignUpdate(m_key.session().handle(), m_first_message);
231 m_first_message.clear();
234 m_key.module()->C_SignUpdate(m_key.session().handle(), input.data(),
static_cast<Ulong>(input.size()));
237 std::vector<uint8_t> sign(RandomNumberGenerator& )
override {
238 std::vector<uint8_t> signature;
239 if(!m_first_message.empty()) {
241 m_key.module()->C_Sign(m_key.session().handle(), m_first_message, signature);
242 m_first_message.clear();
245 m_key.module()->C_SignFinal(m_key.session().handle(), signature);
247 m_initialized =
false;
251 std::string hash_function()
const override;
253 AlgorithmIdentifier algorithm_identifier()
const override;
256 PKCS11_RSA_PrivateKey m_key;
257 bool m_initialized =
false;
258 secure_vector<uint8_t> m_first_message;
259 MechanismWrapper m_mechanism;
264std::string hash_function_name_from_pkcs11_rsa_mechanism_type(MechanismType type) {
266 case MechanismType::Sha1RsaPkcs:
267 case MechanismType::Sha1RsaPkcsPss:
268 case MechanismType::Sha1RsaX931:
271 case MechanismType::Sha224RsaPkcs:
272 case MechanismType::Sha224RsaPkcsPss:
275 case MechanismType::Sha256RsaPkcs:
276 case MechanismType::Sha256RsaPkcsPss:
279 case MechanismType::Sha384RsaPkcs:
280 case MechanismType::Sha384RsaPkcsPss:
283 case MechanismType::Sha512RsaPkcs:
284 case MechanismType::Sha512RsaPkcsPss:
287 case MechanismType::RsaX509:
288 case MechanismType::RsaX931:
289 case MechanismType::RsaPkcs:
290 case MechanismType::RsaPkcsPss:
294 throw Internal_Error(
"Unable to determine associated hash function of PKCS11 RSA signature operation");
300std::string PKCS11_RSA_Signature_Operation::hash_function()
const {
301 return hash_function_name_from_pkcs11_rsa_mechanism_type(m_mechanism.mechanism_type());
304AlgorithmIdentifier PKCS11_RSA_Signature_Operation::algorithm_identifier()
const {
305 const std::string hash = this->hash_function();
307 switch(m_mechanism.mechanism_type()) {
308 case MechanismType::Sha1RsaPkcs:
309 case MechanismType::Sha224RsaPkcs:
310 case MechanismType::Sha256RsaPkcs:
311 case MechanismType::Sha384RsaPkcs:
312 case MechanismType::Sha512RsaPkcs: {
313 const OID oid = OID::from_string(
"RSA/EMSA3(" + hash +
")");
314 return AlgorithmIdentifier(oid, AlgorithmIdentifier::USE_NULL_PARAM);
317 case MechanismType::Sha1RsaPkcsPss:
318 case MechanismType::Sha224RsaPkcsPss:
319 case MechanismType::Sha256RsaPkcsPss:
320 case MechanismType::Sha384RsaPkcsPss:
321 case MechanismType::Sha512RsaPkcsPss:
322 throw Not_Implemented(
"RSA-PSS identifier encoding missing for PKCS11");
325 throw Not_Implemented(
"No algorithm identifier defined for RSA with this PKCS11 mechanism");
329class PKCS11_RSA_Verification_Operation
final :
public PK_Ops::Verification {
331 PKCS11_RSA_Verification_Operation(
const PKCS11_RSA_PublicKey& key, std::string_view padding) :
332 m_key(key), m_mechanism(MechanismWrapper::create_rsa_sign_mechanism(padding)) {}
334 void update(std::span<const uint8_t> input)
override {
337 m_key.module()->C_VerifyInit(m_key.session().handle(), m_mechanism.data(), m_key.handle());
338 m_initialized =
true;
339 m_first_message.assign(input.begin(), input.end());
343 if(!m_first_message.empty()) {
345 m_key.module()->C_VerifyUpdate(m_key.session().handle(), m_first_message);
346 m_first_message.clear();
349 m_key.module()->C_VerifyUpdate(m_key.session().handle(), input.data(),
static_cast<Ulong>(input.size()));
352 bool is_valid_signature(std::span<const uint8_t> sig)
override {
353 ReturnValue return_value = ReturnValue::SignatureInvalid;
354 if(!m_first_message.empty()) {
356 m_key.module()->C_Verify(m_key.session().handle(),
357 m_first_message.data(),
358 static_cast<Ulong>(m_first_message.size()),
360 static_cast<Ulong>(sig.size()),
362 m_first_message.clear();
365 m_key.module()->C_VerifyFinal(
366 m_key.session().handle(), sig.data(),
static_cast<Ulong>(sig.size()), &return_value);
368 m_initialized =
false;
369 if(return_value != ReturnValue::OK && return_value != ReturnValue::SignatureInvalid) {
370 throw PKCS11_ReturnError(return_value);
372 return return_value == ReturnValue::OK;
375 std::string hash_function()
const override;
378 const PKCS11_RSA_PublicKey m_key;
379 bool m_initialized =
false;
380 secure_vector<uint8_t> m_first_message;
381 MechanismWrapper m_mechanism;
384std::string PKCS11_RSA_Verification_Operation::hash_function()
const {
385 return hash_function_name_from_pkcs11_rsa_mechanism_type(m_mechanism.mechanism_type());
390std::unique_ptr<PK_Ops::Encryption> PKCS11_RSA_PublicKey::create_encryption_op(RandomNumberGenerator& ,
391 std::string_view params,
392 std::string_view )
const {
393 return std::make_unique<PKCS11_RSA_Encryption_Operation>(*
this, params);
396std::unique_ptr<PK_Ops::Verification> PKCS11_RSA_PublicKey::create_verification_op(
397 std::string_view params, std::string_view )
const {
398 return std::make_unique<PKCS11_RSA_Verification_Operation>(*
this, params);
401std::unique_ptr<PK_Ops::Decryption> PKCS11_RSA_PrivateKey::create_decryption_op(RandomNumberGenerator& rng,
402 std::string_view params,
403 std::string_view )
const {
404 if(params !=
"Raw" && m_use_software_padding) {
405 return std::make_unique<PKCS11_RSA_Decryption_Operation_Software_EME>(*
this, params, rng);
407 return std::make_unique<PKCS11_RSA_Decryption_Operation>(*
this, params, rng);
411std::unique_ptr<PK_Ops::Signature> PKCS11_RSA_PrivateKey::create_signature_op(RandomNumberGenerator& ,
412 std::string_view params,
413 std::string_view )
const {
414 return std::make_unique<PKCS11_RSA_Signature_Operation>(*
this, params);
417PKCS11_RSA_KeyPair generate_rsa_keypair(Session& session,
418 const RSA_PublicKeyGenerationProperties& pub_props,
419 const RSA_PrivateKeyGenerationProperties& priv_props) {
425 session.module()->C_GenerateKeyPair(session.handle(),
428 static_cast<Ulong>(pub_props.count()),
430 static_cast<Ulong>(priv_props.count()),
434 return std::make_pair(PKCS11_RSA_PublicKey(session, pub_key_handle),
435 PKCS11_RSA_PrivateKey(session, priv_key_handle));
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
std::string encrypt(const uint8_t input[], size_t input_len, std::string_view passphrase, RandomNumberGenerator &rng)
std::string decrypt(const uint8_t input[], size_t input_len, std::string_view passphrase)
CK_OBJECT_HANDLE ObjectHandle
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
#define CK_INVALID_HANDLE
CK_ULONG CK_MECHANISM_TYPE