crypto/weakcryptospi/source/asymmetric/dh.cpp
changeset 19 cd501b96611d
child 43 2f10d260163b
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     1 /*
       
     2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <asymmetric.h>
       
    20 #include <asymmetrickeys.h>
       
    21 #include "keys.h"
       
    22 #include "keyconverter.h"
       
    23 #include "cryptokeyagreementapi.h"
       
    24 #include <cryptospi/cryptospidef.h>
       
    25 
       
    26 using namespace CryptoSpi;
       
    27 
       
    28 CKeyAgreement* GetKeyAgreementCryptoInterfaceLC(TUid aKeyAgreementAlgoUid,
       
    29 	  CKey& aPrivateKey, CCryptoParams* aParams)
       
    30 	{
       
    31 	CKeyAgreement* keyAgreementImpl =  0;
       
    32 	CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementImpl,
       
    33 												aKeyAgreementAlgoUid, aPrivateKey,
       
    34 												aParams);
       
    35 	CleanupStack::PushL(keyAgreementImpl);
       
    36 	return keyAgreementImpl;
       
    37 	}
       
    38 
       
    39 EXPORT_C CDH* CDH::NewL(const CDHPrivateKey& aPrivateKey)
       
    40 	{
       
    41 	CDH* self = CDH::NewLC(aPrivateKey);
       
    42 	CleanupStack::Pop(self);
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 EXPORT_C CDH* CDH::NewLC(const CDHPrivateKey& aPrivateKey)
       
    47 	{
       
    48 	CDH* self = new (ELeave) CDH(aPrivateKey);
       
    49 	CleanupStack::PushL(self);
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 EXPORT_C HBufC8* CDH::AgreeL(const CDHPublicKey& aPublicKey) const
       
    54 	{
       
    55 	/*
       
    56 	 * both DH keys (ie our private and their public keys) must use the same N and G parameters
       
    57 	 */
       
    58 	if ((aPublicKey.N() != iPrivateKey.N()) || (aPublicKey.G() != iPrivateKey.G()))
       
    59 		{
       
    60 		User::Leave(KErrArgument);
       
    61 		}
       
    62 
       
    63 	CKey* privateKey = KeyConverter::CreateKeyL(iPrivateKey);
       
    64 	CleanupStack::PushL(privateKey);
       
    65 
       
    66 	/*
       
    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
       
    71 	 */
       
    72 	CCryptoParams* keyParameters = CCryptoParams::NewLC();
       
    73 	keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid);
       
    74 	keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid);
       
    75 
       
    76 	/* 
       
    77 	 * get a DH key agreement interface
       
    78 	 */
       
    79 	CKeyAgreement* keyAgreementImpl =  GetKeyAgreementCryptoInterfaceLC(KDHAgreementUid, *privateKey, keyParameters);
       
    80 
       
    81 	/* 
       
    82 	 * call the api to get a DH agreed key
       
    83 	 */
       
    84 	CKey* publicKey = KeyConverter::CreateKeyL(aPublicKey);
       
    85 	CleanupStack::PushL(publicKey);
       
    86 
       
    87 	CKey* agreedKey = keyAgreementImpl->AgreeL(*publicKey, keyParameters);
       
    88 	CleanupStack::PushL(agreedKey);
       
    89 
       
    90 	/*
       
    91 	 * extract the agreed key
       
    92 	 */
       
    93 	const TInteger& agreedKeyData = agreedKey->GetBigIntL(KSymmetricKeyParameterUid);
       
    94 	HBufC8 *agreedKeyToReturn = agreedKeyData.BufferLC();
       
    95 	CleanupStack::Pop(agreedKeyToReturn);
       
    96 
       
    97 	/* 
       
    98 	 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
       
    99 	 */
       
   100 	CleanupStack::PopAndDestroy(5, privateKey);
       
   101 
       
   102 	return agreedKeyToReturn;
       
   103 	}
       
   104 
       
   105 EXPORT_C CDH::CDH(const CDHPrivateKey& aPrivateKey) : iPrivateKey(aPrivateKey)
       
   106 	{
       
   107 	}