1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/asymmetric/dhkeypairshim.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,153 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "dhkeypairshim.h"
1.23 +#include <bigint.h>
1.24 +#include <cryptospi/cryptokeypairgeneratorapi.h>
1.25 +#include <cryptospi/keypair.h>
1.26 +#include <cryptospi/cryptoparams.h>
1.27 +#include <cryptospi/cryptospidef.h>
1.28 +
1.29 +
1.30 +using namespace CryptoSpi;
1.31 +
1.32 +
1.33 +/* CDHKeyPair */
1.34 +CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG)
1.35 + {
1.36 + CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
1.37 + CleanupStack::PushL(self);
1.38 + self->ConstructL(aN, aG);
1.39 + return self;
1.40 + }
1.41 +
1.42 +CDHKeyPairShim* CDHKeyPairShim::NewLC(RInteger& aN, RInteger& aG, RInteger& ax)
1.43 + {
1.44 + CDHKeyPairShim* self = new(ELeave) CDHKeyPairShim();
1.45 + CleanupStack::PushL(self);
1.46 + self->ConstructL(aN, aG, ax);
1.47 + return self;
1.48 + }
1.49 +
1.50 +CDHKeyPairShim::~CDHKeyPairShim(void)
1.51 + {
1.52 + }
1.53 +
1.54 +CDHKeyPairShim::CDHKeyPairShim(void)
1.55 + {
1.56 + }
1.57 +
1.58 +void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG)
1.59 + {
1.60 + RInteger x = RInteger::NewL();
1.61 + CleanupClosePushL(x);
1.62 + KeyConstructorL(aN, aG, x, EFalse);
1.63 + CleanupStack::PopAndDestroy(1, &x);
1.64 + }
1.65 +
1.66 +void CDHKeyPairShim::ConstructL(RInteger& aN, RInteger& aG, RInteger& ax)
1.67 + {
1.68 + KeyConstructorL(aN, aG, ax, ETrue);
1.69 + }
1.70 +
1.71 +void CDHKeyPairShim::KeyConstructorL(RInteger& aN, RInteger& aG, RInteger& ax, TBool xIncluded)
1.72 + {
1.73 + RInteger& nminus2 = aN;
1.74 +
1.75 + /*
1.76 + * do some sanity checks
1.77 + */
1.78 + --nminus2;
1.79 + --nminus2;
1.80 + if( aG < TInteger::Two() || aG > nminus2 )
1.81 + {
1.82 + User::Leave(KErrArgument);
1.83 + }
1.84 +
1.85 + if (xIncluded)
1.86 + {
1.87 + if( ax < TInteger::One() || ax > nminus2 )
1.88 + {
1.89 + User::Leave(KErrArgument);
1.90 + }
1.91 + }
1.92 +
1.93 + /*
1.94 + *find out how big the key should be - the key must be in the range x | 1 <= x <= n-2
1.95 + * nminus2 is the largest the key can be so get the number of bits required to represent that number
1.96 + */
1.97 + const TInt keySize = nminus2.BitCount();
1.98 +
1.99 + // increment aN back to its original value
1.100 + ++nminus2;
1.101 + ++nminus2;
1.102 +
1.103 + // obtain an RSA key pair generator interface
1.104 +
1.105 + CKeyPairGenerator* keyPairGeneratorImpl=NULL;
1.106 + CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
1.107 + keyPairGeneratorImpl,
1.108 + KDHKeyPairGeneratorUid,
1.109 + NULL);
1.110 + CleanupStack::PushL(keyPairGeneratorImpl);
1.111 +
1.112 + /*
1.113 + * put the DH parameters into an array
1.114 + */
1.115 + CCryptoParams* keyParameters = CCryptoParams::NewLC();
1.116 + keyParameters->AddL(aN, KDhKeyParameterNUid);
1.117 + keyParameters->AddL(aG, KDhKeyParameterGUid);
1.118 + if (xIncluded)
1.119 + {
1.120 + // the private key x has been supplied so add it to the params array so the key generator algo can use it
1.121 + keyParameters->AddL(ax, KDhKeyParameterxUid);
1.122 + ax.Close();
1.123 + }
1.124 +
1.125 + /*
1.126 + * call the api to create a DH key pair
1.127 + */
1.128 + CKeyPair* keyPair = 0;
1.129 + keyPairGeneratorImpl->GenerateKeyPairL(keySize, *keyParameters, keyPair);
1.130 + CleanupStack::PushL(keyPair);
1.131 +
1.132 + /*
1.133 + * for compatibility convert the CKeyPair to CDHPrivateKey and CDHPublicKey
1.134 + */
1.135 +
1.136 + // create new RInteger copies of aN, aG and x so the private key can own them
1.137 + RInteger NCopy = RInteger::NewL(aN);
1.138 + CleanupClosePushL(NCopy);
1.139 + RInteger GCopy = RInteger::NewL(aG);
1.140 + CleanupClosePushL(GCopy);
1.141 + RInteger x = RInteger::NewL(keyPair->PrivateKey().GetBigIntL(KDhKeyParameterxUid));
1.142 + CleanupClosePushL(x);
1.143 + iPrivate = CDHPrivateKey::NewL(NCopy, GCopy, x);
1.144 + CleanupStack::Pop(3, &NCopy);
1.145 +
1.146 + // the public key becomes the owner of aN, aG and X
1.147 + RInteger X = RInteger::NewL(keyPair->PublicKey().GetBigIntL(KDhKeyParameterXUid));
1.148 + CleanupClosePushL(X);
1.149 + iPublic = CDHPublicKey::NewL(aN, aG, X);
1.150 + CleanupStack::Pop(&X);
1.151 +
1.152 + /*
1.153 + * cleanup stack - it should contain keyPairGeneratorImpl, keyParameters, keyPair, X, NCopy, GCopy and x
1.154 + */
1.155 + CleanupStack::PopAndDestroy(3, keyPairGeneratorImpl);
1.156 + }