os/security/crypto/weakcryptospi/source/asymmetric/rsashim.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/source/asymmetric/rsashim.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,365 @@
     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 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "rsashim.h"
    1.23 +#include <cryptospi/cryptoasymmetriccipherapi.h>
    1.24 +#include <cryptospi/cryptosignatureapi.h>
    1.25 +#include <cryptospi/cryptospidef.h>
    1.26 +#include "keyconverter.h"
    1.27 +#include <cryptospi/keys.h>
    1.28 +
    1.29 +#include "../common/inlines.h"
    1.30 +
    1.31 +
    1.32 +using namespace CryptoSpi;
    1.33 +
    1.34 +// CRSAPKCS1v15EncryptorShim ////////////////////////////////////////////////////////
    1.35 +
    1.36 +CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewL(const CRSAPublicKey& aKey)
    1.37 +	{
    1.38 +	CRSAPKCS1v15EncryptorShim* self = CRSAPKCS1v15EncryptorShim::NewLC(aKey);
    1.39 +	CleanupStack::Pop(self);
    1.40 +	return self;
    1.41 +	}
    1.42 +
    1.43 +CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewLC(const CRSAPublicKey& aKey)
    1.44 +	{
    1.45 +	CRSAPKCS1v15EncryptorShim* self = new (ELeave) CRSAPKCS1v15EncryptorShim(aKey);
    1.46 +	CleanupStack::PushL(self);
    1.47 +	self->ConstructL(aKey);
    1.48 +	return self;
    1.49 +	}
    1.50 +
    1.51 +CRSAPKCS1v15EncryptorShim::CRSAPKCS1v15EncryptorShim(const CRSAPublicKey& aKey)
    1.52 +	: CRSAPKCS1v15Encryptor(aKey)
    1.53 +	{
    1.54 +	}
    1.55 +
    1.56 +CRSAPKCS1v15EncryptorShim::~CRSAPKCS1v15EncryptorShim()
    1.57 +	{
    1.58 +	delete iAsymmetricCipherImpl;
    1.59 +	delete iKey;
    1.60 +	}
    1.61 +
    1.62 +void CRSAPKCS1v15EncryptorShim::ConstructL(const CRSAPublicKey& aKey)
    1.63 +	{
    1.64 +	iKey = KeyConverter::CreateKeyL(aKey);
    1.65 +	CAsymmetricCipherFactory::CreateAsymmetricCipherL(
    1.66 +												iAsymmetricCipherImpl,
    1.67 +												KRsaCipherUid,
    1.68 +												*iKey,
    1.69 +												KCryptoModeEncryptUid,
    1.70 +												KPaddingModePkcs1_v1_5_EncryptionUid,
    1.71 +												NULL);
    1.72 +	}
    1.73 +
    1.74 +void CRSAPKCS1v15EncryptorShim::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
    1.75 +	{
    1.76 +	iAsymmetricCipherImpl->ProcessL(aInput, aOutput);
    1.77 +	}
    1.78 +
    1.79 +TInt CRSAPKCS1v15EncryptorShim::MaxInputLength(void) const
    1.80 +	{
    1.81 +	TInt maxInputLength=0; 
    1.82 +	TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL())
    1.83 +	if (err==KErrNone)
    1.84 +		{
    1.85 +		return maxInputLength;
    1.86 +		}
    1.87 +	else
    1.88 +		{
    1.89 +		return err;
    1.90 +		}
    1.91 +	}
    1.92 +
    1.93 +TInt CRSAPKCS1v15EncryptorShim::MaxOutputLength(void) const
    1.94 +	{
    1.95 +	TInt maxOutputLength=0; 
    1.96 +	TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL())
    1.97 +	if (err==KErrNone)
    1.98 +		{
    1.99 +		return maxOutputLength;
   1.100 +		}
   1.101 +	else
   1.102 +		{
   1.103 +		return err;
   1.104 +		}
   1.105 +	}
   1.106 +
   1.107 +// CRSAPKCS1v15DecryptorShim ////////////////////////////////////////////////////////
   1.108 +CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewL(const CRSAPrivateKey& aKey)
   1.109 +	{
   1.110 +	CRSAPKCS1v15DecryptorShim* self = CRSAPKCS1v15DecryptorShim::NewLC(aKey);
   1.111 +	CleanupStack::Pop(self);
   1.112 +	return self;
   1.113 +	}
   1.114 +
   1.115 +
   1.116 +CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewLC(const CRSAPrivateKey& aKey)
   1.117 +	{
   1.118 +	CRSAPKCS1v15DecryptorShim* self = new (ELeave) CRSAPKCS1v15DecryptorShim(aKey);
   1.119 +	CleanupStack::PushL(self);
   1.120 +	self->ConstructL(aKey);
   1.121 +	return self;
   1.122 +	}
   1.123 +
   1.124 +CRSAPKCS1v15DecryptorShim::CRSAPKCS1v15DecryptorShim(const CRSAPrivateKey& aKey)
   1.125 +	: CRSAPKCS1v15Decryptor(aKey)
   1.126 +	{
   1.127 +	}
   1.128 +
   1.129 +CRSAPKCS1v15DecryptorShim::~CRSAPKCS1v15DecryptorShim()
   1.130 +	{
   1.131 +	delete iAsymmetricCipherImpl;
   1.132 +	delete iKey;
   1.133 +	}
   1.134 +
   1.135 +void CRSAPKCS1v15DecryptorShim::ConstructL(const CRSAPrivateKey& aKey)
   1.136 +	{
   1.137 +	iKey = KeyConverter::CreateKeyL(aKey);
   1.138 +	CAsymmetricCipherFactory::CreateAsymmetricCipherL(
   1.139 +												iAsymmetricCipherImpl,
   1.140 +												KRsaCipherUid,
   1.141 +												*iKey,
   1.142 +												KCryptoModeDecryptUid,
   1.143 +												KPaddingModePkcs1_v1_5_EncryptionUid,
   1.144 +												NULL);
   1.145 +	}
   1.146 +
   1.147 +void CRSAPKCS1v15DecryptorShim::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
   1.148 +	{
   1.149 +	iAsymmetricCipherImpl->ProcessL(aInput, aOutput);
   1.150 +	}
   1.151 +
   1.152 +TInt CRSAPKCS1v15DecryptorShim::MaxInputLength(void) const
   1.153 +	{
   1.154 +	TInt maxInputLength=0; 
   1.155 +	TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL())
   1.156 +	if (err==KErrNone)
   1.157 +		{
   1.158 +		return maxInputLength;
   1.159 +		}
   1.160 +	else
   1.161 +		{
   1.162 +		return err;
   1.163 +		}
   1.164 +	}
   1.165 +
   1.166 +TInt CRSAPKCS1v15DecryptorShim::MaxOutputLength(void) const
   1.167 +	{
   1.168 +	TInt maxOutputLength=0; 
   1.169 +	TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL())
   1.170 +	if (err==KErrNone)
   1.171 +		{
   1.172 +		return maxOutputLength;
   1.173 +		}
   1.174 +	else
   1.175 +		{
   1.176 +		return err;
   1.177 +		}
   1.178 +	}
   1.179 +
   1.180 +// CRSAPKCS1v15SignerShim ////////////////////////////////////////////////////////
   1.181 +CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewL(const CRSAPrivateKey& aKey)
   1.182 +	{
   1.183 +	CRSAPKCS1v15SignerShim* self = CRSAPKCS1v15SignerShim::NewLC(aKey);
   1.184 +	CleanupStack::Pop(self);
   1.185 +	return self;
   1.186 +	}
   1.187 +
   1.188 +CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewLC(const CRSAPrivateKey& aKey)
   1.189 +	{
   1.190 +	CRSAPKCS1v15SignerShim* self = new (ELeave) CRSAPKCS1v15SignerShim(aKey);
   1.191 +	CleanupStack::PushL(self);
   1.192 +	self->ConstructL(aKey);
   1.193 +	return self;
   1.194 +	}
   1.195 +
   1.196 +CRSASignature* CRSAPKCS1v15SignerShim::SignL(const TDesC8& aInput) const
   1.197 +	{
   1.198 +	//Sign the input data
   1.199 +	CCryptoParams* signature = CCryptoParams::NewLC();
   1.200 +	iSignerImpl->SignL(aInput, *signature);
   1.201 +
   1.202 +	//Retrieve the S in RSA signature from the array
   1.203 +	const TInteger& cS=signature->GetBigIntL(KRsaSignatureParameterSUid);
   1.204 +
   1.205 +	//Make copies of the RSA signature
   1.206 +	RInteger s=RInteger::NewL(cS);
   1.207 +	CleanupClosePushL(s);	
   1.208 +
   1.209 +	//Create the RSA signature object, the ownership of s is transfered to rsaSig
   1.210 +	CRSASignature* rsaSig=CRSASignature::NewL(s);
   1.211 +
   1.212 +	//Cleanup
   1.213 +	CleanupStack::Pop(&s);
   1.214 +	CleanupStack::PopAndDestroy(signature);
   1.215 +	return rsaSig;
   1.216 +	}
   1.217 +
   1.218 +TInt CRSAPKCS1v15SignerShim::MaxInputLength(void) const
   1.219 +	{
   1.220 +	TInt maxInputLength=0; 
   1.221 +	TRAPD(err, maxInputLength=iSignerImpl->GetMaximumInputLengthL())
   1.222 +	if (err==KErrNone)
   1.223 +		{
   1.224 +		return maxInputLength;
   1.225 +		}
   1.226 +	else
   1.227 +		{
   1.228 +		return err;
   1.229 +		}
   1.230 +	}
   1.231 +	
   1.232 +TInt CRSAPKCS1v15SignerShim::MaxOutputLength(void) const
   1.233 +	{
   1.234 +	TInt maxOutputLength=0; 
   1.235 +	TRAPD(err, maxOutputLength=iSignerImpl->GetMaximumOutputLengthL())
   1.236 +	if (err==KErrNone)
   1.237 +		{
   1.238 +		return maxOutputLength;
   1.239 +		}
   1.240 +	else
   1.241 +		{
   1.242 +		return err;
   1.243 +		}
   1.244 +	}
   1.245 +
   1.246 +CRSAPKCS1v15SignerShim::~CRSAPKCS1v15SignerShim(void)
   1.247 +	{
   1.248 +	delete iSignerImpl;
   1.249 +	delete iKey;
   1.250 +	}
   1.251 +
   1.252 +CRSAPKCS1v15SignerShim::CRSAPKCS1v15SignerShim(const CRSAPrivateKey& aKey)
   1.253 +	: CRSAPKCS1v15Signer(aKey)
   1.254 +	{
   1.255 +	}
   1.256 +	
   1.257 +void CRSAPKCS1v15SignerShim::ConstructL(const CRSAPrivateKey& aKey)
   1.258 +	{
   1.259 +	iKey = KeyConverter::CreateKeyL(aKey);
   1.260 +	CSignatureFactory::CreateSignerL(
   1.261 +									iSignerImpl,
   1.262 +									KRsaSignerUid,
   1.263 +									*iKey,
   1.264 +									KPaddingModePkcs1_v1_5_SignatureUid,
   1.265 +									NULL);
   1.266 +	}
   1.267 +
   1.268 +// CRSAPKCS1v15VerifierShim ////////////////////////////////////////////////////////
   1.269 +CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewL(const CRSAPublicKey& aKey)
   1.270 +	{
   1.271 +	CRSAPKCS1v15VerifierShim* self = CRSAPKCS1v15VerifierShim::NewLC(aKey);
   1.272 +	CleanupStack::Pop(self);
   1.273 +	return self;
   1.274 +	}
   1.275 +
   1.276 +CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewLC(const CRSAPublicKey& aKey)
   1.277 +	{
   1.278 +	CRSAPKCS1v15VerifierShim* self = new (ELeave) CRSAPKCS1v15VerifierShim(aKey);
   1.279 +	CleanupStack::PushL(self);
   1.280 +	self->ConstructL(aKey);
   1.281 +	return self;
   1.282 +	}
   1.283 +	
   1.284 +TBool CRSAPKCS1v15VerifierShim::VerifyL(const TDesC8& aInput, const CRSASignature& aSignature) const
   1.285 +	{
   1.286 +	//create the array format rsa signature for the new crypto spi
   1.287 +	CCryptoParams* rsaSig = CCryptoParams::NewLC();
   1.288 +
   1.289 +	rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid);
   1.290 +
   1.291 +	//pass the signature and input to crypto spi to be verified
   1.292 +	TBool verificationResult = EFalse;	
   1.293 +	iVerifierImpl->VerifyL(aInput, *rsaSig, verificationResult);
   1.294 +
   1.295 +	//Cleanup the array
   1.296 +	CleanupStack::PopAndDestroy(rsaSig);
   1.297 +	return verificationResult;
   1.298 +	}
   1.299 +
   1.300 +HBufC8* CRSAPKCS1v15VerifierShim::InverseSignLC(const CRSASignature& aSignature) const
   1.301 +	{
   1.302 +	//create the array format rsa signature for the new crypto spi
   1.303 +	CCryptoParams* rsaSig = CCryptoParams::NewLC();
   1.304 +
   1.305 +	rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid);
   1.306 +
   1.307 +	//pass the signature and input to crypto spi to be verified
   1.308 +	HBufC8* output = NULL;
   1.309 +	iVerifierImpl->InverseSignL(output, *rsaSig);
   1.310 +
   1.311 +	//Cleanup the array
   1.312 +	CleanupStack::PopAndDestroy(rsaSig);
   1.313 +
   1.314 +	// leave output on the cleanup stack
   1.315 +	CleanupStack::PushL(output);
   1.316 +	return output;
   1.317 +	}
   1.318 +
   1.319 +TInt CRSAPKCS1v15VerifierShim::MaxInputLength(void) const
   1.320 +	{
   1.321 +	TInt maxInputLength=0; 
   1.322 +	TRAPD(err, maxInputLength=iVerifierImpl->GetMaximumInputLengthL())
   1.323 +	if (err==KErrNone)
   1.324 +		{
   1.325 +		return maxInputLength;
   1.326 +		}
   1.327 +	else
   1.328 +		{
   1.329 +		return err;
   1.330 +		}
   1.331 +	}
   1.332 +	
   1.333 +TInt CRSAPKCS1v15VerifierShim::MaxOutputLength(void) const
   1.334 +	{
   1.335 +	TInt maxOutputLength=0; 
   1.336 +	TRAPD(err, maxOutputLength=iVerifierImpl->GetMaximumOutputLengthL())
   1.337 +	if (err==KErrNone)
   1.338 +		{
   1.339 +		return maxOutputLength;
   1.340 +		}
   1.341 +	else
   1.342 +		{
   1.343 +		return err;
   1.344 +		}
   1.345 +	}
   1.346 +
   1.347 +CRSAPKCS1v15VerifierShim::~CRSAPKCS1v15VerifierShim(void)
   1.348 +	{
   1.349 +	delete iVerifierImpl;
   1.350 +	delete iKey;
   1.351 +	}
   1.352 +
   1.353 +CRSAPKCS1v15VerifierShim::CRSAPKCS1v15VerifierShim(const CRSAPublicKey& aKey)
   1.354 +	: CRSAPKCS1v15Verifier(aKey)
   1.355 +	{
   1.356 +	}
   1.357 +
   1.358 +void CRSAPKCS1v15VerifierShim::ConstructL(const CRSAPublicKey& aKey)
   1.359 +	{
   1.360 +	iKey = KeyConverter::CreateKeyL(aKey);
   1.361 +	CSignatureFactory::CreateVerifierL(
   1.362 +									iVerifierImpl,
   1.363 +									KRsaVerifierUid,
   1.364 +									*iKey,
   1.365 +									KPaddingModePkcs1_v1_5_SignatureUid,
   1.366 +									NULL);
   1.367 +	}
   1.368 +