os/security/crypto/weakcryptospi/test/thash/thash.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2009 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 */
    22 
    23 #include "hashtestutils.h"
    24 #include <utf.h>
    25 
    26 TBool gInOOMTest=EFalse;
    27 
    28 RTest test(_L("Hash Tests"));
    29 
    30 const TInt KMaxHashSize = 64; // Hash size in bytes
    31 
    32 void Hex(HBufC8& aString)
    33 	{
    34 	TPtr8 ptr=aString.Des();
    35 	if (aString.Length()%2)
    36 		{
    37 		ptr.SetLength(0);
    38 		return;
    39 		}
    40 	TInt i;
    41 	for (i=0;i<aString.Length();i+=2)
    42 		{
    43 		TUint8 tmp;
    44 		tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
    45 		tmp*=16;
    46 		tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
    47 		ptr[i/2]=tmp;
    48 		}
    49 	ptr.SetLength(aString.Length()/2);
    50 	}
    51 
    52 void Spin()
    53 {//	Pointless function to print a dot
    54 	if (gInOOMTest)
    55 		{
    56 		static TInt count=0;
    57 		if (count++==100)
    58 			{
    59 			test.Printf(_L("o"));
    60 			count=0;
    61 			}
    62 		return;
    63 		}
    64 	test.Printf(_L("."));
    65 }
    66 
    67 void FunctionalityTestL(CMessageDigest* aMD)
    68 	{
    69 	if (!gInOOMTest)
    70 		test.Next(_L("Functionality test - original API"));
    71 
    72 	const TInt maxbuffersize=1024;
    73 	TInt buffersize;
    74 	TInt increment;
    75 	if (gInOOMTest)
    76 		{
    77 		buffersize=256;
    78 		increment=6;
    79 		}
    80 	else
    81 		{
    82 		buffersize=maxbuffersize;
    83 		increment=1;
    84 		}
    85 
    86 	TBool testSuccess = ETrue;
    87 	TInt i = 0;
    88 	TBuf8<maxbuffersize> buf(maxbuffersize);
    89 	for (;i<buffersize;i++)
    90 		{
    91 		buf[i]=(TUint8)i;
    92 		}
    93 	
    94 	for (i=0;i<buffersize;i+=increment)
    95 		{
    96 		TInt j;
    97 		Spin();
    98 		if (!gInOOMTest)
    99 			{
   100 			if (i>128)
   101 				{
   102 				increment=8;
   103 				}
   104 			}
   105 		else
   106 			{
   107 			if (i>24)
   108 				{
   109 				increment=32;
   110 				}
   111 			}
   112 
   113 		for (j=0;j<i;j+=16)
   114 			{
   115 			buf[0]=(TUint8)j;
   116 			TPtrC8 ptr=buf.Left(i);
   117 			TPtrC8 ptr2=buf.Left(j);
   118 			TPtrC8 ptr3=buf.Mid(j,i-j);
   119 
   120 			CMessageDigest* first=aMD->ReplicateL();
   121 			CleanupStack::PushL(first);
   122 			TPtrC8 firstFinal = first->Hash(ptr);
   123 			
   124 			aMD->Reset();
   125 			aMD->Update(ptr);
   126 			TPtrC8 aMDFinal = aMD->Final();
   127 
   128 			CMessageDigest* second=aMD->ReplicateL();
   129 			CleanupStack::PushL(second);
   130 			second->Hash(ptr2);
   131 			
   132 			CMessageDigest* third=second->CopyL();
   133 			CleanupStack::PushL(third);
   134 			
   135 			TPtrC8 secondFinal = second->Hash(ptr3);
   136 			
   137 			if (aMDFinal!=firstFinal)
   138 				{
   139 				testSuccess = EFalse;
   140 				}
   141 			
   142 			if (firstFinal!=secondFinal)
   143 				{
   144 				testSuccess = EFalse;
   145 				}
   146 
   147 			TPtrC8 thirdFinal = third->Hash(ptr3);
   148 			if (firstFinal!=thirdFinal)
   149 				{
   150 				testSuccess = EFalse;
   151 				}
   152 			CleanupStack::PopAndDestroy(3);		// first, second, third
   153 			}
   154 		}
   155 	
   156 ///////////////////////////////////////////////////////////////
   157 //	Now test the new API - Update/Final functions	
   158 ///////////////////////////////////////////////////////////////
   159 	if (!gInOOMTest)
   160 		test.Next(_L("\rFunctionality test - calls added API functions Final & Update"));
   161 
   162 	for (i=0;i<buffersize;i++)
   163 		{
   164 		buf[i]=(TUint8)i;
   165 		}
   166 	
   167 	for (i=0;i<buffersize;i+=increment)
   168 		{
   169 		TInt j;
   170 		Spin();
   171 		if (!gInOOMTest)
   172 			{
   173 			if (i>128)
   174 				{
   175 				increment=8;
   176 				}
   177 			}
   178 		else
   179 			{
   180 			if (i>24)
   181 				{
   182 				increment=32;
   183 				}
   184 			}
   185 		for (j=0;j<i;j+=16)
   186 			{
   187 			buf[0]=(TUint8)j;
   188 			TPtrC8 ptr=buf.Left(i);
   189 			TPtrC8 ptr2=buf.Left(j);
   190 			TPtrC8 ptr3=buf.Mid(j,i-j);
   191 			
   192 			CMessageDigest* first=aMD->ReplicateL();
   193 			CleanupStack::PushL(first);
   194 			first->Update(ptr);
   195 			
   196 			aMD->Update(ptr);
   197 
   198 			CMessageDigest* second=aMD->ReplicateL();
   199 			CleanupStack::PushL(second);			
   200 			second->Update(ptr2);
   201 			
   202 			CMessageDigest* third=second->CopyL();
   203 			CleanupStack::PushL(third);
   204 			third->Update(ptr3);
   205 			
   206 			second->Update(ptr3);
   207 			
   208 			TPtrC8 aMDFinal = aMD->Final();
   209 			TPtrC8 firstFinal = first->Final();
   210 			TPtrC8 secondFinal = second->Final();
   211 			TPtrC8 thirdFinal = third->Final();
   212 			
   213 			if (aMDFinal!=firstFinal)
   214 				{
   215 				testSuccess = EFalse;
   216 				}
   217 
   218 			if (firstFinal!=secondFinal)
   219 				{
   220 				testSuccess = EFalse;
   221 				}
   222 			
   223 			if (firstFinal!=thirdFinal)
   224 				{
   225 				testSuccess = EFalse;
   226 				}
   227 
   228 			CleanupStack::PopAndDestroy(3);		// first, second, third
   229 			}
   230 		}
   231 	
   232 
   233 	test.Printf(_L("\r\n"));
   234 	
   235 	if (!testSuccess)
   236 		User::Leave(KErrGeneral);
   237 	}
   238 
   239 void VectorTestL(CMessageDigest* aMD,const TDesC& aFilename)
   240 	{
   241 	test.Next(_L("Test Vector tests - original API"));
   242 	TBool finished=EFalse;
   243 	TBool testSuccess = ETrue;
   244 	//aFilename is a .dat file which contains the message and the corresponding digest
   245 	CTestData* data = CTestData::NewL(aFilename);
   246 	CleanupStack::PushL(data);
   247 
   248 	while (!finished)
   249 		{
   250 		switch (data->Type())
   251 			{
   252 		case CTestData::EMessage:
   253 			{
   254 			break;
   255 			}
   256 		//This is added to read large input data from the files.
   257 		case CTestData::EFileName:
   258 			{
   259 			CMessageDigest* md=aMD->ReplicateL();
   260 			CleanupStack::PushL(md);
   261 			
   262 			//get the filename from the .dat file
   263 			HBufC8* filename = (*data)[1];
   264 			User::LeaveIfNull(filename);
   265 			CleanupStack::PushL(filename);
   266 			
   267 			HBufC8* output= (*data)[2];
   268 			User::LeaveIfNull(output);
   269 			CleanupStack::PushL(output);
   270 			Hex(*output);
   271 			
   272 			HBufC16* inputFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*filename);
   273 			
   274 			RFs fs;
   275 			RFile file;
   276 			CleanupClosePushL(fs);
   277 			User::LeaveIfError(fs.Connect());
   278 			TDriveUnit sysDrive(fs.GetSystemDrive());
   279 			TBuf<24> filePath (sysDrive.Name());
   280 			filePath.Append(_L("\\thash\\"));
   281 			User::LeaveIfError(fs.SetSessionPath(filePath));
   282 			CleanupClosePushL(file);
   283 			User::LeaveIfError(file.Open(fs,*inputFileName,EFileShareAny|EFileRead));
   284 			// read into iFile
   285 			TInt size=0;
   286 			file.Size(size);
   287 						
   288 			HBufC8* fileContents=HBufC8::NewMaxL(size);
   289 			
   290 			TPtr8 ptr=fileContents->Des();
   291 			User::LeaveIfError(file.Read(ptr));
   292 			CleanupStack::PopAndDestroy(2, &fs);
   293 			CleanupStack::PushL(fileContents);
   294 			delete inputFileName;
   295 			
   296 			TPtrC8 digest = md->Hash(*fileContents);
   297 			if (digest!=(*output))	
   298 				{
   299 				testSuccess = EFalse;
   300 				}
   301 		
   302 			md->Reset();
   303 
   304 			TPtrC8 digest2 = md->Hash(*fileContents);
   305 			if (digest2!=(*output))	
   306 				{
   307 				testSuccess = EFalse;
   308 				}
   309 
   310 		//	Now try this in 2 half sections (just a quick check)
   311 			md->Reset();
   312 			
   313 			TInt inputLen = fileContents->Length();
   314 			if (inputLen > 1)
   315 				{
   316 				TInt leftHandLen = inputLen/2;
   317 				TPtrC8 left = fileContents->Left(leftHandLen);
   318 				TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
   319 
   320 				TPtrC8 halfDigest = md->Hash(left);
   321 				if (halfDigest.Size()==0)	//	Unnecessary test, but removes
   322 					User::Leave(KErrAbort);	//	a warning about not using halfDigest
   323 					
   324 				TPtrC8 wholeDigest = md->Hash(right);
   325 				
   326 				if (wholeDigest!=(*output))	
   327 					{
   328 					testSuccess = EFalse;
   329 					}			
   330 				}
   331 				
   332 			md->Reset();
   333 			if (md->Final(*fileContents)!=(*output))
   334 				{
   335 				testSuccess = EFalse;
   336 				}
   337 
   338 		//	Now try this in 2 half sections (just a quick check)
   339 			md->Reset();
   340 			inputLen = fileContents->Length();
   341 			if (inputLen > 1)
   342 				{
   343 				TInt leftHandLen = inputLen/2;
   344 				TPtrC8 left = fileContents->Left(leftHandLen);
   345 				TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
   346 
   347 				md->Update(left);
   348 				TPtrC8 wholeDigest = md->Final(right);
   349 				
   350 				if (wholeDigest!=(*output))	
   351 					{
   352 					testSuccess = EFalse;
   353 					}			
   354 				}
   355 			CleanupStack::PopAndDestroy(4, md);//md,filename,output,fileContents
   356 	
   357 			break;
   358 			}
   359 		case CTestData::EData:
   360 			{
   361 			CMessageDigest* md=aMD->ReplicateL();
   362 			CleanupStack::PushL(md);
   363 			
   364 			HBufC8* input = (*data)[0];
   365 			User::LeaveIfNull(input);
   366 		    CleanupStack::PushL(input);
   367 			
   368 			HBufC8* output= (*data)[1];
   369 			User::LeaveIfNull(output);
   370 			CleanupStack::PushL(output);
   371 			Hex(*input);
   372 			Hex(*output);
   373             
   374             //call the Hash function of the Algorithm.
   375 			TPtrC8 digest = md->Hash(*input);
   376 			//Compare the digest generated with the output(digest) expected.
   377 			if (digest!=(*output))	
   378 				{
   379 				testSuccess = EFalse;
   380 				}
   381 		
   382 			//check the Reset() function.
   383 			md->Reset();
   384 
   385 			TPtrC8 digest2 = md->Hash(*input);
   386 			if (digest2!=(*output))	
   387 				{
   388 				testSuccess = EFalse;
   389 				}
   390 
   391 		//	Now try this in 2 half sections (just a quick check)
   392 			md->Reset();
   393 			
   394 			TInt inputLen = input->Length();
   395 			if (inputLen > 1)
   396 				{
   397 				TInt leftHandLen = inputLen/2;
   398 				TPtrC8 left = input->Left(leftHandLen);
   399 				TPtrC8 right = input->Right(inputLen - leftHandLen);
   400 
   401 				TPtrC8 halfDigest = md->Hash(left);
   402 				if (halfDigest.Size()==0)	//	Unnecessary test, but removes
   403 					User::Leave(KErrAbort);	//	a warning about not using halfDigest
   404 					
   405 				TPtrC8 wholeDigest = md->Hash(right);
   406 				
   407 				if (wholeDigest!=(*output))	
   408 					{
   409 					testSuccess = EFalse;
   410 					}			
   411 				}
   412 
   413 ///////////////////////////////////////////////////////////////
   414 //	Now test the new API - Update/Final functions	
   415 ///////////////////////////////////////////////////////////////
   416 			md->Reset();
   417 			if (md->Final(*input)!=(*output))
   418 				{
   419 				testSuccess = EFalse;
   420 				}
   421 
   422 		//	Now try this in 2 half sections (just a quick check)
   423 			md->Reset();
   424 			inputLen = input->Length();
   425 			if (inputLen > 1)
   426 				{
   427 				TInt leftHandLen = inputLen/2;
   428 				TPtrC8 left = input->Left(leftHandLen);
   429 				TPtrC8 right = input->Right(inputLen - leftHandLen);
   430 
   431 				md->Update(left);
   432 				TPtrC8 wholeDigest = md->Final(right);
   433 				
   434 				if (wholeDigest!=(*output))	
   435 					{
   436 					testSuccess = EFalse;
   437 					}			
   438 				}
   439 			CleanupStack::PopAndDestroy(3);
   440 			break;
   441 			}
   442 		case CTestData::EFinished:
   443 			finished=ETrue;
   444 			break;
   445 		default:
   446 			test.Printf(_L("Error in data file\r\n"));
   447 			break;
   448 			}
   449 		};
   450 
   451 	CleanupStack::PopAndDestroy(data);
   452 	
   453 	if (!testSuccess)
   454 		User::Leave(KErrGeneral);
   455 	}
   456 
   457 void OOMTestL(CMessageDigest* aMD)
   458 	{
   459 	test.Next(_L("Out of memory test"));
   460 	TInt err = KErrNoMemory;
   461 	TInt nextFailure=0;
   462 	gInOOMTest=ETrue;
   463 	while (err!=KErrNone)
   464 		{
   465 		__UHEAP_MARK;
   466 		__UHEAP_FAILNEXT(nextFailure);
   467 		TRAP(err,FunctionalityTestL(aMD));		
   468 		__UHEAP_MARKEND;
   469 		nextFailure++;
   470 		}
   471 	__UHEAP_RESET;
   472 	gInOOMTest=EFalse;
   473 	}
   474 
   475 void HMACVectorTestL(CMessageDigest* aMD,const TDesC& aFilename)
   476 	{
   477 	test.Next(_L("HMAC Test Vector tests"));
   478 	CTestData* data = CTestData::NewL(aFilename);
   479 	CleanupStack::PushL(data);
   480 
   481 	TBool finished=EFalse;
   482 	TBool testSuccess = ETrue;
   483 	
   484 	while (!finished)
   485 		{
   486 		switch (data->Type())
   487 			{
   488 		case CTestData::EMessage:
   489 			{
   490 			break;
   491 			}
   492 		case CTestData::EFileName:
   493 			{
   494 			//get the filename from the .dat file
   495 			HBufC8* filename = (*data)[1];
   496 			User::LeaveIfNull(filename);
   497 			CleanupStack::PushL(filename);
   498 			
   499 			HBufC8* key= (*data)[2];
   500 			User::LeaveIfNull(key);
   501 			CleanupStack::PushL(key);
   502 						
   503 			HBufC16* inputFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*filename);
   504 			
   505 			HBufC8* output = (*data)[3];
   506 			User::LeaveIfNull(output);
   507 			CleanupStack::PushL(output);
   508 			
   509 			Hex(*key);
   510 			Hex(*output);
   511 			
   512 			RFs fs;
   513 			RFile file;
   514 			CleanupClosePushL(fs);
   515 			User::LeaveIfError(fs.Connect());
   516 			TDriveUnit sysDrive(fs.GetSystemDrive());
   517 			TBuf<24> filePath (sysDrive.Name());
   518 			filePath.Append(_L("\\thash\\"));
   519 			User::LeaveIfError(fs.SetSessionPath(filePath));
   520 			CleanupClosePushL(file);
   521 			User::LeaveIfError(file.Open(fs,*inputFileName,EFileShareAny|EFileRead));
   522 			// read into iFile
   523 			TInt size=0;
   524 			file.Size(size);
   525 			HBufC8* fileContents=HBufC8::NewMaxL(size);
   526 			
   527 			TPtr8 ptr=fileContents->Des();
   528 			User::LeaveIfError(file.Read(ptr));
   529 			CleanupStack::PopAndDestroy(2, &fs);
   530 			CleanupStack::PushL(fileContents);
   531 			delete inputFileName;
   532 			
   533 			CMessageDigest* temp = aMD->ReplicateL();
   534 			CleanupStack::PushL(temp);
   535 			CMessageDigest* md = CHMAC::NewL(*key, temp);
   536 			CleanupStack::Pop(temp);	//	Now owned by md
   537 			
   538 			TPtrC8 digest = md->Hash(*fileContents);
   539 			if (digest!=(*output))
   540 				{
   541 				testSuccess = EFalse;
   542 				}
   543 						
   544 		//	Now try this in 2 half sections (just a quick check)
   545 			md->Reset();
   546 		
   547 			TInt inputLen = fileContents->Length();
   548 			if (inputLen > 1)
   549 				{
   550 				TInt leftHandLen = inputLen/2;
   551 				TPtrC8 left = fileContents->Left(leftHandLen);
   552 				TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
   553 
   554 				TPtrC8 halfDigest = md->Hash(left);
   555 				if (halfDigest.Size()==0)	//	Unnecessary test, but removes
   556 					User::Leave(KErrAbort);	//	a warning about not using halfDigest
   557 				TPtrC8 wholeDigest = md->Hash(right);
   558 				
   559 				if (wholeDigest!=(*output))	
   560 					{
   561 					testSuccess = EFalse;
   562 					}
   563 				}
   564 
   565 			md->Reset();
   566 			TPtrC8 finalDigest = md->Final(*fileContents);
   567 			if (finalDigest!=(*output))
   568 				{
   569 				testSuccess = EFalse;
   570 				}			
   571 
   572 		//	Now try this in 2 half sections (just a quick check)
   573 			md->Reset();
   574 		
   575 			inputLen = fileContents->Length();
   576 			if (inputLen > 1)
   577 				{
   578 				TInt leftHandLen = inputLen/2;
   579 				TPtrC8 left = fileContents->Left(leftHandLen);
   580 				TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
   581 
   582 				md->Update(left);
   583 				TPtrC8 wholeDigest = md->Final(right);
   584 				
   585 				if (wholeDigest!=(*output))	
   586 					{
   587 					testSuccess = EFalse;
   588 					}
   589 				}
   590 			delete md;
   591 			CleanupStack::PopAndDestroy(4, filename);	//	filename, key, output,fileContents			
   592 			break;
   593 			}
   594 		case CTestData::EData:
   595 			{
   596 			HBufC8* input = (*data)[0];
   597 			User::LeaveIfNull(input);
   598 			CleanupStack::PushL(input);
   599 			
   600 			HBufC8* key = (*data)[1];
   601 			User::LeaveIfNull(key);
   602 			CleanupStack::PushL(key);
   603 			
   604 			HBufC8* output = (*data)[2];
   605 			User::LeaveIfNull(output);
   606 			CleanupStack::PushL(output);
   607 			
   608 			Hex(*input);
   609 			Hex(*key);
   610 			Hex(*output);
   611 			
   612 			CMessageDigest* temp = aMD->ReplicateL();
   613 			CleanupStack::PushL(temp);
   614 			CMessageDigest* md = CHMAC::NewL(*key, temp);
   615 			CleanupStack::Pop(temp);	//	Now owned by md
   616 			
   617 			TPtrC8 digest = md->Hash(*input);
   618 			if (digest!=(*output))
   619 				{
   620 				testSuccess = EFalse;
   621 				}
   622 						
   623 		//	Now try this in 2 half sections (just a quick check)
   624 			md->Reset();
   625 		
   626 			TInt inputLen = input->Length();
   627 			if (inputLen > 1)
   628 				{
   629 				TInt leftHandLen = inputLen/2;
   630 				TPtrC8 left = input->Left(leftHandLen);
   631 				TPtrC8 right = input->Right(inputLen - leftHandLen);
   632 
   633 				TPtrC8 halfDigest = md->Hash(left);
   634 				if (halfDigest.Size()==0)	//	Unnecessary test, but removes
   635 					User::Leave(KErrAbort);	//	a warning about not using halfDigest
   636 				TPtrC8 wholeDigest = md->Hash(right);
   637 				
   638 				if (wholeDigest!=(*output))	
   639 					{
   640 					testSuccess = EFalse;
   641 					}
   642 				}
   643 
   644 ///////////////////////////////////////////////////////////////
   645 //	Now test the new API - Update/Final functions	
   646 ///////////////////////////////////////////////////////////////
   647 			md->Reset();
   648 			TPtrC8 finalDigest = md->Final(*input);
   649 			if (finalDigest!=(*output))
   650 				{
   651 				testSuccess = EFalse;
   652 				}			
   653 
   654 		//	Now try this in 2 half sections (just a quick check)
   655 			md->Reset();
   656 		
   657 			inputLen = input->Length();
   658 			if (inputLen > 1)
   659 				{
   660 				TInt leftHandLen = inputLen/2;
   661 				TPtrC8 left = input->Left(leftHandLen);
   662 				TPtrC8 right = input->Right(inputLen - leftHandLen);
   663 
   664 				md->Update(left);
   665 				TPtrC8 wholeDigest = md->Final(right);
   666 				
   667 				if (wholeDigest!=(*output))	
   668 					{
   669 					testSuccess = EFalse;
   670 					}
   671 				}
   672 			delete md;
   673 			CleanupStack::PopAndDestroy(3);	//	input, key, output			
   674 			break;
   675 			}
   676 		case CTestData::EFinished:
   677 			finished=ETrue;
   678 			break;
   679 		default:
   680 			test.Printf(_L("Error in data file\r\n"));
   681 			break;
   682 			}
   683 		};
   684 
   685 	CleanupStack::PopAndDestroy(data);
   686 	
   687 	if (!testSuccess)
   688 		User::Leave(KErrGeneral);
   689 	}
   690 
   691 void HMACTestsL(CMessageDigest* aMD,const TDesC& aFilename, const TDesC& aHashType)
   692 	{
   693 	TBuf<0x40> formattable;
   694 	formattable.Format(_L("HMAC Tests for %S"), &aHashType);
   695 	test.Next(formattable);
   696 	CMessageDigest* temp = aMD->ReplicateL();
   697 	CleanupStack::PushL(temp);
   698 	CHMAC* hmac=CHMAC::NewL(_L8("aaaaaaaa"), temp);
   699 	CleanupStack::Pop(temp);
   700 	CleanupStack::PushL(hmac);
   701 	
   702 //	For each of the available digests
   703 	FunctionalityTestL(hmac);		//JCS for now
   704 	HMACVectorTestL(aMD,aFilename);	
   705 	OOMTestL(hmac);
   706 		
   707 	CleanupStack::PopAndDestroy(hmac);
   708 	}
   709 
   710 void MD2TestsL()
   711 	{
   712 	CMD2* md2;
   713 	md2=CMD2::NewL();
   714 	CleanupStack::PushL(md2);
   715 
   716 	FunctionalityTestL(md2);
   717 	
   718 	VectorTestL(md2,_L("md2.dat"));
   719 	
   720 	OOMTestL(md2);
   721 		
   722 	CleanupStack::PopAndDestroy(md2);
   723 
   724 // Problem reported by Jal Panvel, 17-12-1999.
   725 // Report by email, CSHA1::Hash() returning zero length descriptor
   726 // 
   727 // This was caused by failure to set internal hash descriptor length on setup, this
   728 // problem was present in all hashes except MD2 which set it up correctly.
   729 // Fixed 17-12-1999.
   730 	test.Next(_L("Fixed bugs Tests"));
   731 	md2 = CMD2::NewL();
   732 	CleanupStack::PushL(md2);
   733 	TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
   734 	TBuf8<128> hash;
   735 	hash = md2->Hash(data);
   736 	test(hash.Length() == md2->HashSize());
   737 	
   738 	HMACTestsL(md2,_L("hmacmd2.dat"), _L("md2"));
   739 	CleanupStack::PopAndDestroy(md2);
   740 	}
   741 
   742 void MD5TestsL()
   743 	{
   744 	CMD5* md5;
   745 	md5=CMD5::NewL();
   746 	CleanupStack::PushL(md5);
   747 
   748 	FunctionalityTestL(md5);
   749 	
   750 	VectorTestL(md5,_L("md5.dat"));
   751 	
   752 	OOMTestL(md5);
   753 	
   754 	CleanupStack::PopAndDestroy(md5);
   755 	test.Next(_L("Fixed bugs Tests"));
   756 // Problem reported by Jal Panvel, 17-12-1999.
   757 // Report by email, CSHA1::Hash() returning zero length descriptor
   758 // 
   759 // This was caused by failure to set internal hash descriptor length on setup, this
   760 // problem was present in all hashes except MD2 which set it up correctly.
   761 // Fixed 17-12-1999.
   762 	CMD5* md = CMD5::NewL();
   763 	CleanupStack::PushL(md);
   764 	TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
   765 	TBuf8<128> hash;
   766 	hash = md->Hash(data);
   767 	test(hash.Length() == md->HashSize());
   768 		
   769 	HMACTestsL(md,_L("hmacmd5.dat"), _L("md5"));
   770 	CleanupStack::PopAndDestroy(md);
   771 
   772 	//	Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
   773 	CMD5* testHasher = CMD5::NewL();
   774 	CleanupStack::PushL(testHasher);
   775 	TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
   776 	TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
   777 
   778 	HBufC8* clientData = client.AllocLC();
   779 	HBufC8* serverData = server.AllocLC();
   780 
   781 	Hex(*clientData);
   782 	Hex(*serverData);
   783 	
   784 	testHasher->Hash(*clientData);
   785 	testHasher->Hash(*serverData);
   786 	
   787 	TBuf8<32> md5buf;
   788 	md5buf.Copy(testHasher->Hash(TPtrC8(0,0)));
   789 
   790 	testHasher->Reset();
   791 
   792 //	Now hash in one chunk
   793 	TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
   794 	HBufC8* allData = all.AllocLC();
   795 	Hex(*allData);
   796 
   797 	TBuf8<32> allbuf;
   798 	allbuf = testHasher->Hash(*allData);
   799 	test(allbuf.Compare(md5buf)==0);
   800 	CleanupStack::PopAndDestroy(4, testHasher);
   801 	}
   802 	
   803 	
   804 void MD4TestsL()
   805 	{
   806 	CMD4* md4;
   807 	md4=CMD4::NewL();
   808 	CleanupStack::PushL(md4);
   809 
   810 	FunctionalityTestL(md4);
   811 	
   812 	VectorTestL(md4,_L("md4.dat"));
   813 	
   814 	OOMTestL(md4);
   815 	
   816 	CleanupStack::PopAndDestroy(md4);
   817 
   818     //Copied the tests carried out for other Digest generating algorithms (MD5, SHA1)
   819 	CMD4* md = CMD4::NewL();
   820 	CleanupStack::PushL(md);
   821 	TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
   822 	TBuf8<128> hash;
   823 	hash = md->Hash(data);
   824 	test(hash.Length() == md->HashSize());
   825 		
   826 	HMACTestsL(md,_L("hmacmd4.dat"), _L("md4"));
   827 	CleanupStack::PopAndDestroy(md);
   828 
   829 	CMD4* testHasher = CMD4::NewL();
   830 	CleanupStack::PushL(testHasher);
   831 	TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
   832 	TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
   833 
   834 	HBufC8* clientData = client.AllocLC();
   835 	HBufC8* serverData = server.AllocLC();
   836 
   837 	Hex(*clientData);
   838 	Hex(*serverData);
   839 	
   840 	testHasher->Hash(*clientData);
   841 	testHasher->Hash(*serverData);
   842 	
   843 	TBuf8<32> md4buf;
   844 	md4buf.Copy(testHasher->Hash(TPtrC8(0,0)));
   845 
   846 	testHasher->Reset();
   847 
   848 //	Now hash in one chunk
   849 	TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
   850 	HBufC8* allData = all.AllocLC();
   851 	Hex(*allData);
   852 
   853 	TBuf8<32> allbuf;
   854 	allbuf = testHasher->Hash(*allData);
   855 	test(allbuf.Compare(md4buf)==0);
   856 	CleanupStack::PopAndDestroy(4, testHasher);
   857 	
   858 	//Tests for the Factory Method CMessageDigestFactory
   859  	CMessageDigest* messageDigest = CMessageDigestFactory::NewDigestLC(CMessageDigest::EMD4);
   860  	VectorTestL(messageDigest,_L("md4.dat"));
   861   	CleanupStack::PopAndDestroy(messageDigest);
   862 
   863 	
   864 	}
   865 
   866 void SHA1TestsL()
   867 	{
   868 	CSHA1* sha;
   869 	sha=CSHA1::NewL();
   870 	CleanupStack::PushL(sha);
   871 
   872 	VectorTestL(sha,_L("sha1.dat"));
   873 	
   874 	FunctionalityTestL(sha);
   875 	
   876 	OOMTestL(sha);
   877 		
   878 	CleanupStack::PopAndDestroy(sha);
   879 
   880 	test.Next(_L("Fixed bugs Tests"));	
   881 // Problem reported by Jal Panvel, 17-12-1999.
   882 // Report by email, CSHA1::Hash() returning zero length descriptor
   883 // 
   884 // This was caused by failure to set internal hash descriptor length on setup, this
   885 // problem was present in all hashes except MD2 which set it up correctly.
   886 // Fixed 17-12-1999.
   887 
   888 	sha=CSHA1::NewL();
   889 	CleanupStack::PushL(sha);
   890 	TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
   891 	TBuf8<128> hash;
   892 	hash = sha->Hash(data);
   893 	test(hash.Length() == sha->HashSize());
   894 	sha->Reset();
   895 	
   896 	//	Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
   897 	CSHA1* testHasher = CSHA1::NewL();
   898 	CleanupStack::PushL(testHasher);
   899 	TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
   900 	TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
   901 
   902 	HBufC8* clientData = client.AllocLC();
   903 	HBufC8* serverData = server.AllocLC();
   904 
   905 	Hex(*clientData);
   906 	Hex(*serverData);
   907 
   908 //	Hash in 2 portions
   909 	TBuf8<32> clientbuf;
   910 	TBuf8<32> serverbuf;
   911 	clientbuf = testHasher->Hash(*clientData);
   912 	serverbuf = testHasher->Hash(*serverData);
   913 	
   914 	TBuf8<32> shabuf;
   915 	shabuf.Copy(testHasher->Hash(TPtrC8(0,0)));
   916 	testHasher->Reset();
   917 
   918 //	Now hash in one chunk
   919 	TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
   920 	HBufC8* allData = all.AllocLC();
   921 	Hex(*allData);
   922 
   923 	TBuf8<32> allbuf;
   924 	allbuf = testHasher->Hash(*allData);
   925 
   926 	test(allbuf.Compare(shabuf)==0);
   927 	CleanupStack::PopAndDestroy(4, testHasher);
   928 	
   929 	// Test hashing non-word aligned data - used to crash on arm
   930 	TPtrC8 nonAlignedData = data.Mid(1);
   931 	hash = sha->Final(nonAlignedData);
   932 	test(hash.Length() == sha->HashSize());
   933 	sha->Reset();
   934 	
   935 //	Test end
   936 	HMACTestsL(sha,_L("hmacsha1.dat"), _L("sha1"));
   937 	CleanupStack::PopAndDestroy();		// sha
   938 	}
   939 
   940 void ExecuteSha2TestsL(CMessageDigest* aMD, const TDesC& aVector, const TDesC& aHMACVector, const TDesC& aHashType)
   941 	{
   942 	VectorTestL(aMD, aVector);
   943 	FunctionalityTestL(aMD);
   944 	OOMTestL(aMD);
   945 	aMD->Reset();
   946 
   947 	test.Next(_L("Fixed bugs Tests"));	
   948 
   949 	_LIT8(KTest1Data, "The quick brown fox jumped over the lazy dog");
   950 	TBuf8<KMaxHashSize> hash;
   951 	hash = aMD->Hash(KTest1Data());
   952 	test(hash.Length() == aMD->HashSize());
   953 	aMD->Reset();
   954 	
   955 	_LIT8(KClientData, "D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4");
   956 	_LIT8(KServerData, "3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8");
   957 	
   958 	HBufC8* clientData = KClientData().AllocLC();
   959 	HBufC8* serverData = KServerData().AllocLC();
   960 
   961 	Hex(*clientData);
   962 	Hex(*serverData);
   963 	
   964 //	Hash in 2 portions
   965 	aMD->Hash(*clientData);
   966 	aMD->Hash(*serverData);
   967 
   968 	CleanupStack::PopAndDestroy(2, clientData);
   969 
   970 	TBuf8<KMaxHashSize> shabuf;
   971 	shabuf.Copy(aMD->Hash(KNullDesC8()));
   972 	aMD->Reset();
   973 
   974 //	Now hash in one chunk
   975 	_LIT8(KAllData, "D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8");
   976 	HBufC8* allData = KAllData().AllocLC();
   977 	Hex(*allData);
   978 
   979 	TBuf8<KMaxHashSize> allbuf;
   980 	allbuf = aMD->Hash(*allData);
   981 	
   982 	test(allbuf.Compare(shabuf)==0);
   983 	CleanupStack::PopAndDestroy(allData);
   984 	
   985 	// Test hashing non-word aligned data - used to crash on arm
   986 	TPtrC8 nonAlignedData = KTest1Data().Mid(1);
   987 	hash = aMD->Final(nonAlignedData);
   988 	test(hash.Length() == aMD->HashSize());
   989 	aMD->Reset();
   990 	
   991 //	Test end
   992 	HMACTestsL(aMD, aHMACVector, aHashType);
   993 	}
   994 
   995 _LIT(K224Algo, "SHA-224.dat");
   996 _LIT(K256Algo, "SHA-256.dat");
   997 _LIT(K384Algo, "SHA-384.dat");
   998 _LIT(K512Algo, "SHA-512.dat");
   999 _LIT(K224Vector, "sha224.dat");
  1000 _LIT(K256Vector, "sha256.dat");
  1001 _LIT(K384Vector, "sha384.dat");
  1002 _LIT(K512Vector, "sha512.dat");
  1003 _LIT(K224HmacVector, "hmacsha224.dat");
  1004 _LIT(K256HmacVector, "hmacsha256.dat");
  1005 _LIT(K384HmacVector, "hmacsha384.dat");
  1006 _LIT(K512HmacVector, "hmacsha512.dat");
  1007 
  1008 const TDesC* gNames[] = 
  1009 	{
  1010 	&K224Vector(),
  1011 	&K224HmacVector(),
  1012 	&K224Algo(),
  1013 	&K256Vector(),
  1014 	&K256HmacVector(),
  1015 	&K256Algo(),
  1016 	&K384Vector(),
  1017 	&K384HmacVector(),
  1018 	&K384Algo(),
  1019 	&K512Vector(),
  1020 	&K512HmacVector(),
  1021 	&K512Algo(),
  1022 	};
  1023 
  1024 
  1025 void SHA2TestsL(CMessageDigest::THashId aHashId)
  1026 	{
  1027 	CMessageDigest* md = CMessageDigestFactory::NewDigestLC(aHashId);
  1028 	TInt pos = aHashId - CMessageDigest::ESHA224;
  1029 	pos *= 3;
  1030 	ExecuteSha2TestsL (md, *gNames[pos], *gNames[pos+1], *gNames[pos+2]);
  1031 	CleanupStack::PopAndDestroy(md);
  1032 	}
  1033 
  1034 void HashTests(void)
  1035 	{
  1036 	TInt32 testsFailed=0, testCount=0;
  1037 
  1038 	test.Start(_L("MD4 Tests"));
  1039 	TRAPD(r, MD4TestsL());
  1040   ++testCount;
  1041 	if (r!=KErrNone)
  1042 		{
  1043 		test.Printf(_L("\r\nMD4 Tests failed error code =%d\r\n\r\n"),r);
  1044 		++testsFailed;
  1045 		}	
  1046 
  1047 	test.Start(_L("SHA1 Tests"));
  1048 	TRAP(r, SHA1TestsL());
  1049 	++testCount;
  1050 	if (r!=KErrNone)
  1051 		{
  1052 		test.Printf(_L("\r\nSHA1 Tests failed error code =%d\r\n\r\n"),r);
  1053 		++testsFailed;
  1054 		}
  1055 
  1056 	test.Start(_L("SHA-224 Tests"));
  1057 	TRAP(r, SHA2TestsL(CMessageDigest::ESHA224));
  1058 	++testCount;
  1059 	if (r!=KErrNone)
  1060 		{
  1061 		test.Printf(_L("\r\nSHA-224 Tests failed error code =%d\r\n\r\n"),r);
  1062 		++testsFailed;
  1063 		}
  1064 
  1065 	test.Start(_L("SHA-256 Tests"));	
  1066 	TRAP(r, SHA2TestsL(CMessageDigest::ESHA256));
  1067 	++testCount;
  1068 	if (r!=KErrNone)
  1069 		{
  1070 		test.Printf(_L("\r\nSHA-256 Tests failed error code =%d\r\n\r\n"),r);
  1071 		++testsFailed;
  1072 		}
  1073 
  1074 	test.Start(_L("SHA-384 Tests"));	
  1075 	TRAP(r, SHA2TestsL(CMessageDigest::ESHA384));
  1076 	++testCount;
  1077 	if (r!=KErrNone)
  1078 		{
  1079 		test.Printf(_L("\r\nSHA-384 Tests failed error code =%d\r\n\r\n"),r);
  1080 		++testsFailed;
  1081 		}
  1082 
  1083 	test.Start(_L("SHA-512 Tests"));	
  1084 	TRAP(r, SHA2TestsL(CMessageDigest::ESHA512));
  1085 	++testCount;
  1086 	if (r!=KErrNone)
  1087 		{
  1088 		test.Printf(_L("\r\nSHA-512 Tests failed error code =%d\r\n\r\n"),r);
  1089 		++testsFailed;
  1090 		}
  1091 		
  1092 	test.Start(_L("MD5 Tests"));
  1093 	TRAP(r, MD5TestsL());
  1094   ++testCount;
  1095  	if (r!=KErrNone)
  1096 		{
  1097 		test.Printf(_L("\r\nMD5 Tests failed error code =%d\r\n\r\n"),r);
  1098 		++testsFailed;
  1099 		}
  1100 
  1101 	test.Start(_L("MD2 Tests"));
  1102 	TRAP(r, MD2TestsL());
  1103   ++testCount;
  1104  	if (r!=KErrNone)
  1105 		{
  1106 		test.Printf(_L("\r\nMD2 Tests failed error code =%d\r\n\r\n"),r);
  1107 		++testsFailed;
  1108 		}
  1109 
  1110   test.Printf(_L("\r\n%d tests failed out of %d \r\n"),testsFailed,testCount);
  1111 	test(testsFailed==0);
  1112 	}
  1113 
  1114 
  1115 GLDEF_C TInt E32Main(void)
  1116 
  1117 	{
  1118 	CTrapCleanup* cleanup;
  1119 	cleanup=CTrapCleanup::New();
  1120 		
  1121 	test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-HASH-0035 Hash Algorithm Tests "));		 
  1122 	CTestConsole* con=NULL;
  1123 	TRAPD(ret, con=CTestConsole::NewL(test.Console()));
  1124 	if(ret != KErrNone)
  1125 		{
  1126 		return ret;	
  1127 		}
  1128 	RFs fs;
  1129 
  1130 	fs.Connect();
  1131 	RFile* file;
  1132 	file=new (ELeave) RFile;
  1133 	
  1134 	TDriveUnit sysDrive (fs.GetSystemDrive());
  1135 	TBuf<24> hashLogFile (sysDrive.Name());
  1136 	hashLogFile.Append(_L("\\HashLog.txt"));
  1137 	
  1138 	file->Replace(fs,hashLogFile,EFileShareAny|EFileWrite);
  1139 	con->SetLogFile(file);
  1140 	
  1141 	test.SetConsole(con);
  1142 	__UHEAP_MARK;
  1143 	 HashTests();		
  1144 	__UHEAP_MARKEND;
  1145 
  1146 	test.End();
  1147 	test.Close();
  1148 	delete cleanup;
  1149 	return(KErrNone);
  1150 	}