crypto/weakcryptospi/test/tcryptospi/src/symmetriccipherstepbase.cpp
changeset 8 35751d3474b7
equal deleted inserted replaced
2:675a964f4eb5 8:35751d3474b7
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21  @internalTechnology
       
    22 */
       
    23  
       
    24 #include "symmetriccipherstepbase.h"
       
    25 #include <bigint.h>
       
    26 
       
    27 
       
    28 /**
       
    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.
       
    36 */
       
    37 void CSymmetricCipherStepBase::SetupCipherL(TBool aArc4Check, TBool aRc2Check, TVariantPtrC& aOperationMode, CSymmetricCipher*& aCipher, CKey*& aKey)
       
    38 	{	
       
    39 	TPtrC keyPath;
       
    40 	TVariantPtrC algorithm;
       
    41 	TVariantPtrC paddingMode;
       
    42 		
       
    43 	if(	!GetStringFromConfig(ConfigSection(),KConfigEncryptKey, keyPath) ||
       
    44 		!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) || 
       
    45 		!GetStringFromConfig(ConfigSection(),KConfigOperationMode, aOperationMode) ||
       
    46 		!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
       
    47 		{
       
    48 		User::Leave(KErrNotFound);
       
    49 		}
       
    50 	
       
    51 	// Create key 
       
    52 	TKeyProperty keyProperty;
       
    53 	CCryptoParams* params = CCryptoParams::NewLC(); 
       
    54 	
       
    55 	CFileReader* keyData = CFileReader::NewLC(keyPath);
       
    56 	params->AddL( *keyData, KSymmetricKeyParameterUid);
       
    57 	
       
    58 	aKey=CKey::NewL(keyProperty, *params);
       
    59 	CleanupStack::PushL(aKey);
       
    60 
       
    61 	CCryptoParams* xparams = NULL;	
       
    62 	if ((aArc4Check) && (TUid(algorithm) == KArc4Uid))
       
    63 		{
       
    64 		// Set the RC4 DiscardBytes to 0
       
    65 		xparams = CCryptoParams::NewLC();
       
    66 		xparams->AddL(NULL, KARC4DiscardBytes);
       
    67 		
       
    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);
       
    73 		}
       
    74 	else if ((aRc2Check) && (TUid(algorithm) == KRc2Uid))
       
    75 		{
       
    76 		TInt keylen = TPtrC8(*keyData).Length() * 8;
       
    77 		xparams = CCryptoParams::NewLC();
       
    78 				
       
    79 		// Set the RC2 EffectiveKeyLen according to the input key size
       
    80 		xparams->AddL( keylen, KRC2EffectiveKeyLenBits);
       
    81 		
       
    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);
       
    87 		}
       
    88 	else
       
    89 		{
       
    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);
       
    94 		}
       
    95 	}
       
    96 	
       
    97 
       
    98 /**
       
    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.
       
   102 */	
       
   103 TInt CSymmetricCipherStepBase::CtrModeCalcBlockSizeL(CSymmetricCipher& aCipher)
       
   104 	{
       
   105   	// aCipher MUST be running in CTR mode
       
   106 	aCipher.SetOperationModeL(KOperationModeCBCUid);
       
   107 	TInt blockSize = aCipher.BlockSize();
       
   108 	aCipher.SetOperationModeL(KOperationModeCTRUid);
       
   109 	return blockSize;	
       
   110 	}
       
   111 
       
   112 	
       
   113 /**
       
   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.
       
   117 */	
       
   118 HBufC8* CSymmetricCipherStepBase::ReadInPlaintextL()
       
   119 	{
       
   120 	TPtrC plaintextPath;
       
   121 	if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, plaintextPath))
       
   122 		{
       
   123 		User::Leave(KErrNotFound);
       
   124 		}
       
   125 	
       
   126 	return ReadFileL(plaintextPath);
       
   127 	}
       
   128 
       
   129 
       
   130 /**
       
   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.
       
   134 
       
   135 */
       
   136 HBufC8* CSymmetricCipherStepBase::ReadInCiphertextL()
       
   137 	{
       
   138 	TPtrC ciphertextPath;
       
   139 	if(!GetStringFromConfig(ConfigSection(),KConfigEncryptedPath, ciphertextPath))
       
   140 		{
       
   141 		User::Leave(KErrNotFound);
       
   142 		}
       
   143 
       
   144 	return ReadFileL(ciphertextPath);
       
   145 	}
       
   146 
       
   147 
       
   148 /**
       
   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.
       
   152 
       
   153 */
       
   154 HBufC8* CSymmetricCipherStepBase::ReadInIvL()
       
   155 	{
       
   156 	TPtrC ivPath;
       
   157 	if(!GetStringFromConfig(ConfigSection(),KConfigIVPath, ivPath))
       
   158 		{
       
   159 		User::Leave(KErrNotFound);
       
   160 		}
       
   161 
       
   162 	return ReadFileL(ivPath);
       
   163 	}
       
   164 	
       
   165 	
       
   166 /**
       
   167 @param aCounter
       
   168 */	
       
   169 HBufC8* CSymmetricCipherStepBase::CtrModeIncrementCounterL(TDesC8& aCounter)
       
   170 	{
       
   171 	RInteger bigInt = RInteger::NewL(aCounter);
       
   172 	CleanupClosePushL(bigInt);
       
   173 	++bigInt;
       
   174 	HBufC8* result = bigInt.BufferLC();
       
   175 	CleanupStack::Pop(result);
       
   176 	CleanupStack::PopAndDestroy();
       
   177 	return result;
       
   178 	}
       
   179 
       
   180 HBufC8* CSymmetricCipherStepBase::ReadInHexCiphertextL()
       
   181 	{
       
   182 	HBufC8* hex = ReadInCiphertextL();
       
   183 	CleanupStack::PushL(hex);
       
   184 	HBufC8* result = ConvertFromHexFormatToRawL((*hex));
       
   185 	CleanupStack::PopAndDestroy(hex);
       
   186 	return result;
       
   187 	}
       
   188 	
       
   189 HBufC8* CSymmetricCipherStepBase::ReadInHexPlaintextL()
       
   190 	{
       
   191 	HBufC8* hex = ReadInPlaintextL();
       
   192 	CleanupStack::PushL(hex);
       
   193 	HBufC8* result = ConvertFromHexFormatToRawL((*hex));
       
   194 	CleanupStack::PopAndDestroy(hex);
       
   195 	return result;
       
   196 	}	
       
   197 
       
   198 HBufC8* CSymmetricCipherStepBase::ReadInHexPlainTextL(TPtrC aFile)
       
   199 	{
       
   200 	HBufC8* data = ReadFileL(aFile);
       
   201 	CleanupStack::PushL(data);
       
   202 	HBufC8* result = ConvertFromHexFormatToRawL(*data);
       
   203 	CleanupStack::PopAndDestroy(data);
       
   204 	return result;
       
   205 	}	
       
   206 
       
   207 HBufC8* CSymmetricCipherStepBase::ReadFileL(TPtrC aFile)
       
   208 	{
       
   209 	CFileReader* reader = CFileReader::NewL(aFile);
       
   210 	CleanupStack::PushL(reader);
       
   211 	TPtrC8 ptr(*reader);
       
   212 	HBufC8* fileData = ptr.AllocL();
       
   213 	CleanupStack::PopAndDestroy(reader);
       
   214 	return fileData;
       
   215 	}
       
   216 
       
   217 HBufC8* CSymmetricCipherStepBase::ReadInHexIvL()
       
   218 	{
       
   219 	HBufC8* hex = ReadInIvL();
       
   220 	CleanupStack::PushL(hex);
       
   221 	HBufC8* result = ConvertFromHexFormatToRawL((*hex));
       
   222 	CleanupStack::PopAndDestroy(hex);
       
   223 	return result;
       
   224 	}
       
   225 
       
   226 HBufC8* CSymmetricCipherStepBase::ConvertFromHexFormatToRawL(TDesC8& aInput)
       
   227 	{
       
   228 	TBuf8<2> hexPair;
       
   229 	HBufC8* result = HBufC8::NewLC(aInput.Length()/2);
       
   230 	TUint8 val;
       
   231 			
       
   232 	for(TInt i = 0 ; i < aInput.Length()-1 ; i+=2)
       
   233 		{
       
   234 		hexPair = aInput.Mid(i,2);
       
   235 		TLex8 lex(hexPair);
       
   236 		User::LeaveIfError(lex.Val(val, EHex));
       
   237 		result->Des().Append(val);			
       
   238 		}
       
   239 	CleanupStack::Pop(result);
       
   240 	return result;	
       
   241 	}