Botan 3.0.0
Crypto and TLS for C&
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 <memory>
15#include <vector>
16
17namespace Botan {
18
19class RSA_Public_Data;
20class RSA_Private_Data;
21
22/**
23* RSA Public Key
24*/
25class 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 */
34 std::span<const 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 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 const BigInt& get_int_field(std::string_view field) 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>
72 create_encryption_op(RandomNumberGenerator& rng,
73 std::string_view params,
74 std::string_view provider) const override;
75
76 std::unique_ptr<PK_Ops::KEM_Encryption>
77 create_kem_encryption_op(std::string_view params,
78 std::string_view provider) const override;
79
80 std::unique_ptr<PK_Ops::Verification>
81 create_verification_op(std::string_view params,
82 std::string_view provider) const override;
83
84 std::unique_ptr<PK_Ops::Verification>
85 create_x509_verification_op(const AlgorithmIdentifier& alg_id,
86 std::string_view provider) const override;
87
88 protected:
89 RSA_PublicKey() = default;
90
91 void init(BigInt&& n, BigInt&& e);
92
93 std::shared_ptr<const RSA_Public_Data> m_public;
94 };
95
96/**
97* RSA Private Key
98*/
99
102
104 {
105 public:
106 /**
107 * Load a private key.
108 * @param alg_id the X.509 algorithm identifier
109 * @param key_bits PKCS#1 RSAPrivateKey bits
110 */
112 std::span<const uint8_t> key_bits);
113
114 /**
115 * Construct a private key from the specified parameters.
116 * @param p the first prime
117 * @param q the second prime
118 * @param e the exponent
119 * @param d if specified, this has to be d with
120 * exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to
121 * the constructor to calculate it.
122 * @param n if specified, this must be n = p * q. Leave it as 0
123 * if you wish to the constructor to calculate it.
124 */
125 RSA_PrivateKey(const BigInt& p, const BigInt& q, const BigInt& e,
126 const BigInt& d = BigInt::zero(),
127 const BigInt& n = BigInt::zero());
128
129 /**
130 * Create a new private key with the specified bit length
131 * @param rng the random number generator to use
132 * @param bits the desired bit length of the private key
133 * @param exp the public exponent to be used
134 */
136 size_t bits, size_t exp = 65537);
137
138 std::unique_ptr<Public_Key> public_key() const override;
139
140 bool check_key(RandomNumberGenerator& rng, bool) const override;
141
142 const BigInt& get_int_field(std::string_view field) const override;
143
144 /**
145 * Get the first prime p.
146 * @return prime p
147 */
148 const BigInt& get_p() const;
149
150 /**
151 * Get the second prime q.
152 * @return prime q
153 */
154 const BigInt& get_q() const;
155
156 /**
157 * Get d with exp * d = 1 mod (p - 1, q - 1).
158 * @return d
159 */
160 const BigInt& get_d() const;
161
162 const BigInt& get_c() const;
163 const BigInt& get_d1() const;
164 const BigInt& get_d2() const;
165
166 secure_vector<uint8_t> private_key_bits() const override;
167
168 // internal functions:
169 std::shared_ptr<const RSA_Private_Data> private_data() const;
170
171 std::unique_ptr<PK_Ops::Decryption>
172 create_decryption_op(RandomNumberGenerator& rng,
173 std::string_view params,
174 std::string_view provider) const override;
175
176 std::unique_ptr<PK_Ops::KEM_Decryption>
177 create_kem_decryption_op(RandomNumberGenerator& rng,
178 std::string_view params,
179 std::string_view provider) const override;
180
181 std::unique_ptr<PK_Ops::Signature>
182 create_signature_op(RandomNumberGenerator& rng,
183 std::string_view params,
184 std::string_view provider) const override;
185
186 private:
187
188 void init(BigInt&& d, BigInt&& p, BigInt&& q, BigInt&& d1, BigInt&& d2, BigInt&& c);
189
190 std::shared_ptr<const RSA_Private_Data> m_private;
191 };
192
194
195}
196
197#endif
std::string algo_name() const override
Definition: rsa.h:43
std::shared_ptr< const RSA_Public_Data > m_public
Definition: rsa.h:93
int(* init)(CTX *)
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition: compiler.h:204
#define BOTAN_DIAGNOSTIC_PUSH
Definition: compiler.h:201
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition: compiler.h:203
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:12
PublicKeyOperation
Definition: pk_keys.h:43
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64