openat: don’t close (-1)
[gnulib.git] / tests / test-hmac-sha256.c
blob34513f31b910ca2facd6a9c1e3e1a640dd08ba8a
1 /*
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. */
19 #include <config.h>
21 #include "hmac.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 static void
28 hmac_check (const void *key, size_t key_len,
29 const void *data, size_t data_len, const char *digest)
31 char out[32];
33 if (hmac_sha256 (key, key_len, data, data_len, out) != 0)
35 printf ("call failure\n");
36 exit (1);
39 if (memcmp (digest, out, 32) != 0)
41 size_t i;
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);
48 printf ("\n");
49 exit (1);
53 int
54 main (int argc, char *argv[])
57 char key[20];
58 size_t key_len = sizeof key;
59 memset (key, '\x0b', sizeof key);
60 const char *data = "Hi There";
61 size_t data_len = 8;
62 const char *digest =
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";
70 size_t key_len = 4;
71 const char *data = "what do ya want for nothing?";
72 size_t data_len = 28;
73 const char *digest =
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);
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 const char *digest =
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);
93 char key[65];
94 size_t key_len = sizeof key;
95 memset (key, '\x0b', sizeof key);
96 const char *data = "Hi There";
97 size_t data_len = 8;
98 const char *digest =
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);
104 return 0;