xref: /openbmc/qemu/tests/unit/test-crypto-hash.c (revision 2e1cacfb)
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;
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         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 
146         g_assert(resultlen == expected_lens[i]);
147         for (j = 0; j < resultlen; j++) {
148             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
149             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
150         }
151         g_free(result);
152     }
153 }
154 
155 
156 /* Test with dynamic allocation */
157 static void test_hash_iov(void)
158 {
159     size_t i;
160 
161     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
162         struct iovec iov[3] = {
163             { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
164             { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
165             { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
166         };
167         uint8_t *result = NULL;
168         size_t resultlen = 0;
169         int ret;
170         size_t j;
171 
172         if (!qcrypto_hash_supports(i)) {
173             continue;
174         }
175 
176         ret = qcrypto_hash_bytesv(i,
177                                   iov, 3,
178                                   &result,
179                                   &resultlen,
180                                   &error_fatal);
181         g_assert(ret == 0);
182         g_assert(resultlen == expected_lens[i]);
183         for (j = 0; j < resultlen; j++) {
184             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
185             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
186         }
187         g_free(result);
188     }
189 }
190 
191 
192 /* Test with printable hashing */
193 static void test_hash_digest(void)
194 {
195     size_t i;
196 
197     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
198         int ret;
199         char *digest;
200         size_t digestsize;
201 
202         if (!qcrypto_hash_supports(i)) {
203             continue;
204         }
205 
206         digestsize = qcrypto_hash_digest_len(i);
207 
208         g_assert_cmpint(digestsize * 2, ==, strlen(expected_outputs[i]));
209 
210         ret = qcrypto_hash_digest(i,
211                                   INPUT_TEXT,
212                                   strlen(INPUT_TEXT),
213                                   &digest,
214                                   &error_fatal);
215         g_assert(ret == 0);
216         g_assert_cmpstr(digest, ==, expected_outputs[i]);
217         g_free(digest);
218     }
219 }
220 
221 /* Test with base64 encoding */
222 static void test_hash_base64(void)
223 {
224     size_t i;
225 
226     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
227         int ret;
228         char *digest;
229 
230         if (!qcrypto_hash_supports(i)) {
231             continue;
232         }
233 
234         ret = qcrypto_hash_base64(i,
235                                   INPUT_TEXT,
236                                   strlen(INPUT_TEXT),
237                                   &digest,
238                                   &error_fatal);
239         g_assert(ret == 0);
240         g_assert_cmpstr(digest, ==, expected_outputs_b64[i]);
241         g_free(digest);
242     }
243 }
244 
245 static void test_hash_accumulate(void)
246 {
247     size_t i;
248 
249     for (i = 0; i < G_N_ELEMENTS(expected_outputs) ; i++) {
250         g_autoptr(QCryptoHash) hash = NULL;
251         struct iovec iov[] = {
252             { .iov_base = (char *)INPUT_TEXT1, .iov_len = strlen(INPUT_TEXT1) },
253             { .iov_base = (char *)INPUT_TEXT2, .iov_len = strlen(INPUT_TEXT2) },
254             { .iov_base = (char *)INPUT_TEXT3, .iov_len = strlen(INPUT_TEXT3) },
255         };
256         g_autofree uint8_t *result = NULL;
257         size_t resultlen = 0;
258         int ret;
259         size_t j;
260 
261         if (!qcrypto_hash_supports(i)) {
262             continue;
263         }
264 
265         hash = qcrypto_hash_new(i, &error_fatal);
266         g_assert(hash != NULL);
267 
268         /* Add each iovec to the hash context separately */
269         for (j = 0; j < G_N_ELEMENTS(iov); j++) {
270             ret = qcrypto_hash_updatev(hash,
271                                       &iov[j], 1,
272                                       &error_fatal);
273 
274             g_assert(ret == 0);
275         }
276 
277         ret = qcrypto_hash_finalize_bytes(hash, &result, &resultlen,
278                                           &error_fatal);
279 
280         g_assert(ret == 0);
281         g_assert(resultlen == expected_lens[i]);
282         for (j = 0; j < resultlen; j++) {
283             g_assert(expected_outputs[i][j * 2] == hex[(result[j] >> 4) & 0xf]);
284             g_assert(expected_outputs[i][j * 2 + 1] == hex[result[j] & 0xf]);
285         }
286     }
287 }
288 
289 int main(int argc, char **argv)
290 {
291     int ret = qcrypto_init(&error_fatal);
292     g_assert(ret == 0);
293 
294     g_test_init(&argc, &argv, NULL);
295     g_test_add_func("/crypto/hash/iov", test_hash_iov);
296     g_test_add_func("/crypto/hash/alloc", test_hash_alloc);
297     g_test_add_func("/crypto/hash/prealloc", test_hash_prealloc);
298     g_test_add_func("/crypto/hash/digest", test_hash_digest);
299     g_test_add_func("/crypto/hash/base64", test_hash_base64);
300     g_test_add_func("/crypto/hash/accumulate", test_hash_accumulate);
301     return g_test_run();
302 }
303