|
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 "dhkeypairgenimpl.h" |
|
20 #include "pluginconfig.h" |
|
21 #include "keypair.h" |
|
22 |
|
23 using namespace SoftwareCrypto; |
|
24 using namespace CryptoSpi; |
|
25 |
|
26 /* CDHKeyPairGenImpl */ |
|
27 CDHKeyPairGenImpl::CDHKeyPairGenImpl() |
|
28 { |
|
29 |
|
30 } |
|
31 |
|
32 CDHKeyPairGenImpl::~CDHKeyPairGenImpl() |
|
33 { |
|
34 |
|
35 } |
|
36 |
|
37 CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewL(void) |
|
38 { |
|
39 CDHKeyPairGenImpl* self = CDHKeyPairGenImpl::NewLC(); |
|
40 CleanupStack::Pop(self); |
|
41 return self; |
|
42 } |
|
43 |
|
44 CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewLC(void) |
|
45 { |
|
46 CDHKeyPairGenImpl* self = new(ELeave) CDHKeyPairGenImpl(); |
|
47 CleanupStack::PushL(self); |
|
48 self->ConstructL(); |
|
49 return self; |
|
50 } |
|
51 |
|
52 void CDHKeyPairGenImpl::ConstructL(void) |
|
53 { |
|
54 CKeyPairGenImpl::ConstructL(); |
|
55 } |
|
56 |
|
57 CExtendedCharacteristics* CDHKeyPairGenImpl::CreateExtendedCharacteristicsL() |
|
58 { |
|
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); |
|
62 } |
|
63 |
|
64 const CExtendedCharacteristics* CDHKeyPairGenImpl::GetExtendedCharacteristicsL() |
|
65 { |
|
66 return CDHKeyPairGenImpl::CreateExtendedCharacteristicsL(); |
|
67 } |
|
68 |
|
69 TUid CDHKeyPairGenImpl::ImplementationUid() const |
|
70 { |
|
71 return KCryptoPluginDhKeyPairGenUid; |
|
72 } |
|
73 |
|
74 void CDHKeyPairGenImpl::Reset() |
|
75 { |
|
76 // does nothing in this plugin |
|
77 } |
|
78 |
|
79 void CDHKeyPairGenImpl::GenerateKeyPairL(TInt /*aKeySize*/, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair) |
|
80 { |
|
81 /* |
|
82 * unpack the parameters, we're expecting the N and G parameters and if present the x parameter (aka private key) |
|
83 */ |
|
84 const TInteger& N = aKeyParameters.GetBigIntL(KDhKeyParameterNUid); |
|
85 const TInteger& G = aKeyParameters.GetBigIntL(KDhKeyParameterGUid); |
|
86 |
|
87 /* |
|
88 * do some sanity checking |
|
89 */ |
|
90 RInteger nminus2 = RInteger::NewL(N); |
|
91 CleanupStack::PushL(nminus2); |
|
92 --nminus2; |
|
93 --nminus2; |
|
94 |
|
95 if ((G < TInteger::Two()) || (G > nminus2)) |
|
96 { |
|
97 User::Leave(KErrArgument); |
|
98 } |
|
99 |
|
100 /* |
|
101 * has a private key x been supplied? if not then generate it |
|
102 */ |
|
103 RInteger x; |
|
104 if (aKeyParameters.IsPresent(KDhKeyParameterxUid)) |
|
105 { |
|
106 x = RInteger::NewL(aKeyParameters.GetBigIntL(KDhKeyParameterxUid)); |
|
107 } |
|
108 else |
|
109 { |
|
110 // find a random x | 1 <= x <= n-2 |
|
111 x = RInteger::NewRandomL(TInteger::One(), nminus2); |
|
112 } |
|
113 CleanupClosePushL(x); |
|
114 /* |
|
115 * generate the public key with X = G^(x) mod N |
|
116 */ |
|
117 RInteger X = TInteger::ModularExponentiateL(G, x, N); |
|
118 CleanupClosePushL(X); |
|
119 |
|
120 /* |
|
121 * create the keys parameters |
|
122 */ |
|
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 }; |
|
131 |
|
132 /* |
|
133 * create the public key |
|
134 */ |
|
135 CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters); |
|
136 CleanupStack::PushL(publicKey); |
|
137 |
|
138 /* |
|
139 * create the private key |
|
140 */ |
|
141 CKey* privateKey = CKey::NewL(privateKeyProperties, *privateKeyParameters); |
|
142 CleanupStack::PushL(privateKey); |
|
143 |
|
144 /* |
|
145 * create the key pair |
|
146 */ |
|
147 aKeyPair = CKeyPair::NewL(publicKey, privateKey); |
|
148 |
|
149 /* |
|
150 * cleanup stack - it should contain nminus2, x (if allocated here), X, publicKeyParameters, privateKeyParameters, publicKey and privateKey |
|
151 */ |
|
152 CleanupStack::Pop(2, publicKey); |
|
153 CleanupStack::PopAndDestroy(5, &nminus2); |
|
154 } |