crypto: add block encryption framework
[qemu/kevin.git] / tests / test-crypto-block.c
blob3f65383a1bb0ac2b4fc734ad34b85cdefdb06e56
1 /*
2 * QEMU Crypto block encryption
4 * Copyright (c) 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 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 "crypto/init.h"
23 #include "crypto/block.h"
24 #include "qemu/buffer.h"
25 #include "crypto/secret.h"
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",
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",
43 static struct QCryptoBlockTestData {
44 const char *path;
45 QCryptoBlockCreateOptions *create_opts;
46 QCryptoBlockOpenOptions *open_opts;
48 bool expect_header;
50 QCryptoCipherAlgorithm cipher_alg;
51 QCryptoCipherMode cipher_mode;
52 QCryptoHashAlgorithm hash_alg;
54 QCryptoIVGenAlgorithm ivgen_alg;
55 QCryptoCipherAlgorithm ivgen_hash;
56 } test_data[] = {
58 .path = "/crypto/block/qcow",
59 .create_opts = &qcow_create_opts,
60 .open_opts = &qcow_open_opts,
62 .expect_header = false,
64 .cipher_alg = QCRYPTO_CIPHER_ALG_AES_128,
65 .cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
67 .ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
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)
79 Buffer *header = opaque;
81 g_assert_cmpint(offset + buflen, <=, header->capacity);
83 memcpy(buf, header->buffer + offset, buflen);
85 return buflen;
89 static ssize_t test_block_init_func(QCryptoBlock *block,
90 size_t headerlen,
91 Error **errp,
92 void *opaque)
94 Buffer *header = opaque;
96 g_assert_cmpint(header->capacity, ==, 0);
98 buffer_reserve(header, headerlen);
100 return headerlen;
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)
111 Buffer *header = opaque;
113 g_assert_cmpint(buflen + offset, <=, header->capacity);
115 memcpy(header->buffer + offset, buf, buflen);
116 header->offset = offset + buflen;
118 return buflen;
122 static Object *test_block_secret(void)
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);
133 static void test_block_assert_setup(const struct QCryptoBlockTestData *data,
134 QCryptoBlock *blk)
136 QCryptoIVGen *ivgen;
137 QCryptoCipher *cipher;
139 ivgen = qcrypto_block_get_ivgen(blk);
140 cipher = qcrypto_block_get_cipher(blk);
142 g_assert(ivgen);
143 g_assert(cipher);
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));
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));
157 static void test_block(gconstpointer opaque)
159 const struct QCryptoBlockTestData *data = opaque;
160 QCryptoBlock *blk;
161 Buffer header;
162 Object *sec = test_block_secret();
164 memset(&header, 0, sizeof(header));
165 buffer_init(&header, "header");
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);
174 if (data->expect_header) {
175 g_assert_cmpint(header.capacity, >, 0);
176 } else {
177 g_assert_cmpint(header.capacity, ==, 0);
180 test_block_assert_setup(data, blk);
182 qcrypto_block_free(blk);
183 object_unparent(sec);
185 /* Ensure we can't open without the secret */
186 blk = qcrypto_block_open(data->open_opts,
187 test_block_read_func,
188 &header,
190 NULL);
191 g_assert(blk == NULL);
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);
200 g_assert(qcrypto_block_get_cipher(blk) == NULL);
201 g_assert(qcrypto_block_get_ivgen(blk) == NULL);
203 qcrypto_block_free(blk);
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,
212 &error_abort);
213 g_assert(blk);
215 test_block_assert_setup(data, blk);
217 qcrypto_block_free(blk);
219 object_unparent(sec);
221 buffer_free(&header);
225 int main(int argc, char **argv)
227 gsize i;
229 module_call_init(MODULE_INIT_QOM);
230 g_test_init(&argc, &argv, NULL);
232 g_assert(qcrypto_init(NULL) == 0);
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);
238 return g_test_run();