os/security/crypto/weakcryptospi/source/asymmetric/dh.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/source/asymmetric/dh.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,107 @@
     1.4 +/*
     1.5 +* Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <asymmetric.h>
    1.23 +#include <asymmetrickeys.h>
    1.24 +#include <cryptospi/keys.h>
    1.25 +#include "keyconverter.h"
    1.26 +#include <cryptospi/cryptokeyagreementapi.h>
    1.27 +#include <cryptospi/cryptospidef.h>
    1.28 +
    1.29 +using namespace CryptoSpi;
    1.30 +
    1.31 +CKeyAgreement* GetKeyAgreementCryptoInterfaceLC(TUid aKeyAgreementAlgoUid,
    1.32 +	  CKey& aPrivateKey, CCryptoParams* aParams)
    1.33 +	{
    1.34 +	CKeyAgreement* keyAgreementImpl =  0;
    1.35 +	CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementImpl,
    1.36 +												aKeyAgreementAlgoUid, aPrivateKey,
    1.37 +												aParams);
    1.38 +	CleanupStack::PushL(keyAgreementImpl);
    1.39 +	return keyAgreementImpl;
    1.40 +	}
    1.41 +
    1.42 +EXPORT_C CDH* CDH::NewL(const CDHPrivateKey& aPrivateKey)
    1.43 +	{
    1.44 +	CDH* self = CDH::NewLC(aPrivateKey);
    1.45 +	CleanupStack::Pop(self);
    1.46 +	return self;
    1.47 +	}
    1.48 +
    1.49 +EXPORT_C CDH* CDH::NewLC(const CDHPrivateKey& aPrivateKey)
    1.50 +	{
    1.51 +	CDH* self = new (ELeave) CDH(aPrivateKey);
    1.52 +	CleanupStack::PushL(self);
    1.53 +	return self;
    1.54 +	}
    1.55 +
    1.56 +EXPORT_C HBufC8* CDH::AgreeL(const CDHPublicKey& aPublicKey) const
    1.57 +	{
    1.58 +	/*
    1.59 +	 * both DH keys (ie our private and their public keys) must use the same N and G parameters
    1.60 +	 */
    1.61 +	if ((aPublicKey.N() != iPrivateKey.N()) || (aPublicKey.G() != iPrivateKey.G()))
    1.62 +		{
    1.63 +		User::Leave(KErrArgument);
    1.64 +		}
    1.65 +
    1.66 +	CKey* privateKey = KeyConverter::CreateKeyL(iPrivateKey);
    1.67 +	CleanupStack::PushL(privateKey);
    1.68 +
    1.69 +	/*
    1.70 +	 *  package the common parameters N and G into a crypto params array
    1.71 +	 * we've already checked that both the private and public keys have the 
    1.72 +	 * same N and G so we only need build this array once for both creating 
    1.73 +	 * and calling the interface
    1.74 +	 */
    1.75 +	CCryptoParams* keyParameters = CCryptoParams::NewLC();
    1.76 +	keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid);
    1.77 +	keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid);
    1.78 +
    1.79 +	/* 
    1.80 +	 * get a DH key agreement interface
    1.81 +	 */
    1.82 +	CKeyAgreement* keyAgreementImpl =  GetKeyAgreementCryptoInterfaceLC(KDHAgreementUid, *privateKey, keyParameters);
    1.83 +
    1.84 +	/* 
    1.85 +	 * call the api to get a DH agreed key
    1.86 +	 */
    1.87 +	CKey* publicKey = KeyConverter::CreateKeyL(aPublicKey);
    1.88 +	CleanupStack::PushL(publicKey);
    1.89 +
    1.90 +	CKey* agreedKey = keyAgreementImpl->AgreeL(*publicKey, keyParameters);
    1.91 +	CleanupStack::PushL(agreedKey);
    1.92 +
    1.93 +	/*
    1.94 +	 * extract the agreed key
    1.95 +	 */
    1.96 +	const TInteger& agreedKeyData = agreedKey->GetBigIntL(KSymmetricKeyParameterUid);
    1.97 +	HBufC8 *agreedKeyToReturn = agreedKeyData.BufferLC();
    1.98 +	CleanupStack::Pop(agreedKeyToReturn);
    1.99 +
   1.100 +	/* 
   1.101 +	 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
   1.102 +	 */
   1.103 +	CleanupStack::PopAndDestroy(5, privateKey);
   1.104 +
   1.105 +	return agreedKeyToReturn;
   1.106 +	}
   1.107 +
   1.108 +EXPORT_C CDH::CDH(const CDHPrivateKey& aPrivateKey) : iPrivateKey(aPrivateKey)
   1.109 +	{
   1.110 +	}