aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/test-crypto-ivgen.c
blob: 29630ed348a53cf09780c822e57ec876bda4ac72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * QEMU Crypto IV generator algorithms
 *
 * Copyright (c) 2015-2016 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "qemu/osdep.h"
#include "qapi/error.h"
#include "crypto/ivgen.h"


struct QCryptoIVGenTestData {
    const char *path;
    uint64_t sector;
    QCryptoIVGenAlgorithm ivalg;
    QCryptoHashAlgorithm hashalg;
    QCryptoCipherAlgorithm cipheralg;
    const uint8_t *key;
    size_t nkey;
    const uint8_t *iv;
    size_t niv;
} test_data[] = {
    /* Small */
    {
        "/crypto/ivgen/plain/1",
        .sector = 0x1,
        .ivalg = QCRYPTO_IVGEN_ALG_PLAIN,
        .iv = (const uint8_t *)"\x01\x00\x00\x00\x00\x00\x00\x00"
                               "\x00\x00\x00\x00\x00\x00\x00\x00",
        .niv = 16,
    },
    /* Big ! */
    {
        "/crypto/ivgen/plain/1f2e3d4c",
        .sector = 0x1f2e3d4cULL,
        .ivalg = QCRYPTO_IVGEN_ALG_PLAIN,
        .iv = (const uint8_t *)"\x4c\x3d\x2e\x1f\x00\x00\x00\x00"
                               "\x00\x00\x00\x00\x00\x00\x00\x00",
        .niv = 16,
    },
    /* Truncation */
    {
        "/crypto/ivgen/plain/1f2e3d4c5b6a7988",
        .sector = 0x1f2e3d4c5b6a7988ULL,
        .ivalg = QCRYPTO_IVGEN_ALG_PLAIN,
        .iv = (const uint8_t *)"\x88\x79\x6a\x5b\x00\x00\x00\x00"
                               "\x00\x00\x00\x00\x00\x00\x00\x00",
        .niv = 16,
    },
    /* Small */
    {
        "/crypto/ivgen/plain64/1",
        .sector = 0x1,
        .ivalg = QCRYPTO_IVGEN_ALG_PLAIN64,
        .iv = (const uint8_t *)"\x01\x00\x00\x00\x00\x00\x00\x00"
                               "\x00\x00\x00\x00\x00\x00\x00\x00",
        .niv = 16,
    },
    /* Big ! */
    {
        "/crypto/ivgen/plain64/1f2e3d4c",
        .sector = 0x1f2e3d4cULL,
        .ivalg = QCRYPTO_IVGEN_ALG_PLAIN64,
        .iv = (const uint8_t *)"\x4c\x3d\x2e\x1f\x00\x00\x00\x00"
                               "\x00\x00\x00\x00\x00\x00\x00\x00",
        .niv = 16,
    },
    /* No Truncation */
    {
        "/crypto/ivgen/plain64/1f2e3d4c5b6a7988",
        .sector = 0x1f2e3d4c5b6a7988ULL,
        .ivalg = QCRYPTO_IVGEN_ALG_PLAIN64,
        .iv = (const uint8_t *)"\x88\x79\x6a\x5b\x4c\x3d\x2e\x1f"
                               "\x00\x00\x00\x00\x00\x00\x00\x00",
        .niv = 16,
    },
    /* Small */
    {
        "/crypto/ivgen/essiv/1",
        .sector = 0x1,
        .ivalg = QCRYPTO_IVGEN_ALG_ESSIV,
        .cipheralg = QCRYPTO_CIPHER_ALG_AES_128,
        .hashalg = QCRYPTO_HASH_ALG_SHA256,
        .key = (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
                                "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
        .nkey = 16,
        .iv = (const uint8_t *)"\xd4\x83\x71\xb2\xa1\x94\x53\x88"
                               "\x1c\x7a\x2d\06\x2d\x0b\x65\x46",
        .niv = 16,
    },
    /* Big ! */
    {
        "/crypto/ivgen/essiv/1f2e3d4c",
        .sector = 0x1f2e3d4cULL,
        .ivalg = QCRYPTO_IVGEN_ALG_ESSIV,
        .cipheralg = QCRYPTO_CIPHER_ALG_AES_128,
        .hashalg = QCRYPTO_HASH_ALG_SHA256,
        .key = (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
                                "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
        .nkey = 16,
        .iv = (const uint8_t *)"\x5d\x36\x09\x5d\xc6\x9e\x5e\xe9"
                               "\xe3\x02\x8d\xd8\x7a\x3d\xe7\x8f",
        .niv = 16,
    },
    /* No Truncation */
    {
        "/crypto/ivgen/essiv/1f2e3d4c5b6a7988",
        .sector = 0x1f2e3d4c5b6a7988ULL,
        .ivalg = QCRYPTO_IVGEN_ALG_ESSIV,
        .cipheralg = QCRYPTO_CIPHER_ALG_AES_128,
        .hashalg = QCRYPTO_HASH_ALG_SHA256,
        .key = (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
                                "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
        .nkey = 16,
        .iv = (const uint8_t *)"\x58\xbb\x81\x94\x51\x83\x23\x23"
                               "\x7a\x08\x93\xa9\xdc\xd2\xd9\xab",
        .niv = 16,
    },
};


static void test_ivgen(const void *opaque)
{
    const struct QCryptoIVGenTestData *data = opaque;
    g_autofree uint8_t *iv = g_new0(uint8_t, data->niv);
    g_autoptr(QCryptoIVGen) ivgen = NULL;

    if (!qcrypto_cipher_supports(data->cipheralg,
                                 QCRYPTO_CIPHER_MODE_ECB)) {
        return;
    }

    ivgen = qcrypto_ivgen_new(
        data->ivalg,
        data->cipheralg,
        data->hashalg,
        data->key,
        data->nkey,
        &error_abort);

    qcrypto_ivgen_calculate(ivgen,
                            data->sector,
                            iv,
                            data->niv,
                            &error_abort);

    g_assert(memcmp(iv, data->iv, data->niv) == 0);
}

int main(int argc, char **argv)
{
    size_t i;
    g_test_init(&argc, &argv, NULL);
    for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
        if (test_data[i].ivalg == QCRYPTO_IVGEN_ALG_ESSIV &&
            !qcrypto_hash_supports(test_data[i].hashalg)) {
            continue;
        }
        g_test_add_data_func(test_data[i].path,
                             &(test_data[i]),
                             test_ivgen);
    }
    return g_test_run();
}