First public contribution.
2 * Copyright (c) 2007-2010 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 "dhkeypairshim.h"
21 #include <cryptospi/cryptokeypairgeneratorapi.h>
22 #include <cryptospi/keypair.h>
23 #include <cryptospi/cryptoparams.h>
24 #include <cryptospi/cryptospidef.h>
27 using namespace CryptoSpi;
31 CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG)
33 CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
34 CleanupStack::PushL(self);
35 self->ConstructL(aN, aG);
39 CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
41 CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
42 CleanupStack::PushL(self);
43 self->ConstructL(aN, aG, ax);
47 CDHKeyPairShim::~CDHKeyPairShim(void)
51 CDHKeyPairShim::CDHKeyPairShim(void)
55 void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG)
57 RInteger x = RInteger::NewL();
59 KeyConstructorL(aN, aG, x, EFalse);
60 CleanupStack::PopAndDestroy(1, &x);
63 void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
65 KeyConstructorL(aN, aG, ax, ETrue);
68 void CDHKeyPairShim::KeyConstructorL(RInteger& aN, RInteger& aG, RInteger& ax, TBool xIncluded)
70 RInteger& nminus2 = aN;
73 * do some sanity checks
77 if( aG < TInteger::Two() || aG > nminus2 )
79 User::Leave(KErrArgument);
84 if( ax < TInteger::One() || ax > nminus2 )
86 User::Leave(KErrArgument);
91 *find out how big the key should be - the key must be in the range x | 1 <= x <= n-2
92 * nminus2 is the largest the key can be so get the number of bits required to represent that number
94 const TInt keySize = nminus2.BitCount();
96 // increment aN back to its original value
100 // obtain an RSA key pair generator interface
102 CKeyPairGenerator* keyPairGeneratorImpl=NULL;
103 CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
104 keyPairGeneratorImpl,
105 KDHKeyPairGeneratorUid,
107 CleanupStack::PushL(keyPairGeneratorImpl);
110 * put the DH parameters into an array
112 CCryptoParams* keyParameters = CCryptoParams::NewLC();
113 keyParameters->AddL(aN, KDhKeyParameterNUid);
114 keyParameters->AddL(aG, KDhKeyParameterGUid);
117 // the private key x has been supplied so add it to the params array so the key generator algo can use it
118 keyParameters->AddL(ax, KDhKeyParameterxUid);
123 * call the api to create a DH key pair
125 CKeyPair* keyPair = 0;
126 keyPairGeneratorImpl->GenerateKeyPairL(keySize, *keyParameters, keyPair);
127 CleanupStack::PushL(keyPair);
130 * for compatibility convert the CKeyPair to CDHPrivateKey and CDHPublicKey
133 // create new RInteger copies of aN, aG and x so the private key can own them
134 RInteger NCopy = RInteger::NewL(aN);
135 CleanupClosePushL(NCopy);
136 RInteger GCopy = RInteger::NewL(aG);
137 CleanupClosePushL(GCopy);
138 RInteger x = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KDhKeyParameterxUid));
139 CleanupClosePushL(x);
140 iPrivate = CDHPrivateKey::NewL(NCopy, GCopy, x);
141 CleanupStack::Pop(3, &NCopy);
143 // the public key becomes the owner of aN, aG and X
144 RInteger X = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KDhKeyParameterXUid));
145 CleanupClosePushL(X);
146 iPublic = CDHPublicKey::NewL(aN, aG, X);
147 CleanupStack::Pop(&X);
150 * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, X, NCopy, GCopy and x
152 CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);