xref: /netbsd-src/tests/lib/libc/hash/t_hmac.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: t_hmac.c,v 1.2 2018/02/07 13:18:33 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_hmac.c,v 1.2 2018/02/07 13:18:33 christos Exp $");
33 
34 #include <atf-c.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <openssl/evp.h>
38 #include <openssl/hmac.h>
39 
40 static void
41 test(void)
42 {
43 	uint8_t         tmp1[EVP_MAX_MD_SIZE];
44 	uint8_t         tmp2[EVP_MAX_MD_SIZE];
45 	uint8_t key[256];
46 	uint8_t data[4096];
47 	unsigned int tmp1len;
48 	size_t tmp2len;
49 	int stop;
50 	void *e1;
51 	const void *evps[] = {
52 #if OPENSSL_VERSION_NUMBER < 0x10100000L
53 		EVP_md2(),
54 #endif
55 		EVP_md4(),
56 		EVP_md5(),
57 		EVP_ripemd160(),
58 		EVP_sha1(),
59 		EVP_sha224(),
60 		EVP_sha256(),
61 		EVP_sha384(),
62 		EVP_sha512(),
63 	};
64 	const char *names[] = {
65 #if OPENSSL_VERSION_NUMBER < 0x10100000L
66 		"md2",
67 #endif
68 		"md4",
69 		"md5",
70 		"rmd160",
71 		"sha1",
72 		"sha224",
73 		"sha256",
74 		"sha384",
75 		"sha512",
76 	};
77 
78 	for (size_t k = 0; k < sizeof(key); k++)
79 		key[k] = k;
80 	for (size_t d = 0; d < sizeof(data); d++)
81 		data[d] = d % 256;
82 
83 	for (size_t t = 0; t < __arraycount(names); t++)
84 	    for (size_t i = 1; i < sizeof(key); i += 9)
85 		for (size_t j = 3; j < sizeof(data); j += 111) {
86 			stop = 0;
87 #ifdef DEBUG
88 			printf("%s: keysize = %zu datasize = %zu\n", names[t],
89 			    i, j);
90 #endif
91 			memset(tmp1, 0, sizeof(tmp1));
92 			memset(tmp2, 0, sizeof(tmp2));
93 			e1 = HMAC(evps[t], key, i, data, j, tmp1, &tmp1len);
94 			ATF_REQUIRE(e1 != NULL);
95 			tmp2len = hmac(names[t], key, i, data, j, tmp2,
96 			    sizeof(tmp2));
97 			ATF_REQUIRE_MSG(tmp1len == tmp2len, "hash %s len %u "
98 			    "!= %zu", names[t], tmp1len, tmp2len);
99 			for (size_t k = 0; k < tmp2len; k++)
100 				if (tmp1[k] != tmp2[k]) {
101 #ifdef DEBUG
102 					printf("%zu %.2x %.2x\n",
103 					    k, tmp1[k], tmp2[k]);
104 #endif
105 					stop = 1;
106 					break;
107 				}
108 			ATF_REQUIRE_MSG(!stop, "hash %s failed for "
109 				"keylen=%zu, datalen=%zu", names[t], i, j);
110 		}
111 }
112 
113 ATF_TC(t_hmac);
114 
115 ATF_TC_HEAD(t_hmac, tc)
116 {
117 	atf_tc_set_md_var(tc, "descr",
118 	    "Test hmac functions for consistent results");
119 }
120 
121 ATF_TC_BODY(t_hmac, tc)
122 {
123 	test();
124 }
125 
126 ATF_TP_ADD_TCS(tp)
127 {
128 	ATF_TP_ADD_TC(tp, t_hmac);
129 	return atf_no_error();
130 }
131 
132