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 #ifndef QCRYPTO_IVGEN_H
22 #define QCRYPTO_IVGEN_H
24 #include "crypto/cipher.h"
25 #include "crypto/hash.h"
28 * This module provides a framework for generating initialization
29 * vectors for block encryption schemes using chained cipher modes
30 * CBC. The principle is that each disk sector is assigned a unique
31 * initialization vector for use for encryption of data in that
35 * <title>Encrypting block data with initialization vectors</title>
37 * uint8_t *data = ....data to encrypt...
39 * uint8_t *key = ....some encryption key...
45 * g_assert((ndata % 512) == 0);
47 * QCryptoIVGen *ivgen = qcrypto_ivgen_new(QCRYPTO_IV_GEN_ALGO_ESSIV,
48 * QCRYPTO_CIPHER_ALGO_AES_128,
49 * QCRYPTO_HASH_ALGO_SHA256,
55 * QCryptoCipher *cipher = qcrypto_cipher_new(QCRYPTO_CIPHER_ALGO_AES_128,
56 * QCRYPTO_CIPHER_MODE_CBC,
62 * niv = qcrypto_cipher_get_iv_len(QCRYPTO_CIPHER_ALGO_AES_128,
63 * QCRYPTO_CIPHER_MODE_CBC);
64 * iv = g_new0(uint8_t, niv);
68 * if (qcrypto_ivgen_calculate(ivgen, sector, iv, niv, errp) < 0) {
71 * if (qcrypto_cipher_setiv(cipher, iv, niv, errp) < 0) {
74 * if (qcrypto_cipher_encrypt(cipher,
75 * data + (sector * 512),
76 * data + (sector * 512),
85 * qcrypto_ivgen_free(ivgen);
86 * qcrypto_cipher_free(cipher);
91 * qcrypto_ivgen_free(ivgen);
92 * qcrypto_cipher_free(cipher);
98 typedef struct QCryptoIVGen QCryptoIVGen
;
100 /* See also QCryptoIVGenAlgo enum in qapi/crypto.json */
105 * @alg: the initialization vector generation algorithm
106 * @cipheralg: the cipher algorithm or 0
107 * @hash: the hash algorithm or 0
108 * @key: the encryption key or NULL
109 * @nkey: the size of @key in bytes
111 * Create a new initialization vector generator that uses
112 * the algorithm @alg. Whether the remaining parameters
113 * are required or not depends on the choice of @alg
116 * - QCRYPTO_IV_GEN_ALGO_PLAIN
118 * The IVs are generated by the 32-bit truncated sector
119 * number. This should never be used for block devices
120 * that are larger than 2^32 sectors in size.
121 * All the other parameters are unused.
123 * - QCRYPTO_IV_GEN_ALGO_PLAIN64
125 * The IVs are generated by the 64-bit sector number.
126 * All the other parameters are unused.
128 * - QCRYPTO_IV_GEN_ALGO_ESSIV:
130 * The IVs are generated by encrypting the 64-bit sector
131 * number with a hash of an encryption key. The @cipheralg,
132 * @hash, @key and @nkey parameters are all required.
134 * Returns: a new IV generator, or NULL on error
136 QCryptoIVGen
*qcrypto_ivgen_new(QCryptoIVGenAlgo alg
,
137 QCryptoCipherAlgo cipheralg
,
138 QCryptoHashAlgo hash
,
139 const uint8_t *key
, size_t nkey
,
143 * qcrypto_ivgen_calculate:
144 * @ivgen: the IV generator object
145 * @sector: the 64-bit sector number
146 * @iv: a pre-allocated buffer to hold the generated IV
147 * @niv: the number of bytes in @iv
148 * @errp: pointer to a NULL-initialized error object
150 * Calculate a new initialization vector for the data
151 * to be stored in sector @sector. The IV will be
152 * written into the buffer @iv of size @niv.
154 * Returns: 0 on success, -1 on error
156 int qcrypto_ivgen_calculate(QCryptoIVGen
*ivgen
,
158 uint8_t *iv
, size_t niv
,
163 * qcrypto_ivgen_get_algorithm:
164 * @ivgen: the IV generator object
166 * Get the algorithm used by this IV generator
168 * Returns: the IV generator algorithm
170 QCryptoIVGenAlgo
qcrypto_ivgen_get_algorithm(QCryptoIVGen
*ivgen
);
174 * qcrypto_ivgen_get_cipher:
175 * @ivgen: the IV generator object
177 * Get the cipher algorithm used by this IV generator (if
180 * Returns: the cipher algorithm
182 QCryptoCipherAlgo
qcrypto_ivgen_get_cipher(QCryptoIVGen
*ivgen
);
186 * qcrypto_ivgen_get_hash:
187 * @ivgen: the IV generator object
189 * Get the hash algorithm used by this IV generator (if
192 * Returns: the hash algorithm
194 QCryptoHashAlgo
qcrypto_ivgen_get_hash(QCryptoIVGen
*ivgen
);
198 * qcrypto_ivgen_free:
199 * @ivgen: the IV generator object
201 * Release all resources associated with @ivgen, or a no-op
204 void qcrypto_ivgen_free(QCryptoIVGen
*ivgen
);
206 G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoIVGen
, qcrypto_ivgen_free
)
208 #endif /* QCRYPTO_IVGEN_H */