os/security/crypto/weakcryptospi/source/asymmetric/dhkeypairshim.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-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 "dhkeypairshim.h"
sl@0
    20
#include <bigint.h>
sl@0
    21
#include <cryptospi/cryptokeypairgeneratorapi.h>
sl@0
    22
#include <cryptospi/keypair.h>
sl@0
    23
#include <cryptospi/cryptoparams.h>
sl@0
    24
#include <cryptospi/cryptospidef.h>
sl@0
    25
sl@0
    26
sl@0
    27
using namespace CryptoSpi;
sl@0
    28
sl@0
    29
sl@0
    30
/* CDHKeyPair */
sl@0
    31
CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG)
sl@0
    32
	{
sl@0
    33
	CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
sl@0
    34
	CleanupStack::PushL(self);
sl@0
    35
	self->ConstructL(aN, aG);
sl@0
    36
	return self;
sl@0
    37
	}
sl@0
    38
sl@0
    39
CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
sl@0
    40
	{
sl@0
    41
	CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
sl@0
    42
	CleanupStack::PushL(self);
sl@0
    43
	self->ConstructL(aN, aG, ax);
sl@0
    44
	return self;
sl@0
    45
	}
sl@0
    46
sl@0
    47
CDHKeyPairShim::~CDHKeyPairShim(void)
sl@0
    48
	{
sl@0
    49
	}
sl@0
    50
sl@0
    51
CDHKeyPairShim::CDHKeyPairShim(void)
sl@0
    52
	{
sl@0
    53
	}	
sl@0
    54
sl@0
    55
void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG)
sl@0
    56
	{
sl@0
    57
	RInteger x = RInteger::NewL();
sl@0
    58
	CleanupClosePushL(x);
sl@0
    59
	KeyConstructorL(aN, aG, x, EFalse);
sl@0
    60
	CleanupStack::PopAndDestroy(1, &x);
sl@0
    61
	}
sl@0
    62
sl@0
    63
void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
sl@0
    64
	{
sl@0
    65
	KeyConstructorL(aN, aG, ax, ETrue);
sl@0
    66
	}
sl@0
    67
sl@0
    68
void CDHKeyPairShim::KeyConstructorL(RInteger& aN, RInteger& aG, RInteger& ax, TBool xIncluded)
sl@0
    69
	{
sl@0
    70
	RInteger& nminus2 = aN;
sl@0
    71
	
sl@0
    72
	/*
sl@0
    73
	 * do some sanity checks
sl@0
    74
	 */
sl@0
    75
	--nminus2;
sl@0
    76
	--nminus2;
sl@0
    77
	if( aG < TInteger::Two() || aG > nminus2 )
sl@0
    78
		{
sl@0
    79
		User::Leave(KErrArgument);
sl@0
    80
		}
sl@0
    81
sl@0
    82
	if (xIncluded)
sl@0
    83
		{
sl@0
    84
		if( ax < TInteger::One() || ax > nminus2 )
sl@0
    85
			{
sl@0
    86
			User::Leave(KErrArgument);
sl@0
    87
			}
sl@0
    88
		}
sl@0
    89
sl@0
    90
	/*
sl@0
    91
	 *find out how big the key should be - the key must be in the range x | 1 <= x <= n-2
sl@0
    92
	 * nminus2 is the largest the key can be so get the number of bits required to represent that number
sl@0
    93
	 */
sl@0
    94
	const TInt keySize = nminus2.BitCount();
sl@0
    95
sl@0
    96
	// increment aN back to its original value
sl@0
    97
	++nminus2;
sl@0
    98
	++nminus2;
sl@0
    99
sl@0
   100
	// obtain an RSA key pair generator interface
sl@0
   101
	
sl@0
   102
	CKeyPairGenerator* keyPairGeneratorImpl=NULL;
sl@0
   103
	CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
sl@0
   104
											keyPairGeneratorImpl,
sl@0
   105
											KDHKeyPairGeneratorUid,
sl@0
   106
											NULL);
sl@0
   107
	CleanupStack::PushL(keyPairGeneratorImpl);
sl@0
   108
	
sl@0
   109
	/* 
sl@0
   110
	 * put the DH parameters into an array
sl@0
   111
	 */
sl@0
   112
	CCryptoParams* keyParameters = CCryptoParams::NewLC();
sl@0
   113
	keyParameters->AddL(aN, KDhKeyParameterNUid);
sl@0
   114
	keyParameters->AddL(aG, KDhKeyParameterGUid);
sl@0
   115
	if (xIncluded)
sl@0
   116
		{
sl@0
   117
		// the private key x has been supplied so add it to the params array so the key generator algo can use it
sl@0
   118
		keyParameters->AddL(ax, KDhKeyParameterxUid);
sl@0
   119
		ax.Close();
sl@0
   120
		}
sl@0
   121
sl@0
   122
	/* 
sl@0
   123
	 * call the api to create a DH key pair
sl@0
   124
	 */
sl@0
   125
	CKeyPair* keyPair = 0;
sl@0
   126
	keyPairGeneratorImpl->GenerateKeyPairL(keySize, *keyParameters, keyPair);
sl@0
   127
	CleanupStack::PushL(keyPair);
sl@0
   128
sl@0
   129
	/* 
sl@0
   130
	 * for compatibility convert the CKeyPair to CDHPrivateKey and CDHPublicKey
sl@0
   131
	 */
sl@0
   132
sl@0
   133
	// create new RInteger copies of aN, aG and x so the private key can own them
sl@0
   134
	RInteger NCopy = RInteger::NewL(aN);
sl@0
   135
	CleanupClosePushL(NCopy);
sl@0
   136
	RInteger GCopy = RInteger::NewL(aG);
sl@0
   137
	CleanupClosePushL(GCopy);
sl@0
   138
	RInteger x = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KDhKeyParameterxUid));
sl@0
   139
	CleanupClosePushL(x);
sl@0
   140
	iPrivate = CDHPrivateKey::NewL(NCopy, GCopy, x);
sl@0
   141
	CleanupStack::Pop(3, &NCopy);
sl@0
   142
	
sl@0
   143
	// the public key becomes the owner of aN, aG and X
sl@0
   144
	RInteger X = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KDhKeyParameterXUid));
sl@0
   145
	CleanupClosePushL(X);
sl@0
   146
	iPublic = CDHPublicKey::NewL(aN, aG, X);
sl@0
   147
	CleanupStack::Pop(&X);	
sl@0
   148
sl@0
   149
	/* 
sl@0
   150
	 * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, X, NCopy, GCopy and x
sl@0
   151
	 */	
sl@0
   152
	CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);
sl@0
   153
	}