sl@0: /* sl@0: * Copyright (c) 2007-2009 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: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #include "symmetriccipherstepbase.h" sl@0: #include sl@0: sl@0: sl@0: /** sl@0: Common setup of a crypto SPI symmetric cipher. sl@0: @param aArc4Check Determines whether the check is run on the cipher type for ARC4 and if it matches, sets up the CCryptoParams accordingly. sl@0: @param aRc2Check Determines whether the check is run on the cipher type for RC2 and if it matches, sets up the CCryptoParams accordingly. sl@0: @param aOperationMode On return is the operation mode read in from the config file and used in the initial setup of the cipher. sl@0: @param aCipher On return the symmetric cipher implementation. sl@0: @param aKey On return aCipher's key object. This is should not be deleted until after aCipher is deleted. sl@0: @return The error value from the call to the symmetric cipher creation function. sl@0: */ sl@0: void CSymmetricCipherStepBase::SetupCipherL(TBool aArc4Check, TBool aRc2Check, TVariantPtrC& aOperationMode, CSymmetricCipher*& aCipher, CKey*& aKey) sl@0: { sl@0: TPtrC keyPath; sl@0: TVariantPtrC algorithm; sl@0: TVariantPtrC paddingMode; sl@0: sl@0: if( !GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigOperationMode, aOperationMode) || sl@0: !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode )) sl@0: { sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: sl@0: // Create key sl@0: TKeyProperty keyProperty; sl@0: CCryptoParams* params = CCryptoParams::NewLC(); sl@0: sl@0: CFileReader* keyData = CFileReader::NewLC(keyPath); sl@0: params->AddL( *keyData, KSymmetricKeyParameterUid); sl@0: sl@0: aKey=CKey::NewL(keyProperty, *params); sl@0: CleanupStack::PushL(aKey); sl@0: sl@0: CCryptoParams* xparams = NULL; sl@0: if ((aArc4Check) && (TUid(algorithm) == KArc4Uid)) sl@0: { sl@0: // Set the RC4 DiscardBytes to 0 sl@0: xparams = CCryptoParams::NewLC(); sl@0: xparams->AddL(NULL, KARC4DiscardBytes); sl@0: sl@0: // Create a Symmetric Cipher with the values from the ini file sl@0: CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams); sl@0: CleanupStack::PopAndDestroy(xparams); sl@0: CleanupStack::Pop(aKey); sl@0: CleanupStack::PopAndDestroy(2, params); sl@0: } sl@0: else if ((aRc2Check) && (TUid(algorithm) == KRc2Uid)) sl@0: { sl@0: TInt keylen = TPtrC8(*keyData).Length() * 8; sl@0: xparams = CCryptoParams::NewLC(); sl@0: sl@0: // Set the RC2 EffectiveKeyLen according to the input key size sl@0: xparams->AddL( keylen, KRC2EffectiveKeyLenBits); sl@0: sl@0: // Create a Symmetric Cipher with the values from the ini file sl@0: CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams); sl@0: CleanupStack::PopAndDestroy(xparams); sl@0: CleanupStack::Pop(aKey); sl@0: CleanupStack::PopAndDestroy(2, params); sl@0: } sl@0: else sl@0: { sl@0: // Create a Symmetric Cipher with the values from the ini file sl@0: CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams); sl@0: CleanupStack::Pop(aKey); sl@0: CleanupStack::PopAndDestroy(2, params); sl@0: } sl@0: } sl@0: sl@0: sl@0: /** sl@0: When running in CTR mode call this function to calculate the block size of a cipher. sl@0: @param aCipher The cipher whose block size is returned. sl@0: @return The block size in bits. sl@0: */ sl@0: TInt CSymmetricCipherStepBase::CtrModeCalcBlockSizeL(CSymmetricCipher& aCipher) sl@0: { sl@0: // aCipher MUST be running in CTR mode sl@0: aCipher.SetOperationModeL(KOperationModeCBCUid); sl@0: TInt blockSize = aCipher.BlockSize(); sl@0: aCipher.SetOperationModeL(KOperationModeCTRUid); sl@0: return blockSize; sl@0: } sl@0: sl@0: sl@0: /** sl@0: Read in the plaintext from the course file listed in the configuration file. sl@0: @param aPlaintext Descriptor pointing to the plaintext. sl@0: @param aReader This CFileReader pointer must be NULL when passed in. It must not be deleted until after the client has finished with the plaintext. sl@0: */ sl@0: HBufC8* CSymmetricCipherStepBase::ReadInPlaintextL() sl@0: { sl@0: TPtrC plaintextPath; sl@0: if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, plaintextPath)) sl@0: { sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: sl@0: return ReadFileL(plaintextPath); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Read in the ciphertext from the course file listed in the configuration file. sl@0: @param aCiphertext Descriptor pointing to the ciphertext. sl@0: @param aReader This CFileReader pointer must be NULL when passed in. It must not be deleted until after the client has finished with the ciphertext. sl@0: sl@0: */ sl@0: HBufC8* CSymmetricCipherStepBase::ReadInCiphertextL() sl@0: { sl@0: TPtrC ciphertextPath; sl@0: if(!GetStringFromConfig(ConfigSection(),KConfigEncryptedPath, ciphertextPath)) sl@0: { sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: sl@0: return ReadFileL(ciphertextPath); sl@0: } sl@0: sl@0: sl@0: /** sl@0: Read in the IV from the course file listed in the configuration file. sl@0: @param aIV Descriptor pointing to the IV. sl@0: @param aReader This CFileReader pointer must be NULL when passed in. It must not be deleted until after the client has finished with the IV. sl@0: sl@0: */ sl@0: HBufC8* CSymmetricCipherStepBase::ReadInIvL() sl@0: { sl@0: TPtrC ivPath; sl@0: if(!GetStringFromConfig(ConfigSection(),KConfigIVPath, ivPath)) sl@0: { sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: sl@0: return ReadFileL(ivPath); sl@0: } sl@0: sl@0: sl@0: /** sl@0: @param aCounter sl@0: */ sl@0: HBufC8* CSymmetricCipherStepBase::CtrModeIncrementCounterL(TDesC8& aCounter) sl@0: { sl@0: RInteger bigInt = RInteger::NewL(aCounter); sl@0: CleanupClosePushL(bigInt); sl@0: ++bigInt; sl@0: HBufC8* result = bigInt.BufferLC(); sl@0: CleanupStack::Pop(result); sl@0: CleanupStack::PopAndDestroy(); sl@0: return result; sl@0: } sl@0: sl@0: HBufC8* CSymmetricCipherStepBase::ReadInHexCiphertextL() sl@0: { sl@0: HBufC8* hex = ReadInCiphertextL(); sl@0: CleanupStack::PushL(hex); sl@0: HBufC8* result = ConvertFromHexFormatToRawL((*hex)); sl@0: CleanupStack::PopAndDestroy(hex); sl@0: return result; sl@0: } sl@0: sl@0: HBufC8* CSymmetricCipherStepBase::ReadInHexPlaintextL() sl@0: { sl@0: HBufC8* hex = ReadInPlaintextL(); sl@0: CleanupStack::PushL(hex); sl@0: HBufC8* result = ConvertFromHexFormatToRawL((*hex)); sl@0: CleanupStack::PopAndDestroy(hex); sl@0: return result; sl@0: } sl@0: sl@0: HBufC8* CSymmetricCipherStepBase::ReadInHexPlainTextL(TPtrC aFile) sl@0: { sl@0: HBufC8* data = ReadFileL(aFile); sl@0: CleanupStack::PushL(data); sl@0: HBufC8* result = ConvertFromHexFormatToRawL(*data); sl@0: CleanupStack::PopAndDestroy(data); sl@0: return result; sl@0: } sl@0: sl@0: HBufC8* CSymmetricCipherStepBase::ReadFileL(TPtrC aFile) sl@0: { sl@0: CFileReader* reader = CFileReader::NewL(aFile); sl@0: CleanupStack::PushL(reader); sl@0: TPtrC8 ptr(*reader); sl@0: HBufC8* fileData = ptr.AllocL(); sl@0: CleanupStack::PopAndDestroy(reader); sl@0: return fileData; sl@0: } sl@0: sl@0: HBufC8* CSymmetricCipherStepBase::ReadInHexIvL() sl@0: { sl@0: HBufC8* hex = ReadInIvL(); sl@0: CleanupStack::PushL(hex); sl@0: HBufC8* result = ConvertFromHexFormatToRawL((*hex)); sl@0: CleanupStack::PopAndDestroy(hex); sl@0: return result; sl@0: } sl@0: sl@0: HBufC8* CSymmetricCipherStepBase::ConvertFromHexFormatToRawL(TDesC8& aInput) sl@0: { sl@0: TBuf8<2> hexPair; sl@0: HBufC8* result = HBufC8::NewLC(aInput.Length()/2); sl@0: TUint8 val; sl@0: sl@0: for(TInt i = 0 ; i < aInput.Length()-1 ; i+=2) sl@0: { sl@0: hexPair = aInput.Mid(i,2); sl@0: TLex8 lex(hexPair); sl@0: User::LeaveIfError(lex.Val(val, EHex)); sl@0: result->Des().Append(val); sl@0: } sl@0: CleanupStack::Pop(result); sl@0: return result; sl@0: }