Botan 3.14.1
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/bigint.h>
12#include <botan/pk_keys.h>
13#include <memory>
14#include <string>
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 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 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(
74 RandomNumberGenerator& rng, const PK_Encryption_Options_Reader& options) const override;
75
76 std::unique_ptr<PK_Ops::KEM_Encryption> _create_kem_encryption_op(
77 const PK_KEM_Options_Reader& options) const override;
78
79 std::unique_ptr<PK_Ops::Verification> _create_verification_op(
80 const PK_Signature_Options_Reader& options) const override;
81
82 std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& alg_id,
83 std::string_view provider) const override;
84
85 protected:
86 RSA_PublicKey() = default;
87
88 void init(BigInt&& n, BigInt&& e);
89
90 std::shared_ptr<const RSA_Public_Data> m_public; // NOLINT(*non-private-member-variable*)
91};
92
93/**
94* RSA Private Key
95*/
96
99
100class BOTAN_PUBLIC_API(2, 0) RSA_PrivateKey final : public virtual Private_Key,
101 public virtual RSA_PublicKey {
102 public:
103 /**
104 * Load a private key.
105 * @param alg_id the X.509 algorithm identifier
106 * @param key_bits PKCS#1 RSAPrivateKey bits
107 */
108 RSA_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
109
110 /**
111 * Construct a private key from the specified parameters.
112 * @param p the first prime
113 * @param q the second prime
114 * @param e the exponent
115 * @param d if specified, this has to be d with
116 * exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to
117 * the constructor to calculate it.
118 * @param n if specified, this must be n = p * q. Leave it as 0
119 * if you wish to the constructor to calculate it.
120 */
121 RSA_PrivateKey(const BigInt& p,
122 const BigInt& q,
123 const BigInt& e,
124 const BigInt& d = BigInt::zero(),
125 const BigInt& n = BigInt::zero());
126
127 /**
128 * Create a new private key with the specified bit length
129 * @param rng the random number generator to use
130 * @param bits the desired bit length of the private key
131 * @param exp the public exponent to be used
132 */
133 RSA_PrivateKey(RandomNumberGenerator& rng, size_t bits, size_t exp = 65537);
134
135 std::unique_ptr<Public_Key> public_key() const override;
136
137 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
138
139 const BigInt& get_int_field(std::string_view field) const override;
140
141 /**
142 * Get the first prime p.
143 * @return prime p
144 */
145 const BigInt& get_p() const;
146
147 /**
148 * Get the second prime q.
149 * @return prime q
150 */
151 const BigInt& get_q() const;
152
153 /**
154 * Get d with exp * d = 1 mod (p - 1, q - 1).
155 * @return d
156 */
157 const BigInt& get_d() const;
158
159 const BigInt& get_c() const;
160 const BigInt& get_d1() const;
161 const BigInt& get_d2() const;
162
164
165 // internal functions:
166 std::shared_ptr<const RSA_Private_Data> private_data() const;
167
168 std::unique_ptr<PK_Ops::Decryption> _create_decryption_op(
169 RandomNumberGenerator& rng, const PK_Encryption_Options_Reader& options) const override;
170
171 std::unique_ptr<PK_Ops::KEM_Decryption> _create_kem_decryption_op(
172 RandomNumberGenerator& rng, const PK_KEM_Options_Reader& options) const override;
173
174 std::unique_ptr<PK_Ops::Signature> _create_signature_op(
175 RandomNumberGenerator& rng, const PK_Signature_Options_Reader& options) const override;
176
177 private:
178 void init(BigInt&& d, BigInt&& p, BigInt&& q, BigInt&& d1, BigInt&& d2, BigInt&& c);
179
180 std::shared_ptr<const RSA_Private_Data> m_private;
181};
182
184
185} // namespace Botan
186
187#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:128
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:125
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:127
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
static BigInt zero()
Definition bigint.h:50
const BigInt & get_q() const
Definition rsa.cpp:476
const BigInt & get_int_field(std::string_view field) const override
Definition rsa.cpp:707
std::shared_ptr< const RSA_Private_Data > private_data() const
Definition rsa.cpp:452
const BigInt & get_c() const
Definition rsa.cpp:484
RSA_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition rsa.cpp:545
const BigInt & get_p() const
Definition rsa.cpp:472
const BigInt & get_d2() const
Definition rsa.cpp:492
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition rsa.cpp:732
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &options) const override
Definition rsa.cpp:1219
const BigInt & get_d() const
Definition rsa.cpp:480
std::unique_ptr< PK_Ops::KEM_Decryption > _create_kem_decryption_op(RandomNumberGenerator &rng, const PK_KEM_Options_Reader &options) const override
Definition rsa.cpp:1212
secure_vector< uint8_t > private_key_bits() const override
Definition rsa.cpp:456
std::unique_ptr< Public_Key > public_key() const override
Definition rsa.cpp:725
const BigInt & get_d1() const
Definition rsa.cpp:488
std::unique_ptr< PK_Ops::Decryption > _create_decryption_op(RandomNumberGenerator &rng, const PK_Encryption_Options_Reader &options) const override
Definition rsa.cpp:1205
void init(BigInt &&n, BigInt &&e)
Definition rsa.cpp:381
std::string algo_name() const override
Definition rsa.h:41
RSA_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition rsa.cpp:391
std::shared_ptr< const RSA_Public_Data > m_public
Definition rsa.h:90
PublicKeyOperation
Definition pk_keys.h:50
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128