17
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
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".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "dhkeypairshim.h"
|
|
20 |
#include <bigint.h>
|
|
21 |
#include "cryptokeypairgeneratorapi.h"
|
|
22 |
#include "keypair.h"
|
|
23 |
#include <cryptospi/cryptoparams.h>
|
|
24 |
#include <cryptospi/cryptospidef.h>
|
|
25 |
|
|
26 |
|
|
27 |
using namespace CryptoSpi;
|
|
28 |
|
|
29 |
|
|
30 |
/* CDHKeyPair */
|
|
31 |
CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG)
|
|
32 |
{
|
|
33 |
CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
|
|
34 |
CleanupStack::PushL(self);
|
|
35 |
self->ConstructL(aN, aG);
|
|
36 |
return self;
|
|
37 |
}
|
|
38 |
|
|
39 |
CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
|
|
40 |
{
|
|
41 |
CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
|
|
42 |
CleanupStack::PushL(self);
|
|
43 |
self->ConstructL(aN, aG, ax);
|
|
44 |
return self;
|
|
45 |
}
|
|
46 |
|
|
47 |
CDHKeyPairShim::~CDHKeyPairShim(void)
|
|
48 |
{
|
|
49 |
}
|
|
50 |
|
|
51 |
CDHKeyPairShim::CDHKeyPairShim(void)
|
|
52 |
{
|
|
53 |
}
|
|
54 |
|
|
55 |
void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG)
|
|
56 |
{
|
|
57 |
RInteger x = RInteger::NewL();
|
|
58 |
CleanupClosePushL(x);
|
|
59 |
KeyConstructorL(aN, aG, x, EFalse);
|
|
60 |
CleanupStack::PopAndDestroy(1, &x);
|
|
61 |
}
|
|
62 |
|
|
63 |
void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
|
|
64 |
{
|
|
65 |
KeyConstructorL(aN, aG, ax, ETrue);
|
|
66 |
}
|
|
67 |
|
|
68 |
void CDHKeyPairShim::KeyConstructorL(RInteger& aN, RInteger& aG, RInteger& ax, TBool xIncluded)
|
|
69 |
{
|
|
70 |
RInteger& nminus2 = aN;
|
|
71 |
|
|
72 |
/*
|
|
73 |
* do some sanity checks
|
|
74 |
*/
|
|
75 |
--nminus2;
|
|
76 |
--nminus2;
|
|
77 |
if( aG < TInteger::Two() || aG > nminus2 )
|
|
78 |
{
|
|
79 |
User::Leave(KErrArgument);
|
|
80 |
}
|
|
81 |
|
|
82 |
if (xIncluded)
|
|
83 |
{
|
|
84 |
if( ax < TInteger::One() || ax > nminus2 )
|
|
85 |
{
|
|
86 |
User::Leave(KErrArgument);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
/*
|
|
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
|
|
93 |
*/
|
|
94 |
const TInt keySize = nminus2.BitCount();
|
|
95 |
|
|
96 |
// increment aN back to its original value
|
|
97 |
++nminus2;
|
|
98 |
++nminus2;
|
|
99 |
|
|
100 |
// obtain an RSA key pair generator interface
|
|
101 |
|
|
102 |
CKeyPairGenerator* keyPairGeneratorImpl=NULL;
|
|
103 |
CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
|
|
104 |
keyPairGeneratorImpl,
|
|
105 |
KDHKeyPairGeneratorUid,
|
|
106 |
NULL);
|
|
107 |
CleanupStack::PushL(keyPairGeneratorImpl);
|
|
108 |
|
|
109 |
/*
|
|
110 |
* put the DH parameters into an array
|
|
111 |
*/
|
|
112 |
CCryptoParams* keyParameters = CCryptoParams::NewLC();
|
|
113 |
keyParameters->AddL(aN, KDhKeyParameterNUid);
|
|
114 |
keyParameters->AddL(aG, KDhKeyParameterGUid);
|
|
115 |
if (xIncluded)
|
|
116 |
{
|
|
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);
|
|
119 |
ax.Close();
|
|
120 |
}
|
|
121 |
|
|
122 |
/*
|
|
123 |
* call the api to create a DH key pair
|
|
124 |
*/
|
|
125 |
CKeyPair* keyPair = 0;
|
|
126 |
keyPairGeneratorImpl->GenerateKeyPairL(keySize, *keyParameters, keyPair);
|
|
127 |
CleanupStack::PushL(keyPair);
|
|
128 |
|
|
129 |
/*
|
|
130 |
* for compatibility convert the CKeyPair to CDHPrivateKey and CDHPublicKey
|
|
131 |
*/
|
|
132 |
|
|
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);
|
|
142 |
|
|
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);
|
|
148 |
|
|
149 |
/*
|
|
150 |
* cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, X, NCopy, GCopy and x
|
|
151 |
*/
|
|
152 |
CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);
|
|
153 |
}
|