1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherstepbase.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,241 @@
1.4 +/*
1.5 +* Copyright (c) 2007-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 +/**
1.23 + @file
1.24 + @internalTechnology
1.25 +*/
1.26 +
1.27 +#include "symmetriccipherstepbase.h"
1.28 +#include <bigint.h>
1.29 +
1.30 +
1.31 +/**
1.32 +Common setup of a crypto SPI symmetric cipher.
1.33 +@param aArc4Check Determines whether the check is run on the cipher type for ARC4 and if it matches, sets up the CCryptoParams accordingly.
1.34 +@param aRc2Check Determines whether the check is run on the cipher type for RC2 and if it matches, sets up the CCryptoParams accordingly.
1.35 +@param aOperationMode On return is the operation mode read in from the config file and used in the initial setup of the cipher.
1.36 +@param aCipher On return the symmetric cipher implementation.
1.37 +@param aKey On return aCipher's key object. This is should not be deleted until after aCipher is deleted.
1.38 +@return The error value from the call to the symmetric cipher creation function.
1.39 +*/
1.40 +void CSymmetricCipherStepBase::SetupCipherL(TBool aArc4Check, TBool aRc2Check, TVariantPtrC& aOperationMode, CSymmetricCipher*& aCipher, CKey*& aKey)
1.41 + {
1.42 + TPtrC keyPath;
1.43 + TVariantPtrC algorithm;
1.44 + TVariantPtrC paddingMode;
1.45 +
1.46 + if( !GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
1.47 + !GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) ||
1.48 + !GetStringFromConfig(ConfigSection(),KConfigOperationMode, aOperationMode) ||
1.49 + !GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
1.50 + {
1.51 + User::Leave(KErrNotFound);
1.52 + }
1.53 +
1.54 + // Create key
1.55 + TKeyProperty keyProperty;
1.56 + CCryptoParams* params = CCryptoParams::NewLC();
1.57 +
1.58 + CFileReader* keyData = CFileReader::NewLC(keyPath);
1.59 + params->AddL( *keyData, KSymmetricKeyParameterUid);
1.60 +
1.61 + aKey=CKey::NewL(keyProperty, *params);
1.62 + CleanupStack::PushL(aKey);
1.63 +
1.64 + CCryptoParams* xparams = NULL;
1.65 + if ((aArc4Check) && (TUid(algorithm) == KArc4Uid))
1.66 + {
1.67 + // Set the RC4 DiscardBytes to 0
1.68 + xparams = CCryptoParams::NewLC();
1.69 + xparams->AddL(NULL, KARC4DiscardBytes);
1.70 +
1.71 + // Create a Symmetric Cipher with the values from the ini file
1.72 + CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
1.73 + CleanupStack::PopAndDestroy(xparams);
1.74 + CleanupStack::Pop(aKey);
1.75 + CleanupStack::PopAndDestroy(2, params);
1.76 + }
1.77 + else if ((aRc2Check) && (TUid(algorithm) == KRc2Uid))
1.78 + {
1.79 + TInt keylen = TPtrC8(*keyData).Length() * 8;
1.80 + xparams = CCryptoParams::NewLC();
1.81 +
1.82 + // Set the RC2 EffectiveKeyLen according to the input key size
1.83 + xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
1.84 +
1.85 + // Create a Symmetric Cipher with the values from the ini file
1.86 + CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
1.87 + CleanupStack::PopAndDestroy(xparams);
1.88 + CleanupStack::Pop(aKey);
1.89 + CleanupStack::PopAndDestroy(2, params);
1.90 + }
1.91 + else
1.92 + {
1.93 + // Create a Symmetric Cipher with the values from the ini file
1.94 + CSymmetricCipherFactory::CreateSymmetricCipherL(aCipher, algorithm, *aKey, KCryptoModeEncryptUid, aOperationMode, paddingMode, xparams);
1.95 + CleanupStack::Pop(aKey);
1.96 + CleanupStack::PopAndDestroy(2, params);
1.97 + }
1.98 + }
1.99 +
1.100 +
1.101 +/**
1.102 +When running in CTR mode call this function to calculate the block size of a cipher.
1.103 +@param aCipher The cipher whose block size is returned.
1.104 +@return The block size in bits.
1.105 +*/
1.106 +TInt CSymmetricCipherStepBase::CtrModeCalcBlockSizeL(CSymmetricCipher& aCipher)
1.107 + {
1.108 + // aCipher MUST be running in CTR mode
1.109 + aCipher.SetOperationModeL(KOperationModeCBCUid);
1.110 + TInt blockSize = aCipher.BlockSize();
1.111 + aCipher.SetOperationModeL(KOperationModeCTRUid);
1.112 + return blockSize;
1.113 + }
1.114 +
1.115 +
1.116 +/**
1.117 +Read in the plaintext from the course file listed in the configuration file.
1.118 +@param aPlaintext Descriptor pointing to the plaintext.
1.119 +@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.
1.120 +*/
1.121 +HBufC8* CSymmetricCipherStepBase::ReadInPlaintextL()
1.122 + {
1.123 + TPtrC plaintextPath;
1.124 + if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, plaintextPath))
1.125 + {
1.126 + User::Leave(KErrNotFound);
1.127 + }
1.128 +
1.129 + return ReadFileL(plaintextPath);
1.130 + }
1.131 +
1.132 +
1.133 +/**
1.134 +Read in the ciphertext from the course file listed in the configuration file.
1.135 +@param aCiphertext Descriptor pointing to the ciphertext.
1.136 +@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.
1.137 +
1.138 +*/
1.139 +HBufC8* CSymmetricCipherStepBase::ReadInCiphertextL()
1.140 + {
1.141 + TPtrC ciphertextPath;
1.142 + if(!GetStringFromConfig(ConfigSection(),KConfigEncryptedPath, ciphertextPath))
1.143 + {
1.144 + User::Leave(KErrNotFound);
1.145 + }
1.146 +
1.147 + return ReadFileL(ciphertextPath);
1.148 + }
1.149 +
1.150 +
1.151 +/**
1.152 +Read in the IV from the course file listed in the configuration file.
1.153 +@param aIV Descriptor pointing to the IV.
1.154 +@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.
1.155 +
1.156 +*/
1.157 +HBufC8* CSymmetricCipherStepBase::ReadInIvL()
1.158 + {
1.159 + TPtrC ivPath;
1.160 + if(!GetStringFromConfig(ConfigSection(),KConfigIVPath, ivPath))
1.161 + {
1.162 + User::Leave(KErrNotFound);
1.163 + }
1.164 +
1.165 + return ReadFileL(ivPath);
1.166 + }
1.167 +
1.168 +
1.169 +/**
1.170 +@param aCounter
1.171 +*/
1.172 +HBufC8* CSymmetricCipherStepBase::CtrModeIncrementCounterL(TDesC8& aCounter)
1.173 + {
1.174 + RInteger bigInt = RInteger::NewL(aCounter);
1.175 + CleanupClosePushL(bigInt);
1.176 + ++bigInt;
1.177 + HBufC8* result = bigInt.BufferLC();
1.178 + CleanupStack::Pop(result);
1.179 + CleanupStack::PopAndDestroy();
1.180 + return result;
1.181 + }
1.182 +
1.183 +HBufC8* CSymmetricCipherStepBase::ReadInHexCiphertextL()
1.184 + {
1.185 + HBufC8* hex = ReadInCiphertextL();
1.186 + CleanupStack::PushL(hex);
1.187 + HBufC8* result = ConvertFromHexFormatToRawL((*hex));
1.188 + CleanupStack::PopAndDestroy(hex);
1.189 + return result;
1.190 + }
1.191 +
1.192 +HBufC8* CSymmetricCipherStepBase::ReadInHexPlaintextL()
1.193 + {
1.194 + HBufC8* hex = ReadInPlaintextL();
1.195 + CleanupStack::PushL(hex);
1.196 + HBufC8* result = ConvertFromHexFormatToRawL((*hex));
1.197 + CleanupStack::PopAndDestroy(hex);
1.198 + return result;
1.199 + }
1.200 +
1.201 +HBufC8* CSymmetricCipherStepBase::ReadInHexPlainTextL(TPtrC aFile)
1.202 + {
1.203 + HBufC8* data = ReadFileL(aFile);
1.204 + CleanupStack::PushL(data);
1.205 + HBufC8* result = ConvertFromHexFormatToRawL(*data);
1.206 + CleanupStack::PopAndDestroy(data);
1.207 + return result;
1.208 + }
1.209 +
1.210 +HBufC8* CSymmetricCipherStepBase::ReadFileL(TPtrC aFile)
1.211 + {
1.212 + CFileReader* reader = CFileReader::NewL(aFile);
1.213 + CleanupStack::PushL(reader);
1.214 + TPtrC8 ptr(*reader);
1.215 + HBufC8* fileData = ptr.AllocL();
1.216 + CleanupStack::PopAndDestroy(reader);
1.217 + return fileData;
1.218 + }
1.219 +
1.220 +HBufC8* CSymmetricCipherStepBase::ReadInHexIvL()
1.221 + {
1.222 + HBufC8* hex = ReadInIvL();
1.223 + CleanupStack::PushL(hex);
1.224 + HBufC8* result = ConvertFromHexFormatToRawL((*hex));
1.225 + CleanupStack::PopAndDestroy(hex);
1.226 + return result;
1.227 + }
1.228 +
1.229 +HBufC8* CSymmetricCipherStepBase::ConvertFromHexFormatToRawL(TDesC8& aInput)
1.230 + {
1.231 + TBuf8<2> hexPair;
1.232 + HBufC8* result = HBufC8::NewLC(aInput.Length()/2);
1.233 + TUint8 val;
1.234 +
1.235 + for(TInt i = 0 ; i < aInput.Length()-1 ; i+=2)
1.236 + {
1.237 + hexPair = aInput.Mid(i,2);
1.238 + TLex8 lex(hexPair);
1.239 + User::LeaveIfError(lex.Val(val, EHex));
1.240 + result->Des().Append(val);
1.241 + }
1.242 + CleanupStack::Pop(result);
1.243 + return result;
1.244 + }