1303980Sngie/*	$NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 christos Exp $	*/
2303980Sngie
3303980Sngie/*-
4303980Sngie * Copyright (c) 2016 The NetBSD Foundation, Inc.
5303980Sngie * All rights reserved.
6303980Sngie *
7303980Sngie * This code is derived from software contributed to The NetBSD Foundation
8303980Sngie * by Christos Zoulas.
9303980Sngie *
10303980Sngie * Redistribution and use in source and binary forms, with or without
11303980Sngie * modification, are permitted provided that the following conditions
12303980Sngie * are met:
13303980Sngie * 1. Redistributions of source code must retain the above copyright
14303980Sngie *    notice, this list of conditions and the following disclaimer.
15303980Sngie * 2. Redistributions in binary form must reproduce the above copyright
16303980Sngie *    notice, this list of conditions and the following disclaimer in the
17303980Sngie *    documentation and/or other materials provided with the distribution.
18303980Sngie *
19303980Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20303980Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21303980Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22303980Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23303980Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24303980Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25303980Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26303980Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27303980Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28303980Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29303980Sngie * POSSIBILITY OF SUCH DAMAGE.
30303980Sngie */
31303980Sngie#include <sys/cdefs.h>
32303980Sngie__RCSID("$NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 christos Exp $");
33303980Sngie
34303980Sngie#include <atf-c.h>
35303980Sngie#include <string.h>
36303980Sngie#include <stdlib.h>
37303980Sngie#include <openssl/evp.h>
38303980Sngie#include <openssl/hmac.h>
39303980Sngie
40303980Sngiestatic void
41303980Sngietest(void)
42303980Sngie{
43303980Sngie	uint8_t         tmp1[EVP_MAX_MD_SIZE];
44303980Sngie	uint8_t         tmp2[EVP_MAX_MD_SIZE];
45303980Sngie	uint8_t key[256];
46303980Sngie	uint8_t data[4096];
47303980Sngie	unsigned int tmp1len;
48303980Sngie	size_t tmp2len;
49303980Sngie	int stop;
50303980Sngie	void *e1;
51303980Sngie	const void *evps[] = {
52303980Sngie		EVP_md2(),
53303980Sngie		EVP_md4(),
54303980Sngie		EVP_md5(),
55303980Sngie		EVP_ripemd160(),
56303980Sngie		EVP_sha1(),
57303980Sngie		EVP_sha224(),
58303980Sngie		EVP_sha256(),
59303980Sngie		EVP_sha384(),
60303980Sngie		EVP_sha512(),
61303980Sngie	};
62303980Sngie	const char *names[] = {
63303980Sngie		"md2",
64303980Sngie		"md4",
65303980Sngie		"md5",
66303980Sngie		"rmd160",
67303980Sngie		"sha1",
68303980Sngie		"sha224",
69303980Sngie		"sha256",
70303980Sngie		"sha384",
71303980Sngie		"sha512",
72303980Sngie	};
73303980Sngie
74303980Sngie	for (size_t k = 0; k < sizeof(key); k++)
75303980Sngie		key[k] = k;
76303980Sngie	for (size_t d = 0; d < sizeof(data); d++)
77303980Sngie		data[d] = d % 256;
78303980Sngie
79303980Sngie	for (size_t t = 0; t < __arraycount(names); t++)
80303980Sngie	    for (size_t i = 1; i < sizeof(key); i += 9)
81303980Sngie		for (size_t j = 3; j < sizeof(data); j += 111) {
82303980Sngie			stop = 0;
83303980Sngie#ifdef DEBUG
84303980Sngie			printf("%s: keysize = %zu datasize = %zu\n", names[t],
85303980Sngie			    i, j);
86303980Sngie#endif
87303980Sngie			memset(tmp1, 0, sizeof(tmp1));
88303980Sngie			memset(tmp2, 0, sizeof(tmp2));
89303980Sngie			e1 = HMAC(evps[t], key, i, data, j, tmp1, &tmp1len);
90303980Sngie			ATF_REQUIRE(e1 != NULL);
91303980Sngie			tmp2len = hmac(names[t], key, i, data, j, tmp2,
92303980Sngie			    sizeof(tmp2));
93303980Sngie			ATF_REQUIRE_MSG(tmp1len == tmp2len, "hash %s len %u "
94303980Sngie			    "!= %zu", names[t], tmp1len, tmp2len);
95303980Sngie			for (size_t k = 0; k < tmp2len; k++)
96303980Sngie				if (tmp1[k] != tmp2[k]) {
97303980Sngie#ifdef DEBUG
98303980Sngie					printf("%zu %.2x %.2x\n",
99303980Sngie					    k, tmp1[k], tmp2[k]);
100303980Sngie#endif
101303980Sngie					stop = 1;
102303980Sngie					break;
103303980Sngie				}
104303980Sngie			ATF_REQUIRE_MSG(!stop, "hash %s failed for "
105303980Sngie				"keylen=%zu, datalen=%zu", names[t], i, j);
106303980Sngie		}
107303980Sngie}
108303980Sngie
109303980SngieATF_TC(t_hmac);
110303980Sngie
111303980SngieATF_TC_HEAD(t_hmac, tc)
112303980Sngie{
113303980Sngie	atf_tc_set_md_var(tc, "descr",
114303980Sngie	    "Test hmac functions for consistent results");
115303980Sngie}
116303980Sngie
117303980SngieATF_TC_BODY(t_hmac, tc)
118303980Sngie{
119303980Sngie	test();
120303980Sngie}
121303980Sngie
122303980SngieATF_TP_ADD_TCS(tp)
123303980Sngie{
124303980Sngie	ATF_TP_ADD_TC(tp, t_hmac);
125303980Sngie	return atf_no_error();
126303980Sngie}
127303980Sngie
128