kerneltest/e32test/random/t_sha256.cpp
branchRCL_3
changeset 80 597aaf25e343
equal deleted inserted replaced
62:4a8fed1c0ef6 80:597aaf25e343
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description: e32test/random/tsha256.cpp
       
    14 //
       
    15 
       
    16 //---------------------------------------------------------------------------------------------------------------------
       
    17 //! @SYMTestCaseID				KBASE-sha256-2701
       
    18 //! @SYMTestType				UT
       
    19 //! @SYMTestCaseDesc			Verifies the implementation of SHA256 used by the Secure RNG
       
    20 //! @SYMPREQ					PREQ211
       
    21 //! @SYMTestPriority			High
       
    22 //! @SYMTestActions				
       
    23 //! 	1. 	Tests the correct operation of the SHA256 implementation used by the Secure RNG using publically published
       
    24 //!         test vectors and corresponding outputs.
       
    25 //! 
       
    26 //! @SYMTestExpectedResults
       
    27 //! 	1.	The implementation should always return the expected outputs for the given test vectors.
       
    28 //---------------------------------------------------------------------------------------------------------------------
       
    29 
       
    30 //epoc include
       
    31 #include <e32test.h>
       
    32 //user include
       
    33 #include "sha256.h"
       
    34 
       
    35 
       
    36 //RTest for testing SHA256
       
    37 RTest test(_L("Unit Test For SHA256"));
       
    38 
       
    39 //Test data input for SHA256 taken from FIPS 180-2 and openssl
       
    40 _LIT8(KTestData1, "\x61\x62\x63");
       
    41 _LIT8(KTestData2, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10");
       
    42 _LIT8(KTestData3, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x01\x02\x03\x04");
       
    43 
       
    44 //Expected output for the above test data input for SHA256 taken from FIPS 180-2
       
    45 _LIT8(KTestVector1, "\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad");
       
    46 _LIT8(KTestVector2, "\x91\x1B\x64\x76\x69\x49\xA2\xE8\x56\xF1\xB6\xC3\x50\x1D\x5A\x6B\xF1\x7D\xD5\x0B\x6A\x78\xD6\x09\x3A\xFC\x42\x52\xD2\xF7\x1A\x18");
       
    47 _LIT8(KTestVector3, "\x27\x53\x57\xF9\x38\x73\xAF\xFF\xF0\x0C\x4A\x83\x04\x33\xCA\x51\x37\xCC\x32\x7D\xDF\xB1\x5C\x46\xD6\xCD\x8A\x0A\x8A\x6E\x48\x3C");
       
    48 
       
    49 /*
       
    50 Functionality test for SHA256 algorithm
       
    51 */
       
    52 void Sha256FunctionalityTest(const TDesC8& aMessageData, const TDesC8& aHashOfMessageData)
       
    53 	{
       
    54 	TBuf8<KSHA256HashSize> hash; // temp buffer
       
    55 	SHA256 sha256; 
       
    56 	sha256.Update(aMessageData.Ptr(), aMessageData.Length());
       
    57 	hash.Copy(sha256.Final().Ptr(),KSHA256HashSize);
       
    58 	TInt compareVal = aHashOfMessageData.Compare(hash);
       
    59 	test(compareVal == 0);
       
    60 	}
       
    61 
       
    62 /*
       
    63 Basic functionality test for Sha256
       
    64 */
       
    65 void SHA2Tests()
       
    66 	{
       
    67 	//functionality test for Hash Algorithm using short message data (3 bytes in length)
       
    68 	Sha256FunctionalityTest(KTestData1(), KTestVector1());
       
    69 	
       
    70 	//functionality test for Hash Algorithm using sha256 block size message data (64 bytes)
       
    71 	Sha256FunctionalityTest(KTestData2(), KTestVector2());
       
    72 	
       
    73 	//functionality test for Hash Algorithm using long message data (68 bytes)
       
    74 	Sha256FunctionalityTest(KTestData3(), KTestVector3());
       
    75 	}
       
    76 
       
    77 /*
       
    78 Main function for sha256 algorithm testing
       
    79 */
       
    80 GLDEF_C TInt E32Main(void)
       
    81 	{
       
    82 	test.Title();
       
    83 	test.Start(_L(" SHA256 Algorithm Test \n"));		 
       
    84 		
       
    85 	CTrapCleanup* cleanup=CTrapCleanup::New();
       
    86 	test(cleanup != NULL);
       
    87 	        
       
    88 	__UHEAP_MARK;
       
    89 	SHA2Tests();		
       
    90 	__UHEAP_MARKEND;
       
    91 
       
    92 	test.End();
       
    93 	delete cleanup;
       
    94 	return KErrNone;
       
    95 	}
       
    96 
       
    97 
       
    98