target/riscv: widening floating-point/integer type-convert instructions
[qemu/ar7.git] / block / crypto.c
blob973b57b3eb7496ab36a96c7bd3472cdd37432903
1 /*
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 "block/qdict.h"
25 #include "sysemu/block-backend.h"
26 #include "crypto/block.h"
27 #include "qapi/opts-visitor.h"
28 #include "qapi/qapi-visit-crypto.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/error.h"
31 #include "qemu/module.h"
32 #include "qemu/option.h"
33 #include "qemu/cutils.h"
34 #include "crypto.h"
36 typedef struct BlockCrypto BlockCrypto;
38 struct BlockCrypto {
39 QCryptoBlock *block;
43 static int block_crypto_probe_generic(QCryptoBlockFormat format,
44 const uint8_t *buf,
45 int buf_size,
46 const char *filename)
48 if (qcrypto_block_has_format(format, buf, buf_size)) {
49 return 100;
50 } else {
51 return 0;
56 static ssize_t block_crypto_read_func(QCryptoBlock *block,
57 size_t offset,
58 uint8_t *buf,
59 size_t buflen,
60 void *opaque,
61 Error **errp)
63 BlockDriverState *bs = opaque;
64 ssize_t ret;
66 ret = bdrv_pread(bs->file, offset, buf, buflen);
67 if (ret < 0) {
68 error_setg_errno(errp, -ret, "Could not read encryption header");
69 return ret;
71 return ret;
75 struct BlockCryptoCreateData {
76 BlockBackend *blk;
77 uint64_t size;
78 PreallocMode prealloc;
82 static ssize_t block_crypto_write_func(QCryptoBlock *block,
83 size_t offset,
84 const uint8_t *buf,
85 size_t buflen,
86 void *opaque,
87 Error **errp)
89 struct BlockCryptoCreateData *data = opaque;
90 ssize_t ret;
92 ret = blk_pwrite(data->blk, offset, buf, buflen, 0);
93 if (ret < 0) {
94 error_setg_errno(errp, -ret, "Could not write encryption header");
95 return ret;
97 return ret;
101 static ssize_t block_crypto_init_func(QCryptoBlock *block,
102 size_t headerlen,
103 void *opaque,
104 Error **errp)
106 struct BlockCryptoCreateData *data = opaque;
107 Error *local_error = NULL;
108 int ret;
110 if (data->size > INT64_MAX || headerlen > INT64_MAX - data->size) {
111 ret = -EFBIG;
112 goto error;
115 /* User provided size should reflect amount of space made
116 * available to the guest, so we must take account of that
117 * which will be used by the crypto header
119 ret = blk_truncate(data->blk, data->size + headerlen, false,
120 data->prealloc, 0, &local_error);
122 if (ret >= 0) {
123 return ret;
126 error:
127 if (ret == -EFBIG) {
128 /* Replace the error message with a better one */
129 error_free(local_error);
130 error_setg(errp, "The requested file size is too large");
131 } else {
132 error_propagate(errp, local_error);
135 return ret;
139 static QemuOptsList block_crypto_runtime_opts_luks = {
140 .name = "crypto",
141 .head = QTAILQ_HEAD_INITIALIZER(block_crypto_runtime_opts_luks.head),
142 .desc = {
143 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
144 { /* end of list */ }
149 static QemuOptsList block_crypto_create_opts_luks = {
150 .name = "crypto",
151 .head = QTAILQ_HEAD_INITIALIZER(block_crypto_create_opts_luks.head),
152 .desc = {
154 .name = BLOCK_OPT_SIZE,
155 .type = QEMU_OPT_SIZE,
156 .help = "Virtual disk size"
158 BLOCK_CRYPTO_OPT_DEF_LUKS_KEY_SECRET(""),
159 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_ALG(""),
160 BLOCK_CRYPTO_OPT_DEF_LUKS_CIPHER_MODE(""),
161 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_ALG(""),
162 BLOCK_CRYPTO_OPT_DEF_LUKS_IVGEN_HASH_ALG(""),
163 BLOCK_CRYPTO_OPT_DEF_LUKS_HASH_ALG(""),
164 BLOCK_CRYPTO_OPT_DEF_LUKS_ITER_TIME(""),
165 { /* end of list */ }
170 QCryptoBlockOpenOptions *
171 block_crypto_open_opts_init(QDict *opts, Error **errp)
173 Visitor *v;
174 QCryptoBlockOpenOptions *ret;
176 v = qobject_input_visitor_new_flat_confused(opts, errp);
177 if (!v) {
178 return NULL;
181 visit_type_QCryptoBlockOpenOptions(v, NULL, &ret, errp);
183 visit_free(v);
184 return ret;
188 QCryptoBlockCreateOptions *
189 block_crypto_create_opts_init(QDict *opts, Error **errp)
191 Visitor *v;
192 QCryptoBlockCreateOptions *ret;
194 v = qobject_input_visitor_new_flat_confused(opts, errp);
195 if (!v) {
196 return NULL;
199 visit_type_QCryptoBlockCreateOptions(v, NULL, &ret, errp);
201 visit_free(v);
202 return ret;
206 static int block_crypto_open_generic(QCryptoBlockFormat format,
207 QemuOptsList *opts_spec,
208 BlockDriverState *bs,
209 QDict *options,
210 int flags,
211 Error **errp)
213 BlockCrypto *crypto = bs->opaque;
214 QemuOpts *opts = NULL;
215 Error *local_err = NULL;
216 int ret = -EINVAL;
217 QCryptoBlockOpenOptions *open_opts = NULL;
218 unsigned int cflags = 0;
219 QDict *cryptoopts = NULL;
221 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
222 BDRV_CHILD_IMAGE, false, errp);
223 if (!bs->file) {
224 return -EINVAL;
227 bs->supported_write_flags = BDRV_REQ_FUA &
228 bs->file->bs->supported_write_flags;
230 opts = qemu_opts_create(opts_spec, NULL, 0, &error_abort);
231 qemu_opts_absorb_qdict(opts, options, &local_err);
232 if (local_err) {
233 error_propagate(errp, local_err);
234 goto cleanup;
237 cryptoopts = qemu_opts_to_qdict(opts, NULL);
238 qdict_put_str(cryptoopts, "format", QCryptoBlockFormat_str(format));
240 open_opts = block_crypto_open_opts_init(cryptoopts, errp);
241 if (!open_opts) {
242 goto cleanup;
245 if (flags & BDRV_O_NO_IO) {
246 cflags |= QCRYPTO_BLOCK_OPEN_NO_IO;
248 crypto->block = qcrypto_block_open(open_opts, NULL,
249 block_crypto_read_func,
251 cflags,
253 errp);
255 if (!crypto->block) {
256 ret = -EIO;
257 goto cleanup;
260 bs->encrypted = true;
262 ret = 0;
263 cleanup:
264 qobject_unref(cryptoopts);
265 qapi_free_QCryptoBlockOpenOptions(open_opts);
266 return ret;
270 static int block_crypto_co_create_generic(BlockDriverState *bs,
271 int64_t size,
272 QCryptoBlockCreateOptions *opts,
273 PreallocMode prealloc,
274 Error **errp)
276 int ret;
277 BlockBackend *blk;
278 QCryptoBlock *crypto = NULL;
279 struct BlockCryptoCreateData data;
281 blk = blk_new_with_bs(bs, BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL,
282 errp);
283 if (!blk) {
284 ret = -EPERM;
285 goto cleanup;
288 if (prealloc == PREALLOC_MODE_METADATA) {
289 prealloc = PREALLOC_MODE_OFF;
292 data = (struct BlockCryptoCreateData) {
293 .blk = blk,
294 .size = size,
295 .prealloc = prealloc,
298 crypto = qcrypto_block_create(opts, NULL,
299 block_crypto_init_func,
300 block_crypto_write_func,
301 &data,
302 errp);
304 if (!crypto) {
305 ret = -EIO;
306 goto cleanup;
309 ret = 0;
310 cleanup:
311 qcrypto_block_free(crypto);
312 blk_unref(blk);
313 return ret;
316 static int coroutine_fn
317 block_crypto_co_truncate(BlockDriverState *bs, int64_t offset, bool exact,
318 PreallocMode prealloc, BdrvRequestFlags flags,
319 Error **errp)
321 BlockCrypto *crypto = bs->opaque;
322 uint64_t payload_offset =
323 qcrypto_block_get_payload_offset(crypto->block);
325 if (payload_offset > INT64_MAX - offset) {
326 error_setg(errp, "The requested file size is too large");
327 return -EFBIG;
330 offset += payload_offset;
332 return bdrv_co_truncate(bs->file, offset, exact, prealloc, 0, errp);
335 static void block_crypto_close(BlockDriverState *bs)
337 BlockCrypto *crypto = bs->opaque;
338 qcrypto_block_free(crypto->block);
341 static int block_crypto_reopen_prepare(BDRVReopenState *state,
342 BlockReopenQueue *queue, Error **errp)
344 /* nothing needs checking */
345 return 0;
349 * 1 MB bounce buffer gives good performance / memory tradeoff
350 * when using cache=none|directsync.
352 #define BLOCK_CRYPTO_MAX_IO_SIZE (1024 * 1024)
354 static coroutine_fn int
355 block_crypto_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
356 QEMUIOVector *qiov, int flags)
358 BlockCrypto *crypto = bs->opaque;
359 uint64_t cur_bytes; /* number of bytes in current iteration */
360 uint64_t bytes_done = 0;
361 uint8_t *cipher_data = NULL;
362 QEMUIOVector hd_qiov;
363 int ret = 0;
364 uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
365 uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block);
367 assert(!flags);
368 assert(payload_offset < INT64_MAX);
369 assert(QEMU_IS_ALIGNED(offset, sector_size));
370 assert(QEMU_IS_ALIGNED(bytes, sector_size));
372 qemu_iovec_init(&hd_qiov, qiov->niov);
374 /* Bounce buffer because we don't wish to expose cipher text
375 * in qiov which points to guest memory.
377 cipher_data =
378 qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_IO_SIZE,
379 qiov->size));
380 if (cipher_data == NULL) {
381 ret = -ENOMEM;
382 goto cleanup;
385 while (bytes) {
386 cur_bytes = MIN(bytes, BLOCK_CRYPTO_MAX_IO_SIZE);
388 qemu_iovec_reset(&hd_qiov);
389 qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes);
391 ret = bdrv_co_preadv(bs->file, payload_offset + offset + bytes_done,
392 cur_bytes, &hd_qiov, 0);
393 if (ret < 0) {
394 goto cleanup;
397 if (qcrypto_block_decrypt(crypto->block, offset + bytes_done,
398 cipher_data, cur_bytes, NULL) < 0) {
399 ret = -EIO;
400 goto cleanup;
403 qemu_iovec_from_buf(qiov, bytes_done, cipher_data, cur_bytes);
405 bytes -= cur_bytes;
406 bytes_done += cur_bytes;
409 cleanup:
410 qemu_iovec_destroy(&hd_qiov);
411 qemu_vfree(cipher_data);
413 return ret;
417 static coroutine_fn int
418 block_crypto_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
419 QEMUIOVector *qiov, int flags)
421 BlockCrypto *crypto = bs->opaque;
422 uint64_t cur_bytes; /* number of bytes in current iteration */
423 uint64_t bytes_done = 0;
424 uint8_t *cipher_data = NULL;
425 QEMUIOVector hd_qiov;
426 int ret = 0;
427 uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
428 uint64_t payload_offset = qcrypto_block_get_payload_offset(crypto->block);
430 assert(!(flags & ~BDRV_REQ_FUA));
431 assert(payload_offset < INT64_MAX);
432 assert(QEMU_IS_ALIGNED(offset, sector_size));
433 assert(QEMU_IS_ALIGNED(bytes, sector_size));
435 qemu_iovec_init(&hd_qiov, qiov->niov);
437 /* Bounce buffer because we're not permitted to touch
438 * contents of qiov - it points to guest memory.
440 cipher_data =
441 qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_IO_SIZE,
442 qiov->size));
443 if (cipher_data == NULL) {
444 ret = -ENOMEM;
445 goto cleanup;
448 while (bytes) {
449 cur_bytes = MIN(bytes, BLOCK_CRYPTO_MAX_IO_SIZE);
451 qemu_iovec_to_buf(qiov, bytes_done, cipher_data, cur_bytes);
453 if (qcrypto_block_encrypt(crypto->block, offset + bytes_done,
454 cipher_data, cur_bytes, NULL) < 0) {
455 ret = -EIO;
456 goto cleanup;
459 qemu_iovec_reset(&hd_qiov);
460 qemu_iovec_add(&hd_qiov, cipher_data, cur_bytes);
462 ret = bdrv_co_pwritev(bs->file, payload_offset + offset + bytes_done,
463 cur_bytes, &hd_qiov, flags);
464 if (ret < 0) {
465 goto cleanup;
468 bytes -= cur_bytes;
469 bytes_done += cur_bytes;
472 cleanup:
473 qemu_iovec_destroy(&hd_qiov);
474 qemu_vfree(cipher_data);
476 return ret;
479 static void block_crypto_refresh_limits(BlockDriverState *bs, Error **errp)
481 BlockCrypto *crypto = bs->opaque;
482 uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
483 bs->bl.request_alignment = sector_size; /* No sub-sector I/O */
487 static int64_t block_crypto_getlength(BlockDriverState *bs)
489 BlockCrypto *crypto = bs->opaque;
490 int64_t len = bdrv_getlength(bs->file->bs);
492 uint64_t offset = qcrypto_block_get_payload_offset(crypto->block);
493 assert(offset < INT64_MAX);
495 if (offset > len) {
496 return -EIO;
499 len -= offset;
501 return len;
505 static BlockMeasureInfo *block_crypto_measure(QemuOpts *opts,
506 BlockDriverState *in_bs,
507 Error **errp)
509 g_autoptr(QCryptoBlockCreateOptions) create_opts = NULL;
510 Error *local_err = NULL;
511 BlockMeasureInfo *info;
512 uint64_t size;
513 size_t luks_payload_size;
514 QDict *cryptoopts;
517 * Preallocation mode doesn't affect size requirements but we must consume
518 * the option.
520 g_free(qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC));
522 size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
524 if (in_bs) {
525 int64_t ssize = bdrv_getlength(in_bs);
527 if (ssize < 0) {
528 error_setg_errno(&local_err, -ssize,
529 "Unable to get image virtual_size");
530 goto err;
533 size = ssize;
536 cryptoopts = qemu_opts_to_qdict_filtered(opts, NULL,
537 &block_crypto_create_opts_luks, true);
538 qdict_put_str(cryptoopts, "format", "luks");
539 create_opts = block_crypto_create_opts_init(cryptoopts, &local_err);
540 qobject_unref(cryptoopts);
541 if (!create_opts) {
542 goto err;
545 if (!qcrypto_block_calculate_payload_offset(create_opts, NULL,
546 &luks_payload_size,
547 &local_err)) {
548 goto err;
552 * Unallocated blocks are still encrypted so allocation status makes no
553 * difference to the file size.
555 info = g_new0(BlockMeasureInfo, 1);
556 info->fully_allocated = luks_payload_size + size;
557 info->required = luks_payload_size + size;
558 return info;
560 err:
561 error_propagate(errp, local_err);
562 return NULL;
566 static int block_crypto_probe_luks(const uint8_t *buf,
567 int buf_size,
568 const char *filename) {
569 return block_crypto_probe_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS,
570 buf, buf_size, filename);
573 static int block_crypto_open_luks(BlockDriverState *bs,
574 QDict *options,
575 int flags,
576 Error **errp)
578 return block_crypto_open_generic(Q_CRYPTO_BLOCK_FORMAT_LUKS,
579 &block_crypto_runtime_opts_luks,
580 bs, options, flags, errp);
583 static int coroutine_fn
584 block_crypto_co_create_luks(BlockdevCreateOptions *create_options, Error **errp)
586 BlockdevCreateOptionsLUKS *luks_opts;
587 BlockDriverState *bs = NULL;
588 QCryptoBlockCreateOptions create_opts;
589 PreallocMode preallocation = PREALLOC_MODE_OFF;
590 int ret;
592 assert(create_options->driver == BLOCKDEV_DRIVER_LUKS);
593 luks_opts = &create_options->u.luks;
595 bs = bdrv_open_blockdev_ref(luks_opts->file, errp);
596 if (bs == NULL) {
597 return -EIO;
600 create_opts = (QCryptoBlockCreateOptions) {
601 .format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
602 .u.luks = *qapi_BlockdevCreateOptionsLUKS_base(luks_opts),
605 if (luks_opts->has_preallocation) {
606 preallocation = luks_opts->preallocation;
609 ret = block_crypto_co_create_generic(bs, luks_opts->size, &create_opts,
610 preallocation, errp);
611 if (ret < 0) {
612 goto fail;
615 ret = 0;
616 fail:
617 bdrv_unref(bs);
618 return ret;
621 static int coroutine_fn block_crypto_co_create_opts_luks(BlockDriver *drv,
622 const char *filename,
623 QemuOpts *opts,
624 Error **errp)
626 QCryptoBlockCreateOptions *create_opts = NULL;
627 BlockDriverState *bs = NULL;
628 QDict *cryptoopts;
629 PreallocMode prealloc;
630 char *buf = NULL;
631 int64_t size;
632 int ret;
633 Error *local_err = NULL;
635 /* Parse options */
636 size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0);
638 buf = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
639 prealloc = qapi_enum_parse(&PreallocMode_lookup, buf,
640 PREALLOC_MODE_OFF, &local_err);
641 g_free(buf);
642 if (local_err) {
643 error_propagate(errp, local_err);
644 return -EINVAL;
647 cryptoopts = qemu_opts_to_qdict_filtered(opts, NULL,
648 &block_crypto_create_opts_luks,
649 true);
651 qdict_put_str(cryptoopts, "format", "luks");
652 create_opts = block_crypto_create_opts_init(cryptoopts, errp);
653 if (!create_opts) {
654 ret = -EINVAL;
655 goto fail;
658 /* Create protocol layer */
659 ret = bdrv_create_file(filename, opts, errp);
660 if (ret < 0) {
661 goto fail;
664 bs = bdrv_open(filename, NULL, NULL,
665 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
666 if (!bs) {
667 ret = -EINVAL;
668 goto fail;
671 /* Create format layer */
672 ret = block_crypto_co_create_generic(bs, size, create_opts, prealloc, errp);
673 if (ret < 0) {
674 goto fail;
677 ret = 0;
678 fail:
680 * If an error occurred, delete 'filename'. Even if the file existed
681 * beforehand, it has been truncated and corrupted in the process.
683 if (ret && bs) {
684 Error *local_delete_err = NULL;
685 int r_del = bdrv_co_delete_file(bs, &local_delete_err);
687 * ENOTSUP will happen if the block driver doesn't support
688 * the 'bdrv_co_delete_file' interface. This is a predictable
689 * scenario and shouldn't be reported back to the user.
691 if ((r_del < 0) && (r_del != -ENOTSUP)) {
692 error_report_err(local_delete_err);
696 bdrv_unref(bs);
697 qapi_free_QCryptoBlockCreateOptions(create_opts);
698 qobject_unref(cryptoopts);
699 return ret;
702 static int block_crypto_get_info_luks(BlockDriverState *bs,
703 BlockDriverInfo *bdi)
705 BlockDriverInfo subbdi;
706 int ret;
708 ret = bdrv_get_info(bs->file->bs, &subbdi);
709 if (ret != 0) {
710 return ret;
713 bdi->unallocated_blocks_are_zero = false;
714 bdi->cluster_size = subbdi.cluster_size;
716 return 0;
719 static ImageInfoSpecific *
720 block_crypto_get_specific_info_luks(BlockDriverState *bs, Error **errp)
722 BlockCrypto *crypto = bs->opaque;
723 ImageInfoSpecific *spec_info;
724 QCryptoBlockInfo *info;
726 info = qcrypto_block_get_info(crypto->block, errp);
727 if (!info) {
728 return NULL;
730 assert(info->format == Q_CRYPTO_BLOCK_FORMAT_LUKS);
732 spec_info = g_new(ImageInfoSpecific, 1);
733 spec_info->type = IMAGE_INFO_SPECIFIC_KIND_LUKS;
734 spec_info->u.luks.data = g_new(QCryptoBlockInfoLUKS, 1);
735 *spec_info->u.luks.data = info->u.luks;
737 /* Blank out pointers we've just stolen to avoid double free */
738 memset(&info->u.luks, 0, sizeof(info->u.luks));
740 qapi_free_QCryptoBlockInfo(info);
742 return spec_info;
745 static const char *const block_crypto_strong_runtime_opts[] = {
746 BLOCK_CRYPTO_OPT_LUKS_KEY_SECRET,
748 NULL
751 static BlockDriver bdrv_crypto_luks = {
752 .format_name = "luks",
753 .instance_size = sizeof(BlockCrypto),
754 .bdrv_probe = block_crypto_probe_luks,
755 .bdrv_open = block_crypto_open_luks,
756 .bdrv_close = block_crypto_close,
757 /* This driver doesn't modify LUKS metadata except when creating image.
758 * Allow share-rw=on as a special case. */
759 .bdrv_child_perm = bdrv_default_perms,
760 .bdrv_co_create = block_crypto_co_create_luks,
761 .bdrv_co_create_opts = block_crypto_co_create_opts_luks,
762 .bdrv_co_truncate = block_crypto_co_truncate,
763 .create_opts = &block_crypto_create_opts_luks,
765 .bdrv_reopen_prepare = block_crypto_reopen_prepare,
766 .bdrv_refresh_limits = block_crypto_refresh_limits,
767 .bdrv_co_preadv = block_crypto_co_preadv,
768 .bdrv_co_pwritev = block_crypto_co_pwritev,
769 .bdrv_getlength = block_crypto_getlength,
770 .bdrv_measure = block_crypto_measure,
771 .bdrv_get_info = block_crypto_get_info_luks,
772 .bdrv_get_specific_info = block_crypto_get_specific_info_luks,
774 .is_format = true,
776 .strong_runtime_opts = block_crypto_strong_runtime_opts,
779 static void block_crypto_init(void)
781 bdrv_register(&bdrv_crypto_luks);
784 block_init(block_crypto_init);