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