os/security/crypto/weakcryptospi/source/asymmetric/rsakeypairshim.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2010 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 "rsakeypairshim.h"
    20 #include <cryptospi/cryptokeypairgeneratorapi.h>
    21 #include <cryptospi/keypair.h>
    22 #include <cryptospi/cryptoparams.h>
    23 #include <cryptospi/cryptospidef.h>
    24 
    25 using namespace CryptoSpi;
    26 
    27 /* CRSAKeyPair */
    28 
    29 const TUint KFermat4 = 65537;
    30 
    31 CRSAKeyPairShim* CRSAKeyPairShim::NewLC(TUint aModulusBits, TRSAPrivateKeyType aKeyType)
    32 	{
    33 	CRSAKeyPairShim* self = new(ELeave) CRSAKeyPairShim();
    34 	CleanupStack::PushL(self);
    35 	self->ConstructL(aModulusBits, aKeyType, KFermat4);
    36 	return self;
    37 	}
    38 
    39 CRSAKeyPairShim::~CRSAKeyPairShim(void)
    40 	{
    41 	}
    42 
    43 CRSAKeyPairShim::CRSAKeyPairShim(void)
    44 	{
    45 	}
    46 
    47 void CRSAKeyPairShim::ConstructL(TUint aModulusBits, TRSAPrivateKeyType aKeyType, TInt aPublicExponent)
    48 	{
    49 	CKeyPairGenerator* keyPairGeneratorImpl=NULL;
    50 	CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
    51 											keyPairGeneratorImpl,
    52 											KRSAKeyPairGeneratorUid,
    53 											NULL);
    54 	CleanupStack::PushL(keyPairGeneratorImpl);
    55 
    56 	// put the RSA parameters into an array
    57 	CCryptoParams* keyParameters = CCryptoParams::NewLC();
    58 	keyParameters->AddL(aPublicExponent, KRsaKeyParameterEUid);
    59 	if (aKeyType == EStandard)
    60 		{
    61 		keyParameters->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
    62 		}
    63 	else if (aKeyType == EStandardCRT)
    64 		{
    65 		keyParameters->AddL(KRsaPrivateKeyCRT, KRsaKeyTypeUid);
    66 		}
    67 
    68 	// call the api to create an RSA key pair
    69 	CKeyPair* keyPair = 0;
    70 	keyPairGeneratorImpl->GenerateKeyPairL(aModulusBits, *keyParameters, keyPair);
    71 	
    72 	CleanupStack::PushL(keyPair);
    73 
    74 	/* 
    75 	 * Convert the CKeyPair to CRSAPrivateKey{CRT/Standard} and CRSAPublicKey
    76 	 * The public key becomes the owner of n and e RIntegers
    77 	 */
    78 	RInteger n = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KRsaKeyParameterNUid));
    79 	CleanupClosePushL(n);
    80 	RInteger e = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KRsaKeyParameterEUid));
    81 	CleanupClosePushL(e);
    82 	iPublic = CRSAPublicKey::NewL(n, e);
    83 
    84 	if (aKeyType == EStandard)
    85 		{
    86 		RInteger n = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterNUid));
    87 		CleanupClosePushL(n);
    88 		RInteger d = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDUid));
    89 		CleanupClosePushL(d);
    90 		iPrivate = CRSAPrivateKeyStandard::NewL(n, d);
    91 		CleanupStack::Pop(2, &n);
    92 		}
    93 	else if (aKeyType == EStandardCRT)
    94 		{
    95 		RInteger n = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterNUid));
    96 		CleanupClosePushL(n);
    97 		RInteger p = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterPUid));
    98 		CleanupClosePushL(p);
    99 		RInteger q = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterQUid));
   100 		CleanupClosePushL(q);
   101 		RInteger dp = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDPUid));
   102 		CleanupClosePushL(p);
   103 		RInteger dq = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDQUid));
   104 		CleanupClosePushL(q);
   105 		RInteger qinv = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterQInvUid));
   106 		CleanupClosePushL(qinv);
   107 		iPrivate = CRSAPrivateKeyCRT::NewL(n, p, q, dp, dq, qinv);
   108 		CleanupStack::Pop(6, &n);
   109 		}
   110 	/*
   111 	 * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, n, e
   112 	 */
   113 	CleanupStack::Pop(2, &n);
   114 	CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);
   115 	}