sl@0: /* sl@0: * Copyright (c) 2003-2009 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 sl@0: #include "dhkeypairshim.h" sl@0: sl@0: sl@0: /* CDHParameters */ sl@0: EXPORT_C const TInteger& CDHParameters::N(void) const sl@0: { sl@0: return iN; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDHParameters::G(void) const sl@0: { sl@0: return iG; sl@0: } sl@0: sl@0: EXPORT_C CDHParameters::~CDHParameters(void) sl@0: { sl@0: iN.Close(); sl@0: iG.Close(); sl@0: } sl@0: sl@0: EXPORT_C CDHParameters::CDHParameters(RInteger& aN, RInteger& aG) : iN(aN), iG(aG) sl@0: { sl@0: } sl@0: sl@0: /* CDHPublicKey */ sl@0: EXPORT_C CDHPublicKey* CDHPublicKey::NewL(RInteger& aN, RInteger& aG, sl@0: RInteger& aX) sl@0: { sl@0: CDHPublicKey* self = new(ELeave) CDHPublicKey(aN, aG, aX); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDHPublicKey* CDHPublicKey::NewLC(RInteger& aN, RInteger& aG, sl@0: RInteger& aX) sl@0: { sl@0: CDHPublicKey* self = NewL(aN, aG, aX); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDHPublicKey::X(void) const sl@0: { sl@0: return iX; sl@0: } sl@0: sl@0: EXPORT_C CDHPublicKey::CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX) sl@0: : CDHParameters(aN, aG), iX(aX) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDHPublicKey::~CDHPublicKey(void) sl@0: { sl@0: iX.Close(); sl@0: } sl@0: sl@0: /* CDHPrivateKey */ sl@0: EXPORT_C CDHPrivateKey* CDHPrivateKey::NewL(RInteger& aN, RInteger& aG, sl@0: RInteger& ax) sl@0: { sl@0: CDHPrivateKey* self = new(ELeave) CDHPrivateKey(aN, aG, ax); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDHPrivateKey* CDHPrivateKey::NewLC(RInteger& aN, RInteger& aG, sl@0: RInteger& ax) sl@0: { sl@0: CDHPrivateKey* self = NewL(aN, aG, ax); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDHPrivateKey::x(void) const sl@0: { sl@0: return ix; sl@0: } sl@0: sl@0: EXPORT_C CDHPrivateKey::CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax) sl@0: : CDHParameters(aN, aG), ix(ax) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDHPrivateKey::~CDHPrivateKey(void) sl@0: { sl@0: ix.Close(); sl@0: } sl@0: sl@0: /* CDHKeyPair */ sl@0: sl@0: EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG) sl@0: { sl@0: CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG) sl@0: { sl@0: CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG, RInteger& ax) sl@0: { sl@0: CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG, RInteger& ax) sl@0: { sl@0: CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const CDHPublicKey& CDHKeyPair::PublicKey(void) const sl@0: { sl@0: return *iPublic; sl@0: } sl@0: sl@0: EXPORT_C const CDHPrivateKey& CDHKeyPair::PrivateKey(void) const sl@0: { sl@0: return *iPrivate; sl@0: } sl@0: sl@0: EXPORT_C CDHKeyPair::~CDHKeyPair(void) sl@0: { sl@0: delete iPublic; sl@0: delete iPrivate; sl@0: } sl@0: sl@0: EXPORT_C CDHKeyPair::CDHKeyPair(void) sl@0: { sl@0: } sl@0: sl@0: // Excluded from coverage due to shim replacements. sl@0: #ifdef _BullseyeCoverage sl@0: #pragma suppress_warnings on sl@0: #pragma BullseyeCoverage off sl@0: #pragma suppress_warnings off sl@0: #endif sl@0: EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG) sl@0: { sl@0: //declaring a reference just for clarity in NewRandomL statement sl@0: RInteger& nminus2 = aN; sl@0: --nminus2; sl@0: --nminus2; sl@0: sl@0: //find a random x | 1 <= x <= n-2 sl@0: RInteger x = RInteger::NewRandomL(TInteger::One(), nminus2); sl@0: CleanupStack::PushL(x); sl@0: ++nminus2; sl@0: ++nminus2; // reincrement aN sl@0: sl@0: ConstructL(aN, aG, x); sl@0: sl@0: CleanupStack::Pop(&x); sl@0: } sl@0: sl@0: EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax) sl@0: { sl@0: //declaring a reference just for clarity in if statements sl@0: RInteger& nminus2 = aN; sl@0: --nminus2; sl@0: --nminus2; sl@0: sl@0: if( aG < TInteger::Two() || aG > nminus2 ) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: //In the case of the other ConstructL calling this function this if sl@0: //statement is redundant. However, we need to check as this is can be sl@0: //called without going through the other api. sl@0: if( ax < TInteger::One() || ax > nminus2 ) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: ++nminus2; sl@0: ++nminus2; // reincrement aN sl@0: sl@0: // Calculate X = g^(x) mod n; (note the case sensitivity) sl@0: RInteger X = TInteger::ModularExponentiateL(aG, ax, aN); sl@0: CleanupStack::PushL(X); sl@0: sl@0: RInteger n1 = RInteger::NewL(aN); sl@0: CleanupStack::PushL(n1); sl@0: RInteger g1 = RInteger::NewL(aG); sl@0: CleanupStack::PushL(g1); sl@0: iPublic = CDHPublicKey::NewL(n1, g1, X); sl@0: CleanupStack::Pop(3, &X); // g1, n1, X all owned by iPublic sl@0: sl@0: iPrivate = CDHPrivateKey::NewL(aN, aG, ax); sl@0: } sl@0: sl@0: // Unused exported and protected members. So, exclude them from coverage. sl@0: EXPORT_C CDHParameters::CDHParameters(void) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDHPrivateKey::CDHPrivateKey(void) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDHPublicKey::CDHPublicKey(void) sl@0: { sl@0: }