crypto/weakcrypto/test/tpkcs5kdf/tpkcs5kdfmain.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     1 /*
       
     2 * Copyright (c) 1998-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 * tpkcs5main.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <pkcs5kdf.h>
       
    21 #include <pkcs12kdf.h>
       
    22 #include "t_testhandler.h"
       
    23 #include "t_testsetup.h"
       
    24 #include "tscripttests.h"
       
    25 #include "tactionderivekey.h"
       
    26 
       
    27 LOCAL_D void callExampleL() // initialize and call example code under cleanup stack
       
    28     {
       
    29     START_SCRIPT_LIST
       
    30 	SCRIPT_ITEM(CActionDeriveKey,_L8("DeriveKey"))
       
    31 	END_SCRIPT_LIST
       
    32 	
       
    33 	TDriveUnit sysDrive (RFs::GetSystemDrive());
       
    34 	TDriveName sysDriveName (sysDrive.Name());
       
    35 	TBuf<64> scriptFile (sysDriveName);
       
    36 	scriptFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.txt"));
       
    37 	
       
    38 	TBuf<64> logFile (sysDriveName);
       
    39 	logFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.log"));
       
    40 	
       
    41 	CTestSetup::CreateAndRunTestsL(theTestTypes, scriptFile, logFile);
       
    42     }
       
    43 
       
    44 
       
    45 GLDEF_C TInt E32Main() // main function called by E32
       
    46     {
       
    47 	__UHEAP_MARK;
       
    48 	CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack
       
    49 	
       
    50 	TRAPD(error, callExampleL());
       
    51 	__ASSERT_ALWAYS(!error,User::Panic(_L("tpkcs5"),error));
       
    52 	delete cleanup; // destroy clean-up stack
       
    53 	__UHEAP_MARKEND;
       
    54 	return 0;
       
    55     }
       
    56 
       
    57 // The following code was used to generate test data.  It is left in
       
    58 // the source file because it provides an easy way to generate a large
       
    59 // number of PKCS#12 derived keys if required.
       
    60 
       
    61 #if 0
       
    62 
       
    63 static void TestPkcs12KdfL();
       
    64 static void TestPasswordL(
       
    65 	TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
       
    66 	const TDesC8& aPassword, const TDesC& aPasswordText);
       
    67 static void PrintKey(const TDesC8& aKey);
       
    68 
       
    69 static void TestPkcs12KdfL()
       
    70 	{
       
    71 	__UHEAP_MARK;
       
    72 	
       
    73 	const TInt KKeyLenCount = 4;
       
    74 	const TInt KKeyLens[KKeyLenCount] = {40, 128, 168, 368};
       
    75 	
       
    76 	const TInt KIterCount = 8;
       
    77 	const TInt KIterCounts[KIterCount] = {1, 2, 4, 8, 128, 1024, 1536, 2048};
       
    78 	
       
    79 	const TInt KSaltCount = 3;
       
    80 	TPtrC8 salts[KSaltCount];
       
    81 	
       
    82 	const TUint8 KSalt4[4] = {0x53, 0x41, 0x4c, 0x54};
       
    83 	salts[0].Set(KSalt4, 4);
       
    84 	
       
    85 	const TUint8 KSalt20[20] =
       
    86 		{
       
    87 		0x1d, 0x56, 0x50, 0x78,		0xc3, 0x50, 0x6f, 0x89,
       
    88 		0xbd, 0xa7, 0x3b, 0xb6,		0xe3, 0xe5, 0xb8, 0xa3,
       
    89 		0x68, 0x3d, 0xd3, 0x62
       
    90 		};
       
    91 	salts[1].Set(KSalt20, 20);
       
    92 	
       
    93 	const TUint8 KSalt25[25] =
       
    94 		{
       
    95 		0xe2, 0x2c, 0x7b, 0x03,		0x16, 0x3a, 0xe5, 0x47,
       
    96 		0xf8, 0x23, 0x9d, 0xa4,		0x0d, 0x6f, 0x46, 0xd7,
       
    97 		0x9e, 0xa3, 0xc6, 0xff,		0xb3, 0xf0, 0x4e, 0xbe,
       
    98 		0x61
       
    99 		};
       
   100 	salts[2].Set(KSalt25, 25);
       
   101 	
       
   102 	const TInt KPasswordCount = 5;
       
   103 	HBufC8* passwords[KPasswordCount];
       
   104 	TPtrC passwords0[KPasswordCount] =
       
   105 		{
       
   106 		_L("0000"), _L("0001"), _L("PSWD"),
       
   107 		_L("password"), _L("abcdefghijklmnopqrstuvwxyz")
       
   108 		};
       
   109 	for (TInt i = 0; i < KPasswordCount; ++i)
       
   110 		passwords[i] = PKCS12KDF::GeneratePasswordLC(passwords0[i]);
       
   111 
       
   112 	for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
       
   113 		{
       
   114 		for (TInt iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
       
   115 			{
       
   116 			for (TInt saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
       
   117 				{
       
   118 				for (TInt pwdIdx = 0; pwdIdx < KPasswordCount; ++pwdIdx)
       
   119 					{
       
   120 					TestPasswordL(KKeyLens[klenIdx], KIterCounts[iterIdx], salts[saltIdx], *passwords[pwdIdx], passwords0[pwdIdx]);
       
   121 					}
       
   122 				}	// for (saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
       
   123 			}	// for (int iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
       
   124 		}	// for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
       
   125 	
       
   126 	CleanupStack::PopAndDestroy(KPasswordCount, passwords[0]);
       
   127 	
       
   128 	__UHEAP_MARKEND;
       
   129 	}
       
   130 
       
   131 static void TestPasswordL(
       
   132 	TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
       
   133 	const TDesC8& aPassword, const TDesC& aPasswordText)
       
   134 	{
       
   135 	__UHEAP_MARK;
       
   136 	
       
   137 	TBuf8<512> key;
       
   138 	key.SetLength(aKeyLenInBits / 8);
       
   139 
       
   140 	RDebug::Print(
       
   141 		_L("\nkey len = %d, iter count = %d, password = \"%S\", salt len = %d"),
       
   142 		aKeyLenInBits, aIterCount, &aPasswordText, aSalt.Length());
       
   143 	PKCS12KDF::DeriveKeyL(key, PKCS12KDF::EIDByteEncryptKey, aPassword, aSalt, aIterCount);
       
   144 	PrintKey(key);
       
   145 	
       
   146 	__UHEAP_MARKEND;
       
   147 	}
       
   148 
       
   149 static void PrintKey(const TDesC8& aKey)
       
   150 /**
       
   151 	Print the supplied key in hex byte format, with
       
   152 	16 bytes on each row.
       
   153 	
       
   154 	@param	aKey			Key to print.
       
   155  */
       
   156 	{
       
   157 	const TInt KBlockSize = 16;
       
   158 	TInt keyLen = aKey.Length();
       
   159 	TInt rowCount = keyLen / KBlockSize;
       
   160 	if ((keyLen % KBlockSize) != 0)
       
   161 		++rowCount;
       
   162 
       
   163 	for (TInt row = 0; row < rowCount; ++row)
       
   164 		{
       
   165 		TInt start = row * KBlockSize;
       
   166 		TInt end = Min(start + KBlockSize, keyLen);
       
   167 
       
   168 		TBuf<64> line;
       
   169 		line.Format(_L("[%04x]"), start);
       
   170 		for (TInt i = start; i < end; ++i)
       
   171 			line.AppendFormat(_L(" %02x"), aKey[i]);
       
   172 		line.Append('\n');
       
   173 		RDebug::Print(line);
       
   174 		}
       
   175 	}
       
   176 
       
   177 #endif	// #if 0
       
   178