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 <bigint.h>
sl@0: #include "../common/inlines.h"
sl@0: 
sl@0: const TUint KFermat4 = 65537;
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: 	CRSAKeyPair* self = 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: 	CRSAKeyPair* self = new(ELeave) CRSAKeyPair();
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aModulusBits, aKeyType, KFermat4);
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: 
sl@0: void CRSAKeyPair::ConstructL(TUint aModulusBits, 
sl@0: 	TRSAPrivateKeyType aKeyType, TUint aPublicExponent)
sl@0: 	{
sl@0: 	RInteger e = RInteger::NewL(aPublicExponent);
sl@0: 	CleanupStack::PushL(e);
sl@0: 
sl@0: 	RInteger p;
sl@0: 	RInteger q;
sl@0: 	
sl@0: 	//these make sure n is a least aModulusBits long
sl@0:     TInt pbits=(aModulusBits+1)/2;
sl@0:     TInt qbits=aModulusBits-pbits;
sl@0: 
sl@0: 	//generate a prime p such that GCD(e,p-1) == 1
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
sl@0: 		CleanupStack::PushL(p);
sl@0: 		--p;
sl@0: 
sl@0: 		RInteger gcd = e.GCDL(p);
sl@0: 		if( gcd == 1 )
sl@0: 			{
sl@0: 			++p;
sl@0: 			gcd.Close();
sl@0: 			//p is still on cleanup stack
sl@0: 			break;
sl@0: 			}
sl@0: 		CleanupStack::PopAndDestroy(&p);
sl@0: 		gcd.Close();
sl@0: 		}
sl@0: 
sl@0: 	//generate a prime q such that GCD(e,q-1) == 1 && (p != q)
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
sl@0: 		CleanupStack::PushL(q);
sl@0: 		--q;
sl@0: 
sl@0: 		RInteger gcd = e.GCDL(q);
sl@0: 		if( gcd == 1 )
sl@0: 			{
sl@0: 			++q;
sl@0: 			if( p != q )
sl@0: 				{
sl@0: 				gcd.Close();
sl@0: 				//q is still on cleanup stack
sl@0: 				break;
sl@0: 				}
sl@0: 			}
sl@0: 		CleanupStack::PopAndDestroy(&q);
sl@0: 		gcd.Close();
sl@0: 		}
sl@0: 		
sl@0: 	//make sure p > q
sl@0:     if ( p < q)
sl@0:         {
sl@0: 		TClassSwap(p,q);
sl@0:         }
sl@0: 
sl@0: 	//calculate n = p * q 
sl@0: 	RInteger n = p.TimesL(q);
sl@0: 	CleanupStack::PushL(n);
sl@0: 
sl@0: 	--p;
sl@0: 	--q;
sl@0: 
sl@0: 	//temp = (p-1)(q-1)
sl@0: 	RInteger temp = p.TimesL(q);
sl@0: 	CleanupStack::PushL(temp);
sl@0: 
sl@0: 	//e * d = 1 mod ((p-1)(q-1)) 
sl@0: 	//d = e^(-1) mod ((p-1)(q-1))
sl@0: 	RInteger d = e.InverseModL(temp);
sl@0: 	CleanupStack::PopAndDestroy(&temp); //temp
sl@0: 	CleanupStack::PushL(d);
sl@0: 
sl@0: 	if (aKeyType==EStandardCRT)
sl@0: 	{
sl@0: 		//calculate dP = d mod (p-1) 
sl@0: 		RInteger dP = d.ModuloL(p); //p is still p-1
sl@0: 		CleanupStack::PushL(dP);
sl@0: 
sl@0: 		//calculate dQ = d mod (q-1) 
sl@0: 		RInteger dQ = d.ModuloL(q); //q is still q-1
sl@0: 		CleanupStack::PushL(dQ);
sl@0: 
sl@0: 		++p;
sl@0: 		++q;
sl@0: 		//calculate inverse of qInv = q^(-1)mod(p)
sl@0: 		RInteger qInv = q.InverseModL(p);
sl@0: 		CleanupStack::PushL(qInv);
sl@0: 
sl@0: 		iPrivate = CRSAPrivateKeyCRT::NewL(n,p,q,dP,dQ,qInv);
sl@0: 		
sl@0: 		CleanupStack::Pop(3, &dP); //qInv, dQ, dP
sl@0: 		CleanupStack::PopAndDestroy(&d); //d	
sl@0: 		CleanupStack::Pop(3, &p); //n, q, p
sl@0: 		//e is still on cleanup stack
sl@0: 	}
sl@0: 	else if (aKeyType==EStandard)
sl@0: 	{
sl@0: 		iPrivate = CRSAPrivateKeyStandard::NewL(n,d);
sl@0: 
sl@0: 		CleanupStack::Pop(2, &n); //d, n
sl@0: 		CleanupStack::PopAndDestroy(2, &p); //q, p
sl@0: 		//e is still on cleanup stack
sl@0: 	}
sl@0: 	else
sl@0: 	{
sl@0: 		User::Leave(KErrNotSupported);
sl@0: 	}
sl@0: 
sl@0: 	//make a copy of n for the public parameters
sl@0: 	RInteger n1 = RInteger::NewL(PrivateKey().N());
sl@0: 	CleanupStack::PushL(n1);
sl@0: 	iPublic = CRSAPublicKey::NewL(n1,e); 
sl@0: 	CleanupStack::Pop(2, &e); //n1, e
sl@0: 	}
sl@0: