1 /* $NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 christos Exp $ */
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_hmac.c,v 1.1 2016/07/02 14:52:09 christos Exp $");
37 #include <openssl/evp.h>
38 #include <openssl/hmac.h>
43 uint8_t tmp1[EVP_MAX_MD_SIZE];
44 uint8_t tmp2[EVP_MAX_MD_SIZE];
51 const void *evps[] = {
62 const char *names[] = {
74 for (size_t k = 0; k < sizeof(key); k++)
76 for (size_t d = 0; d < sizeof(data); d++)
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) {
84 printf("%s: keysize = %zu datasize = %zu\n", names[t],
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,
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]) {
98 printf("%zu %.2x %.2x\n",
104 ATF_REQUIRE_MSG(!stop, "hash %s failed for "
105 "keylen=%zu, datalen=%zu", names[t], i, j);
111 ATF_TC_HEAD(t_hmac, tc)
113 atf_tc_set_md_var(tc, "descr",
114 "Test hmac functions for consistent results");
117 ATF_TC_BODY(t_hmac, tc)
124 ATF_TP_ADD_TC(tp, t_hmac);
125 return atf_no_error();