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