os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/dhkeypairgenimpl.cpp
First public contribution.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "dhkeypairgenimpl.h"
20 #include "pluginconfig.h"
23 using namespace SoftwareCrypto;
24 using namespace CryptoSpi;
26 /* CDHKeyPairGenImpl */
27 CDHKeyPairGenImpl::CDHKeyPairGenImpl()
32 CDHKeyPairGenImpl::~CDHKeyPairGenImpl()
37 CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewL(void)
39 CDHKeyPairGenImpl* self = CDHKeyPairGenImpl::NewLC();
40 CleanupStack::Pop(self);
44 CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewLC(void)
46 CDHKeyPairGenImpl* self = new(ELeave) CDHKeyPairGenImpl();
47 CleanupStack::PushL(self);
52 void CDHKeyPairGenImpl::ConstructL(void)
54 CKeyPairGenImpl::ConstructL();
57 CExtendedCharacteristics* CDHKeyPairGenImpl::CreateExtendedCharacteristicsL()
59 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
60 // for exclusive use and are not CERTIFIED to be standards compliant.
61 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
64 const CExtendedCharacteristics* CDHKeyPairGenImpl::GetExtendedCharacteristicsL()
66 return CDHKeyPairGenImpl::CreateExtendedCharacteristicsL();
69 TUid CDHKeyPairGenImpl::ImplementationUid() const
71 return KCryptoPluginDhKeyPairGenUid;
74 void CDHKeyPairGenImpl::Reset()
76 // does nothing in this plugin
79 void CDHKeyPairGenImpl::GenerateKeyPairL(TInt /*aKeySize*/, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
82 * unpack the parameters, we're expecting the N and G parameters and if present the x parameter (aka private key)
84 const TInteger& N = aKeyParameters.GetBigIntL(KDhKeyParameterNUid);
85 const TInteger& G = aKeyParameters.GetBigIntL(KDhKeyParameterGUid);
88 * do some sanity checking
90 RInteger nminus2 = RInteger::NewL(N);
91 CleanupStack::PushL(nminus2);
95 if ((G < TInteger::Two()) || (G > nminus2))
97 User::Leave(KErrArgument);
101 * has a private key x been supplied? if not then generate it
104 if (aKeyParameters.IsPresent(KDhKeyParameterxUid))
106 x = RInteger::NewL(aKeyParameters.GetBigIntL(KDhKeyParameterxUid));
110 // find a random x | 1 <= x <= n-2
111 x = RInteger::NewRandomL(TInteger::One(), nminus2);
113 CleanupClosePushL(x);
115 * generate the public key with X = G^(x) mod N
117 RInteger X = TInteger::ModularExponentiateL(G, x, N);
118 CleanupClosePushL(X);
121 * create the keys parameters
123 CCryptoParams* publicKeyParameters = CCryptoParams::NewLC();
124 publicKeyParameters->AddL(X, KDhKeyParameterXUid);
125 TKeyProperty publicKeyProperties = {KDHKeyPairGeneratorUid, KCryptoPluginDhKeyPairGenUid,
126 KDHPublicKeyUid, KNonEmbeddedKeyUid };
127 CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
128 privateKeyParameters->AddL(x, KDhKeyParameterxUid);
129 TKeyProperty privateKeyProperties = {KDHKeyPairGeneratorUid, KCryptoPluginDhKeyPairGenUid,
130 KDHPrivateKeyUid, KNonEmbeddedKeyUid };
133 * create the public key
135 CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
136 CleanupStack::PushL(publicKey);
139 * create the private key
141 CKey* privateKey = CKey::NewL(privateKeyProperties, *privateKeyParameters);
142 CleanupStack::PushL(privateKey);
145 * create the key pair
147 aKeyPair = CKeyPair::NewL(publicKey, privateKey);
150 * cleanup stack - it should contain nminus2, x (if allocated here), X, publicKeyParameters, privateKeyParameters, publicKey and privateKey
152 CleanupStack::Pop(2, publicKey);
153 CleanupStack::PopAndDestroy(5, &nminus2);