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