maint: update copyright
[gnulib.git] / tests / test-hmac-md5.c
1 /*
2  * Copyright (C) 2005, 2010-2014 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 <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Simon Josefsson.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <string.h>
23 #include "hmac.h"
24
25 /* Test vectors from RFC 2104. */
26
27 int
28 main (int argc, char *argv[])
29 {
30   {
31     /*
32        key =         0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
33        key_len =     16 bytes
34        data =        "Hi There"
35        data_len =    8  bytes
36        digest =      0x9294727a3638bb1c13f48ef8158bfc9d
37      */
38     char *key =
39       "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
40     size_t key_len = 16;
41     char *data = "Hi There";
42     size_t data_len = 8;
43     char *digest =
44       "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
45     char out[16];
46
47     if (hmac_md5 (key, key_len, data, data_len, out) != 0)
48       {
49         printf ("call failure\n");
50         return 1;
51       }
52
53     if (memcmp (digest, out, 16) != 0)
54       {
55         size_t i;
56         printf ("hash 1 mismatch. expected:\n");
57         for (i = 0; i < 16; i++)
58           printf ("%02x ", digest[i] & 0xFF);
59         printf ("\ncomputed:\n");
60         for (i = 0; i < 16; i++)
61           printf ("%02x ", out[i] & 0xFF);
62         printf ("\n");
63         return 1;
64       }
65   }
66
67   {
68     /*
69        key =         "Jefe"
70        data =        "what do ya want for nothing?"
71        data_len =    28 bytes
72        digest =      0x750c783e6ab0b503eaa86e310a5db738
73      */
74     char *key = "Jefe";
75     size_t key_len = 4;
76     char *data = "what do ya want for nothing?";
77     size_t data_len = 28;
78     char *digest =
79       "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
80     char out[16];
81
82     if (hmac_md5 (key, key_len, data, data_len, out) != 0)
83       {
84         printf ("call failure\n");
85         return 1;
86       }
87
88     if (memcmp (digest, out, 16) != 0)
89       {
90         size_t i;
91         printf ("hash 2 mismatch. expected:\n");
92         for (i = 0; i < 16; i++)
93           printf ("%02x ", digest[i] & 0xFF);
94         printf ("\ncomputed:\n");
95         for (i = 0; i < 16; i++)
96           printf ("%02x ", out[i] & 0xFF);
97         printf ("\n");
98         return 1;
99       }
100   }
101
102   {
103     /*
104        key =         0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
105        key_len       16 bytes
106        data =        0xDDDDDDDDDDDDDDDDDDDD...
107        ..DDDDDDDDDDDDDDDDDDDD...
108        ..DDDDDDDDDDDDDDDDDDDD...
109        ..DDDDDDDDDDDDDDDDDDDD...
110        ..DDDDDDDDDDDDDDDDDDDD
111        data_len =    50 bytes
112        digest =      0x56be34521d144c88dbb8c733f0e8b3f6
113      */
114     char *key =
115       "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
116     size_t key_len = 16;
117     char *data = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
118       "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
119       "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
120       "\xDD\xDD";
121     size_t data_len = 50;
122     char *digest =
123       "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
124     char out[16];
125
126     if (hmac_md5 (key, key_len, data, data_len, out) != 0)
127       {
128         printf ("call failure\n");
129         return 1;
130       }
131
132     if (memcmp (digest, out, 16) != 0)
133       {
134         size_t i;
135         printf ("hash 3 mismatch. expected:\n");
136         for (i = 0; i < 16; i++)
137           printf ("%02x ", digest[i] & 0xFF);
138         printf ("\ncomputed:\n");
139         for (i = 0; i < 16; i++)
140           printf ("%02x ", out[i] & 0xFF);
141         printf ("\n");
142         return 1;
143       }
144   }
145
146   return 0;
147 }