os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/dhkeypairgenimpl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2009 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 "dhkeypairgenimpl.h"
sl@0
    20
#include "pluginconfig.h"
sl@0
    21
#include "keypair.h"
sl@0
    22
sl@0
    23
using namespace SoftwareCrypto;
sl@0
    24
using namespace CryptoSpi;
sl@0
    25
sl@0
    26
/* CDHKeyPairGenImpl */
sl@0
    27
CDHKeyPairGenImpl::CDHKeyPairGenImpl()
sl@0
    28
	{
sl@0
    29
	
sl@0
    30
	}
sl@0
    31
sl@0
    32
CDHKeyPairGenImpl::~CDHKeyPairGenImpl()
sl@0
    33
	{
sl@0
    34
	
sl@0
    35
	}
sl@0
    36
sl@0
    37
CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewL(void)
sl@0
    38
	{
sl@0
    39
	CDHKeyPairGenImpl* self = CDHKeyPairGenImpl::NewLC();
sl@0
    40
	CleanupStack::Pop(self);
sl@0
    41
	return self;
sl@0
    42
	}
sl@0
    43
sl@0
    44
CDHKeyPairGenImpl* CDHKeyPairGenImpl::NewLC(void)
sl@0
    45
	{
sl@0
    46
	CDHKeyPairGenImpl* self = new(ELeave) CDHKeyPairGenImpl();
sl@0
    47
	CleanupStack::PushL(self);
sl@0
    48
	self->ConstructL();
sl@0
    49
	return self;
sl@0
    50
	}
sl@0
    51
sl@0
    52
void CDHKeyPairGenImpl::ConstructL(void)
sl@0
    53
	{
sl@0
    54
	CKeyPairGenImpl::ConstructL();
sl@0
    55
	}
sl@0
    56
sl@0
    57
CExtendedCharacteristics* CDHKeyPairGenImpl::CreateExtendedCharacteristicsL()
sl@0
    58
	{
sl@0
    59
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    60
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    61
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    62
	}
sl@0
    63
sl@0
    64
const CExtendedCharacteristics* CDHKeyPairGenImpl::GetExtendedCharacteristicsL()
sl@0
    65
	{
sl@0
    66
	return CDHKeyPairGenImpl::CreateExtendedCharacteristicsL();
sl@0
    67
	}
sl@0
    68
sl@0
    69
TUid CDHKeyPairGenImpl::ImplementationUid() const
sl@0
    70
	{
sl@0
    71
	return KCryptoPluginDhKeyPairGenUid;
sl@0
    72
	}
sl@0
    73
sl@0
    74
void CDHKeyPairGenImpl::Reset()
sl@0
    75
	{
sl@0
    76
	// does nothing in this plugin
sl@0
    77
	}
sl@0
    78
sl@0
    79
void CDHKeyPairGenImpl::GenerateKeyPairL(TInt /*aKeySize*/, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
sl@0
    80
	{
sl@0
    81
	/*
sl@0
    82
	 * unpack the parameters, we're expecting the N and G parameters and if present the x parameter (aka private key)
sl@0
    83
	 */
sl@0
    84
	const TInteger& N = aKeyParameters.GetBigIntL(KDhKeyParameterNUid);
sl@0
    85
	const TInteger& G = aKeyParameters.GetBigIntL(KDhKeyParameterGUid);
sl@0
    86
sl@0
    87
	/*
sl@0
    88
	 * do some sanity checking
sl@0
    89
	 */
sl@0
    90
	RInteger nminus2 = RInteger::NewL(N);
sl@0
    91
	CleanupStack::PushL(nminus2);
sl@0
    92
	--nminus2;
sl@0
    93
	--nminus2;
sl@0
    94
sl@0
    95
	if ((G < TInteger::Two()) || (G > nminus2))
sl@0
    96
		{
sl@0
    97
		User::Leave(KErrArgument);
sl@0
    98
		}
sl@0
    99
sl@0
   100
	/*
sl@0
   101
	 * has a private key x been supplied? if not then generate it
sl@0
   102
	 */
sl@0
   103
	RInteger x;
sl@0
   104
	if (aKeyParameters.IsPresent(KDhKeyParameterxUid))
sl@0
   105
		{
sl@0
   106
		x = RInteger::NewL(aKeyParameters.GetBigIntL(KDhKeyParameterxUid));
sl@0
   107
		}
sl@0
   108
		else
sl@0
   109
		{
sl@0
   110
		// find a random x | 1 <= x <= n-2
sl@0
   111
		x = RInteger::NewRandomL(TInteger::One(), nminus2);
sl@0
   112
		}
sl@0
   113
	CleanupClosePushL(x);
sl@0
   114
	/*
sl@0
   115
	 * generate the public key with X = G^(x) mod N
sl@0
   116
	 */
sl@0
   117
	RInteger X = TInteger::ModularExponentiateL(G, x, N);
sl@0
   118
	CleanupClosePushL(X);
sl@0
   119
sl@0
   120
	/*
sl@0
   121
	 * create the keys parameters
sl@0
   122
	  */
sl@0
   123
	CCryptoParams* publicKeyParameters = CCryptoParams::NewLC();
sl@0
   124
	publicKeyParameters->AddL(X, KDhKeyParameterXUid);
sl@0
   125
	TKeyProperty publicKeyProperties = {KDHKeyPairGeneratorUid, 	KCryptoPluginDhKeyPairGenUid, 
sl@0
   126
									KDHPublicKeyUid, KNonEmbeddedKeyUid };
sl@0
   127
	CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
sl@0
   128
	privateKeyParameters->AddL(x, KDhKeyParameterxUid);
sl@0
   129
	TKeyProperty privateKeyProperties = {KDHKeyPairGeneratorUid, KCryptoPluginDhKeyPairGenUid,
sl@0
   130
									KDHPrivateKeyUid, KNonEmbeddedKeyUid };
sl@0
   131
sl@0
   132
	/*
sl@0
   133
	 * create the public key
sl@0
   134
	 */
sl@0
   135
	CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
sl@0
   136
	CleanupStack::PushL(publicKey);
sl@0
   137
sl@0
   138
	/*
sl@0
   139
	 * create the private key
sl@0
   140
	 */
sl@0
   141
	CKey* privateKey = CKey::NewL(privateKeyProperties, *privateKeyParameters);
sl@0
   142
	CleanupStack::PushL(privateKey);
sl@0
   143
sl@0
   144
	/*
sl@0
   145
	 * create the key pair
sl@0
   146
	 */
sl@0
   147
	aKeyPair = CKeyPair::NewL(publicKey, privateKey);
sl@0
   148
	
sl@0
   149
	/* 
sl@0
   150
	 * cleanup stack - it should contain nminus2, x (if allocated here), X, publicKeyParameters, privateKeyParameters, publicKey and privateKey
sl@0
   151
	 */
sl@0
   152
	CleanupStack::Pop(2, publicKey);
sl@0
   153
	CleanupStack::PopAndDestroy(5, &nminus2);
sl@0
   154
	}