19
|
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 |
@file
|
|
21 |
*/
|
|
22 |
|
|
23 |
#include "pkcs5kdf.h"
|
|
24 |
|
|
25 |
/* Before complaining about the variable names in this file,
|
|
26 |
* read the pkcs5 spec and all will become clear.
|
|
27 |
*/
|
|
28 |
|
|
29 |
EXPORT_C void TPKCS5KDF::DeriveKeyL(TDes8& aKey, const TDesC8& aPasswd, const TDesC8& aSalt,
|
|
30 |
const TUint aIterations)
|
|
31 |
{
|
|
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);
|
|
37 |
|
|
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
|
|
42 |
{
|
|
43 |
l+=1;
|
|
44 |
}
|
|
45 |
TUint r = aKey.Length() - (l-1) * hashBytes; //r == length of last block
|
|
46 |
|
|
47 |
HBufC8* TiTemp = HBufC8::NewLC(hashBytes);
|
|
48 |
TUint32* Ti = (TUint32*)(TiTemp->Ptr());
|
|
49 |
aKey.SetLength(0); //we've already saved the length we want
|
|
50 |
|
|
51 |
HBufC8* STemp = HBufC8::NewLC(aSalt.Length() + sizeof(TUint32));
|
|
52 |
TUint32* S = (TUint32*)(STemp->Ptr());
|
|
53 |
|
|
54 |
HBufC8* UiTemp = HBufC8::NewLC(hashBytes);
|
|
55 |
TUint32* Ui = (TUint32*)(UiTemp->Ptr());
|
|
56 |
|
|
57 |
const TUint32* salt = (TUint32*)(aSalt.Ptr());
|
|
58 |
TUint saltBytes = aSalt.Length();
|
|
59 |
|
|
60 |
for(TUint i = 1; i<=l; i++)
|
|
61 |
{
|
|
62 |
F(*hmac, Ti, S, Ui, hashBytes, salt, saltBytes, c, i);
|
|
63 |
if(i == l)
|
|
64 |
aKey.Append((TUint8*)Ti, r);
|
|
65 |
else
|
|
66 |
aKey.Append((TUint8*)Ti, hashBytes);
|
|
67 |
}
|
|
68 |
|
|
69 |
CleanupStack::PopAndDestroy(UiTemp);
|
|
70 |
CleanupStack::PopAndDestroy(STemp);
|
|
71 |
CleanupStack::PopAndDestroy(TiTemp);
|
|
72 |
CleanupStack::PopAndDestroy(hmac);
|
|
73 |
}
|
|
74 |
|
|
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)
|
|
78 |
{
|
|
79 |
TUint8 itmp[4];
|
|
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);
|
|
86 |
|
|
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);
|
|
91 |
|
|
92 |
for(TUint j=1; j<c; j++)
|
|
93 |
{
|
|
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);
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
inline void TPKCS5KDF::XORString(const TUint32* aOp1, TUint32* aOp2,
|
|
102 |
TUint aLength)
|
|
103 |
{
|
|
104 |
const TUint32* i = aOp1;
|
|
105 |
|
|
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); )
|
|
111 |
{
|
|
112 |
*aOp2++ ^= *aOp1++;
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|