Botan: src/pubkey/rsa/rsa.h Source File

Botan 1.10.17

rsa.h
Go to the documentation of this file.
1 /*
2 * RSA
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_RSA_H__
9 #define BOTAN_RSA_H__
10
11 #include <botan/if_algo.h>
12 #include <botan/pk_ops.h>
13 #include <botan/reducer.h>
14 #include <botan/blinding.h>
15
16 namespace Botan {
17
18 /**
19 * RSA Public Key
20 */
21 class BOTAN_DLL RSA_PublicKey : public virtual IF_Scheme_PublicKey
22 {
23 public:
24 std::string algo_name() const { return "RSA"; }
25
26 RSA_PublicKey(const AlgorithmIdentifier& alg_id,
27 const MemoryRegion<byte>& key_bits) :
28 IF_Scheme_PublicKey(alg_id, key_bits)
29 {}
30
31 /**
32 * Create a RSA_PublicKey
33 * @arg n the modulus
34 * @arg e the exponent
35 */
36 RSA_PublicKey(const BigInt& n, const BigInt& e) :
37 IF_Scheme_PublicKey(n, e)
38 {}
39
40 protected:
41 RSA_PublicKey() {}
42 };
43
44 /**
45 * RSA Private Key
46 */
47 class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey,
48 public IF_Scheme_PrivateKey
49 {
50 public:
51 bool check_key(RandomNumberGenerator& rng, bool) const;
52
53 RSA_PrivateKey(const AlgorithmIdentifier& alg_id,
54 const MemoryRegion<byte>& key_bits,
55 RandomNumberGenerator& rng) :
56 IF_Scheme_PrivateKey(rng, alg_id, key_bits) {}
57
58 /**
59 * Construct a private key from the specified parameters.
60 * @param rng a random number generator
61 * @param p the first prime
62 * @param q the second prime
63 * @param e the exponent
64 * @param d if specified, this has to be d with
65 * exp * d = 1 mod (p - 1, q - 1). Leave it as 0 if you wish to
66 * the constructor to calculate it.
67 * @param n if specified, this must be n = p * q. Leave it as 0
68 * if you wish to the constructor to calculate it.
69 */
70 RSA_PrivateKey(RandomNumberGenerator& rng,
71 const BigInt& p, const BigInt& q,
72 const BigInt& e, const BigInt& d = 0,
73 const BigInt& n = 0) :
74 IF_Scheme_PrivateKey(rng, p, q, e, d, n) {}
75
76 /**
77 * Create a new private key with the specified bit length
78 * @param rng the random number generator to use
79 * @param bits the desired bit length of the private key
80 * @param exp the public exponent to be used
81 */
82 RSA_PrivateKey(RandomNumberGenerator& rng,
83 size_t bits, size_t exp = 65537);
84 };
85
86 /**
87 * RSA private (decrypt/sign) operation
88 */
89 class BOTAN_DLL RSA_Private_Operation : public PK_Ops::Signature,
90 public PK_Ops::Decryption
91 {
92 public:
93 RSA_Private_Operation(const RSA_PrivateKey& rsa);
94
95 size_t max_input_bits() const { return (n.bits() - 1); }
96
97 SecureVector<byte> sign(const byte msg[], size_t msg_len,
98 RandomNumberGenerator& rng);
99
100 SecureVector<byte> decrypt(const byte msg[], size_t msg_len);
101
102 private:
103 BigInt private_op(const BigInt& m) const;
104
105 const BigInt& n;
106 const BigInt& q;
107 const BigInt& c;
108 Fixed_Exponent_Power_Mod powermod_e_n, powermod_d1_p, powermod_d2_q;
109 Modular_Reducer mod_p;
110 Blinder blinder;
111 };
112
113 /**
114 * RSA public (encrypt/verify) operation
115 */
116 class BOTAN_DLL RSA_Public_Operation : public PK_Ops::Verification,
117 public PK_Ops::Encryption
118 {
119 public:
120 RSA_Public_Operation(const RSA_PublicKey& rsa) :
121 n(rsa.get_n()), powermod_e_n(rsa.get_e(), rsa.get_n())
122 {}
123
124 size_t max_input_bits() const { return (n.bits() - 1); }
125 bool with_recovery() const { return true; }
126
127 SecureVector<byte> encrypt(const byte msg[], size_t msg_len,
128 RandomNumberGenerator&)
129 {
130 BigInt m(msg, msg_len);
131 return BigInt::encode_1363(public_op(m), n.bytes());
132 }
133
134 SecureVector<byte> verify_mr(const byte msg[], size_t msg_len)
135 {
136 BigInt m(msg, msg_len);
137 return BigInt::encode(public_op(m));
138 }
139
140 private:
141 BigInt public_op(const BigInt& m) const
142 {
143 if(m >= n)
144 throw Invalid_Argument("RSA public op - input is too large");
145 return powermod_e_n(m);
146 }
147
148 const BigInt& n;
149 Fixed_Exponent_Power_Mod powermod_e_n;
150 };
151
152 }
153
154 #endif
Botan::RSA_Public_Operation::encrypt
SecureVector< byte > encrypt(const byte msg[], size_t msg_len, RandomNumberGenerator &)
Definition: rsa.h:127
Botan::RSA_PublicKey::RSA_PublicKey
RSA_PublicKey()
Definition: rsa.h:41
Botan::BigInt
Definition: bigint.h:22
Botan::IF_Scheme_PrivateKey
Definition: if_algo.h:58
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::IF_Scheme_PublicKey
Definition: if_algo.h:21
Botan::RSA_Public_Operation::verify_mr
SecureVector< byte > verify_mr(const byte msg[], size_t msg_len)
Definition: rsa.h:134
Botan::RSA_PublicKey::algo_name
std::string algo_name() const
Definition: rsa.h:24
Botan::RSA_PrivateKey::RSA_PrivateKey
RSA_PrivateKey(RandomNumberGenerator &rng, const BigInt &p, const BigInt &q, const BigInt &e, const BigInt &d=0, const BigInt &n=0)
Definition: rsa.h:70
Botan::PK_Ops::Signature
Definition: pk_ops.h:49
Botan::RSA_Public_Operation
Definition: rsa.h:116
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::RSA_PublicKey::RSA_PublicKey
RSA_PublicKey(const BigInt &n, const BigInt &e)
Definition: rsa.h:36
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::RSA_Private_Operation
Definition: rsa.h:89
Botan::RSA_Public_Operation::max_input_bits
size_t max_input_bits() const
Definition: rsa.h:124
Botan::RSA_Private_Operation::max_input_bits
size_t max_input_bits() const
Definition: rsa.h:95
Botan::RSA_PrivateKey
Definition: rsa.h:47
Botan::RSA_PublicKey::RSA_PublicKey
RSA_PublicKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition: rsa.h:26
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::RSA_Public_Operation::with_recovery
bool with_recovery() const
Definition: rsa.h:125
Botan::RSA_Public_Operation::RSA_Public_Operation
RSA_Public_Operation(const RSA_PublicKey &rsa)
Definition: rsa.h:120
Botan::Blinder
Definition: blinding.h:19
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::Fixed_Exponent_Power_Mod
Definition: pow_mod.h:73
Botan::SecureVector< byte >
Botan::PK_Ops::Decryption
Definition: pk_ops.h:35
Botan::PK_Ops::Encryption
Definition: pk_ops.h:21
Botan::RSA_PrivateKey::RSA_PrivateKey
RSA_PrivateKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits, RandomNumberGenerator &rng)
Definition: rsa.h:53
Botan::BigInt::encode_1363
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78
Botan::Modular_Reducer
Definition: reducer.h:18
Botan::PK_Ops::Verification
Definition: pk_ops.h:85
Botan::RSA_PublicKey
Definition: rsa.h:21