-
Notifications
You must be signed in to change notification settings - Fork 15
/
mac.c
54 lines (49 loc) · 1.31 KB
/
mac.c
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
#include "mac.h"
#include <stdlib.h>
#include <string.h>
#include "sha-256.h"
static uint64_t last_nonce = 0;
static size_t secret_len = 0;
static char* secret = NULL;
void mac_init(const char* sec, size_t seclen) {
if ((sec == NULL) || (seclen == 0)) {
sec = NULL;
seclen = 0;
}
if (secret_len) {
free(secret);
}
secret_len = seclen;
if (seclen) {
secret = malloc(seclen);
memcpy(secret, sec, seclen);
} else {
secret = NULL;
}
last_nonce = 0;
}
mac_t mac_gen(const char* msg, size_t msglen, uint64_t nonce) {
mac_t mac;
if ((msg == NULL) || (msglen == 0)) {
msg = NULL;
msglen = 0;
}
char* buf = malloc(secret_len + msglen + sizeof(nonce));
memcpy(buf, secret, secret_len);
memcpy(buf + secret_len, msg, msglen);
memcpy(buf + secret_len + msglen, &nonce, sizeof(nonce));
calc_sha_256(mac.hash, buf, secret_len + msglen + sizeof(nonce));
mac.nonce = nonce;
free(buf);
return mac;
}
bool mac_test(const char* msg, size_t msglen, mac_t mac) {
if (mac.nonce > last_nonce) {
mac_t own_mac = mac_gen(msg, msglen, mac.nonce);
if (memcmp(&own_mac, &mac, sizeof(mac_t)) == 0) {
last_nonce = mac.nonce;
return true;
}
}
return false;
}