crypto/weakcrypto/test/tasymmetric/tdhvector.cpp
author hgs
Thu, 12 Aug 2010 21:07:10 +0530
changeset 90 8c545fea2798
parent 72 de46a57f75fb
permissions -rw-r--r--
201031_01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     1
/*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     3
* All rights reserved.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     8
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    11
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    12
* Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    13
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    14
* Description: 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    15
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    16
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    17
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    18
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    19
#include "tdhvector.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
#include <t_input.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
#include "performancetest.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
CTestAction* CDHVector::NewL(RFs& aFs,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
                               CConsoleBase& aConsole,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
                               Output& aOut, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
                               const TTestActionSpec& aTestActionSpec)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
	CTestAction* self = CDHVector::NewLC(aFs, aConsole,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
		aOut, aTestActionSpec);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
	CleanupStack::Pop();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
CTestAction* CDHVector::NewLC(RFs& aFs,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
                                CConsoleBase& aConsole,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
                                Output& aOut, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
                                const TTestActionSpec& aTestActionSpec)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
	CDHVector* self = new(ELeave) CDHVector(aFs, aConsole, aOut);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
	self->ConstructL(aTestActionSpec);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
CDHVector::~CDHVector()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
	delete iN;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
	delete iG;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
CDHVector::CDHVector(RFs& /*aFs*/, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
                                     CConsoleBase& aConsole,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
                                     Output& aOut)					 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
: CVectorTest(aConsole, aOut)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
void CDHVector::ConstructL(const TTestActionSpec& aTestActionSpec)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	CVectorTest::ConstructL(aTestActionSpec);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
    iN = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<n>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
    iG = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<g>"));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
void CDHVector::DoPerformPrerequisite(TRequestStatus& aStatus)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
	iN1 = RInteger::NewL(*iN);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
	iN2 = RInteger::NewL(*iN);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
	iG1 = RInteger::NewL(*iG);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
	iG2 = RInteger::NewL(*iG);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	TRequestStatus* status = &aStatus;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
	User::RequestComplete(status, KErrNone);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
	iActionState = CTestAction::EAction;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
void CDHVector::DoPerformanceTestActionL()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	iResult = ETrue;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
	__UHEAP_MARK;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
	CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
	CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86
	CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    87
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    88
	TTimeIntervalMicroSeconds agreeTime(0);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    89
	TTime start, end;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    90
	TTimeIntervalSeconds diff(0);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    91
	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
	TInt index = 0;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
	start.UniversalTime();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
	while (diff < KIterationTime)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    99
		delete const_cast<HBufC8*>(dh1->AgreeL(keyPair2->PublicKey()));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   100
		end.UniversalTime();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   101
		end.SecondsFrom(start, diff);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   102
		index++;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   103
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   104
	end.UniversalTime();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   105
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   106
	agreeTime = end.MicroSecondsFrom(start);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   107
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   108
    CleanupStack::PopAndDestroy(dh1);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   109
    CleanupStack::PopAndDestroy(keyPair2);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   110
    CleanupStack::PopAndDestroy(keyPair1);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   111
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   112
	__UHEAP_MARKEND;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   113
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   114
	if (iResult)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   115
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   116
		TReal rate = I64REAL(agreeTime.Int64()) / index;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   117
		TReal agreetime = I64REAL(agreeTime.Int64());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   118
		TBuf<256> buf;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   119
		_LIT(KEncryptTime, "\tKey Agreements: %f us/agreement (%i in %f us)\r\n");
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   120
		buf.Format(KEncryptTime, rate, index, agreetime);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   121
		iOut.writeString(buf);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   122
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   123
	else
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   124
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   125
		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   126
		iOut.writeString(KNoTimingInfo);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   127
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   128
}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   129
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   130
void CDHVector::DoPerformActionL()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   131
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   132
	iResult = ETrue;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   133
	__UHEAP_MARK;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   134
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   135
	CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   136
	CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   137
	CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   138
	CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   139
	const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   140
	CleanupStack::PushL(const_cast<HBufC8*>(key1));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   141
	const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   142
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   143
	if(*key1 != *key2)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   144
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   145
		iResult = EFalse;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   146
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   147
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   148
	delete const_cast<HBufC8*>(key2);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   149
	CleanupStack::PopAndDestroy(const_cast<HBufC8*>(key1));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   150
	CleanupStack::PopAndDestroy(dh2);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   151
	CleanupStack::PopAndDestroy(dh1);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   152
	CleanupStack::PopAndDestroy(keyPair2);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   153
	CleanupStack::PopAndDestroy(keyPair1);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   154
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   155
	__UHEAP_MARKEND;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   156
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   157
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   158