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