os/security/crypto/weakcryptospi/test/tcryptospi/src/plugincharskeyagreestep.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 "plugincharskeyagreestep.h"
    25 #include "plugincharschecker.h"
    26 
    27 #include <cryptospi/keypair.h>
    28 #include <cryptospi/cryptokeypairgeneratorapi.h>
    29 #include <cryptospi/cryptokeyagreementapi.h>
    30 
    31 using namespace CryptoSpi;
    32 
    33 CPluginCharsKeyAgreeStep::~CPluginCharsKeyAgreeStep()
    34 	{
    35 	}
    36 
    37 CPluginCharsKeyAgreeStep::CPluginCharsKeyAgreeStep()
    38 	{
    39 	SetTestStepName(KPluginCharsKeyAgreeStep);
    40 	}
    41 
    42 TVerdict CPluginCharsKeyAgreeStep::doTestStepPreambleL()
    43 	{
    44 	SetTestStepResult(EPass);
    45 	return TestStepResult();
    46 	}
    47 				
    48 TVerdict CPluginCharsKeyAgreeStep::doTestStepL()
    49 	{
    50 	
    51 	INFO_PRINTF1(_L("Plugin Characteristics - Key Agreement 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 		
    62 		//Each of the individual parameters required to create the Key Agreement object
    63 		//are read in from the specified INI configuration file					
    64 		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid))
    65 			{
    66 			ERR_PRINTF1(_L("** .INI Error: Key Agreement Object Arguments Not Located **"));
    67 			SetTestStepResult(EFail);
    68 			}
    69 		else
    70 			{
    71 			INFO_PRINTF1(_L("Creating Primes and Base Integers...")); 
    72 	 		 
    73 			RInteger DH_N = RInteger::NewPrimeL(64);
    74 			CleanupClosePushL(DH_N);
    75 			
    76 			RInteger DH_N_MinusOne = RInteger::NewL(DH_N);
    77 			CleanupClosePushL(DH_N_MinusOne);
    78 			DH_N_MinusOne-=1;
    79 			
    80 			RInteger DH_G = RInteger::NewRandomL(TInteger::Two(), DH_N_MinusOne);	
    81 			CleanupClosePushL(DH_G);
    82 			
    83 			CCryptoParams* keyParams = CCryptoParams::NewLC(); 
    84 						
    85 			TRAPD_LOG(err,keyParams->AddL(DH_N, KDhKeyParameterNUid));
    86 			TRAP_LOG(err,keyParams->AddL(DH_G, KDhKeyParameterGUid));
    87 			
    88 			//****************************************************
    89 			//Create Key Pair and Key Pair Generator Objects
    90    			CKeyPair* keyPair = NULL;
    91 			CKeyPairGenerator * keypairImpl = NULL;
    92 			
    93 			INFO_PRINTF1(_L("Generating Key Pair..."));
    94 			
    95 			// create a key pair generator implementation interface
    96 			TRAP_LOG(err,CKeyPairGeneratorFactory::CreateKeyPairGeneratorL(keypairImpl, 
    97 																			KDHKeyPairGeneratorUid, 
    98 																			keyParams));								
    99 			CleanupStack::PushL(keypairImpl);
   100 
   101 			// Create a Key Pair	
   102 			TRAP_LOG(err,keypairImpl->GenerateKeyPairL(NULL, *keyParams, keyPair));
   103 			
   104 			CleanupStack::PushL(keyPair);
   105 			
   106 			//*****************************************************
   107 			
   108 			CKeyAgreement* keyAgreementImpl = NULL;	
   109 				
   110 			TRAP(err,CKeyAgreementFactory::CreateKeyAgreementL(keyAgreementImpl,
   111 																algorithmUid,
   112 																keyPair->PrivateKey(),
   113 																keyParams));
   114 			
   115 			if(keyAgreementImpl && (err == KErrNone))
   116 				{
   117 				
   118 				CleanupStack::PushL(keyAgreementImpl);
   119 				
   120 				INFO_PRINTF1(_L("** Successfully Loaded Key Agreement Object **"));
   121 				
   122 				//Define a pointer of type TCharacteristics in order to store the key agreement
   123 				//object's characterisctics	
   124 				const TCharacteristics* chars(NULL);
   125 							
   126 				//Retrieve the characteristics for the key agreement signer implementation object
   127 				TRAP_LOG(err, keyAgreementImpl->GetCharacteristicsL(chars));
   128 				
   129 				//Static cast the characteristics to type TKeyAgreementCharacteristics
   130 				const TKeyAgreementCharacteristics* keyagreeChars = static_cast<const TKeyAgreementCharacteristics*>(chars);
   131 
   132 				//Retrieve all the Common characteristics that are required for the test
   133 				TVariantPtrC exInterfaceUid;
   134 				TVariantPtrC exAlgorithmUid;
   135 				TVariantPtrC exImplementationUid;
   136 				TVariantPtrC exCreatorName;
   137 				TBool exFIPSApproved;
   138 				TBool exHardwareSupported;
   139 				TInt exMaxConcurrencySupported;
   140 				TVariantPtrC exAlgorithmName;
   141 				TInt exLatency;
   142 				TInt exThroughput;
   143 				
   144 				if(!GetStringFromConfig(ConfigSection(),KConfigExInterfaceUid,exInterfaceUid) ||
   145 					!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmUid,exAlgorithmUid) ||
   146 					!GetStringFromConfig(ConfigSection(),KConfigExImplementationUid,exImplementationUid) ||
   147 					!GetStringFromConfig(ConfigSection(),KConfigExCreatorName,exCreatorName) ||
   148 					!GetBoolFromConfig(ConfigSection(),KConfigExFIPSApproved,exFIPSApproved) ||
   149 					!GetBoolFromConfig(ConfigSection(),KConfigExHardwareSupport,exHardwareSupported) ||
   150 					!GetIntFromConfig(ConfigSection(),KConfigExMaxConcurrency,exMaxConcurrencySupported) ||
   151 					!GetStringFromConfig(ConfigSection(),KConfigExAlgorithmName,exAlgorithmName) ||
   152 					!GetIntFromConfig(ConfigSection(),KConfigExLatency,exLatency) ||
   153 					!GetIntFromConfig(ConfigSection(),KConfigExThroughput,exThroughput))
   154 					{
   155 					ERR_PRINTF1(_L("** .INI Error: Expected Key Agreement/Common Characteristics Not Located **"));
   156 					SetTestStepResult(EFail);
   157 					}
   158 				else
   159 					{
   160 			
   161 					INFO_PRINTF1(_L("** Checking Key Agreement/Common Characteristics.... **"));
   162 					
   163 					CPluginCharsChecker* pluginCheck = CPluginCharsChecker::NewLC();
   164 					
   165 					//Retrieve the Common Characteristics from TKeyAgreementCharacteristics 
   166 					const TCommonCharacteristics* keyagreeCommonChars = &keyagreeChars->cmn;
   167 					
   168 					TPtrC errorMessage;
   169 					
   170 					//Perform Key Agreement/Common Characteristic Checks
   171 					if(pluginCheck->checkCommonCharacteristics(keyagreeCommonChars,
   172 																exInterfaceUid,
   173 																exAlgorithmUid,
   174 																exImplementationUid,
   175 																exCreatorName,
   176 																exFIPSApproved,
   177 																exHardwareSupported,
   178 																exMaxConcurrencySupported,
   179 																exAlgorithmName,
   180 																exLatency,
   181 																exThroughput,
   182 																errorMessage))
   183 						{
   184 						INFO_PRINTF1(_L("** PASS : Key Agreement/Common characteristics successfully match expected values **"));
   185 						SetTestStepResult(EPass);	
   186 						}
   187 					else
   188 						{
   189 						ERR_PRINTF2(_L("** FAIL: Characteristic Mismatch - %S **"),&errorMessage);
   190 						}
   191 					
   192 					CleanupStack::PopAndDestroy(pluginCheck);
   193 					}
   194 				
   195 				CleanupStack::PopAndDestroy(keyAgreementImpl);
   196 				}
   197 			else
   198 				{
   199 				ERR_PRINTF2(_L("*** FAIL: Failed to Create Key Agreement Object - %d ***"), err);	
   200 				}
   201 				
   202 			CleanupStack::PopAndDestroy(6,&DH_N);
   203 			}
   204 		
   205 		}
   206 		
   207 	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
   208 	return TestStepResult();
   209 	}
   210 
   211 TVerdict CPluginCharsKeyAgreeStep::doTestStepPostambleL()
   212 	{
   213 	return TestStepResult();
   214 	}