]> git.ipfire.org Git - thirdparty/qemu.git/blob - tests/test-crypto-block.c
crypto: add block encryption framework
[thirdparty/qemu.git] / tests / test-crypto-block.c
1 /*
2 * QEMU Crypto block encryption
3 *
4 * Copyright (c) 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/init.h"
23 #include "crypto/block.h"
24 #include "qemu/buffer.h"
25 #include "crypto/secret.h"
26
27 static QCryptoBlockCreateOptions qcow_create_opts = {
28 .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
29 .u.qcow = {
30 .has_key_secret = true,
31 .key_secret = (char *)"sec0",
32 },
33 };
34
35 static QCryptoBlockOpenOptions qcow_open_opts = {
36 .format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
37 .u.qcow = {
38 .has_key_secret = true,
39 .key_secret = (char *)"sec0",
40 },
41 };
42
43 static struct QCryptoBlockTestData {
44 const char *path;
45 QCryptoBlockCreateOptions *create_opts;
46 QCryptoBlockOpenOptions *open_opts;
47
48 bool expect_header;
49
50 QCryptoCipherAlgorithm cipher_alg;
51 QCryptoCipherMode cipher_mode;
52 QCryptoHashAlgorithm hash_alg;
53
54 QCryptoIVGenAlgorithm ivgen_alg;
55 QCryptoCipherAlgorithm ivgen_hash;
56 } test_data[] = {
57 {
58 .path = "/crypto/block/qcow",
59 .create_opts = &qcow_create_opts,
60 .open_opts = &qcow_open_opts,
61
62 .expect_header = false,
63
64 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_128,
65 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
66
67 .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
68 },
69 };
70
71
72 static ssize_t test_block_read_func(QCryptoBlock *block,
73 size_t offset,
74 uint8_t *buf,
75 size_t buflen,
76 Error **errp,
77 void *opaque)
78 {
79 Buffer *header = opaque;
80
81 g_assert_cmpint(offset + buflen, <=, header->capacity);
82
83 memcpy(buf, header->buffer + offset, buflen);
84
85 return buflen;
86 }
87
88
89 static ssize_t test_block_init_func(QCryptoBlock *block,
90 size_t headerlen,
91 Error **errp,
92 void *opaque)
93 {
94 Buffer *header = opaque;
95
96 g_assert_cmpint(header->capacity, ==, 0);
97
98 buffer_reserve(header, headerlen);
99
100 return headerlen;
101 }
102
103
104 static ssize_t test_block_write_func(QCryptoBlock *block,
105 size_t offset,
106 const uint8_t *buf,
107 size_t buflen,
108 Error **errp,
109 void *opaque)
110 {
111 Buffer *header = opaque;
112
113 g_assert_cmpint(buflen + offset, <=, header->capacity);
114
115 memcpy(header->buffer + offset, buf, buflen);
116 header->offset = offset + buflen;
117
118 return buflen;
119 }
120
121
122 static Object *test_block_secret(void)
123 {
124 return object_new_with_props(
125 TYPE_QCRYPTO_SECRET,
126 object_get_objects_root(),
127 "sec0",
128 &error_abort,
129 "data", "123456",
130 NULL);
131 }
132
133 static void test_block_assert_setup(const struct QCryptoBlockTestData *data,
134 QCryptoBlock *blk)
135 {
136 QCryptoIVGen *ivgen;
137 QCryptoCipher *cipher;
138
139 ivgen = qcrypto_block_get_ivgen(blk);
140 cipher = qcrypto_block_get_cipher(blk);
141
142 g_assert(ivgen);
143 g_assert(cipher);
144
145 g_assert_cmpint(data->cipher_alg, ==, cipher->alg);
146 g_assert_cmpint(data->cipher_mode, ==, cipher->mode);
147 g_assert_cmpint(data->hash_alg, ==,
148 qcrypto_block_get_kdf_hash(blk));
149
150 g_assert_cmpint(data->ivgen_alg, ==,
151 qcrypto_ivgen_get_algorithm(ivgen));
152 g_assert_cmpint(data->ivgen_hash, ==,
153 qcrypto_ivgen_get_hash(ivgen));
154 }
155
156
157 static void test_block(gconstpointer opaque)
158 {
159 const struct QCryptoBlockTestData *data = opaque;
160 QCryptoBlock *blk;
161 Buffer header;
162 Object *sec = test_block_secret();
163
164 memset(&header, 0, sizeof(header));
165 buffer_init(&header, "header");
166
167 blk = qcrypto_block_create(data->create_opts,
168 test_block_init_func,
169 test_block_write_func,
170 &header,
171 &error_abort);
172 g_assert(blk);
173
174 if (data->expect_header) {
175 g_assert_cmpint(header.capacity, >, 0);
176 } else {
177 g_assert_cmpint(header.capacity, ==, 0);
178 }
179
180 test_block_assert_setup(data, blk);
181
182 qcrypto_block_free(blk);
183 object_unparent(sec);
184
185 /* Ensure we can't open without the secret */
186 blk = qcrypto_block_open(data->open_opts,
187 test_block_read_func,
188 &header,
189 0,
190 NULL);
191 g_assert(blk == NULL);
192
193 /* Ensure we can't open without the secret, unless NO_IO */
194 blk = qcrypto_block_open(data->open_opts,
195 test_block_read_func,
196 &header,
197 QCRYPTO_BLOCK_OPEN_NO_IO,
198 &error_abort);
199
200 g_assert(qcrypto_block_get_cipher(blk) == NULL);
201 g_assert(qcrypto_block_get_ivgen(blk) == NULL);
202
203 qcrypto_block_free(blk);
204
205
206 /* Now open for real with secret */
207 sec = test_block_secret();
208 blk = qcrypto_block_open(data->open_opts,
209 test_block_read_func,
210 &header,
211 0,
212 &error_abort);
213 g_assert(blk);
214
215 test_block_assert_setup(data, blk);
216
217 qcrypto_block_free(blk);
218
219 object_unparent(sec);
220
221 buffer_free(&header);
222 }
223
224
225 int main(int argc, char **argv)
226 {
227 gsize i;
228
229 module_call_init(MODULE_INIT_QOM);
230 g_test_init(&argc, &argv, NULL);
231
232 g_assert(qcrypto_init(NULL) == 0);
233
234 for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
235 g_test_add_data_func(test_data[i].path, &test_data[i], test_block);
236 }
237
238 return g_test_run();
239 }