xref: /freebsd/contrib/netbsd-tests/lib/libc/hash/t_hmac.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*	$NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 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.1 2016/07/02 14:52:09 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 		EVP_md2(),
53 		EVP_md4(),
54 		EVP_md5(),
55 		EVP_ripemd160(),
56 		EVP_sha1(),
57 		EVP_sha224(),
58 		EVP_sha256(),
59 		EVP_sha384(),
60 		EVP_sha512(),
61 	};
62 	const char *names[] = {
63 		"md2",
64 		"md4",
65 		"md5",
66 		"rmd160",
67 		"sha1",
68 		"sha224",
69 		"sha256",
70 		"sha384",
71 		"sha512",
72 	};
73 
74 	for (size_t k = 0; k < sizeof(key); k++)
75 		key[k] = k;
76 	for (size_t d = 0; d < sizeof(data); d++)
77 		data[d] = d % 256;
78 
79 	for (size_t t = 0; t < __arraycount(names); t++)
80 	    for (size_t i = 1; i < sizeof(key); i += 9)
81 		for (size_t j = 3; j < sizeof(data); j += 111) {
82 			stop = 0;
83 #ifdef DEBUG
84 			printf("%s: keysize = %zu datasize = %zu\n", names[t],
85 			    i, j);
86 #endif
87 			memset(tmp1, 0, sizeof(tmp1));
88 			memset(tmp2, 0, sizeof(tmp2));
89 			e1 = HMAC(evps[t], key, i, data, j, tmp1, &tmp1len);
90 			ATF_REQUIRE(e1 != NULL);
91 			tmp2len = hmac(names[t], key, i, data, j, tmp2,
92 			    sizeof(tmp2));
93 			ATF_REQUIRE_MSG(tmp1len == tmp2len, "hash %s len %u "
94 			    "!= %zu", names[t], tmp1len, tmp2len);
95 			for (size_t k = 0; k < tmp2len; k++)
96 				if (tmp1[k] != tmp2[k]) {
97 #ifdef DEBUG
98 					printf("%zu %.2x %.2x\n",
99 					    k, tmp1[k], tmp2[k]);
100 #endif
101 					stop = 1;
102 					break;
103 				}
104 			ATF_REQUIRE_MSG(!stop, "hash %s failed for "
105 				"keylen=%zu, datalen=%zu", names[t], i, j);
106 		}
107 }
108 
109 ATF_TC(t_hmac);
110 
111 ATF_TC_HEAD(t_hmac, tc)
112 {
113 	atf_tc_set_md_var(tc, "descr",
114 	    "Test hmac functions for consistent results");
115 }
116 
117 ATF_TC_BODY(t_hmac, tc)
118 {
119 	test();
120 }
121 
122 ATF_TP_ADD_TCS(tp)
123 {
124 	ATF_TP_ADD_TC(tp, t_hmac);
125 	return atf_no_error();
126 }
127 
128