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