os/security/crypto/weakcryptospi/test/tcryptospi/src/plugincharsasymmetriccipherstep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2010 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 "plugincharsasymmetriccipherstep.h"
    25 #include "plugincharschecker.h"
    26 
    27 #include <cryptospi/cryptoasymmetriccipherapi.h>
    28 #include <cryptospi/cryptokeypairgeneratorapi.h>
    29 #include <cryptospi/keypair.h>
    30 
    31 using namespace CryptoSpi;
    32 
    33 CPluginCharsAsymmetricCipherStep::~CPluginCharsAsymmetricCipherStep()
    34 	{
    35 	}
    36 
    37 CPluginCharsAsymmetricCipherStep::CPluginCharsAsymmetricCipherStep()
    38 	{
    39 	SetTestStepName(KPluginCharsAsymmetricCipherStep);
    40 	}
    41 
    42 TVerdict CPluginCharsAsymmetricCipherStep::doTestStepPreambleL()
    43 	{
    44 	SetTestStepResult(EPass);
    45 	return TestStepResult();
    46 	}
    47 				
    48 TVerdict CPluginCharsAsymmetricCipherStep::doTestStepL()
    49 	{
    50 	
    51 	INFO_PRINTF1(_L("Plugin Characteristics - Asymmetric Cipher Chracteristics"));
    52 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
    53 		
    54 	if (TestStepResult()==EPass)
    55 		{
    56 		
    57 		//Assume faliure, unless all is successful
    58 		SetTestStepResult(EFail);
    59 		
    60 		TVariantPtrC algorithmUid;
    61 		TVariantPtrC cryptoMode;
    62 		TVariantPtrC paddingMode;
    63 		TVariantPtrC invalidPaddingMode;
    64 		
    65 		//Each of the individual parameters required to create the Asymmetric Cipher object
    66 		//are read in from the specified INI configuration file					
    67 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
    68 			!GetStringFromConfig(ConfigSection(),KConfigCryptoMode,cryptoMode) ||
    69 			!GetStringFromConfig(ConfigSection(),KConfigPaddingMode,paddingMode) ||
    70 			!GetStringFromConfig(ConfigSection(),KConfigInvalidPaddingMode,invalidPaddingMode))
    71 			{
    72 			ERR_PRINTF1(_L("** .INI Error: Asymmetric Cipher Arguments Not Located **"));
    73 			SetTestStepResult(EFail);
    74 			}
    75 		else
    76 			{
    77 			CCryptoParams* keyParams = CCryptoParams::NewLC(); 
    78 			
    79 			//****************************************************
    80 			//Create Key Pair and Key Pair Generator Objects
    81    			CKeyPair* keyPair = NULL;
    82 			CKeyPairGenerator * keypairImpl = NULL;
    83 			
    84 			INFO_PRINTF1(_L("Generating RSA keys"));
    85 			
    86 			// create an RSA key pair
    87 			keyParams->AddL(KKeyExponent, KRsaKeyParameterEUid);
    88 			keyParams->AddL(KRsaPrivateKeyStandard, KRsaKeyTypeUid);
    89 
    90 			// create a key pair generator implementation interface
    91 			TRAPD_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl, 
    92 																			KRSAKeyPairGeneratorUid, 
    93 																			keyParams));
    94 											
    95 			CleanupStack::PushL(keypairImpl);
    96 
    97 			// Create a Key Pair	
    98 			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(1024, *keyParams, keyPair));
    99 			
   100 			CleanupStack::PushL(keyPair);
   101 			
   102 			//*****************************************************
   103 			
   104 			CAsymmetricCipher* asymmetricCipherImpl = NULL;	
   105 				
   106 			TRAP(err,CAsymmetricCipherFactory::CreateAsymmetricCipherL(asymmetricCipherImpl,
   107 																	algorithmUid,
   108 																	keyPair->PrivateKey(),
   109 																	cryptoMode,
   110 																	paddingMode,
   111 																	NULL));
   112 			
   113 			if(asymmetricCipherImpl && (err == KErrNone))
   114 				{
   115 				
   116 				CleanupStack::PushL(asymmetricCipherImpl);
   117 				
   118 				INFO_PRINTF1(_L("** Successfully Loaded Asymmetric Cipher Object **"));
   119 				
   120 				//Define a pointer of type TCharacteristics in order to store the asymmetric cipher
   121 				//encryption object's characterisctics	
   122 				const TCharacteristics* chars(NULL);
   123 							
   124 				//Retrieve the characteristics for the asymmetric cipher implementation object
   125 				TRAP_LOG(err, asymmetricCipherImpl->GetCharacteristicsL(chars));
   126 					
   127 				//Static cast the characteristics to type TAsymmetricCipherCharacteristics
   128 				const TAsymmetricCipherCharacteristics* asymmetricChars = static_cast<const TAsymmetricCipherCharacteristics*>(chars);
   129 				
   130 				//Retrieve all the Common characteristics that are required for all test cases
   131 				TVariantPtrC exInterfaceUid;
   132 				TVariantPtrC exAlgorithmUid;
   133 				TVariantPtrC exImplementationUid;
   134 				TVariantPtrC exCreatorName;
   135 				TBool exFIPSApproved;
   136 				TBool exHardwareSupported;
   137 				TInt exMaxConcurrencySupported;
   138 				TVariantPtrC exAlgorithmName;
   139 				TInt exLatency;
   140 				TInt exThroughput;
   141 				
   142 				//Retrieve all the Asymmetric characteristics that are required for all test cases
   143 				TInt exAsymmetricMaxKeyLength;
   144 				TVariantPtrC exAsymmetricPaddingModes;
   145 				TInt exAsymmetricPaddingModeNum;
   146 				TInt exAsymmetricKeySupportMode;
   147 				
   148 				if(!GetStringFromConfig(ConfigSection(),KConfigExInterfaceUid,exInterfaceUid) ||
   149 					!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmUid,exAlgorithmUid) ||
   150 					!GetStringFromConfig(ConfigSection(),KConfigExImplementationUid,exImplementationUid) ||
   151 					!GetStringFromConfig(ConfigSection(),KConfigExCreatorName,exCreatorName) ||
   152 					!GetBoolFromConfig(ConfigSection(),KConfigExFIPSApproved,exFIPSApproved) ||
   153 					!GetBoolFromConfig(ConfigSection(),KConfigExHardwareSupport,exHardwareSupported) ||
   154 					!GetIntFromConfig(ConfigSection(),KConfigExMaxConcurrency,exMaxConcurrencySupported) ||
   155 					!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmName,exAlgorithmName) ||
   156 					!GetIntFromConfig(ConfigSection(),KConfigExLatency,exLatency) ||
   157 					!GetIntFromConfig(ConfigSection(),KConfigExThroughput,exThroughput) ||
   158 					!GetIntFromConfig(ConfigSection(),KConfigExMaxKeyLength,exAsymmetricMaxKeyLength) ||
   159 					!GetStringFromConfig(ConfigSection(),KConfigExPaddingModes,exAsymmetricPaddingModes) ||
   160 					!GetIntFromConfig(ConfigSection(),KConfigExPaddingModeNum,exAsymmetricPaddingModeNum) ||
   161 					!GetIntFromConfig(ConfigSection(),KConfigExKeySupportMode,exAsymmetricKeySupportMode))
   162 					{
   163 					ERR_PRINTF1(_L("** .INI Error: Expected Asymmetric Cipher/Common Chracteristic Arguments Not Located **"));
   164 					SetTestStepResult(EFail);
   165 					}
   166 				else
   167 					{
   168 					INFO_PRINTF1(_L("** Checking Asymmetric Cipher/Common Characteristics.... **"));
   169 					
   170 					CPluginCharsChecker* pluginCheck = CPluginCharsChecker::NewLC();
   171 					
   172 					//Retrieve the Common Characteristics from TAsymmetricCipherCharacteristics 
   173 					const TCommonCharacteristics* asymCommonChars = &asymmetricChars->cmn;
   174 					
   175 					TPtrC errorMessage;
   176 					
   177 					//Perform Asymmetric Cipher/Common Characteristic Checks
   178 					if(pluginCheck->checkCommonCharacteristics(asymCommonChars,
   179 																exInterfaceUid,
   180 																exAlgorithmUid,
   181 																exImplementationUid,
   182 																exCreatorName,
   183 																exFIPSApproved,
   184 																exHardwareSupported,
   185 																exMaxConcurrencySupported,
   186 																exAlgorithmName,
   187 																exLatency,
   188 																exThroughput,
   189 																errorMessage) &&
   190 						pluginCheck->checkAsymmetricCharacteristics(asymmetricChars,
   191 																exAsymmetricMaxKeyLength,
   192 																exAsymmetricPaddingModes,
   193 																exAsymmetricPaddingModeNum,
   194 																exAsymmetricKeySupportMode,
   195 																errorMessage))
   196 						{
   197 						INFO_PRINTF1(_L("Asymmetric Cipher/Common characteristics successfully match expected values..."));
   198 						
   199 						if(asymmetricChars->IsPaddingModeSupported(paddingMode) && !asymmetricChars->IsPaddingModeSupported(invalidPaddingMode))
   200 							{
   201 							INFO_PRINTF1(_L("Successful Padding Mode Supported Tests...."));
   202 							
   203 							INFO_PRINTF1(_L("** PASS: Asymmetric Cipher/Common Characteristics Tests Successful **"));
   204 
   205 							SetTestStepResult(EPass);
   206 							}
   207 						else
   208 							{
   209 							ERR_PRINTF1(_L("** FAIL: Unexpected 'Padding' Mode Supported Results"));	
   210 							}	
   211 						}
   212 					else
   213 						{
   214 						ERR_PRINTF2(_L("** FAIL: Characteristic Mismatch - %S **"),&errorMessage);		
   215 						}
   216 						
   217 					CleanupStack::PopAndDestroy(pluginCheck);
   218 					}
   219 					
   220 				CleanupStack::PopAndDestroy(asymmetricCipherImpl);
   221 				}				
   222 			else
   223 				{
   224 				ERR_PRINTF1(_L("** Error: Loading Asymmetric Cipher Object **"));
   225 				}
   226 				
   227 			CleanupStack::PopAndDestroy(3,keyParams);
   228 			}
   229 			
   230 		}
   231 		
   232 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   233 	return TestStepResult();
   234 	}
   235 
   236 TVerdict CPluginCharsAsymmetricCipherStep::doTestStepPostambleL()
   237 	{
   238 	return TestStepResult();
   239 	}