user-exec: Don't reextract sigmask from usercontext pointer
[qemu/cris-port.git] / crypto / block.c
blobda60eba85f2dbe03b88a2f1dbb5dee730cdc5ef2
1 /*
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,
34 const uint8_t *buf,
35 size_t len)
37 const QCryptoBlockDriver *driver;
39 if (format >= G_N_ELEMENTS(qcrypto_block_drivers) ||
40 !qcrypto_block_drivers[format]) {
41 return false;
44 driver = qcrypto_block_drivers[format];
46 return driver->has_format(buf, len);
50 QCryptoBlock *qcrypto_block_open(QCryptoBlockOpenOptions *options,
51 QCryptoBlockReadFunc readfunc,
52 void *opaque,
53 unsigned int flags,
54 Error **errp)
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);
63 g_free(block);
64 return NULL;
67 block->driver = qcrypto_block_drivers[options->format];
69 if (block->driver->open(block, options,
70 readfunc, opaque, flags, errp) < 0) {
71 g_free(block);
72 return NULL;
75 return block;
79 QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options,
80 QCryptoBlockInitFunc initfunc,
81 QCryptoBlockWriteFunc writefunc,
82 void *opaque,
83 Error **errp)
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);
92 g_free(block);
93 return NULL;
96 block->driver = qcrypto_block_drivers[options->format];
98 if (block->driver->create(block, options, initfunc,
99 writefunc, opaque, errp) < 0) {
100 g_free(block);
101 return NULL;
104 return block;
108 int qcrypto_block_decrypt(QCryptoBlock *block,
109 uint64_t startsector,
110 uint8_t *buf,
111 size_t len,
112 Error **errp)
114 return block->driver->decrypt(block, startsector, buf, len, errp);
118 int qcrypto_block_encrypt(QCryptoBlock *block,
119 uint64_t startsector,
120 uint8_t *buf,
121 size_t len,
122 Error **errp)
124 return block->driver->encrypt(block, startsector, buf, len, errp);
128 QCryptoCipher *qcrypto_block_get_cipher(QCryptoBlock *block)
130 return block->cipher;
134 QCryptoIVGen *qcrypto_block_get_ivgen(QCryptoBlock *block)
136 return block->ivgen;
140 QCryptoHashAlgorithm qcrypto_block_get_kdf_hash(QCryptoBlock *block)
142 return block->kdfhash;
146 uint64_t qcrypto_block_get_payload_offset(QCryptoBlock *block)
148 return block->payload_offset;
152 void qcrypto_block_free(QCryptoBlock *block)
154 if (!block) {
155 return;
158 block->driver->cleanup(block);
160 qcrypto_cipher_free(block->cipher);
161 qcrypto_ivgen_free(block->ivgen);
162 g_free(block);
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)
175 uint8_t *iv;
176 int ret = -1;
178 iv = niv ? g_new0(uint8_t, niv) : NULL;
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;
190 if (qcrypto_cipher_setiv(cipher,
191 iv, niv,
192 errp) < 0) {
193 goto cleanup;
197 nbytes = len > sectorsize ? sectorsize : len;
198 if (qcrypto_cipher_decrypt(cipher, buf, buf,
199 nbytes, errp) < 0) {
200 goto cleanup;
203 startsector++;
204 buf += nbytes;
205 len -= nbytes;
208 ret = 0;
209 cleanup:
210 g_free(iv);
211 return ret;
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)
224 uint8_t *iv;
225 int ret = -1;
227 iv = niv ? g_new0(uint8_t, niv) : NULL;
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;
239 if (qcrypto_cipher_setiv(cipher,
240 iv, niv,
241 errp) < 0) {
242 goto cleanup;
246 nbytes = len > sectorsize ? sectorsize : len;
247 if (qcrypto_cipher_encrypt(cipher, buf, buf,
248 nbytes, errp) < 0) {
249 goto cleanup;
252 startsector++;
253 buf += nbytes;
254 len -= nbytes;
257 ret = 0;
258 cleanup:
259 g_free(iv);
260 return ret;