os/security/crypto/weakcryptospi/source/asymmetric/rsakeypairshim.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/source/asymmetric/rsakeypairshim.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2010 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 "rsakeypairshim.h"
    1.23 +#include <cryptospi/cryptokeypairgeneratorapi.h>
    1.24 +#include <cryptospi/keypair.h>
    1.25 +#include <cryptospi/cryptoparams.h>
    1.26 +#include <cryptospi/cryptospidef.h>
    1.27 +
    1.28 +using namespace CryptoSpi;
    1.29 +
    1.30 +/* CRSAKeyPair */
    1.31 +
    1.32 +const TUint KFermat4 = 65537;
    1.33 +
    1.34 +CRSAKeyPairShim* CRSAKeyPairShim::NewLC(TUint aModulusBits, TRSAPrivateKeyType aKeyType)
    1.35 +	{
    1.36 +	CRSAKeyPairShim* self = new(ELeave) CRSAKeyPairShim();
    1.37 +	CleanupStack::PushL(self);
    1.38 +	self->ConstructL(aModulusBits, aKeyType, KFermat4);
    1.39 +	return self;
    1.40 +	}
    1.41 +
    1.42 +CRSAKeyPairShim::~CRSAKeyPairShim(void)
    1.43 +	{
    1.44 +	}
    1.45 +
    1.46 +CRSAKeyPairShim::CRSAKeyPairShim(void)
    1.47 +	{
    1.48 +	}
    1.49 +
    1.50 +void CRSAKeyPairShim::ConstructL(TUint aModulusBits, TRSAPrivateKeyType aKeyType, TInt aPublicExponent)
    1.51 +	{
    1.52 +	CKeyPairGenerator* keyPairGeneratorImpl=NULL;
    1.53 +	CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
    1.54 +											keyPairGeneratorImpl,
    1.55 +											KRSAKeyPairGeneratorUid,
    1.56 +											NULL);
    1.57 +	CleanupStack::PushL(keyPairGeneratorImpl);
    1.58 +
    1.59 +	// put the RSA parameters into an array
    1.60 +	CCryptoParams* keyParameters = CCryptoParams::NewLC();
    1.61 +	keyParameters->AddL(aPublicExponent, KRsaKeyParameterEUid);
    1.62 +	if (aKeyType == EStandard)
    1.63 +		{
    1.64 +		keyParameters->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
    1.65 +		}
    1.66 +	else if (aKeyType == EStandardCRT)
    1.67 +		{
    1.68 +		keyParameters->AddL(KRsaPrivateKeyCRT, KRsaKeyTypeUid);
    1.69 +		}
    1.70 +
    1.71 +	// call the api to create an RSA key pair
    1.72 +	CKeyPair* keyPair = 0;
    1.73 +	keyPairGeneratorImpl->GenerateKeyPairL(aModulusBits, *keyParameters, keyPair);
    1.74 +	
    1.75 +	CleanupStack::PushL(keyPair);
    1.76 +
    1.77 +	/* 
    1.78 +	 * Convert the CKeyPair to CRSAPrivateKey{CRT/Standard} and CRSAPublicKey
    1.79 +	 * The public key becomes the owner of n and e RIntegers
    1.80 +	 */
    1.81 +	RInteger n = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KRsaKeyParameterNUid));
    1.82 +	CleanupClosePushL(n);
    1.83 +	RInteger e = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KRsaKeyParameterEUid));
    1.84 +	CleanupClosePushL(e);
    1.85 +	iPublic = CRSAPublicKey::NewL(n, e);
    1.86 +
    1.87 +	if (aKeyType == EStandard)
    1.88 +		{
    1.89 +		RInteger n = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterNUid));
    1.90 +		CleanupClosePushL(n);
    1.91 +		RInteger d = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDUid));
    1.92 +		CleanupClosePushL(d);
    1.93 +		iPrivate = CRSAPrivateKeyStandard::NewL(n, d);
    1.94 +		CleanupStack::Pop(2, &n);
    1.95 +		}
    1.96 +	else if (aKeyType == EStandardCRT)
    1.97 +		{
    1.98 +		RInteger n = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterNUid));
    1.99 +		CleanupClosePushL(n);
   1.100 +		RInteger p = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterPUid));
   1.101 +		CleanupClosePushL(p);
   1.102 +		RInteger q = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterQUid));
   1.103 +		CleanupClosePushL(q);
   1.104 +		RInteger dp = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDPUid));
   1.105 +		CleanupClosePushL(p);
   1.106 +		RInteger dq = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDQUid));
   1.107 +		CleanupClosePushL(q);
   1.108 +		RInteger qinv = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterQInvUid));
   1.109 +		CleanupClosePushL(qinv);
   1.110 +		iPrivate = CRSAPrivateKeyCRT::NewL(n, p, q, dp, dq, qinv);
   1.111 +		CleanupStack::Pop(6, &n);
   1.112 +		}
   1.113 +	/*
   1.114 +	 * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, n, e
   1.115 +	 */
   1.116 +	CleanupStack::Pop(2, &n);
   1.117 +	CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);
   1.118 +	}