os/security/crypto/weakcryptospi/test/tpkcs5kdf/tactionderivekey.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "tactionderivekey.h"
sl@0
    20
#include "t_input.h"
sl@0
    21
#include <pkcs5kdf.h>
sl@0
    22
#include <pkcs12kdf.h>
sl@0
    23
#include <stdlib.h>
sl@0
    24
sl@0
    25
_LIT8(KKdfStart, "<kdf>");
sl@0
    26
_LIT8(KKdfEnd, "</kdf>");
sl@0
    27
_LIT8(KPKCS12Kdf, "pkcs#12");
sl@0
    28
_LIT8(KDeriveKeyStart, "<derivekey>");
sl@0
    29
_LIT8(KDeriveKeyEnd, "</derivekey>");
sl@0
    30
_LIT8(KKeyStart, "<key>");
sl@0
    31
_LIT8(KKeyEnd, "</key>");
sl@0
    32
_LIT8(KPasswdStart, "<passwd>");
sl@0
    33
_LIT8(KPasswdEnd, "</passwd>");
sl@0
    34
_LIT8(KSaltStart, "<salt>");
sl@0
    35
_LIT8(KSaltEnd, "</salt>");
sl@0
    36
_LIT8(KIterationsStart, "<iterations>");
sl@0
    37
_LIT8(KIterationsEnd, "</iterations>");
sl@0
    38
_LIT8(KLeaveInPerformAction, "<leaveinperformaction>");
sl@0
    39
_LIT8(KLeaveInPerformActionEnd, "</leaveinperformaction>");
sl@0
    40
sl@0
    41
CTestAction* CActionDeriveKey::NewL(RFs& aFs,
sl@0
    42
									   CConsoleBase& aConsole,
sl@0
    43
									   Output& aOut, 
sl@0
    44
									   const TTestActionSpec& aTestActionSpec)
sl@0
    45
	{
sl@0
    46
	CTestAction* self = CActionDeriveKey::NewLC(aFs, aConsole,
sl@0
    47
		aOut, aTestActionSpec);
sl@0
    48
	CleanupStack::Pop();
sl@0
    49
	return self;
sl@0
    50
	}
sl@0
    51
sl@0
    52
CTestAction* CActionDeriveKey::NewLC(RFs& aFs,
sl@0
    53
										CConsoleBase& aConsole,
sl@0
    54
										Output& aOut, 
sl@0
    55
										const TTestActionSpec& aTestActionSpec)
sl@0
    56
	{
sl@0
    57
	CActionDeriveKey* self = new(ELeave) CActionDeriveKey(aFs, aConsole, aOut);
sl@0
    58
	CleanupStack::PushL(self);
sl@0
    59
	self->ConstructL(aTestActionSpec);
sl@0
    60
	return self;
sl@0
    61
	}
sl@0
    62
sl@0
    63
CActionDeriveKey::~CActionDeriveKey()
sl@0
    64
	{
sl@0
    65
	delete iBody;
sl@0
    66
	}
sl@0
    67
sl@0
    68
CActionDeriveKey::CActionDeriveKey(RFs& aFs, 
sl@0
    69
								 CConsoleBase& aConsole,
sl@0
    70
								 Output& aOut)
sl@0
    71
								 
sl@0
    72
: CTestAction(aConsole, aOut), iFs(aFs)
sl@0
    73
	{
sl@0
    74
	}
sl@0
    75
sl@0
    76
void CActionDeriveKey::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    77
	{
sl@0
    78
	CTestAction::ConstructL(aTestActionSpec);
sl@0
    79
	iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
sl@0
    80
	iBody->Des().Copy(aTestActionSpec.iActionBody);
sl@0
    81
	}
sl@0
    82
sl@0
    83
void CActionDeriveKey::DoPerformPrerequisite(TRequestStatus& aStatus)
sl@0
    84
	{
sl@0
    85
	TRequestStatus* status = &aStatus;
sl@0
    86
	TInt err = KErrNone;
sl@0
    87
	TInt pos = 0;
sl@0
    88
	TPtrC8 deriveKey = Input::ParseElement(*iBody, KDeriveKeyStart, KDeriveKeyEnd, pos, err);
sl@0
    89
sl@0
    90
	// KDF is only explicitly specified for PKCS#12 derived keys
sl@0
    91
	TPtrC8 kdfTemp = Input::ParseElement(deriveKey, KKdfStart, KKdfEnd, pos=0, err);
sl@0
    92
	if (err == KErrNone)
sl@0
    93
		iKdf = kdfTemp.AllocL();
sl@0
    94
	
sl@0
    95
	TPtrC8 passwdTemp = Input::ParseElement(deriveKey, KPasswdStart, KPasswdEnd, pos=0, err);
sl@0
    96
	iPasswd = HBufC8::NewL(passwdTemp.Length());
sl@0
    97
	*iPasswd = passwdTemp;
sl@0
    98
sl@0
    99
	TPtrC8 iterationsTemp = Input::ParseElement(deriveKey, KIterationsStart, KIterationsEnd, pos=0, err);
sl@0
   100
	iIterations = HBufC8::NewL(iterationsTemp.Length() + 1); //added 1 for the null zero used later
sl@0
   101
	*iIterations = iterationsTemp;
sl@0
   102
sl@0
   103
	TPtrC8 saltTemp = Input::ParseElement(deriveKey, KSaltStart, KSaltEnd, pos=0, err);
sl@0
   104
	iSalt = HBufC8::NewL(saltTemp.Length());
sl@0
   105
	*iSalt = saltTemp;
sl@0
   106
	Hex(*iSalt);
sl@0
   107
sl@0
   108
	TPtrC8 keyTemp = Input::ParseElement(deriveKey, KKeyStart, KKeyEnd, pos=0, err);
sl@0
   109
	iKey = HBufC8::NewL(keyTemp.Length());
sl@0
   110
	*iKey = keyTemp;
sl@0
   111
	Hex(*iKey);
sl@0
   112
sl@0
   113
	iOutput = HBufC8::NewL(iKey->Length());
sl@0
   114
sl@0
   115
	iLeaveInPerformAction = Input::ParseIntElement(deriveKey, 
sl@0
   116
		KLeaveInPerformAction, KLeaveInPerformActionEnd, pos=0, err);
sl@0
   117
	if (err)
sl@0
   118
		{
sl@0
   119
		iLeaveInPerformAction = 0;
sl@0
   120
		}
sl@0
   121
	User::RequestComplete(status, KErrNone);
sl@0
   122
	iActionState = CTestAction::EAction;
sl@0
   123
	}
sl@0
   124
sl@0
   125
void CActionDeriveKey::DoPerformPostrequisite(TRequestStatus& aStatus)
sl@0
   126
	{
sl@0
   127
	TRequestStatus* status = &aStatus;
sl@0
   128
	delete iKey;
sl@0
   129
	delete iSalt;
sl@0
   130
	delete iIterations;
sl@0
   131
	delete iPasswd;
sl@0
   132
	delete iOutput;
sl@0
   133
	delete iKdf;
sl@0
   134
	iKdf = 0;
sl@0
   135
	
sl@0
   136
	iFinished = ETrue;
sl@0
   137
	User::RequestComplete(status, KErrNone);
sl@0
   138
	}
sl@0
   139
sl@0
   140
void CActionDeriveKey::DoReportAction(void)
sl@0
   141
	{
sl@0
   142
	}
sl@0
   143
sl@0
   144
void CActionDeriveKey::DoCheckResult(TInt)
sl@0
   145
	{
sl@0
   146
sl@0
   147
	}
sl@0
   148
sl@0
   149
void CActionDeriveKey::PerformAction(TRequestStatus& aStatus)
sl@0
   150
	{
sl@0
   151
	TRequestStatus* status = &aStatus;
sl@0
   152
	iResult = EFalse;
sl@0
   153
	
sl@0
   154
	if (iLeaveInPerformAction)
sl@0
   155
		{
sl@0
   156
		User::Leave(KErrArgument);
sl@0
   157
		}
sl@0
   158
	iOutput->Des().SetLength(iKey->Length());
sl@0
   159
sl@0
   160
	TUint8* nptr= (TUint8*)(iIterations->Des().PtrZ()); 
sl@0
   161
	TUint32 i = strtoul((char*)nptr, 0, 10); 
sl@0
   162
	
sl@0
   163
	iConsole.Printf(_L("."));
sl@0
   164
	TPtr8 outputActual = iOutput->Des();
sl@0
   165
	TPtr8 passwdActual = iPasswd->Des();
sl@0
   166
	if (iKdf != 0 && *iKdf == KPKCS12Kdf)
sl@0
   167
		{
sl@0
   168
		// convert the password to PKCS#12 password format
sl@0
   169
		HBufC* pwdNative = HBufC::NewLC(iPasswd->Length());
sl@0
   170
		pwdNative->Des().Copy(*iPasswd);
sl@0
   171
		HBufC8* pwdPKCS12 = PKCS12KDF::GeneratePasswordLC(*pwdNative);
sl@0
   172
		PKCS12KDF::DeriveKeyL(outputActual, PKCS12KDF::EIDByteEncryptKey, *pwdPKCS12, *iSalt, i);
sl@0
   173
		CleanupStack::PopAndDestroy(2, pwdNative);
sl@0
   174
		}
sl@0
   175
	else	// PKCS#5
sl@0
   176
		{
sl@0
   177
		TPtr8 saltActual = iSalt->Des();
sl@0
   178
		TPKCS5KDF::DeriveKeyL(outputActual, passwdActual, saltActual,i);
sl@0
   179
		}
sl@0
   180
		
sl@0
   181
	if(*iOutput == *iKey)
sl@0
   182
		{
sl@0
   183
		iResult = ETrue;
sl@0
   184
		}
sl@0
   185
		
sl@0
   186
	User::RequestComplete(status, KErrNone);
sl@0
   187
	iActionState = CTestAction::EPostrequisite;
sl@0
   188
	}
sl@0
   189
sl@0
   190
void CActionDeriveKey::Hex(HBufC8& aString)
sl@0
   191
/**
sl@0
   192
	Convert the supplied hex string into the binary equivalent.
sl@0
   193
	
sl@0
   194
	@param	aString			Hex string.  On entry this contains
sl@0
   195
							a sequence of hexadecimal characters,
sl@0
   196
							e.g., "3037AFC8EA".  On exit it is
sl@0
   197
							half the original length and each two-digit
sl@0
   198
							hex number is reduced to the matching
sl@0
   199
							byte value.
sl@0
   200
 */
sl@0
   201
    {
sl@0
   202
    TPtr8 ptr=aString.Des();
sl@0
   203
    if (aString.Length()%2)
sl@0
   204
        {
sl@0
   205
        ptr.SetLength(0);
sl@0
   206
        return;
sl@0
   207
        }
sl@0
   208
    TInt i;
sl@0
   209
    for (i=0;i<aString.Length();i+=2)
sl@0
   210
        {
sl@0
   211
        TUint8 tmp;
sl@0
   212
        tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
sl@0
   213
        tmp*=16;
sl@0
   214
        tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
sl@0
   215
        ptr[i/2]=tmp;
sl@0
   216
        }
sl@0
   217
    ptr.SetLength(aString.Length()/2);
sl@0
   218
    }
sl@0
   219