Update contrib.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include "symmetriccipherstepbase.h"
29 Common setup of a crypto SPI symmetric cipher.
30 @param aArc4Check Determines whether the check is run on the cipher type for ARC4 and if it matches, sets up the CCryptoParams accordingly.
31 @param aRc2Check Determines whether the check is run on the cipher type for RC2 and if it matches, sets up the CCryptoParams accordingly.
32 @param aOperationMode On return is the operation mode read in from the config file and used in the initial setup of the cipher.
33 @param aCipher On return the symmetric cipher implementation.
34 @param aKey On return aCipher's key object. This is should not be deleted until after aCipher is deleted.
35 @return The error value from the call to the symmetric cipher creation function.
37 void CSymmetricCipherStepBase::SetupCipherL(TBool aArc4Check, TBool aRc2Check, TVariantPtrC& aOperationMode, CSymmetricCipher*& aCipher, CKey*& aKey)
40 TVariantPtrC algorithm;
41 TVariantPtrC paddingMode;
43 if( !GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
44 !GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
45 !GetStringFromConfig(ConfigSection(),KConfigOperationMode, aOperationMode) ||
46 !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
48 User::Leave(KErrNotFound);
52 TKeyProperty keyProperty;
53 CCryptoParams* params = CCryptoParams::NewLC();
55 CFileReader* keyData = CFileReader::NewLC(keyPath);
56 params->AddL( *keyData, KSymmetricKeyParameterUid);
58 aKey=CKey::NewL(keyProperty, *params);
59 CleanupStack::PushL(aKey);
61 CCryptoParams* xparams = NULL;
62 if ((aArc4Check) && (TUid(algorithm) == KArc4Uid))
64 // Set the RC4 DiscardBytes to 0
65 xparams = CCryptoParams::NewLC();
66 xparams->AddL(NULL, KARC4DiscardBytes);
68 // Create a Symmetric Cipher with the values from the ini file
69 CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
70 CleanupStack::PopAndDestroy(xparams);
71 CleanupStack::Pop(aKey);
72 CleanupStack::PopAndDestroy(2, params);
74 else if ((aRc2Check) && (TUid(algorithm) == KRc2Uid))
76 TInt keylen = TPtrC8(*keyData).Length() * 8;
77 xparams = CCryptoParams::NewLC();
79 // Set the RC2 EffectiveKeyLen according to the input key size
80 xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
82 // Create a Symmetric Cipher with the values from the ini file
83 CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
84 CleanupStack::PopAndDestroy(xparams);
85 CleanupStack::Pop(aKey);
86 CleanupStack::PopAndDestroy(2, params);
90 // Create a Symmetric Cipher with the values from the ini file
91 CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
92 CleanupStack::Pop(aKey);
93 CleanupStack::PopAndDestroy(2, params);
99 When running in CTR mode call this function to calculate the block size of a cipher.
100 @param aCipher The cipher whose block size is returned.
101 @return The block size in bits.
103 TInt CSymmetricCipherStepBase::CtrModeCalcBlockSizeL(CSymmetricCipher& aCipher)
105 // aCipher MUST be running in CTR mode
106 aCipher.SetOperationModeL(KOperationModeCBCUid);
107 TInt blockSize = aCipher.BlockSize();
108 aCipher.SetOperationModeL(KOperationModeCTRUid);
114 Read in the plaintext from the course file listed in the configuration file.
115 @param aPlaintext Descriptor pointing to the plaintext.
116 @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.
118 HBufC8* CSymmetricCipherStepBase::ReadInPlaintextL()
121 if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, plaintextPath))
123 User::Leave(KErrNotFound);
126 return ReadFileL(plaintextPath);
131 Read in the ciphertext from the course file listed in the configuration file.
132 @param aCiphertext Descriptor pointing to the ciphertext.
133 @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.
136 HBufC8* CSymmetricCipherStepBase::ReadInCiphertextL()
138 TPtrC ciphertextPath;
139 if(!GetStringFromConfig(ConfigSection(),KConfigEncryptedPath, ciphertextPath))
141 User::Leave(KErrNotFound);
144 return ReadFileL(ciphertextPath);
149 Read in the IV from the course file listed in the configuration file.
150 @param aIV Descriptor pointing to the IV.
151 @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.
154 HBufC8* CSymmetricCipherStepBase::ReadInIvL()
157 if(!GetStringFromConfig(ConfigSection(),KConfigIVPath, ivPath))
159 User::Leave(KErrNotFound);
162 return ReadFileL(ivPath);
169 HBufC8* CSymmetricCipherStepBase::CtrModeIncrementCounterL(TDesC8& aCounter)
171 RInteger bigInt = RInteger::NewL(aCounter);
172 CleanupClosePushL(bigInt);
174 HBufC8* result = bigInt.BufferLC();
175 CleanupStack::Pop(result);
176 CleanupStack::PopAndDestroy();
180 HBufC8* CSymmetricCipherStepBase::ReadInHexCiphertextL()
182 HBufC8* hex = ReadInCiphertextL();
183 CleanupStack::PushL(hex);
184 HBufC8* result = ConvertFromHexFormatToRawL((*hex));
185 CleanupStack::PopAndDestroy(hex);
189 HBufC8* CSymmetricCipherStepBase::ReadInHexPlaintextL()
191 HBufC8* hex = ReadInPlaintextL();
192 CleanupStack::PushL(hex);
193 HBufC8* result = ConvertFromHexFormatToRawL((*hex));
194 CleanupStack::PopAndDestroy(hex);
198 HBufC8* CSymmetricCipherStepBase::ReadInHexPlainTextL(TPtrC aFile)
200 HBufC8* data = ReadFileL(aFile);
201 CleanupStack::PushL(data);
202 HBufC8* result = ConvertFromHexFormatToRawL(*data);
203 CleanupStack::PopAndDestroy(data);
207 HBufC8* CSymmetricCipherStepBase::ReadFileL(TPtrC aFile)
209 CFileReader* reader = CFileReader::NewL(aFile);
210 CleanupStack::PushL(reader);
212 HBufC8* fileData = ptr.AllocL();
213 CleanupStack::PopAndDestroy(reader);
217 HBufC8* CSymmetricCipherStepBase::ReadInHexIvL()
219 HBufC8* hex = ReadInIvL();
220 CleanupStack::PushL(hex);
221 HBufC8* result = ConvertFromHexFormatToRawL((*hex));
222 CleanupStack::PopAndDestroy(hex);
226 HBufC8* CSymmetricCipherStepBase::ConvertFromHexFormatToRawL(TDesC8& aInput)
229 HBufC8* result = HBufC8::NewLC(aInput.Length()/2);
232 for(TInt i = 0 ; i < aInput.Length()-1 ; i+=2)
234 hexPair = aInput.Mid(i,2);
236 User::LeaveIfError(lex.Val(val, EHex));
237 result->Des().Append(val);
239 CleanupStack::Pop(result);