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
,
30 .has_key_secret
= true,
31 .key_secret
= (char *)"sec0",
35 static QCryptoBlockOpenOptions qcow_open_opts
= {
36 .format
= Q_CRYPTO_BLOCK_FORMAT_QCOW
,
38 .has_key_secret
= true,
39 .key_secret
= (char *)"sec0",
43 static struct QCryptoBlockTestData
{
45 QCryptoBlockCreateOptions
*create_opts
;
46 QCryptoBlockOpenOptions
*open_opts
;
50 QCryptoCipherAlgorithm cipher_alg
;
51 QCryptoCipherMode cipher_mode
;
52 QCryptoHashAlgorithm hash_alg
;
54 QCryptoIVGenAlgorithm ivgen_alg
;
55 QCryptoCipherAlgorithm ivgen_hash
;
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
,
79 Buffer
*header
= opaque
;
81 g_assert_cmpint(offset
+ buflen
, <=, header
->capacity
);
83 memcpy(buf
, header
->buffer
+ offset
, buflen
);
89 static ssize_t
test_block_init_func(QCryptoBlock
*block
,
94 Buffer
*header
= opaque
;
96 g_assert_cmpint(header
->capacity
, ==, 0);
98 buffer_reserve(header
, headerlen
);
104 static ssize_t
test_block_write_func(QCryptoBlock
*block
,
111 Buffer
*header
= opaque
;
113 g_assert_cmpint(buflen
+ offset
, <=, header
->capacity
);
115 memcpy(header
->buffer
+ offset
, buf
, buflen
);
116 header
->offset
= offset
+ buflen
;
122 static Object
*test_block_secret(void)
124 return object_new_with_props(
126 object_get_objects_root(),
133 static void test_block_assert_setup(const struct QCryptoBlockTestData
*data
,
137 QCryptoCipher
*cipher
;
139 ivgen
= qcrypto_block_get_ivgen(blk
);
140 cipher
= qcrypto_block_get_cipher(blk
);
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
;
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
,
174 if (data
->expect_header
) {
175 g_assert_cmpint(header
.capacity
, >, 0);
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
,
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
,
197 QCRYPTO_BLOCK_OPEN_NO_IO
,
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
,
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
)
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
);