aboutsummaryrefslogtreecommitdiff
path: root/crypto/hmac.c
blob: f9b81c12e8b30953801518ed19562cded0900a96 (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
#include "hmac.h"

/* function doing the HMAC-SHA-1 calculation */
void hmac_sha1(const uint8_t* key, const uint32_t keysize, const uint8_t* msg, const uint32_t msgsize, uint8_t* output)
{
  struct sha1 outer, inner;
  uint8_t tmp;

  sha1_reset(&outer);
  sha1_reset(&inner);

  uint32_t i;
  for (i = 0; i < keysize; ++i)
  {
    tmp = key[i] ^ 0x5C;
    sha1_input(&outer, &tmp, 1);
    tmp = key[i] ^ 0x36;
    sha1_input(&inner, &tmp, 1);
  }
  for (; i < 64; ++i)
  {
    tmp = 0x5C;
    sha1_input(&outer, &tmp, 1);
    tmp = 0x36;
    sha1_input(&inner, &tmp, 1);
  }

  sha1_input(&inner, msg, msgsize);
  sha1_result(&inner, output);

  sha1_input(&outer, output, HMAC_SHA1_HASH_SIZE);
  sha1_result(&outer, output);
}