|
1 /* |
|
2 * Copyright (c) 1998-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 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "trsaencryptfb.h" |
|
20 #include "t_input.h" |
|
21 #include <asymmetric.h> |
|
22 #include <padding.h> |
|
23 #include <bigint.h> |
|
24 #include "performancetest.h" |
|
25 #include <securityerr.h> |
|
26 |
|
27 _LIT8(KInputStart, "<input>"); |
|
28 _LIT8(KInputEnd, "</input>"); |
|
29 _LIT8(KKeyBitsStart, "<keybits>"); |
|
30 _LIT8(KKeyBitsEnd, "</keybits>"); |
|
31 |
|
32 CRSAEncryptFB::~CRSAEncryptFB() |
|
33 { |
|
34 delete iBody; |
|
35 } |
|
36 |
|
37 CRSAEncryptFB::CRSAEncryptFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut) |
|
38 : CTestAction(aConsole, aOut), iFs(aFs), iKeyBits(512) // Default key size = 512 |
|
39 { |
|
40 } |
|
41 |
|
42 CTestAction* CRSAEncryptFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, |
|
43 const TTestActionSpec& aTestActionSpec) |
|
44 { |
|
45 CTestAction* self = CRSAEncryptFB::NewLC(aFs, aConsole, |
|
46 aOut, aTestActionSpec); |
|
47 CleanupStack::Pop(); |
|
48 return self; |
|
49 } |
|
50 |
|
51 CTestAction* CRSAEncryptFB::NewLC(RFs& aFs, CConsoleBase& aConsole,Output& aOut, |
|
52 const TTestActionSpec& aTestActionSpec) |
|
53 { |
|
54 CRSAEncryptFB* self = new(ELeave) CRSAEncryptFB(aFs, aConsole, aOut); |
|
55 CleanupStack::PushL(self); |
|
56 self->ConstructL(aTestActionSpec); |
|
57 return self; |
|
58 } |
|
59 |
|
60 void CRSAEncryptFB::ConstructL(const TTestActionSpec& aTestActionSpec) |
|
61 { |
|
62 CTestAction::ConstructL(aTestActionSpec); |
|
63 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length()); |
|
64 iBody->Des().Copy(aTestActionSpec.iActionBody); |
|
65 |
|
66 if (CPerformance::PerformanceTester()->IsTestingPerformance()) |
|
67 {// Number of test iterations |
|
68 TPtrC8 itsPtr = Input::ParseElement(aTestActionSpec.iActionBody, KIterationsStart, KIterationsEnd); |
|
69 if (itsPtr!=KNullDesC8) |
|
70 { |
|
71 TLex8 lex; |
|
72 lex.Assign(itsPtr); |
|
73 User::LeaveIfError(lex.Val(iPerfTestIterations)); |
|
74 if (iPerfTestIterations > KMaxIterations) |
|
75 User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument); |
|
76 } |
|
77 TPtrC8 cryptoPtr = Input::ParseElement(aTestActionSpec.iActionBody, KTypeOfCryptoStart, KTypeOfCryptoEnd); |
|
78 if (cryptoPtr.CompareF(KRSAStandard) == 0) |
|
79 { |
|
80 iCryptoType = EStandard; |
|
81 } |
|
82 else if (cryptoPtr.CompareF(KRSAStandardCRT) == 0) |
|
83 { |
|
84 iCryptoType = EStandardCRT; |
|
85 } |
|
86 else |
|
87 { |
|
88 User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument); |
|
89 } |
|
90 } |
|
91 |
|
92 TPtrC8 keyBitsPtr = Input::ParseElement(*iBody, KKeyBitsStart, KKeyBitsEnd); |
|
93 if (keyBitsPtr!=KNullDesC8) |
|
94 { |
|
95 TLex8 lex; |
|
96 lex.Assign(keyBitsPtr); |
|
97 User::LeaveIfError(lex.Val(iKeyBits)); |
|
98 } |
|
99 } |
|
100 |
|
101 void CRSAEncryptFB::DoPerformPrerequisite(TRequestStatus& aStatus) |
|
102 { |
|
103 TRequestStatus* status = &aStatus; |
|
104 |
|
105 TPtrC8 inputTemp = Input::ParseElement(*iBody, KInputStart, KInputEnd); |
|
106 iInput = HBufC8::NewL(inputTemp.Length()); |
|
107 *iInput = inputTemp; |
|
108 Hex(*iInput); |
|
109 |
|
110 User::RequestComplete(status, KErrNone); |
|
111 iActionState = CTestAction::EAction; |
|
112 } |
|
113 |
|
114 void CRSAEncryptFB::DoPerformPostrequisite(TRequestStatus& aStatus) |
|
115 { |
|
116 TRequestStatus* status = &aStatus; |
|
117 delete iInput; |
|
118 |
|
119 iFinished = ETrue; |
|
120 User::RequestComplete(status, iActionResult); |
|
121 } |
|
122 |
|
123 void CRSAEncryptFB::DoReportAction(void) |
|
124 { |
|
125 } |
|
126 |
|
127 void CRSAEncryptFB::DoCheckResult(TInt) |
|
128 { |
|
129 if (iResult) |
|
130 iConsole.Printf(_L(".")); |
|
131 else |
|
132 iConsole.Printf(_L("X")); |
|
133 } |
|
134 |
|
135 void CRSAEncryptFB::Hex(HBufC8& aString) |
|
136 { |
|
137 TPtr8 ptr=aString.Des(); |
|
138 if (aString.Length()%2) |
|
139 { |
|
140 ptr.SetLength(0); |
|
141 return; |
|
142 } |
|
143 TInt i; |
|
144 for (i=0;i<aString.Length();i+=2) |
|
145 { |
|
146 TUint8 tmp; |
|
147 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0')); |
|
148 tmp*=16; |
|
149 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0')); |
|
150 ptr[i/2]=tmp; |
|
151 } |
|
152 ptr.SetLength(aString.Length()/2); |
|
153 } |
|
154 |
|
155 |
|
156 void CRSAEncryptFB::PerformAction(TRequestStatus& aStatus) |
|
157 { |
|
158 TRequestStatus* status = &aStatus; |
|
159 |
|
160 if (CPerformance::PerformanceTester()->IsTestingPerformance()) |
|
161 { |
|
162 iConsole.Printf(_L(">")); // Indicates start of test |
|
163 TRAP(iActionResult, DoPerformanceTestActionL()); |
|
164 } |
|
165 else |
|
166 { |
|
167 TRAP(iActionResult, DoPerformActionL()); |
|
168 } |
|
169 |
|
170 if (iActionResult==KErrNoMemory) |
|
171 { |
|
172 User::Leave(iActionResult); // For OOM testing |
|
173 } |
|
174 if (iActionResult==KErrKeyNotWeakEnough) |
|
175 { |
|
176 iResult = ETrue; |
|
177 iConsole.Printf(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\n\r")); |
|
178 iOut.writeString(_L("Crypto libraries returned KErrKeyNotWeakEnough! Passing test automatically.\r\n")); |
|
179 } |
|
180 |
|
181 User::RequestComplete(status, iActionResult); |
|
182 |
|
183 iActionState = CTestAction::EPostrequisite; |
|
184 } |
|
185 |
|
186 void CRSAEncryptFB::DoPerformanceTestActionL() |
|
187 { |
|
188 __UHEAP_MARK; |
|
189 iResult = EFalse; |
|
190 |
|
191 TTimeIntervalMicroSeconds keyCreateTime(0); |
|
192 TTimeIntervalMicroSeconds encryptTime(0); |
|
193 TTimeIntervalMicroSeconds decryptTime(0); |
|
194 TTime start, end; |
|
195 TTimeIntervalSeconds diff(0); |
|
196 const TTimeIntervalSeconds KIterationTime(iPerfTestIterations); |
|
197 |
|
198 // Time key pair creation |
|
199 CRSAKeyPair *rsaPair = NULL; |
|
200 TInt noKeyPairCreations = 0; |
|
201 |
|
202 start.UniversalTime(); |
|
203 while (diff < KIterationTime) |
|
204 { |
|
205 rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); |
|
206 CleanupStack::PopAndDestroy(); |
|
207 noKeyPairCreations++; |
|
208 end.UniversalTime(); |
|
209 end.SecondsFrom(start, diff); |
|
210 } |
|
211 end.UniversalTime(); |
|
212 keyCreateTime = end.MicroSecondsFrom(start); |
|
213 TReal keycreatetime = I64REAL(keyCreateTime.Int64()); |
|
214 |
|
215 rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); // Create one keypair for operations |
|
216 |
|
217 CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(rsaPair->PublicKey()); |
|
218 CleanupStack::PushL(encryptor); |
|
219 |
|
220 CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(rsaPair->PrivateKey()); |
|
221 CleanupStack::PushL(decryptor); |
|
222 |
|
223 HBufC8 *eResult = HBufC8::NewLC(encryptor->MaxOutputLength()); |
|
224 TPtr8 ePtr = eResult->Des(); |
|
225 TPtr8* eResultPtr = &ePtr; |
|
226 |
|
227 |
|
228 // Time encryption |
|
229 diff = 0; |
|
230 TInt noEncryptions = 0; |
|
231 start.UniversalTime(); |
|
232 while (diff < KIterationTime) |
|
233 { |
|
234 eResultPtr->Zero(); |
|
235 encryptor->EncryptL(*iInput, *eResultPtr); |
|
236 noEncryptions++; |
|
237 end.UniversalTime(); |
|
238 end.SecondsFrom(start, diff); |
|
239 } |
|
240 end.UniversalTime(); |
|
241 encryptTime = end.MicroSecondsFrom(start); |
|
242 TReal encrypttime = I64REAL(encryptTime.Int64()); |
|
243 |
|
244 // Time decryption |
|
245 diff = 0; |
|
246 HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength()); |
|
247 ePtr = eResult->Des(); |
|
248 eResultPtr = &ePtr; |
|
249 TPtr8 dPtr = dResult->Des(); |
|
250 TPtr8* dResultPtr = &dPtr; |
|
251 TInt noDecryptions = 0; |
|
252 start.UniversalTime(); |
|
253 while (diff < KIterationTime) |
|
254 { |
|
255 decryptor->DecryptL(*eResultPtr, *dResultPtr); |
|
256 noDecryptions++; |
|
257 end.UniversalTime(); |
|
258 end.SecondsFrom(start, diff); |
|
259 } |
|
260 end.UniversalTime(); |
|
261 decryptTime = end.MicroSecondsFrom(start); |
|
262 TReal decrypttime = I64REAL(decryptTime.Int64()); |
|
263 |
|
264 iResult = ETrue; |
|
265 if (*iInput != *dResult) |
|
266 { |
|
267 iResult = EFalse; |
|
268 } |
|
269 |
|
270 CleanupStack::PopAndDestroy(5); // dResult,eResult,decryptor,encryptor,rsaPair |
|
271 __UHEAP_MARKEND; |
|
272 |
|
273 if (iResult) |
|
274 { |
|
275 TReal rate = I64REAL(keyCreateTime.Int64()) / noKeyPairCreations; |
|
276 TBuf<256> buf; |
|
277 _LIT(KKeyCreateTime, "\n\tKeyCreate: %f us/key pair (%i key pairs in %f us)\r\n"); |
|
278 buf.Format(KKeyCreateTime, rate, noKeyPairCreations, keycreatetime); |
|
279 iOut.writeString(buf); |
|
280 |
|
281 rate = I64REAL(encryptTime.Int64()) / noEncryptions; |
|
282 _LIT(KEncryptTime, "\tEncrypt: %f us/encryption (%i encryptions in %f us)\r\n"); |
|
283 buf.Format(KEncryptTime, rate, noEncryptions, encrypttime); |
|
284 iOut.writeString(buf); |
|
285 |
|
286 rate = I64REAL(decryptTime.Int64()) / noDecryptions; |
|
287 _LIT(KDecryptTime, "\tDecrypt: %f us/decryption (%i decryptions in %f us)\r\n"); |
|
288 buf.Format(KDecryptTime, rate, noDecryptions, decrypttime); |
|
289 iOut.writeString(buf); |
|
290 } |
|
291 else |
|
292 { |
|
293 _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n"); |
|
294 iOut.writeString(KNoTimingInfo); |
|
295 } |
|
296 } |
|
297 |
|
298 void CRSAEncryptFB::DoPerformActionL() |
|
299 { |
|
300 __UHEAP_MARK; |
|
301 |
|
302 iResult = EFalse; |
|
303 |
|
304 CRSAKeyPair* rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); |
|
305 //LogKeyData(static_cast<const CRSAPublicKey*>(&rsaPair->PublicKey()), static_cast<const CRSAPrivateKeyCRT*>(&rsaPair->PrivateKey()), KNullDesC8); |
|
306 |
|
307 CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(rsaPair->PublicKey()); |
|
308 CleanupStack::PushL(encryptor); |
|
309 |
|
310 CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(rsaPair->PrivateKey()); |
|
311 CleanupStack::PushL(decryptor); |
|
312 |
|
313 HBufC8* eResult = HBufC8::NewLC(encryptor->MaxOutputLength()); |
|
314 HBufC8* dResult = HBufC8::NewLC(decryptor->MaxOutputLength()); |
|
315 |
|
316 TPtr8 eResultPtr = eResult->Des(); |
|
317 encryptor->EncryptL(*iInput, eResultPtr); |
|
318 |
|
319 TPtr8 dResultPtr = dResult->Des(); |
|
320 if (TypeOfCrypto() == EStandardCRT) |
|
321 { |
|
322 TRAPD(res, decryptor->DecryptL(eResultPtr, dResultPtr)); |
|
323 if (res==KErrCorrupt) // Log all the key data for this failure |
|
324 LogKeyData(static_cast<const CRSAPublicKey*>(&rsaPair->PublicKey()), static_cast<const CRSAPrivateKeyCRT*>(&rsaPair->PrivateKey()), *eResult); |
|
325 |
|
326 User::LeaveIfError(res); |
|
327 } |
|
328 else |
|
329 { |
|
330 decryptor->DecryptL(eResultPtr, dResultPtr); |
|
331 } |
|
332 |
|
333 if(*iInput == *dResult) |
|
334 { |
|
335 iResult = ETrue; |
|
336 } |
|
337 |
|
338 CleanupStack::PopAndDestroy(dResult); |
|
339 CleanupStack::PopAndDestroy(eResult); |
|
340 CleanupStack::PopAndDestroy(decryptor); |
|
341 CleanupStack::PopAndDestroy(encryptor); |
|
342 CleanupStack::PopAndDestroy(rsaPair); |
|
343 |
|
344 __UHEAP_MARKEND; |
|
345 } |
|
346 |
|
347 void CRSAEncryptFB::LogKeyData(const CRSAPublicKey* aPublicKey, const CRSAPrivateKeyCRT* aPrivateKeyCRT, const TDesC8& aCipherText) |
|
348 { |
|
349 iOut.writeString(_L("\n\t ----------- e -----------\n\r")); |
|
350 const TInteger& keyE = aPublicKey->E(); |
|
351 HBufC8* theE = keyE.BufferLC(); |
|
352 iOut.writeOctetString(*theE); |
|
353 |
|
354 iOut.writeString(_L("\n\t ----------- P -----------\n\r")); |
|
355 const TInteger& keyP = aPrivateKeyCRT->P(); |
|
356 HBufC8* theP = keyP.BufferLC(); |
|
357 iOut.writeOctetString(*theP); |
|
358 |
|
359 iOut.writeString(_L("\n\t ----------- Q -----------\n\r")); |
|
360 const TInteger& keyQ = aPrivateKeyCRT->Q(); |
|
361 HBufC8* theQ = keyQ.BufferLC(); |
|
362 iOut.writeOctetString(*theQ); |
|
363 |
|
364 iOut.writeString(_L("\n\t ----------- DP -----------\n\r")); |
|
365 const TInteger& keyDP = aPrivateKeyCRT->DP(); |
|
366 HBufC8* theDP = keyDP.BufferLC(); |
|
367 iOut.writeOctetString(*theDP); |
|
368 |
|
369 iOut.writeString(_L("\n\t ----------- DQ -----------\n\r")); |
|
370 const TInteger& keyDQ = aPrivateKeyCRT->DQ(); |
|
371 HBufC8* theDQ = keyDQ.BufferLC(); |
|
372 iOut.writeOctetString(*theDQ); |
|
373 |
|
374 iOut.writeString(_L("\n\t ----------- QInv -----------\n\r")); |
|
375 const TInteger& keyQInv = aPrivateKeyCRT->QInv(); |
|
376 HBufC8* theQInv = keyQInv.BufferLC(); |
|
377 iOut.writeOctetString(*theQInv); |
|
378 |
|
379 iOut.writeString(_L("\n\t ----------- N -----------\n\r")); |
|
380 const TInteger& keyN = aPrivateKeyCRT->N(); |
|
381 HBufC8* theN = keyN.BufferLC(); |
|
382 iOut.writeOctetString(*theN); |
|
383 |
|
384 iOut.writeString(_L("\n\t ----------- ciphertext -----------\n\r")); |
|
385 iOut.writeOctetString(aCipherText); |
|
386 |
|
387 iOut.writeString(_L("\n\t -------------------------\n\r")); |
|
388 |
|
389 CleanupStack::PopAndDestroy(6); |
|
390 |
|
391 } |