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 "dhkeypairshim.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: sl@0: /* CDHKeyPair */ sl@0: CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG) sl@0: { sl@0: CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aN, aG); sl@0: return self; sl@0: } sl@0: sl@0: CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG, RInteger& ax) sl@0: { sl@0: CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aN, aG, ax); sl@0: return self; sl@0: } sl@0: sl@0: CDHKeyPairShim::~CDHKeyPairShim(void) sl@0: { sl@0: } sl@0: sl@0: CDHKeyPairShim::CDHKeyPairShim(void) sl@0: { sl@0: } sl@0: sl@0: void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG) sl@0: { sl@0: RInteger x = RInteger::NewL(); sl@0: CleanupClosePushL(x); sl@0: KeyConstructorL(aN, aG, x, EFalse); sl@0: CleanupStack::PopAndDestroy(1, &x); sl@0: } sl@0: sl@0: void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax) sl@0: { sl@0: KeyConstructorL(aN, aG, ax, ETrue); sl@0: } sl@0: sl@0: void CDHKeyPairShim::KeyConstructorL(RInteger& aN, RInteger& aG, RInteger& ax, TBool xIncluded) sl@0: { sl@0: RInteger& nminus2 = aN; sl@0: sl@0: /* sl@0: * do some sanity checks sl@0: */ sl@0: --nminus2; sl@0: --nminus2; sl@0: if( aG < TInteger::Two() || aG > nminus2 ) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: if (xIncluded) sl@0: { sl@0: if( ax < TInteger::One() || ax > nminus2 ) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: *find out how big the key should be - the key must be in the range x | 1 <= x <= n-2 sl@0: * nminus2 is the largest the key can be so get the number of bits required to represent that number sl@0: */ sl@0: const TInt keySize = nminus2.BitCount(); sl@0: sl@0: // increment aN back to its original value sl@0: ++nminus2; sl@0: ++nminus2; sl@0: sl@0: // obtain an RSA key pair generator interface sl@0: sl@0: CKeyPairGenerator* keyPairGeneratorImpl=NULL; sl@0: CKeyPairGeneratorFactory::CreateKeyPairGeneratorL( sl@0: keyPairGeneratorImpl, sl@0: KDHKeyPairGeneratorUid, sl@0: NULL); sl@0: CleanupStack::PushL(keyPairGeneratorImpl); sl@0: sl@0: /* sl@0: * put the DH parameters into an array sl@0: */ sl@0: CCryptoParams* keyParameters = CCryptoParams::NewLC(); sl@0: keyParameters->AddL(aN, KDhKeyParameterNUid); sl@0: keyParameters->AddL(aG, KDhKeyParameterGUid); sl@0: if (xIncluded) sl@0: { sl@0: // the private key x has been supplied so add it to the params array so the key generator algo can use it sl@0: keyParameters->AddL(ax, KDhKeyParameterxUid); sl@0: ax.Close(); sl@0: } sl@0: sl@0: /* sl@0: * call the api to create a DH key pair sl@0: */ sl@0: CKeyPair* keyPair = 0; sl@0: keyPairGeneratorImpl->GenerateKeyPairL(keySize, *keyParameters, keyPair); sl@0: CleanupStack::PushL(keyPair); sl@0: sl@0: /* sl@0: * for compatibility convert the CKeyPair to CDHPrivateKey and CDHPublicKey sl@0: */ sl@0: sl@0: // create new RInteger copies of aN, aG and x so the private key can own them sl@0: RInteger NCopy = RInteger::NewL(aN); sl@0: CleanupClosePushL(NCopy); sl@0: RInteger GCopy = RInteger::NewL(aG); sl@0: CleanupClosePushL(GCopy); sl@0: RInteger x = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KDhKeyParameterxUid)); sl@0: CleanupClosePushL(x); sl@0: iPrivate = CDHPrivateKey::NewL(NCopy, GCopy, x); sl@0: CleanupStack::Pop(3, &NCopy); sl@0: sl@0: // the public key becomes the owner of aN, aG and X sl@0: RInteger X = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KDhKeyParameterXUid)); sl@0: CleanupClosePushL(X); sl@0: iPublic = CDHPublicKey::NewL(aN, aG, X); sl@0: CleanupStack::Pop(&X); sl@0: sl@0: /* sl@0: * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, X, NCopy, GCopy and x sl@0: */ sl@0: CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl); sl@0: }