Update contrib.
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
25 /* Before complaining about the variable names in this file,
26 * read the pkcs5 spec and all will become clear.
29 EXPORT_C void TPKCS5KDF::DeriveKeyL(TDes8& aKey, const TDesC8& aPasswd, const TDesC8& aSalt,
30 const TUint aIterations)
32 CSHA1* sha1 = CSHA1::NewL();
33 CleanupStack::PushL(sha1);
34 CHMAC* hmac = CHMAC::NewL(aPasswd, sha1);
35 CleanupStack::Pop(sha1); //hmac now owns it
36 CleanupStack::PushL(hmac);
38 TUint hashBytes = hmac->HashSize();
39 TUint c = aIterations;
40 TUint l = aKey.Length() / hashBytes;
41 if(aKey.Length() % hashBytes != 0) //round up if mod !=0
45 TUint r = aKey.Length() - (l-1) * hashBytes; //r == length of last block
47 HBufC8* TiTemp = HBufC8::NewLC(hashBytes);
48 TUint32* Ti = (TUint32*)(TiTemp->Ptr());
49 aKey.SetLength(0); //we've already saved the length we want
51 HBufC8* STemp = HBufC8::NewLC(aSalt.Length() + sizeof(TUint32));
52 TUint32* S = (TUint32*)(STemp->Ptr());
54 HBufC8* UiTemp = HBufC8::NewLC(hashBytes);
55 TUint32* Ui = (TUint32*)(UiTemp->Ptr());
57 const TUint32* salt = (TUint32*)(aSalt.Ptr());
58 TUint saltBytes = aSalt.Length();
60 for(TUint i = 1; i<=l; i++)
62 F(*hmac, Ti, S, Ui, hashBytes, salt, saltBytes, c, i);
64 aKey.Append((TUint8*)Ti, r);
66 aKey.Append((TUint8*)Ti, hashBytes);
69 CleanupStack::PopAndDestroy(UiTemp);
70 CleanupStack::PopAndDestroy(STemp);
71 CleanupStack::PopAndDestroy(TiTemp);
72 CleanupStack::PopAndDestroy(hmac);
75 void TPKCS5KDF::F(CMessageDigest& aDigest, TUint32* aAccumulator,
76 TUint32* S, TUint32* Ui, TUint aHashBytes, const TUint32* aSalt,
77 TUint aSaltBytes, TUint c, TUint i)
80 itmp[0] = (TUint8)((i >> 24) & 0xff);
81 itmp[1] = (TUint8)((i >> 16) & 0xff);
82 itmp[2] = (TUint8)((i >> 8) & 0xff);
83 itmp[3] = (TUint8)(i & 0xff);
84 TUint8* endOfS = Mem::Copy(S, aSalt, aSaltBytes);
85 Mem::Copy((TUint32*)endOfS, (TUint32*)&itmp, 4);
87 TPtr8 sptr((TUint8*)S, aSaltBytes+4);
88 sptr.SetLength(aSaltBytes+4);
89 Mem::Copy(aAccumulator, (TUint32*)((aDigest.Final(sptr)).Ptr()),aHashBytes);
90 Mem::Copy(Ui, aAccumulator, aHashBytes);
92 for(TUint j=1; j<c; j++)
94 TPtr8 uiptr((TUint8*)Ui, aHashBytes);
95 uiptr.SetLength(aHashBytes);
96 Mem::Copy(Ui, (TUint32*)((aDigest.Final(uiptr)).Ptr()), aHashBytes);
97 XORString(Ui, aAccumulator, aHashBytes);
101 inline void TPKCS5KDF::XORString(const TUint32* aOp1, TUint32* aOp2,
104 const TUint32* i = aOp1;
106 //this will overflow the whole final word if aLength % 4 != 0
107 //but I can't see this mattering cuz all memory allocation is on a word by word basis
108 //i don't want to do this byte by byte as it'll be way slower
109 //also, every sane digest is going to be a multiple of 4 -- so this isn't a problem
110 for( ; aOp1 != (TUint32*)((TUint8*)i + aLength); )