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