kerneltest/e32test/random/t_sha256.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Fri, 12 Mar 2010 15:50:11 +0200
branchRCL_3
changeset 80 597aaf25e343
permissions -rw-r--r--
Revision: 201008 Kit: 201008
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
80
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     1
// Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     2
// All rights reserved.
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     3
// This component and the accompanying materials are made available
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     4
// under the terms of the License "Eclipse Public License v1.0"
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     5
// which accompanies this distribution, and is available
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     7
//
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     8
// Initial Contributors:
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
     9
// Nokia Corporation - initial contribution.
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    10
//
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    11
// Contributors:
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    12
//
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    13
// Description: e32test/random/tsha256.cpp
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    14
//
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    15
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    16
//---------------------------------------------------------------------------------------------------------------------
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    17
//! @SYMTestCaseID				KBASE-sha256-2701
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    18
//! @SYMTestType				UT
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    19
//! @SYMTestCaseDesc			Verifies the implementation of SHA256 used by the Secure RNG
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    20
//! @SYMPREQ					PREQ211
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    21
//! @SYMTestPriority			High
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    22
//! @SYMTestActions				
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    23
//! 	1. 	Tests the correct operation of the SHA256 implementation used by the Secure RNG using publically published
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    24
//!         test vectors and corresponding outputs.
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    25
//! 
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    26
//! @SYMTestExpectedResults
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    27
//! 	1.	The implementation should always return the expected outputs for the given test vectors.
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    28
//---------------------------------------------------------------------------------------------------------------------
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    29
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    30
//epoc include
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    31
#include <e32test.h>
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    32
//user include
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    33
#include "sha256.h"
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    34
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    35
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    36
//RTest for testing SHA256
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    37
RTest test(_L("Unit Test For SHA256"));
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    38
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    39
//Test data input for SHA256 taken from FIPS 180-2 and openssl
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    40
_LIT8(KTestData1, "\x61\x62\x63");
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    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");
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    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");
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    43
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    44
//Expected output for the above test data input for SHA256 taken from FIPS 180-2
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    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");
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    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");
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    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");
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    48
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    49
/*
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    50
Functionality test for SHA256 algorithm
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    51
*/
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    52
void Sha256FunctionalityTest(const TDesC8& aMessageData, const TDesC8& aHashOfMessageData)
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    53
	{
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    54
	TBuf8<KSHA256HashSize> hash; // temp buffer
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    55
	SHA256 sha256; 
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    56
	sha256.Update(aMessageData.Ptr(), aMessageData.Length());
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    57
	hash.Copy(sha256.Final().Ptr(),KSHA256HashSize);
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    58
	TInt compareVal = aHashOfMessageData.Compare(hash);
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    59
	test(compareVal == 0);
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    60
	}
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    61
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    62
/*
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    63
Basic functionality test for Sha256
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    64
*/
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    65
void SHA2Tests()
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    66
	{
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    67
	//functionality test for Hash Algorithm using short message data (3 bytes in length)
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    68
	Sha256FunctionalityTest(KTestData1(), KTestVector1());
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    69
	
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    70
	//functionality test for Hash Algorithm using sha256 block size message data (64 bytes)
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    71
	Sha256FunctionalityTest(KTestData2(), KTestVector2());
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    72
	
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    73
	//functionality test for Hash Algorithm using long message data (68 bytes)
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    74
	Sha256FunctionalityTest(KTestData3(), KTestVector3());
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    75
	}
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    76
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    77
/*
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    78
Main function for sha256 algorithm testing
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    79
*/
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    80
GLDEF_C TInt E32Main(void)
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    81
	{
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    82
	test.Title();
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    83
	test.Start(_L(" SHA256 Algorithm Test \n"));		 
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    84
		
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    85
	CTrapCleanup* cleanup=CTrapCleanup::New();
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    86
	test(cleanup != NULL);
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    87
	        
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    88
	__UHEAP_MARK;
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    89
	SHA2Tests();		
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    90
	__UHEAP_MARKEND;
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    91
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    92
	test.End();
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    93
	delete cleanup;
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    94
	return KErrNone;
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    95
	}
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    96
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    97
597aaf25e343 Revision: 201008
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
diff changeset
    98