Update contrib.
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Example CTestStep derived implementation
24 #include "keyexchangesyncstep.h"
26 #include <cryptospi/keypair.h>
27 #include <cryptospi/cryptokeypairgeneratorapi.h>
28 #include <cryptospi/cryptokeyagreementapi.h>
31 using namespace CryptoSpi;
33 CKeyExchangeSyncStep::~CKeyExchangeSyncStep()
37 CKeyExchangeSyncStep::CKeyExchangeSyncStep()
39 SetTestStepName(KKeyExchangeSyncStep);
42 TVerdict CKeyExchangeSyncStep::doTestStepPreambleL()
44 SetTestStepResult(EPass);
45 return TestStepResult();
48 TVerdict CKeyExchangeSyncStep::doTestStepL()
50 INFO_PRINTF1(_L("*** Key Exchange - DH Key Agreement ***"));
51 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
53 if (TestStepResult()==EPass)
55 //Assume faliure, unless all is successful
56 SetTestStepResult(EFail);
58 TVariantPtrC testVariant;
59 TVariantPtrC dhnVariant;
60 TVariantPtrC dhgVariant;
62 if( !GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhnVariant) ||
63 !GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhgVariant )
66 // Leave if there's any error.
67 User::Leave(KErrNotFound);
72 * both DH keys (ie our private and their public keys) must use the same N and G parameters
74 INFO_PRINTF1(_L("Creating Primes and Base Integers..."));
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);
82 RInteger DH_G = RInteger::NewRandomL(TInteger::Two(), DH_N_MinusTwo);
83 CleanupClosePushL(DH_G);
85 INFO_PRINTF1(_L("Creating Key Pair Generator..."));
87 // create a DH key pair generator interface for creating the 2 key pairs
88 CKeyPairGenerator* keyPairGeneratorImpl = NULL;
90 TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keyPairGeneratorImpl,
91 KDHKeyPairGeneratorUid,
94 if(keyPairGeneratorImpl && (err==KErrNone))
97 CleanupStack::PushL(keyPairGeneratorImpl);
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);
105 * call the api to create a DH key pair for alice
107 INFO_PRINTF1(_L("Generating DH ALICE Key Pair..."));
108 CKeyPair* keyAlice = NULL;
110 TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL,
113 CleanupStack::PushL(keyAlice);
116 * call the api to create a DH key pair for bob
118 INFO_PRINTF1(_L("Generating DH BOB Key Pair..."));
119 CKeyPair* keyBob = NULL;
120 TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL,
123 CleanupStack::PushL(keyBob);
126 * get DH key agreement interfaces
128 INFO_PRINTF1(_L("Generating ALICE & BOB Key Agreement Interfaces..."));
129 CKeyAgreement* keyAgreementAliceImpl = NULL;
131 TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementAliceImpl,
133 keyAlice->PrivateKey(),
135 CleanupStack::PushL(keyAgreementAliceImpl);
137 CKeyAgreement* keyAgreementBobImpl = NULL;
139 TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementBobImpl,
141 keyBob->PrivateKey(),
143 CleanupStack::PushL(keyAgreementBobImpl);
146 * call the api to get a DH agreed keys
148 INFO_PRINTF1(_L("Generating Agreed Keys..."));
150 CKey* agreedKeyAlice = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
151 CleanupStack::PushL(agreedKeyAlice);
153 CKey* agreedKeyBob = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
154 CleanupStack::PushL(agreedKeyBob);
157 * compare the agreed keys
159 const TInteger& agreedKeyDataAlice = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
160 const TInteger& agreedKeyDataBob = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
162 if (agreedKeyDataAlice == agreedKeyDataBob)
164 INFO_PRINTF1(_L("*** PASS : Key Agreement Success ***"));
165 SetTestStepResult(EPass);
169 INFO_PRINTF1(_L("*** FAIL : Agreed Keys Mismatch ***"));
170 SetTestStepResult(EFail);
173 // Set the private keys to check the new agreements.
174 keyAgreementAliceImpl->SetKeyL(keyAlice->PrivateKey(), keyParameters);
175 keyAgreementAliceImpl->SetKeyL(keyBob->PrivateKey(), keyParameters);
178 * call the api to get a DH agreed keys
180 INFO_PRINTF1(_L("Generating Agreed Keys second time..."));
182 CKey* agreedKeyAlice1 = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters);
183 CleanupStack::PushL(agreedKeyAlice1);
185 CKey* agreedKeyBob1 = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters);
186 CleanupStack::PushL(agreedKeyBob1);
189 * compare the agreed keys
191 const TInteger& agreedKeyDataAlice1 = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid);
192 const TInteger& agreedKeyDataBob1 = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid);
194 if (agreedKeyDataAlice1 != agreedKeyDataBob1)
196 INFO_PRINTF1(_L("*** FAIL : Second Agreed Keys Mismatch ***"));
197 SetTestStepResult(EFail);
200 const CCryptoParams& cryptoParams1 = agreedKeyAlice1->KeyParameters();
201 const CCryptoParams& cryptoParams2 = agreedKeyBob1->KeyParameters();
202 if (cryptoParams1.Count() != cryptoParams2.GetParams().Count())
204 INFO_PRINTF1(_L("*** FAIL : Key Parameters' Count Mismatch ***"));
205 SetTestStepResult(EFail);
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);
216 INFO_PRINTF1(_L("*** FAIL : Parameter construction with descriptor failed ***"));
217 SetTestStepResult(EFail);
220 if (agreedKeyAlice1->IsPresent(KSymmetricKeyParameterUid))
222 TRAPD(err, agreedKeyAlice1->GetTIntL(KSymmetricKeyParameterUid));
225 INFO_PRINTF1(_L("*** FAIL : Expected Key Parameter Int Value Mismatch ***"));
226 SetTestStepResult(EFail);
230 // Clear the second key agreement elements.
231 CleanupStack::PopAndDestroy(3, agreedKeyAlice1);
234 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
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);
246 CleanupStack::PopAndDestroy(&DH_G);
247 CleanupStack::PopAndDestroy(&DH_N_MinusTwo);
248 CleanupStack::PopAndDestroy(&DH_N);
251 INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
254 return TestStepResult();
257 TVerdict CKeyExchangeSyncStep::doTestStepPostambleL()
259 return TestStepResult();