os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsakeypairgenimpl.cpp
Update contrib.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * RSA Keypair implementation
16 * RSA keypair generation implementation
25 #include "rsakeypairgenimpl.h"
26 #include "pluginconfig.h"
29 #include <cryptospi/cryptospidef.h>
31 #include "common/inlines.h" // For TClassSwap
33 using namespace SoftwareCrypto;
35 /* CRSAKeyPairGenImpl */
36 CRSAKeyPairGenImpl::CRSAKeyPairGenImpl()
40 CRSAKeyPairGenImpl::~CRSAKeyPairGenImpl()
44 CRSAKeyPairGenImpl* CRSAKeyPairGenImpl::NewL(void)
46 CRSAKeyPairGenImpl* self = CRSAKeyPairGenImpl::NewLC();
47 CleanupStack::Pop(self);
51 CRSAKeyPairGenImpl* CRSAKeyPairGenImpl::NewLC(void)
53 CRSAKeyPairGenImpl* self = new(ELeave) CRSAKeyPairGenImpl();
54 CleanupStack::PushL(self);
59 void CRSAKeyPairGenImpl::ConstructL(void)
61 CKeyPairGenImpl::ConstructL();
64 CExtendedCharacteristics* CRSAKeyPairGenImpl::CreateExtendedCharacteristicsL()
66 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
67 // for exclusive use and are not CERTIFIED to be standards compliant.
68 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
71 const CExtendedCharacteristics* CRSAKeyPairGenImpl::GetExtendedCharacteristicsL()
73 return CRSAKeyPairGenImpl::CreateExtendedCharacteristicsL();
76 TUid CRSAKeyPairGenImpl::ImplementationUid() const
78 return KCryptoPluginRsaKeyPairGenUid;
81 void CRSAKeyPairGenImpl::Reset()
83 // does nothing in this plugin
86 void CRSAKeyPairGenImpl::GenerateKeyPairL(TInt aKeySize, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
91 const TInt aKeyType = aKeyParameters.GetTIntL(KRsaKeyTypeUid);
92 const TInt aPublicExponent = aKeyParameters.GetTIntL(KRsaKeyParameterEUid);
94 RInteger e = RInteger::NewL(aPublicExponent);
95 CleanupStack::PushL(e);
98 * calculate p, q, n & d
103 //these make sure n is a least aKeySize long
104 TInt pbits=(aKeySize+1)/2;
105 TInt qbits=aKeySize-pbits;
107 //generate a prime p such that GCD(e,p-1) == 1
110 p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
111 CleanupStack::PushL(p);
114 RInteger gcd = e.GCDL(p);
119 //p is still on cleanup stack
122 CleanupStack::PopAndDestroy(&p);
126 //generate a prime q such that GCD(e,q-1) == 1 && (p != q)
129 q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
130 CleanupStack::PushL(q);
133 RInteger gcd = e.GCDL(q);
140 //q is still on cleanup stack
144 CleanupStack::PopAndDestroy(&q);
154 //calculate n = p * q
155 RInteger n = p.TimesL(q);
156 CleanupStack::PushL(n);
162 RInteger temp = p.TimesL(q);
163 CleanupStack::PushL(temp);
165 //e * d = 1 mod ((p-1)(q-1))
166 //d = e^(-1) mod ((p-1)(q-1))
167 RInteger d = e.InverseModL(temp);
168 CleanupStack::PopAndDestroy(&temp); //temp
169 CleanupStack::PushL(d);
172 * create private key depending on aKeyType
174 CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
175 privateKeyParameters->AddL(n, KRsaKeyParameterNUid);
176 TKeyProperty* privateKeyProperties = NULL;
177 TKeyProperty privateKeyProperties_RsaPrivateKeyCRT = {KRSAKeyPairGeneratorUid, KCryptoPluginRsaKeyPairGenUid,
178 KRsaPrivateKeyCRTUid, KNonEmbeddedKeyUid };
179 TKeyProperty privateKeyProperties_RsaPrivateKeyStandard = {KRSAKeyPairGeneratorUid, KCryptoPluginRsaKeyPairGenUid,
180 KRsaPrivateKeyStandardUid, KNonEmbeddedKeyUid };
182 CCryptoParams*publicKeyParameters = CCryptoParams::NewLC();
183 publicKeyParameters->AddL(n, KRsaKeyParameterNUid);
184 publicKeyParameters->AddL(e, KRsaKeyParameterEUid);
185 TKeyProperty publicKeyProperties = {KRSAKeyPairGeneratorUid, KCryptoPluginRsaKeyPairGenUid,
186 KRsaPublicKeyUid, KNonEmbeddedKeyUid };
188 if (aKeyType == KRsaPrivateKeyCRT) // cleanup stack contains e, p, q, n, d and privateKeyParameters
192 * calculate dP, dQ and qInv
194 //calculate dP = d mod (p-1)
195 RInteger dP = d.ModuloL(p); //p is still p-1
196 CleanupStack::PushL(dP);
197 privateKeyParameters->AddL(dP, KRsaKeyParameterDPUid);
198 CleanupStack::PopAndDestroy(&dP);
200 //calculate dQ = d mod (q-1)
201 RInteger dQ = d.ModuloL(q); //q is still q-1
202 CleanupStack::PushL(dQ);
203 privateKeyParameters->AddL(dQ, KRsaKeyParameterDQUid);
204 CleanupStack::PopAndDestroy(&dQ);
208 //calculate inverse of qInv = q^(-1)mod(p)
209 RInteger qInv = q.InverseModL(p);
210 CleanupStack::PushL(qInv);
211 privateKeyParameters->AddL(qInv, KRsaKeyParameterQInvUid);
212 CleanupStack::PopAndDestroy(&qInv);
214 privateKeyParameters->AddL(p, KRsaKeyParameterPUid);
215 privateKeyParameters->AddL(q, KRsaKeyParameterQUid);
217 privateKeyProperties = &privateKeyProperties_RsaPrivateKeyCRT;
219 else if (aKeyType == KRsaPrivateKeyStandard)
221 privateKeyParameters->AddL(d, KRsaKeyParameterDUid);
222 privateKeyProperties = &privateKeyProperties_RsaPrivateKeyStandard;
226 User::Leave(KErrNotSupported);
228 // cleanup stack contains e, p, q, n, d and privateKeyParameters
229 CKey* privateKey = CKey::NewL(*privateKeyProperties, *privateKeyParameters);
230 CleanupStack::PushL(privateKey);
235 CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
236 CleanupStack::PushL(publicKey);
239 * create the key pair
241 aKeyPair = CKeyPair::NewL(publicKey, privateKey);
243 CleanupStack::Pop(2, privateKey); //privateKey and publicKey
244 CleanupStack::PopAndDestroy(7, &e); //e, p, q, n, d, privateKeyParameters and publicKeyParameters