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