]> Git Repo - qemu.git/blame - crypto/block.c
block: convert crypto driver to bdrv_co_preadv|pwritev
[qemu.git] / crypto / block.c
CommitLineData
7d969014
DB
1/*
2 * QEMU Crypto block device encryption
3 *
4 * Copyright (c) 2015-2016 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21#include "qemu/osdep.h"
da34e65c 22#include "qapi/error.h"
7d969014
DB
23#include "crypto/blockpriv.h"
24#include "crypto/block-qcow.h"
3e308f20 25#include "crypto/block-luks.h"
7d969014
DB
26
27static const QCryptoBlockDriver *qcrypto_block_drivers[] = {
28 [Q_CRYPTO_BLOCK_FORMAT_QCOW] = &qcrypto_block_driver_qcow,
3e308f20 29 [Q_CRYPTO_BLOCK_FORMAT_LUKS] = &qcrypto_block_driver_luks,
7d969014
DB
30};
31
32
33bool qcrypto_block_has_format(QCryptoBlockFormat format,
34 const uint8_t *buf,
35 size_t len)
36{
37 const QCryptoBlockDriver *driver;
38
39 if (format >= G_N_ELEMENTS(qcrypto_block_drivers) ||
40 !qcrypto_block_drivers[format]) {
41 return false;
42 }
43
44 driver = qcrypto_block_drivers[format];
45
46 return driver->has_format(buf, len);
47}
48
49
50QCryptoBlock *qcrypto_block_open(QCryptoBlockOpenOptions *options,
1cd9a787 51 const char *optprefix,
7d969014
DB
52 QCryptoBlockReadFunc readfunc,
53 void *opaque,
54 unsigned int flags,
55 Error **errp)
56{
57 QCryptoBlock *block = g_new0(QCryptoBlock, 1);
58
59 block->format = options->format;
60
61 if (options->format >= G_N_ELEMENTS(qcrypto_block_drivers) ||
62 !qcrypto_block_drivers[options->format]) {
90d6f60d 63 error_setg(errp, "Unsupported block driver %s",
977c736f 64 QCryptoBlockFormat_str(options->format));
7d969014
DB
65 g_free(block);
66 return NULL;
67 }
68
69 block->driver = qcrypto_block_drivers[options->format];
70
1cd9a787 71 if (block->driver->open(block, options, optprefix,
7d969014
DB
72 readfunc, opaque, flags, errp) < 0) {
73 g_free(block);
74 return NULL;
75 }
76
77 return block;
78}
79
80
81QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options,
1cd9a787 82 const char *optprefix,
7d969014
DB
83 QCryptoBlockInitFunc initfunc,
84 QCryptoBlockWriteFunc writefunc,
85 void *opaque,
86 Error **errp)
87{
88 QCryptoBlock *block = g_new0(QCryptoBlock, 1);
89
90 block->format = options->format;
91
92 if (options->format >= G_N_ELEMENTS(qcrypto_block_drivers) ||
93 !qcrypto_block_drivers[options->format]) {
90d6f60d 94 error_setg(errp, "Unsupported block driver %s",
977c736f 95 QCryptoBlockFormat_str(options->format));
7d969014
DB
96 g_free(block);
97 return NULL;
98 }
99
100 block->driver = qcrypto_block_drivers[options->format];
101
1cd9a787 102 if (block->driver->create(block, options, optprefix, initfunc,
7d969014
DB
103 writefunc, opaque, errp) < 0) {
104 g_free(block);
105 return NULL;
106 }
107
108 return block;
109}
110
111
40c85028
DB
112QCryptoBlockInfo *qcrypto_block_get_info(QCryptoBlock *block,
113 Error **errp)
114{
115 QCryptoBlockInfo *info = g_new0(QCryptoBlockInfo, 1);
116
117 info->format = block->format;
118
119 if (block->driver->get_info &&
120 block->driver->get_info(block, info, errp) < 0) {
121 g_free(info);
122 return NULL;
123 }
124
125 return info;
126}
127
128
7d969014
DB
129int qcrypto_block_decrypt(QCryptoBlock *block,
130 uint64_t startsector,
131 uint8_t *buf,
132 size_t len,
133 Error **errp)
134{
135 return block->driver->decrypt(block, startsector, buf, len, errp);
136}
137
138
139int qcrypto_block_encrypt(QCryptoBlock *block,
140 uint64_t startsector,
141 uint8_t *buf,
142 size_t len,
143 Error **errp)
144{
145 return block->driver->encrypt(block, startsector, buf, len, errp);
146}
147
148
149QCryptoCipher *qcrypto_block_get_cipher(QCryptoBlock *block)
150{
151 return block->cipher;
152}
153
154
155QCryptoIVGen *qcrypto_block_get_ivgen(QCryptoBlock *block)
156{
157 return block->ivgen;
158}
159
160
161QCryptoHashAlgorithm qcrypto_block_get_kdf_hash(QCryptoBlock *block)
162{
163 return block->kdfhash;
164}
165
166
167uint64_t qcrypto_block_get_payload_offset(QCryptoBlock *block)
168{
169 return block->payload_offset;
170}
171
172
850f49de
DB
173uint64_t qcrypto_block_get_sector_size(QCryptoBlock *block)
174{
175 return block->sector_size;
176}
177
178
7d969014
DB
179void qcrypto_block_free(QCryptoBlock *block)
180{
181 if (!block) {
182 return;
183 }
184
185 block->driver->cleanup(block);
186
187 qcrypto_cipher_free(block->cipher);
188 qcrypto_ivgen_free(block->ivgen);
189 g_free(block);
190}
191
192
193int qcrypto_block_decrypt_helper(QCryptoCipher *cipher,
194 size_t niv,
195 QCryptoIVGen *ivgen,
196 int sectorsize,
197 uint64_t startsector,
198 uint8_t *buf,
199 size_t len,
200 Error **errp)
201{
202 uint8_t *iv;
203 int ret = -1;
204
205 iv = niv ? g_new0(uint8_t, niv) : NULL;
206
207 while (len > 0) {
208 size_t nbytes;
209 if (niv) {
210 if (qcrypto_ivgen_calculate(ivgen,
211 startsector,
212 iv, niv,
213 errp) < 0) {
214 goto cleanup;
215 }
216
217 if (qcrypto_cipher_setiv(cipher,
218 iv, niv,
219 errp) < 0) {
220 goto cleanup;
221 }
222 }
223
224 nbytes = len > sectorsize ? sectorsize : len;
225 if (qcrypto_cipher_decrypt(cipher, buf, buf,
226 nbytes, errp) < 0) {
227 goto cleanup;
228 }
229
230 startsector++;
231 buf += nbytes;
232 len -= nbytes;
233 }
234
235 ret = 0;
236 cleanup:
237 g_free(iv);
238 return ret;
239}
240
241
242int qcrypto_block_encrypt_helper(QCryptoCipher *cipher,
243 size_t niv,
244 QCryptoIVGen *ivgen,
245 int sectorsize,
246 uint64_t startsector,
247 uint8_t *buf,
248 size_t len,
249 Error **errp)
250{
251 uint8_t *iv;
252 int ret = -1;
253
254 iv = niv ? g_new0(uint8_t, niv) : NULL;
255
256 while (len > 0) {
257 size_t nbytes;
258 if (niv) {
259 if (qcrypto_ivgen_calculate(ivgen,
260 startsector,
261 iv, niv,
262 errp) < 0) {
263 goto cleanup;
264 }
265
266 if (qcrypto_cipher_setiv(cipher,
267 iv, niv,
268 errp) < 0) {
269 goto cleanup;
270 }
271 }
272
273 nbytes = len > sectorsize ? sectorsize : len;
274 if (qcrypto_cipher_encrypt(cipher, buf, buf,
275 nbytes, errp) < 0) {
276 goto cleanup;
277 }
278
279 startsector++;
280 buf += nbytes;
281 len -= nbytes;
282 }
283
284 ret = 0;
285 cleanup:
286 g_free(iv);
287 return ret;
288}
This page took 0.157343 seconds and 4 git commands to generate.