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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "rsakeypairshim.h"
sl@0
    20
#include <cryptospi/cryptokeypairgeneratorapi.h>
sl@0
    21
#include <cryptospi/keypair.h>
sl@0
    22
#include <cryptospi/cryptoparams.h>
sl@0
    23
#include <cryptospi/cryptospidef.h>
sl@0
    24
sl@0
    25
using namespace CryptoSpi;
sl@0
    26
sl@0
    27
/* CRSAKeyPair */
sl@0
    28
sl@0
    29
const TUint KFermat4 = 65537;
sl@0
    30
sl@0
    31
CRSAKeyPairShim* CRSAKeyPairShim::NewLC(TUint aModulusBits, TRSAPrivateKeyType aKeyType)
sl@0
    32
	{
sl@0
    33
	CRSAKeyPairShim* self = new(ELeave) CRSAKeyPairShim();
sl@0
    34
	CleanupStack::PushL(self);
sl@0
    35
	self->ConstructL(aModulusBits, aKeyType, KFermat4);
sl@0
    36
	return self;
sl@0
    37
	}
sl@0
    38
sl@0
    39
CRSAKeyPairShim::~CRSAKeyPairShim(void)
sl@0
    40
	{
sl@0
    41
	}
sl@0
    42
sl@0
    43
CRSAKeyPairShim::CRSAKeyPairShim(void)
sl@0
    44
	{
sl@0
    45
	}
sl@0
    46
sl@0
    47
void CRSAKeyPairShim::ConstructL(TUint aModulusBits, TRSAPrivateKeyType aKeyType, TInt aPublicExponent)
sl@0
    48
	{
sl@0
    49
	CKeyPairGenerator* keyPairGeneratorImpl=NULL;
sl@0
    50
	CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
sl@0
    51
											keyPairGeneratorImpl,
sl@0
    52
											KRSAKeyPairGeneratorUid,
sl@0
    53
											NULL);
sl@0
    54
	CleanupStack::PushL(keyPairGeneratorImpl);
sl@0
    55
sl@0
    56
	// put the RSA parameters into an array
sl@0
    57
	CCryptoParams* keyParameters = CCryptoParams::NewLC();
sl@0
    58
	keyParameters->AddL(aPublicExponent, KRsaKeyParameterEUid);
sl@0
    59
	if (aKeyType == EStandard)
sl@0
    60
		{
sl@0
    61
		keyParameters->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
sl@0
    62
		}
sl@0
    63
	else if (aKeyType == EStandardCRT)
sl@0
    64
		{
sl@0
    65
		keyParameters->AddL(KRsaPrivateKeyCRT, KRsaKeyTypeUid);
sl@0
    66
		}
sl@0
    67
sl@0
    68
	// call the api to create an RSA key pair
sl@0
    69
	CKeyPair* keyPair = 0;
sl@0
    70
	keyPairGeneratorImpl->GenerateKeyPairL(aModulusBits, *keyParameters, keyPair);
sl@0
    71
	
sl@0
    72
	CleanupStack::PushL(keyPair);
sl@0
    73
sl@0
    74
	/* 
sl@0
    75
	 * Convert the CKeyPair to CRSAPrivateKey{CRT/Standard} and CRSAPublicKey
sl@0
    76
	 * The public key becomes the owner of n and e RIntegers
sl@0
    77
	 */
sl@0
    78
	RInteger n = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KRsaKeyParameterNUid));
sl@0
    79
	CleanupClosePushL(n);
sl@0
    80
	RInteger e = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KRsaKeyParameterEUid));
sl@0
    81
	CleanupClosePushL(e);
sl@0
    82
	iPublic = CRSAPublicKey::NewL(n, e);
sl@0
    83
sl@0
    84
	if (aKeyType == EStandard)
sl@0
    85
		{
sl@0
    86
		RInteger n = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterNUid));
sl@0
    87
		CleanupClosePushL(n);
sl@0
    88
		RInteger d = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDUid));
sl@0
    89
		CleanupClosePushL(d);
sl@0
    90
		iPrivate = CRSAPrivateKeyStandard::NewL(n, d);
sl@0
    91
		CleanupStack::Pop(2, &n);
sl@0
    92
		}
sl@0
    93
	else if (aKeyType == EStandardCRT)
sl@0
    94
		{
sl@0
    95
		RInteger n = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterNUid));
sl@0
    96
		CleanupClosePushL(n);
sl@0
    97
		RInteger p = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterPUid));
sl@0
    98
		CleanupClosePushL(p);
sl@0
    99
		RInteger q = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterQUid));
sl@0
   100
		CleanupClosePushL(q);
sl@0
   101
		RInteger dp = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDPUid));
sl@0
   102
		CleanupClosePushL(p);
sl@0
   103
		RInteger dq = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterDQUid));
sl@0
   104
		CleanupClosePushL(q);
sl@0
   105
		RInteger qinv = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KRsaKeyParameterQInvUid));
sl@0
   106
		CleanupClosePushL(qinv);
sl@0
   107
		iPrivate = CRSAPrivateKeyCRT::NewL(n, p, q, dp, dq, qinv);
sl@0
   108
		CleanupStack::Pop(6, &n);
sl@0
   109
		}
sl@0
   110
	/*
sl@0
   111
	 * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, n, e
sl@0
   112
	 */
sl@0
   113
	CleanupStack::Pop(2, &n);
sl@0
   114
	CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);
sl@0
   115
	}