monotone

monotone Mtn Source Tree

Root/cryptopp/rsa.cpp

1// rsa.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "rsa.h"
5#include "asn.h"
6#include "oids.h"
7#include "modarith.h"
8#include "nbtheory.h"
9#include "sha.h"
10#include "algparam.h"
11#include "fips140.h"
12
13#ifndef NDEBUG
14#include "pssr.h"
15#endif
16
17#include "oaep.cpp"
18
19NAMESPACE_BEGIN(CryptoPP)
20
21#ifndef NDEBUG
22void RSA_TestInstantiations()
23{
24RSASS<PKCS1v15, SHA>::Verifier x1(1, 1);
25RSASS<PKCS1v15, SHA>::Signer x2(NullRNG(), 1);
26RSASS<PKCS1v15, SHA>::Verifier x3(x2);
27RSASS<PKCS1v15, SHA>::Verifier x4(x2.GetKey());
28RSASS<PSS, SHA>::Verifier x5(x3);
29#ifndef __MWERKS__
30RSASS<PSSR, SHA>::Signer x6 = x2;
31x3 = x2;
32x6 = x2;
33#endif
34RSAES<PKCS1v15>::Encryptor x7(x2);
35#ifndef __GNUC__
36RSAES<PKCS1v15>::Encryptor x8(x3);
37#endif
38RSAES<OAEP<SHA> >::Encryptor x9(x2);
39
40x4 = x2.GetKey();
41}
42#endif
43
44template class OAEP<SHA>;
45
46OID RSAFunction::GetAlgorithmID() const
47{
48return ASN1::rsaEncryption();
49}
50
51void RSAFunction::BERDecodeKey(BufferedTransformation &bt)
52{
53BERSequenceDecoder seq(bt);
54m_n.BERDecode(seq);
55m_e.BERDecode(seq);
56seq.MessageEnd();
57}
58
59void RSAFunction::DEREncodeKey(BufferedTransformation &bt) const
60{
61DERSequenceEncoder seq(bt);
62m_n.DEREncode(seq);
63m_e.DEREncode(seq);
64seq.MessageEnd();
65}
66
67Integer RSAFunction::ApplyFunction(const Integer &x) const
68{
69DoQuickSanityCheck();
70return a_exp_b_mod_c(x, m_e, m_n);
71}
72
73bool RSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
74{
75bool pass = true;
76pass = pass && m_n > Integer::One() && m_n.IsOdd();
77pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
78return pass;
79}
80
81bool RSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
82{
83return GetValueHelper(this, name, valueType, pValue).Assignable()
84CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
85CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
86;
87}
88
89void RSAFunction::AssignFrom(const NameValuePairs &source)
90{
91AssignFromHelper(this, source)
92CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
93CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
94;
95}
96
97// *****************************************************************************
98
99class RSAPrimeSelector : public PrimeSelector
100{
101public:
102RSAPrimeSelector(const Integer &e) : m_e(e) {}
103bool IsAcceptable(const Integer &candidate) const {return RelativelyPrime(m_e, candidate-Integer::One());}
104Integer m_e;
105};
106
107void InvertibleRSAFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
108{
109int modulusSize = 2048;
110alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
111
112if (modulusSize < 16)
113throw InvalidArgument("InvertibleRSAFunction: specified modulus size is too small");
114
115m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
116
117if (m_e < 3 || m_e.IsEven())
118throw InvalidArgument("InvertibleRSAFunction: invalid public exponent");
119
120RSAPrimeSelector selector(m_e);
121const NameValuePairs &primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
122("PointerToPrimeSelector", selector.GetSelectorPointer());
123m_p.GenerateRandom(rng, primeParam);
124m_q.GenerateRandom(rng, primeParam);
125
126m_d = EuclideanMultiplicativeInverse(m_e, LCM(m_p-1, m_q-1));
127assert(m_d.IsPositive());
128
129m_dp = m_d % (m_p-1);
130m_dq = m_d % (m_q-1);
131m_n = m_p * m_q;
132m_u = m_q.InverseMod(m_p);
133
134if (FIPS_140_2_ComplianceEnabled())
135{
136RSASS<PKCS1v15, SHA>::Signer signer(*this);
137RSASS<PKCS1v15, SHA>::Verifier verifier(signer);
138SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
139
140RSAES<OAEP<SHA> >::Decryptor decryptor(*this);
141RSAES<OAEP<SHA> >::Encryptor encryptor(decryptor);
142EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
143}
144}
145
146void InvertibleRSAFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
147{
148GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e+e.IsEven()));
149}
150
151void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d)
152{
153m_n = n;
154m_e = e;
155m_d = d;
156
157Integer r = --(d*e);
158while (r.IsEven())
159r >>= 1;
160
161ModularArithmetic modn(n);
162for (Integer i = 2; ; ++i)
163{
164Integer a = modn.Exponentiate(i, r);
165if (a == 1)
166continue;
167Integer b;
168while (a != -1)
169{
170b = modn.Square(a);
171if (b == 1)
172{
173m_p = GCD(a-1, n);
174m_q = n/m_p;
175m_dp = m_d % (m_p-1);
176m_dq = m_d % (m_q-1);
177m_u = m_q.InverseMod(m_p);
178return;
179}
180a = b;
181}
182}
183}
184
185void InvertibleRSAFunction::BERDecodeKey(BufferedTransformation &bt)
186{
187BERSequenceDecoder privateKey(bt);
188word32 version;
189BERDecodeUnsigned<word32>(privateKey, version, INTEGER, 0, 0);// check version
190m_n.BERDecode(privateKey);
191m_e.BERDecode(privateKey);
192m_d.BERDecode(privateKey);
193m_p.BERDecode(privateKey);
194m_q.BERDecode(privateKey);
195m_dp.BERDecode(privateKey);
196m_dq.BERDecode(privateKey);
197m_u.BERDecode(privateKey);
198privateKey.MessageEnd();
199}
200
201void InvertibleRSAFunction::DEREncodeKey(BufferedTransformation &bt) const
202{
203DERSequenceEncoder privateKey(bt);
204DEREncodeUnsigned<word32>(privateKey, 0);// version
205m_n.DEREncode(privateKey);
206m_e.DEREncode(privateKey);
207m_d.DEREncode(privateKey);
208m_p.DEREncode(privateKey);
209m_q.DEREncode(privateKey);
210m_dp.DEREncode(privateKey);
211m_dq.DEREncode(privateKey);
212m_u.DEREncode(privateKey);
213privateKey.MessageEnd();
214}
215
216Integer InvertibleRSAFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
217{
218DoQuickSanityCheck();
219ModularArithmetic modn(m_n);
220Integer r(rng, Integer::One(), m_n - Integer::One());
221Integer re = modn.Exponentiate(r, m_e);
222re = modn.Multiply(re, x);// blind
223// here we follow the notation of PKCS #1 and let u=q inverse mod p
224// but in ModRoot, u=p inverse mod q, so we reverse the order of p and q
225Integer y = ModularRoot(re, m_dq, m_dp, m_q, m_p, m_u);
226y = modn.Divide(y, r);// unblind
227if (modn.Exponentiate(y, m_e) != x)// check
228throw Exception(Exception::OTHER_ERROR, "InvertibleRSAFunction: computational error during private key operation");
229return y;
230}
231
232bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
233{
234bool pass = RSAFunction::Validate(rng, level);
235pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
236pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
237pass = pass && m_d > Integer::One() && m_d.IsOdd() && m_d < m_n;
238pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p;
239pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q;
240pass = pass && m_u.IsPositive() && m_u < m_p;
241if (level >= 1)
242{
243pass = pass && m_p * m_q == m_n;
244pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1;
245pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
246pass = pass && m_u * m_q % m_p == 1;
247}
248if (level >= 2)
249pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
250return pass;
251}
252
253bool InvertibleRSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
254{
255return GetValueHelper<RSAFunction>(this, name, valueType, pValue).Assignable()
256CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
257CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
258CRYPTOPP_GET_FUNCTION_ENTRY(PrivateExponent)
259CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
260CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
261CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
262;
263}
264
265void InvertibleRSAFunction::AssignFrom(const NameValuePairs &source)
266{
267AssignFromHelper<RSAFunction>(this, source)
268CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
269CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
270CRYPTOPP_SET_FUNCTION_ENTRY(PrivateExponent)
271CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
272CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
273CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
274;
275}
276
277NAMESPACE_END

Archive Download this file

Branches

Tags

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