os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsaimpl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 aCryptoMode,
    31 	TUid aPadding) :
    32 	CAsymmetricCipherImpl(aCryptoMode, aPadding)
    33 	{
    34 	}
    35 
    36 CRSAImpl* CRSAImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aPadding)
    37 	{
    38 	CRSAImpl* self = CRSAImpl::NewLC(aKey, aCryptoMode, aPadding);
    39 	CleanupStack::Pop(self);
    40 	return self;
    41 	}
    42 	
    43 CRSAImpl* CRSAImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aPadding)
    44 	{
    45 	CRSAImpl* self = new(ELeave) CRSAImpl(aCryptoMode, aPadding);
    46 	CleanupStack::PushL(self);
    47 	self->ConstructL(aKey);
    48 	return self;
    49 	}
    50 	
    51 CRSAImpl::~CRSAImpl()
    52 	{
    53 	}
    54 	
    55 TInt CRSAImpl::GetMaximumOutputLengthL() const
    56 	{
    57 	const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
    58 	
    59 	if (iCryptoMode.iUid == KCryptoModeDecrypt)
    60 		return N.ByteCount() - iPadding->MinPaddingLength();
    61 	else
    62 		return N.ByteCount();
    63 	}
    64 	
    65 TInt CRSAImpl::GetMaximumInputLengthL() const
    66 	{
    67 	const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
    68 	
    69 	if (iCryptoMode.iUid == KCryptoModeEncrypt)
    70 		return N.ByteCount() - iPadding->MinPaddingLength();
    71 	else
    72 		return N.ByteCount();
    73 	}
    74 	
    75 void CRSAImpl::ConstructL(const CKey& aKey)
    76 	{
    77 	const TInteger& N = aKey.GetBigIntL(KRsaKeyParameterNUid);
    78 	TCrypto::IsAsymmetricWeakEnoughL(N.BitCount());
    79 	CAsymmetricCipherImpl::ConstructL(aKey);
    80 	
    81 	if (! IsValidKeyLengthL(N.ByteCount()))
    82 		{
    83 		User::Leave(KErrKeySize);
    84 		}
    85 	}
    86 	
    87 CExtendedCharacteristics* CRSAImpl::CreateExtendedCharacteristicsL()
    88 	{
    89 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
    90 	// for exclusive use and are not CERTIFIED to be standards compliant.
    91 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
    92 	}
    93 	
    94 const CExtendedCharacteristics* CRSAImpl::GetExtendedCharacteristicsL()
    95 	{
    96 	return CRSAImpl::CreateExtendedCharacteristicsL();
    97 	}
    98 
    99 TUid CRSAImpl::ImplementationUid() const
   100 	{
   101 	return KCryptoPluginRsaCipherUid;
   102 	}
   103 	
   104 void CRSAImpl::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
   105 	{
   106 	__ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
   107 	__ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
   108 	
   109 	HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
   110 	TPtr8 ptr = buf->Des();
   111 	
   112 	iPadding->PadL(aInput, ptr);
   113 	RInteger input = RInteger::NewL(ptr);
   114 	CleanupStack::PushL(input);
   115 	
   116 	RInteger output;
   117 	RSAFunction::EncryptL(*iKey, input, output);
   118 	CleanupStack::PushL(output);
   119 	
   120 	aOutput.Append(*(output.BufferLC()));
   121 	CleanupStack::PopAndDestroy(4, buf); //BufferLC, output, input, buf
   122 	}
   123 
   124 void CRSAImpl::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
   125 	{
   126 	__ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
   127 	__ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
   128 	
   129 	RInteger input = RInteger::NewL(aInput);
   130 	CleanupStack::PushL(input);
   131 	
   132 	RInteger output;
   133 	
   134 	RSAFunction::DecryptL(*iKey, input, output);
   135 	CleanupStack::PushL(output);
   136 	
   137 	TPtrC8 ptr = *(output.BufferLC());
   138 	iPadding->UnPadL(ptr, aOutput);
   139 	
   140 	CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
   141 	}
   142 
   143 void CRSAImpl::ProcessL(const TDesC8& aInput, TDes8& aOutput)
   144 	{
   145 	if (iCryptoMode.iUid == KCryptoModeEncrypt)
   146 		{
   147 		EncryptL(aInput, aOutput);
   148 		}
   149 	else
   150 		{
   151 		DecryptL(aInput, aOutput);
   152 		}
   153 	}
   154 
   155 TBool CRSAImpl::IsValidKeyLengthL(TInt aKeyBytes) const
   156 	{
   157 	if (aKeyBytes < 1)
   158 		return EFalse;
   159 	
   160 	switch (iCryptoMode.iUid)
   161 		{
   162 		case KCryptoModeEncrypt:
   163 			// Check if GetMaximumInputLengthL() makes sense,
   164 			// if not the key length must be too small
   165 			if (GetMaximumInputLengthL() <= 0)
   166 				return EFalse;
   167 			break;
   168 		
   169 		case KCryptoModeDecrypt:
   170 			// Check if GetMaximumOutputLengthL() makes sense,
   171 			// if not the key length must be too small
   172 			if (GetMaximumOutputLengthL() <= 0)
   173 				return EFalse;
   174 			break;
   175 		}
   176 	return ETrue;
   177 	}
   178