os/security/crypto/weakcryptospi/test/tplugins/src/tplugin01/rsakeypairgenextendimpl.cpp
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 (Extended Characteristics) implementation
16 * RSA keypair generation implementation
25 #include "rsakeypairgenextendimpl.h"
26 #include "pluginconfig.h"
29 #include <cryptospi/keypair.h>
30 #include <cryptospi/cryptospidef.h>
32 #include "../../../source/common/inlines.h" // For TClassSwap
34 //Extended Charcteristics
36 static const TInt32 KExtendCharAttribute1 = 0x102ABCD1;
37 static const TUid KExtendCharAttribute1Uid ={KExtendCharAttribute1};
39 static const TInt32 KExtendCharAttribute2 = 0x102ABCD2;
40 static const TUid KExtendCharAttribute2Uid ={KExtendCharAttribute2};
42 static const TInt32 KExtendCharAttribute3 = 0x102ABCD3;
43 static const TUid KExtendCharAttribute3Uid ={KExtendCharAttribute3};
45 using namespace SoftwareCrypto;
47 /* CRSAKeyPairGenExtendImpl */
48 CRSAKeyPairGenExtendImpl::CRSAKeyPairGenExtendImpl(TUid aImplementationUid) : CKeyPairGenImpl(aImplementationUid)
52 CRSAKeyPairGenExtendImpl::~CRSAKeyPairGenExtendImpl()
57 CRSAKeyPairGenExtendImpl* CRSAKeyPairGenExtendImpl::NewL(TUid aImplementationUid)
59 CRSAKeyPairGenExtendImpl* self = CRSAKeyPairGenExtendImpl::NewLC(aImplementationUid);
60 CleanupStack::Pop(self);
64 CRSAKeyPairGenExtendImpl* CRSAKeyPairGenExtendImpl::NewLC(TUid aImplementationUid)
66 CRSAKeyPairGenExtendImpl* self = new(ELeave) CRSAKeyPairGenExtendImpl(aImplementationUid);
67 CleanupStack::PushL(self);
72 void CRSAKeyPairGenExtendImpl::ConstructL(void)
74 CKeyPairGenImpl::ConstructL();
75 iExtendChars = CreateExtendedCharacteristicsL();
78 CExtendedCharacteristics* CRSAKeyPairGenExtendImpl::CreateExtendedCharacteristicsL()
80 //***************************************************************
81 CExtendedCharacteristics* exChars = CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
82 CleanupStack::PushL(exChars);
84 exChars->AddCharacteristicL(9999,KExtendCharAttribute1Uid);
85 exChars->AddCharacteristicL(1010,KExtendCharAttribute2Uid);
86 exChars->AddCharacteristicL(_L8("SYMBIANTESTCHARACTERISTIC"),KExtendCharAttribute3Uid);
87 //**************************************************************
88 CleanupStack::Pop(exChars);
93 const CExtendedCharacteristics* CRSAKeyPairGenExtendImpl::GetExtendedCharacteristicsL()
98 TUid CRSAKeyPairGenExtendImpl::ImplementationUid() const
100 return iImplementationUid;
103 void CRSAKeyPairGenExtendImpl::Reset()
105 // does nothing in this plugin
108 void CRSAKeyPairGenExtendImpl::GenerateKeyPairL(TInt aKeySize, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
113 const TInt aKeyType = aKeyParameters.GetTIntL(KRsaKeyTypeUid);
114 const TInt aPublicExponent = aKeyParameters.GetTIntL(KRsaKeyParameterEUid);
116 RInteger e = RInteger::NewL(aPublicExponent);
117 CleanupStack::PushL(e);
120 * calculate p, q, n & d
125 //these make sure n is a least aKeySize long
126 TInt pbits=(aKeySize+1)/2;
127 TInt qbits=aKeySize-pbits;
129 //generate a prime p such that GCD(e,p-1) == 1
132 p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
133 CleanupStack::PushL(p);
136 RInteger gcd = e.GCDL(p);
141 //p is still on cleanup stack
144 CleanupStack::PopAndDestroy(&p);
148 //generate a prime q such that GCD(e,q-1) == 1 && (p != q)
151 q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
152 CleanupStack::PushL(q);
155 RInteger gcd = e.GCDL(q);
162 //q is still on cleanup stack
166 CleanupStack::PopAndDestroy(&q);
176 //calculate n = p * q
177 RInteger n = p.TimesL(q);
178 CleanupStack::PushL(n);
184 RInteger temp = p.TimesL(q);
185 CleanupStack::PushL(temp);
187 //e * d = 1 mod ((p-1)(q-1))
188 //d = e^(-1) mod ((p-1)(q-1))
189 RInteger d = e.InverseModL(temp);
190 CleanupStack::PopAndDestroy(&temp); //temp
191 CleanupStack::PushL(d);
194 * create private key depending on aKeyType
196 CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
197 privateKeyParameters->AddL(n, KRsaKeyParameterNUid);
198 TKeyProperty* privateKeyProperties = NULL;
199 TKeyProperty privateKeyProperties_RsaPrivateKeyCRT = {KRSAKeyPairGeneratorUid, iImplementationUid,
200 KRsaPrivateKeyCRTUid, KNonEmbeddedKeyUid };
201 TKeyProperty privateKeyProperties_RsaPrivateKeyStandard = {KRSAKeyPairGeneratorUid, iImplementationUid,
202 KRsaPrivateKeyStandardUid, KNonEmbeddedKeyUid };
204 CCryptoParams*publicKeyParameters = CCryptoParams::NewLC();
205 publicKeyParameters->AddL(n, KRsaKeyParameterNUid);
206 publicKeyParameters->AddL(e, KRsaKeyParameterEUid);
207 TKeyProperty publicKeyProperties = {KRSAKeyPairGeneratorUid, iImplementationUid,
208 KRsaPublicKeyUid, KNonEmbeddedKeyUid };
210 if (aKeyType == KRsaPrivateKeyCRT) // cleanup stack contains e, p, q, n, d and privateKeyParameters
214 * calculate dP, dQ and qInv
216 //calculate dP = d mod (p-1)
217 RInteger dP = d.ModuloL(p); //p is still p-1
218 CleanupStack::PushL(dP);
219 privateKeyParameters->AddL(dP, KRsaKeyParameterDPUid);
220 CleanupStack::PopAndDestroy(&dP);
222 //calculate dQ = d mod (q-1)
223 RInteger dQ = d.ModuloL(q); //q is still q-1
224 CleanupStack::PushL(dQ);
225 privateKeyParameters->AddL(dQ, KRsaKeyParameterDQUid);
226 CleanupStack::PopAndDestroy(&dQ);
230 //calculate inverse of qInv = q^(-1)mod(p)
231 RInteger qInv = q.InverseModL(p);
232 CleanupStack::PushL(qInv);
233 privateKeyParameters->AddL(qInv, KRsaKeyParameterQInvUid);
234 CleanupStack::PopAndDestroy(&qInv);
236 privateKeyParameters->AddL(p, KRsaKeyParameterPUid);
237 privateKeyParameters->AddL(q, KRsaKeyParameterQUid);
239 privateKeyProperties = &privateKeyProperties_RsaPrivateKeyCRT;
241 else if (aKeyType == KRsaPrivateKeyStandard)
243 privateKeyParameters->AddL(d, KRsaKeyParameterDUid);
244 privateKeyProperties = &privateKeyProperties_RsaPrivateKeyStandard;
248 User::Leave(KErrNotSupported);
250 // cleanup stack contains e, p, q, n, d and privateKeyParameters
251 CKey* privateKey = CKey::NewL(*privateKeyProperties, *privateKeyParameters);
252 CleanupStack::PushL(privateKey);
257 CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
258 CleanupStack::PushL(publicKey);
261 * create the key pair
263 aKeyPair = CKeyPair::NewL(publicKey, privateKey);
265 CleanupStack::Pop(2, privateKey); //privateKey and publicKey
266 CleanupStack::PopAndDestroy(7, &e); //e, p, q, n, d, privateKeyParameters and publicKeyParameters