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
16namespace Botan {
17
18/**
19* RSA Public Key
20*/
21class BOTAN_DLL RSA_PublicKey : public virtual IF_Scheme_PublicKey
22 {
23 public:
24 std::string algo_name() const { return "RSA"; }
25
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) :
38 {}
39
40 protected:
42 };
43
44/**
45* RSA Private Key
46*/
47class BOTAN_DLL RSA_PrivateKey : public RSA_PublicKey,
49 {
50 public:
51 bool check_key(RandomNumberGenerator& rng, bool) const;
52
54 const MemoryRegion<byte>& key_bits,
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 */
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 */
83 size_t bits, size_t exp = 65537);
84 };
85
86/**
87* RSA private (decrypt/sign) operation
88*/
91 {
92 public:
94
95 size_t max_input_bits() const { return (n.bits() - 1); }
96
97 SecureVector<byte> sign(const byte msg[], size_t msg_len,
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*/
117 public PK_Ops::Encryption
118 {
119 public:
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,
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
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:78
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition big_code.cpp:64
IF_Scheme_PrivateKey(RandomNumberGenerator &rng, const BigInt &prime1, const BigInt &prime2, const BigInt &exp, const BigInt &d_exp, const BigInt &mod)
Definition if_algo.cpp:89
IF_Scheme_PublicKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition if_algo.cpp:31
bool check_key(RandomNumberGenerator &rng, bool) const
Definition rsa.cpp:49
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
RSA_PrivateKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits, RandomNumberGenerator &rng)
Definition rsa.h:53
size_t max_input_bits() const
Definition rsa.h:95
RSA_Private_Operation(const RSA_PrivateKey &rsa)
Definition rsa.cpp:63
RSA_PublicKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition rsa.h:26
RSA_PublicKey(const BigInt &n, const BigInt &e)
Definition rsa.h:36
std::string algo_name() const
Definition rsa.h:24
SecureVector< byte > verify_mr(const byte msg[], size_t msg_len)
Definition rsa.h:134
RSA_Public_Operation(const RSA_PublicKey &rsa)
Definition rsa.h:120
size_t max_input_bits() const
Definition rsa.h:124
bool with_recovery() const
Definition rsa.h:125
SecureVector< byte > encrypt(const byte msg[], size_t msg_len, RandomNumberGenerator &)
Definition rsa.h:127
std::invalid_argument Invalid_Argument
Definition exceptn.h:20