os/security/crypto/weakcryptospi/test/tasymmetric/tdhvector.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/tdhvector.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,264 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "tdhvector.h"
    1.23 +#include "t_input.h"
    1.24 +#include "performancetest.h"
    1.25 +
    1.26 +enum TActionMode {EMode1 = 1, EMode2 = 2, EMode3 = 3, EMode4 = 4};
    1.27 +
    1.28 +CTestAction* CDHVector::NewL(RFs& aFs,
    1.29 +                               CConsoleBase& aConsole,
    1.30 +                               Output& aOut, 
    1.31 +                               const TTestActionSpec& aTestActionSpec)
    1.32 +	{
    1.33 +	CTestAction* self = CDHVector::NewLC(aFs, aConsole,
    1.34 +		aOut, aTestActionSpec);
    1.35 +	CleanupStack::Pop();
    1.36 +	return self;
    1.37 +	}
    1.38 +
    1.39 +CTestAction* CDHVector::NewLC(RFs& aFs,
    1.40 +                                CConsoleBase& aConsole,
    1.41 +                                Output& aOut, 
    1.42 +                                const TTestActionSpec& aTestActionSpec)
    1.43 +	{
    1.44 +	CDHVector* self = new(ELeave) CDHVector(aFs, aConsole, aOut);
    1.45 +	CleanupStack::PushL(self);
    1.46 +	self->ConstructL(aTestActionSpec);
    1.47 +	return self;
    1.48 +	}
    1.49 +
    1.50 +CDHVector::~CDHVector()
    1.51 +	{
    1.52 +	delete iN;
    1.53 +	delete iG;
    1.54 +	delete iX;
    1.55 +
    1.56 +	if (iActionMode != EMode2)
    1.57 +		{
    1.58 +		iX1.Close();
    1.59 +		iX2.Close();
    1.60 +		}
    1.61 +	
    1.62 +	if (iActionMode == EMode3)
    1.63 +		{
    1.64 +		delete iNd;
    1.65 +		delete iGd;
    1.66 +		iN2.Close();
    1.67 +		iG2.Close();
    1.68 +		}
    1.69 +	
    1.70 +	 if (iActionMode == EMode4)
    1.71 +		 {
    1.72 +		 iN1.Close();
    1.73 +		 iG1.Close();
    1.74 +		 iN2.Close();
    1.75 +		 iG2.Close();
    1.76 +		 }
    1.77 +	}
    1.78 +
    1.79 +CDHVector::CDHVector(RFs& /*aFs*/, 
    1.80 +                                     CConsoleBase& aConsole,
    1.81 +                                     Output& aOut)					 
    1.82 +: CVectorTest(aConsole, aOut)
    1.83 +	{
    1.84 +	}
    1.85 +
    1.86 +void CDHVector::ConstructL(const TTestActionSpec& aTestActionSpec)
    1.87 +	{
    1.88 +	CVectorTest::ConstructL(aTestActionSpec);
    1.89 +
    1.90 +	iActionMode = Input::ParseIntElement(aTestActionSpec.iActionBody, _L8("<actionmode>"), _L8("</actionmode>"));
    1.91 +	
    1.92 +    iN = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<n>"));
    1.93 +    iG = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<g>"));
    1.94 +    
    1.95 +    if(iActionMode == EMode2 || iActionMode == EMode4)
    1.96 +    	{
    1.97 +    	iX = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<x>"));
    1.98 +    	}
    1.99 +    else if(iActionMode == EMode3)
   1.100 +    	{
   1.101 +    	iNd = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<nd>"));
   1.102 +    	iGd = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<gd>"));
   1.103 +    	}
   1.104 +	}
   1.105 +
   1.106 +void CDHVector::DoPerformPrerequisite(TRequestStatus& aStatus)
   1.107 +	{
   1.108 +	iN1 = RInteger::NewL(*iN);
   1.109 +	iN2 = RInteger::NewL(*iN);
   1.110 +	iG1 = RInteger::NewL(*iG);
   1.111 +	iG2 = RInteger::NewL(*iG);
   1.112 +	
   1.113 +	if(iActionMode == EMode2 || iActionMode == EMode4)
   1.114 +		{
   1.115 +		iX1 = RInteger::NewL(*iX);
   1.116 +		iX2 = RInteger::NewL(*iX);
   1.117 +		}	
   1.118 +	else if(iActionMode == EMode3)
   1.119 +		{
   1.120 +		iN3 = RInteger::NewL(*iNd);
   1.121 +		iG3 = RInteger::NewL(*iGd);
   1.122 +		}
   1.123 +	
   1.124 +	TRequestStatus* status = &aStatus;
   1.125 +	User::RequestComplete(status, KErrNone);
   1.126 +	iActionState = CTestAction::EAction;
   1.127 +	}
   1.128 +
   1.129 +void CDHVector::DoPerformanceTestActionL()
   1.130 +	{
   1.131 +	iResult = ETrue;
   1.132 +	__UHEAP_MARK;
   1.133 +
   1.134 +	CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);
   1.135 +	CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
   1.136 +	CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
   1.137 +	
   1.138 +	TTimeIntervalMicroSeconds agreeTime(0);
   1.139 +	TTime start, end;
   1.140 +	TTimeIntervalSeconds diff(0);
   1.141 +	const TTimeIntervalSeconds KIterationTime(iPerfTestIterations);
   1.142 +
   1.143 +	TInt index = 0;
   1.144 +
   1.145 +	start.UniversalTime();
   1.146 +
   1.147 +	while (diff < KIterationTime)
   1.148 +		{
   1.149 +		delete const_cast<HBufC8*>(dh1->AgreeL(keyPair2->PublicKey()));
   1.150 +		end.UniversalTime();
   1.151 +		end.SecondsFrom(start, diff);
   1.152 +		index++;
   1.153 +		}
   1.154 +	end.UniversalTime();
   1.155 +	
   1.156 +	agreeTime = end.MicroSecondsFrom(start);
   1.157 +	
   1.158 +    CleanupStack::PopAndDestroy(dh1);
   1.159 +    CleanupStack::PopAndDestroy(keyPair2);
   1.160 +    CleanupStack::PopAndDestroy(keyPair1);
   1.161 +
   1.162 +	__UHEAP_MARKEND;
   1.163 +
   1.164 +	if (iResult)
   1.165 +		{
   1.166 +		TReal rate = I64REAL(agreeTime.Int64()) / index;
   1.167 +		TReal agreetime = I64REAL(agreeTime.Int64());
   1.168 +		TBuf<256> buf;
   1.169 +		_LIT(KEncryptTime, "\tKey Agreements\t%f");
   1.170 +		buf.Format(KEncryptTime, rate);
   1.171 +		iOut.writeString(buf);
   1.172 +		}
   1.173 +	else
   1.174 +		{
   1.175 +		_LIT(KNoTimingInfo, "\tTest Failed! No benchmark data\n");
   1.176 +		iOut.writeString(KNoTimingInfo);
   1.177 +		}
   1.178 +}
   1.179 +
   1.180 +void CDHVector::DoPerformActionL()
   1.181 +	{
   1.182 +	iResult = ETrue;
   1.183 +	__UHEAP_MARK;
   1.184 +
   1.185 +	switch (iActionMode)
   1.186 +		{
   1.187 +		// Test for successful keypair generation with N and G
   1.188 +		case EMode1:
   1.189 +			{
   1.190 +			CDHKeyPair* keyPair1 = CDHKeyPair::NewL(iN1, iG1);
   1.191 +			CleanupStack::PushL(keyPair1);
   1.192 +			CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2);
   1.193 +			CDH* dh1 = CDH::NewL(keyPair1->PrivateKey());
   1.194 +			CleanupStack::PushL(dh1);
   1.195 +			CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
   1.196 +			const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
   1.197 +			CleanupStack::PushL(const_cast<HBufC8*>(key1));
   1.198 +			const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
   1.199 +			CleanupStack::PushL(const_cast<HBufC8*>(key2));
   1.200 +			if(*key1 != *key2)
   1.201 +				{
   1.202 +				iResult = EFalse;
   1.203 +				}			
   1.204 +			CleanupStack::PopAndDestroy(2, const_cast<HBufC8*>(key1));	
   1.205 +			CleanupStack::PopAndDestroy(4, keyPair1);
   1.206 +			}
   1.207 +		break;
   1.208 +		
   1.209 +		// Test for successful keypair generation with N , G and X
   1.210 +		case EMode2:
   1.211 +			{
   1.212 +			CDHKeyPair* keyPair1 = CDHKeyPair::NewL(iN1, iG1, iX1);
   1.213 +			CleanupStack::PushL(keyPair1);
   1.214 +			CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN2, iG2, iX2);
   1.215 +			CDH* dh1 = CDH::NewL(keyPair1->PrivateKey());
   1.216 +			CleanupStack::PushL(dh1);
   1.217 +			CDH* dh2 = CDH::NewLC(keyPair2->PrivateKey());
   1.218 +			const HBufC8* key1 = dh1->AgreeL(keyPair2->PublicKey());
   1.219 +			CleanupStack::PushL(const_cast<HBufC8*>(key1));
   1.220 +			const HBufC8* key2 = dh2->AgreeL(keyPair1->PublicKey());
   1.221 +			CleanupStack::PushL(const_cast<HBufC8*>(key2));
   1.222 +			if(*key1 != *key2)
   1.223 +				{
   1.224 +				iResult = EFalse;
   1.225 +				}			
   1.226 +			CleanupStack::PopAndDestroy(2, const_cast<HBufC8*>(key1));	
   1.227 +			CleanupStack::PopAndDestroy(4, keyPair1);
   1.228 +			}
   1.229 +			break;
   1.230 +		
   1.231 +		// Test for mis matching N & G key pair. 
   1.232 +		case EMode3:
   1.233 +			{
   1.234 +			CDHKeyPair* keyPair1 = CDHKeyPair::NewLC(iN1, iG1);			
   1.235 +			CDHKeyPair* keyPair2 = CDHKeyPair::NewLC(iN3, iG3);
   1.236 +			CDH* dh1 = CDH::NewLC(keyPair1->PrivateKey());
   1.237 +			HBufC8* key1 = NULL;
   1.238 +			TRAPD(err, key1 = dh1->AgreeL(keyPair2->PublicKey()));
   1.239 +			if (err != KErrArgument)
   1.240 +				{
   1.241 +				iResult = EFalse;
   1.242 +				delete key1;
   1.243 +				}
   1.244 +			CleanupStack::PopAndDestroy(3, keyPair1);
   1.245 +			}
   1.246 +			break;
   1.247 +
   1.248 +		// Test for wromg values of G and X
   1.249 +		case EMode4:
   1.250 +			{
   1.251 +			CDHKeyPair* keyPair1 = NULL;			
   1.252 +			TRAPD(err, keyPair1 = CDHKeyPair::NewLC(iN1, iG1, iX1));
   1.253 +			if (err != KErrArgument)
   1.254 +				{
   1.255 +				iResult = EFalse;
   1.256 +				delete keyPair1;
   1.257 +				}			
   1.258 +			}
   1.259 +			break;
   1.260 +
   1.261 +		default:
   1.262 +			break;			
   1.263 +		}
   1.264 +	__UHEAP_MARKEND;
   1.265 +	}
   1.266 +
   1.267 +