annotate tests/test-hmac-sha1.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 67e1644a2382
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
1 /*
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 39875
diff changeset
2 * Copyright (C) 2005, 2010-2019 Free Software Foundation, Inc.
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
3 *
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8891
diff changeset
4 * This program is free software: you can redistribute it and/or modify
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8891
diff changeset
6 * the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8891
diff changeset
7 * (at your option) any later version.
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
8 *
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
9 * This program is distributed in the hope that it will be useful,
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
12 * GNU General Public License for more details.
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
13 *
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
14 * You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
16
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
17 /* Written by Simon Josefsson. */
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
18
8891
633babea5f62 Unconditionally include <config.h> in unit tests.
Eric Blake <ebb9@byu.net>
parents: 6436
diff changeset
19 #include <config.h>
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
20
19260
8fc416080bd2 crypto/*: Verify that the header file is self-contained.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
21 #include "hmac.h"
8fc416080bd2 crypto/*: Verify that the header file is self-contained.
Bruno Haible <bruno@clisp.org>
parents: 19190
diff changeset
22
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
23 #include <stdio.h>
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
24 #include <stdlib.h>
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
25 #include <string.h>
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
26
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
27 static void
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
28 hmac_check (const void *key, size_t key_len,
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
29 const void *data, size_t data_len, const char *digest)
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
30 {
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
31 char out[20];
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
32
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
33 if (hmac_sha1 (key, key_len, data, data_len, out) != 0)
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
34 {
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
35 printf ("call failure\n");
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
36 exit (1);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
37 }
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
38
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
39 if (memcmp (digest, out, 20) != 0)
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
40 {
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
41 size_t i;
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
42 printf ("hash 1 mismatch. expected:\n");
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
43 for (i = 0; i < 20; i++)
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
44 printf ("%02x ", digest[i] & 0xFF);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
45 printf ("\ncomputed:\n");
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
46 for (i = 0; i < 20; i++)
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
47 printf ("%02x ", out[i] & 0xFF);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
48 printf ("\n");
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
49 exit (1);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
50 }
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
51 }
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
52
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
53 int
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
54 main (int argc, char *argv[])
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
55 {
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
56 {
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
57 char key[16];
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
58 size_t key_len = sizeof key;
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
59 memset (key, '\x0b', sizeof key);
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
60 char *data = "Hi There";
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
61 size_t data_len = 8;
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
62 char *digest =
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
63 "\x67\x5b\x0b\x3a\x1b\x4d\xdf\x4e\x12\x48\x72\xda\x6c\x2f\x63\x2b"
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
64 "\xfe\xd9\x57\xe9";
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
65 hmac_check (key, key_len, data, data_len, digest);
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
66 }
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
67
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
68 {
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
69 char *key = "Jefe";
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
70 size_t key_len = 4;
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
71 char *data = "what do ya want for nothing?";
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
72 size_t data_len = 28;
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
73 char *digest =
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
74 "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c"
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
75 "\x25\x9a\x7c\x79";
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
76 hmac_check (key, key_len, data, data_len, digest);
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
77 }
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
78
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
79 {
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
80 char key[20];
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
81 size_t key_len = sizeof key;
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
82 memset (key, '\xAA', sizeof key);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
83 char data[50];
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
84 size_t data_len = sizeof data;
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
85 memset (data, '\xDD', sizeof data);
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
86 char *digest =
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
87 "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f"
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
88 "\x63\xf1\x75\xd3";
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
89 hmac_check (key, key_len, data, data_len, digest);
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
90 }
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
91
39875
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
92 {
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
93 char key[65];
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
94 size_t key_len = sizeof key;
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
95 memset (key, '\x0b', sizeof key);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
96 char *data = "Hi There";
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
97 size_t data_len = 8;
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
98 char *digest =
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
99 "\x29\xda\xa9\xe9\xcc\x4b\x9f\x09\x48\x29\xdc\xd4\x03\xc0\x69\x27"
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
100 "\xd8\xa9\x53\x93";
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
101 hmac_check (key, key_len, data, data_len, digest);
67e1644a2382 hmac-*: refactor to remove repetitive code
Pádraig Brady <P@draigBrady.com>
parents: 19484
diff changeset
102 }
6364
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
103 return 0;
fb852f022f3f Add hmac-sha1 module.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
104 }