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 <asymmetrickeys.h>
sl@0: #include "rsakeypairshim.h"
sl@0: #include "../common/inlines.h"
sl@0: 
sl@0: /* CRSAParameters */
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAParameters::N(void) const
sl@0: 	{
sl@0: 	return iN;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAParameters::~CRSAParameters(void)
sl@0: 	{
sl@0: 	iN.Close();
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAParameters::CRSAParameters(RInteger& aN) : iN(aN)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAParameters::CRSAParameters(void)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: /* CRSAPublicKey */
sl@0: 
sl@0: EXPORT_C CRSAPublicKey* CRSAPublicKey::NewL(RInteger& aN, RInteger& aE)
sl@0: 	{
sl@0: 	CRSAPublicKey* self = NewLC(aN, aE);
sl@0: 	CleanupStack::Pop();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPublicKey* CRSAPublicKey::NewLC(RInteger& aN, RInteger& aE)
sl@0: 	{
sl@0: 	CRSAPublicKey* self = new(ELeave) CRSAPublicKey(aN, aE);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: void CRSAPublicKey::ConstructL()
sl@0: 	{
sl@0: 	// Check that the modulus and exponent are positive integers 
sl@0: 	// as specified by RSA
sl@0: 	if(!N().IsPositive() || !E().IsPositive() || (E() <= 1))
sl@0: 		{
sl@0: 		// If we need to leave during construction we must release ownership
sl@0: 		// of the RInteger parameters that were passed in.
sl@0: 		// These parameters should be on the cleanup stack so if we don't 
sl@0: 		// release ownership they will be deleted twice, causing a panic
sl@0: 		iN = RInteger();
sl@0: 		iE = RInteger();
sl@0: 		User::Leave(KErrArgument);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPublicKey::E(void) const
sl@0: 	{
sl@0: 	return iE;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPublicKey::CRSAPublicKey()
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPublicKey::CRSAPublicKey(RInteger& aN, RInteger& aE)
sl@0: 	: CRSAParameters(aN), iE(aE)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPublicKey::~CRSAPublicKey(void)
sl@0: 	{
sl@0: 	iE.Close();
sl@0: 	}
sl@0: 
sl@0: /* CRSAPrivateKeyType */
sl@0: 
sl@0: CRSAPrivateKey::CRSAPrivateKey(const TRSAPrivateKeyType aKeyType, RInteger& aN)
sl@0: :	CRSAParameters(aN), iKeyType(aKeyType)
sl@0: {}
sl@0: 
sl@0: 
sl@0: /* CRSAPrivateKeyStandard */
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewL(RInteger& aN, 
sl@0: 	RInteger& aD)
sl@0: 	{
sl@0: 	CRSAPrivateKeyStandard* self = NewLC(aN, aD);
sl@0: 	CleanupStack::Pop();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyStandard* CRSAPrivateKeyStandard::NewLC(RInteger& aN,
sl@0: 	RInteger& aD)
sl@0: 	{
sl@0: 	CRSAPrivateKeyStandard* self = new(ELeave) CRSAPrivateKeyStandard(aN, aD);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: void CRSAPrivateKeyStandard::ConstructL()
sl@0: 	{
sl@0: 	// Check that the modulus and exponent are positive integers
sl@0: 	if(!N().IsPositive() || !D().IsPositive() || (D() <= 1))
sl@0: 		{
sl@0: 		// If we need to leave during construction we must release ownership
sl@0: 		// of the RInteger parameters that were passed in.
sl@0: 		// These parameters should be on the cleanup stack so if we don't 
sl@0: 		// release ownership they will be deleted twice, causing a panic
sl@0: 		iN = RInteger();
sl@0: 		iD = RInteger();
sl@0: 		User::Leave(KErrArgument);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPrivateKeyStandard::D(void) const
sl@0: 	{
sl@0: 	return iD;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyStandard::CRSAPrivateKeyStandard(RInteger& aN, 
sl@0: 	RInteger& aD) : CRSAPrivateKey(EStandard, aN), iD(aD)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyStandard::~CRSAPrivateKeyStandard()
sl@0: 	{	
sl@0: 	iD.Close();
sl@0: 	}
sl@0: 
sl@0: /* CRSAPrivateKeyCRT */
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewL(RInteger& aN, RInteger& aP,
sl@0: 	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
sl@0: 	{
sl@0: 	CRSAPrivateKeyCRT* self = NewLC(aN, aP, aQ, aDP, aDQ, aQInv);
sl@0: 	CleanupStack::Pop();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyCRT* CRSAPrivateKeyCRT::NewLC(RInteger& aN, RInteger& aP, 
sl@0: 	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv)
sl@0: 	{
sl@0: 	CRSAPrivateKeyCRT* self = new(ELeave) CRSAPrivateKeyCRT(aN, aP, aQ, 
sl@0: 		aDP, aDQ, aQInv);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyCRT::CRSAPrivateKeyCRT(RInteger& aN, RInteger& aP, 
sl@0: 	RInteger& aQ, RInteger& aDP, RInteger& aDQ, RInteger& aQInv) 
sl@0: 	: CRSAPrivateKey(EStandardCRT, aN), iP(aP), iQ(aQ), iDP(aDP), iDQ(aDQ), 
sl@0: 		iQInv(aQInv)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: void CRSAPrivateKeyCRT::ConstructL()
sl@0: 	{
sl@0: 	// Check that all parameters are positive integers
sl@0: 	if(!P().IsPositive() || !Q().IsPositive() || !DP().IsPositive() 
sl@0: 		|| !DQ().IsPositive() || !QInv().IsPositive())
sl@0: 		{
sl@0: 		// If we need to leave during construction we must release ownership
sl@0: 		// of the RInteger parameters that were passed in.
sl@0: 		// These parameters should be on the cleanup stack so if we don't 
sl@0: 		// release ownership they will be deleted twice, causing a panic
sl@0: 		iN = RInteger();
sl@0: 		iP = RInteger();
sl@0: 		iQ = RInteger();
sl@0: 		iDP = RInteger();
sl@0: 		iDQ = RInteger();
sl@0: 		iQInv = RInteger();
sl@0: 		User::Leave(KErrArgument);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: EXPORT_C CRSAPrivateKeyCRT::~CRSAPrivateKeyCRT()
sl@0: 	{	
sl@0: 	iP.Close();
sl@0: 	iQ.Close();
sl@0: 	iDP.Close();
sl@0: 	iDQ.Close();
sl@0: 	iQInv.Close();
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPrivateKeyCRT::P(void) const
sl@0: 	{
sl@0: 	return iP;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPrivateKeyCRT::Q(void) const
sl@0: 	{
sl@0: 	return iQ;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPrivateKeyCRT::DP(void) const
sl@0: 	{
sl@0: 	return iDP;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPrivateKeyCRT::DQ(void) const
sl@0: 	{
sl@0: 	return iDQ;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const TInteger& CRSAPrivateKeyCRT::QInv(void) const
sl@0: 	{
sl@0: 	return iQInv;
sl@0: 	}
sl@0: 
sl@0: /* CRSAKeyPair */
sl@0: 
sl@0: EXPORT_C CRSAKeyPair* CRSAKeyPair::NewL(TUint aModulusBits, 
sl@0: 	TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
sl@0: 	{
sl@0: 	CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
sl@0: 	CleanupStack::Pop();
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAKeyPair* CRSAKeyPair::NewLC(TUint aModulusBits, 
sl@0: 	TRSAPrivateKeyType aKeyType /*= EStandardCRT*/)
sl@0: 	{
sl@0: 	CRSAKeyPairShim* self = CRSAKeyPairShim::NewLC(aModulusBits, aKeyType);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C const CRSAPublicKey& CRSAKeyPair::PublicKey(void) const
sl@0: 	{
sl@0: 	return *iPublic;
sl@0: 	}
sl@0: 	
sl@0: EXPORT_C const CRSAPrivateKey& CRSAKeyPair::PrivateKey(void) const
sl@0: 	{
sl@0: 	return *iPrivate;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAKeyPair::~CRSAKeyPair(void)
sl@0: 	{
sl@0: 	delete iPublic;
sl@0: 	delete iPrivate;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C CRSAKeyPair::CRSAKeyPair(void)
sl@0: 	{
sl@0: 	}
sl@0: