sl@0: /*
sl@0: * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: * tpkcs5main.cpp
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: #include <pkcs5kdf.h>
sl@0: #include <pkcs12kdf.h>
sl@0: #include "t_testhandler.h"
sl@0: #include "t_testsetup.h"
sl@0: #include "tscripttests.h"
sl@0: #include "tactionderivekey.h"
sl@0: 
sl@0: LOCAL_D void callExampleL() // initialize and call example code under cleanup stack
sl@0:     {
sl@0:     START_SCRIPT_LIST
sl@0: 	SCRIPT_ITEM(CActionDeriveKey,_L8("DeriveKey"))
sl@0: 	END_SCRIPT_LIST
sl@0: 	
sl@0: 	TDriveUnit sysDrive (RFs::GetSystemDrive());
sl@0: 	TDriveName sysDriveName (sysDrive.Name());
sl@0: 	TBuf<64> scriptFile (sysDriveName);
sl@0: 	scriptFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.txt"));
sl@0: 	
sl@0: 	TBuf<64> logFile (sysDriveName);
sl@0: 	logFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.log"));
sl@0: 	
sl@0: 	CTestSetup::CreateAndRunTestsL(theTestTypes, scriptFile, logFile);
sl@0:     }
sl@0: 
sl@0: 
sl@0: GLDEF_C TInt E32Main() // main function called by E32
sl@0:     {
sl@0: 	__UHEAP_MARK;
sl@0: 	CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack
sl@0: 	
sl@0: 	TRAPD(error, callExampleL());
sl@0: 	__ASSERT_ALWAYS(!error,User::Panic(_L("tpkcs5"),error));
sl@0: 	delete cleanup; // destroy clean-up stack
sl@0: 	__UHEAP_MARKEND;
sl@0: 	return 0;
sl@0:     }
sl@0: 
sl@0: // The following code was used to generate test data.  It is left in
sl@0: // the source file because it provides an easy way to generate a large
sl@0: // number of PKCS#12 derived keys if required.
sl@0: 
sl@0: #if 0
sl@0: 
sl@0: static void TestPkcs12KdfL();
sl@0: static void TestPasswordL(
sl@0: 	TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
sl@0: 	const TDesC8& aPassword, const TDesC& aPasswordText);
sl@0: static void PrintKey(const TDesC8& aKey);
sl@0: 
sl@0: static void TestPkcs12KdfL()
sl@0: 	{
sl@0: 	__UHEAP_MARK;
sl@0: 	
sl@0: 	const TInt KKeyLenCount = 4;
sl@0: 	const TInt KKeyLens[KKeyLenCount] = {40, 128, 168, 368};
sl@0: 	
sl@0: 	const TInt KIterCount = 8;
sl@0: 	const TInt KIterCounts[KIterCount] = {1, 2, 4, 8, 128, 1024, 1536, 2048};
sl@0: 	
sl@0: 	const TInt KSaltCount = 3;
sl@0: 	TPtrC8 salts[KSaltCount];
sl@0: 	
sl@0: 	const TUint8 KSalt4[4] = {0x53, 0x41, 0x4c, 0x54};
sl@0: 	salts[0].Set(KSalt4, 4);
sl@0: 	
sl@0: 	const TUint8 KSalt20[20] =
sl@0: 		{
sl@0: 		0x1d, 0x56, 0x50, 0x78,		0xc3, 0x50, 0x6f, 0x89,
sl@0: 		0xbd, 0xa7, 0x3b, 0xb6,		0xe3, 0xe5, 0xb8, 0xa3,
sl@0: 		0x68, 0x3d, 0xd3, 0x62
sl@0: 		};
sl@0: 	salts[1].Set(KSalt20, 20);
sl@0: 	
sl@0: 	const TUint8 KSalt25[25] =
sl@0: 		{
sl@0: 		0xe2, 0x2c, 0x7b, 0x03,		0x16, 0x3a, 0xe5, 0x47,
sl@0: 		0xf8, 0x23, 0x9d, 0xa4,		0x0d, 0x6f, 0x46, 0xd7,
sl@0: 		0x9e, 0xa3, 0xc6, 0xff,		0xb3, 0xf0, 0x4e, 0xbe,
sl@0: 		0x61
sl@0: 		};
sl@0: 	salts[2].Set(KSalt25, 25);
sl@0: 	
sl@0: 	const TInt KPasswordCount = 5;
sl@0: 	HBufC8* passwords[KPasswordCount];
sl@0: 	TPtrC passwords0[KPasswordCount] =
sl@0: 		{
sl@0: 		_L("0000"), _L("0001"), _L("PSWD"),
sl@0: 		_L("password"), _L("abcdefghijklmnopqrstuvwxyz")
sl@0: 		};
sl@0: 	for (TInt i = 0; i < KPasswordCount; ++i)
sl@0: 		passwords[i] = PKCS12KDF::GeneratePasswordLC(passwords0[i]);
sl@0: 
sl@0: 	for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
sl@0: 		{
sl@0: 		for (TInt iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
sl@0: 			{
sl@0: 			for (TInt saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
sl@0: 				{
sl@0: 				for (TInt pwdIdx = 0; pwdIdx < KPasswordCount; ++pwdIdx)
sl@0: 					{
sl@0: 					TestPasswordL(KKeyLens[klenIdx], KIterCounts[iterIdx], salts[saltIdx], *passwords[pwdIdx], passwords0[pwdIdx]);
sl@0: 					}
sl@0: 				}	// for (saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
sl@0: 			}	// for (int iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
sl@0: 		}	// for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
sl@0: 	
sl@0: 	CleanupStack::PopAndDestroy(KPasswordCount, passwords[0]);
sl@0: 	
sl@0: 	__UHEAP_MARKEND;
sl@0: 	}
sl@0: 
sl@0: static void TestPasswordL(
sl@0: 	TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
sl@0: 	const TDesC8& aPassword, const TDesC& aPasswordText)
sl@0: 	{
sl@0: 	__UHEAP_MARK;
sl@0: 	
sl@0: 	TBuf8<512> key;
sl@0: 	key.SetLength(aKeyLenInBits / 8);
sl@0: 
sl@0: 	RDebug::Print(
sl@0: 		_L("\nkey len = %d, iter count = %d, password = \"%S\", salt len = %d"),
sl@0: 		aKeyLenInBits, aIterCount, &aPasswordText, aSalt.Length());
sl@0: 	PKCS12KDF::DeriveKeyL(key, PKCS12KDF::EIDByteEncryptKey, aPassword, aSalt, aIterCount);
sl@0: 	PrintKey(key);
sl@0: 	
sl@0: 	__UHEAP_MARKEND;
sl@0: 	}
sl@0: 
sl@0: static void PrintKey(const TDesC8& aKey)
sl@0: /**
sl@0: 	Print the supplied key in hex byte format, with
sl@0: 	16 bytes on each row.
sl@0: 	
sl@0: 	@param	aKey			Key to print.
sl@0:  */
sl@0: 	{
sl@0: 	const TInt KBlockSize = 16;
sl@0: 	TInt keyLen = aKey.Length();
sl@0: 	TInt rowCount = keyLen / KBlockSize;
sl@0: 	if ((keyLen % KBlockSize) != 0)
sl@0: 		++rowCount;
sl@0: 
sl@0: 	for (TInt row = 0; row < rowCount; ++row)
sl@0: 		{
sl@0: 		TInt start = row * KBlockSize;
sl@0: 		TInt end = Min(start + KBlockSize, keyLen);
sl@0: 
sl@0: 		TBuf<64> line;
sl@0: 		line.Format(_L("[%04x]"), start);
sl@0: 		for (TInt i = start; i < end; ++i)
sl@0: 			line.AppendFormat(_L(" %02x"), aKey[i]);
sl@0: 		line.Append('\n');
sl@0: 		RDebug::Print(line);
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: #endif	// #if 0
sl@0: