1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/asymmetric/dhkeys.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,225 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <asymmetrickeys.h>
1.23 +#include "dhkeypairshim.h"
1.24 +
1.25 +
1.26 +/* CDHParameters */
1.27 +EXPORT_C const TInteger& CDHParameters::N(void) const
1.28 + {
1.29 + return iN;
1.30 + }
1.31 +
1.32 +EXPORT_C const TInteger& CDHParameters::G(void) const
1.33 + {
1.34 + return iG;
1.35 + }
1.36 +
1.37 +EXPORT_C CDHParameters::~CDHParameters(void)
1.38 + {
1.39 + iN.Close();
1.40 + iG.Close();
1.41 + }
1.42 +
1.43 +EXPORT_C CDHParameters::CDHParameters(RInteger& aN, RInteger& aG) : iN(aN), iG(aG)
1.44 + {
1.45 + }
1.46 +
1.47 +/* CDHPublicKey */
1.48 +EXPORT_C CDHPublicKey* CDHPublicKey::NewL(RInteger& aN, RInteger& aG,
1.49 + RInteger& aX)
1.50 + {
1.51 + CDHPublicKey* self = new(ELeave) CDHPublicKey(aN, aG, aX);
1.52 + return self;
1.53 + }
1.54 +
1.55 +EXPORT_C CDHPublicKey* CDHPublicKey::NewLC(RInteger& aN, RInteger& aG,
1.56 + RInteger& aX)
1.57 + {
1.58 + CDHPublicKey* self = NewL(aN, aG, aX);
1.59 + CleanupStack::PushL(self);
1.60 + return self;
1.61 + }
1.62 +
1.63 +EXPORT_C const TInteger& CDHPublicKey::X(void) const
1.64 + {
1.65 + return iX;
1.66 + }
1.67 +
1.68 +EXPORT_C CDHPublicKey::CDHPublicKey(RInteger& aN, RInteger& aG, RInteger& aX)
1.69 + : CDHParameters(aN, aG), iX(aX)
1.70 + {
1.71 + }
1.72 +
1.73 +EXPORT_C CDHPublicKey::~CDHPublicKey(void)
1.74 + {
1.75 + iX.Close();
1.76 + }
1.77 +
1.78 +/* CDHPrivateKey */
1.79 +EXPORT_C CDHPrivateKey* CDHPrivateKey::NewL(RInteger& aN, RInteger& aG,
1.80 + RInteger& ax)
1.81 + {
1.82 + CDHPrivateKey* self = new(ELeave) CDHPrivateKey(aN, aG, ax);
1.83 + return self;
1.84 + }
1.85 +
1.86 +EXPORT_C CDHPrivateKey* CDHPrivateKey::NewLC(RInteger& aN, RInteger& aG,
1.87 + RInteger& ax)
1.88 + {
1.89 + CDHPrivateKey* self = NewL(aN, aG, ax);
1.90 + CleanupStack::PushL(self);
1.91 + return self;
1.92 + }
1.93 +
1.94 +EXPORT_C const TInteger& CDHPrivateKey::x(void) const
1.95 + {
1.96 + return ix;
1.97 + }
1.98 +
1.99 +EXPORT_C CDHPrivateKey::CDHPrivateKey(RInteger& aN, RInteger& aG, RInteger& ax)
1.100 + : CDHParameters(aN, aG), ix(ax)
1.101 + {
1.102 + }
1.103 +
1.104 +EXPORT_C CDHPrivateKey::~CDHPrivateKey(void)
1.105 + {
1.106 + ix.Close();
1.107 + }
1.108 +
1.109 +/* CDHKeyPair */
1.110 +
1.111 +EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG)
1.112 + {
1.113 + CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG);
1.114 + CleanupStack::Pop(self);
1.115 + return self;
1.116 + }
1.117 +
1.118 +EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG)
1.119 + {
1.120 + CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG);
1.121 + return self;
1.122 + }
1.123 +
1.124 +EXPORT_C CDHKeyPair* CDHKeyPair::NewL(RInteger& aN, RInteger& aG, RInteger& ax)
1.125 + {
1.126 + CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax);
1.127 + CleanupStack::Pop(self);
1.128 + return self;
1.129 + }
1.130 +
1.131 +EXPORT_C CDHKeyPair* CDHKeyPair::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
1.132 + {
1.133 + CDHKeyPairShim* self = CDHKeyPairShim::NewLC(aN, aG, ax);
1.134 + return self;
1.135 + }
1.136 +
1.137 +EXPORT_C const CDHPublicKey& CDHKeyPair::PublicKey(void) const
1.138 + {
1.139 + return *iPublic;
1.140 + }
1.141 +
1.142 +EXPORT_C const CDHPrivateKey& CDHKeyPair::PrivateKey(void) const
1.143 + {
1.144 + return *iPrivate;
1.145 + }
1.146 +
1.147 +EXPORT_C CDHKeyPair::~CDHKeyPair(void)
1.148 + {
1.149 + delete iPublic;
1.150 + delete iPrivate;
1.151 + }
1.152 +
1.153 +EXPORT_C CDHKeyPair::CDHKeyPair(void)
1.154 + {
1.155 + }
1.156 +
1.157 +// Excluded from coverage due to shim replacements.
1.158 +#ifdef _BullseyeCoverage
1.159 +#pragma suppress_warnings on
1.160 +#pragma BullseyeCoverage off
1.161 +#pragma suppress_warnings off
1.162 +#endif
1.163 +EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG)
1.164 + {
1.165 + //declaring a reference just for clarity in NewRandomL statement
1.166 + RInteger& nminus2 = aN;
1.167 + --nminus2;
1.168 + --nminus2;
1.169 +
1.170 + //find a random x | 1 <= x <= n-2
1.171 + RInteger x = RInteger::NewRandomL(TInteger::One(), nminus2);
1.172 + CleanupStack::PushL(x);
1.173 + ++nminus2;
1.174 + ++nminus2; // reincrement aN
1.175 +
1.176 + ConstructL(aN, aG, x);
1.177 +
1.178 + CleanupStack::Pop(&x);
1.179 + }
1.180 +
1.181 +EXPORT_C void CDHKeyPair::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
1.182 + {
1.183 + //declaring a reference just for clarity in if statements
1.184 + RInteger& nminus2 = aN;
1.185 + --nminus2;
1.186 + --nminus2;
1.187 +
1.188 + if( aG < TInteger::Two() || aG > nminus2 )
1.189 + {
1.190 + User::Leave(KErrArgument);
1.191 + }
1.192 + //In the case of the other ConstructL calling this function this if
1.193 + //statement is redundant. However, we need to check as this is can be
1.194 + //called without going through the other api.
1.195 + if( ax < TInteger::One() || ax > nminus2 )
1.196 + {
1.197 + User::Leave(KErrArgument);
1.198 + }
1.199 +
1.200 + ++nminus2;
1.201 + ++nminus2; // reincrement aN
1.202 +
1.203 + // Calculate X = g^(x) mod n; (note the case sensitivity)
1.204 + RInteger X = TInteger::ModularExponentiateL(aG, ax, aN);
1.205 + CleanupStack::PushL(X);
1.206 +
1.207 + RInteger n1 = RInteger::NewL(aN);
1.208 + CleanupStack::PushL(n1);
1.209 + RInteger g1 = RInteger::NewL(aG);
1.210 + CleanupStack::PushL(g1);
1.211 + iPublic = CDHPublicKey::NewL(n1, g1, X);
1.212 + CleanupStack::Pop(3, &X); // g1, n1, X all owned by iPublic
1.213 +
1.214 + iPrivate = CDHPrivateKey::NewL(aN, aG, ax);
1.215 + }
1.216 +
1.217 +// Unused exported and protected members. So, exclude them from coverage.
1.218 +EXPORT_C CDHParameters::CDHParameters(void)
1.219 + {
1.220 + }
1.221 +
1.222 +EXPORT_C CDHPrivateKey::CDHPrivateKey(void)
1.223 + {
1.224 + }
1.225 +
1.226 +EXPORT_C CDHPublicKey::CDHPublicKey(void)
1.227 + {
1.228 + }