Update contrib.
2 * Copyright (c) 2003-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.
19 #include <asymmetrickeys.h>
20 #include "dhkeypairshim.h"
24 EXPORT_C const TInteger& CDHParameters::N(void) const
29 EXPORT_C const TInteger& CDHParameters::G(void) const
34 EXPORT_C CDHParameters::~CDHParameters(void)
40 EXPORT_C CDHParameters::CDHParameters(RInteger& aN, RInteger& aG) : iN(aN), iG(aG)
45 EXPORT_C CDHPublicKey* CDHPublicKey::NewL(RInteger& aN, RInteger& aG,
48 CDHPublicKey* self = new(ELeave) CDHPublicKey(aN, aG, aX);
52 EXPORT_C CDHPublicKey* CDHPublicKey::NewLC(RInteger& aN, RInteger& aG,
55 CDHPublicKey* self = NewL(aN, aG, aX);
56 CleanupStack::PushL(self);
60 EXPORT_C const TInteger& CDHPublicKey::X(void) const
65 EXPORT_C CDHPublicKey::CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX)
66 : CDHParameters(aN, aG), iX(aX)
70 EXPORT_C CDHPublicKey::~CDHPublicKey(void)
76 EXPORT_C CDHPrivateKey* CDHPrivateKey::NewL(RInteger& aN, RInteger& aG,
79 CDHPrivateKey* self = new(ELeave) CDHPrivateKey(aN, aG, ax);
83 EXPORT_C CDHPrivateKey* CDHPrivateKey::NewLC(RInteger& aN, RInteger& aG,
86 CDHPrivateKey* self = NewL(aN, aG, ax);
87 CleanupStack::PushL(self);
91 EXPORT_C const TInteger& CDHPrivateKey::x(void) const
96 EXPORT_C CDHPrivateKey::CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax)
97 : CDHParameters(aN, aG), ix(ax)
101 EXPORT_C CDHPrivateKey::~CDHPrivateKey(void)
108 EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG)
110 CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG);
111 CleanupStack::Pop(self);
115 EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG)
117 CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG);
121 EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG, RInteger& ax)
123 CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax);
124 CleanupStack::Pop(self);
128 EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
130 CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax);
134 EXPORT_C const CDHPublicKey& CDHKeyPair::PublicKey(void) const
139 EXPORT_C const CDHPrivateKey& CDHKeyPair::PrivateKey(void) const
144 EXPORT_C CDHKeyPair::~CDHKeyPair(void)
150 EXPORT_C CDHKeyPair::CDHKeyPair(void)
154 // Excluded from coverage due to shim replacements.
155 #ifdef _BullseyeCoverage
156 #pragma suppress_warnings on
157 #pragma BullseyeCoverage off
158 #pragma suppress_warnings off
160 EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG)
162 //declaring a reference just for clarity in NewRandomL statement
163 RInteger& nminus2 = aN;
167 //find a random x | 1 <= x <= n-2
168 RInteger x = RInteger::NewRandomL(TInteger::One(), nminus2);
169 CleanupStack::PushL(x);
171 ++nminus2; // reincrement aN
173 ConstructL(aN, aG, x);
175 CleanupStack::Pop(&x);
178 EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
180 //declaring a reference just for clarity in if statements
181 RInteger& nminus2 = aN;
185 if( aG < TInteger::Two() || aG > nminus2 )
187 User::Leave(KErrArgument);
189 //In the case of the other ConstructL calling this function this if
190 //statement is redundant. However, we need to check as this is can be
191 //called without going through the other api.
192 if( ax < TInteger::One() || ax > nminus2 )
194 User::Leave(KErrArgument);
198 ++nminus2; // reincrement aN
200 // Calculate X = g^(x) mod n; (note the case sensitivity)
201 RInteger X = TInteger::ModularExponentiateL(aG, ax, aN);
202 CleanupStack::PushL(X);
204 RInteger n1 = RInteger::NewL(aN);
205 CleanupStack::PushL(n1);
206 RInteger g1 = RInteger::NewL(aG);
207 CleanupStack::PushL(g1);
208 iPublic = CDHPublicKey::NewL(n1, g1, X);
209 CleanupStack::Pop(3, &X); // g1, n1, X all owned by iPublic
211 iPrivate = CDHPrivateKey::NewL(aN, aG, ax);
214 // Unused exported and protected members. So, exclude them from coverage.
215 EXPORT_C CDHParameters::CDHParameters(void)
219 EXPORT_C CDHPrivateKey::CDHPrivateKey(void)
223 EXPORT_C CDHPublicKey::CDHPublicKey(void)