os/security/crypto/weakcrypto/source/pkcs5kdf/pkcs5kdf.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) 2005-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
/**
sl@0
    20
 @file
sl@0
    21
*/
sl@0
    22
sl@0
    23
#include "pkcs5kdf.h"
sl@0
    24
sl@0
    25
/* Before complaining about the variable names in this file, 
sl@0
    26
 * read the pkcs5 spec and all will become clear.
sl@0
    27
 */
sl@0
    28
sl@0
    29
EXPORT_C void TPKCS5KDF::DeriveKeyL(TDes8& aKey, const TDesC8& aPasswd, const TDesC8& aSalt, 
sl@0
    30
	const TUint aIterations)
sl@0
    31
{
sl@0
    32
	CSHA1* sha1 = CSHA1::NewL();
sl@0
    33
	CleanupStack::PushL(sha1);
sl@0
    34
	CHMAC* hmac = CHMAC::NewL(aPasswd, sha1);
sl@0
    35
	CleanupStack::Pop(sha1); //hmac now owns it
sl@0
    36
	CleanupStack::PushL(hmac);
sl@0
    37
sl@0
    38
	TUint hashBytes = hmac->HashSize();
sl@0
    39
	TUint c = aIterations;
sl@0
    40
	TUint l = aKey.Length() / hashBytes; 
sl@0
    41
	if(aKey.Length() % hashBytes != 0) //round up if mod !=0
sl@0
    42
		{
sl@0
    43
		l+=1;
sl@0
    44
		}
sl@0
    45
	TUint r = aKey.Length() - (l-1) * hashBytes; //r == length of last block
sl@0
    46
	
sl@0
    47
	HBufC8* TiTemp = HBufC8::NewLC(hashBytes);
sl@0
    48
	TUint32* Ti = (TUint32*)(TiTemp->Ptr());
sl@0
    49
	aKey.SetLength(0); //we've already saved the length we want
sl@0
    50
sl@0
    51
	HBufC8* STemp = HBufC8::NewLC(aSalt.Length() + sizeof(TUint32));
sl@0
    52
	TUint32* S = (TUint32*)(STemp->Ptr());
sl@0
    53
sl@0
    54
	HBufC8* UiTemp = HBufC8::NewLC(hashBytes);
sl@0
    55
	TUint32* Ui = (TUint32*)(UiTemp->Ptr());
sl@0
    56
sl@0
    57
	const TUint32* salt = (TUint32*)(aSalt.Ptr());
sl@0
    58
	TUint saltBytes = aSalt.Length();
sl@0
    59
sl@0
    60
	for(TUint i = 1; i<=l; i++)
sl@0
    61
		{
sl@0
    62
		F(*hmac, Ti, S, Ui, hashBytes, salt, saltBytes, c, i);
sl@0
    63
		if(i == l)
sl@0
    64
			aKey.Append((TUint8*)Ti, r);
sl@0
    65
		else 
sl@0
    66
			aKey.Append((TUint8*)Ti, hashBytes);
sl@0
    67
		}
sl@0
    68
sl@0
    69
	CleanupStack::PopAndDestroy(UiTemp);
sl@0
    70
	CleanupStack::PopAndDestroy(STemp);
sl@0
    71
	CleanupStack::PopAndDestroy(TiTemp);
sl@0
    72
	CleanupStack::PopAndDestroy(hmac);
sl@0
    73
	}
sl@0
    74
sl@0
    75
void TPKCS5KDF::F(CMessageDigest& aDigest, TUint32* aAccumulator, 
sl@0
    76
	TUint32* S, TUint32* Ui, TUint aHashBytes, const TUint32* aSalt, 
sl@0
    77
	TUint aSaltBytes, TUint c, TUint i)
sl@0
    78
	{
sl@0
    79
	TUint8 itmp[4];
sl@0
    80
	itmp[0] = (TUint8)((i >> 24) & 0xff);
sl@0
    81
	itmp[1] = (TUint8)((i >> 16) & 0xff);
sl@0
    82
	itmp[2] = (TUint8)((i >> 8) & 0xff);
sl@0
    83
	itmp[3] = (TUint8)(i & 0xff);
sl@0
    84
	TUint8* endOfS = Mem::Copy(S, aSalt, aSaltBytes);
sl@0
    85
	Mem::Copy((TUint32*)endOfS, (TUint32*)&itmp, 4);
sl@0
    86
sl@0
    87
	TPtr8 sptr((TUint8*)S, aSaltBytes+4);
sl@0
    88
	sptr.SetLength(aSaltBytes+4);
sl@0
    89
	Mem::Copy(aAccumulator, (TUint32*)((aDigest.Final(sptr)).Ptr()),aHashBytes);
sl@0
    90
	Mem::Copy(Ui, aAccumulator, aHashBytes);
sl@0
    91
sl@0
    92
	for(TUint j=1; j<c; j++)
sl@0
    93
		{
sl@0
    94
		TPtr8 uiptr((TUint8*)Ui, aHashBytes);
sl@0
    95
		uiptr.SetLength(aHashBytes);
sl@0
    96
		Mem::Copy(Ui, (TUint32*)((aDigest.Final(uiptr)).Ptr()), aHashBytes);
sl@0
    97
		XORString(Ui, aAccumulator, aHashBytes);
sl@0
    98
		}
sl@0
    99
	}
sl@0
   100
sl@0
   101
inline void TPKCS5KDF::XORString(const TUint32* aOp1, TUint32* aOp2,
sl@0
   102
	TUint aLength)
sl@0
   103
	{
sl@0
   104
	const TUint32* i = aOp1;
sl@0
   105
sl@0
   106
	//this will overflow the whole final word if aLength % 4 != 0 
sl@0
   107
	//but I can't see this mattering cuz all memory allocation is on a word by word basis
sl@0
   108
	//i don't want to do this byte by byte as it'll be way slower
sl@0
   109
	//also, every sane digest is going to be a multiple of 4 -- so this isn't a problem
sl@0
   110
	for( ; aOp1 != (TUint32*)((TUint8*)i + aLength); )
sl@0
   111
		{
sl@0
   112
			*aOp2++ ^= *aOp1++;	
sl@0
   113
		}
sl@0
   114
	}
sl@0
   115