os/security/crypto/weakcryptospi/source/asymmetric/rsashim.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2006-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 "rsashim.h"
    20 #include <cryptospi/cryptoasymmetriccipherapi.h>
    21 #include <cryptospi/cryptosignatureapi.h>
    22 #include <cryptospi/cryptospidef.h>
    23 #include "keyconverter.h"
    24 #include <cryptospi/keys.h>
    25 
    26 #include "../common/inlines.h"
    27 
    28 
    29 using namespace CryptoSpi;
    30 
    31 // CRSAPKCS1v15EncryptorShim ////////////////////////////////////////////////////////
    32 
    33 CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewL(const CRSAPublicKey& aKey)
    34 	{
    35 	CRSAPKCS1v15EncryptorShim* self = CRSAPKCS1v15EncryptorShim::NewLC(aKey);
    36 	CleanupStack::Pop(self);
    37 	return self;
    38 	}
    39 
    40 CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewLC(const CRSAPublicKey& aKey)
    41 	{
    42 	CRSAPKCS1v15EncryptorShim* self = new (ELeave) CRSAPKCS1v15EncryptorShim(aKey);
    43 	CleanupStack::PushL(self);
    44 	self->ConstructL(aKey);
    45 	return self;
    46 	}
    47 
    48 CRSAPKCS1v15EncryptorShim::CRSAPKCS1v15EncryptorShim(const CRSAPublicKey& aKey)
    49 	: CRSAPKCS1v15Encryptor(aKey)
    50 	{
    51 	}
    52 
    53 CRSAPKCS1v15EncryptorShim::~CRSAPKCS1v15EncryptorShim()
    54 	{
    55 	delete iAsymmetricCipherImpl;
    56 	delete iKey;
    57 	}
    58 
    59 void CRSAPKCS1v15EncryptorShim::ConstructL(const CRSAPublicKey& aKey)
    60 	{
    61 	iKey = KeyConverter::CreateKeyL(aKey);
    62 	CAsymmetricCipherFactory::CreateAsymmetricCipherL(
    63 												iAsymmetricCipherImpl,
    64 												KRsaCipherUid,
    65 												*iKey,
    66 												KCryptoModeEncryptUid,
    67 												KPaddingModePkcs1_v1_5_EncryptionUid,
    68 												NULL);
    69 	}
    70 
    71 void CRSAPKCS1v15EncryptorShim::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
    72 	{
    73 	iAsymmetricCipherImpl->ProcessL(aInput, aOutput);
    74 	}
    75 
    76 TInt CRSAPKCS1v15EncryptorShim::MaxInputLength(void) const
    77 	{
    78 	TInt maxInputLength=0; 
    79 	TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL())
    80 	if (err==KErrNone)
    81 		{
    82 		return maxInputLength;
    83 		}
    84 	else
    85 		{
    86 		return err;
    87 		}
    88 	}
    89 
    90 TInt CRSAPKCS1v15EncryptorShim::MaxOutputLength(void) const
    91 	{
    92 	TInt maxOutputLength=0; 
    93 	TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL())
    94 	if (err==KErrNone)
    95 		{
    96 		return maxOutputLength;
    97 		}
    98 	else
    99 		{
   100 		return err;
   101 		}
   102 	}
   103 
   104 // CRSAPKCS1v15DecryptorShim ////////////////////////////////////////////////////////
   105 CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewL(const CRSAPrivateKey& aKey)
   106 	{
   107 	CRSAPKCS1v15DecryptorShim* self = CRSAPKCS1v15DecryptorShim::NewLC(aKey);
   108 	CleanupStack::Pop(self);
   109 	return self;
   110 	}
   111 
   112 
   113 CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewLC(const CRSAPrivateKey& aKey)
   114 	{
   115 	CRSAPKCS1v15DecryptorShim* self = new (ELeave) CRSAPKCS1v15DecryptorShim(aKey);
   116 	CleanupStack::PushL(self);
   117 	self->ConstructL(aKey);
   118 	return self;
   119 	}
   120 
   121 CRSAPKCS1v15DecryptorShim::CRSAPKCS1v15DecryptorShim(const CRSAPrivateKey& aKey)
   122 	: CRSAPKCS1v15Decryptor(aKey)
   123 	{
   124 	}
   125 
   126 CRSAPKCS1v15DecryptorShim::~CRSAPKCS1v15DecryptorShim()
   127 	{
   128 	delete iAsymmetricCipherImpl;
   129 	delete iKey;
   130 	}
   131 
   132 void CRSAPKCS1v15DecryptorShim::ConstructL(const CRSAPrivateKey& aKey)
   133 	{
   134 	iKey = KeyConverter::CreateKeyL(aKey);
   135 	CAsymmetricCipherFactory::CreateAsymmetricCipherL(
   136 												iAsymmetricCipherImpl,
   137 												KRsaCipherUid,
   138 												*iKey,
   139 												KCryptoModeDecryptUid,
   140 												KPaddingModePkcs1_v1_5_EncryptionUid,
   141 												NULL);
   142 	}
   143 
   144 void CRSAPKCS1v15DecryptorShim::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
   145 	{
   146 	iAsymmetricCipherImpl->ProcessL(aInput, aOutput);
   147 	}
   148 
   149 TInt CRSAPKCS1v15DecryptorShim::MaxInputLength(void) const
   150 	{
   151 	TInt maxInputLength=0; 
   152 	TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL())
   153 	if (err==KErrNone)
   154 		{
   155 		return maxInputLength;
   156 		}
   157 	else
   158 		{
   159 		return err;
   160 		}
   161 	}
   162 
   163 TInt CRSAPKCS1v15DecryptorShim::MaxOutputLength(void) const
   164 	{
   165 	TInt maxOutputLength=0; 
   166 	TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL())
   167 	if (err==KErrNone)
   168 		{
   169 		return maxOutputLength;
   170 		}
   171 	else
   172 		{
   173 		return err;
   174 		}
   175 	}
   176 
   177 // CRSAPKCS1v15SignerShim ////////////////////////////////////////////////////////
   178 CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewL(const CRSAPrivateKey& aKey)
   179 	{
   180 	CRSAPKCS1v15SignerShim* self = CRSAPKCS1v15SignerShim::NewLC(aKey);
   181 	CleanupStack::Pop(self);
   182 	return self;
   183 	}
   184 
   185 CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewLC(const CRSAPrivateKey& aKey)
   186 	{
   187 	CRSAPKCS1v15SignerShim* self = new (ELeave) CRSAPKCS1v15SignerShim(aKey);
   188 	CleanupStack::PushL(self);
   189 	self->ConstructL(aKey);
   190 	return self;
   191 	}
   192 
   193 CRSASignature* CRSAPKCS1v15SignerShim::SignL(const TDesC8& aInput) const
   194 	{
   195 	//Sign the input data
   196 	CCryptoParams* signature = CCryptoParams::NewLC();
   197 	iSignerImpl->SignL(aInput, *signature);
   198 
   199 	//Retrieve the S in RSA signature from the array
   200 	const TInteger& cS=signature->GetBigIntL(KRsaSignatureParameterSUid);
   201 
   202 	//Make copies of the RSA signature
   203 	RInteger s=RInteger::NewL(cS);
   204 	CleanupClosePushL(s);	
   205 
   206 	//Create the RSA signature object, the ownership of s is transfered to rsaSig
   207 	CRSASignature* rsaSig=CRSASignature::NewL(s);
   208 
   209 	//Cleanup
   210 	CleanupStack::Pop(&s);
   211 	CleanupStack::PopAndDestroy(signature);
   212 	return rsaSig;
   213 	}
   214 
   215 TInt CRSAPKCS1v15SignerShim::MaxInputLength(void) const
   216 	{
   217 	TInt maxInputLength=0; 
   218 	TRAPD(err, maxInputLength=iSignerImpl->GetMaximumInputLengthL())
   219 	if (err==KErrNone)
   220 		{
   221 		return maxInputLength;
   222 		}
   223 	else
   224 		{
   225 		return err;
   226 		}
   227 	}
   228 	
   229 TInt CRSAPKCS1v15SignerShim::MaxOutputLength(void) const
   230 	{
   231 	TInt maxOutputLength=0; 
   232 	TRAPD(err, maxOutputLength=iSignerImpl->GetMaximumOutputLengthL())
   233 	if (err==KErrNone)
   234 		{
   235 		return maxOutputLength;
   236 		}
   237 	else
   238 		{
   239 		return err;
   240 		}
   241 	}
   242 
   243 CRSAPKCS1v15SignerShim::~CRSAPKCS1v15SignerShim(void)
   244 	{
   245 	delete iSignerImpl;
   246 	delete iKey;
   247 	}
   248 
   249 CRSAPKCS1v15SignerShim::CRSAPKCS1v15SignerShim(const CRSAPrivateKey& aKey)
   250 	: CRSAPKCS1v15Signer(aKey)
   251 	{
   252 	}
   253 	
   254 void CRSAPKCS1v15SignerShim::ConstructL(const CRSAPrivateKey& aKey)
   255 	{
   256 	iKey = KeyConverter::CreateKeyL(aKey);
   257 	CSignatureFactory::CreateSignerL(
   258 									iSignerImpl,
   259 									KRsaSignerUid,
   260 									*iKey,
   261 									KPaddingModePkcs1_v1_5_SignatureUid,
   262 									NULL);
   263 	}
   264 
   265 // CRSAPKCS1v15VerifierShim ////////////////////////////////////////////////////////
   266 CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewL(const CRSAPublicKey& aKey)
   267 	{
   268 	CRSAPKCS1v15VerifierShim* self = CRSAPKCS1v15VerifierShim::NewLC(aKey);
   269 	CleanupStack::Pop(self);
   270 	return self;
   271 	}
   272 
   273 CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewLC(const CRSAPublicKey& aKey)
   274 	{
   275 	CRSAPKCS1v15VerifierShim* self = new (ELeave) CRSAPKCS1v15VerifierShim(aKey);
   276 	CleanupStack::PushL(self);
   277 	self->ConstructL(aKey);
   278 	return self;
   279 	}
   280 	
   281 TBool CRSAPKCS1v15VerifierShim::VerifyL(const TDesC8& aInput, const CRSASignature& aSignature) const
   282 	{
   283 	//create the array format rsa signature for the new crypto spi
   284 	CCryptoParams* rsaSig = CCryptoParams::NewLC();
   285 
   286 	rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid);
   287 
   288 	//pass the signature and input to crypto spi to be verified
   289 	TBool verificationResult = EFalse;	
   290 	iVerifierImpl->VerifyL(aInput, *rsaSig, verificationResult);
   291 
   292 	//Cleanup the array
   293 	CleanupStack::PopAndDestroy(rsaSig);
   294 	return verificationResult;
   295 	}
   296 
   297 HBufC8* CRSAPKCS1v15VerifierShim::InverseSignLC(const CRSASignature& aSignature) const
   298 	{
   299 	//create the array format rsa signature for the new crypto spi
   300 	CCryptoParams* rsaSig = CCryptoParams::NewLC();
   301 
   302 	rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid);
   303 
   304 	//pass the signature and input to crypto spi to be verified
   305 	HBufC8* output = NULL;
   306 	iVerifierImpl->InverseSignL(output, *rsaSig);
   307 
   308 	//Cleanup the array
   309 	CleanupStack::PopAndDestroy(rsaSig);
   310 
   311 	// leave output on the cleanup stack
   312 	CleanupStack::PushL(output);
   313 	return output;
   314 	}
   315 
   316 TInt CRSAPKCS1v15VerifierShim::MaxInputLength(void) const
   317 	{
   318 	TInt maxInputLength=0; 
   319 	TRAPD(err, maxInputLength=iVerifierImpl->GetMaximumInputLengthL())
   320 	if (err==KErrNone)
   321 		{
   322 		return maxInputLength;
   323 		}
   324 	else
   325 		{
   326 		return err;
   327 		}
   328 	}
   329 	
   330 TInt CRSAPKCS1v15VerifierShim::MaxOutputLength(void) const
   331 	{
   332 	TInt maxOutputLength=0; 
   333 	TRAPD(err, maxOutputLength=iVerifierImpl->GetMaximumOutputLengthL())
   334 	if (err==KErrNone)
   335 		{
   336 		return maxOutputLength;
   337 		}
   338 	else
   339 		{
   340 		return err;
   341 		}
   342 	}
   343 
   344 CRSAPKCS1v15VerifierShim::~CRSAPKCS1v15VerifierShim(void)
   345 	{
   346 	delete iVerifierImpl;
   347 	delete iKey;
   348 	}
   349 
   350 CRSAPKCS1v15VerifierShim::CRSAPKCS1v15VerifierShim(const CRSAPublicKey& aKey)
   351 	: CRSAPKCS1v15Verifier(aKey)
   352 	{
   353 	}
   354 
   355 void CRSAPKCS1v15VerifierShim::ConstructL(const CRSAPublicKey& aKey)
   356 	{
   357 	iKey = KeyConverter::CreateKeyL(aKey);
   358 	CSignatureFactory::CreateVerifierL(
   359 									iVerifierImpl,
   360 									KRsaVerifierUid,
   361 									*iKey,
   362 									KPaddingModePkcs1_v1_5_SignatureUid,
   363 									NULL);
   364 	}
   365