os/security/crypto/weakcrypto/test/tasymmetric/trsavector.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 "trsavector.h"
    20 #include "tvectorutils.h"
    21 #include "t_input.h"
    22 #include <bigint.h>
    23 #include "performancetest.h"
    24 #include "tbrokenrandom.h"
    25 _LIT8(KPlaintextStart, "<plaintext>");
    26 _LIT8(KPlaintextEnd, "</plaintext>");
    27 _LIT8(KCiphertextStart, "<ciphertext>");
    28 _LIT8(KCiphertextEnd, "</ciphertext>");
    29 _LIT8(KSignatureStart, "<signature>");
    30 _LIT8(KSignatureEnd, "</signature>");
    31 _LIT8(KDigestInfoStart, "<digestInfo>");
    32 _LIT8(KDigestInfoEnd, "</digestInfo>");
    33 
    34 ////////////////////////////////////////////////////////////////////////////////
    35 // CRSAEncryptVector
    36 ////////////////////////////////////////////////////////////////////////////////
    37 
    38 CTestAction* CRSAEncryptVector::NewL(RFs& aFs,
    39                                CConsoleBase& aConsole,
    40                                Output& aOut, 
    41                                const TTestActionSpec& aTestActionSpec)
    42 	{
    43 	CTestAction* self = CRSAEncryptVector::NewLC(aFs, aConsole,
    44 		aOut, aTestActionSpec);
    45 	CleanupStack::Pop();
    46 	return self;
    47 	}
    48 
    49 CTestAction* CRSAEncryptVector::NewLC(RFs& aFs,
    50                                 CConsoleBase& aConsole,
    51                                 Output& aOut, 
    52                                 const TTestActionSpec& aTestActionSpec)
    53 	{
    54 	CRSAEncryptVector* self = new(ELeave) CRSAEncryptVector(aFs, aConsole, aOut);
    55 	CleanupStack::PushL(self);
    56 	self->ConstructL(aTestActionSpec);
    57 	return self;
    58 	}
    59 
    60 CRSAEncryptVector::~CRSAEncryptVector()
    61 	{
    62 	delete iPubKey;
    63 	delete iPlaintext;
    64 	delete iCiphertext;
    65 	}
    66 
    67 CRSAEncryptVector::CRSAEncryptVector(RFs& /*aFs*/, 
    68                                      CConsoleBase& aConsole,
    69                                      Output& aOut)					 
    70 : CVectorTest(aConsole, aOut)
    71 	{
    72 	}
    73 
    74 void CRSAEncryptVector::ConstructL(const TTestActionSpec& aTestActionSpec)
    75 	{
    76 	CVectorTest::ConstructL(aTestActionSpec);
    77 
    78     iPubKey = VectorUtils::ReadRSAPublicKeyL(aTestActionSpec.iActionBody);
    79 
    80 	TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd);
    81 	iPlaintext = VectorUtils::ParseBinaryL(ptextIn);
    82 
    83 	TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd);
    84 	iCiphertext = VectorUtils::ParseBinaryL(ctextIn);
    85 	}
    86 
    87 void CRSAEncryptVector::DoPerformanceTestActionL()
    88 {
    89 	__UHEAP_MARK;
    90 
    91 	CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(*iPubKey);
    92 	CleanupStack::PushL(encryptor);
    93  
    94 	TTimeIntervalMicroSeconds encryptTime(0);
    95 	TTime start, end;
    96 	TTimeIntervalSeconds diff(0);
    97 	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
    98 
    99 	TInt noEncryptions = 0;
   100 	HBufC8 *eResult = HBufC8::NewLC(encryptor->MaxOutputLength());
   101 	TPtr8 ePtr = eResult->Des();
   102 	TPtr8* eResultPtr = &ePtr;
   103 	
   104 //	Time encryption	
   105 	CRandomIncrementing* brokenRandom = new(ELeave)CRandomIncrementing(1);
   106 	SetThreadRandomLC(brokenRandom);
   107 	start.UniversalTime();
   108 	while (diff < KIterationTime)
   109 		{
   110 		eResultPtr->Zero();
   111 		encryptor->EncryptL(*iPlaintext, *eResultPtr);
   112 		noEncryptions++;
   113 		end.UniversalTime();
   114 		end.SecondsFrom(start, diff);
   115 		}
   116 	end.UniversalTime();
   117 	encryptTime = end.MicroSecondsFrom(start);
   118 	TReal encrypttime = I64REAL(encryptTime.Int64());
   119 	CleanupStack::PopAndDestroy(1); //SetThreadRandomLC
   120 	
   121 	iResult = ETrue;
   122 	if (*iCiphertext!=*eResult)
   123 	{
   124 		iResult = EFalse;		
   125 	}
   126 
   127     CleanupStack::PopAndDestroy(2);		// encryptor, eResult
   128 
   129 	__UHEAP_MARKEND;
   130 
   131 	if (iResult)
   132 	{
   133 		TReal rate = I64REAL(encryptTime.Int64()) / noEncryptions;
   134 		TBuf<256> buf;
   135 		_LIT(KEncryptTime, "\tEncrypt Time: %f us/encryption (%i encryptions in %f us)\r\n");
   136  		buf.Format(KEncryptTime, rate, noEncryptions, encrypttime);
   137 		iOut.writeString(buf);
   138 	}
   139 	else
   140 	{
   141 		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   142 		iOut.writeString(KNoTimingInfo);
   143 	}
   144 }
   145 
   146 void CRSAEncryptVector::DoPerformActionL()
   147 	{
   148 	__UHEAP_MARK;
   149 
   150 	CRSAPKCS1v15Encryptor* encryptor = CRSAPKCS1v15Encryptor::NewL(*iPubKey);
   151 	CleanupStack::PushL(encryptor);
   152     HBufC8* encryptBuf = HBufC8::NewLC(encryptor->MaxOutputLength());
   153 	TPtr8 encryptPtr = encryptBuf->Des();
   154 	CRandomIncrementing* brokenRandom = new(ELeave)CRandomIncrementing(1);
   155 	SetThreadRandomLC(brokenRandom);
   156 	encryptor->EncryptL(*iPlaintext, encryptPtr);
   157 	CleanupStack::PopAndDestroy(1); //threadrandom;
   158     iResult = (*iCiphertext == *encryptBuf);
   159 
   160 	CleanupStack::PopAndDestroy(encryptBuf);
   161     CleanupStack::PopAndDestroy(encryptor);
   162 
   163 	__UHEAP_MARKEND;
   164 	}
   165 
   166 ////////////////////////////////////////////////////////////////////////////////
   167 // CRSADecryptVector
   168 ////////////////////////////////////////////////////////////////////////////////
   169 
   170 CTestAction* CRSADecryptVector::NewL(RFs& aFs,
   171                                CConsoleBase& aConsole,
   172                                Output& aOut, 
   173                                const TTestActionSpec& aTestActionSpec)
   174 	{
   175 	CTestAction* self = CRSADecryptVector::NewLC(aFs, aConsole,
   176 		aOut, aTestActionSpec);
   177 	CleanupStack::Pop();
   178 	return self;
   179 	}
   180 
   181 CTestAction* CRSADecryptVector::NewLC(RFs& aFs,
   182                                 CConsoleBase& aConsole,
   183                                 Output& aOut, 
   184                                 const TTestActionSpec& aTestActionSpec)
   185 	{
   186 	CRSADecryptVector* self = new(ELeave) CRSADecryptVector(aFs, aConsole, aOut);
   187 	CleanupStack::PushL(self);
   188 	self->ConstructL(aTestActionSpec);
   189 	return self;
   190 	}
   191 
   192 CRSADecryptVector::~CRSADecryptVector()
   193 	{
   194 	delete iPrivKey;
   195 	delete iPlaintext;
   196 	delete iCiphertext;
   197 	}
   198 
   199 CRSADecryptVector::CRSADecryptVector(RFs& /*aFs*/, 
   200                                      CConsoleBase& aConsole,
   201                                      Output& aOut)					 
   202 : CVectorTest(aConsole, aOut)
   203 	{
   204 	}
   205 
   206 void CRSADecryptVector::ConstructL(const TTestActionSpec& aTestActionSpec)
   207 	{
   208 	CVectorTest::ConstructL(aTestActionSpec);
   209 
   210     iPrivKey = VectorUtils::ReadRSAPrivateKeyL(aTestActionSpec.iActionBody);
   211 
   212 	TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd);
   213 	iCiphertext = VectorUtils::ParseBinaryL(ctextIn);
   214 
   215 	TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd);
   216 	iPlaintext = VectorUtils::ParseBinaryL(ptextIn);
   217 	}
   218 
   219 void CRSADecryptVector::DoPerformanceTestActionL()
   220 {
   221 	__UHEAP_MARK;
   222 
   223     CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
   224 	CleanupStack::PushL(decryptor);
   225 
   226 	TTimeIntervalMicroSeconds decryptTime(0);
   227 	TTime start, end;
   228 	TTimeIntervalSeconds diff(0);
   229 	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
   230 
   231 	HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
   232 	TPtr8 dPtr = dResult->Des();
   233 	TPtr8* dResultPtr = &dPtr;
   234 	TInt noDecryptions = 0;
   235 
   236 //	Time decryption	
   237 	start.UniversalTime();
   238 	while (diff < KIterationTime)
   239 		{
   240 		decryptor->DecryptL(*iCiphertext, *dResultPtr);
   241 		noDecryptions++;
   242 		end.UniversalTime();
   243 		end.SecondsFrom(start, diff);
   244 		}
   245 	end.UniversalTime();
   246 	decryptTime = end.MicroSecondsFrom(start);
   247 	TReal decrypttime = I64REAL(decryptTime.Int64());
   248 	iResult = ETrue;
   249 	if (*iPlaintext!=*dResult)
   250 	{
   251 		iResult = EFalse;		
   252 	}
   253 
   254     CleanupStack::PopAndDestroy(2);		// decryptor, dResult
   255 
   256 	__UHEAP_MARKEND;
   257 	
   258 	if (iResult)
   259 	{
   260 		TReal rate = I64REAL(decryptTime.Int64()) / noDecryptions;
   261 		TBuf<256> buf;
   262 		_LIT(KDecryptTime, "\tDecrypt Time: %f us/decryption (%i decryptions in %f us)\r\n");
   263   		buf.Format(KDecryptTime, rate, noDecryptions, decrypttime);
   264 		iOut.writeString(buf);
   265 	}
   266 	else
   267 	{
   268 		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   269 		iOut.writeString(KNoTimingInfo);
   270 	}
   271 }
   272 
   273 void CRSADecryptVector::DoPerformActionL()
   274 	{
   275 	__UHEAP_MARK;
   276 
   277     CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
   278 	CleanupStack::PushL(decryptor);
   279 
   280 	HBufC8* decryptBuf = HBufC8::NewLC(decryptor->MaxOutputLength());
   281 	TPtr8 decryptPtr = decryptBuf->Des();
   282 	TRAPD(err, decryptor->DecryptL(*iCiphertext, decryptPtr));
   283     iResult = (err == KErrNone) && (*iPlaintext == *decryptBuf);
   284 
   285  	CleanupStack::PopAndDestroy(decryptBuf);
   286  	CleanupStack::PopAndDestroy(decryptor);
   287 
   288 	__UHEAP_MARKEND;
   289 	}
   290 
   291 
   292 ////////////////////////////////////////////////////////////////////////////////
   293 // CRSADecryptVectorCRT
   294 ////////////////////////////////////////////////////////////////////////////////
   295 CTestAction* CRSADecryptVectorCRT::NewL(RFs& aFs,
   296                                CConsoleBase& aConsole,
   297                                Output& aOut, 
   298                                const TTestActionSpec& aTestActionSpec)
   299 	{
   300 	CTestAction* self = CRSADecryptVectorCRT::NewLC(aFs, aConsole,
   301 		aOut, aTestActionSpec);
   302 	CleanupStack::Pop();
   303 	return self;
   304 	}
   305 
   306 CTestAction* CRSADecryptVectorCRT::NewLC(RFs& aFs,
   307                                 CConsoleBase& aConsole,
   308                                 Output& aOut, 
   309                                 const TTestActionSpec& aTestActionSpec)
   310 	{
   311 	CRSADecryptVectorCRT* self = new(ELeave) CRSADecryptVectorCRT(aFs, aConsole, aOut);
   312 	CleanupStack::PushL(self);
   313 	self->ConstructL(aTestActionSpec);
   314 	return self;
   315 	}
   316 
   317 CRSADecryptVectorCRT::~CRSADecryptVectorCRT()
   318 	{
   319 	delete iPrivKey;
   320 	delete iPlaintext;
   321 	delete iCiphertext;
   322 	}
   323 
   324 CRSADecryptVectorCRT::CRSADecryptVectorCRT(RFs& /*aFs*/, 
   325                                      CConsoleBase& aConsole,
   326                                      Output& aOut)					 
   327 : CVectorTest(aConsole, aOut)
   328 	{
   329 	}
   330 
   331 void CRSADecryptVectorCRT::ConstructL(const TTestActionSpec& aTestActionSpec)
   332 	{
   333 	CVectorTest::ConstructL(aTestActionSpec);
   334 
   335     iPrivKey = VectorUtils::ReadRSAPrivateKeyCRTL(aTestActionSpec.iActionBody);
   336 
   337 	TPtrC8 ctextIn = Input::ParseElement(aTestActionSpec.iActionBody, KCiphertextStart, KCiphertextEnd);
   338 	iCiphertext = VectorUtils::ParseBinaryL(ctextIn);
   339 
   340 	TPtrC8 ptextIn = Input::ParseElement(aTestActionSpec.iActionBody, KPlaintextStart, KPlaintextEnd);
   341 	iPlaintext = VectorUtils::ParseBinaryL(ptextIn);
   342 	}
   343 
   344 void CRSADecryptVectorCRT::DoPerformanceTestActionL()
   345 {
   346 	__UHEAP_MARK;
   347 
   348     CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
   349 	CleanupStack::PushL(decryptor);
   350 
   351 	TTimeIntervalMicroSeconds decryptTime(0);
   352 	TTime start, end;
   353 	TTimeIntervalSeconds diff(0);
   354 	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
   355 	
   356 	TInt noDecryptions = 0;
   357 	HBufC8 *dResult = HBufC8::NewLC(decryptor->MaxOutputLength());
   358 	TPtr8 dPtr = dResult->Des();
   359 	TPtr8* dResultPtr = &dPtr;
   360 	
   361 //	Time decryption	
   362 	start.UniversalTime();
   363 	while (diff < KIterationTime)
   364 		{
   365 		decryptor->DecryptL(*iCiphertext, *dResultPtr);
   366 		noDecryptions++;
   367 		end.UniversalTime();
   368 		end.SecondsFrom(start, diff);
   369 		}
   370 	end.UniversalTime();
   371 	decryptTime = end.MicroSecondsFrom(start);
   372 	TReal decrypttime = I64REAL(decryptTime.Int64());
   373 		
   374 	iResult = ETrue;
   375 	if (*iPlaintext!=*dResult)
   376 	{
   377 		iResult = EFalse;		
   378 	}
   379 
   380 
   381     CleanupStack::PopAndDestroy(2);		// decryptor, dResult
   382 
   383 	__UHEAP_MARKEND;
   384 	
   385 	if (iResult)
   386 	{
   387 		TReal rate = I64REAL(decryptTime.Int64()) / noDecryptions;
   388 		TBuf<256> buf;
   389 		_LIT(KDecryptTime, "\tDecrypt Time: %f us/decryption (%i decryptions in %f us)\r\n");
   390   		buf.Format(KDecryptTime, rate, noDecryptions, decrypttime);
   391 		iOut.writeString(buf);
   392 	}
   393 	else
   394 	{
   395 		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   396 		iOut.writeString(KNoTimingInfo);
   397 	}
   398 }
   399 
   400 void CRSADecryptVectorCRT::DoPerformActionL()
   401 	{
   402 	__UHEAP_MARK;
   403 
   404     CRSAPKCS1v15Decryptor* decryptor = CRSAPKCS1v15Decryptor::NewL(*iPrivKey);
   405 	CleanupStack::PushL(decryptor);
   406 
   407 	HBufC8* decryptBuf = HBufC8::NewLC(decryptor->MaxOutputLength());
   408 	TPtr8 decryptPtr = decryptBuf->Des();
   409 	TRAPD(err, decryptor->DecryptL(*iCiphertext, decryptPtr));
   410     iResult = (err == KErrNone) && (*iPlaintext == *decryptBuf);
   411 
   412  	CleanupStack::PopAndDestroy(decryptBuf);
   413  	CleanupStack::PopAndDestroy(decryptor);
   414 
   415 	__UHEAP_MARKEND;
   416 	}
   417 
   418 ////////////////////////////////////////////////////////////////////////////////
   419 // CRSASignVector
   420 ////////////////////////////////////////////////////////////////////////////////
   421 
   422 CTestAction* CRSASignVector::NewL(RFs& aFs,
   423                                   CConsoleBase& aConsole,
   424                                   Output& aOut, 
   425                                   const TTestActionSpec& aTestActionSpec)
   426 	{
   427 	CTestAction* self = CRSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   428 	CleanupStack::Pop();
   429 	return self;
   430 	}
   431 
   432 CTestAction* CRSASignVector::NewLC(RFs& aFs,
   433                                    CConsoleBase& aConsole,
   434                                    Output& aOut, 
   435                                    const TTestActionSpec& aTestActionSpec)
   436 	{
   437 	CRSASignVector* self = new(ELeave) CRSASignVector(aFs, aConsole, aOut);
   438 	CleanupStack::PushL(self);
   439 	self->ConstructL(aTestActionSpec);
   440 	return self;
   441 	}
   442 
   443 CRSASignVector::~CRSASignVector()
   444 	{
   445     delete iPrivKey;
   446 	delete iDigestInfo;
   447 	delete iSignature;
   448 	}
   449 
   450 CRSASignVector::CRSASignVector(RFs& /*aFs*/, 
   451                                CConsoleBase& aConsole,
   452                                Output& aOut)					 
   453     : CVectorTest(aConsole, aOut)
   454 	{
   455 	}
   456 
   457 void CRSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
   458 	{
   459 	CVectorTest::ConstructL(aTestActionSpec);
   460 
   461     iPrivKey = VectorUtils::ReadRSAPrivateKeyL(aTestActionSpec.iActionBody);
   462 
   463 	TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd);
   464 	iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn);
   465 
   466 	TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd);
   467 	RInteger integer = VectorUtils::ParseIntegerL(signatureIn);
   468 	CleanupStack::PushL(integer);
   469 	iSignature = CRSASignature::NewL(integer);
   470 	CleanupStack::Pop(&integer);
   471 	}
   472 
   473 void CRSASignVector::DoPerformanceTestActionL()
   474 {
   475 	__UHEAP_MARK;
   476 
   477     CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey);
   478 	CleanupStack::PushL(signer);
   479 
   480 	TPtrC8 digestPtr = iDigestInfo->Des();
   481 
   482 	TTimeIntervalMicroSeconds signTime(0);
   483 	TTime start, end;
   484 	TTimeIntervalSeconds diff(0);
   485 	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
   486 
   487 	TInt noSignings = 0;
   488 
   489 	const CRSASignature *testSig = 0;
   490 	
   491 //	Time signing
   492 	start.UniversalTime();
   493 	while (diff < KIterationTime)
   494 		{
   495 		testSig = signer->SignL(digestPtr);
   496 		delete testSig;
   497 		noSignings++;
   498 		end.UniversalTime();
   499 		end.SecondsFrom(start, diff);
   500 		}
   501 	end.UniversalTime();
   502 	signTime = end.MicroSecondsFrom(start);
   503 	TReal signtime = I64REAL(signTime.Int64());
   504 	
   505 	iResult = ETrue;
   506 	testSig = signer->SignL(digestPtr);
   507 	if (!(*testSig==*iSignature))
   508 	{
   509 		iResult = EFalse;		
   510 	}
   511 	delete testSig;
   512 
   513 	CleanupStack::PopAndDestroy();		//	signer
   514 
   515 	__UHEAP_MARKEND;
   516 	
   517 	if (iResult)
   518 	{
   519 		TReal rate = I64REAL(signTime.Int64()) / noSignings;
   520 		TBuf<256> buf;
   521 		_LIT(KSignTime, "\tSign Time: %f us/signing (%i signings in %f us)\r\n");
   522 		buf.Format(KSignTime, rate, noSignings, signtime);
   523 		iOut.writeString(buf);
   524 	}
   525 	else
   526 	{
   527 		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   528 		iOut.writeString(KNoTimingInfo);
   529 	}
   530 }
   531 
   532 void CRSASignVector::DoPerformActionL()
   533 	{
   534 	__UHEAP_MARK;
   535 
   536 	CRSAPKCS1v15Signer* digestSigner = CRSAPKCS1v15Signer::NewL(*iPrivKey);
   537 	CleanupStack::PushL(digestSigner);
   538 	
   539 	TPtrC8 digestPtr2 = iDigestInfo->Des();
   540 	const CRSASignature* testSig2 = digestSigner->SignL(digestPtr2);
   541     CleanupStack::PushL(const_cast<CRSASignature*>(testSig2));
   542     iResult = (*testSig2 == *iSignature);
   543 
   544 	CleanupStack::PopAndDestroy(const_cast<CRSASignature*>(testSig2));
   545 	CleanupStack::PopAndDestroy(digestSigner);
   546 
   547 	__UHEAP_MARKEND;
   548 	}
   549 
   550 ////////////////////////////////////////////////////////////////////////////////
   551 // CRSASignVectorCRT
   552 ////////////////////////////////////////////////////////////////////////////////
   553 
   554 CTestAction* CRSASignVectorCRT::NewL(RFs& aFs,
   555                                   CConsoleBase& aConsole,
   556                                   Output& aOut, 
   557                                   const TTestActionSpec& aTestActionSpec)
   558 	{
   559 	CTestAction* self = CRSASignVectorCRT::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   560 	CleanupStack::Pop();
   561 	return self;
   562 	}
   563 
   564 CTestAction* CRSASignVectorCRT::NewLC(RFs& aFs,
   565                                    CConsoleBase& aConsole,
   566                                    Output& aOut, 
   567                                    const TTestActionSpec& aTestActionSpec)
   568 	{
   569 	CRSASignVectorCRT* self = new(ELeave) CRSASignVectorCRT(aFs, aConsole, aOut);
   570 	CleanupStack::PushL(self);
   571 	self->ConstructL(aTestActionSpec);
   572 	return self;
   573 	}
   574 
   575 CRSASignVectorCRT::~CRSASignVectorCRT()
   576 	{
   577     delete iPrivKey;
   578 	delete iDigestInfo;
   579 	delete iSignature;
   580 	}
   581 
   582 CRSASignVectorCRT::CRSASignVectorCRT(RFs& /*aFs*/, 
   583                                CConsoleBase& aConsole,
   584                                Output& aOut)					 
   585     : CVectorTest(aConsole, aOut)
   586 	{
   587 	}
   588 
   589 void CRSASignVectorCRT::ConstructL(const TTestActionSpec& aTestActionSpec)
   590 	{
   591 	CVectorTest::ConstructL(aTestActionSpec);
   592 
   593     iPrivKey = VectorUtils::ReadRSAPrivateKeyCRTL(aTestActionSpec.iActionBody);
   594 
   595 	TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd);
   596 	iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn);
   597 
   598 	TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd);
   599 	RInteger integer = VectorUtils::ParseIntegerL(signatureIn);
   600 	CleanupStack::PushL(integer);
   601 	iSignature = CRSASignature::NewL(integer);
   602 	CleanupStack::Pop(&integer);
   603 	}
   604 
   605 void CRSASignVectorCRT::DoPerformanceTestActionL()
   606 {
   607 	__UHEAP_MARK;
   608 
   609     CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey);
   610 	CleanupStack::PushL(signer);
   611 
   612 	TPtrC8 digestPtr = iDigestInfo->Des();
   613 
   614 	TTimeIntervalMicroSeconds signTime(0);
   615 	TTime start, end;
   616 	TTimeIntervalSeconds diff(0);
   617 	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
   618 	TInt noSignings = 0;
   619 
   620 	const CRSASignature *testSig = NULL;
   621 	
   622 //	Time signing
   623 	start.UniversalTime();
   624 	while (diff < KIterationTime)
   625 		{
   626 		testSig = signer->SignL(digestPtr);
   627 		delete testSig;
   628 		noSignings++;
   629 		end.UniversalTime();
   630 		end.SecondsFrom(start, diff);
   631 		}
   632 	end.UniversalTime();
   633 	signTime = end.MicroSecondsFrom(start);
   634 	TReal signtime = I64REAL(signTime.Int64());
   635 	
   636 	testSig = signer->SignL(digestPtr);
   637 	iResult = ETrue;
   638 	if (!(*testSig==*iSignature))
   639 	{
   640 		iResult = EFalse;		
   641 	}
   642 	delete testSig;
   643 
   644 	CleanupStack::PopAndDestroy(); // signer
   645 
   646 	__UHEAP_MARKEND;
   647 	
   648 	if (iResult)
   649 	{
   650 		TReal rate = I64REAL(signTime.Int64()) / noSignings;
   651 		TBuf<256> buf;
   652 		_LIT(KSignTime, "\tSign Time: %f us/signing (%i signings in %f us)\r\n");
   653 		buf.Format(KSignTime, rate, noSignings, signtime);
   654 		iOut.writeString(buf);
   655 	}
   656 	else
   657 	{
   658 		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   659 		iOut.writeString(KNoTimingInfo);
   660 	}
   661 }
   662 
   663 void CRSASignVectorCRT::DoPerformActionL()
   664 	{
   665 	__UHEAP_MARK;
   666 
   667 	CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(*iPrivKey);
   668 	CleanupStack::PushL(signer);
   669 
   670 	TPtrC8 digestPtr = iDigestInfo->Des();
   671 	const CRSASignature* testSig = signer->SignL(digestPtr);
   672     CleanupStack::PushL(const_cast<CRSASignature*>(testSig));	
   673 
   674 #ifdef _DEBUG 
   675 	HBufC8* theResultSig = testSig->S().BufferLC();
   676 	HBufC8* theExpectedSig = iSignature->S().BufferLC();
   677 	iResult = (theResultSig->Compare(*theExpectedSig)) ==0 ? ETrue : EFalse;
   678 	CleanupStack::PopAndDestroy(2);	
   679 #endif	
   680 	
   681 	iResult = (*testSig == *iSignature);
   682 
   683 	CleanupStack::PopAndDestroy(const_cast<CRSASignature*>(testSig));
   684     CleanupStack::PopAndDestroy(signer);
   685 
   686 	__UHEAP_MARKEND;
   687 	}
   688 
   689 
   690 ////////////////////////////////////////////////////////////////////////////////
   691 // CRSAVerifyVector
   692 ////////////////////////////////////////////////////////////////////////////////
   693 
   694 CTestAction* CRSAVerifyVector::NewL(RFs& aFs,
   695                                   CConsoleBase& aConsole,
   696                                   Output& aOut, 
   697                                   const TTestActionSpec& aTestActionSpec)
   698 	{
   699 	CTestAction* self = CRSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   700 	CleanupStack::Pop();
   701 	return self;
   702 	}
   703 
   704 CTestAction* CRSAVerifyVector::NewLC(RFs& aFs,
   705                                    CConsoleBase& aConsole,
   706                                    Output& aOut, 
   707                                    const TTestActionSpec& aTestActionSpec)
   708 	{
   709 	CRSAVerifyVector* self = new(ELeave) CRSAVerifyVector(aFs, aConsole, aOut);
   710 	CleanupStack::PushL(self);
   711 	self->ConstructL(aTestActionSpec);
   712 	return self;
   713 	}
   714 
   715 CRSAVerifyVector::~CRSAVerifyVector()
   716 	{
   717     delete iPubKey;
   718 	delete iDigestInfo;
   719 	delete iSignature;
   720 	}
   721 
   722 CRSAVerifyVector::CRSAVerifyVector(RFs& /*aFs*/, 
   723                                    CConsoleBase& aConsole,
   724                                    Output& aOut)					 
   725     : CVectorTest(aConsole, aOut)
   726 	{
   727 	}
   728 
   729 void CRSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
   730 	{
   731 	CVectorTest::ConstructL(aTestActionSpec);
   732 
   733     iPubKey = VectorUtils::ReadRSAPublicKeyL(aTestActionSpec.iActionBody);
   734 
   735 	TPtrC8 digestInfoIn = Input::ParseElement(aTestActionSpec.iActionBody, KDigestInfoStart, KDigestInfoEnd);
   736 	iDigestInfo = VectorUtils::ParseBinaryL(digestInfoIn);
   737 
   738 	TPtrC8 signatureIn = Input::ParseElement(aTestActionSpec.iActionBody, KSignatureStart, KSignatureEnd);
   739 	RInteger integer = VectorUtils::ParseIntegerL(signatureIn);
   740 	CleanupStack::PushL(integer);
   741 	iSignature = CRSASignature::NewL(integer);
   742 	CleanupStack::Pop(&integer);
   743 	}
   744 
   745 void CRSAVerifyVector::DoPerformanceTestActionL()
   746 {
   747 	__UHEAP_MARK;
   748 
   749 
   750 	TTimeIntervalMicroSeconds verifyTime(0);
   751 	TTime start, end;
   752 	TTimeIntervalSeconds diff(0);
   753 	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
   754 
   755 	TInt noVerifies = 0;
   756     
   757 	CRSAPKCS1v15Verifier *verifier = CRSAPKCS1v15Verifier::NewLC(*iPubKey);	
   758 	    	
   759 //	Time verification
   760 	start.UniversalTime();
   761 	while (diff < KIterationTime)
   762 		{
   763 		iResult = verifier->VerifyL(*iDigestInfo, *iSignature);
   764 		if (!iResult)
   765 			{
   766 			break;
   767 			}
   768 		noVerifies++;
   769 		end.UniversalTime();
   770 		end.SecondsFrom(start, diff);
   771 		}
   772 	end.UniversalTime();
   773 	verifyTime = end.MicroSecondsFrom(start);
   774 	TReal verifytime = I64REAL(verifyTime.Int64());
   775 
   776 	CleanupStack::PopAndDestroy(verifier);
   777 
   778 	__UHEAP_MARKEND;
   779 	
   780 	if (iResult)
   781 	{
   782 		TReal rate = I64REAL(verifyTime.Int64()) / noVerifies;
   783 		TBuf<256> buf;
   784 		_LIT(KVerifyTime, "\tVerify Time: %f us/verify (%i verifies in %f us)\r\n");
   785 		buf.Format(KVerifyTime, rate, noVerifies, verifytime);
   786 		iOut.writeString(buf);
   787 	}
   788 	else
   789 	{
   790 		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   791 		iOut.writeString(KNoTimingInfo);
   792 	}
   793 }
   794 
   795 void CRSAVerifyVector::DoPerformActionL()
   796 	{
   797 	__UHEAP_MARK;
   798 
   799 	CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(*iPubKey);
   800 	CleanupStack::PushL(verifier);
   801     iResult = verifier->VerifyL(*iDigestInfo, *iSignature);
   802 
   803 	CleanupStack::PopAndDestroy(verifier);
   804     
   805 	__UHEAP_MARKEND;
   806 	}