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