1
/* hmac.c - HMAC regression tests
2
* Copyright (C) 2005 Free Software Foundation, Inc.
4
* This file is part of Libgcrypt.
6
* Libgcrypt is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 2.1 of
9
* the License, or (at your option) any later version.
11
* Libgcrypt is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
29
#include "../src/gcrypt.h"
32
static int error_count;
35
fail (const char *format, ...)
39
va_start (arg_ptr, format);
40
vfprintf (stderr, format, arg_ptr);
46
die (const char *format, ...)
50
va_start (arg_ptr, format);
51
vfprintf (stderr, format, arg_ptr);
59
check_one_mac (int algo,
60
const void *key, size_t keylen,
61
const void *data, size_t datalen,
70
err = gcry_md_open (&hd, algo, GCRY_MD_FLAG_HMAC);
73
fail ("algo %d, grcy_md_open failed: %s\n", algo, gpg_strerror (err));
77
mdlen = gcry_md_get_algo_dlen (algo);
78
if (mdlen < 1 || mdlen > 500)
80
fail ("algo %d, grcy_md_get_algo_dlen failed: %d\n", algo, mdlen);
84
err = gcry_md_setkey (hd, key, keylen);
87
fail ("algo %d, grcy_md_setkey failed: %s\n", algo, gpg_strerror (err));
91
gcry_md_write (hd, data, datalen);
93
p = gcry_md_read (hd, 0);
95
if (memcmp (p, expect, mdlen))
97
printf ("computed: ");
98
for (i = 0; i < mdlen; i++)
99
printf ("%02x ", p[i] & 0xFF);
100
printf ("\nexpected: ");
101
for (i = 0; i < mdlen; i++)
102
printf ("%02x ", expect[i] & 0xFF);
105
fail ("algo %d, MAC does not match\n", algo);
114
unsigned char key[128];
118
fprintf (stderr, "checking FIPS-198a, A.1\n");
119
for (i=0; i < 64; i++)
121
check_one_mac (GCRY_MD_SHA1, key, 64, "Sample #1", 9,
122
"\x4f\x4c\xa3\xd5\xd6\x8b\xa7\xcc\x0a\x12"
123
"\x08\xc9\xc6\x1e\x9c\x5d\xa0\x40\x3c\x0a");
126
fprintf (stderr, "checking FIPS-198a, A.2\n");
127
for (i=0, j=0x30; i < 20; i++)
129
check_one_mac (GCRY_MD_SHA1, key, 20, "Sample #2", 9,
130
"\x09\x22\xd3\x40\x5f\xaa\x3d\x19\x4f\x82"
131
"\xa4\x58\x30\x73\x7d\x5c\xc6\xc7\x5d\x24");
134
fprintf (stderr, "checking FIPS-198a, A.3\n");
135
for (i=0, j=0x50; i < 100; i++)
137
check_one_mac (GCRY_MD_SHA1, key, 100, "Sample #3", 9,
138
"\xbc\xf4\x1e\xab\x8b\xb2\xd8\x02\xf3\xd0"
139
"\x5c\xaf\x7c\xb0\x92\xec\xf8\xd1\xa3\xaa");
142
fprintf (stderr, "checking FIPS-198a, A.4\n");
143
for (i=0, j=0x70; i < 49; i++)
145
check_one_mac (GCRY_MD_SHA1, key, 49, "Sample #4", 9,
146
"\x9e\xa8\x86\xef\xe2\x68\xdb\xec\xce\x42"
147
"\x0c\x75\x24\xdf\x32\xe0\x75\x1a\x2a\x26");
152
main (int argc, char **argv)
156
if (argc > 1 && !strcmp (argv[1], "--verbose"))
158
else if (argc > 1 && !strcmp (argv[1], "--debug"))
161
if (!gcry_check_version (GCRYPT_VERSION))
162
die ("version mismatch\n");
164
gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
165
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
167
gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
170
return error_count ? 1 : 0;