sl@0: /* sl@0: * Copyright (c) 2003-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: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "keyconverter.h" sl@0: #include sl@0: #include sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: CKeyAgreement* GetKeyAgreementCryptoInterfaceLC(TUid aKeyAgreementAlgoUid, sl@0: CKey& aPrivateKey, CCryptoParams* aParams) sl@0: { sl@0: CKeyAgreement* keyAgreementImpl = 0; sl@0: CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementImpl, sl@0: aKeyAgreementAlgoUid, aPrivateKey, sl@0: aParams); sl@0: CleanupStack::PushL(keyAgreementImpl); sl@0: return keyAgreementImpl; sl@0: } sl@0: sl@0: EXPORT_C CDH* CDH::NewL(const CDHPrivateKey& aPrivateKey) sl@0: { sl@0: CDH* self = CDH::NewLC(aPrivateKey); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDH* CDH::NewLC(const CDHPrivateKey& aPrivateKey) sl@0: { sl@0: CDH* self = new (ELeave) CDH(aPrivateKey); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C HBufC8* CDH::AgreeL(const CDHPublicKey& aPublicKey) const 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: if ((aPublicKey.N() != iPrivateKey.N()) || (aPublicKey.G() != iPrivateKey.G())) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: CKey* privateKey = KeyConverter::CreateKeyL(iPrivateKey); sl@0: CleanupStack::PushL(privateKey); sl@0: sl@0: /* sl@0: * package the common parameters N and G into a crypto params array sl@0: * we've already checked that both the private and public keys have the sl@0: * same N and G so we only need build this array once for both creating sl@0: * and calling the interface sl@0: */ sl@0: CCryptoParams* keyParameters = CCryptoParams::NewLC(); sl@0: keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid); sl@0: keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid); sl@0: sl@0: /* sl@0: * get a DH key agreement interface sl@0: */ sl@0: CKeyAgreement* keyAgreementImpl = GetKeyAgreementCryptoInterfaceLC(KDHAgreementUid, *privateKey, keyParameters); sl@0: sl@0: /* sl@0: * call the api to get a DH agreed key sl@0: */ sl@0: CKey* publicKey = KeyConverter::CreateKeyL(aPublicKey); sl@0: CleanupStack::PushL(publicKey); sl@0: sl@0: CKey* agreedKey = keyAgreementImpl->AgreeL(*publicKey, keyParameters); sl@0: CleanupStack::PushL(agreedKey); sl@0: sl@0: /* sl@0: * extract the agreed key sl@0: */ sl@0: const TInteger& agreedKeyData = agreedKey->GetBigIntL(KSymmetricKeyParameterUid); sl@0: HBufC8 *agreedKeyToReturn = agreedKeyData.BufferLC(); sl@0: CleanupStack::Pop(agreedKeyToReturn); sl@0: sl@0: /* sl@0: * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey sl@0: */ sl@0: CleanupStack::PopAndDestroy(5, privateKey); sl@0: sl@0: return agreedKeyToReturn; sl@0: } sl@0: sl@0: EXPORT_C CDH::CDH(const CDHPrivateKey& aPrivateKey) : iPrivateKey(aPrivateKey) sl@0: { sl@0: }