sl@0: /* sl@0: * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Example CTestStep derived implementation sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: #include "keyexchangesyncstep.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: CKeyExchangeSyncStep::~CKeyExchangeSyncStep() sl@0: { sl@0: } sl@0: sl@0: CKeyExchangeSyncStep::CKeyExchangeSyncStep() sl@0: { sl@0: SetTestStepName(KKeyExchangeSyncStep); sl@0: } sl@0: sl@0: TVerdict CKeyExchangeSyncStep::doTestStepPreambleL() sl@0: { sl@0: SetTestStepResult(EPass); sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: TVerdict CKeyExchangeSyncStep::doTestStepL() sl@0: { sl@0: INFO_PRINTF1(_L("*** Key Exchange - DH Key Agreement ***")); sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: sl@0: if (TestStepResult()==EPass) sl@0: { sl@0: //Assume faliure, unless all is successful sl@0: SetTestStepResult(EFail); sl@0: sl@0: TVariantPtrC testVariant; sl@0: TVariantPtrC dhnVariant; sl@0: TVariantPtrC dhgVariant; sl@0: sl@0: if( !GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhnVariant) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigExchangeKey, dhgVariant ) sl@0: ) sl@0: { sl@0: // Leave if there's any error. sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: else sl@0: { sl@0: /* sl@0: * both DH keys (ie our private and their public keys) must use the same N and G parameters sl@0: */ sl@0: INFO_PRINTF1(_L("Creating Primes and Base Integers...")); sl@0: sl@0: RInteger DH_N = RInteger::NewPrimeL(1024); // from ini file sl@0: CleanupClosePushL(DH_N); sl@0: RInteger DH_N_MinusTwo = RInteger::NewL(DH_N); sl@0: CleanupClosePushL(DH_N_MinusTwo); sl@0: DH_N_MinusTwo-=2; sl@0: sl@0: RInteger DH_G = RInteger::NewRandomL(TInteger::Two(), DH_N_MinusTwo); sl@0: CleanupClosePushL(DH_G); sl@0: sl@0: INFO_PRINTF1(_L("Creating Key Pair Generator...")); sl@0: sl@0: // create a DH key pair generator interface for creating the 2 key pairs sl@0: CKeyPairGenerator* keyPairGeneratorImpl = NULL; sl@0: sl@0: TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keyPairGeneratorImpl, sl@0: KDHKeyPairGeneratorUid, sl@0: NULL)); sl@0: sl@0: if(keyPairGeneratorImpl && (err==KErrNone)) sl@0: { sl@0: sl@0: CleanupStack::PushL(keyPairGeneratorImpl); sl@0: sl@0: // package up the common parameters N and G for use through the rest of this method sl@0: CCryptoParams* keyParameters = CCryptoParams::NewLC(); sl@0: keyParameters->AddL(DH_N, KDhKeyParameterNUid); sl@0: keyParameters->AddL(DH_G, KDhKeyParameterGUid); sl@0: sl@0: /* sl@0: * call the api to create a DH key pair for alice sl@0: */ sl@0: INFO_PRINTF1(_L("Generating DH ALICE Key Pair...")); sl@0: CKeyPair* keyAlice = NULL; sl@0: sl@0: TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL, sl@0: *keyParameters, sl@0: keyAlice)); sl@0: CleanupStack::PushL(keyAlice); sl@0: sl@0: /* sl@0: * call the api to create a DH key pair for bob sl@0: */ sl@0: INFO_PRINTF1(_L("Generating DH BOB Key Pair...")); sl@0: CKeyPair* keyBob = NULL; sl@0: TRAP_LOG(err,keyPairGeneratorImpl->GenerateKeyPairL(NULL, sl@0: *keyParameters, sl@0: keyBob)); sl@0: CleanupStack::PushL(keyBob); sl@0: sl@0: /* sl@0: * get DH key agreement interfaces sl@0: */ sl@0: INFO_PRINTF1(_L("Generating ALICE & BOB Key Agreement Interfaces...")); sl@0: CKeyAgreement* keyAgreementAliceImpl = NULL; sl@0: sl@0: TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementAliceImpl, sl@0: KDHAgreementUid, sl@0: keyAlice->PrivateKey(), sl@0: keyParameters)); sl@0: CleanupStack::PushL(keyAgreementAliceImpl); sl@0: sl@0: CKeyAgreement* keyAgreementBobImpl = NULL; sl@0: sl@0: TRAP_LOG(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementBobImpl, sl@0: KDHAgreementUid, sl@0: keyBob->PrivateKey(), sl@0: keyParameters)); sl@0: CleanupStack::PushL(keyAgreementBobImpl); sl@0: sl@0: /* sl@0: * call the api to get a DH agreed keys sl@0: */ sl@0: INFO_PRINTF1(_L("Generating Agreed Keys...")); sl@0: sl@0: CKey* agreedKeyAlice = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters); sl@0: CleanupStack::PushL(agreedKeyAlice); sl@0: sl@0: CKey* agreedKeyBob = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters); sl@0: CleanupStack::PushL(agreedKeyBob); sl@0: sl@0: /* sl@0: * compare the agreed keys sl@0: */ sl@0: const TInteger& agreedKeyDataAlice = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid); sl@0: const TInteger& agreedKeyDataBob = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid); sl@0: sl@0: if (agreedKeyDataAlice == agreedKeyDataBob) sl@0: { sl@0: INFO_PRINTF1(_L("*** PASS : Key Agreement Success ***")); sl@0: SetTestStepResult(EPass); sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF1(_L("*** FAIL : Agreed Keys Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: // Set the private keys to check the new agreements. sl@0: keyAgreementAliceImpl->SetKeyL(keyAlice->PrivateKey(), keyParameters); sl@0: keyAgreementAliceImpl->SetKeyL(keyBob->PrivateKey(), keyParameters); sl@0: sl@0: /* sl@0: * call the api to get a DH agreed keys sl@0: */ sl@0: INFO_PRINTF1(_L("Generating Agreed Keys second time...")); sl@0: sl@0: CKey* agreedKeyAlice1 = keyAgreementAliceImpl->AgreeL(keyBob->PublicKey(), keyParameters); sl@0: CleanupStack::PushL(agreedKeyAlice1); sl@0: sl@0: CKey* agreedKeyBob1 = keyAgreementBobImpl->AgreeL(keyAlice->PublicKey(), keyParameters); sl@0: CleanupStack::PushL(agreedKeyBob1); sl@0: sl@0: /* sl@0: * compare the agreed keys sl@0: */ sl@0: const TInteger& agreedKeyDataAlice1 = agreedKeyAlice->GetBigIntL(KSymmetricKeyParameterUid); sl@0: const TInteger& agreedKeyDataBob1 = agreedKeyBob->GetBigIntL(KSymmetricKeyParameterUid); sl@0: sl@0: if (agreedKeyDataAlice1 != agreedKeyDataBob1) sl@0: { sl@0: INFO_PRINTF1(_L("*** FAIL : Second Agreed Keys Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: const CCryptoParams& cryptoParams1 = agreedKeyAlice1->KeyParameters(); sl@0: const CCryptoParams& cryptoParams2 = agreedKeyBob1->KeyParameters(); sl@0: if (cryptoParams1.Count() != cryptoParams2.GetParams().Count()) sl@0: { sl@0: INFO_PRINTF1(_L("*** FAIL : Key Parameters' Count Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: TInt paramLength = 10; sl@0: HBufC16* buf = HBufC16::NewLC(paramLength); sl@0: TPtr16 ptr = buf->Des(); sl@0: ptr.Copy(_L("DH_N")); sl@0: CCryptoParams* params = CCryptoParams::NewL(); sl@0: params->AddL(*buf, KDhKeyParameterNUid); sl@0: if(!params->Count()) sl@0: { sl@0: INFO_PRINTF1(_L("*** FAIL : Parameter construction with descriptor failed ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: sl@0: if (agreedKeyAlice1->IsPresent(KSymmetricKeyParameterUid)) sl@0: { sl@0: TRAPD(err, agreedKeyAlice1->GetTIntL(KSymmetricKeyParameterUid)); sl@0: if(err == KErrNone) sl@0: { sl@0: INFO_PRINTF1(_L("*** FAIL : Expected Key Parameter Int Value Mismatch ***")); sl@0: SetTestStepResult(EFail); sl@0: } sl@0: } sl@0: sl@0: // Clear the second key agreement elements. sl@0: CleanupStack::PopAndDestroy(3, agreedKeyAlice1); sl@0: sl@0: /* sl@0: * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey sl@0: */ sl@0: CleanupStack::PopAndDestroy(agreedKeyBob); sl@0: CleanupStack::PopAndDestroy(agreedKeyAlice); sl@0: CleanupStack::PopAndDestroy(keyAgreementBobImpl); sl@0: CleanupStack::PopAndDestroy(keyAgreementAliceImpl); sl@0: CleanupStack::PopAndDestroy(keyBob); sl@0: CleanupStack::PopAndDestroy(keyAlice); sl@0: CleanupStack::PopAndDestroy(keyParameters); sl@0: CleanupStack::PopAndDestroy(keyPairGeneratorImpl); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(&DH_G); sl@0: CleanupStack::PopAndDestroy(&DH_N_MinusTwo); sl@0: CleanupStack::PopAndDestroy(&DH_N); sl@0: } sl@0: sl@0: INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells()); sl@0: sl@0: } sl@0: return TestStepResult(); sl@0: } sl@0: sl@0: TVerdict CKeyExchangeSyncStep::doTestStepPostambleL() sl@0: { sl@0: return TestStepResult(); sl@0: }