crypto/weakcryptospi/test/tplugins/src/rsaimpl.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2006-2009 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 "rsaimpl.h"
       
    20 #include "rsafunction.h"
       
    21 #include "pluginconfig.h"
       
    22 #include <cryptopanic.h>
       
    23 #include <cryptostrength.h>
       
    24 #include <securityerr.h>
       
    25 
       
    26 using namespace SoftwareCrypto;
       
    27 
       
    28 /* CRSAImpl */
       
    29 CRSAImpl::CRSAImpl(
       
    30 	TUid aImplementationUid,
       
    31 	TUid aCryptoMode,
       
    32 	TUid aPadding) : 
       
    33 	CAsymmetricCipherImpl(aCryptoMode, aPadding),
       
    34 	iImplementationUid(aImplementationUid)
       
    35 	{
       
    36 	}
       
    37 
       
    38 CRSAImpl* CRSAImpl::NewL(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aPadding)
       
    39 	{
       
    40 	CRSAImpl* self = CRSAImpl::NewLC(aImplementationUid, aKey, aCryptoMode, aPadding);
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 	
       
    45 CRSAImpl* CRSAImpl::NewLC(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aPadding)
       
    46 	{
       
    47 	CRSAImpl* self = new(ELeave) CRSAImpl(aImplementationUid, aCryptoMode, aPadding);
       
    48 	CleanupStack::PushL(self);
       
    49 	self->ConstructL(aKey);
       
    50 	return self;
       
    51 	}
       
    52 	
       
    53 CRSAImpl::~CRSAImpl()
       
    54 	{
       
    55 	}
       
    56 	
       
    57 TInt CRSAImpl::GetMaximumOutputLengthL() const
       
    58 	{
       
    59 	const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
       
    60 	
       
    61 	if (iCryptoMode.iUid == KCryptoModeDecrypt)
       
    62 		return N.ByteCount() - iPadding->MinPaddingLength();
       
    63 	else
       
    64 		return N.ByteCount();
       
    65 	}
       
    66 	
       
    67 TInt CRSAImpl::GetMaximumInputLengthL() const
       
    68 	{
       
    69 	const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
       
    70 	
       
    71 	if (iCryptoMode.iUid == KCryptoModeEncrypt)
       
    72 		return N.ByteCount() - iPadding->MinPaddingLength();
       
    73 	else
       
    74 		return N.ByteCount();
       
    75 	}
       
    76 	
       
    77 void CRSAImpl::ConstructL(const CKey& aKey)
       
    78 	{
       
    79 	const TInteger& N = aKey.GetBigIntL(KRsaKeyParameterNUid);
       
    80 	TCrypto::IsAsymmetricWeakEnoughL(N.BitCount());
       
    81 	CAsymmetricCipherImpl::ConstructL(aKey);
       
    82 	
       
    83 	if (! IsValidKeyLengthL(N.ByteCount()))
       
    84 		{
       
    85 		User::Leave(KErrKeySize);
       
    86 		}
       
    87 	}
       
    88 	
       
    89 CExtendedCharacteristics* CRSAImpl::CreateExtendedCharacteristicsL()
       
    90 	{
       
    91 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
       
    92 	// for exclusive use and are not CERTIFIED to be standards compliant.
       
    93 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
       
    94 	}
       
    95 	
       
    96 const CExtendedCharacteristics* CRSAImpl::GetExtendedCharacteristicsL()
       
    97 	{
       
    98 	return CRSAImpl::CreateExtendedCharacteristicsL();
       
    99 	}
       
   100 	
       
   101 TUid CRSAImpl::ImplementationUid() const
       
   102 	{
       
   103 	return iImplementationUid;
       
   104 	}
       
   105 	
       
   106 void CRSAImpl::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
       
   107 	{
       
   108 	__ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
       
   109 	__ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
       
   110 	
       
   111 	HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
       
   112 	TPtr8 ptr = buf->Des();
       
   113 	
       
   114 	iPadding->PadL(aInput, ptr);
       
   115 	RInteger input = RInteger::NewL(ptr);
       
   116 	CleanupStack::PushL(input);
       
   117 	
       
   118 	RInteger output;
       
   119 	RSAFunction::EncryptL(*iKey, input, output);
       
   120 	CleanupStack::PushL(output);
       
   121 	
       
   122 	aOutput.Append(*(output.BufferLC()));
       
   123 	CleanupStack::PopAndDestroy(4, buf); //BufferLC, output, input, buf
       
   124 	}
       
   125 
       
   126 void CRSAImpl::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
       
   127 	{
       
   128 	__ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
       
   129 	__ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
       
   130 	
       
   131 	RInteger input = RInteger::NewL(aInput);
       
   132 	CleanupStack::PushL(input);
       
   133 	
       
   134 	RInteger output;
       
   135 	
       
   136 	RSAFunction::DecryptL(*iKey, input, output);
       
   137 	CleanupStack::PushL(output);
       
   138 	
       
   139 	TPtrC8 ptr = *(output.BufferLC());
       
   140 	iPadding->UnPadL(ptr, aOutput);
       
   141 	
       
   142 	CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
       
   143 	}
       
   144 
       
   145 void CRSAImpl::ProcessL(const TDesC8& aInput, TDes8& aOutput)
       
   146 	{
       
   147 	if (iCryptoMode.iUid == KCryptoModeEncrypt)
       
   148 		{
       
   149 		EncryptL(aInput, aOutput);
       
   150 		}
       
   151 	else
       
   152 		{
       
   153 		DecryptL(aInput, aOutput);
       
   154 		}
       
   155 	}
       
   156 
       
   157 TBool CRSAImpl::IsValidKeyLengthL(TInt aKeyBytes) const
       
   158 	{
       
   159 	if (aKeyBytes < 1)
       
   160 		return EFalse;
       
   161 	
       
   162 	switch (iCryptoMode.iUid)
       
   163 		{
       
   164 		case KCryptoModeEncrypt:
       
   165 			// Check if GetMaximumInputLengthL() makes sense,
       
   166 			// if not the key length must be too small
       
   167 			if (GetMaximumInputLengthL() <= 0)
       
   168 				return EFalse;
       
   169 			break;
       
   170 		
       
   171 		case KCryptoModeDecrypt:
       
   172 			// Check if GetMaximumOutputLengthL() makes sense,
       
   173 			// if not the key length must be too small
       
   174 			if (GetMaximumOutputLengthL() <= 0)
       
   175 				return EFalse;
       
   176 			break;
       
   177 		}
       
   178 	return ETrue;
       
   179 	}
       
   180