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