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"
29 #include <sys/resource.h>
32 #if (defined(_WIN32) || defined RUSAGE_THREAD) && \
33 (defined(CONFIG_NETTLE) || defined(CONFIG_GCRYPT))
39 static QCryptoBlockCreateOptions qcow_create_opts
= {
40 .format
= Q_CRYPTO_BLOCK_FORMAT_QCOW
,
42 .has_key_secret
= true,
43 .key_secret
= (char *)"sec0",
47 static QCryptoBlockOpenOptions qcow_open_opts
= {
48 .format
= Q_CRYPTO_BLOCK_FORMAT_QCOW
,
50 .has_key_secret
= true,
51 .key_secret
= (char *)"sec0",
57 static QCryptoBlockOpenOptions luks_open_opts
= {
58 .format
= Q_CRYPTO_BLOCK_FORMAT_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
,
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
,
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
,
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
{
114 QCryptoBlockCreateOptions
*create_opts
;
115 QCryptoBlockOpenOptions
*open_opts
;
119 QCryptoCipherAlgorithm cipher_alg
;
120 QCryptoCipherMode cipher_mode
;
121 QCryptoHashAlgorithm hash_alg
;
123 QCryptoIVGenAlgorithm ivgen_alg
;
124 QCryptoHashAlgorithm ivgen_hash
;
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
,
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
,
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
,
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
,
191 static ssize_t
test_block_read_func(QCryptoBlock
*block
,
198 Buffer
*header
= opaque
;
200 g_assert_cmpint(offset
+ buflen
, <=, header
->capacity
);
202 memcpy(buf
, header
->buffer
+ offset
, buflen
);
208 static ssize_t
test_block_init_func(QCryptoBlock
*block
,
213 Buffer
*header
= opaque
;
215 g_assert_cmpint(header
->capacity
, ==, 0);
217 buffer_reserve(header
, headerlen
);
223 static ssize_t
test_block_write_func(QCryptoBlock
*block
,
230 Buffer
*header
= opaque
;
232 g_assert_cmpint(buflen
+ offset
, <=, header
->capacity
);
234 memcpy(header
->buffer
+ offset
, buf
, buflen
);
235 header
->offset
= offset
+ buflen
;
241 static Object
*test_block_secret(void)
243 return object_new_with_props(
245 object_get_objects_root(),
252 static void test_block_assert_setup(const struct QCryptoBlockTestData
*data
,
256 QCryptoCipher
*cipher
;
258 ivgen
= qcrypto_block_get_ivgen(blk
);
259 cipher
= qcrypto_block_get_cipher(blk
);
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
;
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
,
293 if (data
->expect_header
) {
294 g_assert_cmpint(header
.capacity
, >, 0);
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
,
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
,
317 QCRYPTO_BLOCK_OPEN_NO_IO
,
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
,
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
)
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
)) {
361 if (!test_data
[i
].slow
||
363 g_test_add_data_func(test_data
[i
].path
, &test_data
[i
], test_block
);