sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: */ sl@0: sl@0: #include "pkcs5kdf.h" sl@0: sl@0: /* Before complaining about the variable names in this file, sl@0: * read the pkcs5 spec and all will become clear. sl@0: */ sl@0: sl@0: EXPORT_C void TPKCS5KDF::DeriveKeyL(TDes8& aKey, const TDesC8& aPasswd, const TDesC8& aSalt, sl@0: const TUint aIterations) sl@0: { sl@0: CSHA1* sha1 = CSHA1::NewL(); sl@0: CleanupStack::PushL(sha1); sl@0: CHMAC* hmac = CHMAC::NewL(aPasswd, sha1); sl@0: CleanupStack::Pop(sha1); //hmac now owns it sl@0: CleanupStack::PushL(hmac); sl@0: sl@0: TUint hashBytes = hmac->HashSize(); sl@0: TUint c = aIterations; sl@0: TUint l = aKey.Length() / hashBytes; sl@0: if(aKey.Length() % hashBytes != 0) //round up if mod !=0 sl@0: { sl@0: l+=1; sl@0: } sl@0: TUint r = aKey.Length() - (l-1) * hashBytes; //r == length of last block sl@0: sl@0: HBufC8* TiTemp = HBufC8::NewLC(hashBytes); sl@0: TUint32* Ti = (TUint32*)(TiTemp->Ptr()); sl@0: aKey.SetLength(0); //we've already saved the length we want sl@0: sl@0: HBufC8* STemp = HBufC8::NewLC(aSalt.Length() + sizeof(TUint32)); sl@0: TUint32* S = (TUint32*)(STemp->Ptr()); sl@0: sl@0: HBufC8* UiTemp = HBufC8::NewLC(hashBytes); sl@0: TUint32* Ui = (TUint32*)(UiTemp->Ptr()); sl@0: sl@0: const TUint32* salt = (TUint32*)(aSalt.Ptr()); sl@0: TUint saltBytes = aSalt.Length(); sl@0: sl@0: for(TUint i = 1; i<=l; i++) sl@0: { sl@0: F(*hmac, Ti, S, Ui, hashBytes, salt, saltBytes, c, i); sl@0: if(i == l) sl@0: aKey.Append((TUint8*)Ti, r); sl@0: else sl@0: aKey.Append((TUint8*)Ti, hashBytes); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(UiTemp); sl@0: CleanupStack::PopAndDestroy(STemp); sl@0: CleanupStack::PopAndDestroy(TiTemp); sl@0: CleanupStack::PopAndDestroy(hmac); sl@0: } sl@0: sl@0: void TPKCS5KDF::F(CMessageDigest& aDigest, TUint32* aAccumulator, sl@0: TUint32* S, TUint32* Ui, TUint aHashBytes, const TUint32* aSalt, sl@0: TUint aSaltBytes, TUint c, TUint i) sl@0: { sl@0: TUint8 itmp[4]; sl@0: itmp[0] = (TUint8)((i >> 24) & 0xff); sl@0: itmp[1] = (TUint8)((i >> 16) & 0xff); sl@0: itmp[2] = (TUint8)((i >> 8) & 0xff); sl@0: itmp[3] = (TUint8)(i & 0xff); sl@0: TUint8* endOfS = Mem::Copy(S, aSalt, aSaltBytes); sl@0: Mem::Copy((TUint32*)endOfS, (TUint32*)&itmp, 4); sl@0: sl@0: TPtr8 sptr((TUint8*)S, aSaltBytes+4); sl@0: sptr.SetLength(aSaltBytes+4); sl@0: Mem::Copy(aAccumulator, (TUint32*)((aDigest.Final(sptr)).Ptr()),aHashBytes); sl@0: Mem::Copy(Ui, aAccumulator, aHashBytes); sl@0: sl@0: for(TUint j=1; j