os/security/crypto/weakcryptospi/test/tasymmetric/tdhvector.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) 2003-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 "tdhvector.h"
sl@0
    20
#include "t_input.h"
sl@0
    21
#include "performancetest.h"
sl@0
    22
sl@0
    23
enum TActionMode {EMode1 = 1, EMode2 = 2, EMode3 = 3, EMode4 = 4};
sl@0
    24
sl@0
    25
CTestAction* CDHVector::NewL(RFs& aFs,
sl@0
    26
                               CConsoleBase& aConsole,
sl@0
    27
                               Output& aOut, 
sl@0
    28
                               const TTestActionSpec& aTestActionSpec)
sl@0
    29
	{
sl@0
    30
	CTestAction* self = CDHVector::NewLC(aFs, aConsole,
sl@0
    31
		aOut, aTestActionSpec);
sl@0
    32
	CleanupStack::Pop();
sl@0
    33
	return self;
sl@0
    34
	}
sl@0
    35
sl@0
    36
CTestAction* CDHVector::NewLC(RFs& aFs,
sl@0
    37
                                CConsoleBase& aConsole,
sl@0
    38
                                Output& aOut, 
sl@0
    39
                                const TTestActionSpec& aTestActionSpec)
sl@0
    40
	{
sl@0
    41
	CDHVector* self = new(ELeave) CDHVector(aFs, aConsole, aOut);
sl@0
    42
	CleanupStack::PushL(self);
sl@0
    43
	self->ConstructL(aTestActionSpec);
sl@0
    44
	return self;
sl@0
    45
	}
sl@0
    46
sl@0
    47
CDHVector::~CDHVector()
sl@0
    48
	{
sl@0
    49
	delete iN;
sl@0
    50
	delete iG;
sl@0
    51
	delete iX;
sl@0
    52
sl@0
    53
	if (iActionMode != EMode2)
sl@0
    54
		{
sl@0
    55
		iX1.Close();
sl@0
    56
		iX2.Close();
sl@0
    57
		}
sl@0
    58
	
sl@0
    59
	if (iActionMode == EMode3)
sl@0
    60
		{
sl@0
    61
		delete iNd;
sl@0
    62
		delete iGd;
sl@0
    63
		iN2.Close();
sl@0
    64
		iG2.Close();
sl@0
    65
		}
sl@0
    66
	
sl@0
    67
	 if (iActionMode == EMode4)
sl@0
    68
		 {
sl@0
    69
		 iN1.Close();
sl@0
    70
		 iG1.Close();
sl@0
    71
		 iN2.Close();
sl@0
    72
		 iG2.Close();
sl@0
    73
		 }
sl@0
    74
	}
sl@0
    75
sl@0
    76
CDHVector::CDHVector(RFs& /*aFs*/, 
sl@0
    77
                                     CConsoleBase& aConsole,
sl@0
    78
                                     Output& aOut)					 
sl@0
    79
: CVectorTest(aConsole, aOut)
sl@0
    80
	{
sl@0
    81
	}
sl@0
    82
sl@0
    83
void CDHVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    84
	{
sl@0
    85
	CVectorTest::ConstructL(aTestActionSpec);
sl@0
    86
sl@0
    87
	iActionMode = Input::ParseIntElement(aTestActionSpec.iActionBody, _L8("<actionmode>"), _L8("</actionmode>"));
sl@0
    88
	
sl@0
    89
    iN = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<n>"));
sl@0
    90
    iG = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<g>"));
sl@0
    91
    
sl@0
    92
    if(iActionMode == EMode2 || iActionMode == EMode4)
sl@0
    93
    	{
sl@0
    94
    	iX = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<x>"));
sl@0
    95
    	}
sl@0
    96
    else if(iActionMode == EMode3)
sl@0
    97
    	{
sl@0
    98
    	iNd = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<nd>"));
sl@0
    99
    	iGd = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<gd>"));
sl@0
   100
    	}
sl@0
   101
	}
sl@0
   102
sl@0
   103
void CDHVector::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
   104
	{
sl@0
   105
	iN1 = RInteger::NewL(*iN);
sl@0
   106
	iN2 = RInteger::NewL(*iN);
sl@0
   107
	iG1 = RInteger::NewL(*iG);
sl@0
   108
	iG2 = RInteger::NewL(*iG);
sl@0
   109
	
sl@0
   110
	if(iActionMode == EMode2 || iActionMode == EMode4)
sl@0
   111
		{
sl@0
   112
		iX1 = RInteger::NewL(*iX);
sl@0
   113
		iX2 = RInteger::NewL(*iX);
sl@0
   114
		}	
sl@0
   115
	else if(iActionMode == EMode3)
sl@0
   116
		{
sl@0
   117
		iN3 = RInteger::NewL(*iNd);
sl@0
   118
		iG3 = RInteger::NewL(*iGd);
sl@0
   119
		}
sl@0
   120
	
sl@0
   121
	TRequestStatus* status = &aStatus;
sl@0
   122
	User::RequestComplete(status, KErrNone);
sl@0
   123
	iActionState = CTestAction::EAction;
sl@0
   124
	}
sl@0
   125
sl@0
   126
void CDHVector::DoPerformanceTestActionL()
sl@0
   127
	{
sl@0
   128
	iResult = ETrue;
sl@0
   129
	__UHEAP_MARK;
sl@0
   130
sl@0
   131
	CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);
sl@0
   132
	CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
sl@0
   133
	CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
sl@0
   134
	
sl@0
   135
	TTimeIntervalMicroSeconds agreeTime(0);
sl@0
   136
	TTime start, end;
sl@0
   137
	TTimeIntervalSeconds diff(0);
sl@0
   138
	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
sl@0
   139
sl@0
   140
	TInt index = 0;
sl@0
   141
sl@0
   142
	start.UniversalTime();
sl@0
   143
sl@0
   144
	while (diff < KIterationTime)
sl@0
   145
		{
sl@0
   146
		delete const_cast<HBufC8*>(dh1->AgreeL(keyPair2->PublicKey()));
sl@0
   147
		end.UniversalTime();
sl@0
   148
		end.SecondsFrom(start, diff);
sl@0
   149
		index++;
sl@0
   150
		}
sl@0
   151
	end.UniversalTime();
sl@0
   152
	
sl@0
   153
	agreeTime = end.MicroSecondsFrom(start);
sl@0
   154
	
sl@0
   155
    CleanupStack::PopAndDestroy(dh1);
sl@0
   156
    CleanupStack::PopAndDestroy(keyPair2);
sl@0
   157
    CleanupStack::PopAndDestroy(keyPair1);
sl@0
   158
sl@0
   159
	__UHEAP_MARKEND;
sl@0
   160
sl@0
   161
	if (iResult)
sl@0
   162
		{
sl@0
   163
		TReal rate = I64REAL(agreeTime.Int64()) / index;
sl@0
   164
		TReal agreetime = I64REAL(agreeTime.Int64());
sl@0
   165
		TBuf<256> buf;
sl@0
   166
		_LIT(KEncryptTime, "\tKey Agreements\t%f");
sl@0
   167
		buf.Format(KEncryptTime, rate);
sl@0
   168
		iOut.writeString(buf);
sl@0
   169
		}
sl@0
   170
	else
sl@0
   171
		{
sl@0
   172
		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
sl@0
   173
		iOut.writeString(KNoTimingInfo);
sl@0
   174
		}
sl@0
   175
}
sl@0
   176
sl@0
   177
void CDHVector::DoPerformActionL()
sl@0
   178
	{
sl@0
   179
	iResult = ETrue;
sl@0
   180
	__UHEAP_MARK;
sl@0
   181
sl@0
   182
	switch (iActionMode)
sl@0
   183
		{
sl@0
   184
		// Test for successful keypair generation with N and G
sl@0
   185
		case EMode1:
sl@0
   186
			{
sl@0
   187
			CDHKeyPair* keyPair1 = CDHKeyPair::NewL(iN1, iG1);
sl@0
   188
			CleanupStack::PushL(keyPair1);
sl@0
   189
			CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
sl@0
   190
			CDH* dh1 = CDH::NewL(keyPair1->PrivateKey());
sl@0
   191
			CleanupStack::PushL(dh1);
sl@0
   192
			CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
sl@0
   193
			const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
sl@0
   194
			CleanupStack::PushL(const_cast<HBufC8*>(key1));
sl@0
   195
			const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
sl@0
   196
			CleanupStack::PushL(const_cast<HBufC8*>(key2));
sl@0
   197
			if(*key1 != *key2)
sl@0
   198
				{
sl@0
   199
				iResult = EFalse;
sl@0
   200
				}			
sl@0
   201
			CleanupStack::PopAndDestroy(2, const_cast<HBufC8*>(key1));	
sl@0
   202
			CleanupStack::PopAndDestroy(4, keyPair1);
sl@0
   203
			}
sl@0
   204
		break;
sl@0
   205
		
sl@0
   206
		// Test for successful keypair generation with N , G and X
sl@0
   207
		case EMode2:
sl@0
   208
			{
sl@0
   209
			CDHKeyPair* keyPair1 = CDHKeyPair::NewL(iN1, iG1, iX1);
sl@0
   210
			CleanupStack::PushL(keyPair1);
sl@0
   211
			CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2, iX2);
sl@0
   212
			CDH* dh1 = CDH::NewL(keyPair1->PrivateKey());
sl@0
   213
			CleanupStack::PushL(dh1);
sl@0
   214
			CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
sl@0
   215
			const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
sl@0
   216
			CleanupStack::PushL(const_cast<HBufC8*>(key1));
sl@0
   217
			const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
sl@0
   218
			CleanupStack::PushL(const_cast<HBufC8*>(key2));
sl@0
   219
			if(*key1 != *key2)
sl@0
   220
				{
sl@0
   221
				iResult = EFalse;
sl@0
   222
				}			
sl@0
   223
			CleanupStack::PopAndDestroy(2, const_cast<HBufC8*>(key1));	
sl@0
   224
			CleanupStack::PopAndDestroy(4, keyPair1);
sl@0
   225
			}
sl@0
   226
			break;
sl@0
   227
		
sl@0
   228
		// Test for mis matching N & G key pair. 
sl@0
   229
		case EMode3:
sl@0
   230
			{
sl@0
   231
			CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);			
sl@0
   232
			CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN3, iG3);
sl@0
   233
			CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
sl@0
   234
			HBufC8* key1 = NULL;
sl@0
   235
			TRAPD(err, key1 = dh1->AgreeL(keyPair2->PublicKey()));
sl@0
   236
			if (err != KErrArgument)
sl@0
   237
				{
sl@0
   238
				iResult = EFalse;
sl@0
   239
				delete key1;
sl@0
   240
				}
sl@0
   241
			CleanupStack::PopAndDestroy(3, keyPair1);
sl@0
   242
			}
sl@0
   243
			break;
sl@0
   244
sl@0
   245
		// Test for wromg values of G and X
sl@0
   246
		case EMode4:
sl@0
   247
			{
sl@0
   248
			CDHKeyPair* keyPair1 = NULL;			
sl@0
   249
			TRAPD(err, keyPair1 = CDHKeyPair::NewLC(iN1, iG1, iX1));
sl@0
   250
			if (err != KErrArgument)
sl@0
   251
				{
sl@0
   252
				iResult = EFalse;
sl@0
   253
				delete keyPair1;
sl@0
   254
				}			
sl@0
   255
			}
sl@0
   256
			break;
sl@0
   257
sl@0
   258
		default:
sl@0
   259
			break;			
sl@0
   260
		}
sl@0
   261
	__UHEAP_MARKEND;
sl@0
   262
	}
sl@0
   263
sl@0
   264