1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/asymmetric/rsakeys.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,271 @@
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 "rsakeypairshim.h"
1.24 +#include "../common/inlines.h"
1.25 +
1.26 +/* CRSAParameters */
1.27 +
1.28 +EXPORT_C const TInteger& CRSAParameters::N(void) const
1.29 + {
1.30 + return iN;
1.31 + }
1.32 +
1.33 +EXPORT_C CRSAParameters::~CRSAParameters(void)
1.34 + {
1.35 + iN.Close();
1.36 + }
1.37 +
1.38 +EXPORT_C CRSAParameters::CRSAParameters(RInteger& aN) : iN(aN)
1.39 + {
1.40 + }
1.41 +
1.42 +EXPORT_C CRSAParameters::CRSAParameters(void)
1.43 + {
1.44 + }
1.45 +
1.46 +/* CRSAPublicKey */
1.47 +
1.48 +EXPORT_C CRSAPublicKey* CRSAPublicKey::NewL(RInteger& aN, RInteger& aE)
1.49 + {
1.50 + CRSAPublicKey* self = NewLC(aN, aE);
1.51 + CleanupStack::Pop();
1.52 + return self;
1.53 + }
1.54 +
1.55 +EXPORT_C CRSAPublicKey* CRSAPublicKey::NewLC(RInteger& aN, RInteger& aE)
1.56 + {
1.57 + CRSAPublicKey* self = new(ELeave) CRSAPublicKey(aN, aE);
1.58 + CleanupStack::PushL(self);
1.59 + self->ConstructL();
1.60 + return self;
1.61 + }
1.62 +
1.63 +
1.64 +void CRSAPublicKey::ConstructL()
1.65 + {
1.66 + // Check that the modulus and exponent are positive integers
1.67 + // as specified by RSA
1.68 + if(!N().IsPositive() || !E().IsPositive() || (E() <= 1))
1.69 + {
1.70 + // If we need to leave during construction we must release ownership
1.71 + // of the RInteger parameters that were passed in.
1.72 + // These parameters should be on the cleanup stack so if we don't
1.73 + // release ownership they will be deleted twice, causing a panic
1.74 + iN = RInteger();
1.75 + iE = RInteger();
1.76 + User::Leave(KErrArgument);
1.77 + }
1.78 + }
1.79 +
1.80 +
1.81 +EXPORT_C const TInteger& CRSAPublicKey::E(void) const
1.82 + {
1.83 + return iE;
1.84 + }
1.85 +
1.86 +EXPORT_C CRSAPublicKey::CRSAPublicKey()
1.87 + {
1.88 + }
1.89 +
1.90 +EXPORT_C CRSAPublicKey::CRSAPublicKey(RInteger& aN, RInteger& aE)
1.91 + : CRSAParameters(aN), iE(aE)
1.92 + {
1.93 + }
1.94 +
1.95 +EXPORT_C CRSAPublicKey::~CRSAPublicKey(void)
1.96 + {
1.97 + iE.Close();
1.98 + }
1.99 +
1.100 +/* CRSAPrivateKeyType */
1.101 +
1.102 +CRSAPrivateKey::CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN)
1.103 +: CRSAParameters(aN), iKeyType(aKeyType)
1.104 +{}
1.105 +
1.106 +
1.107 +/* CRSAPrivateKeyStandard */
1.108 +
1.109 +EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewL(RInteger& aN,
1.110 + RInteger& aD)
1.111 + {
1.112 + CRSAPrivateKeyStandard* self = NewLC(aN, aD);
1.113 + CleanupStack::Pop();
1.114 + return self;
1.115 + }
1.116 +
1.117 +EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewLC(RInteger& aN,
1.118 + RInteger& aD)
1.119 + {
1.120 + CRSAPrivateKeyStandard* self = new(ELeave) CRSAPrivateKeyStandard(aN, aD);
1.121 + CleanupStack::PushL(self);
1.122 + self->ConstructL();
1.123 + return self;
1.124 + }
1.125 +
1.126 +void CRSAPrivateKeyStandard::ConstructL()
1.127 + {
1.128 + // Check that the modulus and exponent are positive integers
1.129 + if(!N().IsPositive() || !D().IsPositive() || (D() <= 1))
1.130 + {
1.131 + // If we need to leave during construction we must release ownership
1.132 + // of the RInteger parameters that were passed in.
1.133 + // These parameters should be on the cleanup stack so if we don't
1.134 + // release ownership they will be deleted twice, causing a panic
1.135 + iN = RInteger();
1.136 + iD = RInteger();
1.137 + User::Leave(KErrArgument);
1.138 + }
1.139 + }
1.140 +
1.141 +EXPORT_C const TInteger& CRSAPrivateKeyStandard::D(void) const
1.142 + {
1.143 + return iD;
1.144 + }
1.145 +
1.146 +EXPORT_C CRSAPrivateKeyStandard::CRSAPrivateKeyStandard(RInteger& aN,
1.147 + RInteger& aD) : CRSAPrivateKey(EStandard, aN), iD(aD)
1.148 + {
1.149 + }
1.150 +
1.151 +EXPORT_C CRSAPrivateKeyStandard::~CRSAPrivateKeyStandard()
1.152 + {
1.153 + iD.Close();
1.154 + }
1.155 +
1.156 +/* CRSAPrivateKeyCRT */
1.157 +
1.158 +EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewL(RInteger& aN, RInteger& aP,
1.159 + RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
1.160 + {
1.161 + CRSAPrivateKeyCRT* self = NewLC(aN, aP, aQ, aDP, aDQ, aQInv);
1.162 + CleanupStack::Pop();
1.163 + return self;
1.164 + }
1.165 +
1.166 +EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewLC(RInteger& aN, RInteger& aP,
1.167 + RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
1.168 + {
1.169 + CRSAPrivateKeyCRT* self = new(ELeave) CRSAPrivateKeyCRT(aN, aP, aQ,
1.170 + aDP, aDQ, aQInv);
1.171 + CleanupStack::PushL(self);
1.172 + self->ConstructL();
1.173 + return self;
1.174 + }
1.175 +
1.176 +EXPORT_C CRSAPrivateKeyCRT::CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP,
1.177 + RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
1.178 + : CRSAPrivateKey(EStandardCRT, aN), iP(aP), iQ(aQ), iDP(aDP), iDQ(aDQ),
1.179 + iQInv(aQInv)
1.180 + {
1.181 + }
1.182 +
1.183 +void CRSAPrivateKeyCRT::ConstructL()
1.184 + {
1.185 + // Check that all parameters are positive integers
1.186 + if(!P().IsPositive() || !Q().IsPositive() || !DP().IsPositive()
1.187 + || !DQ().IsPositive() || !QInv().IsPositive())
1.188 + {
1.189 + // If we need to leave during construction we must release ownership
1.190 + // of the RInteger parameters that were passed in.
1.191 + // These parameters should be on the cleanup stack so if we don't
1.192 + // release ownership they will be deleted twice, causing a panic
1.193 + iN = RInteger();
1.194 + iP = RInteger();
1.195 + iQ = RInteger();
1.196 + iDP = RInteger();
1.197 + iDQ = RInteger();
1.198 + iQInv = RInteger();
1.199 + User::Leave(KErrArgument);
1.200 + }
1.201 + }
1.202 +
1.203 +
1.204 +EXPORT_C CRSAPrivateKeyCRT::~CRSAPrivateKeyCRT()
1.205 + {
1.206 + iP.Close();
1.207 + iQ.Close();
1.208 + iDP.Close();
1.209 + iDQ.Close();
1.210 + iQInv.Close();
1.211 + }
1.212 +
1.213 +EXPORT_C const TInteger& CRSAPrivateKeyCRT::P(void) const
1.214 + {
1.215 + return iP;
1.216 + }
1.217 +
1.218 +EXPORT_C const TInteger& CRSAPrivateKeyCRT::Q(void) const
1.219 + {
1.220 + return iQ;
1.221 + }
1.222 +
1.223 +EXPORT_C const TInteger& CRSAPrivateKeyCRT::DP(void) const
1.224 + {
1.225 + return iDP;
1.226 + }
1.227 +
1.228 +EXPORT_C const TInteger& CRSAPrivateKeyCRT::DQ(void) const
1.229 + {
1.230 + return iDQ;
1.231 + }
1.232 +
1.233 +EXPORT_C const TInteger& CRSAPrivateKeyCRT::QInv(void) const
1.234 + {
1.235 + return iQInv;
1.236 + }
1.237 +
1.238 +/* CRSAKeyPair */
1.239 +
1.240 +EXPORT_C CRSAKeyPair* CRSAKeyPair::NewL(TUint aModulusBits,
1.241 + TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
1.242 + {
1.243 + CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
1.244 + CleanupStack::Pop();
1.245 + return self;
1.246 + }
1.247 +
1.248 +EXPORT_C CRSAKeyPair* CRSAKeyPair::NewLC(TUint aModulusBits,
1.249 + TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
1.250 + {
1.251 + CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
1.252 + return self;
1.253 + }
1.254 +
1.255 +EXPORT_C const CRSAPublicKey& CRSAKeyPair::PublicKey(void) const
1.256 + {
1.257 + return *iPublic;
1.258 + }
1.259 +
1.260 +EXPORT_C const CRSAPrivateKey& CRSAKeyPair::PrivateKey(void) const
1.261 + {
1.262 + return *iPrivate;
1.263 + }
1.264 +
1.265 +EXPORT_C CRSAKeyPair::~CRSAKeyPair(void)
1.266 + {
1.267 + delete iPublic;
1.268 + delete iPrivate;
1.269 + }
1.270 +
1.271 +EXPORT_C CRSAKeyPair::CRSAKeyPair(void)
1.272 + {
1.273 + }
1.274 +