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