sl@0: /* sl@0: * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "dhkeypairgenimpl.h" sl@0: #include "pluginconfig.h" sl@0: #include sl@0: sl@0: using namespace SoftwareCrypto; sl@0: using namespace CryptoSpi; sl@0: sl@0: /* CDHKeyPairGenImpl */ sl@0: CDHKeyPairGenImpl::CDHKeyPairGenImpl(TUid aImplementationUid) : CKeyPairGenImpl(aImplementationUid) sl@0: { sl@0: sl@0: } sl@0: sl@0: CDHKeyPairGenImpl::~CDHKeyPairGenImpl() sl@0: { sl@0: sl@0: } sl@0: sl@0: CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewL(TUid aImplementationUid) sl@0: { sl@0: CDHKeyPairGenImpl* self = CDHKeyPairGenImpl::NewLC(aImplementationUid); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewLC(TUid aImplementationUid) sl@0: { sl@0: CDHKeyPairGenImpl* self = new(ELeave) CDHKeyPairGenImpl(aImplementationUid); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: return self; sl@0: } sl@0: sl@0: void CDHKeyPairGenImpl::ConstructL(void) sl@0: { sl@0: CKeyPairGenImpl::ConstructL(); sl@0: } sl@0: sl@0: CExtendedCharacteristics* CDHKeyPairGenImpl::CreateExtendedCharacteristicsL() sl@0: { sl@0: // All Symbian software plug-ins have unlimited concurrency, cannot be reserved sl@0: // for exclusive use and are not CERTIFIED to be standards compliant. sl@0: return CExtendedCharacteristics::NewL(KMaxTInt, EFalse); sl@0: } sl@0: sl@0: const CExtendedCharacteristics* CDHKeyPairGenImpl::GetExtendedCharacteristicsL() sl@0: { sl@0: return CDHKeyPairGenImpl::CreateExtendedCharacteristicsL(); sl@0: } sl@0: sl@0: TUid CDHKeyPairGenImpl::ImplementationUid() const sl@0: { sl@0: return iImplementationUid; sl@0: } sl@0: sl@0: void CDHKeyPairGenImpl::Reset() sl@0: { sl@0: // does nothing in this plugin sl@0: } sl@0: sl@0: void CDHKeyPairGenImpl::GenerateKeyPairL(TInt /*aKeySize*/, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair) sl@0: { sl@0: /* sl@0: * unpack the parameters, we're expecting the N and G parameters and if present the x parameter (aka private key) sl@0: */ sl@0: const TInteger& N = aKeyParameters.GetBigIntL(KDhKeyParameterNUid); sl@0: const TInteger& G = aKeyParameters.GetBigIntL(KDhKeyParameterGUid); sl@0: sl@0: /* sl@0: * do some sanity checking sl@0: */ sl@0: RInteger nminus2 = RInteger::NewL(N); sl@0: CleanupStack::PushL(nminus2); sl@0: --nminus2; sl@0: --nminus2; sl@0: sl@0: if ((G < TInteger::Two()) || (G > nminus2)) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: /* sl@0: * has a private key x been supplied? if not then generate it sl@0: */ sl@0: RInteger x; sl@0: if (aKeyParameters.IsPresent(KDhKeyParameterxUid)) sl@0: { sl@0: x = RInteger::NewL(aKeyParameters.GetBigIntL(KDhKeyParameterxUid)); sl@0: } sl@0: else sl@0: { sl@0: // find a random x | 1 <= x <= n-2 sl@0: x = RInteger::NewRandomL(TInteger::One(), nminus2); sl@0: } sl@0: CleanupClosePushL(x); sl@0: /* sl@0: * generate the public key with X = G^(x) mod N sl@0: */ sl@0: RInteger X = TInteger::ModularExponentiateL(G, x, N); sl@0: CleanupClosePushL(X); sl@0: sl@0: /* sl@0: * create the keys parameters sl@0: */ sl@0: CCryptoParams* publicKeyParameters = CCryptoParams::NewLC(); sl@0: publicKeyParameters->AddL(X, KDhKeyParameterXUid); sl@0: TKeyProperty publicKeyProperties = {KDHKeyPairGeneratorUid, iImplementationUid, sl@0: KDHPublicKeyUid, KNonEmbeddedKeyUid }; sl@0: CCryptoParams* privateKeyParameters = CCryptoParams::NewLC(); sl@0: privateKeyParameters->AddL(x, KDhKeyParameterxUid); sl@0: TKeyProperty privateKeyProperties = {KDHKeyPairGeneratorUid, iImplementationUid, sl@0: KDHPrivateKeyUid, KNonEmbeddedKeyUid }; sl@0: sl@0: /* sl@0: * create the public key sl@0: */ sl@0: CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters); sl@0: CleanupStack::PushL(publicKey); sl@0: sl@0: /* sl@0: * create the private key sl@0: */ sl@0: CKey* privateKey = CKey::NewL(privateKeyProperties, *privateKeyParameters); sl@0: CleanupStack::PushL(privateKey); sl@0: sl@0: /* sl@0: * create the key pair sl@0: */ sl@0: aKeyPair = CKeyPair::NewL(publicKey, privateKey); sl@0: sl@0: /* sl@0: * cleanup stack - it should contain nminus2, x (if allocated here), X, publicKeyParameters, privateKeyParameters, publicKey and privateKey sl@0: */ sl@0: CleanupStack::Pop(2, publicKey); sl@0: CleanupStack::PopAndDestroy(5, &nminus2); sl@0: }