crypto/weakcryptospi/source/asymmetric/rsakeys.cpp
changeset 19 cd501b96611d
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     1 /*
       
     2 * Copyright (c) 2003-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 <asymmetrickeys.h>
       
    20 #include "rsakeypairshim.h"
       
    21 #include "../common/inlines.h"
       
    22 
       
    23 /* CRSAParameters */
       
    24 
       
    25 EXPORT_C const TInteger& CRSAParameters::N(void) const
       
    26 	{
       
    27 	return iN;
       
    28 	}
       
    29 
       
    30 EXPORT_C CRSAParameters::~CRSAParameters(void)
       
    31 	{
       
    32 	iN.Close();
       
    33 	}
       
    34 
       
    35 EXPORT_C CRSAParameters::CRSAParameters(RInteger& aN) : iN(aN)
       
    36 	{
       
    37 	}
       
    38 
       
    39 EXPORT_C CRSAParameters::CRSAParameters(void)
       
    40 	{
       
    41 	}
       
    42 
       
    43 /* CRSAPublicKey */
       
    44 
       
    45 EXPORT_C CRSAPublicKey* CRSAPublicKey::NewL(RInteger& aN, RInteger& aE)
       
    46 	{
       
    47 	CRSAPublicKey* self = NewLC(aN, aE);
       
    48 	CleanupStack::Pop();
       
    49 	return self;
       
    50 	}
       
    51 
       
    52 EXPORT_C CRSAPublicKey* CRSAPublicKey::NewLC(RInteger& aN, RInteger& aE)
       
    53 	{
       
    54 	CRSAPublicKey* self = new(ELeave) CRSAPublicKey(aN, aE);
       
    55 	CleanupStack::PushL(self);
       
    56 	self->ConstructL();
       
    57 	return self;
       
    58 	}
       
    59 
       
    60 
       
    61 void CRSAPublicKey::ConstructL()
       
    62 	{
       
    63 	// Check that the modulus and exponent are positive integers 
       
    64 	// as specified by RSA
       
    65 	if(!N().IsPositive() || !E().IsPositive() || (E() <= 1))
       
    66 		{
       
    67 		// If we need to leave during construction we must release ownership
       
    68 		// of the RInteger parameters that were passed in.
       
    69 		// These parameters should be on the cleanup stack so if we don't 
       
    70 		// release ownership they will be deleted twice, causing a panic
       
    71 		iN = RInteger();
       
    72 		iE = RInteger();
       
    73 		User::Leave(KErrArgument);
       
    74 		}
       
    75 	}
       
    76 
       
    77 
       
    78 EXPORT_C const TInteger& CRSAPublicKey::E(void) const
       
    79 	{
       
    80 	return iE;
       
    81 	}
       
    82 
       
    83 EXPORT_C CRSAPublicKey::CRSAPublicKey()
       
    84 	{
       
    85 	}
       
    86 
       
    87 EXPORT_C CRSAPublicKey::CRSAPublicKey(RInteger& aN, RInteger& aE)
       
    88 	: CRSAParameters(aN), iE(aE)
       
    89 	{
       
    90 	}
       
    91 
       
    92 EXPORT_C CRSAPublicKey::~CRSAPublicKey(void)
       
    93 	{
       
    94 	iE.Close();
       
    95 	}
       
    96 
       
    97 /* CRSAPrivateKeyType */
       
    98 
       
    99 CRSAPrivateKey::CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN)
       
   100 :	CRSAParameters(aN), iKeyType(aKeyType)
       
   101 {}
       
   102 
       
   103 
       
   104 /* CRSAPrivateKeyStandard */
       
   105 
       
   106 EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewL(RInteger& aN, 
       
   107 	RInteger& aD)
       
   108 	{
       
   109 	CRSAPrivateKeyStandard* self = NewLC(aN, aD);
       
   110 	CleanupStack::Pop();
       
   111 	return self;
       
   112 	}
       
   113 
       
   114 EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewLC(RInteger& aN,
       
   115 	RInteger& aD)
       
   116 	{
       
   117 	CRSAPrivateKeyStandard* self = new(ELeave) CRSAPrivateKeyStandard(aN, aD);
       
   118 	CleanupStack::PushL(self);
       
   119 	self->ConstructL();
       
   120 	return self;
       
   121 	}
       
   122 
       
   123 void CRSAPrivateKeyStandard::ConstructL()
       
   124 	{
       
   125 	// Check that the modulus and exponent are positive integers
       
   126 	if(!N().IsPositive() || !D().IsPositive() || (D() <= 1))
       
   127 		{
       
   128 		// If we need to leave during construction we must release ownership
       
   129 		// of the RInteger parameters that were passed in.
       
   130 		// These parameters should be on the cleanup stack so if we don't 
       
   131 		// release ownership they will be deleted twice, causing a panic
       
   132 		iN = RInteger();
       
   133 		iD = RInteger();
       
   134 		User::Leave(KErrArgument);
       
   135 		}
       
   136 	}
       
   137 
       
   138 EXPORT_C const TInteger& CRSAPrivateKeyStandard::D(void) const
       
   139 	{
       
   140 	return iD;
       
   141 	}
       
   142 
       
   143 EXPORT_C CRSAPrivateKeyStandard::CRSAPrivateKeyStandard(RInteger& aN, 
       
   144 	RInteger& aD) : CRSAPrivateKey(EStandard, aN), iD(aD)
       
   145 	{
       
   146 	}
       
   147 
       
   148 EXPORT_C CRSAPrivateKeyStandard::~CRSAPrivateKeyStandard()
       
   149 	{	
       
   150 	iD.Close();
       
   151 	}
       
   152 
       
   153 /* CRSAPrivateKeyCRT */
       
   154 
       
   155 EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewL(RInteger& aN, RInteger& aP,
       
   156 	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
       
   157 	{
       
   158 	CRSAPrivateKeyCRT* self = NewLC(aN, aP, aQ, aDP, aDQ, aQInv);
       
   159 	CleanupStack::Pop();
       
   160 	return self;
       
   161 	}
       
   162 
       
   163 EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewLC(RInteger& aN, RInteger& aP, 
       
   164 	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
       
   165 	{
       
   166 	CRSAPrivateKeyCRT* self = new(ELeave) CRSAPrivateKeyCRT(aN, aP, aQ, 
       
   167 		aDP, aDQ, aQInv);
       
   168 	CleanupStack::PushL(self);
       
   169 	self->ConstructL();
       
   170 	return self;
       
   171 	}
       
   172 
       
   173 EXPORT_C CRSAPrivateKeyCRT::CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP, 
       
   174 	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv) 
       
   175 	: CRSAPrivateKey(EStandardCRT, aN), iP(aP), iQ(aQ), iDP(aDP), iDQ(aDQ), 
       
   176 		iQInv(aQInv)
       
   177 	{
       
   178 	}
       
   179 
       
   180 void CRSAPrivateKeyCRT::ConstructL()
       
   181 	{
       
   182 	// Check that all parameters are positive integers
       
   183 	if(!P().IsPositive() || !Q().IsPositive() || !DP().IsPositive() 
       
   184 		|| !DQ().IsPositive() || !QInv().IsPositive())
       
   185 		{
       
   186 		// If we need to leave during construction we must release ownership
       
   187 		// of the RInteger parameters that were passed in.
       
   188 		// These parameters should be on the cleanup stack so if we don't 
       
   189 		// release ownership they will be deleted twice, causing a panic
       
   190 		iN = RInteger();
       
   191 		iP = RInteger();
       
   192 		iQ = RInteger();
       
   193 		iDP = RInteger();
       
   194 		iDQ = RInteger();
       
   195 		iQInv = RInteger();
       
   196 		User::Leave(KErrArgument);
       
   197 		}
       
   198 	}
       
   199 
       
   200 
       
   201 EXPORT_C CRSAPrivateKeyCRT::~CRSAPrivateKeyCRT()
       
   202 	{	
       
   203 	iP.Close();
       
   204 	iQ.Close();
       
   205 	iDP.Close();
       
   206 	iDQ.Close();
       
   207 	iQInv.Close();
       
   208 	}
       
   209 
       
   210 EXPORT_C const TInteger& CRSAPrivateKeyCRT::P(void) const
       
   211 	{
       
   212 	return iP;
       
   213 	}
       
   214 
       
   215 EXPORT_C const TInteger& CRSAPrivateKeyCRT::Q(void) const
       
   216 	{
       
   217 	return iQ;
       
   218 	}
       
   219 
       
   220 EXPORT_C const TInteger& CRSAPrivateKeyCRT::DP(void) const
       
   221 	{
       
   222 	return iDP;
       
   223 	}
       
   224 
       
   225 EXPORT_C const TInteger& CRSAPrivateKeyCRT::DQ(void) const
       
   226 	{
       
   227 	return iDQ;
       
   228 	}
       
   229 
       
   230 EXPORT_C const TInteger& CRSAPrivateKeyCRT::QInv(void) const
       
   231 	{
       
   232 	return iQInv;
       
   233 	}
       
   234 
       
   235 /* CRSAKeyPair */
       
   236 
       
   237 EXPORT_C CRSAKeyPair* CRSAKeyPair::NewL(TUint aModulusBits, 
       
   238 	TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
       
   239 	{
       
   240 	CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
       
   241 	CleanupStack::Pop();
       
   242 	return self;
       
   243 	}
       
   244 
       
   245 EXPORT_C CRSAKeyPair* CRSAKeyPair::NewLC(TUint aModulusBits, 
       
   246 	TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
       
   247 	{
       
   248 	CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
       
   249 	return self;
       
   250 	}
       
   251 
       
   252 EXPORT_C const CRSAPublicKey& CRSAKeyPair::PublicKey(void) const
       
   253 	{
       
   254 	return *iPublic;
       
   255 	}
       
   256 	
       
   257 EXPORT_C const CRSAPrivateKey& CRSAKeyPair::PrivateKey(void) const
       
   258 	{
       
   259 	return *iPrivate;
       
   260 	}
       
   261 
       
   262 EXPORT_C CRSAKeyPair::~CRSAKeyPair(void)
       
   263 	{
       
   264 	delete iPublic;
       
   265 	delete iPrivate;
       
   266 	}
       
   267 
       
   268 EXPORT_C CRSAKeyPair::CRSAKeyPair(void)
       
   269 	{
       
   270 	}
       
   271