First public contribution.
2 * Copyright (c) 2006-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.
20 #include "pluginconfig.h"
22 using namespace SoftwareCrypto;
23 using namespace CryptoSpi;
34 CDHImpl* CDHImpl::NewL(const CKey& aPrivateKey, const CCryptoParams* aParams)
36 CDHImpl* self = CDHImpl::NewLC(aPrivateKey, aParams);
37 CleanupStack::Pop(self);
41 CDHImpl* CDHImpl::NewLC(const CKey& aPrivateKey, const CCryptoParams* aParams)
43 CDHImpl* self = new(ELeave) CDHImpl();
44 CleanupStack::PushL(self);
45 self->ConstructL(aPrivateKey, aParams);
49 CKey* CDHImpl::AgreeL(const CKey& aOtherPublicKey, const CCryptoParams* aParams)
52 * unpack the parameters, we're expecting the N and G parameters
54 const TInteger& N = aParams->GetBigIntL(KDhKeyParameterNUid);
55 const TInteger& G = aParams->GetBigIntL(KDhKeyParameterGUid);
56 const TInteger& privateN = iSharedParams->GetBigIntL(KDhKeyParameterNUid);
57 const TInteger& privateG = iSharedParams->GetBigIntL(KDhKeyParameterGUid);
58 const TInteger& X = aOtherPublicKey.GetBigIntL(KDhKeyParameterXUid);
59 const TInteger& x = iPrivateKey->GetBigIntL(KDhKeyParameterxUid);
62 * both DH keys (ie our private and their public keys) must use the same N and G parameters
64 if ((N != privateN) || (G != privateG))
66 User::Leave(KErrArgument);
70 * do the key agreement algo X ^ x mod N
72 RInteger result = TInteger::ModularExponentiateL(X, x, N);
73 CleanupClosePushL(result);
76 * create the agreed key
78 CCryptoParams* agreedKeyParameters = CCryptoParams::NewLC();
79 agreedKeyParameters->AddL(result, KSymmetricKeyParameterUid);
80 TKeyProperty agreedKeyProperties = {KDHAgreementUid, KCryptoPluginDhKeyAgreementUid,
81 KDHAgreedKeyUid, KNonEmbeddedKeyUid };
82 CKey* agreedKey = CKey::NewL(agreedKeyProperties, *agreedKeyParameters);
84 // cleanup result, agreedKeyParameters
85 CleanupStack::PopAndDestroy(2, &result);
89 TUid CDHImpl::ImplementationUid() const
91 return KCryptoPluginDhKeyAgreementUid;
94 CExtendedCharacteristics* CDHImpl::CreateExtendedCharacteristicsL()
96 // All Symbian software plug-ins have unlimited concurrency, cannot be
97 // reserved for exclusive use and are not CERTIFIED to be standards compliant.
98 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
101 const CExtendedCharacteristics* CDHImpl::GetExtendedCharacteristicsL()
103 return CDHImpl::CreateExtendedCharacteristicsL();