Fabcoin Core  0.16.2
P2P Digital Currency
rsa.h
Go to the documentation of this file.
1 // rsa.h - written and placed in the public domain by Wei Dai
2 
7 
8 #ifndef CRYPTOPP_RSA_H
9 #define CRYPTOPP_RSA_H
10 
11 #include "cryptlib.h"
12 #include "pubkey.h"
13 #include "integer.h"
14 #include "pkcspad.h"
15 #include "oaep.h"
16 #include "emsa2.h"
17 #include "asn.h"
18 
20 
25 {
27 
28 public:
32  void Initialize(const Integer &n, const Integer &e)
33  {m_n = n; m_e = e;}
34 
35  // X509PublicKey
36  OID GetAlgorithmID() const;
37  void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
38  void DEREncodePublicKey(BufferedTransformation &bt) const;
39 
40  // CryptoMaterial
41  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
42  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
43  void AssignFrom(const NameValuePairs &source);
44 
45  // TrapdoorFunction
46  Integer ApplyFunction(const Integer &x) const;
47  Integer PreimageBound() const {return m_n;}
48  Integer ImageBound() const {return m_n;}
49 
50  // non-derived
51  const Integer & GetModulus() const {return m_n;}
52  const Integer & GetPublicExponent() const {return m_e;}
53 
54  void SetModulus(const Integer &n) {m_n = n;}
55  void SetPublicExponent(const Integer &e) {m_e = e;}
56 
57 protected:
58  Integer m_n, m_e;
59 };
60 
65 {
67 
68 public:
77  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
78 
89  void Initialize(const Integer &n, const Integer &e, const Integer &d, const Integer &p, const Integer &q, const Integer &dp, const Integer &dq, const Integer &u)
90  {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
91 
98  void Initialize(const Integer &n, const Integer &e, const Integer &d);
99 
100  // PKCS8PrivateKey
107  void Save(BufferedTransformation &bt) const
110  void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
112 
113  // TrapdoorFunctionInverse
115 
116  // GeneratableCryptoMaterial
117  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
119  void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
120  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
121  void AssignFrom(const NameValuePairs &source);
122 
123  // non-derived interface
124  const Integer& GetPrime1() const {return m_p;}
125  const Integer& GetPrime2() const {return m_q;}
126  const Integer& GetPrivateExponent() const {return m_d;}
127  const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
128  const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
130 
131  void SetPrime1(const Integer &p) {m_p = p;}
132  void SetPrime2(const Integer &q) {m_q = q;}
133  void SetPrivateExponent(const Integer &d) {m_d = d;}
134  void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
135  void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
137 
138 protected:
139  Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
140 };
141 
146 {
147 public:
148  Integer ApplyFunction(const Integer &x) const;
149  Integer PreimageBound() const {return ++(m_n>>1);}
150 };
151 
156 {
157 public:
159  Integer PreimageBound() const {return ++(m_n>>1);}
160 };
161 
166 {
170 };
171 
177 template <class STANDARD>
178 struct RSAES : public TF_ES<RSA, STANDARD>
179 {
180 };
181 
189 template <class STANDARD, class H>
190 struct RSASS : public TF_SS<RSA, STANDARD, H>
191 {
192 };
193 
198 {
202 };
203 
208 template <class H>
209 struct RSASS_ISO : public TF_SS<RSA_ISO, P1363_EMSA2, H>
210 {
211 };
212 
215 DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Decryptor, RSAES_PKCS1v15_Decryptor);
218 DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Encryptor, RSAES_PKCS1v15_Encryptor);
219 
222 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA> >::Decryptor, RSAES_OAEP_SHA_Decryptor);
225 DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA> >::Encryptor, RSAES_OAEP_SHA_Encryptor);
226 
227 #ifdef CRYPTOPP_DOXYGEN_PROCESSING
228 class RSASSA_PKCS1v15_SHA_Signer : public RSASS<PKCS1v15,SHA>::Signer {};
235 class RSASSA_PKCS1v15_SHA_Verifier : public RSASS<PKCS1v15,SHA>::Verifier {};
236 
237 namespace Weak {
238 
242 class RSASSA_PKCS1v15_MD2_Signer : public RSASS<PKCS1v15, Weak1::MD2>::Signer {};
246 class RSASSA_PKCS1v15_MD2_Verifier : public RSASS<PKCS1v15, Weak1::MD2>::Verifier {};
247 
251 class RSASSA_PKCS1v15_MD5_Signer : public RSASS<PKCS1v15, Weak1::MD5>::Signer {};
255 class RSASSA_PKCS1v15_MD5_Verifier : public RSASS<PKCS1v15, Weak1::MD5>::Verifier {};
256 }
257 
258 #else
261 
262 namespace Weak {
267 }
268 #endif // CRYPTOPP_DOXYGEN_PROCESSING
269 
271 
272 #endif
virtual void AssignFrom(const NameValuePairs &source)=0
Assign values to this object.
virtual void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)=0
decode privateKey part of privateKeyInfo, without the OCTET STRING header
const Integer & GetMultiplicativeInverseOfPrime2ModPrime1() const
Definition: rsa.h:129
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: rsa.h:103
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: rsa.h:105
Trapdoor Function (TF) encryption scheme.
Definition: pubkey.h:2078
#define CRYPTOPP_STATIC_CONSTEXPR
Definition: config.h:892
Classes and functions for various padding schemes used in public key algorithms.
const Integer & GetPrivateExponent() const
Definition: rsa.h:126
Encodes and decodesprivateKeyInfo.
Definition: asn.h:415
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Abstract base classes that provide a uniform interface to this library.
RSASS< PKCS1v15, Weak1::MD5 >::Verifier RSASSA_PKCS1v15_MD5_Verifier
Definition: rsa.h:266
Integer m_n
Definition: rsa.h:58
InvertibleRSAFunction_ISO PrivateKey
Definition: rsa.h:201
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: asn.cpp:587
Interface for random number generators.
Definition: cryptlib.h:1188
OID GetAlgorithmID() const
Retrieves the OID of the algorithm.
Definition: rsa.h:109
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0
encode privateKey part of privateKeyInfo, without the OCTET STRING header
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: asn.cpp:567
Interface for buffered transformations.
Definition: cryptlib.h:1352
virtual CRYPTOPP_DLL bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
void SetPrime2(const Integer &q)
Definition: rsa.h:132
Applies the inverse of the trapdoor function.
Definition: pubkey.h:183
CRYPTOPP_STATIC_CONSTEXPR const char *CRYPTOPP_API StaticAlgorithmName()
Definition: rsa.h:199
#define x(i)
Classes for PKCS padding schemes.
void SetModPrime1PrivateExponent(const Integer &dp)
Definition: rsa.h:134
const char * source
Definition: rpcconsole.cpp:60
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0
Check this object for errors.
const Integer & GetModulus() const
Definition: rsa.h:51
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: rsa.h:107
const char * name
Definition: rest.cpp:36
CRYPTOPP_STATIC_CONSTEXPR const char *CRYPTOPP_API StaticAlgorithmName()
Definition: rsa.h:167
bool Validate(int, bool, const char *)
Definition: test.cpp:884
void SetPublicExponent(const Integer &e)
Definition: rsa.h:55
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
#define CRYPTOPP_API
Definition: config.h:705
const Integer & GetPrime1() const
Definition: rsa.h:124
RSA trapdoor function using the private key.
Definition: rsa.h:155
void Initialize(const Integer &n, const Integer &e)
Initialize a RSA public key.
Definition: rsa.h:32
Applies the trapdoor function.
Definition: pubkey.h:128
RSASS< PKCS1v15, Weak1::MD2 >::Signer RSASSA_PKCS1v15_MD2_Signer
Definition: rsa.h:263
const Integer & GetPublicExponent() const
Definition: rsa.h:52
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: rsa.h:101
RSASS< PKCS1v15, SHA >::Signer RSASSA_PKCS1v15_SHA_Signer
Definition: rsa.h:259
virtual Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const =0
Calculates the inverse of an element.
void SetPrime1(const Integer &p)
Definition: rsa.h:131
Classes and functions for working with ANS.1 objects.
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rsa.h:149
RSASS< PKCS1v15, SHA >::Verifier RSASSA_PKCS1v15_SHA_Verifier
Definition: rsa.h:260
void SetModulus(const Integer &n)
Definition: rsa.h:54
void Initialize(const Integer &n, const Integer &e, const Integer &d, const Integer &p, const Integer &q, const Integer &dp, const Integer &dq, const Integer &u)
Initialize a RSA private key.
Definition: rsa.h:89
RSA signature algorithm.
Definition: rsa.h:190
DOCUMENTED_TYPEDEF(RSAES< PKCS1v15 >::Decryptor, RSAES_PKCS1v15_Decryptor)
RSAES<PKCS1v15>::Decryptor typedef
const Integer & GetModPrime1PrivateExponent() const
Definition: rsa.h:127
uint8_t const size_t const size
Definition: sha3.h:20
RSAFunction PublicKey
Definition: rsa.h:168
void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u)
Definition: rsa.h:136
RSAFunction ThisClass
Definition: rsa.h:26
virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random key or crypto parameters.
Definition: cryptlib.h:2166
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: rsa.h:48
const Integer & GetModPrime2PrivateExponent() const
Definition: rsa.h:128
OID GetAlgorithmID() const
Retrieves the OID of the algorithm.
Definition: rsa.cpp:43
RSA encryption algorithm.
Definition: rsa.h:178
Multiple precision integer with arithmetic operations.
#define NAMESPACE_END
Definition: config.h:201
#define e(i)
Definition: sha.cpp:733
RSA trapdoor function using the public key.
Definition: rsa.h:145
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: rsa.cpp:64
RSAFunction_ISO PublicKey
Definition: rsa.h:200
#define d(i)
Definition: sha.cpp:732
RSA algorithm.
Definition: rsa.h:197
Encodes and decodes subjectPublicKeyInfo.
Definition: asn.h:392
#define CRYPTOPP_DLL
Definition: config.h:704
InvertibleRSAFunction ThisClass
Definition: rsa.h:66
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rsa.h:159
Object Identifier.
Definition: asn.h:166
InvertibleRSAFunction PrivateKey
Definition: rsa.h:169
RSASS< PKCS1v15, Weak1::MD2 >::Verifier RSASSA_PKCS1v15_MD2_Verifier
Definition: rsa.h:264
const Integer & GetPrime2() const
Definition: rsa.h:125
void SetModPrime2PrivateExponent(const Integer &dq)
Definition: rsa.h:135
RSASS< PKCS1v15, Weak1::MD5 >::Signer RSASSA_PKCS1v15_MD5_Signer
Definition: rsa.h:265
void SetPrivateExponent(const Integer &d)
Definition: rsa.h:133
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: rsa.cpp:227
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rsa.h:47
Definition: panama.cpp:423
RSA trapdoor function using the public key.
Definition: rsa.h:24
OAEP padding.
Definition: oaep.h:28
RSA algorithm.
Definition: rsa.h:165
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:1989
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2105
RSA trapdoor function using the private key.
Definition: rsa.h:64
RSA signature algorithm.
Definition: rsa.h:209