os/security/crypto/weakcryptospi/test/tcryptospi/src/hmacsetoperationmodecheckingstep.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Example CTestStep derived implementation
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
/**
sl@0
    21
 @file
sl@0
    22
 @internalTechnology
sl@0
    23
*/
sl@0
    24
#include "hmacsetoperationmodecheckingstep.h"
sl@0
    25
sl@0
    26
#include <cryptospi/cryptohashapi.h>
sl@0
    27
#include <cryptospi/keys.h>
sl@0
    28
#include <cryptospi/plugincharacteristics.h>
sl@0
    29
sl@0
    30
using namespace CryptoSpi;
sl@0
    31
sl@0
    32
CHmacSetOperationModeCheckingStep::~CHmacSetOperationModeCheckingStep()
sl@0
    33
	{
sl@0
    34
	}
sl@0
    35
sl@0
    36
sl@0
    37
CHmacSetOperationModeCheckingStep::CHmacSetOperationModeCheckingStep()
sl@0
    38
	{
sl@0
    39
	SetTestStepName(KHmacSetOperationModeCheckingStep);
sl@0
    40
	}
sl@0
    41
sl@0
    42
sl@0
    43
TVerdict CHmacSetOperationModeCheckingStep::doTestStepPreambleL()
sl@0
    44
	{
sl@0
    45
	SetTestStepResult(EPass);
sl@0
    46
	return TestStepResult();
sl@0
    47
	}
sl@0
    48
sl@0
    49
sl@0
    50
TVerdict CHmacSetOperationModeCheckingStep::doTestStepL()
sl@0
    51
	{
sl@0
    52
	if (TestStepResult()==EPass)
sl@0
    53
		{
sl@0
    54
		
sl@0
    55
		//Assume faliure, unless all is successful
sl@0
    56
		SetTestStepResult(EFail);
sl@0
    57
		
sl@0
    58
		INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking ***"));
sl@0
    59
		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
sl@0
    60
		
sl@0
    61
		TVariantPtrC algorithmUid;
sl@0
    62
		TVariantPtrC operationModeUid;
sl@0
    63
		TVariantPtrC secondOperationModeUid;
sl@0
    64
		TPtrC sourcePath;
sl@0
    65
		TPtrC expectedHash;
sl@0
    66
		TPtrC expectedHmac;
sl@0
    67
		TPtrC encryptKey;
sl@0
    68
		TVariantPtrC keyType;
sl@0
    69
		
sl@0
    70
		//Extract the Test Case ID parameter from the specified INI file
sl@0
    71
		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
sl@0
    72
			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
sl@0
    73
			!GetStringFromConfig(ConfigSection(),KConfigSecondOperationMode,secondOperationModeUid) ||
sl@0
    74
			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
sl@0
    75
			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
sl@0
    76
			!GetStringFromConfig(ConfigSection(),KConfigExSecondHashHmacValue,expectedHmac) ||
sl@0
    77
			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
sl@0
    78
			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
sl@0
    79
			{
sl@0
    80
			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
sl@0
    81
			SetTestStepResult(EFail);
sl@0
    82
			}
sl@0
    83
		else
sl@0
    84
			{	
sl@0
    85
			RFs fsSession;
sl@0
    86
				
sl@0
    87
			//Create a connection to the file server	
sl@0
    88
			User::LeaveIfError(fsSession.Connect());
sl@0
    89
						
sl@0
    90
			RFile sourceFile;
sl@0
    91
	    	CleanupClosePushL(sourceFile);
sl@0
    92
	    			
sl@0
    93
	    	//Open the specified source file		
sl@0
    94
	    	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
sl@0
    95
	    					
sl@0
    96
			TInt sourceLength = 0;
sl@0
    97
			User::LeaveIfError(sourceFile.Size(sourceLength));
sl@0
    98
						
sl@0
    99
			//Create a heap based descriptor to store the data
sl@0
   100
			HBufC8* sourceData = HBufC8::NewL(sourceLength);						
sl@0
   101
			CleanupStack::PushL(sourceData);
sl@0
   102
			TPtr8 sourcePtr = sourceData->Des();
sl@0
   103
						
sl@0
   104
			sourceFile.Read(sourcePtr);
sl@0
   105
							
sl@0
   106
			if(sourcePtr.Length() != sourceLength)
sl@0
   107
				{
sl@0
   108
				ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
sl@0
   109
				SetTestStepResult(EFail);	
sl@0
   110
				}
sl@0
   111
			else
sl@0
   112
				{
sl@0
   113
				//Create a pointer for the Hash + Key (Hmac) Implementation Object
sl@0
   114
				CHash* hashHmacImpl = NULL;
sl@0
   115
				
sl@0
   116
				//Convert encryption key to an 8 Bit Descriptor
sl@0
   117
				HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
sl@0
   118
				TPtr8 keyStrPtr = keyStr->Des();
sl@0
   119
				
sl@0
   120
				keyStrPtr.Copy(encryptKey);
sl@0
   121
				
sl@0
   122
				//Create an new CryptoParams object to encapsulate the invalid key type and key string
sl@0
   123
				CCryptoParams* keyParams = CCryptoParams::NewL();
sl@0
   124
				CleanupStack::PushL(keyParams);
sl@0
   125
				keyParams->AddL(*keyStr,keyType);
sl@0
   126
				
sl@0
   127
				//Create a valid CKey Object
sl@0
   128
				TKeyProperty keyProperty;
sl@0
   129
				CKey* key = CKey::NewL(keyProperty,*keyParams);
sl@0
   130
				CleanupStack::PushL(key);
sl@0
   131
				
sl@0
   132
				//Construct an initial hash object with NO key, Catching any possible Leaves			
sl@0
   133
				TRAPD(err,CHashFactory::CreateHashL(
sl@0
   134
									hashHmacImpl,
sl@0
   135
									algorithmUid,
sl@0
   136
									operationModeUid,
sl@0
   137
									NULL,
sl@0
   138
									NULL));
sl@0
   139
									
sl@0
   140
										
sl@0
   141
				if(hashHmacImpl && (err == KErrNone))
sl@0
   142
					{
sl@0
   143
					
sl@0
   144
					//Push the Implementation Object onto the Cleanup Stack
sl@0
   145
					CleanupStack::PushL(hashHmacImpl);
sl@0
   146
					
sl@0
   147
					//Create a NULL TCharacteristics pointer
sl@0
   148
					const TCharacteristics* charsPtrA(NULL);
sl@0
   149
					
sl@0
   150
					//Retrieve the characteristics for the hash implementation object
sl@0
   151
					TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrA));
sl@0
   152
					
sl@0
   153
					//Static cast the characteristics to type THashCharacteristics
sl@0
   154
					const THashCharacteristics* hashCharsPtrA = static_cast<const THashCharacteristics*>(charsPtrA);
sl@0
   155
					
sl@0
   156
					//The hash output size is returned in Bits, divide by 8 to get the Byte size
sl@0
   157
					TInt hashSize = hashCharsPtrA->iOutputSize/8;
sl@0
   158
					
sl@0
   159
					//Retrieve the final 8bit hash value and convert to 16bit												
sl@0
   160
					HBufC* hashDataA = HBufC::NewLC(hashSize);
sl@0
   161
					TPtr hashPtrA = hashDataA->Des();
sl@0
   162
					
sl@0
   163
					hashPtrA.Copy(hashHmacImpl->Hash(*sourceData));
sl@0
   164
					
sl@0
   165
					//Take the 16bit descriptor and convert the string to hexadecimal
sl@0
   166
					TVariantPtrC convertHash;
sl@0
   167
					convertHash.Set(hashPtrA);
sl@0
   168
					
sl@0
   169
					HBufC* resultA = convertHash.HexStringLC();							
sl@0
   170
						
sl@0
   171
					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultA);
sl@0
   172
					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
sl@0
   173
					
sl@0
   174
					if(*resultA == expectedHash)
sl@0
   175
						{
sl@0
   176
						INFO_PRINTF1(_L("*** PRIMARY HASH VALID - STAGE 1 PASS ***"));
sl@0
   177
						
sl@0
   178
						//Set the valid key within the Hmac Implementation Object
sl@0
   179
						TRAP(err,hashHmacImpl->SetKeyL(*key));
sl@0
   180
						
sl@0
   181
						if(err!=KErrNone)
sl@0
   182
							{
sl@0
   183
							ERR_PRINTF2(_L("*** ERROR %d: Setting Key ***"),err);
sl@0
   184
							User::Leave(err);	
sl@0
   185
							}
sl@0
   186
						else
sl@0
   187
							{
sl@0
   188
							INFO_PRINTF1(_L("*** HMAC KEY SET ***"));	
sl@0
   189
							}
sl@0
   190
										
sl@0
   191
						//Set the Operation Mode of the Hmac Implementation Object
sl@0
   192
						hashHmacImpl->SetOperationModeL(secondOperationModeUid);
sl@0
   193
						
sl@0
   194
						if(err!=KErrNone)
sl@0
   195
							{
sl@0
   196
							ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&secondOperationModeUid);	
sl@0
   197
							User::Leave(err);
sl@0
   198
							}
sl@0
   199
						else
sl@0
   200
							{
sl@0
   201
							INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&secondOperationModeUid);	
sl@0
   202
							}
sl@0
   203
						
sl@0
   204
						//Create a NULL TCharacteristics pointer
sl@0
   205
						const TCharacteristics* charsPtrB(NULL);
sl@0
   206
					
sl@0
   207
						//Retrieve the characteristics for the hash implementation object
sl@0
   208
						TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrB));
sl@0
   209
					
sl@0
   210
						//Static cast the characteristics to type THashCharacteristics
sl@0
   211
						const THashCharacteristics* hashCharsPtrB = static_cast<const THashCharacteristics*>(charsPtrB);
sl@0
   212
						
sl@0
   213
						//The hash output size is returned in Bits, divide by 8 to get the Byte size
sl@0
   214
						hashSize = hashCharsPtrB->iOutputSize/8;
sl@0
   215
						
sl@0
   216
						//Retrieve the final 8bit hash value and convert to 16bit												
sl@0
   217
						HBufC* hashDataB = HBufC::NewLC(hashSize);
sl@0
   218
						TPtr hashPtrB = hashDataB->Des();
sl@0
   219
						
sl@0
   220
						hashPtrB.Copy(hashHmacImpl->Hash(*sourceData));
sl@0
   221
						
sl@0
   222
						//Take the 16bit descriptor and convert the string to hexadecimal
sl@0
   223
						convertHash.Set(hashPtrB);
sl@0
   224
						HBufC* resultB = convertHash.HexStringLC();						
sl@0
   225
						
sl@0
   226
						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultB);
sl@0
   227
						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHmac);
sl@0
   228
						
sl@0
   229
						if(*resultB == expectedHmac)
sl@0
   230
							{
sl@0
   231
							INFO_PRINTF1(_L("*** SECONDARY HASH VALID - STAGE 2 PASS ***"));
sl@0
   232
							
sl@0
   233
							//Set the Operation Mode of the Hmac Implementation Object
sl@0
   234
							TRAP(err,hashHmacImpl->SetOperationModeL(operationModeUid));
sl@0
   235
							
sl@0
   236
							if(err!=KErrNone)
sl@0
   237
								{
sl@0
   238
								ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&operationModeUid);	
sl@0
   239
								User::Leave(err);
sl@0
   240
								}
sl@0
   241
							else
sl@0
   242
								{
sl@0
   243
								INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&operationModeUid);	
sl@0
   244
								}
sl@0
   245
								
sl@0
   246
							//Create a NULL TCharacteristics pointer
sl@0
   247
							const TCharacteristics* charsPtrC(NULL);
sl@0
   248
						
sl@0
   249
							//Retrieve the characteristics for the hash implementation object
sl@0
   250
							TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrC));
sl@0
   251
						
sl@0
   252
							//Static cast the characteristics to type THashCharacteristics
sl@0
   253
							const THashCharacteristics* hashCharsPtrC = static_cast<const THashCharacteristics*>(charsPtrC);
sl@0
   254
							
sl@0
   255
							//The hash output size is returned in Bits, divide by 8 to get the Byte size
sl@0
   256
							hashSize = hashCharsPtrC->iOutputSize/8;
sl@0
   257
							
sl@0
   258
							//Retrieve the final 8bit hash value and convert to 16bit												
sl@0
   259
							HBufC* hashDataC = HBufC::NewLC(hashSize);
sl@0
   260
							TPtr hashPtrC = hashDataC->Des();
sl@0
   261
							
sl@0
   262
							hashPtrC.Copy(hashHmacImpl->Hash(*sourceData));
sl@0
   263
							
sl@0
   264
							//Take the 16bit descriptor and convert the string to hexadecimal
sl@0
   265
							convertHash.Set(hashPtrC);
sl@0
   266
							HBufC* resultC = convertHash.HexStringLC();							
sl@0
   267
							
sl@0
   268
							INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultC);
sl@0
   269
							INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
sl@0
   270
									
sl@0
   271
							if(*resultC == expectedHash)
sl@0
   272
								{
sl@0
   273
								INFO_PRINTF1(_L("*** FINAL HASH VALID - STAGE 3 PASS ***"));
sl@0
   274
								INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking : PASS ***"));
sl@0
   275
								SetTestStepResult(EPass);
sl@0
   276
								}
sl@0
   277
							else
sl@0
   278
								{
sl@0
   279
								ERR_PRINTF1(_L("*** STAGE 3 FAIL: Hash and Expected Value Mismatch ***"));
sl@0
   280
								SetTestStepResult(EFail);	
sl@0
   281
								}
sl@0
   282
								
sl@0
   283
							CleanupStack::PopAndDestroy(resultC);
sl@0
   284
							CleanupStack::PopAndDestroy(hashDataC);
sl@0
   285
							}
sl@0
   286
						else
sl@0
   287
							{
sl@0
   288
							ERR_PRINTF1(_L("*** STAGE 2 FAIL: Hash and Expected Value Mismatch ***"));
sl@0
   289
							SetTestStepResult(EFail);	
sl@0
   290
							}
sl@0
   291
							
sl@0
   292
						CleanupStack::PopAndDestroy(resultB);
sl@0
   293
						CleanupStack::PopAndDestroy(hashDataB);	
sl@0
   294
						}
sl@0
   295
					else
sl@0
   296
						{
sl@0
   297
						ERR_PRINTF1(_L("*** STAGE 1 FAIL: Hash and Expected Value Match ***"));
sl@0
   298
						SetTestStepResult(EFail);	
sl@0
   299
						}
sl@0
   300
						
sl@0
   301
					CleanupStack::PopAndDestroy(resultA);
sl@0
   302
					CleanupStack::PopAndDestroy(hashDataA);
sl@0
   303
					CleanupStack::PopAndDestroy(hashHmacImpl);										
sl@0
   304
					}
sl@0
   305
				else if(err==KErrNotSupported)
sl@0
   306
					{
sl@0
   307
					if((((TUid)operationModeUid != KHashModeUid) && ((TUid)operationModeUid != KHmacModeUid)) ||
sl@0
   308
						((TUid)algorithmUid != KMd2Uid) && (TUid)algorithmUid != KMd5Uid && (TUid)algorithmUid != KSha1Uid && (TUid)algorithmUid != KMd4Uid && (TUid)algorithmUid != KSha224Uid && (TUid)algorithmUid != KSha256Uid && (TUid)algorithmUid != KSha384Uid && (TUid)algorithmUid != KSha512Uid)
sl@0
   309
						{
sl@0
   310
						ERR_PRINTF2(_L("*** Object Load Failure - Invalid Operation Mode : %d ***"), err);
sl@0
   311
						User::Leave(err);	
sl@0
   312
						}
sl@0
   313
					else
sl@0
   314
						{
sl@0
   315
						SetTestStepResult(EFail);	
sl@0
   316
						}
sl@0
   317
					}
sl@0
   318
				else
sl@0
   319
					{
sl@0
   320
					ERR_PRINTF2(_L("*** Hash/Hmac Facotry Object Load Failure : %d ***"), err);
sl@0
   321
					User::Leave(err);	
sl@0
   322
					}
sl@0
   323
					
sl@0
   324
				CleanupStack::PopAndDestroy(key);
sl@0
   325
				CleanupStack::PopAndDestroy(keyParams);
sl@0
   326
				CleanupStack::PopAndDestroy(keyStr);
sl@0
   327
				}
sl@0
   328
							
sl@0
   329
			CleanupStack::PopAndDestroy(sourceData);
sl@0
   330
								
sl@0
   331
			//Cleanup the Source RFile	
sl@0
   332
			CleanupStack::PopAndDestroy();	
sl@0
   333
						
sl@0
   334
			fsSession.Close();	
sl@0
   335
			}
sl@0
   336
			
sl@0
   337
		}
sl@0
   338
		else
sl@0
   339
		{
sl@0
   340
			SetTestStepResult(EFail);	
sl@0
   341
		}		
sl@0
   342
		
sl@0
   343
	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
sl@0
   344
	
sl@0
   345
	return TestStepResult();
sl@0
   346
	}
sl@0
   347
sl@0
   348
sl@0
   349
TVerdict CHmacSetOperationModeCheckingStep::doTestStepPostambleL()
sl@0
   350
	{
sl@0
   351
	return TestStepResult();
sl@0
   352
	}