os/security/crypto/weakcryptospi/source/asymmetric/dh.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <asymmetric.h>
sl@0
    20
#include <asymmetrickeys.h>
sl@0
    21
#include <cryptospi/keys.h>
sl@0
    22
#include "keyconverter.h"
sl@0
    23
#include <cryptospi/cryptokeyagreementapi.h>
sl@0
    24
#include <cryptospi/cryptospidef.h>
sl@0
    25
sl@0
    26
using namespace CryptoSpi;
sl@0
    27
sl@0
    28
CKeyAgreement* GetKeyAgreementCryptoInterfaceLC(TUid aKeyAgreementAlgoUid,
sl@0
    29
	  CKey& aPrivateKey, CCryptoParams* aParams)
sl@0
    30
	{
sl@0
    31
	CKeyAgreement* keyAgreementImpl =  0;
sl@0
    32
	CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementImpl,
sl@0
    33
												aKeyAgreementAlgoUid, aPrivateKey,
sl@0
    34
												aParams);
sl@0
    35
	CleanupStack::PushL(keyAgreementImpl);
sl@0
    36
	return keyAgreementImpl;
sl@0
    37
	}
sl@0
    38
sl@0
    39
EXPORT_C CDH* CDH::NewL(const CDHPrivateKey& aPrivateKey)
sl@0
    40
	{
sl@0
    41
	CDH* self = CDH::NewLC(aPrivateKey);
sl@0
    42
	CleanupStack::Pop(self);
sl@0
    43
	return self;
sl@0
    44
	}
sl@0
    45
sl@0
    46
EXPORT_C CDH* CDH::NewLC(const CDHPrivateKey& aPrivateKey)
sl@0
    47
	{
sl@0
    48
	CDH* self = new (ELeave) CDH(aPrivateKey);
sl@0
    49
	CleanupStack::PushL(self);
sl@0
    50
	return self;
sl@0
    51
	}
sl@0
    52
sl@0
    53
EXPORT_C HBufC8* CDH::AgreeL(const CDHPublicKey& aPublicKey) const
sl@0
    54
	{
sl@0
    55
	/*
sl@0
    56
	 * both DH keys (ie our private and their public keys) must use the same N and G parameters
sl@0
    57
	 */
sl@0
    58
	if ((aPublicKey.N() != iPrivateKey.N()) || (aPublicKey.G() != iPrivateKey.G()))
sl@0
    59
		{
sl@0
    60
		User::Leave(KErrArgument);
sl@0
    61
		}
sl@0
    62
sl@0
    63
	CKey* privateKey = KeyConverter::CreateKeyL(iPrivateKey);
sl@0
    64
	CleanupStack::PushL(privateKey);
sl@0
    65
sl@0
    66
	/*
sl@0
    67
	 *  package the common parameters N and G into a crypto params array
sl@0
    68
	 * we've already checked that both the private and public keys have the 
sl@0
    69
	 * same N and G so we only need build this array once for both creating 
sl@0
    70
	 * and calling the interface
sl@0
    71
	 */
sl@0
    72
	CCryptoParams* keyParameters = CCryptoParams::NewLC();
sl@0
    73
	keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid);
sl@0
    74
	keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid);
sl@0
    75
sl@0
    76
	/* 
sl@0
    77
	 * get a DH key agreement interface
sl@0
    78
	 */
sl@0
    79
	CKeyAgreement* keyAgreementImpl =  GetKeyAgreementCryptoInterfaceLC(KDHAgreementUid, *privateKey, keyParameters);
sl@0
    80
sl@0
    81
	/* 
sl@0
    82
	 * call the api to get a DH agreed key
sl@0
    83
	 */
sl@0
    84
	CKey* publicKey = KeyConverter::CreateKeyL(aPublicKey);
sl@0
    85
	CleanupStack::PushL(publicKey);
sl@0
    86
sl@0
    87
	CKey* agreedKey = keyAgreementImpl->AgreeL(*publicKey, keyParameters);
sl@0
    88
	CleanupStack::PushL(agreedKey);
sl@0
    89
sl@0
    90
	/*
sl@0
    91
	 * extract the agreed key
sl@0
    92
	 */
sl@0
    93
	const TInteger& agreedKeyData = agreedKey->GetBigIntL(KSymmetricKeyParameterUid);
sl@0
    94
	HBufC8 *agreedKeyToReturn = agreedKeyData.BufferLC();
sl@0
    95
	CleanupStack::Pop(agreedKeyToReturn);
sl@0
    96
sl@0
    97
	/* 
sl@0
    98
	 * cleanup stack - it should contain privateKey, keyAgreementImpl, publicKey, keyParameters and agreedKey
sl@0
    99
	 */
sl@0
   100
	CleanupStack::PopAndDestroy(5, privateKey);
sl@0
   101
sl@0
   102
	return agreedKeyToReturn;
sl@0
   103
	}
sl@0
   104
sl@0
   105
EXPORT_C CDH::CDH(const CDHPrivateKey& aPrivateKey) : iPrivateKey(aPrivateKey)
sl@0
   106
	{
sl@0
   107
	}