cryptoplugins/cryptospiplugins/source/softwarecrypto/rsakeypairgenimpl.cpp
changeset 19 cd501b96611d
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     1 /*
       
     2 * Copyright (c) 2006-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 * RSA Keypair implementation
       
    16 * RSA keypair generation implementation
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include "rsakeypairgenimpl.h"
       
    26 #include "pluginconfig.h"
       
    27 
       
    28 #include "keypair.h"
       
    29 #include <cryptospi/cryptospidef.h>
       
    30 
       
    31 #include "common/inlines.h"    // For TClassSwap
       
    32 
       
    33 using namespace SoftwareCrypto;
       
    34 
       
    35 /* CRSAKeyPairGenImpl */
       
    36 CRSAKeyPairGenImpl::CRSAKeyPairGenImpl()
       
    37 	{
       
    38 	}
       
    39 
       
    40 CRSAKeyPairGenImpl::~CRSAKeyPairGenImpl()
       
    41 	{
       
    42 	}
       
    43 
       
    44 CRSAKeyPairGenImpl* CRSAKeyPairGenImpl::NewL(void)
       
    45 	{
       
    46 	CRSAKeyPairGenImpl* self = CRSAKeyPairGenImpl::NewLC();
       
    47 	CleanupStack::Pop(self);
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 CRSAKeyPairGenImpl* CRSAKeyPairGenImpl::NewLC(void)
       
    52 	{
       
    53 	CRSAKeyPairGenImpl* self = new(ELeave) CRSAKeyPairGenImpl();
       
    54 	CleanupStack::PushL(self);
       
    55 	self->ConstructL();
       
    56 	return self;
       
    57 	}
       
    58 
       
    59 void CRSAKeyPairGenImpl::ConstructL(void)
       
    60 	{
       
    61 	CKeyPairGenImpl::ConstructL();
       
    62 	}
       
    63 
       
    64 CExtendedCharacteristics* CRSAKeyPairGenImpl::CreateExtendedCharacteristicsL()
       
    65 	{
       
    66 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
       
    67 	// for exclusive use and are not CERTIFIED to be standards compliant.
       
    68 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
       
    69 }
       
    70 
       
    71 const CExtendedCharacteristics* CRSAKeyPairGenImpl::GetExtendedCharacteristicsL()
       
    72 	{
       
    73 	return CRSAKeyPairGenImpl::CreateExtendedCharacteristicsL();
       
    74 	}
       
    75 
       
    76 TUid CRSAKeyPairGenImpl::ImplementationUid() const
       
    77 	{
       
    78 	return KCryptoPluginRsaKeyPairGenUid;
       
    79 	}
       
    80 
       
    81 void CRSAKeyPairGenImpl::Reset()
       
    82 	{
       
    83 	// does nothing in this plugin
       
    84 	}
       
    85 
       
    86 void CRSAKeyPairGenImpl::GenerateKeyPairL(TInt aKeySize, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
       
    87 	{
       
    88 	/*
       
    89 	 * extract e
       
    90 	 */ 
       
    91 	const TInt aKeyType = aKeyParameters.GetTIntL(KRsaKeyTypeUid);
       
    92 	const TInt aPublicExponent = aKeyParameters.GetTIntL(KRsaKeyParameterEUid);
       
    93 
       
    94 	RInteger e = RInteger::NewL(aPublicExponent);
       
    95 	CleanupStack::PushL(e);
       
    96 
       
    97 	/*
       
    98 	 * calculate p, q, n & d
       
    99 	 */ 
       
   100 	RInteger p;
       
   101 	RInteger q;
       
   102 	
       
   103 	//these make sure n is a least aKeySize long
       
   104 	TInt pbits=(aKeySize+1)/2;
       
   105 	TInt qbits=aKeySize-pbits;
       
   106 
       
   107 	//generate a prime p such that GCD(e,p-1) == 1
       
   108 	for (;;)
       
   109 		{
       
   110 		p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
       
   111 		CleanupStack::PushL(p);
       
   112 		--p;
       
   113 
       
   114 		RInteger gcd = e.GCDL(p);
       
   115 		if( gcd == 1 )
       
   116 			{
       
   117 			++p;
       
   118 			gcd.Close();
       
   119 			//p is still on cleanup stack
       
   120 			break;
       
   121 			}
       
   122 		CleanupStack::PopAndDestroy(&p);
       
   123 		gcd.Close();
       
   124 		}
       
   125 
       
   126 	//generate a prime q such that GCD(e,q-1) == 1 && (p != q)
       
   127 	for (;;)
       
   128 		{
       
   129 		q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
       
   130 		CleanupStack::PushL(q);
       
   131 		--q;
       
   132 
       
   133 		RInteger gcd = e.GCDL(q);
       
   134 		if( gcd == 1 )
       
   135 			{
       
   136 			++q;
       
   137 			if( p != q )
       
   138 				{
       
   139 				gcd.Close();
       
   140 				//q is still on cleanup stack
       
   141 				break;
       
   142 				}
       
   143 			}
       
   144 		CleanupStack::PopAndDestroy(&q);
       
   145 		gcd.Close();
       
   146 		}
       
   147 		
       
   148 	//make sure p > q
       
   149 	if ( p < q)
       
   150 		{
       
   151 		TClassSwap(p,q);
       
   152 		}
       
   153 
       
   154 	//calculate n = p * q
       
   155 	RInteger n = p.TimesL(q);
       
   156 	CleanupStack::PushL(n);
       
   157 
       
   158 	--p;
       
   159 	--q;
       
   160 
       
   161 	//temp = (p-1)(q-1)
       
   162 	RInteger temp = p.TimesL(q);
       
   163 	CleanupStack::PushL(temp);
       
   164 
       
   165 	//e * d = 1 mod ((p-1)(q-1))
       
   166 	//d = e^(-1) mod ((p-1)(q-1))
       
   167 	RInteger d = e.InverseModL(temp);
       
   168 	CleanupStack::PopAndDestroy(&temp); //temp
       
   169 	CleanupStack::PushL(d);
       
   170 
       
   171 	/*
       
   172 	 * create private key depending on aKeyType
       
   173 	 */ 
       
   174 	CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
       
   175 	privateKeyParameters->AddL(n, KRsaKeyParameterNUid);
       
   176 	TKeyProperty* privateKeyProperties = NULL;
       
   177 	TKeyProperty privateKeyProperties_RsaPrivateKeyCRT = {KRSAKeyPairGeneratorUid, KCryptoPluginRsaKeyPairGenUid,
       
   178 									KRsaPrivateKeyCRTUid, KNonEmbeddedKeyUid };
       
   179 	TKeyProperty privateKeyProperties_RsaPrivateKeyStandard = {KRSAKeyPairGeneratorUid, KCryptoPluginRsaKeyPairGenUid,
       
   180 									KRsaPrivateKeyStandardUid, KNonEmbeddedKeyUid };
       
   181 
       
   182 	CCryptoParams*publicKeyParameters = CCryptoParams::NewLC();
       
   183 	publicKeyParameters->AddL(n, KRsaKeyParameterNUid);
       
   184 	publicKeyParameters->AddL(e, KRsaKeyParameterEUid);
       
   185 	TKeyProperty publicKeyProperties = {KRSAKeyPairGeneratorUid, KCryptoPluginRsaKeyPairGenUid,
       
   186 									KRsaPublicKeyUid, KNonEmbeddedKeyUid };
       
   187 
       
   188 	if (aKeyType == KRsaPrivateKeyCRT)			// cleanup stack contains e, p, q, n, d and privateKeyParameters
       
   189 	{
       
   190 
       
   191 		/*
       
   192 		 * calculate dP, dQ and qInv
       
   193 		 */ 
       
   194 		//calculate dP = d mod (p-1)
       
   195 		RInteger dP = d.ModuloL(p); //p is still p-1
       
   196 		CleanupStack::PushL(dP);
       
   197 		privateKeyParameters->AddL(dP, KRsaKeyParameterDPUid);
       
   198 		CleanupStack::PopAndDestroy(&dP);
       
   199 
       
   200 		//calculate dQ = d mod (q-1)
       
   201 		RInteger dQ = d.ModuloL(q); //q is still q-1
       
   202 		CleanupStack::PushL(dQ);
       
   203 		privateKeyParameters->AddL(dQ, KRsaKeyParameterDQUid);
       
   204 		CleanupStack::PopAndDestroy(&dQ);
       
   205 
       
   206 		++p;
       
   207 		++q;
       
   208 		//calculate inverse of qInv = q^(-1)mod(p)
       
   209 		RInteger qInv = q.InverseModL(p);
       
   210 		CleanupStack::PushL(qInv);
       
   211 		privateKeyParameters->AddL(qInv, KRsaKeyParameterQInvUid);
       
   212 		CleanupStack::PopAndDestroy(&qInv);
       
   213 		
       
   214 		privateKeyParameters->AddL(p, KRsaKeyParameterPUid);
       
   215 		privateKeyParameters->AddL(q, KRsaKeyParameterQUid);
       
   216 		
       
   217 		privateKeyProperties = &privateKeyProperties_RsaPrivateKeyCRT;
       
   218 	}
       
   219 	else if (aKeyType == KRsaPrivateKeyStandard)
       
   220 	{
       
   221 		privateKeyParameters->AddL(d, KRsaKeyParameterDUid);
       
   222 		privateKeyProperties = &privateKeyProperties_RsaPrivateKeyStandard;
       
   223 	}
       
   224 	else
       
   225 	{
       
   226 		User::Leave(KErrNotSupported);
       
   227 	}
       
   228 	// cleanup stack contains e, p, q, n, d and privateKeyParameters
       
   229 	CKey* privateKey = CKey::NewL(*privateKeyProperties, *privateKeyParameters);
       
   230 	CleanupStack::PushL(privateKey);
       
   231 
       
   232 	/*
       
   233 	 * create public key
       
   234 	 */
       
   235 	CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
       
   236 	CleanupStack::PushL(publicKey);
       
   237 
       
   238 	/*
       
   239 	* create the key pair
       
   240 	*/
       
   241 	aKeyPair = CKeyPair::NewL(publicKey, privateKey);
       
   242 
       
   243 	CleanupStack::Pop(2, privateKey); //privateKey and publicKey
       
   244 	CleanupStack::PopAndDestroy(7, &e); //e, p, q, n, d, privateKeyParameters and publicKeyParameters
       
   245 	}