os/security/crypto/weakcryptospi/source/asymmetric/dsakeypairshim.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 "dsakeypairshim.h"
    20 #include <cryptospi/cryptokeypairgeneratorapi.h>
    21 #include <cryptospi/keypair.h>
    22 #include <cryptospi/cryptoparams.h>
    23 #include <cryptospi/cryptospidef.h>
    24 
    25 
    26 using namespace CryptoSpi;
    27 
    28 
    29 /* CDSAKeyPair */
    30 CDSAKeyPairShim* CDSAKeyPairShim::NewLC(TUint aKeyBits)
    31 	{
    32 	CDSAKeyPairShim* self = new(ELeave) CDSAKeyPairShim();
    33 	CleanupStack::PushL(self);
    34 	self->ConstructL(aKeyBits);
    35 	return self;
    36 	}
    37 
    38 CDSAKeyPairShim::~CDSAKeyPairShim()
    39 	{
    40 	}
    41 
    42 CDSAKeyPairShim::CDSAKeyPairShim()
    43 	{
    44 	}
    45 
    46 void CDSAKeyPairShim::ConstructL(TUint aKeyBits)
    47 	{
    48 	CKeyPairGenerator* keyPairGeneratorImpl=NULL;
    49 	CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
    50 											keyPairGeneratorImpl,
    51 											KDSAKeyPairGeneratorUid,
    52 											NULL);
    53 	CleanupStack::PushL(keyPairGeneratorImpl);
    54 	
    55 	
    56 	
    57 	
    58 	// put the DSA parameters into an array
    59 	CCryptoParams* keyParameters = CCryptoParams::NewLC();
    60 
    61 	// call the api to create an DSA key pair
    62 	CKeyPair* keyPair = 0;
    63 	keyPairGeneratorImpl->GenerateKeyPairL(aKeyBits, *keyParameters, keyPair);
    64 	CleanupStack::PushL(keyPair);
    65 	
    66 	//Just to keep BC
    67 	if (keyParameters->IsPresent(KDsaKeyGenerationSeedUid))
    68 		{
    69 		const TDesC8& seed = keyParameters->GetTDesC8L(KDsaKeyGenerationSeedUid);
    70 		TInt counter=keyParameters->GetTIntL(KDsaKeyGenerationCounterUid);
    71 		iPrimeCertificate=CDSAPrimeCertificate::NewL(seed, counter);	
    72 		}
    73 	
    74 	//
    75 	//for compatibility convert the CKeyPair to CDSAPrivateKey and CDSAPublicKey
    76 	//
    77 	// the public key becomes the owner of P,Q,G,Y
    78 	const CKey& publicKey = keyPair->PublicKey();
    79 	RInteger P = RInteger::NewL(publicKey.GetBigIntL(KDsaKeyParameterPUid));
    80 	CleanupClosePushL(P);
    81 
    82 	RInteger Q = RInteger::NewL(publicKey.GetBigIntL(KDsaKeyParameterQUid));
    83 	CleanupClosePushL(Q);
    84 	
    85 	RInteger G = RInteger::NewL(publicKey.GetBigIntL(KDsaKeyParameterGUid));
    86 	CleanupClosePushL(G);
    87 	
    88 	RInteger Y = RInteger::NewL(publicKey.GetBigIntL(KDsaKeyParameterYUid));
    89 	CleanupClosePushL(Y);
    90 	
    91 	iPublic = CDSAPublicKey::NewL(P, Q, G, Y);
    92 	CleanupStack::Pop(4, &P); //Y,G,Q,P
    93 
    94 	// the private key becomes the owner of P1,Q1,G1,X
    95 	const CKey& privateKey = keyPair->PrivateKey();
    96 	RInteger P1 = RInteger::NewL(privateKey.GetBigIntL(KDsaKeyParameterPUid));
    97 	CleanupClosePushL(P1);
    98 
    99 	RInteger Q1 = RInteger::NewL(privateKey.GetBigIntL(KDsaKeyParameterQUid));
   100 	CleanupClosePushL(Q1);
   101 	
   102 	RInteger G1 = RInteger::NewL(privateKey.GetBigIntL(KDsaKeyParameterGUid));
   103 	CleanupClosePushL(G1);
   104 
   105 	RInteger X = RInteger::NewL(privateKey.GetBigIntL(KDsaKeyParameterXUid));
   106 	CleanupClosePushL(X);
   107 
   108 	iPrivate = CDSAPrivateKey::NewL(P1, Q1, G1, X);
   109 	CleanupStack::Pop(4, &P1); //X,G1,Q1,P1
   110 	
   111 	CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl); // keyPair, keyParameters, keyPairGeneratorImpl
   112 	}