1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "qapi/error.h"
23
24#include "ivgenpriv.h"
25#include "ivgen-plain.h"
26#include "ivgen-plain64.h"
27#include "ivgen-essiv.h"
28
29
30QCryptoIVGen *qcrypto_ivgen_new(QCryptoIVGenAlgorithm alg,
31 QCryptoCipherAlgorithm cipheralg,
32 QCryptoHashAlgorithm hash,
33 const uint8_t *key, size_t nkey,
34 Error **errp)
35{
36 QCryptoIVGen *ivgen = g_new0(QCryptoIVGen, 1);
37
38 ivgen->algorithm = alg;
39 ivgen->cipher = cipheralg;
40 ivgen->hash = hash;
41
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;
56 }
57
58 if (ivgen->driver->init(ivgen, key, nkey, errp) < 0) {
59 g_free(ivgen);
60 return NULL;
61 }
62
63 return ivgen;
64}
65
66
67int qcrypto_ivgen_calculate(QCryptoIVGen *ivgen,
68 uint64_t sector,
69 uint8_t *iv, size_t niv,
70 Error **errp)
71{
72 return ivgen->driver->calculate(ivgen, sector, iv, niv, errp);
73}
74
75
76QCryptoIVGenAlgorithm qcrypto_ivgen_get_algorithm(QCryptoIVGen *ivgen)
77{
78 return ivgen->algorithm;
79}
80
81
82QCryptoCipherAlgorithm qcrypto_ivgen_get_cipher(QCryptoIVGen *ivgen)
83{
84 return ivgen->cipher;
85}
86
87
88QCryptoHashAlgorithm qcrypto_ivgen_get_hash(QCryptoIVGen *ivgen)
89{
90 return ivgen->hash;
91}
92
93
94void qcrypto_ivgen_free(QCryptoIVGen *ivgen)
95{
96 if (!ivgen) {
97 return;
98 }
99 ivgen->driver->cleanup(ivgen);
100 g_free(ivgen);
101}
102