Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/crypto/ivgen.c
Line
Count
Source
1
/*
2
 * QEMU Crypto block IV generator
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.1 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
24
#include "ivgenpriv.h"
25
#include "ivgen-plain.h"
26
#include "ivgen-plain64.h"
27
#include "ivgen-essiv.h"
28
29
30
QCryptoIVGen *qcrypto_ivgen_new(QCryptoIVGenAlgo alg,
31
                                QCryptoCipherAlgo cipheralg,
32
                                QCryptoHashAlgo hash,
33
                                const uint8_t *key, size_t nkey,
34
                                Error **errp)
35
0
{
36
0
    g_autofree QCryptoIVGen *ivgen = g_new0(QCryptoIVGen, 1);
37
38
0
    ivgen->algorithm = alg;
39
0
    ivgen->cipher = cipheralg;
40
0
    ivgen->hash = hash;
41
42
0
    switch (alg) {
43
0
    case QCRYPTO_IV_GEN_ALGO_PLAIN:
44
0
        ivgen->driver = &qcrypto_ivgen_plain;
45
0
        break;
46
0
    case QCRYPTO_IV_GEN_ALGO_PLAIN64:
47
0
        ivgen->driver = &qcrypto_ivgen_plain64;
48
0
        break;
49
0
    case QCRYPTO_IV_GEN_ALGO_ESSIV:
50
0
        ivgen->driver = &qcrypto_ivgen_essiv;
51
0
        break;
52
0
    default:
53
0
        error_setg(errp, "Unknown block IV generator algorithm %d", alg);
54
0
        return NULL;
55
0
    }
56
57
0
    if (ivgen->driver->init(ivgen, key, nkey, errp) < 0) {
58
0
        return NULL;
59
0
    }
60
61
0
    return g_steal_pointer(&ivgen);
62
0
}
63
64
65
int qcrypto_ivgen_calculate(QCryptoIVGen *ivgen,
66
                            uint64_t sector,
67
                            uint8_t *iv, size_t niv,
68
                            Error **errp)
69
0
{
70
0
    return ivgen->driver->calculate(ivgen, sector, iv, niv, errp);
71
0
}
72
73
74
QCryptoIVGenAlgo qcrypto_ivgen_get_algorithm(QCryptoIVGen *ivgen)
75
0
{
76
0
    return ivgen->algorithm;
77
0
}
78
79
80
QCryptoCipherAlgo qcrypto_ivgen_get_cipher(QCryptoIVGen *ivgen)
81
0
{
82
0
    return ivgen->cipher;
83
0
}
84
85
86
QCryptoHashAlgo qcrypto_ivgen_get_hash(QCryptoIVGen *ivgen)
87
0
{
88
0
    return ivgen->hash;
89
0
}
90
91
92
void qcrypto_ivgen_free(QCryptoIVGen *ivgen)
93
0
{
94
0
    if (!ivgen) {
95
0
        return;
96
0
    }
97
0
    ivgen->driver->cleanup(ivgen);
98
0
    g_free(ivgen);
99
0
}