arm/aspeed: Rework NIC attachment
[qemu/ar7.git] / tests / test-crypto-block.c
blob7c1ab07855415a0c2a884b8519e8fe99440bb83b
1 /*
2 * QEMU Crypto block encryption
4 * Copyright (c) 2016 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "crypto/init.h"
24 #include "crypto/block.h"
25 #include "qemu/buffer.h"
26 #include "qemu/module.h"
27 #include "crypto/secret.h"
28 #ifndef _WIN32
29 #include <sys/resource.h>
30 #endif
32 #if (defined(_WIN32) || defined RUSAGE_THREAD) && \
33 (defined(CONFIG_NETTLE) || defined(CONFIG_GCRYPT))
34 #define TEST_LUKS
35 #else
36 #undef TEST_LUKS
37 #endif
39 static QCryptoBlockCreateOptions qcow_create_opts = {
40 .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
41 .u.qcow = {
42 .has_key_secret = true,
43 .key_secret = (char *)"sec0",
47 static QCryptoBlockOpenOptions qcow_open_opts = {
48 .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
49 .u.qcow = {
50 .has_key_secret = true,
51 .key_secret = (char *)"sec0",
56 #ifdef TEST_LUKS
57 static QCryptoBlockOpenOptions luks_open_opts = {
58 .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
59 .u.luks = {
60 .has_key_secret = true,
61 .key_secret = (char *)"sec0",
66 /* Creation with all default values */
67 static QCryptoBlockCreateOptions luks_create_opts_default = {
68 .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
69 .u.luks = {
70 .has_key_secret = true,
71 .key_secret = (char *)"sec0",
76 /* ...and with explicit values */
77 static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_plain64 = {
78 .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
79 .u.luks = {
80 .has_key_secret = true,
81 .key_secret = (char *)"sec0",
82 .has_cipher_alg = true,
83 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
84 .has_cipher_mode = true,
85 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
86 .has_ivgen_alg = true,
87 .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
92 static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_essiv = {
93 .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
94 .u.luks = {
95 .has_key_secret = true,
96 .key_secret = (char *)"sec0",
97 .has_cipher_alg = true,
98 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
99 .has_cipher_mode = true,
100 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
101 .has_ivgen_alg = true,
102 .ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
103 .has_ivgen_hash_alg = true,
104 .ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256,
105 .has_hash_alg = true,
106 .hash_alg = QCRYPTO_HASH_ALG_SHA1,
109 #endif /* TEST_LUKS */
112 static struct QCryptoBlockTestData {
113 const char *path;
114 QCryptoBlockCreateOptions *create_opts;
115 QCryptoBlockOpenOptions *open_opts;
117 bool expect_header;
119 QCryptoCipherAlgorithm cipher_alg;
120 QCryptoCipherMode cipher_mode;
121 QCryptoHashAlgorithm hash_alg;
123 QCryptoIVGenAlgorithm ivgen_alg;
124 QCryptoHashAlgorithm ivgen_hash;
126 bool slow;
127 } test_data[] = {
129 .path = "/crypto/block/qcow",
130 .create_opts = &qcow_create_opts,
131 .open_opts = &qcow_open_opts,
133 .expect_header = false,
135 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_128,
136 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
138 .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
140 #ifdef TEST_LUKS
142 .path = "/crypto/block/luks/default",
143 .create_opts = &luks_create_opts_default,
144 .open_opts = &luks_open_opts,
146 .expect_header = true,
148 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
149 .cipher_mode = QCRYPTO_CIPHER_MODE_XTS,
150 .hash_alg = QCRYPTO_HASH_ALG_SHA256,
152 .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
154 .slow = true,
157 .path = "/crypto/block/luks/aes-256-cbc-plain64",
158 .create_opts = &luks_create_opts_aes256_cbc_plain64,
159 .open_opts = &luks_open_opts,
161 .expect_header = true,
163 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
164 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
165 .hash_alg = QCRYPTO_HASH_ALG_SHA256,
167 .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
169 .slow = true,
172 .path = "/crypto/block/luks/aes-256-cbc-essiv",
173 .create_opts = &luks_create_opts_aes256_cbc_essiv,
174 .open_opts = &luks_open_opts,
176 .expect_header = true,
178 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
179 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
180 .hash_alg = QCRYPTO_HASH_ALG_SHA1,
182 .ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
183 .ivgen_hash = QCRYPTO_HASH_ALG_SHA256,
185 .slow = true,
187 #endif
191 static ssize_t test_block_read_func(QCryptoBlock *block,
192 size_t offset,
193 uint8_t *buf,
194 size_t buflen,
195 void *opaque,
196 Error **errp)
198 Buffer *header = opaque;
200 g_assert_cmpint(offset + buflen, <=, header->capacity);
202 memcpy(buf, header->buffer + offset, buflen);
204 return buflen;
208 static ssize_t test_block_init_func(QCryptoBlock *block,
209 size_t headerlen,
210 void *opaque,
211 Error **errp)
213 Buffer *header = opaque;
215 g_assert_cmpint(header->capacity, ==, 0);
217 buffer_reserve(header, headerlen);
219 return headerlen;
223 static ssize_t test_block_write_func(QCryptoBlock *block,
224 size_t offset,
225 const uint8_t *buf,
226 size_t buflen,
227 void *opaque,
228 Error **errp)
230 Buffer *header = opaque;
232 g_assert_cmpint(buflen + offset, <=, header->capacity);
234 memcpy(header->buffer + offset, buf, buflen);
235 header->offset = offset + buflen;
237 return buflen;
241 static Object *test_block_secret(void)
243 return object_new_with_props(
244 TYPE_QCRYPTO_SECRET,
245 object_get_objects_root(),
246 "sec0",
247 &error_abort,
248 "data", "123456",
249 NULL);
252 static void test_block_assert_setup(const struct QCryptoBlockTestData *data,
253 QCryptoBlock *blk)
255 QCryptoIVGen *ivgen;
256 QCryptoCipher *cipher;
258 ivgen = qcrypto_block_get_ivgen(blk);
259 cipher = qcrypto_block_get_cipher(blk);
261 g_assert(ivgen);
262 g_assert(cipher);
264 g_assert_cmpint(data->cipher_alg, ==, cipher->alg);
265 g_assert_cmpint(data->cipher_mode, ==, cipher->mode);
266 g_assert_cmpint(data->hash_alg, ==,
267 qcrypto_block_get_kdf_hash(blk));
269 g_assert_cmpint(data->ivgen_alg, ==,
270 qcrypto_ivgen_get_algorithm(ivgen));
271 g_assert_cmpint(data->ivgen_hash, ==,
272 qcrypto_ivgen_get_hash(ivgen));
276 static void test_block(gconstpointer opaque)
278 const struct QCryptoBlockTestData *data = opaque;
279 QCryptoBlock *blk;
280 Buffer header;
281 Object *sec = test_block_secret();
283 memset(&header, 0, sizeof(header));
284 buffer_init(&header, "header");
286 blk = qcrypto_block_create(data->create_opts, NULL,
287 test_block_init_func,
288 test_block_write_func,
289 &header,
290 &error_abort);
291 g_assert(blk);
293 if (data->expect_header) {
294 g_assert_cmpint(header.capacity, >, 0);
295 } else {
296 g_assert_cmpint(header.capacity, ==, 0);
299 test_block_assert_setup(data, blk);
301 qcrypto_block_free(blk);
302 object_unparent(sec);
304 /* Ensure we can't open without the secret */
305 blk = qcrypto_block_open(data->open_opts, NULL,
306 test_block_read_func,
307 &header,
310 NULL);
311 g_assert(blk == NULL);
313 /* Ensure we can't open without the secret, unless NO_IO */
314 blk = qcrypto_block_open(data->open_opts, NULL,
315 test_block_read_func,
316 &header,
317 QCRYPTO_BLOCK_OPEN_NO_IO,
319 &error_abort);
321 g_assert(qcrypto_block_get_cipher(blk) == NULL);
322 g_assert(qcrypto_block_get_ivgen(blk) == NULL);
324 qcrypto_block_free(blk);
327 /* Now open for real with secret */
328 sec = test_block_secret();
329 blk = qcrypto_block_open(data->open_opts, NULL,
330 test_block_read_func,
331 &header,
334 &error_abort);
335 g_assert(blk);
337 test_block_assert_setup(data, blk);
339 qcrypto_block_free(blk);
341 object_unparent(sec);
343 buffer_free(&header);
347 int main(int argc, char **argv)
349 gsize i;
351 module_call_init(MODULE_INIT_QOM);
352 g_test_init(&argc, &argv, NULL);
354 g_assert(qcrypto_init(NULL) == 0);
356 for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
357 if (test_data[i].open_opts->format == Q_CRYPTO_BLOCK_FORMAT_LUKS &&
358 !qcrypto_hash_supports(test_data[i].hash_alg)) {
359 continue;
361 if (!test_data[i].slow ||
362 g_test_slow()) {
363 g_test_add_data_func(test_data[i].path, &test_data[i], test_block);
367 return g_test_run();