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