Update contrib.
2 * Copyright (c) 2006-2010 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/keypair.h>
30 #include <cryptospi/cryptospidef.h>
32 #include "../../../source/common/inlines.h" // For TClassSwap
34 using namespace SoftwareCrypto;
36 /* CRSAKeyPairGenImpl */
37 CRSAKeyPairGenImpl::CRSAKeyPairGenImpl(TUid aImplementationUid) : CKeyPairGenImpl(aImplementationUid)
41 CRSAKeyPairGenImpl::~CRSAKeyPairGenImpl()
45 CRSAKeyPairGenImpl* CRSAKeyPairGenImpl::NewL(TUid aImplementationUid)
47 CRSAKeyPairGenImpl* self = CRSAKeyPairGenImpl::NewLC(aImplementationUid);
48 CleanupStack::Pop(self);
52 CRSAKeyPairGenImpl* CRSAKeyPairGenImpl::NewLC(TUid aImplementationUid)
54 CRSAKeyPairGenImpl* self = new(ELeave) CRSAKeyPairGenImpl(aImplementationUid);
55 CleanupStack::PushL(self);
60 void CRSAKeyPairGenImpl::ConstructL(void)
62 CKeyPairGenImpl::ConstructL();
65 CExtendedCharacteristics* CRSAKeyPairGenImpl::CreateExtendedCharacteristicsL()
67 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
68 // for exclusive use and are not CERTIFIED to be standards compliant.
69 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
72 const CExtendedCharacteristics* CRSAKeyPairGenImpl::GetExtendedCharacteristicsL()
74 return CRSAKeyPairGenImpl::CreateExtendedCharacteristicsL();
77 TUid CRSAKeyPairGenImpl::ImplementationUid() const
79 return iImplementationUid;
82 void CRSAKeyPairGenImpl::Reset()
84 // does nothing in this plugin
87 void CRSAKeyPairGenImpl::GenerateKeyPairL(TInt aKeySize, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
92 const TInt aKeyType = aKeyParameters.GetTIntL(KRsaKeyTypeUid);
93 const TInt aPublicExponent = aKeyParameters.GetTIntL(KRsaKeyParameterEUid);
95 RInteger e = RInteger::NewL(aPublicExponent);
96 CleanupStack::PushL(e);
99 * calculate p, q, n & d
104 //these make sure n is a least aKeySize long
105 TInt pbits=(aKeySize+1)/2;
106 TInt qbits=aKeySize-pbits;
108 //generate a prime p such that GCD(e,p-1) == 1
111 p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
112 CleanupStack::PushL(p);
115 RInteger gcd = e.GCDL(p);
120 //p is still on cleanup stack
123 CleanupStack::PopAndDestroy(&p);
127 //generate a prime q such that GCD(e,q-1) == 1 && (p != q)
130 q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
131 CleanupStack::PushL(q);
134 RInteger gcd = e.GCDL(q);
141 //q is still on cleanup stack
145 CleanupStack::PopAndDestroy(&q);
155 //calculate n = p * q
156 RInteger n = p.TimesL(q);
157 CleanupStack::PushL(n);
163 RInteger temp = p.TimesL(q);
164 CleanupStack::PushL(temp);
166 //e * d = 1 mod ((p-1)(q-1))
167 //d = e^(-1) mod ((p-1)(q-1))
168 RInteger d = e.InverseModL(temp);
169 CleanupStack::PopAndDestroy(&temp); //temp
170 CleanupStack::PushL(d);
173 * create private key depending on aKeyType
175 CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
176 privateKeyParameters->AddL(n, KRsaKeyParameterNUid);
177 TKeyProperty* privateKeyProperties = NULL;
178 TKeyProperty privateKeyProperties_RsaPrivateKeyCRT = {KRSAKeyPairGeneratorUid, iImplementationUid,
179 KRsaPrivateKeyCRTUid, KNonEmbeddedKeyUid };
180 TKeyProperty privateKeyProperties_RsaPrivateKeyStandard = {KRSAKeyPairGeneratorUid, iImplementationUid,
181 KRsaPrivateKeyStandardUid, KNonEmbeddedKeyUid };
183 CCryptoParams*publicKeyParameters = CCryptoParams::NewLC();
184 publicKeyParameters->AddL(n, KRsaKeyParameterNUid);
185 publicKeyParameters->AddL(e, KRsaKeyParameterEUid);
186 TKeyProperty publicKeyProperties = {KRSAKeyPairGeneratorUid, iImplementationUid,
187 KRsaPublicKeyUid, KNonEmbeddedKeyUid };
189 if (aKeyType == KRsaPrivateKeyCRT) // cleanup stack contains e, p, q, n, d and privateKeyParameters
193 * calculate dP, dQ and qInv
195 //calculate dP = d mod (p-1)
196 RInteger dP = d.ModuloL(p); //p is still p-1
197 CleanupStack::PushL(dP);
198 privateKeyParameters->AddL(dP, KRsaKeyParameterDPUid);
199 CleanupStack::PopAndDestroy(&dP);
201 //calculate dQ = d mod (q-1)
202 RInteger dQ = d.ModuloL(q); //q is still q-1
203 CleanupStack::PushL(dQ);
204 privateKeyParameters->AddL(dQ, KRsaKeyParameterDQUid);
205 CleanupStack::PopAndDestroy(&dQ);
209 //calculate inverse of qInv = q^(-1)mod(p)
210 RInteger qInv = q.InverseModL(p);
211 CleanupStack::PushL(qInv);
212 privateKeyParameters->AddL(qInv, KRsaKeyParameterQInvUid);
213 CleanupStack::PopAndDestroy(&qInv);
215 privateKeyParameters->AddL(p, KRsaKeyParameterPUid);
216 privateKeyParameters->AddL(q, KRsaKeyParameterQUid);
218 privateKeyProperties = &privateKeyProperties_RsaPrivateKeyCRT;
220 else if (aKeyType == KRsaPrivateKeyStandard)
222 privateKeyParameters->AddL(d, KRsaKeyParameterDUid);
223 privateKeyProperties = &privateKeyProperties_RsaPrivateKeyStandard;
227 User::Leave(KErrNotSupported);
229 // cleanup stack contains e, p, q, n, d and privateKeyParameters
230 CKey* privateKey = CKey::NewL(*privateKeyProperties, *privateKeyParameters);
231 CleanupStack::PushL(privateKey);
236 CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
237 CleanupStack::PushL(publicKey);
240 * create the key pair
242 aKeyPair = CKeyPair::NewL(publicKey, privateKey);
244 CleanupStack::Pop(2, privateKey); //privateKey and publicKey
245 CleanupStack::PopAndDestroy(7, &e); //e, p, q, n, d, privateKeyParameters and publicKeyParameters