os/security/crypto/weakcryptospi/test/tcryptospi/src/KeyExchangeSyncStep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/KeyExchangeSyncStep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,260 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2010 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 +* Example CTestStep derived implementation
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +#include "keyexchangesyncstep.h"
    1.28 +
    1.29 +#include <cryptospi/keypair.h>
    1.30 +#include <cryptospi/cryptokeypairgeneratorapi.h>
    1.31 +#include <cryptospi/cryptokeyagreementapi.h>
    1.32 +#include <bigint.h>
    1.33 +
    1.34 +using namespace CryptoSpi;
    1.35 +
    1.36 +CKeyExchangeSyncStep::~CKeyExchangeSyncStep()
    1.37 +	{
    1.38 +	}
    1.39 +
    1.40 +CKeyExchangeSyncStep::CKeyExchangeSyncStep()
    1.41 +	{
    1.42 +	SetTestStepName(KKeyExchangeSyncStep);
    1.43 +	}
    1.44 +
    1.45 +TVerdict CKeyExchangeSyncStep::doTestStepPreambleL()
    1.46 +	{
    1.47 +	SetTestStepResult(EPass);
    1.48 +	return TestStepResult();
    1.49 +	}
    1.50 +
    1.51 +TVerdict CKeyExchangeSyncStep::doTestStepL()
    1.52 +	{
    1.53 +	INFO_PRINTF1(_L("*** Key Exchange - DH Key Agreement ***"));
    1.54 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.55 +	
    1.56 +  	if (TestStepResult()==EPass)
    1.57 +		{
    1.58 +		//Assume faliure, unless all is successful
    1.59 +		SetTestStepResult(EFail);
    1.60 +
    1.61 +		TVariantPtrC testVariant;
    1.62 +		TVariantPtrC dhnVariant;
    1.63 +		TVariantPtrC dhgVariant;
    1.64 +
    1.65 +		if(	!GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhnVariant) ||
    1.66 +			!GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhgVariant )
    1.67 +			)
    1.68 +			{
    1.69 +			// Leave if there's any error.
    1.70 +			User::Leave(KErrNotFound);
    1.71 +			}
    1.72 +		else
    1.73 +			{
    1.74 +			/*
    1.75 +	 		 * both DH keys (ie our private and their public keys) must use the same N and G parameters
    1.76 +	 		 */
    1.77 +	 		INFO_PRINTF1(_L("Creating Primes and Base Integers...")); 
    1.78 +	 		 
    1.79 +			RInteger DH_N = RInteger::NewPrimeL(1024);	// from ini file
    1.80 +			CleanupClosePushL(DH_N);
    1.81 +			RInteger DH_N_MinusTwo = RInteger::NewL(DH_N);
    1.82 +			CleanupClosePushL(DH_N_MinusTwo);
    1.83 +			DH_N_MinusTwo-=2;
    1.84 +			
    1.85 +			RInteger DH_G = RInteger::NewRandomL(TInteger::Two(), DH_N_MinusTwo);	
    1.86 +			CleanupClosePushL(DH_G);
    1.87 +			
    1.88 +			INFO_PRINTF1(_L("Creating Key Pair Generator...")); 
    1.89 +
    1.90 +			// create a DH key pair generator interface for creating the 2 key pairs
    1.91 +			CKeyPairGenerator* keyPairGeneratorImpl = NULL;
    1.92 +			
    1.93 +			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keyPairGeneratorImpl, 
    1.94 +														KDHKeyPairGeneratorUid, 
    1.95 +														NULL));
    1.96 +														
    1.97 +			if(keyPairGeneratorImpl && (err==KErrNone))	
    1.98 +				{
    1.99 +				
   1.100 +				CleanupStack::PushL(keyPairGeneratorImpl);
   1.101 +
   1.102 +				// package up the common parameters N and G for use through the rest of this method
   1.103 +				CCryptoParams* keyParameters = CCryptoParams::NewLC();
   1.104 +				keyParameters->AddL(DH_N, KDhKeyParameterNUid);
   1.105 +				keyParameters->AddL(DH_G, KDhKeyParameterGUid);
   1.106 +
   1.107 +				/* 
   1.108 +				 * call the api to create a DH key pair for alice
   1.109 +				 */
   1.110 +				INFO_PRINTF1(_L("Generating DH ALICE Key Pair..."));
   1.111 +				CKeyPair* keyAlice = NULL;
   1.112 +				
   1.113 +				TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL,
   1.114 +																*keyParameters, 
   1.115 +																keyAlice));
   1.116 +				CleanupStack::PushL(keyAlice);
   1.117 +
   1.118 +				/* 
   1.119 +				 * call the api to create a DH key pair for bob
   1.120 +				 */
   1.121 +				INFO_PRINTF1(_L("Generating DH BOB Key Pair..."));
   1.122 +				CKeyPair* keyBob = NULL;
   1.123 +				TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL, 
   1.124 +																*keyParameters, 
   1.125 +																keyBob));
   1.126 +				CleanupStack::PushL(keyBob);
   1.127 +
   1.128 +				/* 
   1.129 +				 * get DH key agreement interfaces
   1.130 +				 */
   1.131 +				INFO_PRINTF1(_L("Generating ALICE & BOB Key Agreement Interfaces..."));
   1.132 +				CKeyAgreement* keyAgreementAliceImpl =  NULL;
   1.133 +				
   1.134 +				TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementAliceImpl, 
   1.135 +																	KDHAgreementUid, 
   1.136 +																	keyAlice->PrivateKey(), 
   1.137 +																	keyParameters));
   1.138 +				CleanupStack::PushL(keyAgreementAliceImpl);
   1.139 +				
   1.140 +				CKeyAgreement* keyAgreementBobImpl =  NULL;
   1.141 +				
   1.142 +				TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementBobImpl, 
   1.143 +																	KDHAgreementUid, 
   1.144 +																	keyBob->PrivateKey(), 
   1.145 +																	keyParameters));
   1.146 +				CleanupStack::PushL(keyAgreementBobImpl);
   1.147 +
   1.148 +				/* 
   1.149 +				 * call the api to get a DH agreed keys
   1.150 +				 */
   1.151 +				INFO_PRINTF1(_L("Generating Agreed Keys..."));
   1.152 +				
   1.153 +				CKey* agreedKeyAlice = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
   1.154 +				CleanupStack::PushL(agreedKeyAlice);
   1.155 +				
   1.156 +				CKey* agreedKeyBob = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
   1.157 +				CleanupStack::PushL(agreedKeyBob);
   1.158 +
   1.159 +				/*
   1.160 +				 * compare the agreed keys
   1.161 +				 */
   1.162 +				const TInteger& agreedKeyDataAlice = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
   1.163 +				const TInteger& agreedKeyDataBob = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
   1.164 +				
   1.165 +				if (agreedKeyDataAlice == agreedKeyDataBob)
   1.166 +					{
   1.167 +					INFO_PRINTF1(_L("*** PASS : Key Agreement Success ***"));
   1.168 +					SetTestStepResult(EPass);
   1.169 +					}
   1.170 +				else
   1.171 +					{
   1.172 +					INFO_PRINTF1(_L("*** FAIL : Agreed Keys Mismatch ***"));
   1.173 +					SetTestStepResult(EFail);
   1.174 +					}
   1.175 +
   1.176 +				// Set the private keys to check the new agreements.
   1.177 +				keyAgreementAliceImpl->SetKeyL(keyAlice->PrivateKey(), keyParameters);
   1.178 +				keyAgreementAliceImpl->SetKeyL(keyBob->PrivateKey(), keyParameters);
   1.179 +				
   1.180 +				/* 
   1.181 +				* call the api to get a DH agreed keys
   1.182 +				*/
   1.183 +				INFO_PRINTF1(_L("Generating Agreed Keys second time..."));
   1.184 +								
   1.185 +				CKey* agreedKeyAlice1 = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
   1.186 +				CleanupStack::PushL(agreedKeyAlice1);
   1.187 +					
   1.188 +				CKey* agreedKeyBob1 = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
   1.189 +				CleanupStack::PushL(agreedKeyBob1);
   1.190 +
   1.191 +				/*
   1.192 +				 * compare the agreed keys
   1.193 +				 */
   1.194 +				const TInteger& agreedKeyDataAlice1 = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
   1.195 +				const TInteger& agreedKeyDataBob1 = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
   1.196 +				
   1.197 +				if (agreedKeyDataAlice1 != agreedKeyDataBob1)
   1.198 +					{					
   1.199 +					INFO_PRINTF1(_L("*** FAIL : Second Agreed Keys Mismatch ***"));
   1.200 +					SetTestStepResult(EFail);
   1.201 +					}	
   1.202 +							
   1.203 +				const CCryptoParams& cryptoParams1 = agreedKeyAlice1->KeyParameters();
   1.204 +				const CCryptoParams& cryptoParams2 = agreedKeyBob1->KeyParameters();				
   1.205 +				if (cryptoParams1.Count() != cryptoParams2.GetParams().Count())					
   1.206 +					{
   1.207 +					INFO_PRINTF1(_L("*** FAIL : Key Parameters' Count Mismatch ***"));
   1.208 +					SetTestStepResult(EFail);
   1.209 +					}
   1.210 +				
   1.211 +				TInt paramLength = 10;
   1.212 +				HBufC16* buf = HBufC16::NewLC(paramLength);
   1.213 +				TPtr16 ptr = buf->Des();
   1.214 +				ptr.Copy(_L("DH_N"));
   1.215 +				CCryptoParams* params = CCryptoParams::NewL();
   1.216 +				params->AddL(*buf, KDhKeyParameterNUid);
   1.217 +				if(!params->Count())
   1.218 +					{
   1.219 +					INFO_PRINTF1(_L("*** FAIL : Parameter construction with descriptor failed ***"));
   1.220 +					SetTestStepResult(EFail);
   1.221 +					}
   1.222 +
   1.223 +				if (agreedKeyAlice1->IsPresent(KSymmetricKeyParameterUid))
   1.224 +					{
   1.225 +					TRAPD(err, agreedKeyAlice1->GetTIntL(KSymmetricKeyParameterUid));
   1.226 +					if(err == KErrNone)
   1.227 +						{
   1.228 +						INFO_PRINTF1(_L("*** FAIL : Expected Key Parameter Int Value Mismatch ***"));
   1.229 +						SetTestStepResult(EFail);
   1.230 +						}
   1.231 +					}
   1.232 +				
   1.233 +				// Clear the second key agreement elements.
   1.234 +				CleanupStack::PopAndDestroy(3, agreedKeyAlice1);
   1.235 +				
   1.236 +				/* 
   1.237 +				 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
   1.238 +				 */
   1.239 +				CleanupStack::PopAndDestroy(agreedKeyBob);
   1.240 +				CleanupStack::PopAndDestroy(agreedKeyAlice);
   1.241 +				CleanupStack::PopAndDestroy(keyAgreementBobImpl);
   1.242 +				CleanupStack::PopAndDestroy(keyAgreementAliceImpl);
   1.243 +				CleanupStack::PopAndDestroy(keyBob);
   1.244 +				CleanupStack::PopAndDestroy(keyAlice);
   1.245 +				CleanupStack::PopAndDestroy(keyParameters);
   1.246 +				CleanupStack::PopAndDestroy(keyPairGeneratorImpl);	
   1.247 +				}
   1.248 +		
   1.249 +			CleanupStack::PopAndDestroy(&DH_G);
   1.250 +			CleanupStack::PopAndDestroy(&DH_N_MinusTwo);
   1.251 +			CleanupStack::PopAndDestroy(&DH_N);
   1.252 +			}
   1.253 +			
   1.254 +		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.255 +
   1.256 +		}
   1.257 +  	return TestStepResult();
   1.258 +	}
   1.259 +
   1.260 +TVerdict CKeyExchangeSyncStep::doTestStepPostambleL()
   1.261 +	{
   1.262 +	return TestStepResult();
   1.263 +	}