Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HmacSha256.cpp « Crypto « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e1efb48a0d6f99a60af4410337bc764a0a9a61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// HmacSha256.cpp

#include "StdAfx.h"

#include "../../../C/CpuArch.h"

#include "HmacSha256.h"

namespace NCrypto {
namespace NSha256 {

static const unsigned kBlockSize = 64;

void CHmac::SetKey(const Byte *key, size_t keySize)
{
  Byte temp[kBlockSize];
  size_t i;
  
  for (i = 0; i < kBlockSize; i++)
    temp[i] = 0;
  
  if (keySize > kBlockSize)
  {
    Sha256_Init(&_sha);
    Sha256_Update(&_sha, key, keySize);
    Sha256_Final(&_sha, temp);
  }
  else
    for (i = 0; i < keySize; i++)
      temp[i] = key[i];
  
  for (i = 0; i < kBlockSize; i++)
    temp[i] ^= 0x36;
  
  Sha256_Init(&_sha);
  Sha256_Update(&_sha, temp, kBlockSize);
  
  for (i = 0; i < kBlockSize; i++)
    temp[i] ^= 0x36 ^ 0x5C;

  Sha256_Init(&_sha2);
  Sha256_Update(&_sha2, temp, kBlockSize);
}

void CHmac::Final(Byte *mac)
{
  Sha256_Final(&_sha, mac);
  Sha256_Update(&_sha2, mac, SHA256_DIGEST_SIZE);
  Sha256_Final(&_sha2, mac);
}

/*
void CHmac::Final(Byte *mac, size_t macSize)
{
  Byte digest[SHA256_DIGEST_SIZE];
  Final(digest);
  for (size_t i = 0; i < macSize; i++)
    mac[i] = digest[i];
}
*/

}}