2 * QEMU block full disk encryption
4 * Copyright (c) 2015-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"
23 #include "block/block_int.h"
24 #include "sysemu/block-backend.h"
25 #include "crypto/block.h"
26 #include "qapi/opts-visitor.h"
27 #include "qapi-visit.h"
28 #include "qapi/error.h"
30 #define BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET "key-secret"
31 #define BLOCK_CRYPTO_OPT_LUKS_CIPHER_ALG "cipher-alg"
32 #define BLOCK_CRYPTO_OPT_LUKS_CIPHER_MODE "cipher-mode"
33 #define BLOCK_CRYPTO_OPT_LUKS_IVGEN_ALG "ivgen-alg"
34 #define BLOCK_CRYPTO_OPT_LUKS_IVGEN_HASH_ALG "ivgen-hash-alg"
35 #define BLOCK_CRYPTO_OPT_LUKS_HASH_ALG "hash-alg"
36 #define BLOCK_CRYPTO_OPT_LUKS_ITER_TIME "iter-time"
38 typedef struct BlockCrypto BlockCrypto
;
45 static int block_crypto_probe_generic(QCryptoBlockFormat format
,
50 if (qcrypto_block_has_format(format
, buf
, buf_size
)) {
58 static ssize_t
block_crypto_read_func(QCryptoBlock
*block
,
65 BlockDriverState
*bs
= opaque
;
68 ret
= bdrv_pread(bs
->file
, offset
, buf
, buflen
);
70 error_setg_errno(errp
, -ret
, "Could not read encryption header");
77 struct BlockCryptoCreateData
{
85 static ssize_t
block_crypto_write_func(QCryptoBlock
*block
,
92 struct BlockCryptoCreateData
*data
= opaque
;
95 ret
= blk_pwrite(data
->blk
, offset
, buf
, buflen
, 0);
97 error_setg_errno(errp
, -ret
, "Could not write encryption header");
104 static ssize_t
block_crypto_init_func(QCryptoBlock
*block
,
109 struct BlockCryptoCreateData
*data
= opaque
;
112 /* User provided size should reflect amount of space made
113 * available to the guest, so we must take account of that
114 * which will be used by the crypto header
116 data
->size
+= headerlen
;
118 qemu_opt_set_number(data
->opts
, BLOCK_OPT_SIZE
, data
->size
, &error_abort
);
119 ret
= bdrv_create_file(data
->filename
, data
->opts
, errp
);
124 data
->blk
= blk_new_open(data
->filename
, NULL
, NULL
,
125 BDRV_O_RDWR
| BDRV_O_PROTOCOL
, errp
);
134 static QemuOptsList block_crypto_runtime_opts_luks
= {
136 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks
.head
),
139 .name
= BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
140 .type
= QEMU_OPT_STRING
,
141 .help
= "ID of the secret that provides the encryption key",
143 { /* end of list */ }
148 static QemuOptsList block_crypto_create_opts_luks
= {
150 .head
= QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks
.head
),
153 .name
= BLOCK_OPT_SIZE
,
154 .type
= QEMU_OPT_SIZE
,
155 .help
= "Virtual disk size"
158 .name
= BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET
,
159 .type
= QEMU_OPT_STRING
,
160 .help
= "ID of the secret that provides the encryption key",
163 .name
= BLOCK_CRYPTO_OPT_LUKS_CIPHER_ALG
,
164 .type
= QEMU_OPT_STRING
,
165 .help
= "Name of encryption cipher algorithm",
168 .name
= BLOCK_CRYPTO_OPT_LUKS_CIPHER_MODE
,
169 .type
= QEMU_OPT_STRING
,
170 .help
= "Name of encryption cipher mode",
173 .name
= BLOCK_CRYPTO_OPT_LUKS_IVGEN_ALG
,
174 .type
= QEMU_OPT_STRING
,
175 .help
= "Name of IV generator algorithm",
178 .name
= BLOCK_CRYPTO_OPT_LUKS_IVGEN_HASH_ALG
,
179 .type
= QEMU_OPT_STRING
,
180 .help
= "Name of IV generator hash algorithm",
183 .name
= BLOCK_CRYPTO_OPT_LUKS_HASH_ALG
,
184 .type
= QEMU_OPT_STRING
,
185 .help
= "Name of encryption hash algorithm",
188 .name
= BLOCK_CRYPTO_OPT_LUKS_ITER_TIME
,
189 .type
= QEMU_OPT_NUMBER
,
190 .help
= "Time to spend in PBKDF in milliseconds",
192 { /* end of list */ }
197 static QCryptoBlockOpenOptions
*
198 block_crypto_open_opts_init(QCryptoBlockFormat format
,
203 QCryptoBlockOpenOptions
*ret
= NULL
;
204 Error
*local_err
= NULL
;
206 ret
= g_new0(QCryptoBlockOpenOptions
, 1);
207 ret
->format
= format
;
209 v
= opts_visitor_new(opts
);
211 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
217 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
218 visit_type_QCryptoBlockOptionsLUKS_members(
219 v
, &ret
->u
.luks
, &local_err
);
223 error_setg(&local_err
, "Unsupported block format %d", format
);
227 visit_check_struct(v
, &local_err
);
230 visit_end_struct(v
, NULL
);
234 error_propagate(errp
, local_err
);
235 qapi_free_QCryptoBlockOpenOptions(ret
);
243 static QCryptoBlockCreateOptions
*
244 block_crypto_create_opts_init(QCryptoBlockFormat format
,
249 QCryptoBlockCreateOptions
*ret
= NULL
;
250 Error
*local_err
= NULL
;
252 ret
= g_new0(QCryptoBlockCreateOptions
, 1);
253 ret
->format
= format
;
255 v
= opts_visitor_new(opts
);
257 visit_start_struct(v
, NULL
, NULL
, 0, &local_err
);
263 case Q_CRYPTO_BLOCK_FORMAT_LUKS
:
264 visit_type_QCryptoBlockCreateOptionsLUKS_members(
265 v
, &ret
->u
.luks
, &local_err
);
269 error_setg(&local_err
, "Unsupported block format %d", format
);
273 visit_check_struct(v
, &local_err
);
276 visit_end_struct(v
, NULL
);
280 error_propagate(errp
, local_err
);
281 qapi_free_QCryptoBlockCreateOptions(ret
);
289 static int block_crypto_open_generic(QCryptoBlockFormat format
,
290 QemuOptsList
*opts_spec
,
291 BlockDriverState
*bs
,
296 BlockCrypto
*crypto
= bs
->opaque
;
297 QemuOpts
*opts
= NULL
;
298 Error
*local_err
= NULL
;
300 QCryptoBlockOpenOptions
*open_opts
= NULL
;
301 unsigned int cflags
= 0;
303 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
309 opts
= qemu_opts_create(opts_spec
, NULL
, 0, &error_abort
);
310 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
312 error_propagate(errp
, local_err
);
316 open_opts
= block_crypto_open_opts_init(format
, opts
, errp
);
321 if (flags
& BDRV_O_NO_IO
) {
322 cflags
|= QCRYPTO_BLOCK_OPEN_NO_IO
;
324 crypto
->block
= qcrypto_block_open(open_opts
,
325 block_crypto_read_func
,
330 if (!crypto
->block
) {
335 bs
->encrypted
= true;
336 bs
->valid_key
= true;
340 qapi_free_QCryptoBlockOpenOptions(open_opts
);
345 static int block_crypto_create_generic(QCryptoBlockFormat format
,
346 const char *filename
,
351 QCryptoBlockCreateOptions
*create_opts
= NULL
;
352 QCryptoBlock
*crypto
= NULL
;
353 struct BlockCryptoCreateData data
= {
354 .size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
357 .filename
= filename
,
360 create_opts
= block_crypto_create_opts_init(format
, opts
, errp
);
365 crypto
= qcrypto_block_create(create_opts
,
366 block_crypto_init_func
,
367 block_crypto_write_func
,
378 qcrypto_block_free(crypto
);
380 qapi_free_QCryptoBlockCreateOptions(create_opts
);
384 static int block_crypto_truncate(BlockDriverState
*bs
, int64_t offset
)
386 BlockCrypto
*crypto
= bs
->opaque
;
387 size_t payload_offset
=
388 qcrypto_block_get_payload_offset(crypto
->block
);
390 offset
+= payload_offset
;
392 return bdrv_truncate(bs
->file
, offset
);
395 static void block_crypto_close(BlockDriverState
*bs
)
397 BlockCrypto
*crypto
= bs
->opaque
;
398 qcrypto_block_free(crypto
->block
);
402 #define BLOCK_CRYPTO_MAX_SECTORS 32
404 static coroutine_fn
int
405 block_crypto_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
406 int remaining_sectors
, QEMUIOVector
*qiov
)
408 BlockCrypto
*crypto
= bs
->opaque
;
409 int cur_nr_sectors
; /* number of sectors in current iteration */
410 uint64_t bytes_done
= 0;
411 uint8_t *cipher_data
= NULL
;
412 QEMUIOVector hd_qiov
;
414 size_t payload_offset
=
415 qcrypto_block_get_payload_offset(crypto
->block
) / 512;
417 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
419 /* Bounce buffer so we have a linear mem region for
420 * entire sector. XXX optimize so we avoid bounce
421 * buffer in case that qiov->niov == 1
424 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_SECTORS
* 512,
426 if (cipher_data
== NULL
) {
431 while (remaining_sectors
) {
432 cur_nr_sectors
= remaining_sectors
;
434 if (cur_nr_sectors
> BLOCK_CRYPTO_MAX_SECTORS
) {
435 cur_nr_sectors
= BLOCK_CRYPTO_MAX_SECTORS
;
438 qemu_iovec_reset(&hd_qiov
);
439 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_nr_sectors
* 512);
441 ret
= bdrv_co_readv(bs
->file
,
442 payload_offset
+ sector_num
,
443 cur_nr_sectors
, &hd_qiov
);
448 if (qcrypto_block_decrypt(crypto
->block
,
450 cipher_data
, cur_nr_sectors
* 512,
456 qemu_iovec_from_buf(qiov
, bytes_done
,
457 cipher_data
, cur_nr_sectors
* 512);
459 remaining_sectors
-= cur_nr_sectors
;
460 sector_num
+= cur_nr_sectors
;
461 bytes_done
+= cur_nr_sectors
* 512;
465 qemu_iovec_destroy(&hd_qiov
);
466 qemu_vfree(cipher_data
);
472 static coroutine_fn
int
473 block_crypto_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
474 int remaining_sectors
, QEMUIOVector
*qiov
)
476 BlockCrypto
*crypto
= bs
->opaque
;
477 int cur_nr_sectors
; /* number of sectors in current iteration */
478 uint64_t bytes_done
= 0;
479 uint8_t *cipher_data
= NULL
;
480 QEMUIOVector hd_qiov
;
482 size_t payload_offset
=
483 qcrypto_block_get_payload_offset(crypto
->block
) / 512;
485 qemu_iovec_init(&hd_qiov
, qiov
->niov
);
487 /* Bounce buffer so we have a linear mem region for
488 * entire sector. XXX optimize so we avoid bounce
489 * buffer in case that qiov->niov == 1
492 qemu_try_blockalign(bs
->file
->bs
, MIN(BLOCK_CRYPTO_MAX_SECTORS
* 512,
494 if (cipher_data
== NULL
) {
499 while (remaining_sectors
) {
500 cur_nr_sectors
= remaining_sectors
;
502 if (cur_nr_sectors
> BLOCK_CRYPTO_MAX_SECTORS
) {
503 cur_nr_sectors
= BLOCK_CRYPTO_MAX_SECTORS
;
506 qemu_iovec_to_buf(qiov
, bytes_done
,
507 cipher_data
, cur_nr_sectors
* 512);
509 if (qcrypto_block_encrypt(crypto
->block
,
511 cipher_data
, cur_nr_sectors
* 512,
517 qemu_iovec_reset(&hd_qiov
);
518 qemu_iovec_add(&hd_qiov
, cipher_data
, cur_nr_sectors
* 512);
520 ret
= bdrv_co_writev(bs
->file
,
521 payload_offset
+ sector_num
,
522 cur_nr_sectors
, &hd_qiov
);
527 remaining_sectors
-= cur_nr_sectors
;
528 sector_num
+= cur_nr_sectors
;
529 bytes_done
+= cur_nr_sectors
* 512;
533 qemu_iovec_destroy(&hd_qiov
);
534 qemu_vfree(cipher_data
);
540 static int64_t block_crypto_getlength(BlockDriverState
*bs
)
542 BlockCrypto
*crypto
= bs
->opaque
;
543 int64_t len
= bdrv_getlength(bs
->file
->bs
);
545 ssize_t offset
= qcrypto_block_get_payload_offset(crypto
->block
);
553 static int block_crypto_probe_luks(const uint8_t *buf
,
555 const char *filename
) {
556 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
557 buf
, buf_size
, filename
);
560 static int block_crypto_open_luks(BlockDriverState
*bs
,
565 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
566 &block_crypto_runtime_opts_luks
,
567 bs
, options
, flags
, errp
);
570 static int block_crypto_create_luks(const char *filename
,
574 return block_crypto_create_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS
,
575 filename
, opts
, errp
);
578 static int block_crypto_get_info_luks(BlockDriverState
*bs
,
579 BlockDriverInfo
*bdi
)
581 BlockDriverInfo subbdi
;
584 ret
= bdrv_get_info(bs
->file
->bs
, &subbdi
);
589 bdi
->unallocated_blocks_are_zero
= false;
590 bdi
->can_write_zeroes_with_unmap
= false;
591 bdi
->cluster_size
= subbdi
.cluster_size
;
596 static ImageInfoSpecific
*
597 block_crypto_get_specific_info_luks(BlockDriverState
*bs
)
599 BlockCrypto
*crypto
= bs
->opaque
;
600 ImageInfoSpecific
*spec_info
;
601 QCryptoBlockInfo
*info
;
603 info
= qcrypto_block_get_info(crypto
->block
, NULL
);
607 if (info
->format
!= Q_CRYPTO_BLOCK_FORMAT_LUKS
) {
608 qapi_free_QCryptoBlockInfo(info
);
612 spec_info
= g_new(ImageInfoSpecific
, 1);
613 spec_info
->type
= IMAGE_INFO_SPECIFIC_KIND_LUKS
;
614 spec_info
->u
.luks
.data
= g_new(QCryptoBlockInfoLUKS
, 1);
615 *spec_info
->u
.luks
.data
= info
->u
.luks
;
617 /* Blank out pointers we've just stolen to avoid double free */
618 memset(&info
->u
.luks
, 0, sizeof(info
->u
.luks
));
620 qapi_free_QCryptoBlockInfo(info
);
625 BlockDriver bdrv_crypto_luks
= {
626 .format_name
= "luks",
627 .instance_size
= sizeof(BlockCrypto
),
628 .bdrv_probe
= block_crypto_probe_luks
,
629 .bdrv_open
= block_crypto_open_luks
,
630 .bdrv_close
= block_crypto_close
,
631 .bdrv_create
= block_crypto_create_luks
,
632 .bdrv_truncate
= block_crypto_truncate
,
633 .create_opts
= &block_crypto_create_opts_luks
,
635 .bdrv_co_readv
= block_crypto_co_readv
,
636 .bdrv_co_writev
= block_crypto_co_writev
,
637 .bdrv_getlength
= block_crypto_getlength
,
638 .bdrv_get_info
= block_crypto_get_info_luks
,
639 .bdrv_get_specific_info
= block_crypto_get_specific_info_luks
,
642 static void block_crypto_init(void)
644 bdrv_register(&bdrv_crypto_luks
);
647 block_init(block_crypto_init
);