1 // HmacSha256.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../../C/CpuArch.h"
6
7 #include "HmacSha256.h"
8
9 namespace NCrypto {
10 namespace NSha256 {
11
SetKey(const Byte * key,size_t keySize)12 void CHmac::SetKey(const Byte *key, size_t keySize)
13 {
14 MY_ALIGN (16)
15 UInt32 temp[SHA256_NUM_BLOCK_WORDS];
16 size_t i;
17
18 for (i = 0; i < SHA256_NUM_BLOCK_WORDS; i++)
19 temp[i] = 0;
20
21 if (keySize > kBlockSize)
22 {
23 Sha256_Init(&_sha);
24 Sha256_Update(&_sha, key, keySize);
25 Sha256_Final(&_sha, (Byte *)temp);
26 }
27 else
28 memcpy(temp, key, keySize);
29
30 for (i = 0; i < SHA256_NUM_BLOCK_WORDS; i++)
31 temp[i] ^= 0x36363636;
32
33 Sha256_Init(&_sha);
34 Sha256_Update(&_sha, (const Byte *)temp, kBlockSize);
35
36 for (i = 0; i < SHA256_NUM_BLOCK_WORDS; i++)
37 temp[i] ^= 0x36363636 ^ 0x5C5C5C5C;
38
39 Sha256_Init(&_sha2);
40 Sha256_Update(&_sha2, (const Byte *)temp, kBlockSize);
41 }
42
43
Final(Byte * mac)44 void CHmac::Final(Byte *mac)
45 {
46 Sha256_Final(&_sha, mac);
47 Sha256_Update(&_sha2, mac, SHA256_DIGEST_SIZE);
48 Sha256_Final(&_sha2, mac);
49 }
50
51 }}
52