os/security/crypto/weakcryptospi/test/tcryptospi/src/asymmetriccipherencrypteddatacheckstep.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/asymmetriccipherencrypteddatacheckstep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,183 @@
     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 "asymmetriccipherencrypteddatacheckstep.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 +CASymmetricCipherEncryptedDataCheckStep::~CASymmetricCipherEncryptedDataCheckStep()
    1.37 +	{
    1.38 +	}
    1.39 +
    1.40 +
    1.41 +CASymmetricCipherEncryptedDataCheckStep::CASymmetricCipherEncryptedDataCheckStep()
    1.42 +	{
    1.43 +	SetTestStepName(KASymmetricCipherEncryptedDataCheckStep);
    1.44 +	}
    1.45 +
    1.46 +
    1.47 +TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepPreambleL()
    1.48 +	{
    1.49 +	SetTestStepResult(EPass);
    1.50 +	return TestStepResult();
    1.51 +	}
    1.52 +
    1.53 +
    1.54 +TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepL()
    1.55 +	{
    1.56 +
    1.57 +	INFO_PRINTF1(_L("*** Asymmetric Cipher - Encrypted Data Check ***"));
    1.58 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.59 +	
    1.60 +  	if (TestStepResult()==EPass)
    1.61 +		{
    1.62 +		
    1.63 +		//Assume faliure, unless all is successful
    1.64 +		SetTestStepResult(EFail);
    1.65 +		
    1.66 +		TVariantPtrC algorithm;
    1.67 +		TVariantPtrC srcPath;
    1.68 +		TVariantPtrC encrypted;
    1.69 +		TVariantPtrC paddingMode;
    1.70 +		
    1.71 +		
    1.72 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid, algorithm) || 
    1.73 +			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode, paddingMode) ||
    1.74 +			!GetStringFromConfig(ConfigSection(),KConfigSourcePath, srcPath) || 
    1.75 +			!GetStringFromConfig(ConfigSection(),KConfigExEncryptedData, encrypted))
    1.76 +			{
    1.77 +			User::Leave(KErrNotFound);
    1.78 +			}
    1.79 +		else
    1.80 +			{
    1.81 +			//assume failure, unless all is successful
    1.82 +			SetTestStepResult(EFail);
    1.83 +			
    1.84 +			CCryptoParams* keyParams = CCryptoParams::NewLC(); 
    1.85 +			
    1.86 +			//****************************************************
    1.87 +			//Create Key Pair and Key Pair Generator Objects
    1.88 +   			CKeyPair* keyPair = NULL;
    1.89 +			CKeyPairGenerator * keypairImpl = NULL;
    1.90 +			
    1.91 +			// create an RSA key pair
    1.92 +			INFO_PRINTF1(_L("Generating RSA keys"));
    1.93 +
    1.94 +			keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
    1.95 +			keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
    1.96 +
    1.97 +			// create a key pair generator implementation interface
    1.98 +			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl, 
    1.99 +													KRSAKeyPairGeneratorUid, 
   1.100 +													keyParams));
   1.101 +													
   1.102 +			CleanupStack::PushL(keypairImpl);
   1.103 +
   1.104 +			// Create a Key Pair	
   1.105 +			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
   1.106 +			
   1.107 +			CleanupStack::PushL(keyPair);
   1.108 +			
   1.109 +			//*****************************************************
   1.110 +			
   1.111 +			INFO_PRINTF1(_L("Creating Asymmetric Cipher Object..."));
   1.112 +
   1.113 +			// Create a Symmetric Cipher 
   1.114 +			CryptoSpi::CAsymmetricCipher * impl = NULL;	
   1.115 +			
   1.116 +			TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL(impl,
   1.117 +														algorithm,
   1.118 +														keyPair->PublicKey(),
   1.119 +														KCryptoModeEncryptUid,
   1.120 +														paddingMode,
   1.121 +														NULL));
   1.122 +	
   1.123 +			if(impl && (err == KErrNone))
   1.124 +				{
   1.125 +				CleanupStack::PushL(impl);
   1.126 +				
   1.127 +				INFO_PRINTF1(_L("*** Asymmetric Cipher Implementation Created... ***"));
   1.128 +			
   1.129 +				//read from src file
   1.130 +				CFileReader* srcData = CFileReader::NewLC(srcPath);		
   1.131 +			
   1.132 +				//Create buffer for encrypted data
   1.133 +				TInt maxOutputLength = impl->GetMaximumOutputLengthL();
   1.134 +				HBufC8* encrypted =	HBufC8::NewLC(maxOutputLength);
   1.135 +				TPtr8 encryptedPtr = encrypted->Des();
   1.136 +				
   1.137 +				INFO_PRINTF1(_L("Encrypting Source Data..."));
   1.138 +
   1.139 +				//Perform the encryption operation
   1.140 +				TRAP_LOG(err,impl->ProcessL(TPtrC8(*srcData), encryptedPtr));
   1.141 +				
   1.142 +				if(err == KErrNone)
   1.143 +					{
   1.144 +					INFO_PRINTF1(_L("*** DATA ENCRYPTED ***"));
   1.145 +					
   1.146 +					//Check that the source data matches the data thats 
   1.147 +					//been encrypted then decrypted
   1.148 +					if(	!encryptedPtr.Compare(TPtrC8(*srcData)))
   1.149 +						{
   1.150 +						INFO_PRINTF1(_L("PASS : Encrypted Data Matches Expected"));
   1.151 +						SetTestStepResult(EPass);
   1.152 +						}
   1.153 +					else
   1.154 +						{
   1.155 +						INFO_PRINTF1(_L("FAIL : Encrypted Data and Expected Mismatch"));	
   1.156 +						SetTestStepResult(EFail);
   1.157 +						}
   1.158 +					}
   1.159 +	
   1.160 +				CleanupStack::PopAndDestroy(encrypted); 
   1.161 +				CleanupStack::PopAndDestroy(srcData);
   1.162 +				CleanupStack::PopAndDestroy(impl);
   1.163 +				}
   1.164 +			else
   1.165 +				{
   1.166 +				ERR_PRINTF2(_L("*** FAIL: Failed to Create Asymmetric Object - %d ***"), err);
   1.167 +				User::Leave(err);	
   1.168 +				}
   1.169 +				
   1.170 +			CleanupStack::PopAndDestroy(keyPair);
   1.171 +			CleanupStack::PopAndDestroy(keypairImpl);
   1.172 +			CleanupStack::PopAndDestroy(keyParams);
   1.173 +			}
   1.174 +			
   1.175 +		}
   1.176 +		
   1.177 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
   1.178 +
   1.179 +	return TestStepResult();
   1.180 +	}
   1.181 +
   1.182 +
   1.183 +TVerdict CASymmetricCipherEncryptedDataCheckStep::doTestStepPostambleL()
   1.184 +	{
   1.185 +	return TestStepResult();
   1.186 +	}