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
|
#include <string.h>
#include "sha1.h"
#define IPAD (0x36)
#define OPAD (0x5C)
void
hmac_sha1(uint8_t *restrict out,
const uint8_t *restrict key, size_t keysz,
const uint8_t *restrict msg, size_t msgsz)
{
uint8_t keyext[SHA1BLKSZ] = {0},
keyipad[SHA1BLKSZ],
keyopad[SHA1BLKSZ];
if (keysz > SHA1BLKSZ) {
sha1_t sha;
sha1init(&sha);
sha1hash(&sha, key, keysz);
sha1end(&sha, keyext);
} else
memcpy(keyext, key, keysz);
for (size_t i = 0; i < sizeof(keyext); i++) {
keyipad[i] = keyext[i] ^ IPAD;
keyopad[i] = keyext[i] ^ OPAD;
}
sha1_t sha;
uint8_t dgst[SHA1DGSTSZ];
sha1init(&sha);
sha1hash(&sha, keyipad, sizeof(keyipad));
sha1hash(&sha, msg, msgsz);
sha1end(&sha, dgst);
sha1init(&sha);
sha1hash(&sha, keyopad, sizeof(keyopad));
sha1hash(&sha, dgst, sizeof(dgst));
sha1end(&sha, out);
}
|