2 * QEMU Crypto IV generator algorithms
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"
23 #include "crypto/ivgen.h"
26 struct QCryptoIVGenTestData
{
29 QCryptoIVGenAlgorithm ivalg
;
30 QCryptoHashAlgorithm hashalg
;
31 QCryptoCipherAlgorithm cipheralg
;
39 "/crypto/ivgen/plain/1",
41 .ivalg
= QCRYPTO_IVGEN_ALG_PLAIN
,
42 .iv
= (const uint8_t *)"\x01\x00\x00\x00\x00\x00\x00\x00"
43 "\x00\x00\x00\x00\x00\x00\x00\x00",
48 "/crypto/ivgen/plain/1f2e3d4c",
49 .sector
= 0x1f2e3d4cULL
,
50 .ivalg
= QCRYPTO_IVGEN_ALG_PLAIN
,
51 .iv
= (const uint8_t *)"\x4c\x3d\x2e\x1f\x00\x00\x00\x00"
52 "\x00\x00\x00\x00\x00\x00\x00\x00",
57 "/crypto/ivgen/plain/1f2e3d4c5b6a7988",
58 .sector
= 0x1f2e3d4c5b6a7988ULL
,
59 .ivalg
= QCRYPTO_IVGEN_ALG_PLAIN
,
60 .iv
= (const uint8_t *)"\x88\x79\x6a\x5b\x00\x00\x00\x00"
61 "\x00\x00\x00\x00\x00\x00\x00\x00",
66 "/crypto/ivgen/plain64/1",
68 .ivalg
= QCRYPTO_IVGEN_ALG_PLAIN64
,
69 .iv
= (const uint8_t *)"\x01\x00\x00\x00\x00\x00\x00\x00"
70 "\x00\x00\x00\x00\x00\x00\x00\x00",
75 "/crypto/ivgen/plain64/1f2e3d4c",
76 .sector
= 0x1f2e3d4cULL
,
77 .ivalg
= QCRYPTO_IVGEN_ALG_PLAIN64
,
78 .iv
= (const uint8_t *)"\x4c\x3d\x2e\x1f\x00\x00\x00\x00"
79 "\x00\x00\x00\x00\x00\x00\x00\x00",
84 "/crypto/ivgen/plain64/1f2e3d4c5b6a7988",
85 .sector
= 0x1f2e3d4c5b6a7988ULL
,
86 .ivalg
= QCRYPTO_IVGEN_ALG_PLAIN64
,
87 .iv
= (const uint8_t *)"\x88\x79\x6a\x5b\x4c\x3d\x2e\x1f"
88 "\x00\x00\x00\x00\x00\x00\x00\x00",
93 "/crypto/ivgen/essiv/1",
95 .ivalg
= QCRYPTO_IVGEN_ALG_ESSIV
,
96 .cipheralg
= QCRYPTO_CIPHER_ALG_AES_128
,
97 .hashalg
= QCRYPTO_HASH_ALG_SHA256
,
98 .key
= (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
99 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
101 .iv
= (const uint8_t *)"\xd4\x83\x71\xb2\xa1\x94\x53\x88"
102 "\x1c\x7a\x2d\06\x2d\x0b\x65\x46",
107 "/crypto/ivgen/essiv/1f2e3d4c",
108 .sector
= 0x1f2e3d4cULL
,
109 .ivalg
= QCRYPTO_IVGEN_ALG_ESSIV
,
110 .cipheralg
= QCRYPTO_CIPHER_ALG_AES_128
,
111 .hashalg
= QCRYPTO_HASH_ALG_SHA256
,
112 .key
= (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
113 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
115 .iv
= (const uint8_t *)"\x5d\x36\x09\x5d\xc6\x9e\x5e\xe9"
116 "\xe3\x02\x8d\xd8\x7a\x3d\xe7\x8f",
121 "/crypto/ivgen/essiv/1f2e3d4c5b6a7988",
122 .sector
= 0x1f2e3d4c5b6a7988ULL
,
123 .ivalg
= QCRYPTO_IVGEN_ALG_ESSIV
,
124 .cipheralg
= QCRYPTO_CIPHER_ALG_AES_128
,
125 .hashalg
= QCRYPTO_HASH_ALG_SHA256
,
126 .key
= (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
127 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
129 .iv
= (const uint8_t *)"\x58\xbb\x81\x94\x51\x83\x23\x23"
130 "\x7a\x08\x93\xa9\xdc\xd2\xd9\xab",
136 static void test_ivgen(const void *opaque
)
138 const struct QCryptoIVGenTestData
*data
= opaque
;
139 g_autofree
uint8_t *iv
= g_new0(uint8_t, data
->niv
);
140 g_autoptr(QCryptoIVGen
) ivgen
= NULL
;
142 if (!qcrypto_cipher_supports(data
->cipheralg
,
143 QCRYPTO_CIPHER_MODE_ECB
)) {
147 ivgen
= qcrypto_ivgen_new(
155 qcrypto_ivgen_calculate(ivgen
,
161 g_assert(memcmp(iv
, data
->iv
, data
->niv
) == 0);
164 int main(int argc
, char **argv
)
167 g_test_init(&argc
, &argv
, NULL
);
168 for (i
= 0; i
< G_N_ELEMENTS(test_data
); i
++) {
169 if (test_data
[i
].ivalg
== QCRYPTO_IVGEN_ALG_ESSIV
&&
170 !qcrypto_hash_supports(test_data
[i
].hashalg
)) {
173 g_test_add_data_func(test_data
[i
].path
,