Update contrib.
2 * Copyright (c) 2003-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.
19 #include <asymmetric.h>
20 #include <asymmetrickeys.h>
21 #include <cryptospi/keys.h>
22 #include "keyconverter.h"
23 #include <cryptospi/cryptokeyagreementapi.h>
24 #include <cryptospi/cryptospidef.h>
26 using namespace CryptoSpi;
28 CKeyAgreement* GetKeyAgreementCryptoInterfaceLC(TUid aKeyAgreementAlgoUid,
29 CKey& aPrivateKey, CCryptoParams* aParams)
31 CKeyAgreement* keyAgreementImpl = 0;
32 CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementImpl,
33 aKeyAgreementAlgoUid, aPrivateKey,
35 CleanupStack::PushL(keyAgreementImpl);
36 return keyAgreementImpl;
39 EXPORT_C CDH* CDH::NewL(const CDHPrivateKey& aPrivateKey)
41 CDH* self = CDH::NewLC(aPrivateKey);
42 CleanupStack::Pop(self);
46 EXPORT_C CDH* CDH::NewLC(const CDHPrivateKey& aPrivateKey)
48 CDH* self = new (ELeave) CDH(aPrivateKey);
49 CleanupStack::PushL(self);
53 EXPORT_C HBufC8* CDH::AgreeL(const CDHPublicKey& aPublicKey) const
56 * both DH keys (ie our private and their public keys) must use the same N and G parameters
58 if ((aPublicKey.N() != iPrivateKey.N()) || (aPublicKey.G() != iPrivateKey.G()))
60 User::Leave(KErrArgument);
63 CKey* privateKey = KeyConverter::CreateKeyL(iPrivateKey);
64 CleanupStack::PushL(privateKey);
67 * package the common parameters N and G into a crypto params array
68 * we've already checked that both the private and public keys have the
69 * same N and G so we only need build this array once for both creating
70 * and calling the interface
72 CCryptoParams* keyParameters = CCryptoParams::NewLC();
73 keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid);
74 keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid);
77 * get a DH key agreement interface
79 CKeyAgreement* keyAgreementImpl = GetKeyAgreementCryptoInterfaceLC(KDHAgreementUid, *privateKey, keyParameters);
82 * call the api to get a DH agreed key
84 CKey* publicKey = KeyConverter::CreateKeyL(aPublicKey);
85 CleanupStack::PushL(publicKey);
87 CKey* agreedKey = keyAgreementImpl->AgreeL(*publicKey, keyParameters);
88 CleanupStack::PushL(agreedKey);
91 * extract the agreed key
93 const TInteger& agreedKeyData = agreedKey->GetBigIntL(KSymmetricKeyParameterUid);
94 HBufC8 *agreedKeyToReturn = agreedKeyData.BufferLC();
95 CleanupStack::Pop(agreedKeyToReturn);
98 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
100 CleanupStack::PopAndDestroy(5, privateKey);
102 return agreedKeyToReturn;
105 EXPORT_C CDH::CDH(const CDHPrivateKey& aPrivateKey) : iPrivateKey(aPrivateKey)