nbd/server: Fix race in draining the export
[qemu/ar7.git] / crypto / ivgen.c
blob12822f8519862f492d3798ac3fbe12abee96582e
1 /*
2 * QEMU Crypto block IV generator
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.1 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"
24 #include "ivgenpriv.h"
25 #include "ivgen-plain.h"
26 #include "ivgen-plain64.h"
27 #include "ivgen-essiv.h"
30 QCryptoIVGen *qcrypto_ivgen_new(QCryptoIVGenAlgorithm alg,
31 QCryptoCipherAlgorithm cipheralg,
32 QCryptoHashAlgorithm hash,
33 const uint8_t *key, size_t nkey,
34 Error **errp)
36 QCryptoIVGen *ivgen = g_new0(QCryptoIVGen, 1);
38 ivgen->algorithm = alg;
39 ivgen->cipher = cipheralg;
40 ivgen->hash = hash;
42 switch (alg) {
43 case QCRYPTO_IVGEN_ALG_PLAIN:
44 ivgen->driver = &qcrypto_ivgen_plain;
45 break;
46 case QCRYPTO_IVGEN_ALG_PLAIN64:
47 ivgen->driver = &qcrypto_ivgen_plain64;
48 break;
49 case QCRYPTO_IVGEN_ALG_ESSIV:
50 ivgen->driver = &qcrypto_ivgen_essiv;
51 break;
52 default:
53 error_setg(errp, "Unknown block IV generator algorithm %d", alg);
54 g_free(ivgen);
55 return NULL;
58 if (ivgen->driver->init(ivgen, key, nkey, errp) < 0) {
59 g_free(ivgen);
60 return NULL;
63 return ivgen;
67 int qcrypto_ivgen_calculate(QCryptoIVGen *ivgen,
68 uint64_t sector,
69 uint8_t *iv, size_t niv,
70 Error **errp)
72 return ivgen->driver->calculate(ivgen, sector, iv, niv, errp);
76 QCryptoIVGenAlgorithm qcrypto_ivgen_get_algorithm(QCryptoIVGen *ivgen)
78 return ivgen->algorithm;
82 QCryptoCipherAlgorithm qcrypto_ivgen_get_cipher(QCryptoIVGen *ivgen)
84 return ivgen->cipher;
88 QCryptoHashAlgorithm qcrypto_ivgen_get_hash(QCryptoIVGen *ivgen)
90 return ivgen->hash;
94 void qcrypto_ivgen_free(QCryptoIVGen *ivgen)
96 if (!ivgen) {
97 return;
99 ivgen->driver->cleanup(ivgen);
100 g_free(ivgen);