Update contrib.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
21 #include <pkcs12kdf.h>
22 #include "t_testhandler.h"
23 #include "t_testsetup.h"
24 #include "tscripttests.h"
25 #include "tactionderivekey.h"
27 LOCAL_D void callExampleL() // initialize and call example code under cleanup stack
30 SCRIPT_ITEM(CActionDeriveKey,_L8("DeriveKey"))
33 TDriveUnit sysDrive (RFs::GetSystemDrive());
34 TDriveName sysDriveName (sysDrive.Name());
35 TBuf<64> scriptFile (sysDriveName);
36 scriptFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.txt"));
38 TBuf<64> logFile (sysDriveName);
39 logFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.log"));
41 CTestSetup::CreateAndRunTestsL(theTestTypes, scriptFile, logFile);
45 GLDEF_C TInt E32Main() // main function called by E32
48 CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack
50 TRAPD(error, callExampleL());
51 __ASSERT_ALWAYS(!error,User::Panic(_L("tpkcs5"),error));
52 delete cleanup; // destroy clean-up stack
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.
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);
69 static void TestPkcs12KdfL()
73 const TInt KKeyLenCount = 4;
74 const TInt KKeyLens[KKeyLenCount] = {40, 128, 168, 368};
76 const TInt KIterCount = 8;
77 const TInt KIterCounts[KIterCount] = {1, 2, 4, 8, 128, 1024, 1536, 2048};
79 const TInt KSaltCount = 3;
80 TPtrC8 salts[KSaltCount];
82 const TUint8 KSalt4[4] = {0x53, 0x41, 0x4c, 0x54};
83 salts[0].Set(KSalt4, 4);
85 const TUint8 KSalt20[20] =
87 0x1d, 0x56, 0x50, 0x78, 0xc3, 0x50, 0x6f, 0x89,
88 0xbd, 0xa7, 0x3b, 0xb6, 0xe3, 0xe5, 0xb8, 0xa3,
89 0x68, 0x3d, 0xd3, 0x62
91 salts[1].Set(KSalt20, 20);
93 const TUint8 KSalt25[25] =
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,
100 salts[2].Set(KSalt25, 25);
102 const TInt KPasswordCount = 5;
103 HBufC8* passwords[KPasswordCount];
104 TPtrC passwords0[KPasswordCount] =
106 _L("0000"), _L("0001"), _L("PSWD"),
107 _L("password"), _L("abcdefghijklmnopqrstuvwxyz")
109 for (TInt i = 0; i < KPasswordCount; ++i)
110 passwords[i] = PKCS12KDF::GeneratePasswordLC(passwords0[i]);
112 for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
114 for (TInt iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
116 for (TInt saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
118 for (TInt pwdIdx = 0; pwdIdx < KPasswordCount; ++pwdIdx)
120 TestPasswordL(KKeyLens[klenIdx], KIterCounts[iterIdx], salts[saltIdx], *passwords[pwdIdx], passwords0[pwdIdx]);
122 } // for (saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
123 } // for (int iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
124 } // for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
126 CleanupStack::PopAndDestroy(KPasswordCount, passwords[0]);
131 static void TestPasswordL(
132 TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
133 const TDesC8& aPassword, const TDesC& aPasswordText)
138 key.SetLength(aKeyLenInBits / 8);
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);
149 static void PrintKey(const TDesC8& aKey)
151 Print the supplied key in hex byte format, with
152 16 bytes on each row.
154 @param aKey Key to print.
157 const TInt KBlockSize = 16;
158 TInt keyLen = aKey.Length();
159 TInt rowCount = keyLen / KBlockSize;
160 if ((keyLen % KBlockSize) != 0)
163 for (TInt row = 0; row < rowCount; ++row)
165 TInt start = row * KBlockSize;
166 TInt end = Min(start + KBlockSize, keyLen);
169 line.Format(_L("[%04x]"), start);
170 for (TInt i = start; i < end; ++i)
171 line.AppendFormat(_L(" %02x"), aKey[i]);