os/security/crypto/weakcryptospi/test/tcryptospi/src/plugincharsasymmetriccipherstep.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/plugincharsasymmetriccipherstep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,239 @@
     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 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +/**
    1.23 + @file
    1.24 + @internalTechnology
    1.25 +*/
    1.26 + 
    1.27 +#include "plugincharsasymmetriccipherstep.h"
    1.28 +#include "plugincharschecker.h"
    1.29 +
    1.30 +#include <cryptospi/cryptoasymmetriccipherapi.h>
    1.31 +#include <cryptospi/cryptokeypairgeneratorapi.h>
    1.32 +#include <cryptospi/keypair.h>
    1.33 +
    1.34 +using namespace CryptoSpi;
    1.35 +
    1.36 +CPluginCharsAsymmetricCipherStep::~CPluginCharsAsymmetricCipherStep()
    1.37 +	{
    1.38 +	}
    1.39 +
    1.40 +CPluginCharsAsymmetricCipherStep::CPluginCharsAsymmetricCipherStep()
    1.41 +	{
    1.42 +	SetTestStepName(KPluginCharsAsymmetricCipherStep);
    1.43 +	}
    1.44 +
    1.45 +TVerdict CPluginCharsAsymmetricCipherStep::doTestStepPreambleL()
    1.46 +	{
    1.47 +	SetTestStepResult(EPass);
    1.48 +	return TestStepResult();
    1.49 +	}
    1.50 +				
    1.51 +TVerdict CPluginCharsAsymmetricCipherStep::doTestStepL()
    1.52 +	{
    1.53 +	
    1.54 +	INFO_PRINTF1(_L("Plugin Characteristics - Asymmetric Cipher Chracteristics"));
    1.55 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    1.56 +		
    1.57 +	if (TestStepResult()==EPass)
    1.58 +		{
    1.59 +		
    1.60 +		//Assume faliure, unless all is successful
    1.61 +		SetTestStepResult(EFail);
    1.62 +		
    1.63 +		TVariantPtrC algorithmUid;
    1.64 +		TVariantPtrC cryptoMode;
    1.65 +		TVariantPtrC paddingMode;
    1.66 +		TVariantPtrC invalidPaddingMode;
    1.67 +		
    1.68 +		//Each of the individual parameters required to create the Asymmetric Cipher object
    1.69 +		//are read in from the specified INI configuration file					
    1.70 +		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    1.71 +			!GetStringFromConfig(ConfigSection(),KConfigCryptoMode,cryptoMode) ||
    1.72 +			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode,paddingMode) ||
    1.73 +			!GetStringFromConfig(ConfigSection(),KConfigInvalidPaddingMode,invalidPaddingMode))
    1.74 +			{
    1.75 +			ERR_PRINTF1(_L("** .INI Error: Asymmetric Cipher Arguments Not Located **"));
    1.76 +			SetTestStepResult(EFail);
    1.77 +			}
    1.78 +		else
    1.79 +			{
    1.80 +			CCryptoParams* keyParams = CCryptoParams::NewLC(); 
    1.81 +			
    1.82 +			//****************************************************
    1.83 +			//Create Key Pair and Key Pair Generator Objects
    1.84 +   			CKeyPair* keyPair = NULL;
    1.85 +			CKeyPairGenerator * keypairImpl = NULL;
    1.86 +			
    1.87 +			INFO_PRINTF1(_L("Generating RSA keys"));
    1.88 +			
    1.89 +			// create an RSA key pair
    1.90 +			keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
    1.91 +			keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
    1.92 +
    1.93 +			// create a key pair generator implementation interface
    1.94 +			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl, 
    1.95 +																			KRSAKeyPairGeneratorUid, 
    1.96 +																			keyParams));
    1.97 +											
    1.98 +			CleanupStack::PushL(keypairImpl);
    1.99 +
   1.100 +			// Create a Key Pair	
   1.101 +			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
   1.102 +			
   1.103 +			CleanupStack::PushL(keyPair);
   1.104 +			
   1.105 +			//*****************************************************
   1.106 +			
   1.107 +			CAsymmetricCipher* asymmetricCipherImpl = NULL;	
   1.108 +				
   1.109 +			TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL(asymmetricCipherImpl,
   1.110 +																	algorithmUid,
   1.111 +																	keyPair->PrivateKey(),
   1.112 +																	cryptoMode,
   1.113 +																	paddingMode,
   1.114 +																	NULL));
   1.115 +			
   1.116 +			if(asymmetricCipherImpl && (err == KErrNone))
   1.117 +				{
   1.118 +				
   1.119 +				CleanupStack::PushL(asymmetricCipherImpl);
   1.120 +				
   1.121 +				INFO_PRINTF1(_L("** Successfully Loaded Asymmetric Cipher Object **"));
   1.122 +				
   1.123 +				//Define a pointer of type TCharacteristics in order to store the asymmetric cipher
   1.124 +				//encryption object's characterisctics	
   1.125 +				const TCharacteristics* chars(NULL);
   1.126 +							
   1.127 +				//Retrieve the characteristics for the asymmetric cipher implementation object
   1.128 +				TRAP_LOG(err, asymmetricCipherImpl->GetCharacteristicsL(chars));
   1.129 +					
   1.130 +				//Static cast the characteristics to type TAsymmetricCipherCharacteristics
   1.131 +				const TAsymmetricCipherCharacteristics* asymmetricChars = static_cast<const TAsymmetricCipherCharacteristics*>(chars);
   1.132 +				
   1.133 +				//Retrieve all the Common characteristics that are required for all test cases
   1.134 +				TVariantPtrC exInterfaceUid;
   1.135 +				TVariantPtrC exAlgorithmUid;
   1.136 +				TVariantPtrC exImplementationUid;
   1.137 +				TVariantPtrC exCreatorName;
   1.138 +				TBool exFIPSApproved;
   1.139 +				TBool exHardwareSupported;
   1.140 +				TInt exMaxConcurrencySupported;
   1.141 +				TVariantPtrC exAlgorithmName;
   1.142 +				TInt exLatency;
   1.143 +				TInt exThroughput;
   1.144 +				
   1.145 +				//Retrieve all the Asymmetric characteristics that are required for all test cases
   1.146 +				TInt exAsymmetricMaxKeyLength;
   1.147 +				TVariantPtrC exAsymmetricPaddingModes;
   1.148 +				TInt exAsymmetricPaddingModeNum;
   1.149 +				TInt exAsymmetricKeySupportMode;
   1.150 +				
   1.151 +				if(!GetStringFromConfig(ConfigSection(),KConfigExInterfaceUid,exInterfaceUid) ||
   1.152 +					!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmUid,exAlgorithmUid) ||
   1.153 +					!GetStringFromConfig(ConfigSection(),KConfigExImplementationUid,exImplementationUid) ||
   1.154 +					!GetStringFromConfig(ConfigSection(),KConfigExCreatorName,exCreatorName) ||
   1.155 +					!GetBoolFromConfig(ConfigSection(),KConfigExFIPSApproved,exFIPSApproved) ||
   1.156 +					!GetBoolFromConfig(ConfigSection(),KConfigExHardwareSupport,exHardwareSupported) ||
   1.157 +					!GetIntFromConfig(ConfigSection(),KConfigExMaxConcurrency,exMaxConcurrencySupported) ||
   1.158 +					!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmName,exAlgorithmName) ||
   1.159 +					!GetIntFromConfig(ConfigSection(),KConfigExLatency,exLatency) ||
   1.160 +					!GetIntFromConfig(ConfigSection(),KConfigExThroughput,exThroughput) ||
   1.161 +					!GetIntFromConfig(ConfigSection(),KConfigExMaxKeyLength,exAsymmetricMaxKeyLength) ||
   1.162 +					!GetStringFromConfig(ConfigSection(),KConfigExPaddingModes,exAsymmetricPaddingModes) ||
   1.163 +					!GetIntFromConfig(ConfigSection(),KConfigExPaddingModeNum,exAsymmetricPaddingModeNum) ||
   1.164 +					!GetIntFromConfig(ConfigSection(),KConfigExKeySupportMode,exAsymmetricKeySupportMode))
   1.165 +					{
   1.166 +					ERR_PRINTF1(_L("** .INI Error: Expected Asymmetric Cipher/Common Chracteristic Arguments Not Located **"));
   1.167 +					SetTestStepResult(EFail);
   1.168 +					}
   1.169 +				else
   1.170 +					{
   1.171 +					INFO_PRINTF1(_L("** Checking Asymmetric Cipher/Common Characteristics.... **"));
   1.172 +					
   1.173 +					CPluginCharsChecker* pluginCheck = CPluginCharsChecker::NewLC();
   1.174 +					
   1.175 +					//Retrieve the Common Characteristics from TAsymmetricCipherCharacteristics 
   1.176 +					const TCommonCharacteristics* asymCommonChars = &asymmetricChars->cmn;
   1.177 +					
   1.178 +					TPtrC errorMessage;
   1.179 +					
   1.180 +					//Perform Asymmetric Cipher/Common Characteristic Checks
   1.181 +					if(pluginCheck->checkCommonCharacteristics(asymCommonChars,
   1.182 +																exInterfaceUid,
   1.183 +																exAlgorithmUid,
   1.184 +																exImplementationUid,
   1.185 +																exCreatorName,
   1.186 +																exFIPSApproved,
   1.187 +																exHardwareSupported,
   1.188 +																exMaxConcurrencySupported,
   1.189 +																exAlgorithmName,
   1.190 +																exLatency,
   1.191 +																exThroughput,
   1.192 +																errorMessage) &&
   1.193 +						pluginCheck->checkAsymmetricCharacteristics(asymmetricChars,
   1.194 +																exAsymmetricMaxKeyLength,
   1.195 +																exAsymmetricPaddingModes,
   1.196 +																exAsymmetricPaddingModeNum,
   1.197 +																exAsymmetricKeySupportMode,
   1.198 +																errorMessage))
   1.199 +						{
   1.200 +						INFO_PRINTF1(_L("Asymmetric Cipher/Common characteristics successfully match expected values..."));
   1.201 +						
   1.202 +						if(asymmetricChars->IsPaddingModeSupported(paddingMode) && !asymmetricChars->IsPaddingModeSupported(invalidPaddingMode))
   1.203 +							{
   1.204 +							INFO_PRINTF1(_L("Successful Padding Mode Supported Tests...."));
   1.205 +							
   1.206 +							INFO_PRINTF1(_L("** PASS: Asymmetric Cipher/Common Characteristics Tests Successful **"));
   1.207 +
   1.208 +							SetTestStepResult(EPass);
   1.209 +							}
   1.210 +						else
   1.211 +							{
   1.212 +							ERR_PRINTF1(_L("** FAIL: Unexpected 'Padding' Mode Supported Results"));	
   1.213 +							}	
   1.214 +						}
   1.215 +					else
   1.216 +						{
   1.217 +						ERR_PRINTF2(_L("** FAIL: Characteristic Mismatch - %S **"),&errorMessage);		
   1.218 +						}
   1.219 +						
   1.220 +					CleanupStack::PopAndDestroy(pluginCheck);
   1.221 +					}
   1.222 +					
   1.223 +				CleanupStack::PopAndDestroy(asymmetricCipherImpl);
   1.224 +				}				
   1.225 +			else
   1.226 +				{
   1.227 +				ERR_PRINTF1(_L("** Error: Loading Asymmetric Cipher Object **"));
   1.228 +				}
   1.229 +				
   1.230 +			CleanupStack::PopAndDestroy(3,keyParams);
   1.231 +			}
   1.232 +			
   1.233 +		}
   1.234 +		
   1.235 +	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   1.236 +	return TestStepResult();
   1.237 +	}
   1.238 +
   1.239 +TVerdict CPluginCharsAsymmetricCipherStep::doTestStepPostambleL()
   1.240 +	{
   1.241 +	return TestStepResult();
   1.242 +	}