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 %d", options
->format
);
67 block
->driver
= qcrypto_block_drivers
[options
->format
];
69 if (block
->driver
->open(block
, options
,
70 readfunc
, opaque
, flags
, errp
) < 0) {
79 QCryptoBlock
*qcrypto_block_create(QCryptoBlockCreateOptions
*options
,
80 QCryptoBlockInitFunc initfunc
,
81 QCryptoBlockWriteFunc writefunc
,
85 QCryptoBlock
*block
= g_new0(QCryptoBlock
, 1);
87 block
->format
= options
->format
;
89 if (options
->format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
90 !qcrypto_block_drivers
[options
->format
]) {
91 error_setg(errp
, "Unsupported block driver %d", options
->format
);
96 block
->driver
= qcrypto_block_drivers
[options
->format
];
98 if (block
->driver
->create(block
, options
, initfunc
,
99 writefunc
, opaque
, errp
) < 0) {
108 QCryptoBlockInfo
*qcrypto_block_get_info(QCryptoBlock
*block
,
111 QCryptoBlockInfo
*info
= g_new0(QCryptoBlockInfo
, 1);
113 info
->format
= block
->format
;
115 if (block
->driver
->get_info
&&
116 block
->driver
->get_info(block
, info
, errp
) < 0) {
125 int qcrypto_block_decrypt(QCryptoBlock
*block
,
126 uint64_t startsector
,
131 return block
->driver
->decrypt(block
, startsector
, buf
, len
, errp
);
135 int qcrypto_block_encrypt(QCryptoBlock
*block
,
136 uint64_t startsector
,
141 return block
->driver
->encrypt(block
, startsector
, buf
, len
, errp
);
145 QCryptoCipher
*qcrypto_block_get_cipher(QCryptoBlock
*block
)
147 return block
->cipher
;
151 QCryptoIVGen
*qcrypto_block_get_ivgen(QCryptoBlock
*block
)
157 QCryptoHashAlgorithm
qcrypto_block_get_kdf_hash(QCryptoBlock
*block
)
159 return block
->kdfhash
;
163 uint64_t qcrypto_block_get_payload_offset(QCryptoBlock
*block
)
165 return block
->payload_offset
;
169 void qcrypto_block_free(QCryptoBlock
*block
)
175 block
->driver
->cleanup(block
);
177 qcrypto_cipher_free(block
->cipher
);
178 qcrypto_ivgen_free(block
->ivgen
);
183 int qcrypto_block_decrypt_helper(QCryptoCipher
*cipher
,
187 uint64_t startsector
,
195 iv
= niv
? g_new0(uint8_t, niv
) : NULL
;
200 if (qcrypto_ivgen_calculate(ivgen
,
207 if (qcrypto_cipher_setiv(cipher
,
214 nbytes
= len
> sectorsize
? sectorsize
: len
;
215 if (qcrypto_cipher_decrypt(cipher
, buf
, buf
,
232 int qcrypto_block_encrypt_helper(QCryptoCipher
*cipher
,
236 uint64_t startsector
,
244 iv
= niv
? g_new0(uint8_t, niv
) : NULL
;
249 if (qcrypto_ivgen_calculate(ivgen
,
256 if (qcrypto_cipher_setiv(cipher
,
263 nbytes
= len
> sectorsize
? sectorsize
: len
;
264 if (qcrypto_cipher_encrypt(cipher
, buf
, buf
,