os/security/crypto/weakcrypto/test/tasymmetric/trsasignfb.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 "trsasignfb.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
sl@0
    26
_LIT8(KInputStart, "<input>");
sl@0
    27
_LIT8(KKeyBitsStart, "<keybits>");
sl@0
    28
_LIT8(KKeyBitsEnd, "</keybits>");
sl@0
    29
sl@0
    30
CRSASignFB::~CRSASignFB()
sl@0
    31
	{
sl@0
    32
	delete iBody;
sl@0
    33
	}
sl@0
    34
sl@0
    35
CRSASignFB::CRSASignFB(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
sl@0
    36
	: CTestAction(aConsole, aOut), iFs(aFs), iPerfTestIterations(100)
sl@0
    37
	{
sl@0
    38
	}
sl@0
    39
sl@0
    40
CTestAction* CRSASignFB::NewL(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
sl@0
    41
	const TTestActionSpec& aTestActionSpec)
sl@0
    42
	{
sl@0
    43
	CTestAction* self = CRSASignFB::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* CRSASignFB::NewLC(RFs& aFs, CConsoleBase& aConsole, Output& aOut, 
sl@0
    50
	const TTestActionSpec& aTestActionSpec)
sl@0
    51
	{
sl@0
    52
	CRSASignFB* self = new(ELeave) CRSASignFB(aFs, aConsole, aOut);
sl@0
    53
	CleanupStack::PushL(self);
sl@0
    54
	self->ConstructL(aTestActionSpec);
sl@0
    55
	return self;
sl@0
    56
	}
sl@0
    57
sl@0
    58
sl@0
    59
TRSAPrivateKeyType CRSASignFB::TypeOfCrypto()
sl@0
    60
	{
sl@0
    61
	return iCryptoType;
sl@0
    62
	}
sl@0
    63
	
sl@0
    64
void CRSASignFB::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    65
{
sl@0
    66
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    67
	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
sl@0
    68
	iBody->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    69
sl@0
    70
	if (CPerformance::PerformanceTester()->IsTestingPerformance())
sl@0
    71
	{//	Number of test iterations
sl@0
    72
		TPtrC8 itsPtr = Input::ParseElement(aTestActionSpec.iActionBody, KIterationsStart, KIterationsEnd);
sl@0
    73
		if (itsPtr!=KNullDesC8)
sl@0
    74
		{
sl@0
    75
			TLex8 lex;
sl@0
    76
			lex.Assign(itsPtr);
sl@0
    77
			User::LeaveIfError(lex.Val(iPerfTestIterations));
sl@0
    78
			if (iPerfTestIterations > KMaxIterations)
sl@0
    79
				User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
sl@0
    80
			
sl@0
    81
		}
sl@0
    82
		TPtrC8 cryptoPtr = Input::ParseElement(aTestActionSpec.iActionBody, KTypeOfCryptoStart, KTypeOfCryptoEnd);
sl@0
    83
		if (cryptoPtr.CompareF(KRSAStandard) == 0)
sl@0
    84
			{
sl@0
    85
			iCryptoType = EStandard;
sl@0
    86
			}
sl@0
    87
		else if (cryptoPtr.CompareF(KRSAStandardCRT) == 0)
sl@0
    88
			{
sl@0
    89
			iCryptoType = EStandardCRT;
sl@0
    90
			}
sl@0
    91
		else
sl@0
    92
			{
sl@0
    93
			User::Panic(_L("AsymmetricPerformance.exe"), KErrArgument);
sl@0
    94
			}
sl@0
    95
	}
sl@0
    96
sl@0
    97
	TPtrC8 keyBitsPtr = Input::ParseElement(*iBody, KKeyBitsStart, KKeyBitsEnd); 
sl@0
    98
	if (keyBitsPtr!=KNullDesC8)
sl@0
    99
	{
sl@0
   100
		TLex8 lex;
sl@0
   101
		lex.Assign(keyBitsPtr);
sl@0
   102
		User::LeaveIfError(lex.Val(iKeyBits));
sl@0
   103
	}
sl@0
   104
}
sl@0
   105
sl@0
   106
void CRSASignFB::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
   107
	{
sl@0
   108
	TRequestStatus* status = &aStatus;
sl@0
   109
sl@0
   110
	HBufC8* input = Input::ParseElementHexL(*iBody, KInputStart);
sl@0
   111
	CleanupStack::PushL(input);
sl@0
   112
	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0
   113
	iSigInput->Update(*input);
sl@0
   114
	iInput = iSigInput->Final().AllocL();
sl@0
   115
	CleanupStack::PopAndDestroy(input);
sl@0
   116
sl@0
   117
	User::RequestComplete(status, KErrNone);
sl@0
   118
	iActionState = CTestAction::EAction;
sl@0
   119
	}
sl@0
   120
sl@0
   121
void CRSASignFB::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
   122
	{
sl@0
   123
	TRequestStatus* status = &aStatus;
sl@0
   124
	delete iInput;
sl@0
   125
	delete iSigInput;
sl@0
   126
sl@0
   127
	iFinished = ETrue;
sl@0
   128
	User::RequestComplete(status, iActionResult);
sl@0
   129
	}
sl@0
   130
sl@0
   131
void CRSASignFB::DoReportAction(void)
sl@0
   132
	{
sl@0
   133
	}
sl@0
   134
sl@0
   135
void CRSASignFB::DoCheckResult(TInt)
sl@0
   136
	{
sl@0
   137
	if (iResult)
sl@0
   138
		iConsole.Printf(_L("."));
sl@0
   139
	else
sl@0
   140
		iConsole.Printf(_L("X"));
sl@0
   141
	}
sl@0
   142
sl@0
   143
void CRSASignFB::Hex(HBufC8& aString)
sl@0
   144
    {
sl@0
   145
    TPtr8 ptr=aString.Des();
sl@0
   146
    if (aString.Length()%2)
sl@0
   147
        {
sl@0
   148
        ptr.SetLength(0);
sl@0
   149
        return;
sl@0
   150
        }
sl@0
   151
    TInt i;
sl@0
   152
    for (i=0;i<aString.Length();i+=2)
sl@0
   153
        {
sl@0
   154
        TUint8 tmp;
sl@0
   155
        tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
sl@0
   156
        tmp*=16;
sl@0
   157
        tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
sl@0
   158
        ptr[i/2]=tmp;
sl@0
   159
        }
sl@0
   160
    ptr.SetLength(aString.Length()/2);
sl@0
   161
    }
sl@0
   162
sl@0
   163
void CRSASignFB::PerformAction(TRequestStatus& aStatus)
sl@0
   164
	{
sl@0
   165
	TRequestStatus* status = &aStatus;
sl@0
   166
sl@0
   167
	if (CPerformance::PerformanceTester()->IsTestingPerformance())
sl@0
   168
	{
sl@0
   169
		iConsole.Printf(_L(">"));	//	Indicates start of test
sl@0
   170
		TRAP(iActionResult, DoPerformanceTestActionL());
sl@0
   171
	}
sl@0
   172
	else
sl@0
   173
	{
sl@0
   174
		TRAP(iActionResult, DoPerformActionL());
sl@0
   175
	}
sl@0
   176
sl@0
   177
	if (iActionResult==KErrNoMemory)
sl@0
   178
		User::Leave(iActionResult);	//	For OOM testing
sl@0
   179
	
sl@0
   180
	User::RequestComplete(status, iActionResult);
sl@0
   181
	iActionState = CTestAction::EPostrequisite;
sl@0
   182
	}
sl@0
   183
sl@0
   184
sl@0
   185
void CRSASignFB::DoPerformanceTestActionL()
sl@0
   186
{
sl@0
   187
	__UHEAP_MARK;
sl@0
   188
	iResult = EFalse;
sl@0
   189
sl@0
   190
	TTimeIntervalMicroSeconds keyCreateTime(0);
sl@0
   191
	TTimeIntervalMicroSeconds signTime(0);
sl@0
   192
	TTimeIntervalMicroSeconds verifyTime(0);
sl@0
   193
	TTime start, end;
sl@0
   194
	TTimeIntervalSeconds diff(0);
sl@0
   195
	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
sl@0
   196
sl@0
   197
	//	Time key pair creation
sl@0
   198
	CRSAKeyPair *rsaPair = NULL;
sl@0
   199
	TUint noRSAPairs = 0;
sl@0
   200
	start.UniversalTime();
sl@0
   201
	while (diff < KIterationTime)
sl@0
   202
	{
sl@0
   203
       rsaPair = CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto());
sl@0
   204
       CleanupStack::PopAndDestroy();
sl@0
   205
       noRSAPairs++;
sl@0
   206
	   end.UniversalTime();
sl@0
   207
	   end.SecondsFrom(start, diff);
sl@0
   208
	}
sl@0
   209
	end.UniversalTime();
sl@0
   210
	keyCreateTime = end.MicroSecondsFrom(start);
sl@0
   211
	TReal keycreatetime = I64REAL(keyCreateTime.Int64());
sl@0
   212
	
sl@0
   213
	rsaPair=CRSAKeyPair::NewLC(iKeyBits, TypeOfCrypto()); // Create one keypair for operations
sl@0
   214
sl@0
   215
	CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(rsaPair->PrivateKey());
sl@0
   216
	CleanupStack::PushL(signer);
sl@0
   217
	CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(rsaPair->PublicKey());
sl@0
   218
	CleanupStack::PushL(verifier);
sl@0
   219
sl@0
   220
	const CRSASignature  *testSig;
sl@0
   221
sl@0
   222
	//	Time signing
sl@0
   223
	diff = 0;
sl@0
   224
	TInt noSignings = 0;
sl@0
   225
	start.UniversalTime();
sl@0
   226
	while (diff < KIterationTime)
sl@0
   227
	{
sl@0
   228
		testSig = signer->SignL(*iInput);
sl@0
   229
		delete testSig;
sl@0
   230
		noSignings++;
sl@0
   231
		end.UniversalTime();
sl@0
   232
		end.SecondsFrom(start, diff);
sl@0
   233
	}
sl@0
   234
	end.UniversalTime();
sl@0
   235
	signTime = end.MicroSecondsFrom(start);
sl@0
   236
	TReal signtime = I64REAL(signTime.Int64());	
sl@0
   237
	//	Time verification
sl@0
   238
	TInt noVerifies = 0;
sl@0
   239
	diff = 0;
sl@0
   240
	testSig = signer->SignL(*iInput);
sl@0
   241
	CleanupStack::PushL(const_cast<CRSASignature*>(testSig));
sl@0
   242
	start.UniversalTime();
sl@0
   243
	while (diff < KIterationTime)
sl@0
   244
	{
sl@0
   245
		iResult = verifier->VerifyL(*iInput, *testSig);
sl@0
   246
sl@0
   247
		// do as many verfies as possible 
sl@0
   248
		if (!iResult)
sl@0
   249
		{
sl@0
   250
		   break;
sl@0
   251
		}
sl@0
   252
		noVerifies++;
sl@0
   253
		end.UniversalTime();
sl@0
   254
		end.SecondsFrom(start, diff);
sl@0
   255
	}	
sl@0
   256
	end.UniversalTime();
sl@0
   257
	verifyTime = end.MicroSecondsFrom(start);
sl@0
   258
    TReal verifytime = I64REAL(verifyTime.Int64());
sl@0
   259
	CleanupStack::PopAndDestroy(4);	//	verifier,signer,testSig,rsaPairs
sl@0
   260
	__UHEAP_MARKEND;
sl@0
   261
		 
sl@0
   262
	if (iResult)
sl@0
   263
	{
sl@0
   264
		TBuf<256> buf;
sl@0
   265
		TReal rate = I64REAL(keyCreateTime.Int64()) / noRSAPairs;
sl@0
   266
		
sl@0
   267
		_LIT(KKeyCreateTime, "\n\tKeyCreate: %f us/key creation (no creations: %i in %f us)\r\n");
sl@0
   268
 		buf.Format(KKeyCreateTime, rate, noRSAPairs, keycreatetime);
sl@0
   269
		iOut.writeString(buf);
sl@0
   270
sl@0
   271
		rate = I64REAL(signTime.Int64()) / noSignings;
sl@0
   272
		_LIT(KSignTime, "\tSigning: %f us/signing (no signings: %i in %f us)\r\n");
sl@0
   273
  		buf.Format(KSignTime, rate, noSignings, signtime);
sl@0
   274
		iOut.writeString(buf);
sl@0
   275
		
sl@0
   276
		rate = I64REAL(verifyTime.Int64()) / noVerifies;
sl@0
   277
		_LIT(KVerifyTime, "\tVerifying: %f us/verify (no verfies: %i in %f us)\r\n");
sl@0
   278
 		buf.Format(KVerifyTime, rate, noVerifies, verifytime);
sl@0
   279
		iOut.writeString(buf);
sl@0
   280
	}
sl@0
   281
	else
sl@0
   282
	{
sl@0
   283
		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
sl@0
   284
		iOut.writeString(KNoTimingInfo);
sl@0
   285
	}
sl@0
   286
}
sl@0
   287
sl@0
   288
void CRSASignFB::DoPerformActionL()
sl@0
   289
	{
sl@0
   290
	__UHEAP_MARK;
sl@0
   291
	
sl@0
   292
	iResult = EFalse;
sl@0
   293
sl@0
   294
	CRSAKeyPair* rsaPair = CRSAKeyPair::NewLC(512, TypeOfCrypto());
sl@0
   295
sl@0
   296
	CRSAPKCS1v15Signer* signer = CRSAPKCS1v15Signer::NewL(rsaPair->PrivateKey());
sl@0
   297
	CleanupStack::PushL(signer);
sl@0
   298
sl@0
   299
	CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewL(rsaPair->PublicKey());
sl@0
   300
	CleanupStack::PushL(verifier);
sl@0
   301
sl@0
   302
	const CRSASignature* signature = signer->SignL(*iInput);
sl@0
   303
	CleanupStack::PushL(const_cast<CRSASignature*>(signature));
sl@0
   304
sl@0
   305
	if(verifier->VerifyL(*iInput, *signature))
sl@0
   306
		{
sl@0
   307
		iResult = ETrue;
sl@0
   308
		}
sl@0
   309
	
sl@0
   310
	CleanupStack::PopAndDestroy(4, rsaPair); //signature, verifier, signer, rsaPair
sl@0
   311
	
sl@0
   312
	__UHEAP_MARKEND;
sl@0
   313
	}
sl@0
   314