vmxnet3: Use common MAC address tracing macros
[qemu.git] / crypto / block.c
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"
22 #include "qapi/error.h"
23 #include "crypto/blockpriv.h"
24 #include "crypto/block-qcow.h"
25 #include "crypto/block-luks.h"
26
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,
30 };
31
32
33 bool 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
50 QCryptoBlock *qcrypto_block_open(QCryptoBlockOpenOptions *options,
51 QCryptoBlockReadFunc readfunc,
52 void *opaque,
53 unsigned int flags,
54 Error **errp)
55 {
56 QCryptoBlock *block = g_new0(QCryptoBlock, 1);
57
58 block->format = options->format;
59
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);
63 g_free(block);
64 return NULL;
65 }
66
67 block->driver = qcrypto_block_drivers[options->format];
68
69 if (block->driver->open(block, options,
70 readfunc, opaque, flags, errp) < 0) {
71 g_free(block);
72 return NULL;
73 }
74
75 return block;
76 }
77
78
79 QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options,
80 QCryptoBlockInitFunc initfunc,
81 QCryptoBlockWriteFunc writefunc,
82 void *opaque,
83 Error **errp)
84 {
85 QCryptoBlock *block = g_new0(QCryptoBlock, 1);
86
87 block->format = options->format;
88
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);
92 g_free(block);
93 return NULL;
94 }
95
96 block->driver = qcrypto_block_drivers[options->format];
97
98 if (block->driver->create(block, options, initfunc,
99 writefunc, opaque, errp) < 0) {
100 g_free(block);
101 return NULL;
102 }
103
104 return block;
105 }
106
107
108 int qcrypto_block_decrypt(QCryptoBlock *block,
109 uint64_t startsector,
110 uint8_t *buf,
111 size_t len,
112 Error **errp)
113 {
114 return block->driver->decrypt(block, startsector, buf, len, errp);
115 }
116
117
118 int qcrypto_block_encrypt(QCryptoBlock *block,
119 uint64_t startsector,
120 uint8_t *buf,
121 size_t len,
122 Error **errp)
123 {
124 return block->driver->encrypt(block, startsector, buf, len, errp);
125 }
126
127
128 QCryptoCipher *qcrypto_block_get_cipher(QCryptoBlock *block)
129 {
130 return block->cipher;
131 }
132
133
134 QCryptoIVGen *qcrypto_block_get_ivgen(QCryptoBlock *block)
135 {
136 return block->ivgen;
137 }
138
139
140 QCryptoHashAlgorithm qcrypto_block_get_kdf_hash(QCryptoBlock *block)
141 {
142 return block->kdfhash;
143 }
144
145
146 uint64_t qcrypto_block_get_payload_offset(QCryptoBlock *block)
147 {
148 return block->payload_offset;
149 }
150
151
152 void qcrypto_block_free(QCryptoBlock *block)
153 {
154 if (!block) {
155 return;
156 }
157
158 block->driver->cleanup(block);
159
160 qcrypto_cipher_free(block->cipher);
161 qcrypto_ivgen_free(block->ivgen);
162 g_free(block);
163 }
164
165
166 int qcrypto_block_decrypt_helper(QCryptoCipher *cipher,
167 size_t niv,
168 QCryptoIVGen *ivgen,
169 int sectorsize,
170 uint64_t startsector,
171 uint8_t *buf,
172 size_t len,
173 Error **errp)
174 {
175 uint8_t *iv;
176 int ret = -1;
177
178 iv = niv ? g_new0(uint8_t, niv) : NULL;
179
180 while (len > 0) {
181 size_t nbytes;
182 if (niv) {
183 if (qcrypto_ivgen_calculate(ivgen,
184 startsector,
185 iv, niv,
186 errp) < 0) {
187 goto cleanup;
188 }
189
190 if (qcrypto_cipher_setiv(cipher,
191 iv, niv,
192 errp) < 0) {
193 goto cleanup;
194 }
195 }
196
197 nbytes = len > sectorsize ? sectorsize : len;
198 if (qcrypto_cipher_decrypt(cipher, buf, buf,
199 nbytes, errp) < 0) {
200 goto cleanup;
201 }
202
203 startsector++;
204 buf += nbytes;
205 len -= nbytes;
206 }
207
208 ret = 0;
209 cleanup:
210 g_free(iv);
211 return ret;
212 }
213
214
215 int qcrypto_block_encrypt_helper(QCryptoCipher *cipher,
216 size_t niv,
217 QCryptoIVGen *ivgen,
218 int sectorsize,
219 uint64_t startsector,
220 uint8_t *buf,
221 size_t len,
222 Error **errp)
223 {
224 uint8_t *iv;
225 int ret = -1;
226
227 iv = niv ? g_new0(uint8_t, niv) : NULL;
228
229 while (len > 0) {
230 size_t nbytes;
231 if (niv) {
232 if (qcrypto_ivgen_calculate(ivgen,
233 startsector,
234 iv, niv,
235 errp) < 0) {
236 goto cleanup;
237 }
238
239 if (qcrypto_cipher_setiv(cipher,
240 iv, niv,
241 errp) < 0) {
242 goto cleanup;
243 }
244 }
245
246 nbytes = len > sectorsize ? sectorsize : len;
247 if (qcrypto_cipher_encrypt(cipher, buf, buf,
248 nbytes, errp) < 0) {
249 goto cleanup;
250 }
251
252 startsector++;
253 buf += nbytes;
254 len -= nbytes;
255 }
256
257 ret = 0;
258 cleanup:
259 g_free(iv);
260 return ret;
261 }