Botan  2.9.0
Crypto and TLS for C++11
rsa.h
Go to the documentation of this file.
1 /*
2 * RSA
3 * (C) 1999-2008,2016 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_RSA_H_
9 #define BOTAN_RSA_H_
10 
11 #include <botan/pk_keys.h>
12 #include <botan/bigint.h>
13 #include <string>
14 #include <vector>
15 
16 namespace Botan {
17 
18 /**
19 * RSA Public Key
20 */
21 class BOTAN_PUBLIC_API(2,0) RSA_PublicKey : public virtual Public_Key
22  {
23  public:
24  /**
25  * Load a public key.
26  * @param alg_id the X.509 algorithm identifier
27  * @param key_bits DER encoded public key bits
28  */
29  RSA_PublicKey(const AlgorithmIdentifier& alg_id,
30  const std::vector<uint8_t>& key_bits);
31 
32  /**
33  * Create a public key.
34  * @arg n the modulus
35  * @arg e the exponent
36  */
37  RSA_PublicKey(const BigInt& n, const BigInt& e) :
38  m_n(n), m_e(e) {}
39 
40  std::string algo_name() const override { return "RSA"; }
41 
42  bool check_key(RandomNumberGenerator& rng, bool) const override;
43 
44  AlgorithmIdentifier algorithm_identifier() const override;
45 
46  std::vector<uint8_t> public_key_bits() const override;
47 
48  /**
49  * @return public modulus
50  */
51  const BigInt& get_n() const { return m_n; }
52 
53  /**
54  * @return public exponent
55  */
56  const BigInt& get_e() const { return m_e; }
57 
58  size_t key_length() const override;
59  size_t estimated_strength() const override;
60 
61  std::unique_ptr<PK_Ops::Encryption>
62  create_encryption_op(RandomNumberGenerator& rng,
63  const std::string& params,
64  const std::string& provider) const override;
65 
66  std::unique_ptr<PK_Ops::KEM_Encryption>
67  create_kem_encryption_op(RandomNumberGenerator& rng,
68  const std::string& params,
69  const std::string& provider) const override;
70 
71  std::unique_ptr<PK_Ops::Verification>
72  create_verification_op(const std::string& params,
73  const std::string& provider) const override;
74 
75  protected:
76  RSA_PublicKey() = default;
77 
79  };
80 
81 /**
82 * RSA Private Key
83 */
85  {
86  public:
87  /**
88  * Load a private key.
89  * @param alg_id the X.509 algorithm identifier
90  * @param key_bits PKCS#1 RSAPrivateKey bits
91  */
92  RSA_PrivateKey(const AlgorithmIdentifier& alg_id,
93  const secure_vector<uint8_t>& key_bits);
94 
95  /**
96  * Construct a private key from the specified parameters.
97  * @param p the first prime
98  * @param q the second prime
99  * @param e the exponent
100  * @param d if specified, this has to be d with
101  * exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to
102  * the constructor to calculate it.
103  * @param n if specified, this must be n = p * q. Leave it as 0
104  * if you wish to the constructor to calculate it.
105  */
106  RSA_PrivateKey(const BigInt& p, const BigInt& q,
107  const BigInt& e, const BigInt& d = 0,
108  const BigInt& n = 0);
109 
110  /**
111  * Create a new private key with the specified bit length
112  * @param rng the random number generator to use
113  * @param bits the desired bit length of the private key
114  * @param exp the public exponent to be used
115  */
117  size_t bits, size_t exp = 65537);
118 
119  bool check_key(RandomNumberGenerator& rng, bool) const override;
120 
121  /**
122  * Get the first prime p.
123  * @return prime p
124  */
125  const BigInt& get_p() const { return m_p; }
126 
127  /**
128  * Get the second prime q.
129  * @return prime q
130  */
131  const BigInt& get_q() const { return m_q; }
132 
133  /**
134  * Get d with exp * d = 1 mod (p - 1, q - 1).
135  * @return d
136  */
137  const BigInt& get_d() const { return m_d; }
138 
139  const BigInt& get_c() const { return m_c; }
140  const BigInt& get_d1() const { return m_d1; }
141  const BigInt& get_d2() const { return m_d2; }
142 
143  secure_vector<uint8_t> private_key_bits() const override;
144 
145  std::unique_ptr<PK_Ops::Decryption>
146  create_decryption_op(RandomNumberGenerator& rng,
147  const std::string& params,
148  const std::string& provider) const override;
149 
150  std::unique_ptr<PK_Ops::KEM_Decryption>
151  create_kem_decryption_op(RandomNumberGenerator& rng,
152  const std::string& params,
153  const std::string& provider) const override;
154 
155  std::unique_ptr<PK_Ops::Signature>
156  create_signature_op(RandomNumberGenerator& rng,
157  const std::string& params,
158  const std::string& provider) const override;
159 
160  private:
161  BigInt m_d, m_p, m_q, m_d1, m_d2, m_c;
162  };
163 
164 }
165 
166 #endif
const BigInt & m_e
Definition: rsa.cpp:412
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
const BigInt & get_n() const
Definition: rsa.h:51
RSA_PublicKey(const BigInt &n, const BigInt &e)
Definition: rsa.h:37
const BigInt & get_c() const
Definition: rsa.h:139
const BigInt & get_d1() const
Definition: rsa.h:140
const BigInt & get_e() const
Definition: rsa.h:56
const BigInt & get_q() const
Definition: rsa.h:131
std::string algo_name() const override
Definition: rsa.h:40
Definition: alg_id.cpp:13
const BigInt & get_d() const
Definition: rsa.h:137
const BigInt & get_d2() const
Definition: rsa.h:141
const BigInt & get_p() const
Definition: rsa.h:125
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
BigInt m_n
Definition: rsa.h:78
const BigInt & m_n
Definition: rsa.cpp:411