sl@0: /* sl@0: * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "rc2shim.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "../common/inlines.h" sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: // CRC2EncryptorShim //////////////////////////////////////////////////////// sl@0: CRC2EncryptorShim* CRC2EncryptorShim::NewL(const TDesC8& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: CRC2EncryptorShim* self = CRC2EncryptorShim::NewLC(aKey, aEffectiveKeyLenBits); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CRC2EncryptorShim* CRC2EncryptorShim::NewLC(const TDesC8& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: CRC2EncryptorShim* self = new (ELeave) CRC2EncryptorShim(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey, aEffectiveKeyLenBits); sl@0: // weak enough if either aKey or aEffectiveKeyLenBits is weak sl@0: TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size())); sl@0: TCrypto::IsSymmetricWeakEnoughL(minKeySize); sl@0: return self; sl@0: } sl@0: sl@0: CRC2EncryptorShim::CRC2EncryptorShim() sl@0: { sl@0: } sl@0: sl@0: CRC2EncryptorShim::~CRC2EncryptorShim() sl@0: { sl@0: delete iSymmetricCipherImpl; sl@0: delete iKey; sl@0: delete iAlgorithmParams; sl@0: } sl@0: sl@0: void CRC2EncryptorShim::ConstructL(const TDesC8& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: TKeyProperty keyProperty = {KRc2Uid, KNullUid, KSymmetricKeyUid, KNonEmbeddedKeyUid}; sl@0: CCryptoParams* keyParam =CCryptoParams::NewLC(); sl@0: keyParam->AddL(aKey, KSymmetricKeyParameterUid); sl@0: iKey=CKey::NewL(keyProperty, *keyParam); sl@0: CleanupStack::PopAndDestroy(keyParam); sl@0: sl@0: iAlgorithmParams = CCryptoParams::NewL(); sl@0: iAlgorithmParams->AddL(aEffectiveKeyLenBits, KRC2EffectiveKeyLenBits); sl@0: sl@0: CSymmetricCipherFactory::CreateSymmetricCipherL( sl@0: iSymmetricCipherImpl, sl@0: KRc2Uid, sl@0: *iKey, sl@0: KCryptoModeEncryptUid, sl@0: KOperationModeECBUid, sl@0: KPaddingModeNoneUid, sl@0: iAlgorithmParams); sl@0: } sl@0: sl@0: TInt CRC2EncryptorShim::BlockSize() const sl@0: { sl@0: // SPI returns block size in BITS sl@0: return BitsToBytes(iSymmetricCipherImpl->BlockSize()); sl@0: } sl@0: sl@0: TInt CRC2EncryptorShim::KeySize() const sl@0: { sl@0: return iSymmetricCipherImpl->KeySize(); sl@0: } sl@0: sl@0: void CRC2EncryptorShim::Transform(TDes8& aBlock) sl@0: { sl@0: iOutputBlock.Zero(); sl@0: TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);) sl@0: aBlock = iOutputBlock; sl@0: } sl@0: sl@0: void CRC2EncryptorShim::Reset() sl@0: { sl@0: iSymmetricCipherImpl->Reset(); sl@0: } sl@0: sl@0: TInt CRC2EncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/) sl@0: { sl@0: TInt ret(KErrExtensionNotSupported); sl@0: sl@0: if (KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl) sl@0: { sl@0: a0=iSymmetricCipherImpl; sl@0: ret=KErrNone; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: // CRC2DecryptorShim //////////////////////////////////////////////////////// sl@0: CRC2DecryptorShim* CRC2DecryptorShim::NewL(const TDesC8& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: CRC2DecryptorShim* self = CRC2DecryptorShim::NewLC(aKey, aEffectiveKeyLenBits); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: CRC2DecryptorShim* CRC2DecryptorShim::NewLC(const TDesC8& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: CRC2DecryptorShim* self = new (ELeave) CRC2DecryptorShim(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey, aEffectiveKeyLenBits); sl@0: // weak enough if either aKey or aEffectiveKeyLenBits is weak sl@0: TInt minKeySize = Min(aEffectiveKeyLenBits, BytesToBits(aKey.Size())); sl@0: TCrypto::IsSymmetricWeakEnoughL(minKeySize); sl@0: return self; sl@0: } sl@0: sl@0: CRC2DecryptorShim::CRC2DecryptorShim() sl@0: { sl@0: } sl@0: sl@0: CRC2DecryptorShim::~CRC2DecryptorShim() sl@0: { sl@0: delete iSymmetricCipherImpl; sl@0: delete iKey; sl@0: delete iAlgorithmParams; sl@0: } sl@0: sl@0: sl@0: void CRC2DecryptorShim::ConstructL(const TDesC8& aKey, TInt aEffectiveKeyLenBits) sl@0: { sl@0: TKeyProperty keyProperty = {KRc2Uid, KNullUid, KSymmetricKeyUid, KNonEmbeddedKeyUid}; sl@0: CCryptoParams* keyParam =CCryptoParams::NewLC(); sl@0: keyParam->AddL(aKey, KSymmetricKeyParameterUid); sl@0: iKey=CKey::NewL(keyProperty, *keyParam); sl@0: CleanupStack::PopAndDestroy(keyParam); sl@0: sl@0: iAlgorithmParams = CCryptoParams::NewL(); sl@0: iAlgorithmParams->AddL(aEffectiveKeyLenBits, KRC2EffectiveKeyLenBits); sl@0: sl@0: CSymmetricCipherFactory::CreateSymmetricCipherL( sl@0: iSymmetricCipherImpl, sl@0: KRc2Uid, sl@0: *iKey, sl@0: KCryptoModeDecryptUid, sl@0: KOperationModeECBUid, sl@0: KPaddingModeNoneUid, sl@0: iAlgorithmParams); sl@0: } sl@0: sl@0: TInt CRC2DecryptorShim::BlockSize() const sl@0: { sl@0: // SPI returns block size in BITS sl@0: return BitsToBytes(iSymmetricCipherImpl->BlockSize()); sl@0: } sl@0: sl@0: TInt CRC2DecryptorShim::KeySize() const sl@0: { sl@0: return iSymmetricCipherImpl->KeySize(); sl@0: } sl@0: sl@0: void CRC2DecryptorShim::Transform(TDes8& aBlock) sl@0: { sl@0: iOutputBlock.Zero(); sl@0: TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aBlock, iOutputBlock);) sl@0: aBlock = iOutputBlock; sl@0: } sl@0: sl@0: void CRC2DecryptorShim::Reset() sl@0: { sl@0: iSymmetricCipherImpl->Reset(); sl@0: } sl@0: sl@0: TInt CRC2DecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/) sl@0: { sl@0: TInt ret(KErrExtensionNotSupported); sl@0: sl@0: if (CryptoSpi::KSymmetricCipherInterface == aExtensionId && iSymmetricCipherImpl) sl@0: { sl@0: a0=iSymmetricCipherImpl; sl@0: ret=KErrNone; sl@0: } sl@0: return ret; sl@0: }