monotone

monotone Mtn Source Tree

Root/cryptopp/rsa.h

1#ifndef CRYPTOPP_RSA_H
2#define CRYPTOPP_RSA_H
3
4/** \file
5This file contains classes that implement the RSA
6ciphers and signature schemes as defined in PKCS #1 v2.0.
7*/
8
9#include "pkcspad.h"
10#include "oaep.h"
11#include "integer.h"
12#include "asn.h"
13
14NAMESPACE_BEGIN(CryptoPP)
15
16//! .
17class RSAFunction : public TrapdoorFunction, public X509PublicKey
18{
19typedef RSAFunction ThisClass;
20
21public:
22void Initialize(const Integer &n, const Integer &e)
23{m_n = n; m_e = e;}
24
25// X509PublicKey
26OID GetAlgorithmID() const;
27void BERDecodeKey(BufferedTransformation &bt);
28void DEREncodeKey(BufferedTransformation &bt) const;
29
30// CryptoMaterial
31bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
32bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
33void AssignFrom(const NameValuePairs &source);
34
35// TrapdoorFunction
36Integer ApplyFunction(const Integer &x) const;
37Integer PreimageBound() const {return m_n;}
38Integer ImageBound() const {return m_n;}
39
40// non-derived
41const Integer & GetModulus() const {return m_n;}
42const Integer & GetPublicExponent() const {return m_e;}
43
44void SetModulus(const Integer &n) {m_n = n;}
45void SetPublicExponent(const Integer &e) {m_e = e;}
46
47protected:
48Integer m_n, m_e;
49};
50
51//! .
52class InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
53{
54typedef InvertibleRSAFunction ThisClass;
55
56public:
57void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
58void 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)
59{m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
60//! factor n given private exponent
61void Initialize(const Integer &n, const Integer &e, const Integer &d);
62
63// PKCS8PrivateKey
64void BERDecode(BufferedTransformation &bt)
65{PKCS8PrivateKey::BERDecode(bt);}
66void DEREncode(BufferedTransformation &bt) const
67{PKCS8PrivateKey::DEREncode(bt);}
68void BERDecodeKey(BufferedTransformation &bt);
69void DEREncodeKey(BufferedTransformation &bt) const;
70
71// TrapdoorFunctionInverse
72Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
73
74// GeneratableCryptoMaterial
75bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
76/*! parameters: (ModulusSize, PublicExponent (default 17)) */
77void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
78bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
79void AssignFrom(const NameValuePairs &source);
80
81// non-derived interface
82const Integer& GetPrime1() const {return m_p;}
83const Integer& GetPrime2() const {return m_q;}
84const Integer& GetPrivateExponent() const {return m_d;}
85const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
86const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
87const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
88
89void SetPrime1(const Integer &p) {m_p = p;}
90void SetPrime2(const Integer &q) {m_q = q;}
91void SetPrivateExponent(const Integer &d) {m_d = d;}
92void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
93void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
94void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
95
96protected:
97virtual void DEREncodeOptionalAttributes(BufferedTransformation &bt) const {}
98virtual void BERDecodeOptionalAttributes(BufferedTransformation &bt) {}
99
100Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
101};
102
103//! .
104struct RSA
105{
106static std::string StaticAlgorithmName() {return "RSA";}
107typedef RSAFunction PublicKey;
108typedef InvertibleRSAFunction PrivateKey;
109};
110
111//! <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
112template <class STANDARD>
113struct RSAES : public TF_ES<STANDARD, RSA>
114{
115};
116
117//! <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
118/*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */
119template <class STANDARD, class H>
120struct RSASS : public TF_SS<STANDARD, H, RSA>
121{
122};
123
124// The two RSA encryption schemes defined in PKCS #1 v2.0
125typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
126typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
127
128typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
129typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
130
131// The three RSA signature schemes defined in PKCS #1 v2.0
132typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
133typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
134
135typedef RSASS<PKCS1v15, MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
136typedef RSASS<PKCS1v15, MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
137
138typedef RSASS<PKCS1v15, MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
139typedef RSASS<PKCS1v15, MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
140
141NAMESPACE_END
142
143#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status