os/security/crypto/weakcryptospi/test/tplugins/src/tplugin01/rsakeypairgenextendimpl.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tplugins/src/tplugin01/rsakeypairgenextendimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,267 @@
1.4 +/*
1.5 +* Copyright (c) 2006-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 +* RSA Keypair (Extended Characteristics) implementation
1.19 +* RSA keypair generation implementation
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +/**
1.25 + @file
1.26 +*/
1.27 +
1.28 +#include "rsakeypairgenextendimpl.h"
1.29 +#include "pluginconfig.h"
1.30 +
1.31 +#include <ct.h>
1.32 +#include <cryptospi/keypair.h>
1.33 +#include <cryptospi/cryptospidef.h>
1.34 +
1.35 +#include "../../../source/common/inlines.h" // For TClassSwap
1.36 +
1.37 +//Extended Charcteristics
1.38 +
1.39 +static const TInt32 KExtendCharAttribute1 = 0x102ABCD1;
1.40 +static const TUid KExtendCharAttribute1Uid ={KExtendCharAttribute1};
1.41 +
1.42 +static const TInt32 KExtendCharAttribute2 = 0x102ABCD2;
1.43 +static const TUid KExtendCharAttribute2Uid ={KExtendCharAttribute2};
1.44 +
1.45 +static const TInt32 KExtendCharAttribute3 = 0x102ABCD3;
1.46 +static const TUid KExtendCharAttribute3Uid ={KExtendCharAttribute3};
1.47 +
1.48 +using namespace SoftwareCrypto;
1.49 +
1.50 +/* CRSAKeyPairGenExtendImpl */
1.51 +CRSAKeyPairGenExtendImpl::CRSAKeyPairGenExtendImpl(TUid aImplementationUid) : CKeyPairGenImpl(aImplementationUid)
1.52 + {
1.53 + }
1.54 +
1.55 +CRSAKeyPairGenExtendImpl::~CRSAKeyPairGenExtendImpl()
1.56 + {
1.57 + delete iExtendChars;
1.58 + }
1.59 +
1.60 +CRSAKeyPairGenExtendImpl* CRSAKeyPairGenExtendImpl::NewL(TUid aImplementationUid)
1.61 + {
1.62 + CRSAKeyPairGenExtendImpl* self = CRSAKeyPairGenExtendImpl::NewLC(aImplementationUid);
1.63 + CleanupStack::Pop(self);
1.64 + return self;
1.65 + }
1.66 +
1.67 +CRSAKeyPairGenExtendImpl* CRSAKeyPairGenExtendImpl::NewLC(TUid aImplementationUid)
1.68 + {
1.69 + CRSAKeyPairGenExtendImpl* self = new(ELeave) CRSAKeyPairGenExtendImpl(aImplementationUid);
1.70 + CleanupStack::PushL(self);
1.71 + self->ConstructL();
1.72 + return self;
1.73 + }
1.74 +
1.75 +void CRSAKeyPairGenExtendImpl::ConstructL(void)
1.76 + {
1.77 + CKeyPairGenImpl::ConstructL();
1.78 + iExtendChars = CreateExtendedCharacteristicsL();
1.79 + }
1.80 +
1.81 +CExtendedCharacteristics* CRSAKeyPairGenExtendImpl::CreateExtendedCharacteristicsL()
1.82 + {
1.83 + //***************************************************************
1.84 + CExtendedCharacteristics* exChars = CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
1.85 + CleanupStack::PushL(exChars);
1.86 +
1.87 + exChars->AddCharacteristicL(9999,KExtendCharAttribute1Uid);
1.88 + exChars->AddCharacteristicL(1010,KExtendCharAttribute2Uid);
1.89 + exChars->AddCharacteristicL(_L8("SYMBIANTESTCHARACTERISTIC"),KExtendCharAttribute3Uid);
1.90 + //**************************************************************
1.91 + CleanupStack::Pop(exChars);
1.92 +
1.93 + return exChars;
1.94 + }
1.95 +
1.96 +const CExtendedCharacteristics* CRSAKeyPairGenExtendImpl::GetExtendedCharacteristicsL()
1.97 + {
1.98 + return iExtendChars;
1.99 + }
1.100 +
1.101 +TUid CRSAKeyPairGenExtendImpl::ImplementationUid() const
1.102 + {
1.103 + return iImplementationUid;
1.104 + }
1.105 +
1.106 +void CRSAKeyPairGenExtendImpl::Reset()
1.107 + {
1.108 + // does nothing in this plugin
1.109 + }
1.110 +
1.111 +void CRSAKeyPairGenExtendImpl::GenerateKeyPairL(TInt aKeySize, const CCryptoParams& aKeyParameters, CKeyPair*& aKeyPair)
1.112 + {
1.113 + /*
1.114 + * extract e
1.115 + */
1.116 + const TInt aKeyType = aKeyParameters.GetTIntL(KRsaKeyTypeUid);
1.117 + const TInt aPublicExponent = aKeyParameters.GetTIntL(KRsaKeyParameterEUid);
1.118 +
1.119 + RInteger e = RInteger::NewL(aPublicExponent);
1.120 + CleanupStack::PushL(e);
1.121 +
1.122 + /*
1.123 + * calculate p, q, n & d
1.124 + */
1.125 + RInteger p;
1.126 + RInteger q;
1.127 +
1.128 + //these make sure n is a least aKeySize long
1.129 + TInt pbits=(aKeySize+1)/2;
1.130 + TInt qbits=aKeySize-pbits;
1.131 +
1.132 + //generate a prime p such that GCD(e,p-1) == 1
1.133 + for (;;)
1.134 + {
1.135 + p = RInteger::NewPrimeL(pbits,TInteger::ETop2BitsSet);
1.136 + CleanupStack::PushL(p);
1.137 + --p;
1.138 +
1.139 + RInteger gcd = e.GCDL(p);
1.140 + if( gcd == 1 )
1.141 + {
1.142 + ++p;
1.143 + gcd.Close();
1.144 + //p is still on cleanup stack
1.145 + break;
1.146 + }
1.147 + CleanupStack::PopAndDestroy(&p);
1.148 + gcd.Close();
1.149 + }
1.150 +
1.151 + //generate a prime q such that GCD(e,q-1) == 1 && (p != q)
1.152 + for (;;)
1.153 + {
1.154 + q = RInteger::NewPrimeL(qbits,TInteger::ETop2BitsSet);
1.155 + CleanupStack::PushL(q);
1.156 + --q;
1.157 +
1.158 + RInteger gcd = e.GCDL(q);
1.159 + if( gcd == 1 )
1.160 + {
1.161 + ++q;
1.162 + if( p != q )
1.163 + {
1.164 + gcd.Close();
1.165 + //q is still on cleanup stack
1.166 + break;
1.167 + }
1.168 + }
1.169 + CleanupStack::PopAndDestroy(&q);
1.170 + gcd.Close();
1.171 + }
1.172 +
1.173 + //make sure p > q
1.174 + if ( p < q)
1.175 + {
1.176 + TClassSwap(p,q);
1.177 + }
1.178 +
1.179 + //calculate n = p * q
1.180 + RInteger n = p.TimesL(q);
1.181 + CleanupStack::PushL(n);
1.182 +
1.183 + --p;
1.184 + --q;
1.185 +
1.186 + //temp = (p-1)(q-1)
1.187 + RInteger temp = p.TimesL(q);
1.188 + CleanupStack::PushL(temp);
1.189 +
1.190 + //e * d = 1 mod ((p-1)(q-1))
1.191 + //d = e^(-1) mod ((p-1)(q-1))
1.192 + RInteger d = e.InverseModL(temp);
1.193 + CleanupStack::PopAndDestroy(&temp); //temp
1.194 + CleanupStack::PushL(d);
1.195 +
1.196 + /*
1.197 + * create private key depending on aKeyType
1.198 + */
1.199 + CCryptoParams* privateKeyParameters = CCryptoParams::NewLC();
1.200 + privateKeyParameters->AddL(n, KRsaKeyParameterNUid);
1.201 + TKeyProperty* privateKeyProperties = NULL;
1.202 + TKeyProperty privateKeyProperties_RsaPrivateKeyCRT = {KRSAKeyPairGeneratorUid, iImplementationUid,
1.203 + KRsaPrivateKeyCRTUid, KNonEmbeddedKeyUid };
1.204 + TKeyProperty privateKeyProperties_RsaPrivateKeyStandard = {KRSAKeyPairGeneratorUid, iImplementationUid,
1.205 + KRsaPrivateKeyStandardUid, KNonEmbeddedKeyUid };
1.206 +
1.207 + CCryptoParams*publicKeyParameters = CCryptoParams::NewLC();
1.208 + publicKeyParameters->AddL(n, KRsaKeyParameterNUid);
1.209 + publicKeyParameters->AddL(e, KRsaKeyParameterEUid);
1.210 + TKeyProperty publicKeyProperties = {KRSAKeyPairGeneratorUid, iImplementationUid,
1.211 + KRsaPublicKeyUid, KNonEmbeddedKeyUid };
1.212 +
1.213 + if (aKeyType == KRsaPrivateKeyCRT) // cleanup stack contains e, p, q, n, d and privateKeyParameters
1.214 + {
1.215 +
1.216 + /*
1.217 + * calculate dP, dQ and qInv
1.218 + */
1.219 + //calculate dP = d mod (p-1)
1.220 + RInteger dP = d.ModuloL(p); //p is still p-1
1.221 + CleanupStack::PushL(dP);
1.222 + privateKeyParameters->AddL(dP, KRsaKeyParameterDPUid);
1.223 + CleanupStack::PopAndDestroy(&dP);
1.224 +
1.225 + //calculate dQ = d mod (q-1)
1.226 + RInteger dQ = d.ModuloL(q); //q is still q-1
1.227 + CleanupStack::PushL(dQ);
1.228 + privateKeyParameters->AddL(dQ, KRsaKeyParameterDQUid);
1.229 + CleanupStack::PopAndDestroy(&dQ);
1.230 +
1.231 + ++p;
1.232 + ++q;
1.233 + //calculate inverse of qInv = q^(-1)mod(p)
1.234 + RInteger qInv = q.InverseModL(p);
1.235 + CleanupStack::PushL(qInv);
1.236 + privateKeyParameters->AddL(qInv, KRsaKeyParameterQInvUid);
1.237 + CleanupStack::PopAndDestroy(&qInv);
1.238 +
1.239 + privateKeyParameters->AddL(p, KRsaKeyParameterPUid);
1.240 + privateKeyParameters->AddL(q, KRsaKeyParameterQUid);
1.241 +
1.242 + privateKeyProperties = &privateKeyProperties_RsaPrivateKeyCRT;
1.243 + }
1.244 + else if (aKeyType == KRsaPrivateKeyStandard)
1.245 + {
1.246 + privateKeyParameters->AddL(d, KRsaKeyParameterDUid);
1.247 + privateKeyProperties = &privateKeyProperties_RsaPrivateKeyStandard;
1.248 + }
1.249 + else
1.250 + {
1.251 + User::Leave(KErrNotSupported);
1.252 + }
1.253 + // cleanup stack contains e, p, q, n, d and privateKeyParameters
1.254 + CKey* privateKey = CKey::NewL(*privateKeyProperties, *privateKeyParameters);
1.255 + CleanupStack::PushL(privateKey);
1.256 +
1.257 + /*
1.258 + * create public key
1.259 + */
1.260 + CKey* publicKey = CKey::NewL(publicKeyProperties, *publicKeyParameters);
1.261 + CleanupStack::PushL(publicKey);
1.262 +
1.263 + /*
1.264 + * create the key pair
1.265 + */
1.266 + aKeyPair = CKeyPair::NewL(publicKey, privateKey);
1.267 +
1.268 + CleanupStack::Pop(2, privateKey); //privateKey and publicKey
1.269 + CleanupStack::PopAndDestroy(7, &e); //e, p, q, n, d, privateKeyParameters and publicKeyParameters
1.270 + }