os/security/crypto/weakcryptospi/test/tcryptospi/src/asymmetriccipherencryptdecryptstep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/asymmetriccipherencryptdecryptstep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,212 @@
     1.4 +/*
     1.5 +* Copyright (c) 2007-2010 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 +* Example CTestStep derived implementation
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +/**
    1.24 + @file
    1.25 + @internalTechnology
    1.26 +*/
    1.27 +#include "asymmetriccipherencryptdecryptstep.h"
    1.28 +
    1.29 +#include <cryptospi/cryptoasymmetriccipherapi.h>
    1.30 +#include <cryptospi/cryptokeypairgeneratorapi.h>
    1.31 +#include <cryptospi/keypair.h>
    1.32 +#include "filereader.h"
    1.33 +
    1.34 +using namespace CryptoSpi;
    1.35 +
    1.36 +CASymmetricCipherEncryptDecryptStep::~CASymmetricCipherEncryptDecryptStep()
    1.37 +	{
    1.38 +	}
    1.39 +
    1.40 +
    1.41 +CASymmetricCipherEncryptDecryptStep::CASymmetricCipherEncryptDecryptStep()
    1.42 +	{
    1.43 +	SetTestStepName(KASymmetricCipherEncryptDecryptStep);
    1.44 +	}
    1.45 +
    1.46 +
    1.47 +TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepPreambleL()
    1.48 +	{
    1.49 +	SetTestStepResult(EPass);
    1.50 +	return TestStepResult();
    1.51 +	}
    1.52 +
    1.53 +
    1.54 +TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepL()
    1.55 +	{
    1.56 +	INFO_PRINTF1(_L("*** Asymmetric Cipher - Encrypt/Decrypt ***"));
    1.57 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.58 +  	
    1.59 +  	if (TestStepResult()==EPass)
    1.60 +		{
    1.61 +		
    1.62 +		//Assume faliure, unless all is successful
    1.63 +		SetTestStepResult(EFail);
    1.64 +		
    1.65 +		TPtrC srcPath;
    1.66 +		TVariantPtrC paddingMode;
    1.67 +		
    1.68 +		if(!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) ||
    1.69 +			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode ))
    1.70 +			{
    1.71 +			User::Leave(KErrNotFound);
    1.72 +			}
    1.73 +		else
    1.74 +			{
    1.75 +			
    1.76 +			CCryptoParams* keyParams = CCryptoParams::NewLC(); 
    1.77 +			
    1.78 +			//****************************************************
    1.79 +			//Create Key Pair and Key Pair Generator Objects
    1.80 +   			CKeyPair* keyPair = NULL;
    1.81 +			CKeyPairGenerator * keypairImpl = NULL;
    1.82 +			
    1.83 +			// create an RSA key pair
    1.84 +			INFO_PRINTF1(_L("Generating RSA keys"));
    1.85 +
    1.86 +			keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
    1.87 +			keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
    1.88 +
    1.89 +			// create a key pair generator implementation interface
    1.90 +			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(
    1.91 +											keypairImpl, 
    1.92 +											KRSAKeyPairGeneratorUid, 
    1.93 +											keyParams));
    1.94 +											
    1.95 +			CleanupStack::PushL(keypairImpl);
    1.96 +
    1.97 +			// Create a Key Pair	
    1.98 +			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
    1.99 +			
   1.100 +			CleanupStack::PushL(keyPair);
   1.101 +			
   1.102 +			//*****************************************************
   1.103 +			
   1.104 +			INFO_PRINTF1(_L("Creating Asymmetric Cipher Object..."));
   1.105 +			
   1.106 +			// Create an Asymmetric Cipher with the values fromt he ini	
   1.107 +			CryptoSpi::CAsymmetricCipher * impl = NULL;	
   1.108 +			
   1.109 +			TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL
   1.110 +										(
   1.111 +										impl,
   1.112 +										KRsaCipherUid,
   1.113 +										keyPair->PublicKey(),
   1.114 +										KCryptoModeEncryptUid,
   1.115 +										paddingMode,
   1.116 +										NULL));
   1.117 +	
   1.118 +			if(impl && (err == KErrNone))
   1.119 +				{
   1.120 +				INFO_PRINTF1(_L("*** Asymmetric Cipher Implementation Created... ***"));
   1.121 +				
   1.122 +				CleanupStack::PushL(impl);
   1.123 +				
   1.124 +				//read from src file
   1.125 +				CFileReader* srcData = CFileReader::NewLC(srcPath);
   1.126 +				
   1.127 +				//Create buffer for encrypted data
   1.128 +				TInt maxOutputLength = impl->GetMaximumOutputLengthL();
   1.129 +				HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
   1.130 +				TPtr8 encryptedPtr = encrypted->Des();
   1.131 +				
   1.132 +				INFO_PRINTF1(_L("Encrypting Source Data..."));
   1.133 +
   1.134 +				//Perform the encryption operation
   1.135 +				TRAP_LOG(err, impl->ProcessL(static_cast<TPtrC8>(*srcData), encryptedPtr));
   1.136 +				
   1.137 +				if(err == KErrNone)
   1.138 +					{
   1.139 +					
   1.140 +					if(encryptedPtr.Compare(TPtrC8(*srcData)) != 0)
   1.141 +						{
   1.142 +
   1.143 +						INFO_PRINTF1(_L("*** DATA ENCRYPTED ***"));
   1.144 +						
   1.145 +						//Switch to decrypt
   1.146 +						TRAP_LOG(err,impl->SetCryptoModeL(KCryptoModeDecryptUid));
   1.147 +						
   1.148 +						TRAP_LOG(err,impl->SetKeyL(keyPair->PrivateKey()));
   1.149 +
   1.150 +						//Create a buffer for the decrypted data
   1.151 +						maxOutputLength = encryptedPtr.Length();
   1.152 +						
   1.153 +						TInt bufSize = impl->GetMaximumOutputLengthL();
   1.154 +						
   1.155 +						HBufC8* output = HBufC8::NewLC(bufSize);
   1.156 +						TPtr8 outputPtr = output->Des();
   1.157 +						
   1.158 +						INFO_PRINTF1(_L("Decrypting Data..."));
   1.159 +						
   1.160 +						//Perform the decryption operation
   1.161 +						TRAP_LOG(err, impl->ProcessL(encryptedPtr, outputPtr));
   1.162 +						
   1.163 +						if(err == KErrNone)
   1.164 +							{
   1.165 +							INFO_PRINTF1(_L("*** DATA DECRYPTED ***"));
   1.166 +							//Check that the source data matches the data thats 
   1.167 +							//been encrypted then decrypted
   1.168 +							if(!outputPtr.Compare(*srcData))
   1.169 +								{
   1.170 +								INFO_PRINTF1(_L("PASS : Decrypted Data matches expected Source"));
   1.171 +								SetTestStepResult(EPass);
   1.172 +								}
   1.173 +							else
   1.174 +								{
   1.175 +								INFO_PRINTF1(_L("FAIL : Decrypted Data and expected Mismatch"));	
   1.176 +								SetTestStepResult(EFail);
   1.177 +								}
   1.178 +							}
   1.179 +
   1.180 +						CleanupStack::PopAndDestroy(output);
   1.181 +						}
   1.182 +					else
   1.183 +						{
   1.184 +						INFO_PRINTF1(_L("FAIL : Encrpyted Data and Source Data length and content is the same"));	
   1.185 +						SetTestStepResult(EFail);	
   1.186 +						}
   1.187 +					}
   1.188 +				
   1.189 +				
   1.190 +				CleanupStack::PopAndDestroy(encrypted);
   1.191 +				CleanupStack::PopAndDestroy(srcData);
   1.192 +				CleanupStack::PopAndDestroy(impl);
   1.193 +				}
   1.194 +			else
   1.195 +				{
   1.196 +				ERR_PRINTF2(_L("*** FAIL: Failed to Create Asymmetric Object - %d ***"), err);
   1.197 +				User::Leave(err);	
   1.198 +				}
   1.199 +			
   1.200 +			CleanupStack::PopAndDestroy(keyPair);
   1.201 +			CleanupStack::PopAndDestroy(keypairImpl);	
   1.202 +			CleanupStack::PopAndDestroy(keyParams);
   1.203 +			}
   1.204 +		}
   1.205 +	
   1.206 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.207 +
   1.208 +	return TestStepResult();
   1.209 +	}
   1.210 +
   1.211 +
   1.212 +TVerdict CASymmetricCipherEncryptDecryptStep::doTestStepPostambleL()
   1.213 +	{
   1.214 +	return TestStepResult();
   1.215 +	}