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