]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-crypto-ivgen.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-usb-20160321-1' into staging
[mirror_qemu.git] / tests / test-crypto-ivgen.c
1 /*
2 * QEMU Crypto IV generator algorithms
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 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 "crypto/ivgen.h"
23
24
25 struct QCryptoIVGenTestData {
26 const char *path;
27 uint64_t sector;
28 QCryptoIVGenAlgorithm ivalg;
29 QCryptoHashAlgorithm hashalg;
30 QCryptoCipherAlgorithm cipheralg;
31 const uint8_t *key;
32 size_t nkey;
33 const uint8_t *iv;
34 size_t niv;
35 } test_data[] = {
36 /* Small */
37 {
38 "/crypto/ivgen/plain/1",
39 .sector = 0x1,
40 .ivalg = QCRYPTO_IVGEN_ALG_PLAIN,
41 .iv = (const uint8_t *)"\x01\x00\x00\x00\x00\x00\x00\x00"
42 "\x00\x00\x00\x00\x00\x00\x00\x00",
43 .niv = 16,
44 },
45 /* Big ! */
46 {
47 "/crypto/ivgen/plain/1f2e3d4c",
48 .sector = 0x1f2e3d4cULL,
49 .ivalg = QCRYPTO_IVGEN_ALG_PLAIN,
50 .iv = (const uint8_t *)"\x4c\x3d\x2e\x1f\x00\x00\x00\x00"
51 "\x00\x00\x00\x00\x00\x00\x00\x00",
52 .niv = 16,
53 },
54 /* Truncation */
55 {
56 "/crypto/ivgen/plain/1f2e3d4c5b6a7988",
57 .sector = 0x1f2e3d4c5b6a7988ULL,
58 .ivalg = QCRYPTO_IVGEN_ALG_PLAIN,
59 .iv = (const uint8_t *)"\x88\x79\x6a\x5b\x00\x00\x00\x00"
60 "\x00\x00\x00\x00\x00\x00\x00\x00",
61 .niv = 16,
62 },
63 /* Small */
64 {
65 "/crypto/ivgen/plain64/1",
66 .sector = 0x1,
67 .ivalg = QCRYPTO_IVGEN_ALG_PLAIN64,
68 .iv = (const uint8_t *)"\x01\x00\x00\x00\x00\x00\x00\x00"
69 "\x00\x00\x00\x00\x00\x00\x00\x00",
70 .niv = 16,
71 },
72 /* Big ! */
73 {
74 "/crypto/ivgen/plain64/1f2e3d4c",
75 .sector = 0x1f2e3d4cULL,
76 .ivalg = QCRYPTO_IVGEN_ALG_PLAIN64,
77 .iv = (const uint8_t *)"\x4c\x3d\x2e\x1f\x00\x00\x00\x00"
78 "\x00\x00\x00\x00\x00\x00\x00\x00",
79 .niv = 16,
80 },
81 /* No Truncation */
82 {
83 "/crypto/ivgen/plain64/1f2e3d4c5b6a7988",
84 .sector = 0x1f2e3d4c5b6a7988ULL,
85 .ivalg = QCRYPTO_IVGEN_ALG_PLAIN64,
86 .iv = (const uint8_t *)"\x88\x79\x6a\x5b\x4c\x3d\x2e\x1f"
87 "\x00\x00\x00\x00\x00\x00\x00\x00",
88 .niv = 16,
89 },
90 /* Small */
91 {
92 "/crypto/ivgen/essiv/1",
93 .sector = 0x1,
94 .ivalg = QCRYPTO_IVGEN_ALG_ESSIV,
95 .cipheralg = QCRYPTO_CIPHER_ALG_AES_128,
96 .hashalg = QCRYPTO_HASH_ALG_SHA256,
97 .key = (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
98 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
99 .nkey = 16,
100 .iv = (const uint8_t *)"\xd4\x83\x71\xb2\xa1\x94\x53\x88"
101 "\x1c\x7a\x2d\06\x2d\x0b\x65\x46",
102 .niv = 16,
103 },
104 /* Big ! */
105 {
106 "/crypto/ivgen/essiv/1f2e3d4c",
107 .sector = 0x1f2e3d4cULL,
108 .ivalg = QCRYPTO_IVGEN_ALG_ESSIV,
109 .cipheralg = QCRYPTO_CIPHER_ALG_AES_128,
110 .hashalg = QCRYPTO_HASH_ALG_SHA256,
111 .key = (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
112 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
113 .nkey = 16,
114 .iv = (const uint8_t *)"\x5d\x36\x09\x5d\xc6\x9e\x5e\xe9"
115 "\xe3\x02\x8d\xd8\x7a\x3d\xe7\x8f",
116 .niv = 16,
117 },
118 /* No Truncation */
119 {
120 "/crypto/ivgen/essiv/1f2e3d4c5b6a7988",
121 .sector = 0x1f2e3d4c5b6a7988ULL,
122 .ivalg = QCRYPTO_IVGEN_ALG_ESSIV,
123 .cipheralg = QCRYPTO_CIPHER_ALG_AES_128,
124 .hashalg = QCRYPTO_HASH_ALG_SHA256,
125 .key = (const uint8_t *)"\x00\x01\x02\x03\x04\x05\x06\x07"
126 "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
127 .nkey = 16,
128 .iv = (const uint8_t *)"\x58\xbb\x81\x94\x51\x83\x23\x23"
129 "\x7a\x08\x93\xa9\xdc\xd2\xd9\xab",
130 .niv = 16,
131 },
132 };
133
134
135 static void test_ivgen(const void *opaque)
136 {
137 const struct QCryptoIVGenTestData *data = opaque;
138 uint8_t *iv = g_new0(uint8_t, data->niv);
139 QCryptoIVGen *ivgen = qcrypto_ivgen_new(
140 data->ivalg,
141 data->cipheralg,
142 data->hashalg,
143 data->key,
144 data->nkey,
145 &error_abort);
146
147 qcrypto_ivgen_calculate(ivgen,
148 data->sector,
149 iv,
150 data->niv,
151 &error_abort);
152
153 g_assert(memcmp(iv, data->iv, data->niv) == 0);
154
155 qcrypto_ivgen_free(ivgen);
156 g_free(iv);
157 }
158
159 int main(int argc, char **argv)
160 {
161 size_t i;
162 g_test_init(&argc, &argv, NULL);
163 for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
164 if (test_data[i].ivalg == QCRYPTO_IVGEN_ALG_ESSIV &&
165 !qcrypto_hash_supports(test_data[i].hashalg)) {
166 continue;
167 }
168 g_test_add_data_func(test_data[i].path,
169 &(test_data[i]),
170 test_ivgen);
171 }
172 return g_test_run();
173 }