Crypto++  5.6.3
Free C++ class library of cryptographic schemes
rsa.h
Go to the documentation of this file.
1 // rsa.h - written and placed in the public domain by Wei Dai
2 
3 //! \file rsa.h
4 //! \brief Classes for the RSA cryptosystem
5 //! \details This file contains classes that implement the RSA
6 //! ciphers and signature schemes as defined in PKCS #1 v2.0.
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 
19 NAMESPACE_BEGIN(CryptoPP)
20 
21 //! _
22 class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
23 {
24  typedef RSAFunction ThisClass;
25 
26 public:
27  void Initialize(const Integer &n, const Integer &e)
28  {m_n = n; m_e = e;}
29 
30  // X509PublicKey
31  OID GetAlgorithmID() const;
32  void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
33  void DEREncodePublicKey(BufferedTransformation &bt) const;
34 
35  // CryptoMaterial
36  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
37  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
38  void AssignFrom(const NameValuePairs &source);
39 
40  // TrapdoorFunction
41  Integer ApplyFunction(const Integer &x) const;
42  Integer PreimageBound() const {return m_n;}
43  Integer ImageBound() const {return m_n;}
44 
45  // non-derived
46  const Integer & GetModulus() const {return m_n;}
47  const Integer & GetPublicExponent() const {return m_e;}
48 
49  void SetModulus(const Integer &n) {m_n = n;}
50  void SetPublicExponent(const Integer &e) {m_e = e;}
51 
52 protected:
53  Integer m_n, m_e;
54 };
55 
56 //! _
57 class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
58 {
60 
61 public:
62  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
63  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)
64  {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
65  //! factor n given private exponent
66  void Initialize(const Integer &n, const Integer &e, const Integer &d);
67 
68  // PKCS8PrivateKey
75  void Save(BufferedTransformation &bt) const
77  OID GetAlgorithmID() const {return RSAFunction::GetAlgorithmID();}
78  void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
80 
81  // TrapdoorFunctionInverse
82  Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
83 
84  // GeneratableCryptoMaterial
85  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
86  /*! parameters: (ModulusSize, PublicExponent (default 17)) */
88  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
89  void AssignFrom(const NameValuePairs &source);
90 
91  // non-derived interface
92  const Integer& GetPrime1() const {return m_p;}
93  const Integer& GetPrime2() const {return m_q;}
94  const Integer& GetPrivateExponent() const {return m_d;}
95  const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
96  const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
97  const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
98 
99  void SetPrime1(const Integer &p) {m_p = p;}
100  void SetPrime2(const Integer &q) {m_q = q;}
101  void SetPrivateExponent(const Integer &d) {m_d = d;}
102  void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
103  void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
104  void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
105 
106 protected:
107  Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
108 };
109 
110 class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
111 {
112 public:
113  Integer ApplyFunction(const Integer &x) const;
114  Integer PreimageBound() const {return ++(m_n>>1);}
115 };
116 
118 {
119 public:
120  Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
121  Integer PreimageBound() const {return ++(m_n>>1);}
122 };
123 
124 //! RSA
125 struct CRYPTOPP_DLL RSA
126 {
127  static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
128  typedef RSAFunction PublicKey;
130 };
131 
132 //! <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
133 template <class STANDARD>
134 struct RSAES : public TF_ES<STANDARD, RSA>
135 {
136 };
137 
138 //! <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
139 /*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */
140 template <class STANDARD, class H>
141 struct RSASS : public TF_SS<STANDARD, H, RSA>
142 {
143 };
144 
145 struct CRYPTOPP_DLL RSA_ISO
146 {
147  static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
148  typedef RSAFunction_ISO PublicKey;
150 };
151 
152 template <class H>
153 struct RSASS_ISO : public TF_SS<P1363_EMSA2, H, RSA_ISO>
154 {
155 };
156 
157 // The two RSA encryption schemes defined in PKCS #1 v2.0
160 
161 typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
162 typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
163 
164 // The three RSA signature schemes defined in PKCS #1 v2.0
167 
168 namespace Weak {
169 typedef RSASS<PKCS1v15, Weak1::MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
170 typedef RSASS<PKCS1v15, Weak1::MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
171 
172 typedef RSASS<PKCS1v15, Weak1::MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
173 typedef RSASS<PKCS1v15, Weak1::MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
174 }
175 
176 NAMESPACE_END
177 
178 #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
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: rsa.h:71
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: rsa.h:73
Trapdoor Function Based Encryption Scheme.
Definition: pubkey.h:2061
Classes and functions for various padding schemes used in public key algorithms.
This file contains helper classes/functions for implementing public key algorithms.
encodes/decodes privateKeyInfo
Definition: asn.h:284
Abstract base classes that provide a uniform interface to this library.
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
virtual void DEREncodePrivateKey(BufferedTransformation &bt) const =0
encode privateKey part of privateKeyInfo, without the OCTET STRING header
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: asn.cpp:576
Interface for random number generators.
Definition: cryptlib.h:1176
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: asn.cpp:556
Interface for buffered transformations.
Definition: cryptlib.h:1342
Applies the inverse of the trapdoor function.
Definition: pubkey.h:185
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: rsa.h:75
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
Applies the trapdoor function.
Definition: pubkey.h:128
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: rsa.h:69
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:114
RSA signature scheme with appendix
Definition: rsa.h:141
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0
Check this object for errors.
virtual void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random key or crypto parameters.
Definition: cryptlib.h:2150
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: rsa.h:43
RSA cryptosystem
Definition: rsa.h:134
Crypto++ library namespace.
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: rsa.cpp:64
Definition: rsa.h:145
encodes/decodes subjectPublicKeyInfo
Definition: asn.h:265
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rsa.h:121
Object Identifier.
Definition: asn.h:91
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: rsa.h:42
Namespace containing weak and wounded algorithms.
Definition: arc4.cpp:14
_
Definition: rsa.h:22
RSA.
Definition: rsa.h:125
Interface for retrieving values given their names.
Definition: cryptlib.h:277
Trapdoor Function Based Signature Scheme.
Definition: pubkey.h:2083
Definition: rsa.h:153