2015-10-24 12:44:13 +02:00
|
|
|
/*
|
|
|
|
* QEMU Crypto block encryption
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-14 15:42:48 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-10-24 12:44:13 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2015-10-24 12:44:13 +02:00
|
|
|
#include "crypto/init.h"
|
|
|
|
#include "crypto/block.h"
|
2022-09-05 15:57:17 +02:00
|
|
|
#include "crypto/block-luks-priv.h"
|
2015-10-24 12:44:13 +02:00
|
|
|
#include "qemu/buffer.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2015-10-24 12:44:13 +02:00
|
|
|
#include "crypto/secret.h"
|
2015-10-24 12:55:48 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#endif
|
|
|
|
|
2017-11-03 12:54:52 +01:00
|
|
|
#if (defined(_WIN32) || defined RUSAGE_THREAD) && \
|
2022-09-05 15:37:05 +02:00
|
|
|
(defined(CONFIG_NETTLE) || defined(CONFIG_GCRYPT) || \
|
|
|
|
defined(CONFIG_GNUTLS_CRYPTO))
|
2015-10-24 12:55:48 +02:00
|
|
|
#define TEST_LUKS
|
|
|
|
#else
|
|
|
|
#undef TEST_LUKS
|
|
|
|
#endif
|
2015-10-24 12:44:13 +02:00
|
|
|
|
|
|
|
static QCryptoBlockCreateOptions qcow_create_opts = {
|
|
|
|
.format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
|
|
|
|
.u.qcow = {
|
|
|
|
.key_secret = (char *)"sec0",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static QCryptoBlockOpenOptions qcow_open_opts = {
|
|
|
|
.format = Q_CRYPTO_BLOCK_FORMAT_QCOW,
|
|
|
|
.u.qcow = {
|
|
|
|
.key_secret = (char *)"sec0",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-10-24 12:55:48 +02:00
|
|
|
|
|
|
|
#ifdef TEST_LUKS
|
|
|
|
static QCryptoBlockOpenOptions luks_open_opts = {
|
|
|
|
.format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
|
|
|
|
.u.luks = {
|
|
|
|
.key_secret = (char *)"sec0",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Creation with all default values */
|
|
|
|
static QCryptoBlockCreateOptions luks_create_opts_default = {
|
|
|
|
.format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
|
|
|
|
.u.luks = {
|
|
|
|
.key_secret = (char *)"sec0",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* ...and with explicit values */
|
|
|
|
static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_plain64 = {
|
|
|
|
.format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
|
|
|
|
.u.luks = {
|
|
|
|
.key_secret = (char *)"sec0",
|
|
|
|
.has_cipher_alg = true,
|
|
|
|
.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
|
|
|
|
.has_cipher_mode = true,
|
|
|
|
.cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
|
|
|
|
.has_ivgen_alg = true,
|
|
|
|
.ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static QCryptoBlockCreateOptions luks_create_opts_aes256_cbc_essiv = {
|
|
|
|
.format = Q_CRYPTO_BLOCK_FORMAT_LUKS,
|
|
|
|
.u.luks = {
|
|
|
|
.key_secret = (char *)"sec0",
|
|
|
|
.has_cipher_alg = true,
|
|
|
|
.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
|
|
|
|
.has_cipher_mode = true,
|
|
|
|
.cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
|
|
|
|
.has_ivgen_alg = true,
|
|
|
|
.ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
|
|
|
|
.has_ivgen_hash_alg = true,
|
|
|
|
.ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256,
|
|
|
|
.has_hash_alg = true,
|
|
|
|
.hash_alg = QCRYPTO_HASH_ALG_SHA1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
#endif /* TEST_LUKS */
|
|
|
|
|
|
|
|
|
2015-10-24 12:44:13 +02:00
|
|
|
static struct QCryptoBlockTestData {
|
|
|
|
const char *path;
|
|
|
|
QCryptoBlockCreateOptions *create_opts;
|
|
|
|
QCryptoBlockOpenOptions *open_opts;
|
|
|
|
|
|
|
|
bool expect_header;
|
|
|
|
|
|
|
|
QCryptoCipherAlgorithm cipher_alg;
|
|
|
|
QCryptoCipherMode cipher_mode;
|
|
|
|
QCryptoHashAlgorithm hash_alg;
|
|
|
|
|
|
|
|
QCryptoIVGenAlgorithm ivgen_alg;
|
2015-10-24 12:55:48 +02:00
|
|
|
QCryptoHashAlgorithm ivgen_hash;
|
|
|
|
|
|
|
|
bool slow;
|
2015-10-24 12:44:13 +02:00
|
|
|
} test_data[] = {
|
|
|
|
{
|
|
|
|
.path = "/crypto/block/qcow",
|
|
|
|
.create_opts = &qcow_create_opts,
|
|
|
|
.open_opts = &qcow_open_opts,
|
|
|
|
|
|
|
|
.expect_header = false,
|
|
|
|
|
|
|
|
.cipher_alg = QCRYPTO_CIPHER_ALG_AES_128,
|
|
|
|
.cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
|
|
|
|
|
|
|
|
.ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
|
|
|
|
},
|
2015-10-24 12:55:48 +02:00
|
|
|
#ifdef TEST_LUKS
|
|
|
|
{
|
|
|
|
.path = "/crypto/block/luks/default",
|
|
|
|
.create_opts = &luks_create_opts_default,
|
|
|
|
.open_opts = &luks_open_opts,
|
|
|
|
|
|
|
|
.expect_header = true,
|
|
|
|
|
|
|
|
.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
|
|
|
|
.cipher_mode = QCRYPTO_CIPHER_MODE_XTS,
|
|
|
|
.hash_alg = QCRYPTO_HASH_ALG_SHA256,
|
|
|
|
|
|
|
|
.ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
|
|
|
|
|
|
|
|
.slow = true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.path = "/crypto/block/luks/aes-256-cbc-plain64",
|
|
|
|
.create_opts = &luks_create_opts_aes256_cbc_plain64,
|
|
|
|
.open_opts = &luks_open_opts,
|
|
|
|
|
|
|
|
.expect_header = true,
|
|
|
|
|
|
|
|
.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
|
|
|
|
.cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
|
|
|
|
.hash_alg = QCRYPTO_HASH_ALG_SHA256,
|
|
|
|
|
|
|
|
.ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64,
|
|
|
|
|
|
|
|
.slow = true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.path = "/crypto/block/luks/aes-256-cbc-essiv",
|
|
|
|
.create_opts = &luks_create_opts_aes256_cbc_essiv,
|
|
|
|
.open_opts = &luks_open_opts,
|
|
|
|
|
|
|
|
.expect_header = true,
|
|
|
|
|
|
|
|
.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256,
|
|
|
|
.cipher_mode = QCRYPTO_CIPHER_MODE_CBC,
|
|
|
|
.hash_alg = QCRYPTO_HASH_ALG_SHA1,
|
|
|
|
|
|
|
|
.ivgen_alg = QCRYPTO_IVGEN_ALG_ESSIV,
|
|
|
|
.ivgen_hash = QCRYPTO_HASH_ALG_SHA256,
|
|
|
|
|
|
|
|
.slow = true,
|
|
|
|
},
|
|
|
|
#endif
|
2015-10-24 12:44:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-06-09 17:27:38 +02:00
|
|
|
static int test_block_read_func(QCryptoBlock *block,
|
|
|
|
size_t offset,
|
|
|
|
uint8_t *buf,
|
|
|
|
size_t buflen,
|
|
|
|
void *opaque,
|
|
|
|
Error **errp)
|
2015-10-24 12:44:13 +02:00
|
|
|
{
|
|
|
|
Buffer *header = opaque;
|
|
|
|
|
|
|
|
g_assert_cmpint(offset + buflen, <=, header->capacity);
|
|
|
|
|
|
|
|
memcpy(buf, header->buffer + offset, buflen);
|
|
|
|
|
2022-06-09 17:27:38 +02:00
|
|
|
return 0;
|
2015-10-24 12:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-09 17:27:38 +02:00
|
|
|
static int test_block_init_func(QCryptoBlock *block,
|
|
|
|
size_t headerlen,
|
|
|
|
void *opaque,
|
|
|
|
Error **errp)
|
2015-10-24 12:44:13 +02:00
|
|
|
{
|
|
|
|
Buffer *header = opaque;
|
|
|
|
|
|
|
|
g_assert_cmpint(header->capacity, ==, 0);
|
|
|
|
|
|
|
|
buffer_reserve(header, headerlen);
|
|
|
|
|
2022-06-09 17:27:38 +02:00
|
|
|
return 0;
|
2015-10-24 12:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-09 17:27:38 +02:00
|
|
|
static int test_block_write_func(QCryptoBlock *block,
|
|
|
|
size_t offset,
|
|
|
|
const uint8_t *buf,
|
|
|
|
size_t buflen,
|
|
|
|
void *opaque,
|
|
|
|
Error **errp)
|
2015-10-24 12:44:13 +02:00
|
|
|
{
|
|
|
|
Buffer *header = opaque;
|
|
|
|
|
|
|
|
g_assert_cmpint(buflen + offset, <=, header->capacity);
|
|
|
|
|
|
|
|
memcpy(header->buffer + offset, buf, buflen);
|
|
|
|
header->offset = offset + buflen;
|
|
|
|
|
2022-06-09 17:27:38 +02:00
|
|
|
return 0;
|
2015-10-24 12:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object *test_block_secret(void)
|
|
|
|
{
|
|
|
|
return object_new_with_props(
|
|
|
|
TYPE_QCRYPTO_SECRET,
|
|
|
|
object_get_objects_root(),
|
|
|
|
"sec0",
|
|
|
|
&error_abort,
|
|
|
|
"data", "123456",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_block_assert_setup(const struct QCryptoBlockTestData *data,
|
|
|
|
QCryptoBlock *blk)
|
|
|
|
{
|
|
|
|
QCryptoIVGen *ivgen;
|
|
|
|
QCryptoCipher *cipher;
|
|
|
|
|
|
|
|
ivgen = qcrypto_block_get_ivgen(blk);
|
|
|
|
cipher = qcrypto_block_get_cipher(blk);
|
|
|
|
|
|
|
|
g_assert(ivgen);
|
|
|
|
g_assert(cipher);
|
|
|
|
|
|
|
|
g_assert_cmpint(data->cipher_alg, ==, cipher->alg);
|
|
|
|
g_assert_cmpint(data->cipher_mode, ==, cipher->mode);
|
|
|
|
g_assert_cmpint(data->hash_alg, ==,
|
|
|
|
qcrypto_block_get_kdf_hash(blk));
|
|
|
|
|
|
|
|
g_assert_cmpint(data->ivgen_alg, ==,
|
|
|
|
qcrypto_ivgen_get_algorithm(ivgen));
|
|
|
|
g_assert_cmpint(data->ivgen_hash, ==,
|
|
|
|
qcrypto_ivgen_get_hash(ivgen));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void test_block(gconstpointer opaque)
|
|
|
|
{
|
|
|
|
const struct QCryptoBlockTestData *data = opaque;
|
|
|
|
QCryptoBlock *blk;
|
|
|
|
Buffer header;
|
|
|
|
Object *sec = test_block_secret();
|
|
|
|
|
|
|
|
memset(&header, 0, sizeof(header));
|
|
|
|
buffer_init(&header, "header");
|
|
|
|
|
2017-06-23 18:24:17 +02:00
|
|
|
blk = qcrypto_block_create(data->create_opts, NULL,
|
2015-10-24 12:44:13 +02:00
|
|
|
test_block_init_func,
|
|
|
|
test_block_write_func,
|
|
|
|
&header,
|
|
|
|
&error_abort);
|
|
|
|
g_assert(blk);
|
|
|
|
|
|
|
|
if (data->expect_header) {
|
|
|
|
g_assert_cmpint(header.capacity, >, 0);
|
|
|
|
} else {
|
|
|
|
g_assert_cmpint(header.capacity, ==, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_block_assert_setup(data, blk);
|
|
|
|
|
|
|
|
qcrypto_block_free(blk);
|
|
|
|
object_unparent(sec);
|
|
|
|
|
|
|
|
/* Ensure we can't open without the secret */
|
2017-06-23 18:24:17 +02:00
|
|
|
blk = qcrypto_block_open(data->open_opts, NULL,
|
2015-10-24 12:44:13 +02:00
|
|
|
test_block_read_func,
|
|
|
|
&header,
|
|
|
|
0,
|
2018-12-07 17:13:51 +01:00
|
|
|
1,
|
2015-10-24 12:44:13 +02:00
|
|
|
NULL);
|
|
|
|
g_assert(blk == NULL);
|
|
|
|
|
|
|
|
/* Ensure we can't open without the secret, unless NO_IO */
|
2017-06-23 18:24:17 +02:00
|
|
|
blk = qcrypto_block_open(data->open_opts, NULL,
|
2015-10-24 12:44:13 +02:00
|
|
|
test_block_read_func,
|
|
|
|
&header,
|
|
|
|
QCRYPTO_BLOCK_OPEN_NO_IO,
|
2018-12-07 17:13:51 +01:00
|
|
|
1,
|
2015-10-24 12:44:13 +02:00
|
|
|
&error_abort);
|
|
|
|
|
|
|
|
g_assert(qcrypto_block_get_cipher(blk) == NULL);
|
|
|
|
g_assert(qcrypto_block_get_ivgen(blk) == NULL);
|
|
|
|
|
|
|
|
qcrypto_block_free(blk);
|
|
|
|
|
|
|
|
|
|
|
|
/* Now open for real with secret */
|
|
|
|
sec = test_block_secret();
|
2017-06-23 18:24:17 +02:00
|
|
|
blk = qcrypto_block_open(data->open_opts, NULL,
|
2015-10-24 12:44:13 +02:00
|
|
|
test_block_read_func,
|
|
|
|
&header,
|
|
|
|
0,
|
2018-12-07 17:13:51 +01:00
|
|
|
1,
|
2015-10-24 12:44:13 +02:00
|
|
|
&error_abort);
|
|
|
|
g_assert(blk);
|
|
|
|
|
|
|
|
test_block_assert_setup(data, blk);
|
|
|
|
|
|
|
|
qcrypto_block_free(blk);
|
|
|
|
|
|
|
|
object_unparent(sec);
|
|
|
|
|
|
|
|
buffer_free(&header);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-05 15:57:17 +02:00
|
|
|
#ifdef TEST_LUKS
|
|
|
|
typedef const char *(*LuksHeaderDoBadStuff)(QCryptoBlockLUKSHeader *hdr);
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_luks_bad_header(gconstpointer data)
|
|
|
|
{
|
|
|
|
LuksHeaderDoBadStuff badstuff = data;
|
|
|
|
QCryptoBlock *blk;
|
|
|
|
Buffer buf;
|
|
|
|
Object *sec = test_block_secret();
|
|
|
|
QCryptoBlockLUKSHeader hdr;
|
|
|
|
Error *err = NULL;
|
|
|
|
const char *msg;
|
|
|
|
|
|
|
|
memset(&buf, 0, sizeof(buf));
|
|
|
|
buffer_init(&buf, "header");
|
|
|
|
|
|
|
|
/* Correctly create the volume initially */
|
|
|
|
blk = qcrypto_block_create(&luks_create_opts_default, NULL,
|
|
|
|
test_block_init_func,
|
|
|
|
test_block_write_func,
|
|
|
|
&buf,
|
|
|
|
&error_abort);
|
|
|
|
g_assert(blk);
|
|
|
|
|
|
|
|
qcrypto_block_free(blk);
|
|
|
|
|
|
|
|
/* Mangle it in some unpleasant way */
|
|
|
|
g_assert(buf.offset >= sizeof(hdr));
|
|
|
|
memcpy(&hdr, buf.buffer, sizeof(hdr));
|
|
|
|
qcrypto_block_luks_to_disk_endian(&hdr);
|
|
|
|
|
|
|
|
msg = badstuff(&hdr);
|
|
|
|
|
|
|
|
qcrypto_block_luks_from_disk_endian(&hdr);
|
|
|
|
memcpy(buf.buffer, &hdr, sizeof(hdr));
|
|
|
|
|
|
|
|
/* Check that we fail to open it again */
|
|
|
|
blk = qcrypto_block_open(&luks_open_opts, NULL,
|
|
|
|
test_block_read_func,
|
|
|
|
&buf,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
&err);
|
|
|
|
g_assert(!blk);
|
|
|
|
g_assert(err);
|
|
|
|
|
|
|
|
g_assert_cmpstr(error_get_pretty(err), ==, msg);
|
|
|
|
error_free(err);
|
|
|
|
|
|
|
|
object_unparent(sec);
|
|
|
|
|
|
|
|
buffer_free(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_null_term_cipher_name(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
/* Replace NUL termination with spaces */
|
|
|
|
char *offset = hdr->cipher_name + strlen(hdr->cipher_name);
|
|
|
|
memset(offset, ' ', sizeof(hdr->cipher_name) - (offset - hdr->cipher_name));
|
|
|
|
|
|
|
|
return "LUKS header cipher name is not NUL terminated";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_null_term_cipher_mode(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
/* Replace NUL termination with spaces */
|
|
|
|
char *offset = hdr->cipher_mode + strlen(hdr->cipher_mode);
|
|
|
|
memset(offset, ' ', sizeof(hdr->cipher_mode) - (offset - hdr->cipher_mode));
|
|
|
|
|
|
|
|
return "LUKS header cipher mode is not NUL terminated";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_null_term_hash_spec(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
/* Replace NUL termination with spaces */
|
|
|
|
char *offset = hdr->hash_spec + strlen(hdr->hash_spec);
|
|
|
|
memset(offset, ' ', sizeof(hdr->hash_spec) - (offset - hdr->hash_spec));
|
|
|
|
|
|
|
|
return "LUKS header hash spec is not NUL terminated";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_cipher_name_empty(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_name, "", 1);
|
|
|
|
|
|
|
|
return "Algorithm '' with key size 32 bytes not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_cipher_name_unknown(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_name, "aess", 5);
|
|
|
|
|
|
|
|
return "Algorithm 'aess' with key size 32 bytes not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_cipher_xts_size(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->master_key_len = 33;
|
|
|
|
|
|
|
|
return "XTS cipher key length should be a multiple of 2";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_cipher_cbc_size(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->master_key_len = 33;
|
|
|
|
memcpy(hdr->cipher_mode, "cbc-essiv", 10);
|
|
|
|
|
|
|
|
return "Algorithm 'aes' with key size 33 bytes not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_cipher_mode_empty(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "", 1);
|
|
|
|
|
|
|
|
return "Unexpected cipher mode string format ''";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_cipher_mode_unknown(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "xfs", 4);
|
|
|
|
|
|
|
|
return "Unexpected cipher mode string format 'xfs'";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_ivgen_separator(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "xts:plain64", 12);
|
|
|
|
|
|
|
|
return "Unexpected cipher mode string format 'xts:plain64'";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_ivgen_name_empty(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "xts-", 5);
|
|
|
|
|
|
|
|
return "IV generator '' not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_ivgen_name_unknown(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "xts-plain65", 12);
|
|
|
|
|
|
|
|
return "IV generator 'plain65' not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_ivgen_hash_empty(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "xts-plain65:", 13);
|
|
|
|
|
|
|
|
return "Hash algorithm '' not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_ivgen_hash_unknown(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->cipher_mode, "xts-plain65:sha257", 19);
|
|
|
|
|
|
|
|
return "Hash algorithm 'sha257' not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_hash_spec_empty(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->hash_spec, "", 1);
|
|
|
|
|
|
|
|
return "Hash algorithm '' not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_hash_spec_unknown(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
memcpy(hdr->hash_spec, "sha2566", 8);
|
|
|
|
|
|
|
|
return "Hash algorithm 'sha2566' not supported";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_stripes(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->key_slots[0].stripes = 3999;
|
|
|
|
|
|
|
|
return "Keyslot 0 is corrupted (stripes 3999 != 4000)";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_key_overlap_header(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->key_slots[0].key_offset_sector = 2;
|
|
|
|
|
|
|
|
return "Keyslot 0 is overlapping with the LUKS header";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_key_overlap_key(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->key_slots[0].key_offset_sector = hdr->key_slots[1].key_offset_sector;
|
|
|
|
|
|
|
|
return "Keyslots 0 and 1 are overlapping in the header";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_key_overlap_payload(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->key_slots[0].key_offset_sector = hdr->payload_offset_sector + 42;
|
|
|
|
|
|
|
|
return "Keyslot 0 is overlapping with the encrypted payload";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_payload_overlap_header(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->payload_offset_sector = 2;
|
|
|
|
|
|
|
|
return "LUKS payload is overlapping with the header";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_key_iterations(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->key_slots[0].iterations = 0;
|
|
|
|
|
|
|
|
return "Keyslot 0 iteration count is zero";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *luks_bad_iterations(QCryptoBlockLUKSHeader *hdr)
|
|
|
|
{
|
|
|
|
hdr->master_key_iterations = 0;
|
|
|
|
|
|
|
|
return "LUKS key iteration count is zero";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-24 12:44:13 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
gsize i;
|
|
|
|
|
|
|
|
module_call_init(MODULE_INIT_QOM);
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
|
|
|
|
g_assert(qcrypto_init(NULL) == 0);
|
|
|
|
|
|
|
|
for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
|
2015-10-24 12:55:48 +02:00
|
|
|
if (test_data[i].open_opts->format == Q_CRYPTO_BLOCK_FORMAT_LUKS &&
|
|
|
|
!qcrypto_hash_supports(test_data[i].hash_alg)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!test_data[i].slow ||
|
|
|
|
g_test_slow()) {
|
|
|
|
g_test_add_data_func(test_data[i].path, &test_data[i], test_block);
|
|
|
|
}
|
2015-10-24 12:44:13 +02:00
|
|
|
}
|
|
|
|
|
2022-09-05 15:57:17 +02:00
|
|
|
#ifdef TEST_LUKS
|
|
|
|
if (g_test_slow()) {
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-name-nul-term",
|
|
|
|
luks_bad_null_term_cipher_name,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-mode-nul-term",
|
|
|
|
luks_bad_null_term_cipher_mode,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/hash-spec-nul-term",
|
|
|
|
luks_bad_null_term_hash_spec,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-name-empty",
|
|
|
|
luks_bad_cipher_name_empty,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-name-unknown",
|
|
|
|
luks_bad_cipher_name_unknown,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-xts-size",
|
|
|
|
luks_bad_cipher_xts_size,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-cbc-size",
|
|
|
|
luks_bad_cipher_cbc_size,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-mode-empty",
|
|
|
|
luks_bad_cipher_mode_empty,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/cipher-mode-unknown",
|
|
|
|
luks_bad_cipher_mode_unknown,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/ivgen-separator",
|
|
|
|
luks_bad_ivgen_separator,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/ivgen-name-empty",
|
|
|
|
luks_bad_ivgen_name_empty,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/ivgen-name-unknown",
|
|
|
|
luks_bad_ivgen_name_unknown,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/ivgen-hash-empty",
|
|
|
|
luks_bad_ivgen_hash_empty,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/ivgen-hash-unknown",
|
|
|
|
luks_bad_ivgen_hash_unknown,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/hash-spec-empty",
|
|
|
|
luks_bad_hash_spec_empty,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/hash-spec-unknown",
|
|
|
|
luks_bad_hash_spec_unknown,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/stripes",
|
|
|
|
luks_bad_stripes,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/key-overlap-header",
|
|
|
|
luks_bad_key_overlap_header,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/key-overlap-key",
|
|
|
|
luks_bad_key_overlap_key,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/key-overlap-payload",
|
|
|
|
luks_bad_key_overlap_payload,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/payload-overlap-header",
|
|
|
|
luks_bad_payload_overlap_header,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/iterations",
|
|
|
|
luks_bad_iterations,
|
|
|
|
test_luks_bad_header);
|
|
|
|
g_test_add_data_func("/crypto/block/luks/bad/key-iterations",
|
|
|
|
luks_bad_key_iterations,
|
|
|
|
test_luks_bad_header);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-24 12:44:13 +02:00
|
|
|
return g_test_run();
|
|
|
|
}
|