xref: /openbmc/qemu/tests/unit/test-crypto-hash.c (revision b278b60d)
1 /*
2  * QEMU Crypto hash algorithms
3  *
4  * Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates
5  * Copyright (c) 2015 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "qemu/osdep.h"
23 
24 #include "crypto/init.h"
25 #include "crypto/hash.h"
26 
27 #define INPUT_TEXT "Hiss hisss Hissss hiss Hiss hisss Hiss hiss"
28 #define INPUT_TEXT1 "Hiss hisss "
29 #define INPUT_TEXT2 "Hissss hiss "
30 #define INPUT_TEXT3 "Hiss hisss Hiss hiss"
31 
32 #define OUTPUT_MD5 "628d206371563035ab8ef62f492bdec9"
33 #define OUTPUT_SHA1 "b2e74f26758a3a421e509cee045244b78753cc02"
34 #define OUTPUT_SHA224 "e2f7415aad33ef79f6516b0986d7175f" \
35                       "9ca3389a85bf6cfed078737b"
36 #define OUTPUT_SHA256 "bc757abb0436586f392b437e5dd24096" \
37                       "f7f224de6b74d4d86e2abc6121b160d0"
38 #define OUTPUT_SHA384 "887ce52efb4f46700376356583b7e279" \
39                       "4f612bd024e4495087ddb946c448c69d" \
40                       "56dbf7152a94a5e63a80f3ba9f0eed78"
41 #define OUTPUT_SHA512 "3a90d79638235ec6c4c11bebd84d83c0" \
42                       "549bc1e84edc4b6ec7086487641256cb" \
43                       "63b54e4cb2d2032b393994aa263c0dbb" \
44                       "e00a9f2fe9ef6037352232a1eec55ee7"
45 #define OUTPUT_RIPEMD160 "f3d658fad3fdfb2b52c9369cf0d441249ddfa8a0"
46 
47 #define OUTPUT_MD5_B64 "Yo0gY3FWMDWrjvYvSSveyQ=="
48 #define OUTPUT_SHA1_B64 "sudPJnWKOkIeUJzuBFJEt4dTzAI="
49 #define OUTPUT_SHA224_B64 "4vdBWq0z73n2UWsJhtcXX5yjOJqFv2z+0Hhzew=="
50 #define OUTPUT_SHA256_B64 "vHV6uwQ2WG85K0N+XdJAlvfyJN5rdNTYbiq8YSGxYNA="
51 #define OUTPUT_SHA384_B64 "iHzlLvtPRnADdjVlg7fieU9hK9Ak5ElQh925RsRI" \
52                           "xp1W2/cVKpSl5jqA87qfDu14"
53 #define OUTPUT_SHA512_B64 "OpDXljgjXsbEwRvr2E2DwFSbwehO3Etuxwhkh2QS" \
54                           "VstjtU5MstIDKzk5lKomPA274AqfL+nvYDc1IjKh" \
55                           "7sVe5w=="
56 #define OUTPUT_RIPEMD160_B64 "89ZY+tP9+ytSyTac8NRBJJ3fqKA="
57 
58 static const char *expected_outputs[] = {
59     [QCRYPTO_HASH_ALGO_MD5] = OUTPUT_MD5,
60     [QCRYPTO_HASH_ALGO_SHA1] = OUTPUT_SHA1,
61     [QCRYPTO_HASH_ALGO_SHA224] = OUTPUT_SHA224,
62     [QCRYPTO_HASH_ALGO_SHA256] = OUTPUT_SHA256,
63     [QCRYPTO_HASH_ALGO_SHA384] = OUTPUT_SHA384,
64     [QCRYPTO_HASH_ALGO_SHA512] = OUTPUT_SHA512,
65     [QCRYPTO_HASH_ALGO_RIPEMD160] = OUTPUT_RIPEMD160,
66 };
67 static const char *expected_outputs_b64[] = {
68     [QCRYPTO_HASH_ALGO_MD5] = OUTPUT_MD5_B64,
69     [QCRYPTO_HASH_ALGO_SHA1] = OUTPUT_SHA1_B64,
70     [QCRYPTO_HASH_ALGO_SHA224] = OUTPUT_SHA224_B64,
71     [QCRYPTO_HASH_ALGO_SHA256] = OUTPUT_SHA256_B64,
72     [QCRYPTO_HASH_ALGO_SHA384] = OUTPUT_SHA384_B64,
73     [QCRYPTO_HASH_ALGO_SHA512] = OUTPUT_SHA512_B64,
74     [QCRYPTO_HASH_ALGO_RIPEMD160] = OUTPUT_RIPEMD160_B64,
75 };
76 static const int expected_lens[] = {
77     [QCRYPTO_HASH_ALGO_MD5] = 16,
78     [QCRYPTO_HASH_ALGO_SHA1] = 20,
79     [QCRYPTO_HASH_ALGO_SHA224] = 28,
80     [QCRYPTO_HASH_ALGO_SHA256] = 32,
81     [QCRYPTO_HASH_ALGO_SHA384] = 48,
82     [QCRYPTO_HASH_ALGO_SHA512] = 64,
83     [QCRYPTO_HASH_ALGO_RIPEMD160] = 20,
84 };
85 
86 static const char hex[] = "0123456789abcdef";
87 
88 /* Test with dynamic allocation */
89 static void test_hash_alloc(void)
90 {
91     size_t i;
92 
93     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
94         uint8_t *result = NULL;
95         size_t resultlen = 0;
96         int ret;
97         size_t j;
98 
99         if (!qcrypto_hash_supports(i)) {
100             continue;
101         }
102 
103         ret = qcrypto_hash_bytes(i,
104                                  INPUT_TEXT,
105                                  strlen(INPUT_TEXT),
106                                  &result,
107                                  &resultlen,
108                                  &error_fatal);
109         g_assert(ret == 0);
110         g_assert(resultlen == expected_lens[i]);
111 
112         for (j = 0; j < resultlen; j++) {
113             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
114             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
115         }
116         g_free(result);
117     }
118 }
119 
120 /* Test with caller preallocating */
121 static void test_hash_prealloc(void)
122 {
123     size_t i;
124 
125     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
126         uint8_t *result, *origresult;
127         size_t resultlen;
128         int ret;
129         size_t j;
130 
131         if (!qcrypto_hash_supports(i)) {
132             continue;
133         }
134 
135         resultlen = expected_lens[i];
136         origresult = result = g_new0(uint8_t, resultlen);
137 
138         ret = qcrypto_hash_bytes(i,
139                                  INPUT_TEXT,
140                                  strlen(INPUT_TEXT),
141                                  &result,
142                                  &resultlen,
143                                  &error_fatal);
144         g_assert(ret == 0);
145         /* Validate that our pre-allocated pointer was not replaced */
146         g_assert(result == origresult);
147         g_assert(resultlen == expected_lens[i]);
148         for (j = 0; j < resultlen; j++) {
149             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
150             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
151         }
152         g_free(result);
153     }
154 }
155 
156 
157 /* Test with dynamic allocation */
158 static void test_hash_iov(void)
159 {
160     size_t i;
161 
162     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
163         struct iovec iov[3] = {
164             { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
165             { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
166             { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
167         };
168         uint8_t *result = NULL;
169         size_t resultlen = 0;
170         int ret;
171         size_t j;
172 
173         if (!qcrypto_hash_supports(i)) {
174             continue;
175         }
176 
177         ret = qcrypto_hash_bytesv(i,
178                                   iov, 3,
179                                   &result,
180                                   &resultlen,
181                                   &error_fatal);
182         g_assert(ret == 0);
183         g_assert(resultlen == expected_lens[i]);
184         for (j = 0; j < resultlen; j++) {
185             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
186             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
187         }
188         g_free(result);
189     }
190 }
191 
192 
193 /* Test with printable hashing */
194 static void test_hash_digest(void)
195 {
196     size_t i;
197 
198     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
199         int ret;
200         char *digest;
201         size_t digestsize;
202 
203         if (!qcrypto_hash_supports(i)) {
204             continue;
205         }
206 
207         digestsize = qcrypto_hash_digest_len(i);
208 
209         g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i]));
210 
211         ret = qcrypto_hash_digest(i,
212                                   INPUT_TEXT,
213                                   strlen(INPUT_TEXT),
214                                   &digest,
215                                   &error_fatal);
216         g_assert(ret == 0);
217         g_assert_cmpstr(digest, ==, expected_outputs[i]);
218         g_free(digest);
219     }
220 }
221 
222 /* Test with base64 encoding */
223 static void test_hash_base64(void)
224 {
225     size_t i;
226 
227     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
228         int ret;
229         char *digest;
230 
231         if (!qcrypto_hash_supports(i)) {
232             continue;
233         }
234 
235         ret = qcrypto_hash_base64(i,
236                                   INPUT_TEXT,
237                                   strlen(INPUT_TEXT),
238                                   &digest,
239                                   &error_fatal);
240         g_assert(ret == 0);
241         g_assert_cmpstr(digest, ==, expected_outputs_b64[i]);
242         g_free(digest);
243     }
244 }
245 
246 static void test_hash_accumulate(void)
247 {
248     size_t i;
249 
250     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
251         g_autoptr(QCryptoHash) hash = NULL;
252         struct iovec iov[] = {
253             { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
254             { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
255             { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
256         };
257         g_autofree uint8_t *result = NULL;
258         size_t resultlen = 0;
259         int ret;
260         size_t j;
261 
262         if (!qcrypto_hash_supports(i)) {
263             continue;
264         }
265 
266         hash = qcrypto_hash_new(i, &error_fatal);
267         g_assert(hash != NULL);
268 
269         /* Add each iovec to the hash context separately */
270         for (j = 0; j < G_N_ELEMENTS(iov); j++) {
271             ret = qcrypto_hash_updatev(hash,
272                                       &iov[j], 1,
273                                       &error_fatal);
274 
275             g_assert(ret == 0);
276         }
277 
278         ret = qcrypto_hash_finalize_bytes(hash, &result, &resultlen,
279                                           &error_fatal);
280 
281         g_assert(ret == 0);
282         g_assert(resultlen == expected_lens[i]);
283         for (j = 0; j < resultlen; j++) {
284             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
285             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
286         }
287     }
288 }
289 
290 int main(int argc, char **argv)
291 {
292     int ret = qcrypto_init(&error_fatal);
293     g_assert(ret == 0);
294 
295     g_test_init(&argc, &argv, NULL);
296     g_test_add_func("/crypto/hash/iov", test_hash_iov);
297     g_test_add_func("/crypto/hash/alloc", test_hash_alloc);
298     g_test_add_func("/crypto/hash/prealloc", test_hash_prealloc);
299     g_test_add_func("/crypto/hash/digest", test_hash_digest);
300     g_test_add_func("/crypto/hash/base64", test_hash_base64);
301     g_test_add_func("/crypto/hash/accumulate", test_hash_accumulate);
302     return g_test_run();
303 }
304