Botan  2.1.0
Crypto and TLS for C++11
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/build.h>
13 #include <botan/p11.h>
14 #include <botan/p11_session.h>
15 #include <botan/p11_object.h>
16 
17 #if defined(BOTAN_HAS_RSA)
18 #include <botan/rsa.h>
19 #include <utility>
20 
21 namespace Botan {
22 namespace PKCS11 {
23 
24 /// Properties for generating a PKCS#11 RSA public key
25 class BOTAN_DLL RSA_PublicKeyGenerationProperties final : public PublicKeyProperties
26  {
27  public:
28  /// @param bits length in bits of modulus n
29  explicit RSA_PublicKeyGenerationProperties(Ulong bits);
30 
31  /// @param pub_exponent public exponent e
32  inline void set_pub_exponent(const BigInt& pub_exponent = BigInt(0x10001))
33  {
34  add_binary(AttributeType::PublicExponent, BigInt::encode(pub_exponent));
35  }
36 
37  virtual ~RSA_PublicKeyGenerationProperties() = default;
38  };
39 
40 /// Properties for importing a PKCS#11 RSA public key
41 class BOTAN_DLL RSA_PublicKeyImportProperties final : public PublicKeyProperties
42  {
43  public:
44  /// @param modulus modulus n
45  /// @param pub_exponent public exponent e
46  RSA_PublicKeyImportProperties(const BigInt& modulus, const BigInt& pub_exponent);
47 
48  /// @return the modulus
49  inline const BigInt& modulus() const
50  {
51  return m_modulus;
52  }
53 
54  /// @return the public exponent
55  inline const BigInt& pub_exponent() const
56  {
57  return m_pub_exponent;
58  }
59 
60  virtual ~RSA_PublicKeyImportProperties() = default;
61  private:
62  const BigInt m_modulus;
63  const BigInt m_pub_exponent;
64  };
65 
66 /// Represents a PKCS#11 RSA public key
67 class BOTAN_DLL PKCS11_RSA_PublicKey final : public RSA_PublicKey,
68  public Object
69  {
70  public:
72 
73  /**
74  * Creates a PKCS11_RSA_PublicKey object from an existing PKCS#11 RSA public key
75  * @param session the session to use
76  * @param handle the handle of the RSA public key
77  */
78  PKCS11_RSA_PublicKey(Session& session, ObjectHandle handle);
79 
80  /**
81  * Imports a RSA public key
82  * @param session the session to use
83  * @param pubkey_props the attributes of the public key
84  */
85  PKCS11_RSA_PublicKey(Session& session, const RSA_PublicKeyImportProperties& pubkey_props);
86 
87  std::unique_ptr<PK_Ops::Encryption>
88  create_encryption_op(RandomNumberGenerator& rng,
89  const std::string& params,
90  const std::string& provider) const override;
91 
92  std::unique_ptr<PK_Ops::Verification>
93  create_verification_op(const std::string& params,
94  const std::string& provider) const override;
95  };
96 
97 /// Properties for importing a PKCS#11 RSA private key
98 class BOTAN_DLL RSA_PrivateKeyImportProperties final : public PrivateKeyProperties
99  {
100  public:
101  /**
102  * @param modulus modulus n
103  * @param priv_exponent private exponent d
104  */
105  RSA_PrivateKeyImportProperties(const BigInt& modulus, const BigInt& priv_exponent);
106 
107  /// @param pub_exponent public exponent e
108  inline void set_pub_exponent(const BigInt& pub_exponent)
109  {
110  add_binary(AttributeType::PublicExponent, BigInt::encode(pub_exponent));
111  }
112 
113  /// @param prime1 prime p
114  inline void set_prime_1(const BigInt& prime1)
115  {
116  add_binary(AttributeType::Prime1, BigInt::encode(prime1));
117  }
118 
119  /// @param prime2 prime q
120  inline void set_prime_2(const BigInt& prime2)
121  {
122  add_binary(AttributeType::Prime2, BigInt::encode(prime2));
123  }
124 
125  /// @param exp1 private exponent d modulo p-1
126  inline void set_exponent_1(const BigInt& exp1)
127  {
128  add_binary(AttributeType::Exponent1, BigInt::encode(exp1));
129  }
130 
131  /// @param exp2 private exponent d modulo q-1
132  inline void set_exponent_2(const BigInt& exp2)
133  {
134  add_binary(AttributeType::Exponent2, BigInt::encode(exp2));
135  }
136 
137  /// @param coeff CRT coefficient q^-1 mod p
138  inline void set_coefficient(const BigInt& coeff)
139  {
140  add_binary(AttributeType::Coefficient, BigInt::encode(coeff));
141  }
142 
143  /// @return the modulus
144  inline const BigInt& modulus() const
145  {
146  return m_modulus;
147  }
148 
149  /// @return the private exponent
150  inline const BigInt& priv_exponent() const
151  {
152  return m_priv_exponent;
153  }
154 
155  virtual ~RSA_PrivateKeyImportProperties() = default;
156 
157  private:
158  const BigInt m_modulus;
159  const BigInt m_priv_exponent;
160  };
161 
162 /// Properties for generating a PKCS#11 RSA private key
163 class BOTAN_DLL RSA_PrivateKeyGenerationProperties final : public PrivateKeyProperties
164  {
165  public:
166  RSA_PrivateKeyGenerationProperties()
167  : PrivateKeyProperties(KeyType::Rsa)
168  {}
169 
170  virtual ~RSA_PrivateKeyGenerationProperties() = default;
171  };
172 
173 /// Represents a PKCS#11 RSA private key
174 class BOTAN_DLL PKCS11_RSA_PrivateKey final : public Private_Key,
175  public RSA_PublicKey,
176  public Object
177  {
178  public:
180 
181  /// Creates a PKCS11_RSA_PrivateKey object from an existing PKCS#11 RSA private key
182  PKCS11_RSA_PrivateKey(Session& session, ObjectHandle handle);
183 
184  /**
185  * Imports a RSA private key
186  * @param session the session to use
187  * @param priv_key_props the properties of the RSA private key
188  */
189  PKCS11_RSA_PrivateKey(Session& session, const RSA_PrivateKeyImportProperties& priv_key_props);
190 
191  /**
192  * Generates a PKCS#11 RSA private key
193  * @param session
194  * @param bits length in bits of modulus n
195  * @param priv_key_props the properties of the RSA private key
196  * @note no persistent public key object will be created
197  */
198  PKCS11_RSA_PrivateKey(Session& session, uint32_t bits, const RSA_PrivateKeyGenerationProperties& priv_key_props);
199 
200  /// @return the exported RSA private key
201  RSA_PrivateKey export_key() const;
202 
203  secure_vector<uint8_t> private_key_bits() const override;
204 
205  std::unique_ptr<PK_Ops::Decryption>
206  create_decryption_op(RandomNumberGenerator& rng,
207  const std::string& params,
208  const std::string& provider) const override;
209 
210  std::unique_ptr<PK_Ops::Signature>
211  create_signature_op(RandomNumberGenerator& rng,
212  const std::string& params,
213  const std::string& provider) const override;
214  };
215 
216 using PKCS11_RSA_KeyPair = std::pair<PKCS11_RSA_PublicKey, PKCS11_RSA_PrivateKey>;
217 
218 /**
219 * RSA key pair generation
220 * @param session the session that should be used for the key generation
221 * @param pub_props properties of the public key
222 * @param priv_props properties of the private key
223 */
224 BOTAN_DLL PKCS11_RSA_KeyPair generate_rsa_keypair(Session& session, const RSA_PublicKeyGenerationProperties& pub_props,
225  const RSA_PrivateKeyGenerationProperties& priv_props);
226 }
227 
228 }
229 #endif
230 
231 #endif
CK_ULONG Ulong
Definition: p11.h:836
Definition: alg_id.cpp:13
CK_OBJECT_HANDLE ObjectHandle
Definition: p11.h:846
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54