sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* RSA Keypair (Extended Characteristics) implementation
|
sl@0
|
16 |
* RSA keypair generation implementation
|
sl@0
|
17 |
*
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
|
sl@0
|
20 |
|
sl@0
|
21 |
/**
|
sl@0
|
22 |
@file
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
|
sl@0
|
25 |
#include "rsakeypairgenextendimpl.h"
|
sl@0
|
26 |
#include "pluginconfig.h"
|
sl@0
|
27 |
|
sl@0
|
28 |
#include <ct.h>
|
sl@0
|
29 |
#include <cryptospi/keypair.h>
|
sl@0
|
30 |
#include <cryptospi/cryptospidef.h>
|
sl@0
|
31 |
|
sl@0
|
32 |
#include "../../../source/common/inlines.h" // For TClassSwap
|
sl@0
|
33 |
|
sl@0
|
34 |
//Extended Charcteristics
|
sl@0
|
35 |
|
sl@0
|
36 |
static const TInt32 KExtendCharAttribute1 = 0x102ABCD1;
|
sl@0
|
37 |
static const TUid KExtendCharAttribute1Uid ={KExtendCharAttribute1};
|
sl@0
|
38 |
|
sl@0
|
39 |
static const TInt32 KExtendCharAttribute2 = 0x102ABCD2;
|
sl@0
|
40 |
static const TUid KExtendCharAttribute2Uid ={KExtendCharAttribute2};
|
sl@0
|
41 |
|
sl@0
|
42 |
static const TInt32 KExtendCharAttribute3 = 0x102ABCD3;
|
sl@0
|
43 |
static const TUid KExtendCharAttribute3Uid ={KExtendCharAttribute3};
|
sl@0
|
44 |
|
sl@0
|
45 |
using namespace SoftwareCrypto;
|
sl@0
|
46 |
|
sl@0
|
47 |
/* CRSAKeyPairGenExtendImpl */
|
sl@0
|
48 |
CRSAKeyPairGenExtendImpl::CRSAKeyPairGenExtendImpl(TUid aImplementationUid) : CKeyPairGenImpl(aImplementationUid)
|
sl@0
|
49 |
{
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
CRSAKeyPairGenExtendImpl::~CRSAKeyPairGenExtendImpl()
|
sl@0
|
53 |
{
|
sl@0
|
54 |
delete iExtendChars;
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
CRSAKeyPairGenExtendImpl* CRSAKeyPairGenExtendImpl::NewL(TUid aImplementationUid)
|
sl@0
|
58 |
{
|
sl@0
|
59 |
CRSAKeyPairGenExtendImpl* self = CRSAKeyPairGenExtendImpl::NewLC(aImplementationUid);
|
sl@0
|
60 |
CleanupStack::Pop(self);
|
sl@0
|
61 |
return self;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
CRSAKeyPairGenExtendImpl* CRSAKeyPairGenExtendImpl::NewLC(TUid aImplementationUid)
|
sl@0
|
65 |
{
|
sl@0
|
66 |
CRSAKeyPairGenExtendImpl* self = new(ELeave) CRSAKeyPairGenExtendImpl(aImplementationUid);
|
sl@0
|
67 |
CleanupStack::PushL(self);
|
sl@0
|
68 |
self->ConstructL();
|
sl@0
|
69 |
return self;
|
sl@0
|
70 |
}
|
sl@0
|
71 |
|
sl@0
|
72 |
void CRSAKeyPairGenExtendImpl::ConstructL(void)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
CKeyPairGenImpl::ConstructL();
|
sl@0
|
75 |
iExtendChars = CreateExtendedCharacteristicsL();
|
sl@0
|
76 |
}
|
sl@0
|
77 |
|
sl@0
|
78 |
CExtendedCharacteristics* CRSAKeyPairGenExtendImpl::CreateExtendedCharacteristicsL()
|
sl@0
|
79 |
{
|
sl@0
|
80 |
//***************************************************************
|
sl@0
|
81 |
CExtendedCharacteristics* exChars = CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
|
sl@0
|
82 |
CleanupStack::PushL(exChars);
|
sl@0
|
83 |
|
sl@0
|
84 |
exChars->AddCharacteristicL(9999,KExtendCharAttribute1Uid);
|
sl@0
|
85 |
exChars->AddCharacteristicL(1010,KExtendCharAttribute2Uid);
|
sl@0
|
86 |
exChars->AddCharacteristicL(_L8("SYMBIANTESTCHARACTERISTIC"),KExtendCharAttribute3Uid);
|
sl@0
|
87 |
//**************************************************************
|
sl@0
|
88 |
CleanupStack::Pop(exChars);
|
sl@0
|
89 |
|
sl@0
|
90 |
return exChars;
|
sl@0
|
91 |
}
|
sl@0
|
92 |
|
sl@0
|
93 |
const CExtendedCharacteristics* CRSAKeyPairGenExtendImpl::GetExtendedCharacteristicsL()
|
sl@0
|
94 |
{
|
sl@0
|
95 |
return iExtendChars;
|
sl@0
|
96 |
}
|
sl@0
|
97 |
|
sl@0
|
98 |
TUid CRSAKeyPairGenExtendImpl::ImplementationUid() const
|
sl@0
|
99 |
{
|
sl@0
|
100 |
return iImplementationUid;
|
sl@0
|
101 |
}
|
sl@0
|
102 |
|
sl@0
|
103 |
void CRSAKeyPairGenExtendImpl::Reset()
|
sl@0
|
104 |
{
|
sl@0
|
105 |
// does nothing in this plugin
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
void CRSAKeyPairGenExtendImpl::GenerateKeyPairL(TInt aKeySize, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
/*
|
sl@0
|
111 |
* extract e
|
sl@0
|
112 |
*/
|
sl@0
|
113 |
const TInt aKeyType = aKeyParameters.GetTIntL(KRsaKeyTypeUid);
|
sl@0
|
114 |
const TInt aPublicExponent = aKeyParameters.GetTIntL(KRsaKeyParameterEUid);
|
sl@0
|
115 |
|
sl@0
|
116 |
RInteger e = RInteger::NewL(aPublicExponent);
|
sl@0
|
117 |
CleanupStack::PushL(e);
|
sl@0
|
118 |
|
sl@0
|
119 |
/*
|
sl@0
|
120 |
* calculate p, q, n & d
|
sl@0
|
121 |
*/
|
sl@0
|
122 |
RInteger p;
|
sl@0
|
123 |
RInteger q;
|
sl@0
|
124 |
|
sl@0
|
125 |
//these make sure n is a least aKeySize long
|
sl@0
|
126 |
TInt pbits=(aKeySize+1)/2;
|
sl@0
|
127 |
TInt qbits=aKeySize-pbits;
|
sl@0
|
128 |
|
sl@0
|
129 |
//generate a prime p such that GCD(e,p-1) == 1
|
sl@0
|
130 |
for (;;)
|
sl@0
|
131 |
{
|
sl@0
|
132 |
p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
|
sl@0
|
133 |
CleanupStack::PushL(p);
|
sl@0
|
134 |
--p;
|
sl@0
|
135 |
|
sl@0
|
136 |
RInteger gcd = e.GCDL(p);
|
sl@0
|
137 |
if( gcd == 1 )
|
sl@0
|
138 |
{
|
sl@0
|
139 |
++p;
|
sl@0
|
140 |
gcd.Close();
|
sl@0
|
141 |
//p is still on cleanup stack
|
sl@0
|
142 |
break;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
CleanupStack::PopAndDestroy(&p);
|
sl@0
|
145 |
gcd.Close();
|
sl@0
|
146 |
}
|
sl@0
|
147 |
|
sl@0
|
148 |
//generate a prime q such that GCD(e,q-1) == 1 && (p != q)
|
sl@0
|
149 |
for (;;)
|
sl@0
|
150 |
{
|
sl@0
|
151 |
q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
|
sl@0
|
152 |
CleanupStack::PushL(q);
|
sl@0
|
153 |
--q;
|
sl@0
|
154 |
|
sl@0
|
155 |
RInteger gcd = e.GCDL(q);
|
sl@0
|
156 |
if( gcd == 1 )
|
sl@0
|
157 |
{
|
sl@0
|
158 |
++q;
|
sl@0
|
159 |
if( p != q )
|
sl@0
|
160 |
{
|
sl@0
|
161 |
gcd.Close();
|
sl@0
|
162 |
//q is still on cleanup stack
|
sl@0
|
163 |
break;
|
sl@0
|
164 |
}
|
sl@0
|
165 |
}
|
sl@0
|
166 |
CleanupStack::PopAndDestroy(&q);
|
sl@0
|
167 |
gcd.Close();
|
sl@0
|
168 |
}
|
sl@0
|
169 |
|
sl@0
|
170 |
//make sure p > q
|
sl@0
|
171 |
if ( p < q)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
TClassSwap(p,q);
|
sl@0
|
174 |
}
|
sl@0
|
175 |
|
sl@0
|
176 |
//calculate n = p * q
|
sl@0
|
177 |
RInteger n = p.TimesL(q);
|
sl@0
|
178 |
CleanupStack::PushL(n);
|
sl@0
|
179 |
|
sl@0
|
180 |
--p;
|
sl@0
|
181 |
--q;
|
sl@0
|
182 |
|
sl@0
|
183 |
//temp = (p-1)(q-1)
|
sl@0
|
184 |
RInteger temp = p.TimesL(q);
|
sl@0
|
185 |
CleanupStack::PushL(temp);
|
sl@0
|
186 |
|
sl@0
|
187 |
//e * d = 1 mod ((p-1)(q-1))
|
sl@0
|
188 |
//d = e^(-1) mod ((p-1)(q-1))
|
sl@0
|
189 |
RInteger d = e.InverseModL(temp);
|
sl@0
|
190 |
CleanupStack::PopAndDestroy(&temp); //temp
|
sl@0
|
191 |
CleanupStack::PushL(d);
|
sl@0
|
192 |
|
sl@0
|
193 |
/*
|
sl@0
|
194 |
* create private key depending on aKeyType
|
sl@0
|
195 |
*/
|
sl@0
|
196 |
CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
|
sl@0
|
197 |
privateKeyParameters->AddL(n, KRsaKeyParameterNUid);
|
sl@0
|
198 |
TKeyProperty* privateKeyProperties = NULL;
|
sl@0
|
199 |
TKeyProperty privateKeyProperties_RsaPrivateKeyCRT = {KRSAKeyPairGeneratorUid, iImplementationUid,
|
sl@0
|
200 |
KRsaPrivateKeyCRTUid, KNonEmbeddedKeyUid };
|
sl@0
|
201 |
TKeyProperty privateKeyProperties_RsaPrivateKeyStandard = {KRSAKeyPairGeneratorUid, iImplementationUid,
|
sl@0
|
202 |
KRsaPrivateKeyStandardUid, KNonEmbeddedKeyUid };
|
sl@0
|
203 |
|
sl@0
|
204 |
CCryptoParams*publicKeyParameters = CCryptoParams::NewLC();
|
sl@0
|
205 |
publicKeyParameters->AddL(n, KRsaKeyParameterNUid);
|
sl@0
|
206 |
publicKeyParameters->AddL(e, KRsaKeyParameterEUid);
|
sl@0
|
207 |
TKeyProperty publicKeyProperties = {KRSAKeyPairGeneratorUid, iImplementationUid,
|
sl@0
|
208 |
KRsaPublicKeyUid, KNonEmbeddedKeyUid };
|
sl@0
|
209 |
|
sl@0
|
210 |
if (aKeyType == KRsaPrivateKeyCRT) // cleanup stack contains e, p, q, n, d and privateKeyParameters
|
sl@0
|
211 |
{
|
sl@0
|
212 |
|
sl@0
|
213 |
/*
|
sl@0
|
214 |
* calculate dP, dQ and qInv
|
sl@0
|
215 |
*/
|
sl@0
|
216 |
//calculate dP = d mod (p-1)
|
sl@0
|
217 |
RInteger dP = d.ModuloL(p); //p is still p-1
|
sl@0
|
218 |
CleanupStack::PushL(dP);
|
sl@0
|
219 |
privateKeyParameters->AddL(dP, KRsaKeyParameterDPUid);
|
sl@0
|
220 |
CleanupStack::PopAndDestroy(&dP);
|
sl@0
|
221 |
|
sl@0
|
222 |
//calculate dQ = d mod (q-1)
|
sl@0
|
223 |
RInteger dQ = d.ModuloL(q); //q is still q-1
|
sl@0
|
224 |
CleanupStack::PushL(dQ);
|
sl@0
|
225 |
privateKeyParameters->AddL(dQ, KRsaKeyParameterDQUid);
|
sl@0
|
226 |
CleanupStack::PopAndDestroy(&dQ);
|
sl@0
|
227 |
|
sl@0
|
228 |
++p;
|
sl@0
|
229 |
++q;
|
sl@0
|
230 |
//calculate inverse of qInv = q^(-1)mod(p)
|
sl@0
|
231 |
RInteger qInv = q.InverseModL(p);
|
sl@0
|
232 |
CleanupStack::PushL(qInv);
|
sl@0
|
233 |
privateKeyParameters->AddL(qInv, KRsaKeyParameterQInvUid);
|
sl@0
|
234 |
CleanupStack::PopAndDestroy(&qInv);
|
sl@0
|
235 |
|
sl@0
|
236 |
privateKeyParameters->AddL(p, KRsaKeyParameterPUid);
|
sl@0
|
237 |
privateKeyParameters->AddL(q, KRsaKeyParameterQUid);
|
sl@0
|
238 |
|
sl@0
|
239 |
privateKeyProperties = &privateKeyProperties_RsaPrivateKeyCRT;
|
sl@0
|
240 |
}
|
sl@0
|
241 |
else if (aKeyType == KRsaPrivateKeyStandard)
|
sl@0
|
242 |
{
|
sl@0
|
243 |
privateKeyParameters->AddL(d, KRsaKeyParameterDUid);
|
sl@0
|
244 |
privateKeyProperties = &privateKeyProperties_RsaPrivateKeyStandard;
|
sl@0
|
245 |
}
|
sl@0
|
246 |
else
|
sl@0
|
247 |
{
|
sl@0
|
248 |
User::Leave(KErrNotSupported);
|
sl@0
|
249 |
}
|
sl@0
|
250 |
// cleanup stack contains e, p, q, n, d and privateKeyParameters
|
sl@0
|
251 |
CKey* privateKey = CKey::NewL(*privateKeyProperties, *privateKeyParameters);
|
sl@0
|
252 |
CleanupStack::PushL(privateKey);
|
sl@0
|
253 |
|
sl@0
|
254 |
/*
|
sl@0
|
255 |
* create public key
|
sl@0
|
256 |
*/
|
sl@0
|
257 |
CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
|
sl@0
|
258 |
CleanupStack::PushL(publicKey);
|
sl@0
|
259 |
|
sl@0
|
260 |
/*
|
sl@0
|
261 |
* create the key pair
|
sl@0
|
262 |
*/
|
sl@0
|
263 |
aKeyPair = CKeyPair::NewL(publicKey, privateKey);
|
sl@0
|
264 |
|
sl@0
|
265 |
CleanupStack::Pop(2, privateKey); //privateKey and publicKey
|
sl@0
|
266 |
CleanupStack::PopAndDestroy(7, &e); //e, p, q, n, d, privateKeyParameters and publicKeyParameters
|
sl@0
|
267 |
}
|