1 // Pbkdf2HmacSha1.cpp
2
3 #include "StdAfx.h"
4
5 #include <string.h>
6
7 #include "../../../C/CpuArch.h"
8
9 #include "HmacSha1.h"
10 #include "Pbkdf2HmacSha1.h"
11
12 namespace NCrypto {
13 namespace NSha1 {
14
Pbkdf2Hmac(const Byte * pwd,size_t pwdSize,const Byte * salt,size_t saltSize,UInt32 numIterations,Byte * key,size_t keySize)15 void Pbkdf2Hmac(const Byte *pwd, size_t pwdSize,
16 const Byte *salt, size_t saltSize,
17 UInt32 numIterations,
18 Byte *key, size_t keySize)
19 {
20 MY_ALIGN (16)
21 CHmac baseCtx;
22 baseCtx.SetKey(pwd, pwdSize);
23
24 for (UInt32 i = 1; keySize != 0; i++)
25 {
26 MY_ALIGN (16)
27 CHmac ctx;
28 ctx = baseCtx;
29 ctx.Update(salt, saltSize);
30
31 MY_ALIGN (16)
32 UInt32 u[kNumDigestWords];
33 SetBe32(u, i)
34
35 ctx.Update((const Byte *)u, 4);
36 ctx.Final((Byte *)u);
37
38 ctx = baseCtx;
39 ctx.GetLoopXorDigest1((void *)u, numIterations - 1);
40
41 const unsigned curSize = (keySize < kDigestSize) ? (unsigned)keySize : kDigestSize;
42 memcpy(key, (const Byte *)u, curSize);
43 key += curSize;
44 keySize -= curSize;
45 }
46 }
47
48 }}
49