예제 #1
0
static long Hash(tHashAlgo hashAlgo, const CByteArray & oData)
{
	CHash oHash;
	CByteArray oHashData = oHash.Hash(hashAlgo, oData);

	printf("Hash: %s\n", oHashData.ToString(true, false).c_str());
	WriteFile("", "hash.bin", oHashData);

	return 0;
}
TVerdict CHashIncrementalHashWithCopyStep::doTestStepL()
	{
	if (TestStepResult()==EPass)
		{
		
		//Assume faliure, unless all is successful
		SetTestStepResult(EFail);
		
		INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy ***"));
		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
		
		TVariantPtrC algorithmUid;
		TVariantPtrC operationModeUid;
		TPtrC sourcePath;
		TPtrC expectedHash;
		
		//Extract the Test Case ID parameter from the specified INI file
		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash))
			{
			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
			SetTestStepResult(EFail);
			}
		else
			{
			//Create a pointer for the Hash Implementation Object
			CHash* hashImpl = NULL;
			
			//Retrieve a Hash Factory Object					
			TRAPD(err,CHashFactory::CreateHashL(hashImpl,
												algorithmUid,
												operationModeUid,
												NULL,
												NULL));  				
	
			if(hashImpl && (err == KErrNone))
				{
				
				//Push the Hash Implementation Object onto the Cleanup Stack
				CleanupStack::PushL(hashImpl);
				
				RFs fsSession;
				
				//Create a connection to the file server	
				err = fsSession.Connect();
					
				if(err != KErrNone)
					{
					ERR_PRINTF2(_L("*** Error: File Server Connection - %d ***"), err);
					SetTestStepResult(EFail);
					}	
				else
					{
					RFile sourceFile;
					CleanupClosePushL(sourceFile);
	    			
	    			//Open the specified source file		
	    			err = sourceFile.Open(fsSession,sourcePath, EFileRead);
	    					
	    			if(err != KErrNone)
						{
						ERR_PRINTF2(_L("*** Error: Opening Source File - %d ***"), err);
						SetTestStepResult(EFail);
						}
					else
						{
						
						TInt sourceLength = 0;
						TInt readPosition = 0;
						TInt readIncrement = 0;
						TBool hashComplete = EFalse;
						TBool hashCopied = EFalse;
						TPtrC8 hashStr;
						
						CHash* hashCopyImpl = NULL;
						
						User::LeaveIfError(sourceFile.Size(sourceLength));
						
						//Divide the total size of the source file up into individual equal sized blocks to read
						//over several increments
						readIncrement = sourceLength/KDataReadBlocks;
						
						do 
							{							
							//Create a heap based descriptor to store the data
							HBufC8* sourceData = HBufC8::NewL(readIncrement);
							CleanupStack::PushL(sourceData);
							TPtr8 sourcePtr = sourceData->Des();
							
							//Read in a block of data from the source file from the current position
							err = sourceFile.Read(readPosition,sourcePtr,readIncrement);
							
							//Update the read position by adding the number of bytes read
							readPosition += readIncrement;
							
							if(readPosition == readIncrement)
								{
								//Read in the first block from the data file into the Hash implementation object
								hashImpl->Hash(*sourceData);
								INFO_PRINTF2(_L("Intial Hash - Bytes Read: %d"), readPosition);
								}
							else if(readPosition >= sourceLength)
								{
								//Reading in the final block, constructs the complete hash value and returns it within a TPtrC8
								hashStr.Set(hashCopyImpl->Final(*sourceData));
								
								//Sets the Complete Flag to ETrue in order to drop out of the loop
								hashComplete = ETrue;
								
								TInt totalRead = (readPosition - readIncrement) + (*sourceData).Length();
								INFO_PRINTF2(_L("Final Hash - Bytes Read: %d"),totalRead);
								}
							//If the read position is half the source length and the implementation
							//object hasn't already been copied
							else if((readPosition >= sourceLength/2) && (hashCopied == EFalse))
								{
								//Update the hash message before copying
								hashImpl->Update(*sourceData);
								
								INFO_PRINTF1(_L("Copying Hash Object..."));
								
								//Create a Copy of the existing Hash Object and all internal state of the message digest
								hashCopyImpl = hashImpl->CopyL();
								
								hashCopied = ETrue;
								
								INFO_PRINTF2(_L("*** HASH COPY - Bytes Read: %d ***"), readPosition);
								}
							else
								{
								//Update the message data within the Hash object with the new block
								if(hashCopied == EFalse)
									{
									hashImpl->Update(*sourceData);
									INFO_PRINTF2(_L("Hash Update - Bytes Read: %d"), readPosition);		
									}
								else
									{
									hashCopyImpl->Update(*sourceData);
									INFO_PRINTF2(_L("Hash Update (Copy) - Bytes Read: %d"), readPosition);		
									}
								}
							
							CleanupStack::PopAndDestroy(sourceData);
							
								
							}while(hashComplete == EFalse);
						
						//Create a NULL TCharacteristics pointer
						const TCharacteristics* charsPtr(NULL);
						
						//Retrieve the characteristics for the hash implementation object
						TRAP_LOG(err, hashImpl->GetCharacteristicsL(charsPtr));
						
						//Static cast the characteristics to type THashCharacteristics
						const THashCharacteristics* hashCharsPtr = static_cast<const THashCharacteristics*>(charsPtr);
						
						//The hash output size is returned in Bits, divide by 8 to get the Byte size
						TInt hashSize = hashCharsPtr->iOutputSize/8;
						
						//Retrieve the final 8bit hash value and convert to 16bit												
						HBufC* hashData = HBufC::NewLC(hashSize);
						TPtr hashPtr = hashData->Des();
						
						//Copy the hashed content into the heap based descriptor
						hashPtr.Copy(hashStr);
						
					 	//Take the 16bit descriptor and convert the string to hexadecimal
						TVariantPtrC convertHash;
						convertHash.Set(hashPtr);
						HBufC* hashResult = convertHash.HexStringLC();
						
						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*hashResult);
						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
						
						//If the returned hash value matches the expected hash, Pass the test		
						if(*hashResult == expectedHash)
							{
							INFO_PRINTF1(_L("*** Hash - Incremental Hash with Copy : PASS ***"));
							SetTestStepResult(EPass);	
							}
						else
							{
							ERR_PRINTF2(_L("*** FAIL: Hashed and Expected Value Mismatch  ***"), err);
							SetTestStepResult(EFail);	
							}
								
						CleanupStack::PopAndDestroy(hashResult);
						CleanupStack::PopAndDestroy(hashData);						
						
						delete hashCopyImpl;
						}
					
					//Cleanup the Source RFile	
					CleanupStack::PopAndDestroy();	
					}

				fsSession.Close();	
				
				CleanupStack::PopAndDestroy(hashImpl);
				}
			else
				{
				ERR_PRINTF2(_L("*** FAIL: Failed to Create Hash Object - %d ***"), err);
				SetTestStepResult(EFail);	
				}
			}
			
		}
		
	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
	return TestStepResult();
	}
TVerdict CHmacSetOperationModeCheckingStep::doTestStepL()
	{
	if (TestStepResult()==EPass)
		{
		
		//Assume faliure, unless all is successful
		SetTestStepResult(EFail);
		
		INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking ***"));
		INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());
		
		TVariantPtrC algorithmUid;
		TVariantPtrC operationModeUid;
		TVariantPtrC secondOperationModeUid;
		TPtrC sourcePath;
		TPtrC expectedHash;
		TPtrC expectedHmac;
		TPtrC encryptKey;
		TVariantPtrC keyType;
		
		//Extract the Test Case ID parameter from the specified INI file
		if(!GetStringFromConfig(ConfigSection(),KConfigAlgorithmUid,algorithmUid) ||
			!GetStringFromConfig(ConfigSection(),KConfigOperationMode,operationModeUid) ||
			!GetStringFromConfig(ConfigSection(),KConfigSecondOperationMode,secondOperationModeUid) ||
			!GetStringFromConfig(ConfigSection(),KConfigSourcePath,sourcePath) ||
			!GetStringFromConfig(ConfigSection(),KConfigExHashHmacValue,expectedHash) ||
			!GetStringFromConfig(ConfigSection(),KConfigExSecondHashHmacValue,expectedHmac) ||
			!GetStringFromConfig(ConfigSection(),KConfigEncryptKey,encryptKey) ||
			!GetStringFromConfig(ConfigSection(),KConfigEncryptKeyType,keyType))
			{
			ERR_PRINTF1(_L("** Error: Failed to Load Configuration Parameters **"));
			SetTestStepResult(EFail);
			}
		else
			{	
			RFs fsSession;
				
			//Create a connection to the file server	
			User::LeaveIfError(fsSession.Connect());
						
			RFile sourceFile;
	    	CleanupClosePushL(sourceFile);
	    			
	    	//Open the specified source file		
	    	User::LeaveIfError(sourceFile.Open(fsSession,sourcePath, EFileRead));
	    					
			TInt sourceLength = 0;
			User::LeaveIfError(sourceFile.Size(sourceLength));
						
			//Create a heap based descriptor to store the data
			HBufC8* sourceData = HBufC8::NewL(sourceLength);						
			CleanupStack::PushL(sourceData);
			TPtr8 sourcePtr = sourceData->Des();
						
			sourceFile.Read(sourcePtr);
							
			if(sourcePtr.Length() != sourceLength)
				{
				ERR_PRINTF1(_L("*** Error: Reading Source File ***"));
				SetTestStepResult(EFail);	
				}
			else
				{
				//Create a pointer for the Hash + Key (Hmac) Implementation Object
				CHash* hashHmacImpl = NULL;
				
				//Convert encryption key to an 8 Bit Descriptor
				HBufC8* keyStr = HBufC8::NewLC(encryptKey.Length());
				TPtr8 keyStrPtr = keyStr->Des();
				
				keyStrPtr.Copy(encryptKey);
				
				//Create an new CryptoParams object to encapsulate the invalid key type and key string
				CCryptoParams* keyParams = CCryptoParams::NewL();
				CleanupStack::PushL(keyParams);
				keyParams->AddL(*keyStr,keyType);
				
				//Create a valid CKey Object
				TKeyProperty keyProperty;
				CKey* key = CKey::NewL(keyProperty,*keyParams);
				CleanupStack::PushL(key);
				
				//Construct an initial hash object with NO key, Catching any possible Leaves			
				TRAPD(err,CHashFactory::CreateHashL(
									hashHmacImpl,
									algorithmUid,
									operationModeUid,
									NULL,
									NULL));
									
										
				if(hashHmacImpl && (err == KErrNone))
					{
					
					//Push the Implementation Object onto the Cleanup Stack
					CleanupStack::PushL(hashHmacImpl);
					
					//Create a NULL TCharacteristics pointer
					const TCharacteristics* charsPtrA(NULL);
					
					//Retrieve the characteristics for the hash implementation object
					TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrA));
					
					//Static cast the characteristics to type THashCharacteristics
					const THashCharacteristics* hashCharsPtrA = static_cast<const THashCharacteristics*>(charsPtrA);
					
					//The hash output size is returned in Bits, divide by 8 to get the Byte size
					TInt hashSize = hashCharsPtrA->iOutputSize/8;
					
					//Retrieve the final 8bit hash value and convert to 16bit												
					HBufC* hashDataA = HBufC::NewLC(hashSize);
					TPtr hashPtrA = hashDataA->Des();
					
					hashPtrA.Copy(hashHmacImpl->Hash(*sourceData));
					
					//Take the 16bit descriptor and convert the string to hexadecimal
					TVariantPtrC convertHash;
					convertHash.Set(hashPtrA);
					
					HBufC* resultA = convertHash.HexStringLC();							
						
					INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultA);
					INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
					
					if(*resultA == expectedHash)
						{
						INFO_PRINTF1(_L("*** PRIMARY HASH VALID - STAGE 1 PASS ***"));
						
						//Set the valid key within the Hmac Implementation Object
						TRAP(err,hashHmacImpl->SetKeyL(*key));
						
						if(err!=KErrNone)
							{
							ERR_PRINTF2(_L("*** ERROR %d: Setting Key ***"),err);
							User::Leave(err);	
							}
						else
							{
							INFO_PRINTF1(_L("*** HMAC KEY SET ***"));	
							}
										
						//Set the Operation Mode of the Hmac Implementation Object
						hashHmacImpl->SetOperationModeL(secondOperationModeUid);
						
						if(err!=KErrNone)
							{
							ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&secondOperationModeUid);	
							User::Leave(err);
							}
						else
							{
							INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&secondOperationModeUid);	
							}
						
						//Create a NULL TCharacteristics pointer
						const TCharacteristics* charsPtrB(NULL);
					
						//Retrieve the characteristics for the hash implementation object
						TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrB));
					
						//Static cast the characteristics to type THashCharacteristics
						const THashCharacteristics* hashCharsPtrB = static_cast<const THashCharacteristics*>(charsPtrB);
						
						//The hash output size is returned in Bits, divide by 8 to get the Byte size
						hashSize = hashCharsPtrB->iOutputSize/8;
						
						//Retrieve the final 8bit hash value and convert to 16bit												
						HBufC* hashDataB = HBufC::NewLC(hashSize);
						TPtr hashPtrB = hashDataB->Des();
						
						hashPtrB.Copy(hashHmacImpl->Hash(*sourceData));
						
						//Take the 16bit descriptor and convert the string to hexadecimal
						convertHash.Set(hashPtrB);
						HBufC* resultB = convertHash.HexStringLC();						
						
						INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultB);
						INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHmac);
						
						if(*resultB == expectedHmac)
							{
							INFO_PRINTF1(_L("*** SECONDARY HASH VALID - STAGE 2 PASS ***"));
							
							//Set the Operation Mode of the Hmac Implementation Object
							TRAP(err,hashHmacImpl->SetOperationModeL(operationModeUid));
							
							if(err!=KErrNone)
								{
								ERR_PRINTF3(_L("*** ERROR %d: Setting Operation Mode %S ***"),err,&operationModeUid);	
								User::Leave(err);
								}
							else
								{
								INFO_PRINTF2(_L("*** OPERATION MODE SET : %S ***"),&operationModeUid);	
								}
								
							//Create a NULL TCharacteristics pointer
							const TCharacteristics* charsPtrC(NULL);
						
							//Retrieve the characteristics for the hash implementation object
							TRAP_LOG(err, hashHmacImpl->GetCharacteristicsL(charsPtrC));
						
							//Static cast the characteristics to type THashCharacteristics
							const THashCharacteristics* hashCharsPtrC = static_cast<const THashCharacteristics*>(charsPtrC);
							
							//The hash output size is returned in Bits, divide by 8 to get the Byte size
							hashSize = hashCharsPtrC->iOutputSize/8;
							
							//Retrieve the final 8bit hash value and convert to 16bit												
							HBufC* hashDataC = HBufC::NewLC(hashSize);
							TPtr hashPtrC = hashDataC->Des();
							
							hashPtrC.Copy(hashHmacImpl->Hash(*sourceData));
							
							//Take the 16bit descriptor and convert the string to hexadecimal
							convertHash.Set(hashPtrC);
							HBufC* resultC = convertHash.HexStringLC();							
							
							INFO_PRINTF2(_L("*** Hashed Data: %S ***"),&*resultC);
							INFO_PRINTF2(_L("*** Expected Hash: %S ***"),&expectedHash);
									
							if(*resultC == expectedHash)
								{
								INFO_PRINTF1(_L("*** FINAL HASH VALID - STAGE 3 PASS ***"));
								INFO_PRINTF1(_L("*** Hmac - Set Operation Mode Checking : PASS ***"));
								SetTestStepResult(EPass);
								}
							else
								{
								ERR_PRINTF1(_L("*** STAGE 3 FAIL: Hash and Expected Value Mismatch ***"));
								SetTestStepResult(EFail);	
								}
								
							CleanupStack::PopAndDestroy(resultC);
							CleanupStack::PopAndDestroy(hashDataC);
							}
						else
							{
							ERR_PRINTF1(_L("*** STAGE 2 FAIL: Hash and Expected Value Mismatch ***"));
							SetTestStepResult(EFail);	
							}
							
						CleanupStack::PopAndDestroy(resultB);
						CleanupStack::PopAndDestroy(hashDataB);	
						}
					else
						{
						ERR_PRINTF1(_L("*** STAGE 1 FAIL: Hash and Expected Value Match ***"));
						SetTestStepResult(EFail);	
						}
						
					CleanupStack::PopAndDestroy(resultA);
					CleanupStack::PopAndDestroy(hashDataA);
					CleanupStack::PopAndDestroy(hashHmacImpl);										
					}
				else if(err==KErrNotSupported)
					{
					if((((TUid)operationModeUid != KHashModeUid) && ((TUid)operationModeUid != KHmacModeUid)) ||
						((TUid)algorithmUid != KMd2Uid) && (TUid)algorithmUid != KMd5Uid && (TUid)algorithmUid != KSha1Uid && (TUid)algorithmUid != KMd4Uid && (TUid)algorithmUid != KSha224Uid && (TUid)algorithmUid != KSha256Uid && (TUid)algorithmUid != KSha384Uid && (TUid)algorithmUid != KSha512Uid)
						{
						ERR_PRINTF2(_L("*** Object Load Failure - Invalid Operation Mode : %d ***"), err);
						User::Leave(err);	
						}
					else
						{
						SetTestStepResult(EFail);	
						}
					}
				else
					{
					ERR_PRINTF2(_L("*** Hash/Hmac Facotry Object Load Failure : %d ***"), err);
					User::Leave(err);	
					}
					
				CleanupStack::PopAndDestroy(key);
				CleanupStack::PopAndDestroy(keyParams);
				CleanupStack::PopAndDestroy(keyStr);
				}
							
			CleanupStack::PopAndDestroy(sourceData);
								
			//Cleanup the Source RFile	
			CleanupStack::PopAndDestroy();	
						
			fsSession.Close();	
			}
			
		}
		else
		{
			SetTestStepResult(EFail);	
		}		
		
	INFO_PRINTF2(_L("HEAP CELLS: %d"), User::CountAllocCells());	
	
	return TestStepResult();
	}