1 /*
2  * Copyright (C) 2005, 2010-2021 Free Software Foundation, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 /* Written by Simon Josefsson.  */
18 
19 #include <config.h>
20 
21 #include "hmac.h"
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 static void
hmac_check(const void * key,size_t key_len,const void * data,size_t data_len,const char * digest)28 hmac_check (const void *key, size_t key_len,
29             const void *data, size_t data_len, const char *digest)
30 {
31   char out[20];
32 
33   if (hmac_sha1 (key, key_len, data, data_len, out) != 0)
34     {
35       printf ("call failure\n");
36       exit (1);
37     }
38 
39   if (memcmp (digest, out, 20) != 0)
40     {
41       size_t i;
42       printf ("hash 1 mismatch. expected:\n");
43       for (i = 0; i < 20; i++)
44         printf ("%02x ", digest[i] & 0xFF);
45       printf ("\ncomputed:\n");
46       for (i = 0; i < 20; i++)
47         printf ("%02x ", out[i] & 0xFF);
48       printf ("\n");
49       exit (1);
50     }
51 }
52 
53 int
main(int argc,char * argv[])54 main (int argc, char *argv[])
55 {
56   {
57     char key[16];
58     size_t key_len = sizeof key;
59     memset (key, '\x0b', sizeof key);
60     char *data = "Hi There";
61     size_t data_len = 8;
62     char *digest =
63       "\x67\x5b\x0b\x3a\x1b\x4d\xdf\x4e\x12\x48\x72\xda\x6c\x2f\x63\x2b"
64       "\xfe\xd9\x57\xe9";
65     hmac_check (key, key_len, data, data_len, digest);
66   }
67 
68   {
69     char *key = "Jefe";
70     size_t key_len = 4;
71     char *data = "what do ya want for nothing?";
72     size_t data_len = 28;
73     char *digest =
74       "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c"
75       "\x25\x9a\x7c\x79";
76     hmac_check (key, key_len, data, data_len, digest);
77   }
78 
79   {
80     char key[20];
81     size_t key_len = sizeof key;
82     memset (key, '\xAA', sizeof key);
83     char data[50];
84     size_t data_len = sizeof data;
85     memset (data, '\xDD', sizeof data);
86     char *digest =
87       "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f"
88       "\x63\xf1\x75\xd3";
89     hmac_check (key, key_len, data, data_len, digest);
90   }
91 
92   {
93     char key[65];
94     size_t key_len = sizeof key;
95     memset (key, '\x0b', sizeof key);
96     char *data = "Hi There";
97     size_t data_len = 8;
98     char *digest =
99       "\x29\xda\xa9\xe9\xcc\x4b\x9f\x09\x48\x29\xdc\xd4\x03\xc0\x69\x27"
100       "\xd8\xa9\x53\x93";
101     hmac_check (key, key_len, data, data_len, digest);
102   }
103   return 0;
104 }
105