2 * Copyright (C) 2005, 2010-2024 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Simon Josefsson. Test vectors from RFC 4231. */
28 hmac_check (const void *key
, size_t key_len
,
29 const void *data
, size_t data_len
, const char *digest
)
33 if (hmac_sha256 (key
, key_len
, data
, data_len
, out
) != 0)
35 printf ("call failure\n");
39 if (memcmp (digest
, out
, 32) != 0)
42 printf ("hash 1 mismatch. expected:\n");
43 for (i
= 0; i
< 32; i
++)
44 printf ("%02x ", digest
[i
] & 0xFF);
45 printf ("\ncomputed:\n");
46 for (i
= 0; i
< 32; i
++)
47 printf ("%02x ", out
[i
] & 0xFF);
54 main (int argc
, char *argv
[])
58 size_t key_len
= sizeof key
;
59 memset (key
, '\x0b', sizeof key
);
60 const char *data
= "Hi There";
63 "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b"
64 "\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7";
65 hmac_check (key
, key_len
, data
, data_len
, digest
);
69 const char *key
= "Jefe";
71 const char *data
= "what do ya want for nothing?";
74 "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75\xc7"
75 "\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec\x38\x43";
76 hmac_check (key
, key_len
, data
, data_len
, digest
);
81 size_t key_len
= sizeof key
;
82 memset (key
, '\xAA', sizeof key
);
84 size_t data_len
= sizeof data
;
85 memset (data
, '\xDD', sizeof data
);
87 "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81\xa7"
88 "\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5\x65\xfe";
89 hmac_check (key
, key_len
, data
, data_len
, digest
);
94 size_t key_len
= sizeof key
;
95 memset (key
, '\x0b', sizeof key
);
96 const char *data
= "Hi There";
99 "\x72\x7b\x82\xfb\xa2\x64\x39\x3c\x5d\x67\xfd\x6d\x6a\xd7\x83\xe9"
100 "\x01\x9a\x1f\xa6\xa8\x57\xfc\xcb\x70\xf5\x85\x2f\x04\xbe\x5d\x5d";
101 hmac_check (key
, key_len
, data
, data_len
, digest
);