1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsaimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,178 @@
1.4 +/*
1.5 +* Copyright (c) 2006-2009 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 "rsaimpl.h"
1.23 +#include "rsafunction.h"
1.24 +#include "pluginconfig.h"
1.25 +#include <cryptopanic.h>
1.26 +#include <cryptostrength.h>
1.27 +#include <securityerr.h>
1.28 +
1.29 +using namespace SoftwareCrypto;
1.30 +
1.31 +/* CRSAImpl */
1.32 +CRSAImpl::CRSAImpl(
1.33 + TUid aCryptoMode,
1.34 + TUid aPadding) :
1.35 + CAsymmetricCipherImpl(aCryptoMode, aPadding)
1.36 + {
1.37 + }
1.38 +
1.39 +CRSAImpl* CRSAImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aPadding)
1.40 + {
1.41 + CRSAImpl* self = CRSAImpl::NewLC(aKey, aCryptoMode, aPadding);
1.42 + CleanupStack::Pop(self);
1.43 + return self;
1.44 + }
1.45 +
1.46 +CRSAImpl* CRSAImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aPadding)
1.47 + {
1.48 + CRSAImpl* self = new(ELeave) CRSAImpl(aCryptoMode, aPadding);
1.49 + CleanupStack::PushL(self);
1.50 + self->ConstructL(aKey);
1.51 + return self;
1.52 + }
1.53 +
1.54 +CRSAImpl::~CRSAImpl()
1.55 + {
1.56 + }
1.57 +
1.58 +TInt CRSAImpl::GetMaximumOutputLengthL() const
1.59 + {
1.60 + const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
1.61 +
1.62 + if (iCryptoMode.iUid == KCryptoModeDecrypt)
1.63 + return N.ByteCount() - iPadding->MinPaddingLength();
1.64 + else
1.65 + return N.ByteCount();
1.66 + }
1.67 +
1.68 +TInt CRSAImpl::GetMaximumInputLengthL() const
1.69 + {
1.70 + const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
1.71 +
1.72 + if (iCryptoMode.iUid == KCryptoModeEncrypt)
1.73 + return N.ByteCount() - iPadding->MinPaddingLength();
1.74 + else
1.75 + return N.ByteCount();
1.76 + }
1.77 +
1.78 +void CRSAImpl::ConstructL(const CKey& aKey)
1.79 + {
1.80 + const TInteger& N = aKey.GetBigIntL(KRsaKeyParameterNUid);
1.81 + TCrypto::IsAsymmetricWeakEnoughL(N.BitCount());
1.82 + CAsymmetricCipherImpl::ConstructL(aKey);
1.83 +
1.84 + if (! IsValidKeyLengthL(N.ByteCount()))
1.85 + {
1.86 + User::Leave(KErrKeySize);
1.87 + }
1.88 + }
1.89 +
1.90 +CExtendedCharacteristics* CRSAImpl::CreateExtendedCharacteristicsL()
1.91 + {
1.92 + // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
1.93 + // for exclusive use and are not CERTIFIED to be standards compliant.
1.94 + return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
1.95 + }
1.96 +
1.97 +const CExtendedCharacteristics* CRSAImpl::GetExtendedCharacteristicsL()
1.98 + {
1.99 + return CRSAImpl::CreateExtendedCharacteristicsL();
1.100 + }
1.101 +
1.102 +TUid CRSAImpl::ImplementationUid() const
1.103 + {
1.104 + return KCryptoPluginRsaCipherUid;
1.105 + }
1.106 +
1.107 +void CRSAImpl::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
1.108 + {
1.109 + __ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
1.110 + __ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
1.111 +
1.112 + HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
1.113 + TPtr8 ptr = buf->Des();
1.114 +
1.115 + iPadding->PadL(aInput, ptr);
1.116 + RInteger input = RInteger::NewL(ptr);
1.117 + CleanupStack::PushL(input);
1.118 +
1.119 + RInteger output;
1.120 + RSAFunction::EncryptL(*iKey, input, output);
1.121 + CleanupStack::PushL(output);
1.122 +
1.123 + aOutput.Append(*(output.BufferLC()));
1.124 + CleanupStack::PopAndDestroy(4, buf); //BufferLC, output, input, buf
1.125 + }
1.126 +
1.127 +void CRSAImpl::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
1.128 + {
1.129 + __ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
1.130 + __ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
1.131 +
1.132 + RInteger input = RInteger::NewL(aInput);
1.133 + CleanupStack::PushL(input);
1.134 +
1.135 + RInteger output;
1.136 +
1.137 + RSAFunction::DecryptL(*iKey, input, output);
1.138 + CleanupStack::PushL(output);
1.139 +
1.140 + TPtrC8 ptr = *(output.BufferLC());
1.141 + iPadding->UnPadL(ptr, aOutput);
1.142 +
1.143 + CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
1.144 + }
1.145 +
1.146 +void CRSAImpl::ProcessL(const TDesC8& aInput, TDes8& aOutput)
1.147 + {
1.148 + if (iCryptoMode.iUid == KCryptoModeEncrypt)
1.149 + {
1.150 + EncryptL(aInput, aOutput);
1.151 + }
1.152 + else
1.153 + {
1.154 + DecryptL(aInput, aOutput);
1.155 + }
1.156 + }
1.157 +
1.158 +TBool CRSAImpl::IsValidKeyLengthL(TInt aKeyBytes) const
1.159 + {
1.160 + if (aKeyBytes < 1)
1.161 + return EFalse;
1.162 +
1.163 + switch (iCryptoMode.iUid)
1.164 + {
1.165 + case KCryptoModeEncrypt:
1.166 + // Check if GetMaximumInputLengthL() makes sense,
1.167 + // if not the key length must be too small
1.168 + if (GetMaximumInputLengthL() <= 0)
1.169 + return EFalse;
1.170 + break;
1.171 +
1.172 + case KCryptoModeDecrypt:
1.173 + // Check if GetMaximumOutputLengthL() makes sense,
1.174 + // if not the key length must be too small
1.175 + if (GetMaximumOutputLengthL() <= 0)
1.176 + return EFalse;
1.177 + break;
1.178 + }
1.179 + return ETrue;
1.180 + }
1.181 +