2 * QEMU Crypto block device 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"
22 #include "qapi/error.h"
23 #include "crypto/blockpriv.h"
24 #include "crypto/block-qcow.h"
25 #include "crypto/block-luks.h"
27 static const QCryptoBlockDriver
*qcrypto_block_drivers
[] = {
28 [Q_CRYPTO_BLOCK_FORMAT_QCOW
] = &qcrypto_block_driver_qcow
,
29 [Q_CRYPTO_BLOCK_FORMAT_LUKS
] = &qcrypto_block_driver_luks
,
33 bool qcrypto_block_has_format(QCryptoBlockFormat format
,
37 const QCryptoBlockDriver
*driver
;
39 if (format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
40 !qcrypto_block_drivers
[format
]) {
44 driver
= qcrypto_block_drivers
[format
];
46 return driver
->has_format(buf
, len
);
50 QCryptoBlock
*qcrypto_block_open(QCryptoBlockOpenOptions
*options
,
51 QCryptoBlockReadFunc readfunc
,
56 QCryptoBlock
*block
= g_new0(QCryptoBlock
, 1);
58 block
->format
= options
->format
;
60 if (options
->format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
61 !qcrypto_block_drivers
[options
->format
]) {
62 error_setg(errp
, "Unsupported block driver %s",
63 QCryptoBlockFormat_lookup
[options
->format
]);
68 block
->driver
= qcrypto_block_drivers
[options
->format
];
70 if (block
->driver
->open(block
, options
,
71 readfunc
, opaque
, flags
, errp
) < 0) {
80 QCryptoBlock
*qcrypto_block_create(QCryptoBlockCreateOptions
*options
,
81 QCryptoBlockInitFunc initfunc
,
82 QCryptoBlockWriteFunc writefunc
,
86 QCryptoBlock
*block
= g_new0(QCryptoBlock
, 1);
88 block
->format
= options
->format
;
90 if (options
->format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
91 !qcrypto_block_drivers
[options
->format
]) {
92 error_setg(errp
, "Unsupported block driver %s",
93 QCryptoBlockFormat_lookup
[options
->format
]);
98 block
->driver
= qcrypto_block_drivers
[options
->format
];
100 if (block
->driver
->create(block
, options
, initfunc
,
101 writefunc
, opaque
, errp
) < 0) {
110 QCryptoBlockInfo
*qcrypto_block_get_info(QCryptoBlock
*block
,
113 QCryptoBlockInfo
*info
= g_new0(QCryptoBlockInfo
, 1);
115 info
->format
= block
->format
;
117 if (block
->driver
->get_info
&&
118 block
->driver
->get_info(block
, info
, errp
) < 0) {
127 int qcrypto_block_decrypt(QCryptoBlock
*block
,
128 uint64_t startsector
,
133 return block
->driver
->decrypt(block
, startsector
, buf
, len
, errp
);
137 int qcrypto_block_encrypt(QCryptoBlock
*block
,
138 uint64_t startsector
,
143 return block
->driver
->encrypt(block
, startsector
, buf
, len
, errp
);
147 QCryptoCipher
*qcrypto_block_get_cipher(QCryptoBlock
*block
)
149 return block
->cipher
;
153 QCryptoIVGen
*qcrypto_block_get_ivgen(QCryptoBlock
*block
)
159 QCryptoHashAlgorithm
qcrypto_block_get_kdf_hash(QCryptoBlock
*block
)
161 return block
->kdfhash
;
165 uint64_t qcrypto_block_get_payload_offset(QCryptoBlock
*block
)
167 return block
->payload_offset
;
171 void qcrypto_block_free(QCryptoBlock
*block
)
177 block
->driver
->cleanup(block
);
179 qcrypto_cipher_free(block
->cipher
);
180 qcrypto_ivgen_free(block
->ivgen
);
185 int qcrypto_block_decrypt_helper(QCryptoCipher
*cipher
,
189 uint64_t startsector
,
197 iv
= niv
? g_new0(uint8_t, niv
) : NULL
;
202 if (qcrypto_ivgen_calculate(ivgen
,
209 if (qcrypto_cipher_setiv(cipher
,
216 nbytes
= len
> sectorsize
? sectorsize
: len
;
217 if (qcrypto_cipher_decrypt(cipher
, buf
, buf
,
234 int qcrypto_block_encrypt_helper(QCryptoCipher
*cipher
,
238 uint64_t startsector
,
246 iv
= niv
? g_new0(uint8_t, niv
) : NULL
;
251 if (qcrypto_ivgen_calculate(ivgen
,
258 if (qcrypto_cipher_setiv(cipher
,
265 nbytes
= len
> sectorsize
? sectorsize
: len
;
266 if (qcrypto_cipher_encrypt(cipher
, buf
, buf
,