1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tpkcs5kdf/tpkcs5kdfmain.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,178 @@
1.4 +/*
1.5 +* Copyright (c) 1998-2010 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* tpkcs5main.cpp
1.19 +*
1.20 +*/
1.21 +
1.22 +
1.23 +#include <pkcs5kdf.h>
1.24 +#include <pkcs12kdf.h>
1.25 +#include "t_testhandler.h"
1.26 +#include "t_testsetup.h"
1.27 +#include "tScriptTests.h"
1.28 +#include "tactionderivekey.h"
1.29 +
1.30 +LOCAL_D void callExampleL() // initialize and call example code under cleanup stack
1.31 + {
1.32 + START_SCRIPT_LIST
1.33 + SCRIPT_ITEM(CActionDeriveKey,_L8("DeriveKey"))
1.34 + END_SCRIPT_LIST
1.35 +
1.36 + TDriveUnit sysDrive (RFs::GetSystemDrive());
1.37 + TDriveName sysDriveName (sysDrive.Name());
1.38 + TBuf<64> scriptFile (sysDriveName);
1.39 + scriptFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.txt"));
1.40 +
1.41 + TBuf<64> logFile (sysDriveName);
1.42 + logFile.Append(_L("\\tpkcs5kdf\\tpkcs12kdftests.log"));
1.43 +
1.44 + CTestSetup::CreateAndRunTestsL(theTestTypes, scriptFile, logFile);
1.45 + }
1.46 +
1.47 +
1.48 +GLDEF_C TInt E32Main() // main function called by E32
1.49 + {
1.50 + __UHEAP_MARK;
1.51 + CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack
1.52 +
1.53 + TRAPD(error, callExampleL());
1.54 + __ASSERT_ALWAYS(!error,User::Panic(_L("tpkcs5"),error));
1.55 + delete cleanup; // destroy clean-up stack
1.56 + __UHEAP_MARKEND;
1.57 + return 0;
1.58 + }
1.59 +
1.60 +// The following code was used to generate test data. It is left in
1.61 +// the source file because it provides an easy way to generate a large
1.62 +// number of PKCS#12 derived keys if required.
1.63 +
1.64 +#if 0
1.65 +
1.66 +static void TestPkcs12KdfL();
1.67 +static void TestPasswordL(
1.68 + TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
1.69 + const TDesC8& aPassword, const TDesC& aPasswordText);
1.70 +static void PrintKey(const TDesC8& aKey);
1.71 +
1.72 +static void TestPkcs12KdfL()
1.73 + {
1.74 + __UHEAP_MARK;
1.75 +
1.76 + const TInt KKeyLenCount = 4;
1.77 + const TInt KKeyLens[KKeyLenCount] = {40, 128, 168, 368};
1.78 +
1.79 + const TInt KIterCount = 8;
1.80 + const TInt KIterCounts[KIterCount] = {1, 2, 4, 8, 128, 1024, 1536, 2048};
1.81 +
1.82 + const TInt KSaltCount = 3;
1.83 + TPtrC8 salts[KSaltCount];
1.84 +
1.85 + const TUint8 KSalt4[4] = {0x53, 0x41, 0x4c, 0x54};
1.86 + salts[0].Set(KSalt4, 4);
1.87 +
1.88 + const TUint8 KSalt20[20] =
1.89 + {
1.90 + 0x1d, 0x56, 0x50, 0x78, 0xc3, 0x50, 0x6f, 0x89,
1.91 + 0xbd, 0xa7, 0x3b, 0xb6, 0xe3, 0xe5, 0xb8, 0xa3,
1.92 + 0x68, 0x3d, 0xd3, 0x62
1.93 + };
1.94 + salts[1].Set(KSalt20, 20);
1.95 +
1.96 + const TUint8 KSalt25[25] =
1.97 + {
1.98 + 0xe2, 0x2c, 0x7b, 0x03, 0x16, 0x3a, 0xe5, 0x47,
1.99 + 0xf8, 0x23, 0x9d, 0xa4, 0x0d, 0x6f, 0x46, 0xd7,
1.100 + 0x9e, 0xa3, 0xc6, 0xff, 0xb3, 0xf0, 0x4e, 0xbe,
1.101 + 0x61
1.102 + };
1.103 + salts[2].Set(KSalt25, 25);
1.104 +
1.105 + const TInt KPasswordCount = 5;
1.106 + HBufC8* passwords[KPasswordCount];
1.107 + TPtrC passwords0[KPasswordCount] =
1.108 + {
1.109 + _L("0000"), _L("0001"), _L("PSWD"),
1.110 + _L("password"), _L("abcdefghijklmnopqrstuvwxyz")
1.111 + };
1.112 + for (TInt i = 0; i < KPasswordCount; ++i)
1.113 + passwords[i] = PKCS12KDF::GeneratePasswordLC(passwords0[i]);
1.114 +
1.115 + for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
1.116 + {
1.117 + for (TInt iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
1.118 + {
1.119 + for (TInt saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
1.120 + {
1.121 + for (TInt pwdIdx = 0; pwdIdx < KPasswordCount; ++pwdIdx)
1.122 + {
1.123 + TestPasswordL(KKeyLens[klenIdx], KIterCounts[iterIdx], salts[saltIdx], *passwords[pwdIdx], passwords0[pwdIdx]);
1.124 + }
1.125 + } // for (saltIdx = 0; saltIdx < KSaltCount; ++saltIdx)
1.126 + } // for (int iterIdx = 0; iterIdx < KIterCount; ++iterIdx)
1.127 + } // for (TInt klenIdx = 0; klenIdx < KKeyLenCount; ++klenIdx)
1.128 +
1.129 + CleanupStack::PopAndDestroy(KPasswordCount, passwords[0]);
1.130 +
1.131 + __UHEAP_MARKEND;
1.132 + }
1.133 +
1.134 +static void TestPasswordL(
1.135 + TInt aKeyLenInBits, TInt aIterCount, const TDesC8& aSalt,
1.136 + const TDesC8& aPassword, const TDesC& aPasswordText)
1.137 + {
1.138 + __UHEAP_MARK;
1.139 +
1.140 + TBuf8<512> key;
1.141 + key.SetLength(aKeyLenInBits / 8);
1.142 +
1.143 + RDebug::Print(
1.144 + _L("\nkey len = %d, iter count = %d, password = \"%S\", salt len = %d"),
1.145 + aKeyLenInBits, aIterCount, &aPasswordText, aSalt.Length());
1.146 + PKCS12KDF::DeriveKeyL(key, PKCS12KDF::EIDByteEncryptKey, aPassword, aSalt, aIterCount);
1.147 + PrintKey(key);
1.148 +
1.149 + __UHEAP_MARKEND;
1.150 + }
1.151 +
1.152 +static void PrintKey(const TDesC8& aKey)
1.153 +/**
1.154 + Print the supplied key in hex byte format, with
1.155 + 16 bytes on each row.
1.156 +
1.157 + @param aKey Key to print.
1.158 + */
1.159 + {
1.160 + const TInt KBlockSize = 16;
1.161 + TInt keyLen = aKey.Length();
1.162 + TInt rowCount = keyLen / KBlockSize;
1.163 + if ((keyLen % KBlockSize) != 0)
1.164 + ++rowCount;
1.165 +
1.166 + for (TInt row = 0; row < rowCount; ++row)
1.167 + {
1.168 + TInt start = row * KBlockSize;
1.169 + TInt end = Min(start + KBlockSize, keyLen);
1.170 +
1.171 + TBuf<64> line;
1.172 + line.Format(_L("[%04x]"), start);
1.173 + for (TInt i = start; i < end; ++i)
1.174 + line.AppendFormat(_L(" %02x"), aKey[i]);
1.175 + line.Append('\n');
1.176 + RDebug::Print(line);
1.177 + }
1.178 + }
1.179 +
1.180 +#endif // #if 0
1.181 +