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