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 "crypto/blockpriv.h"
23 #include "crypto/block-qcow.h"
24 #include "crypto/block-luks.h"
26 static const QCryptoBlockDriver
*qcrypto_block_drivers
[] = {
27 [Q_CRYPTO_BLOCK_FORMAT_QCOW
] = &qcrypto_block_driver_qcow
,
28 [Q_CRYPTO_BLOCK_FORMAT_LUKS
] = &qcrypto_block_driver_luks
,
32 bool qcrypto_block_has_format(QCryptoBlockFormat format
,
36 const QCryptoBlockDriver
*driver
;
38 if (format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
39 !qcrypto_block_drivers
[format
]) {
43 driver
= qcrypto_block_drivers
[format
];
45 return driver
->has_format(buf
, len
);
49 QCryptoBlock
*qcrypto_block_open(QCryptoBlockOpenOptions
*options
,
50 QCryptoBlockReadFunc readfunc
,
55 QCryptoBlock
*block
= g_new0(QCryptoBlock
, 1);
57 block
->format
= options
->format
;
59 if (options
->format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
60 !qcrypto_block_drivers
[options
->format
]) {
61 error_setg(errp
, "Unsupported block driver %d", options
->format
);
66 block
->driver
= qcrypto_block_drivers
[options
->format
];
68 if (block
->driver
->open(block
, options
,
69 readfunc
, opaque
, flags
, errp
) < 0) {
78 QCryptoBlock
*qcrypto_block_create(QCryptoBlockCreateOptions
*options
,
79 QCryptoBlockInitFunc initfunc
,
80 QCryptoBlockWriteFunc writefunc
,
84 QCryptoBlock
*block
= g_new0(QCryptoBlock
, 1);
86 block
->format
= options
->format
;
88 if (options
->format
>= G_N_ELEMENTS(qcrypto_block_drivers
) ||
89 !qcrypto_block_drivers
[options
->format
]) {
90 error_setg(errp
, "Unsupported block driver %d", options
->format
);
95 block
->driver
= qcrypto_block_drivers
[options
->format
];
97 if (block
->driver
->create(block
, options
, initfunc
,
98 writefunc
, opaque
, errp
) < 0) {
107 int qcrypto_block_decrypt(QCryptoBlock
*block
,
108 uint64_t startsector
,
113 return block
->driver
->decrypt(block
, startsector
, buf
, len
, errp
);
117 int qcrypto_block_encrypt(QCryptoBlock
*block
,
118 uint64_t startsector
,
123 return block
->driver
->encrypt(block
, startsector
, buf
, len
, errp
);
127 QCryptoCipher
*qcrypto_block_get_cipher(QCryptoBlock
*block
)
129 return block
->cipher
;
133 QCryptoIVGen
*qcrypto_block_get_ivgen(QCryptoBlock
*block
)
139 QCryptoHashAlgorithm
qcrypto_block_get_kdf_hash(QCryptoBlock
*block
)
141 return block
->kdfhash
;
145 uint64_t qcrypto_block_get_payload_offset(QCryptoBlock
*block
)
147 return block
->payload_offset
;
151 void qcrypto_block_free(QCryptoBlock
*block
)
157 block
->driver
->cleanup(block
);
159 qcrypto_cipher_free(block
->cipher
);
160 qcrypto_ivgen_free(block
->ivgen
);
165 int qcrypto_block_decrypt_helper(QCryptoCipher
*cipher
,
169 uint64_t startsector
,
177 iv
= niv
? g_new0(uint8_t, niv
) : NULL
;
182 if (qcrypto_ivgen_calculate(ivgen
,
189 if (qcrypto_cipher_setiv(cipher
,
196 nbytes
= len
> sectorsize
? sectorsize
: len
;
197 if (qcrypto_cipher_decrypt(cipher
, buf
, buf
,
214 int qcrypto_block_encrypt_helper(QCryptoCipher
*cipher
,
218 uint64_t startsector
,
226 iv
= niv
? g_new0(uint8_t, niv
) : NULL
;
231 if (qcrypto_ivgen_calculate(ivgen
,
238 if (qcrypto_cipher_setiv(cipher
,
245 nbytes
= len
> sectorsize
? sectorsize
: len
;
246 if (qcrypto_cipher_encrypt(cipher
, buf
, buf
,