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