os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/dhkeypairgenimpl.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/dhkeypairgenimpl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,154 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2009 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 "dhkeypairgenimpl.h"
    1.23 +#include "pluginconfig.h"
    1.24 +#include "keypair.h"
    1.25 +
    1.26 +using namespace SoftwareCrypto;
    1.27 +using namespace CryptoSpi;
    1.28 +
    1.29 +/* CDHKeyPairGenImpl */
    1.30 +CDHKeyPairGenImpl::CDHKeyPairGenImpl()
    1.31 +	{
    1.32 +	
    1.33 +	}
    1.34 +
    1.35 +CDHKeyPairGenImpl::~CDHKeyPairGenImpl()
    1.36 +	{
    1.37 +	
    1.38 +	}
    1.39 +
    1.40 +CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewL(void)
    1.41 +	{
    1.42 +	CDHKeyPairGenImpl* self = CDHKeyPairGenImpl::NewLC();
    1.43 +	CleanupStack::Pop(self);
    1.44 +	return self;
    1.45 +	}
    1.46 +
    1.47 +CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewLC(void)
    1.48 +	{
    1.49 +	CDHKeyPairGenImpl* self = new(ELeave) CDHKeyPairGenImpl();
    1.50 +	CleanupStack::PushL(self);
    1.51 +	self->ConstructL();
    1.52 +	return self;
    1.53 +	}
    1.54 +
    1.55 +void CDHKeyPairGenImpl::ConstructL(void)
    1.56 +	{
    1.57 +	CKeyPairGenImpl::ConstructL();
    1.58 +	}
    1.59 +
    1.60 +CExtendedCharacteristics* CDHKeyPairGenImpl::CreateExtendedCharacteristicsL()
    1.61 +	{
    1.62 +	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
    1.63 +	// for exclusive use and are not CERTIFIED to be standards compliant.
    1.64 +	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
    1.65 +	}
    1.66 +
    1.67 +const CExtendedCharacteristics* CDHKeyPairGenImpl::GetExtendedCharacteristicsL()
    1.68 +	{
    1.69 +	return CDHKeyPairGenImpl::CreateExtendedCharacteristicsL();
    1.70 +	}
    1.71 +
    1.72 +TUid CDHKeyPairGenImpl::ImplementationUid() const
    1.73 +	{
    1.74 +	return KCryptoPluginDhKeyPairGenUid;
    1.75 +	}
    1.76 +
    1.77 +void CDHKeyPairGenImpl::Reset()
    1.78 +	{
    1.79 +	// does nothing in this plugin
    1.80 +	}
    1.81 +
    1.82 +void CDHKeyPairGenImpl::GenerateKeyPairL(TInt /*aKeySize*/, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
    1.83 +	{
    1.84 +	/*
    1.85 +	 * unpack the parameters, we're expecting the N and G parameters and if present the x parameter (aka private key)
    1.86 +	 */
    1.87 +	const TInteger& N = aKeyParameters.GetBigIntL(KDhKeyParameterNUid);
    1.88 +	const TInteger& G = aKeyParameters.GetBigIntL(KDhKeyParameterGUid);
    1.89 +
    1.90 +	/*
    1.91 +	 * do some sanity checking
    1.92 +	 */
    1.93 +	RInteger nminus2 = RInteger::NewL(N);
    1.94 +	CleanupStack::PushL(nminus2);
    1.95 +	--nminus2;
    1.96 +	--nminus2;
    1.97 +
    1.98 +	if ((G < TInteger::Two()) || (G > nminus2))
    1.99 +		{
   1.100 +		User::Leave(KErrArgument);
   1.101 +		}
   1.102 +
   1.103 +	/*
   1.104 +	 * has a private key x been supplied? if not then generate it
   1.105 +	 */
   1.106 +	RInteger x;
   1.107 +	if (aKeyParameters.IsPresent(KDhKeyParameterxUid))
   1.108 +		{
   1.109 +		x = RInteger::NewL(aKeyParameters.GetBigIntL(KDhKeyParameterxUid));
   1.110 +		}
   1.111 +		else
   1.112 +		{
   1.113 +		// find a random x | 1 <= x <= n-2
   1.114 +		x = RInteger::NewRandomL(TInteger::One(), nminus2);
   1.115 +		}
   1.116 +	CleanupClosePushL(x);
   1.117 +	/*
   1.118 +	 * generate the public key with X = G^(x) mod N
   1.119 +	 */
   1.120 +	RInteger X = TInteger::ModularExponentiateL(G, x, N);
   1.121 +	CleanupClosePushL(X);
   1.122 +
   1.123 +	/*
   1.124 +	 * create the keys parameters
   1.125 +	  */
   1.126 +	CCryptoParams* publicKeyParameters = CCryptoParams::NewLC();
   1.127 +	publicKeyParameters->AddL(X, KDhKeyParameterXUid);
   1.128 +	TKeyProperty publicKeyProperties = {KDHKeyPairGeneratorUid, 	KCryptoPluginDhKeyPairGenUid, 
   1.129 +									KDHPublicKeyUid, KNonEmbeddedKeyUid };
   1.130 +	CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
   1.131 +	privateKeyParameters->AddL(x, KDhKeyParameterxUid);
   1.132 +	TKeyProperty privateKeyProperties = {KDHKeyPairGeneratorUid, KCryptoPluginDhKeyPairGenUid,
   1.133 +									KDHPrivateKeyUid, KNonEmbeddedKeyUid };
   1.134 +
   1.135 +	/*
   1.136 +	 * create the public key
   1.137 +	 */
   1.138 +	CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
   1.139 +	CleanupStack::PushL(publicKey);
   1.140 +
   1.141 +	/*
   1.142 +	 * create the private key
   1.143 +	 */
   1.144 +	CKey* privateKey = CKey::NewL(privateKeyProperties, *privateKeyParameters);
   1.145 +	CleanupStack::PushL(privateKey);
   1.146 +
   1.147 +	/*
   1.148 +	 * create the key pair
   1.149 +	 */
   1.150 +	aKeyPair = CKeyPair::NewL(publicKey, privateKey);
   1.151 +	
   1.152 +	/* 
   1.153 +	 * cleanup stack - it should contain nminus2, x (if allocated here), X, publicKeyParameters, privateKeyParameters, publicKey and privateKey
   1.154 +	 */
   1.155 +	CleanupStack::Pop(2, publicKey);
   1.156 +	CleanupStack::PopAndDestroy(5, &nminus2);
   1.157 +	}