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