Update contrib.
2 * Copyright (c) 1998-2009 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.
19 #include "trsaencryptfb.h"
21 #include <asymmetric.h>
24 #include "performancetest.h"
25 #include <securityerr.h>
27 _LIT8(KInputStart, "<input>");
28 _LIT8(KInputEnd, "</input>");
29 _LIT8(KKeyBitsStart, "<keybits>");
30 _LIT8(KKeyBitsEnd, "</keybits>");
32 CRSAEncryptFB::~CRSAEncryptFB()
37 CRSAEncryptFB::CRSAEncryptFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
38 : CTestAction(aConsole, aOut), iFs(aFs), iKeyBits(512) // Default key size = 512
42 CTestAction* CRSAEncryptFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut,
43 const TTestActionSpec& aTestActionSpec)
45 CTestAction* self = CRSAEncryptFB::NewLC(aFs, aConsole,
46 aOut, aTestActionSpec);
51 CTestAction* CRSAEncryptFB::NewLC(RFs& aFs, CConsoleBase& aConsole,Output& aOut,
52 const TTestActionSpec& aTestActionSpec)
54 CRSAEncryptFB* self = new(ELeave) CRSAEncryptFB(aFs, aConsole, aOut);
55 CleanupStack::PushL(self);
56 self->ConstructL(aTestActionSpec);
60 void CRSAEncryptFB::ConstructL(const TTestActionSpec& aTestActionSpec)
62 CTestAction::ConstructL(aTestActionSpec);
63 iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
64 iBody->Des().Copy(aTestActionSpec.iActionBody);
66 if (CPerformance::PerformanceTester()->IsTestingPerformance())
67 {// Number of test iterations
68 TPtrC8 itsPtr = Input::ParseElement(aTestActionSpec.iActionBody, KIterationsStart, KIterationsEnd);
69 if (itsPtr!=KNullDesC8)
73 User::LeaveIfError(lex.Val(iPerfTestIterations));
74 if (iPerfTestIterations > KMaxIterations)
75 User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
77 TPtrC8 cryptoPtr = Input::ParseElement(aTestActionSpec.iActionBody, KTypeOfCryptoStart, KTypeOfCryptoEnd);
78 if (cryptoPtr.CompareF(KRSAStandard) == 0)
80 iCryptoType = EStandard;
82 else if (cryptoPtr.CompareF(KRSAStandardCRT) == 0)
84 iCryptoType = EStandardCRT;
88 User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
92 TPtrC8 keyBitsPtr = Input::ParseElement(*iBody, KKeyBitsStart, KKeyBitsEnd);
93 if (keyBitsPtr!=KNullDesC8)
96 lex.Assign(keyBitsPtr);
97 User::LeaveIfError(lex.Val(iKeyBits));
101 void CRSAEncryptFB::DoPerformPrerequisite(TRequestStatus& aStatus)
103 TRequestStatus* status = &aStatus;
105 TPtrC8 inputTemp = Input::ParseElement(*iBody, KInputStart, KInputEnd);
106 iInput = HBufC8::NewL(inputTemp.Length());
110 User::RequestComplete(status, KErrNone);
111 iActionState = CTestAction::EAction;
114 void CRSAEncryptFB::DoPerformPostrequisite(TRequestStatus& aStatus)
116 TRequestStatus* status = &aStatus;
120 User::RequestComplete(status, iActionResult);
123 void CRSAEncryptFB::DoReportAction(void)
127 void CRSAEncryptFB::DoCheckResult(TInt)
130 iConsole.Printf(_L("."));
132 iConsole.Printf(_L("X"));
135 void CRSAEncryptFB::Hex(HBufC8& aString)
137 TPtr8 ptr=aString.Des();
138 if (aString.Length()%2)
144 for (i=0;i<aString.Length();i+=2)
147 tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
149 tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
152 ptr.SetLength(aString.Length()/2);
156 void CRSAEncryptFB::PerformAction(TRequestStatus& aStatus)
158 TRequestStatus* status = &aStatus;
160 if (CPerformance::PerformanceTester()->IsTestingPerformance())
162 iConsole.Printf(_L(">")); // Indicates start of test
163 TRAP(iActionResult, DoPerformanceTestActionL());
167 TRAP(iActionResult, DoPerformActionL());
170 if (iActionResult==KErrNoMemory)
172 User::Leave(iActionResult); // For OOM testing
174 if (iActionResult==KErrKeyNotWeakEnough)
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"));
181 User::RequestComplete(status, iActionResult);
183 iActionState = CTestAction::EPostrequisite;
186 void CRSAEncryptFB::DoPerformanceTestActionL()
191 TTimeIntervalMicroSeconds keyCreateTime(0);
192 TTimeIntervalMicroSeconds encryptTime(0);
193 TTimeIntervalMicroSeconds decryptTime(0);
195 TTimeIntervalSeconds diff(0);
196 const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
198 // Time key pair creation
199 CRSAKeyPair *rsaPair = NULL;
200 TInt noKeyPairCreations = 0;
202 start.UniversalTime();
203 while (diff < KIterationTime)
205 rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto());
206 CleanupStack::PopAndDestroy();
207 noKeyPairCreations++;
209 end.SecondsFrom(start, diff);
212 keyCreateTime = end.MicroSecondsFrom(start);
213 TReal keycreatetime = I64REAL(keyCreateTime.Int64());
215 rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); // Create one keypair for operations
217 CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(rsaPair->PublicKey());
218 CleanupStack::PushL(encryptor);
220 CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(rsaPair->PrivateKey());
221 CleanupStack::PushL(decryptor);
223 HBufC8 *eResult = HBufC8::NewLC(encryptor->MaxOutputLength());
224 TPtr8 ePtr = eResult->Des();
225 TPtr8* eResultPtr = &ePtr;
230 TInt noEncryptions = 0;
231 start.UniversalTime();
232 while (diff < KIterationTime)
235 encryptor->EncryptL(*iInput, *eResultPtr);
238 end.SecondsFrom(start, diff);
241 encryptTime = end.MicroSecondsFrom(start);
242 TReal encrypttime = I64REAL(encryptTime.Int64());
246 HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
247 ePtr = eResult->Des();
249 TPtr8 dPtr = dResult->Des();
250 TPtr8* dResultPtr = &dPtr;
251 TInt noDecryptions = 0;
252 start.UniversalTime();
253 while (diff < KIterationTime)
255 decryptor->DecryptL(*eResultPtr, *dResultPtr);
258 end.SecondsFrom(start, diff);
261 decryptTime = end.MicroSecondsFrom(start);
262 TReal decrypttime = I64REAL(decryptTime.Int64());
265 if (*iInput != *dResult)
270 CleanupStack::PopAndDestroy(5); // dResult,eResult,decryptor,encryptor,rsaPair
275 TReal rate = I64REAL(keyCreateTime.Int64()) / noKeyPairCreations;
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);
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);
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);
293 _LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
294 iOut.writeString(KNoTimingInfo);
298 void CRSAEncryptFB::DoPerformActionL()
304 CRSAKeyPair* rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto());
305 //LogKeyData(static_cast<const CRSAPublicKey*>(&rsaPair->PublicKey()), static_cast<const CRSAPrivateKeyCRT*>(&rsaPair->PrivateKey()), KNullDesC8);
307 CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(rsaPair->PublicKey());
308 CleanupStack::PushL(encryptor);
310 CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(rsaPair->PrivateKey());
311 CleanupStack::PushL(decryptor);
313 HBufC8* eResult = HBufC8::NewLC(encryptor->MaxOutputLength());
314 HBufC8* dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
316 TPtr8 eResultPtr = eResult->Des();
317 encryptor->EncryptL(*iInput, eResultPtr);
319 TPtr8 dResultPtr = dResult->Des();
320 if (TypeOfCrypto() == EStandardCRT)
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);
326 User::LeaveIfError(res);
330 decryptor->DecryptL(eResultPtr, dResultPtr);
333 if(*iInput == *dResult)
338 CleanupStack::PopAndDestroy(dResult);
339 CleanupStack::PopAndDestroy(eResult);
340 CleanupStack::PopAndDestroy(decryptor);
341 CleanupStack::PopAndDestroy(encryptor);
342 CleanupStack::PopAndDestroy(rsaPair);
347 void CRSAEncryptFB::LogKeyData(const CRSAPublicKey* aPublicKey, const CRSAPrivateKeyCRT* aPrivateKeyCRT, const TDesC8& aCipherText)
349 iOut.writeString(_L("\n\t ----------- e -----------\n\r"));
350 const TInteger& keyE = aPublicKey->E();
351 HBufC8* theE = keyE.BufferLC();
352 iOut.writeOctetString(*theE);
354 iOut.writeString(_L("\n\t ----------- P -----------\n\r"));
355 const TInteger& keyP = aPrivateKeyCRT->P();
356 HBufC8* theP = keyP.BufferLC();
357 iOut.writeOctetString(*theP);
359 iOut.writeString(_L("\n\t ----------- Q -----------\n\r"));
360 const TInteger& keyQ = aPrivateKeyCRT->Q();
361 HBufC8* theQ = keyQ.BufferLC();
362 iOut.writeOctetString(*theQ);
364 iOut.writeString(_L("\n\t ----------- DP -----------\n\r"));
365 const TInteger& keyDP = aPrivateKeyCRT->DP();
366 HBufC8* theDP = keyDP.BufferLC();
367 iOut.writeOctetString(*theDP);
369 iOut.writeString(_L("\n\t ----------- DQ -----------\n\r"));
370 const TInteger& keyDQ = aPrivateKeyCRT->DQ();
371 HBufC8* theDQ = keyDQ.BufferLC();
372 iOut.writeOctetString(*theDQ);
374 iOut.writeString(_L("\n\t ----------- QInv -----------\n\r"));
375 const TInteger& keyQInv = aPrivateKeyCRT->QInv();
376 HBufC8* theQInv = keyQInv.BufferLC();
377 iOut.writeOctetString(*theQInv);
379 iOut.writeString(_L("\n\t ----------- N -----------\n\r"));
380 const TInteger& keyN = aPrivateKeyCRT->N();
381 HBufC8* theN = keyN.BufferLC();
382 iOut.writeOctetString(*theN);
384 iOut.writeString(_L("\n\t ----------- ciphertext -----------\n\r"));
385 iOut.writeOctetString(aCipherText);
387 iOut.writeString(_L("\n\t -------------------------\n\r"));
389 CleanupStack::PopAndDestroy(6);