crypto/weakcryptospi/test/tcryptospi/src/KeyExchangeSyncStep.cpp
changeset 8 35751d3474b7
child 43 2f10d260163b
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-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 * Example CTestStep derived implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalTechnology
       
    23 */
       
    24 #include "keyexchangesyncstep.h"
       
    25 
       
    26 #include "keypair.h"
       
    27 #include "cryptokeypairgeneratorapi.h"
       
    28 #include "cryptokeyagreementapi.h"
       
    29 #include <bigint.h>
       
    30 
       
    31 using namespace CryptoSpi;
       
    32 
       
    33 CKeyExchangeSyncStep::~CKeyExchangeSyncStep()
       
    34 	{
       
    35 	}
       
    36 
       
    37 CKeyExchangeSyncStep::CKeyExchangeSyncStep()
       
    38 	{
       
    39 	SetTestStepName(KKeyExchangeSyncStep);
       
    40 	}
       
    41 
       
    42 TVerdict CKeyExchangeSyncStep::doTestStepPreambleL()
       
    43 	{
       
    44 	SetTestStepResult(EPass);
       
    45 	return TestStepResult();
       
    46 	}
       
    47 
       
    48 TVerdict CKeyExchangeSyncStep::doTestStepL()
       
    49 	{
       
    50 	INFO_PRINTF1(_L("*** Key Exchange - DH Key Agreement ***"));
       
    51 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
    52 	
       
    53   	if (TestStepResult()==EPass)
       
    54 		{
       
    55 		//Assume faliure, unless all is successful
       
    56 		SetTestStepResult(EFail);
       
    57 
       
    58 		TVariantPtrC testVariant;
       
    59 		TVariantPtrC dhnVariant;
       
    60 		TVariantPtrC dhgVariant;
       
    61 
       
    62 		if(	!GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhnVariant) ||
       
    63 			!GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhgVariant )
       
    64 			)
       
    65 			{
       
    66 			// Leave if there's any error.
       
    67 			User::Leave(KErrNotFound);
       
    68 			}
       
    69 		else
       
    70 			{
       
    71 			/*
       
    72 	 		 * both DH keys (ie our private and their public keys) must use the same N and G parameters
       
    73 	 		 */
       
    74 	 		INFO_PRINTF1(_L("Creating Primes and Base Integers...")); 
       
    75 	 		 
       
    76 			RInteger DH_N = RInteger::NewPrimeL(1024);	// from ini file
       
    77 			CleanupClosePushL(DH_N);
       
    78 			RInteger DH_N_MinusTwo = RInteger::NewL(DH_N);
       
    79 			CleanupClosePushL(DH_N_MinusTwo);
       
    80 			DH_N_MinusTwo-=2;
       
    81 			
       
    82 			RInteger DH_G = RInteger::NewRandomL(TInteger::Two(), DH_N_MinusTwo);	
       
    83 			CleanupClosePushL(DH_G);
       
    84 			
       
    85 			INFO_PRINTF1(_L("Creating Key Pair Generator...")); 
       
    86 
       
    87 			// create a DH key pair generator interface for creating the 2 key pairs
       
    88 			CKeyPairGenerator* keyPairGeneratorImpl = NULL;
       
    89 			
       
    90 			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keyPairGeneratorImpl, 
       
    91 														KDHKeyPairGeneratorUid, 
       
    92 														NULL));
       
    93 														
       
    94 			if(keyPairGeneratorImpl && (err==KErrNone))	
       
    95 				{
       
    96 				
       
    97 				CleanupStack::PushL(keyPairGeneratorImpl);
       
    98 
       
    99 				// package up the common parameters N and G for use through the rest of this method
       
   100 				CCryptoParams* keyParameters = CCryptoParams::NewLC();
       
   101 				keyParameters->AddL(DH_N, KDhKeyParameterNUid);
       
   102 				keyParameters->AddL(DH_G, KDhKeyParameterGUid);
       
   103 
       
   104 				/* 
       
   105 				 * call the api to create a DH key pair for alice
       
   106 				 */
       
   107 				INFO_PRINTF1(_L("Generating DH ALICE Key Pair..."));
       
   108 				CKeyPair* keyAlice = NULL;
       
   109 				
       
   110 				TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL,
       
   111 																*keyParameters, 
       
   112 																keyAlice));
       
   113 				CleanupStack::PushL(keyAlice);
       
   114 
       
   115 				/* 
       
   116 				 * call the api to create a DH key pair for bob
       
   117 				 */
       
   118 				INFO_PRINTF1(_L("Generating DH BOB Key Pair..."));
       
   119 				CKeyPair* keyBob = NULL;
       
   120 				TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL, 
       
   121 																*keyParameters, 
       
   122 																keyBob));
       
   123 				CleanupStack::PushL(keyBob);
       
   124 
       
   125 				/* 
       
   126 				 * get DH key agreement interfaces
       
   127 				 */
       
   128 				INFO_PRINTF1(_L("Generating ALICE & BOB Key Agreement Interfaces..."));
       
   129 				CKeyAgreement* keyAgreementAliceImpl =  NULL;
       
   130 				
       
   131 				TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementAliceImpl, 
       
   132 																	KDHAgreementUid, 
       
   133 																	keyAlice->PrivateKey(), 
       
   134 																	keyParameters));
       
   135 				CleanupStack::PushL(keyAgreementAliceImpl);
       
   136 				
       
   137 				CKeyAgreement* keyAgreementBobImpl =  NULL;
       
   138 				
       
   139 				TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementBobImpl, 
       
   140 																	KDHAgreementUid, 
       
   141 																	keyBob->PrivateKey(), 
       
   142 																	keyParameters));
       
   143 				CleanupStack::PushL(keyAgreementBobImpl);
       
   144 
       
   145 				/* 
       
   146 				 * call the api to get a DH agreed keys
       
   147 				 */
       
   148 				INFO_PRINTF1(_L("Generating Agreed Keys..."));
       
   149 				
       
   150 				CKey* agreedKeyAlice = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
       
   151 				CleanupStack::PushL(agreedKeyAlice);
       
   152 				
       
   153 				CKey* agreedKeyBob = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
       
   154 				CleanupStack::PushL(agreedKeyBob);
       
   155 
       
   156 				/*
       
   157 				 * compare the agreed keys
       
   158 				 */
       
   159 				const TInteger& agreedKeyDataAlice = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
       
   160 				const TInteger& agreedKeyDataBob = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
       
   161 				
       
   162 				if (agreedKeyDataAlice == agreedKeyDataBob)
       
   163 					{
       
   164 					INFO_PRINTF1(_L("*** PASS : Key Agreement Success ***"));
       
   165 					SetTestStepResult(EPass);
       
   166 					}
       
   167 				else
       
   168 					{
       
   169 					INFO_PRINTF1(_L("*** FAIL : Agreed Keys Mismatch ***"));
       
   170 					SetTestStepResult(EFail);
       
   171 					}
       
   172 
       
   173 				// Set the private keys to check the new agreements.
       
   174 				keyAgreementAliceImpl->SetKeyL(keyAlice->PrivateKey(), keyParameters);
       
   175 				keyAgreementAliceImpl->SetKeyL(keyBob->PrivateKey(), keyParameters);
       
   176 				
       
   177 				/* 
       
   178 				* call the api to get a DH agreed keys
       
   179 				*/
       
   180 				INFO_PRINTF1(_L("Generating Agreed Keys second time..."));
       
   181 								
       
   182 				CKey* agreedKeyAlice1 = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
       
   183 				CleanupStack::PushL(agreedKeyAlice1);
       
   184 					
       
   185 				CKey* agreedKeyBob1 = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
       
   186 				CleanupStack::PushL(agreedKeyBob1);
       
   187 
       
   188 				/*
       
   189 				 * compare the agreed keys
       
   190 				 */
       
   191 				const TInteger& agreedKeyDataAlice1 = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
       
   192 				const TInteger& agreedKeyDataBob1 = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
       
   193 				
       
   194 				if (agreedKeyDataAlice1 != agreedKeyDataBob1)
       
   195 					{					
       
   196 					INFO_PRINTF1(_L("*** FAIL : Second Agreed Keys Mismatch ***"));
       
   197 					SetTestStepResult(EFail);
       
   198 					}	
       
   199 							
       
   200 				const CCryptoParams& cryptoParams1 = agreedKeyAlice1->KeyParameters();
       
   201 				const CCryptoParams& cryptoParams2 = agreedKeyBob1->KeyParameters();				
       
   202 				if (cryptoParams1.Count() != cryptoParams2.GetParams().Count())					
       
   203 					{
       
   204 					INFO_PRINTF1(_L("*** FAIL : Key Parameters' Count Mismatch ***"));
       
   205 					SetTestStepResult(EFail);
       
   206 					}
       
   207 				
       
   208 				TInt paramLength = 10;
       
   209 				HBufC16* buf = HBufC16::NewLC(paramLength);
       
   210 				TPtr16 ptr = buf->Des();
       
   211 				ptr.Copy(_L("DH_N"));
       
   212 				CCryptoParams* params = CCryptoParams::NewL();
       
   213 				params->AddL(*buf, KDhKeyParameterNUid);
       
   214 				if(!params->Count())
       
   215 					{
       
   216 					INFO_PRINTF1(_L("*** FAIL : Parameter construction with descriptor failed ***"));
       
   217 					SetTestStepResult(EFail);
       
   218 					}
       
   219 
       
   220 				if (agreedKeyAlice1->IsPresent(KSymmetricKeyParameterUid))
       
   221 					{
       
   222 					TRAPD(err, agreedKeyAlice1->GetTIntL(KSymmetricKeyParameterUid));
       
   223 					if(err == KErrNone)
       
   224 						{
       
   225 						INFO_PRINTF1(_L("*** FAIL : Expected Key Parameter Int Value Mismatch ***"));
       
   226 						SetTestStepResult(EFail);
       
   227 						}
       
   228 					}
       
   229 				
       
   230 				// Clear the second key agreement elements.
       
   231 				CleanupStack::PopAndDestroy(3, agreedKeyAlice1);
       
   232 				
       
   233 				/* 
       
   234 				 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
       
   235 				 */
       
   236 				CleanupStack::PopAndDestroy(agreedKeyBob);
       
   237 				CleanupStack::PopAndDestroy(agreedKeyAlice);
       
   238 				CleanupStack::PopAndDestroy(keyAgreementBobImpl);
       
   239 				CleanupStack::PopAndDestroy(keyAgreementAliceImpl);
       
   240 				CleanupStack::PopAndDestroy(keyBob);
       
   241 				CleanupStack::PopAndDestroy(keyAlice);
       
   242 				CleanupStack::PopAndDestroy(keyParameters);
       
   243 				CleanupStack::PopAndDestroy(keyPairGeneratorImpl);	
       
   244 				}
       
   245 		
       
   246 			CleanupStack::PopAndDestroy(&DH_G);
       
   247 			CleanupStack::PopAndDestroy(&DH_N_MinusTwo);
       
   248 			CleanupStack::PopAndDestroy(&DH_N);
       
   249 			}
       
   250 			
       
   251 		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
       
   252 
       
   253 		}
       
   254   	return TestStepResult();
       
   255 	}
       
   256 
       
   257 TVerdict CKeyExchangeSyncStep::doTestStepPostambleL()
       
   258 	{
       
   259 	return TestStepResult();
       
   260 	}