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