radsecproxy-1.6.5.
[radsecproxy.git] / tests / t_fticks.c
1 /* Copyright (C) 2011,2013, NORDUnet A/S */
2 /* See LICENSE for licensing information. */
3
4 #include <stdio.h>
5 #include <errno.h>
6 #include "../radsecproxy.h"
7 #include "../fticks_hashmac.h"
8
9 static int
10 _check_hash(const char *mac, const char *key, const char *hash, const char*hmac)
11 {
12   int rv = 0;
13   uint8_t buf[64+1];
14
15   if (fticks_hashmac((const uint8_t *) mac, NULL, sizeof(buf), buf) != 0)
16     return -ENOMEM;
17   if (strcmp(hash, (const char *) buf) != 0)
18     rv = !!fprintf(stderr, "%s: bad hash: %s\n", mac, buf);
19   if (fticks_hashmac((const uint8_t *) mac, (const uint8_t *) key,
20                      sizeof(buf), buf) != 0)
21     return -ENOMEM;
22   if (strcmp(hmac, (const char *) buf) != 0)
23     rv = !!fprintf(stderr, "%s: bad hash (key=\"%s\"): %s\n", mac, key, buf);
24
25   return rv;
26 }
27
28 #define MAC1 "00:23:14:0a:f7:24"
29 #define MAC1_UC "00:23:14:0A:F7:24"
30 #define MAC1_APPENDED "00:23:14:0a:f7:24;cruft"
31 #define MAC1_WEIRD "00:23:-[?xyzzy!]-14:0a:f7:24"
32 #define KEY1 "magic passphrase"
33 #define HASH1 "29c0ee9d9c41771795a11ff75fefe9f5ccaab523ad31fc4fd8e776c707ad1581"
34 #define HMAC1 "57c8cd8031142c51ac9747370f48a5aa731006729d0cdf589ba101864f35f390"
35
36 int
37 main (int argc, char *argv[])
38 {
39   if (_check_hash(MAC1, KEY1, HASH1, HMAC1) != 0)
40     return 1;
41   /* Again, for good measure.  (Or rather to make sure there's no
42      state left.)  */
43   if (_check_hash(MAC1, KEY1, HASH1, HMAC1) != 0)
44     return 1;
45   if (_check_hash(MAC1_UC, KEY1, HASH1, HMAC1) != 0)
46     return 1;
47   if (_check_hash(MAC1_APPENDED, KEY1, HASH1, HMAC1) != 0)
48     return 1;
49   if (_check_hash(MAC1_WEIRD, KEY1, HASH1, HMAC1) != 0)
50     return 1;
51
52   return 0;
53 }