Botan 2.19.5 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
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 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 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,
115 const BigInt& e, const BigInt& d = 0,
116 const BigInt& n = 0);
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 bool check_key(RandomNumberGenerator& rng, bool) const override;
128
129 /**
130 * Get the first prime p.
131 * @return prime p
132 */
133 const BigInt& get_p() const;
134
135 /**
136 * Get the second prime q.
137 * @return prime q
138 */
139 const BigInt& get_q() const;
140
141 /**
142 * Get d with exp * d = 1 mod (p - 1, q - 1).
143 * @return d
144 */
145 const BigInt& get_d() const;
146
147 const BigInt& get_c() const;
148 const BigInt& get_d1() const;
149 const BigInt& get_d2() const;
150
151 secure_vector<uint8_t> private_key_bits() const override;
152
153 // internal functions:
154 std::shared_ptr<const RSA_Private_Data> private_data() const;
155
156 std::unique_ptr<PK_Ops::Decryption>
157 create_decryption_op(RandomNumberGenerator& rng,
158 const std::string& params,
159 const std::string& provider) const override;
160
161 std::unique_ptr<PK_Ops::KEM_Decryption>
162 create_kem_decryption_op(RandomNumberGenerator& rng,
163 const std::string& params,
164 const std::string& provider) const override;
165
166 std::unique_ptr<PK_Ops::Signature>
167 create_signature_op(RandomNumberGenerator& rng,
168 const std::string& params,
169 const std::string& provider) const override;
170
171 private:
172
173 void init(BigInt&& d, BigInt&& p, BigInt&& q, BigInt&& d1, BigInt&& d2, BigInt&& c);
174
175 std::shared_ptr<const RSA_Private_Data> m_private;
176 };
177
178}
179
180#endif
Botan::AlgorithmIdentifier
Definition: asn1_obj.h:430
Botan::BigInt
Definition: bigint.h:25
Botan::Private_Key
Definition: pk_keys.h:181
Botan::Public_Key
Definition: pk_keys.h:29
Botan::RSA_PrivateKey
Definition: rsa.h:93
Botan::RSA_PublicKey
Definition: rsa.h:26
Botan::RSA_PublicKey::algo_name
std::string algo_name() const override
Definition: rsa.h:43
Botan::RSA_PublicKey::RSA_PublicKey
RSA_PublicKey()=default
Botan::RSA_PublicKey::m_public
std::shared_ptr< const RSA_Public_Data > m_public
Definition: rsa.h:86
Botan::RandomNumberGenerator
Definition: rng.h:26
init
int(* init)(CTX *)
Definition: commoncrypto_hash.cpp:27
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:29
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Botan
Definition: alg_id.cpp:13
Botan::secure_vector
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65