From 3b568940b0414db2fb78b1662d3e43c896df51ac Mon Sep 17 00:00:00 2001
From: wangzhiqiang <wangzhiqiang95@huawei.com>
Date: Fri, 29 Jul 2022 20:41:21 +0800
Subject: [PATCH] [Backport]add test case for sm4-xts-plain64

add test case
api-test-sm.c: test cryptsetup with sm4-xts-plain64 and plain
api-test-2-sm.c: test cryptsetup with sm4-xts-plain64 and luks

Conflict:NA
Reference:https://github.com/mbroz/cryptsetup/blob/main/tests/api-test.c

Signed-off-by: wangzhiqiang <wangzhiqiang95@huawei.com>
---
 tests/00modules-test  |   1 +
 tests/Makefile.am     |  16 +-
 tests/api-test-2-sm.c | 831 +++++++++++++++++++++++++++++++++++++
 tests/api-test-sm.c   | 933 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1780 insertions(+), 1 deletion(-)
 create mode 100644 tests/api-test-2-sm.c
 create mode 100644 tests/api-test-sm.c

diff --git a/tests/00modules-test b/tests/00modules-test
index 10f52cc..a004b98 100755
--- a/tests/00modules-test
+++ b/tests/00modules-test
@@ -41,6 +41,7 @@ modprobe dm-crypt     >/dev/null 2>&1
 modprobe dm-verity    >/dev/null 2>&1
 modprobe dm-integrity >/dev/null 2>&1
 modprobe dm-zero      >/dev/null 2>&1
+modprobe sm4_generic  >/dev/null 2>&1
 
 dmsetup version
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index dc8fa00..125c7d8 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,8 @@
 TESTS = 00modules-test \
 	api-test \
 	api-test-2 \
+	api-test-sm \
+       api-test-2-sm \
 	compat-test-args \
 	compat-test \
 	compat-test2 \
@@ -110,6 +112,18 @@ api_test_2_LDFLAGS = $(AM_LDFLAGS) -static
 api_test_2_CFLAGS = -g -Wall -O0 $(AM_CFLAGS) -I$(top_srcdir)/lib
 api_test_2_CPPFLAGS = $(AM_CPPFLAGS) -include config.h
 
+api_test_sm_SOURCES = api-test-sm.c api_test.h test_utils.c
+api_test_sm_LDADD = ../libcryptsetup.la
+api_test_sm_LDFLAGS = $(AM_LDFLAGS) -static
+api_test_sm_CFLAGS = -g -Wall -O0 $(AM_CFLAGS) -I$(top_srcdir)/lib
+api_test_sm_CPPFLAGS = $(AM_CPPFLAGS) -include config.h
+
+api_test_2_sm_SOURCES = api-test-2-sm.c api_test.h test_utils.c
+api_test_2_sm_LDADD = ../libcryptsetup.la
+api_test_2_sm_LDFLAGS = $(AM_LDFLAGS) -static
+api_test_2_sm_CFLAGS = -g -Wall -O0 $(AM_CFLAGS) -I$(top_srcdir)/lib
+api_test_2_sm_CPPFLAGS = $(AM_CPPFLAGS) -include config.h
+
 vectors_test_SOURCES = crypto-vectors.c
 vectors_test_LDADD = ../libcrypto_backend.la @CRYPTO_LIBS@ @LIBARGON2_LIBS@
 vectors_test_LDFLAGS = $(AM_LDFLAGS) -static
@@ -134,7 +148,7 @@ all_symbols_test_LDFLAGS = $(AM_LDFLAGS) -ldl
 all_symbols_test_CFLAGS = $(AM_CFLAGS)
 all_symbols_test_CPPFLAGS = $(AM_CPPFLAGS) -D_GNU_SOURCE
 
-check_PROGRAMS = api-test api-test-2 differ vectors-test unit-utils-io all-symbols-test
+check_PROGRAMS = api-test api-test-2 api-test-sm api-test-2-sm differ vectors-test unit-utils-io all-symbols-test
 
 check-programs: $(check_PROGRAMS) fake_token_path.so
 
diff --git a/tests/api-test-2-sm.c b/tests/api-test-2-sm.c
new file mode 100644
index 0000000..73ecbeb
--- /dev/null
+++ b/tests/api-test-2-sm.c
@@ -0,0 +1,831 @@
+/*
+ * cryptsetup library LUKS2 API check functions
+ *
+ * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2021 Milan Broz
+ * Copyright (C) 2016-2021 Ondrej Kozina
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <signal.h>
+#include <sys/stat.h>
+#include <inttypes.h>
+#include <sys/types.h>
+#ifdef KERNEL_KEYRING
+#include <linux/keyctl.h>
+#include <sys/syscall.h>
+#ifndef HAVE_KEY_SERIAL_T
+#define HAVE_KEY_SERIAL_T
+#include <stdint.h>
+typedef int32_t key_serial_t;
+#endif
+#endif
+
+#include "api_test.h"
+#include "luks1/luks.h"
+#include "libcryptsetup.h"
+
+#define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
+#define DEVICE_EMPTY_name "crypt_zero"
+#define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
+#define DEVICE_ERROR_name "crypt_error"
+#define DEVICE_ERROR DMDIR DEVICE_ERROR_name
+
+#define CDEVICE_1 "ctest1"
+#define CDEVICE_2 "ctest2"
+#define CDEVICE_WRONG "O_o"
+#define H_DEVICE "head_ok"
+#define H_DEVICE_WRONG "head_wr"
+#define L_DEVICE_1S "luks_onesec"
+#define L_DEVICE_0S "luks_zerosec"
+#define L_DEVICE_WRONG "luks_wr"
+#define L_DEVICE_OK "luks_ok"
+#define REQS_LUKS2_HEADER "luks2_header_requirements"
+#define NO_REQS_LUKS2_HEADER "luks2_header_requirements_free"
+#define BACKUP_FILE "csetup_backup_file"
+#define IMAGE1 "compatimage2.img"
+#define IMAGE_EMPTY "empty.img"
+#define IMAGE_EMPTY_SMALL "empty_small.img"
+#define IMAGE_EMPTY_SMALL_2 "empty_small2.img"
+#define IMAGE_PV_LUKS2_SEC "blkid-luks2-pv.img"
+
+#define KEYFILE1 "key1.file"
+#define KEY1 "compatkey"
+
+#define KEYFILE2 "key2.file"
+#define KEY2 "0123456789abcdef"
+
+#define PASSPHRASE "blabla"
+#define PASSPHRASE1 "albalb"
+
+#define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
+
+#define DEVICE_WRONG "/dev/Ooo_"
+#define DEVICE_CHAR "/dev/zero"
+#define THE_LFILE_TEMPLATE "cryptsetup-tstlp.XXXXXX"
+
+#define KEY_DESC_TEST0 "cs_token_test:test_key0"
+#define KEY_DESC_TEST1 "cs_token_test:test_key1"
+
+#define CONV_DIR "conversion_imgs"
+#define CONV_L1_128 "l1_128b"
+#define CONV_L1_256 "l1_256b"
+#define CONV_L1_512 "l1_512b"
+#define CONV_L2_128 "l2_128b"
+#define CONV_L2_128_FULL "l2_128b_full"
+#define CONV_L2_256 "l2_256b"
+#define CONV_L2_256_FULL "l2_256b_full"
+#define CONV_L2_512 "l2_512b"
+#define CONV_L2_512_FULL "l2_512b_full"
+#define CONV_L1_128_DET "l1_128b_det"
+#define CONV_L1_256_DET "l1_256b_det"
+#define CONV_L1_512_DET "l1_512b_det"
+#define CONV_L2_128_DET "l2_128b_det"
+#define CONV_L2_128_DET_FULL "l2_128b_det_full"
+#define CONV_L2_256_DET "l2_256b_det"
+#define CONV_L2_256_DET_FULL "l2_256b_det_full"
+#define CONV_L2_512_DET "l2_512b_det"
+#define CONV_L2_512_DET_FULL "l2_512b_det_full"
+#define CONV_L1_256_LEGACY "l1_256b_legacy_offset"
+#define CONV_L1_256_UNMOVABLE "l1_256b_unmovable"
+#define PASS0 "aaa"
+#define PASS1 "hhh"
+#define PASS2 "ccc"
+#define PASS3 "ddd"
+#define PASS4 "eee"
+#define PASS5 "fff"
+#define PASS6 "ggg"
+#define PASS7 "bbb"
+#define PASS8 "iii"
+
+static int _fips_mode = 0;
+
+static char *DEVICE_1 = NULL;
+static char *DEVICE_2 = NULL;
+static char *DEVICE_3 = NULL;
+static char *DEVICE_4 = NULL;
+static char *DEVICE_5 = NULL;
+static char *DEVICE_6 = NULL;
+
+static char *tmp_file_1 = NULL;
+static char *test_loop_file = NULL;
+
+unsigned int test_progress_steps;
+
+struct crypt_device *cd = NULL, *cd2 = NULL;
+
+static const char *default_luks1_hash = NULL;
+static uint32_t default_luks1_iter_time = 0;
+
+static const char *default_luks2_pbkdf = NULL;
+static uint32_t default_luks2_iter_time = 0;
+static uint32_t default_luks2_memory_kb = 0;
+static uint32_t default_luks2_parallel_threads = 0;
+
+static struct crypt_pbkdf_type min_pbkdf2 = {
+	.type = "pbkdf2",
+	.iterations = 1000,
+	.flags = CRYPT_PBKDF_NO_BENCHMARK
+}, min_argon2 = {
+	.type = "argon2id",
+	.iterations = 4,
+	.max_memory_kb = 32,
+	.parallel_threads = 1,
+	.flags = CRYPT_PBKDF_NO_BENCHMARK
+};
+
+// Helpers
+
+static int get_luks2_offsets(int metadata_device,
+			    unsigned int alignpayload_sec,
+			    unsigned int sector_size,
+			    uint64_t *r_header_size,
+			    uint64_t *r_payload_offset)
+{
+	struct crypt_device *cd = NULL;
+	static uint64_t default_header_size = 0;
+
+	if (!default_header_size) {
+		if (crypt_init(&cd, THE_LOOP_DEV))
+			return -EINVAL;
+		if (crypt_format(cd, CRYPT_LUKS2, "aes", "xts-plain64", NULL, NULL, 64, NULL)) {
+			crypt_free(cd);
+			return -EINVAL;
+		}
+
+		default_header_size = crypt_get_data_offset(cd);
+
+		crypt_free(cd);
+	}
+
+	if (!sector_size)
+		sector_size = 512; /* default? */
+
+	if ((sector_size % 512) && (sector_size % 4096))
+		return -1;
+
+	if (r_payload_offset) {
+		if (metadata_device)
+			*r_payload_offset = alignpayload_sec * sector_size;
+		else
+			*r_payload_offset = DIV_ROUND_UP_MODULO(default_header_size * 512, (alignpayload_sec ?: 1) * sector_size);
+
+		*r_payload_offset /= sector_size;
+	}
+
+	if (r_header_size)
+		*r_header_size = default_header_size;
+
+	return 0;
+}
+
+static bool get_luks_pbkdf_defaults(void)
+{
+	const struct crypt_pbkdf_type *pbkdf_defaults = crypt_get_pbkdf_default(CRYPT_LUKS1);
+
+	if (!pbkdf_defaults)
+		return false;
+
+	default_luks1_hash = pbkdf_defaults->hash;
+	default_luks1_iter_time = pbkdf_defaults->time_ms;
+
+	pbkdf_defaults = crypt_get_pbkdf_default(CRYPT_LUKS2);
+	if (!pbkdf_defaults)
+		return false;
+
+	default_luks2_pbkdf = pbkdf_defaults->type;
+	default_luks2_iter_time = pbkdf_defaults->time_ms;
+	default_luks2_memory_kb = pbkdf_defaults->max_memory_kb;
+	default_luks2_parallel_threads = pbkdf_defaults->parallel_threads;
+
+	return true;
+}
+
+static void _remove_keyfiles(void)
+{
+	remove(KEYFILE1);
+	remove(KEYFILE2);
+}
+
+#if HAVE_DECL_DM_TASK_RETRY_REMOVE
+#define DM_RETRY "--retry "
+#else
+#define DM_RETRY ""
+#endif
+
+#define DM_NOSTDERR " 2>/dev/null"
+
+static void _cleanup_dmdevices(void)
+{
+	struct stat st;
+
+	if (!stat(DMDIR H_DEVICE, &st))
+		_system("dmsetup remove " DM_RETRY H_DEVICE DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR H_DEVICE_WRONG, &st))
+		_system("dmsetup remove " DM_RETRY H_DEVICE_WRONG DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_0S, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_0S DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_1S, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_1S DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_WRONG, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_WRONG DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_OK, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_OK DM_NOSTDERR, 0);
+
+	t_dev_offset = 0;
+}
+
+static void _cleanup(void)
+{
+	struct stat st;
+
+	CRYPT_FREE(cd);
+	CRYPT_FREE(cd2);
+
+	//_system("udevadm settle", 0);
+
+	if (!stat(DMDIR CDEVICE_1, &st))
+		_system("dmsetup remove " DM_RETRY CDEVICE_1 DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR CDEVICE_2, &st))
+		_system("dmsetup remove " DM_RETRY CDEVICE_2 DM_NOSTDERR, 0);
+
+	if (!stat(DEVICE_EMPTY, &st))
+		_system("dmsetup remove " DM_RETRY DEVICE_EMPTY_name DM_NOSTDERR, 0);
+
+	if (!stat(DEVICE_ERROR, &st))
+		_system("dmsetup remove " DM_RETRY DEVICE_ERROR_name DM_NOSTDERR, 0);
+
+	_cleanup_dmdevices();
+
+	if (loop_device(THE_LOOP_DEV))
+		loop_detach(THE_LOOP_DEV);
+
+	if (loop_device(DEVICE_1))
+		loop_detach(DEVICE_1);
+
+	if (loop_device(DEVICE_2))
+		loop_detach(DEVICE_2);
+
+	if (loop_device(DEVICE_3))
+		loop_detach(DEVICE_3);
+
+	if (loop_device(DEVICE_4))
+		loop_detach(DEVICE_4);
+
+	if (loop_device(DEVICE_5))
+		loop_detach(DEVICE_5);
+
+	if (loop_device(DEVICE_6))
+		loop_detach(DEVICE_6);
+
+	_system("rm -f " IMAGE_EMPTY, 0);
+	_system("rm -f " IMAGE1, 0);
+	_system("rm -rf " CONV_DIR, 0);
+
+	if (test_loop_file)
+		remove(test_loop_file);
+	if (tmp_file_1)
+		remove(tmp_file_1);
+
+	remove(REQS_LUKS2_HEADER);
+	remove(NO_REQS_LUKS2_HEADER);
+	remove(BACKUP_FILE);
+	remove(IMAGE_PV_LUKS2_SEC);
+	remove(IMAGE_PV_LUKS2_SEC ".bcp");
+	remove(IMAGE_EMPTY_SMALL);
+	remove(IMAGE_EMPTY_SMALL_2);
+
+	_remove_keyfiles();
+
+	free(tmp_file_1);
+	free(test_loop_file);
+	free(THE_LOOP_DEV);
+	free(DEVICE_1);
+	free(DEVICE_2);
+	free(DEVICE_3);
+	free(DEVICE_4);
+	free(DEVICE_5);
+	free(DEVICE_6);
+}
+
+static int _setup(void)
+{
+	int fd, ro = 0;
+	char cmd[128];
+
+	test_loop_file = strdup(THE_LFILE_TEMPLATE);
+	if (!test_loop_file)
+		return 1;
+
+	if ((fd=mkstemp(test_loop_file)) == -1) {
+		printf("cannot create temporary file with template %s\n", test_loop_file);
+		return 1;
+	}
+	close(fd);
+	snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
+		 test_loop_file, TST_SECTOR_SIZE, TST_LOOP_FILE_SIZE);
+	if (_system(cmd, 1))
+		return 1;
+
+	fd = loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
+	close(fd);
+
+	tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
+	if (!tmp_file_1)
+		return 1;
+
+	if ((fd=mkstemp(tmp_file_1)) == -1) {
+		printf("cannot create temporary file with template %s\n", tmp_file_1);
+		return 1;
+	}
+	close(fd);
+	snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
+		 tmp_file_1, TST_SECTOR_SIZE, 10);
+	if (_system(cmd, 1))
+		return 1;
+
+	_system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
+	_system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
+
+	_system(" [ ! -e " IMAGE1 " ] && xz -dk " IMAGE1 ".xz", 1);
+	fd = loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
+	close(fd);
+
+	_system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=32 2>/dev/null", 1);
+	fd = loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
+	close(fd);
+
+	_system("dd if=/dev/zero of=" IMAGE_EMPTY_SMALL " bs=1M count=7 2>/dev/null", 1);
+
+	_system("dd if=/dev/zero of=" IMAGE_EMPTY_SMALL_2 " bs=512 count=2050 2>/dev/null", 1);
+
+	_system(" [ ! -e " NO_REQS_LUKS2_HEADER " ] && xz -dk " NO_REQS_LUKS2_HEADER ".xz", 1);
+	fd = loop_attach(&DEVICE_4, NO_REQS_LUKS2_HEADER, 0, 0, &ro);
+	close(fd);
+
+	_system(" [ ! -e " REQS_LUKS2_HEADER " ] && xz -dk " REQS_LUKS2_HEADER ".xz", 1);
+	fd = loop_attach(&DEVICE_5, REQS_LUKS2_HEADER, 0, 0, &ro);
+	close(fd);
+
+	_system(" [ ! -e " IMAGE_PV_LUKS2_SEC " ] && xz -dk " IMAGE_PV_LUKS2_SEC ".xz", 1);
+	_system(" [ ! -e " IMAGE_PV_LUKS2_SEC ".bcp ] && cp " IMAGE_PV_LUKS2_SEC " " IMAGE_PV_LUKS2_SEC ".bcp", 1);
+	fd = loop_attach(&DEVICE_6, IMAGE_PV_LUKS2_SEC, 0, 0, &ro);
+	close(fd);
+
+	_system(" [ ! -d " CONV_DIR " ] && tar xJf " CONV_DIR ".tar.xz 2>/dev/null", 1);
+
+	if (_system("modprobe dm-crypt >/dev/null 2>&1", 1))
+		return 1;
+
+	if (t_dm_check_versions())
+		return 1;
+
+	_system("rmmod dm-crypt >/dev/null 2>&1", 0);
+
+	_fips_mode = fips_mode();
+	if (_debug)
+		printf("FIPS MODE: %d\n", _fips_mode);
+
+	/* Use default log callback */
+	crypt_set_log_callback(NULL, &global_log_callback, NULL);
+
+	if (!get_luks_pbkdf_defaults())
+		return 1;
+
+	min_pbkdf2.hash = min_argon2.hash = default_luks1_hash;
+
+	return 0;
+}
+
+static int set_fast_pbkdf(struct crypt_device *cd)
+{
+	const struct crypt_pbkdf_type *pbkdf = &min_argon2;
+
+	/* Cannot use Argon2 in FIPS */
+	if (_fips_mode)
+		pbkdf = &min_pbkdf2;
+
+	return crypt_set_pbkdf_type(cd, pbkdf);
+}
+
+
+static void AddDeviceLuks2(void)
+{
+	enum { OFFSET_1M = 2048 , OFFSET_2M = 4096, OFFSET_4M = 8192, OFFSET_8M = 16384 };
+	struct crypt_pbkdf_type pbkdf = {
+		.type = CRYPT_KDF_ARGON2I,
+		.hash = "sm3",
+		.parallel_threads = 4,
+		.max_memory_kb = 1024,
+		.time_ms = 1
+	}, pbkdf_tmp;
+	struct crypt_params_luks2 params = {
+		.pbkdf = &pbkdf,
+		.data_device = DEVICE_2,
+		.sector_size = 512
+	};
+	char key[128], key2[128], key3[128];
+
+	const char *tmp_buf, *passphrase = "blabla", *passphrase2 = "nsdkFI&Y#.sd";
+	const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a818af818af";
+	const char *mk_hex2 = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a818af818af";
+	size_t key_size = strlen(mk_hex) / 2;
+	const char *cipher = "sm4";
+	const char *cipher_mode = "xts-plain64";
+	uint64_t r_payload_offset, r_header_size, r_size_1;
+
+	/* Cannot use Argon2 in FIPS */
+	if (_fips_mode) {
+		pbkdf.type = CRYPT_KDF_PBKDF2;
+		pbkdf.parallel_threads = 0;
+		pbkdf.max_memory_kb = 0;
+	}
+
+	crypt_decode_key(key, mk_hex, key_size);
+	crypt_decode_key(key3, mk_hex2, key_size);
+
+	// init test devices
+	OK_(get_luks2_offsets(0, 0, 0, &r_header_size, &r_payload_offset));
+	OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
+	OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
+
+
+	// format
+	OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
+	params.data_alignment = 0;
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params), "Not enough space for keyslots material");
+	CRYPT_FREE(cd);
+
+	// test payload_offset = 0 for encrypted device with external header device
+	OK_(crypt_init(&cd, DMDIR H_DEVICE));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), 0);
+	CRYPT_FREE(cd);
+
+	params.data_alignment = 0;
+	params.data_device = NULL;
+
+	// test payload_offset = 0. format() should look up alignment offset from device topology
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	OK_(!(crypt_get_data_offset(cd) > 0));
+	CRYPT_FREE(cd);
+
+	// set_data_offset has priority, alignment must be 0 or must be compatible
+	params.data_alignment = 0;
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_set_data_offset(cd, OFFSET_8M));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), OFFSET_8M);
+	CRYPT_FREE(cd);
+
+	// Load gets the value from metadata
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(crypt_set_data_offset(cd, OFFSET_2M));
+	OK_(crypt_load(cd, CRYPT_LUKS2, NULL));
+	EQ_(crypt_get_data_offset(cd), OFFSET_8M);
+	CRYPT_FREE(cd);
+
+	params.data_alignment = OFFSET_4M;
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(set_fast_pbkdf(cd));
+	FAIL_(crypt_set_data_offset(cd, OFFSET_2M + 1), "Not aligned to 4096"); // must be aligned to 4k
+	OK_(crypt_set_data_offset(cd, OFFSET_2M));
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params), "Alignment not compatible");
+	OK_(crypt_set_data_offset(cd, OFFSET_4M));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), OFFSET_4M);
+	CRYPT_FREE(cd);
+
+	/*
+	 * test limit values for backing device size
+	 */
+	params.data_alignment = OFFSET_4M;
+	OK_(get_luks2_offsets(0, params.data_alignment, 0, NULL, &r_payload_offset));
+	OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
+	OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
+	OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
+
+	// 1 sector less than required
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
+	OK_(set_fast_pbkdf(cd));
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params),	"Device too small");
+	CRYPT_FREE(cd);
+
+	// 0 sectors for encrypted area
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
+	CRYPT_FREE(cd);
+
+	// 1 sector for encrypted area
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), r_payload_offset);
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	OK_(t_device_size(DMDIR CDEVICE_1, &r_size_1));
+	EQ_(r_size_1, TST_SECTOR_SIZE);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	// restrict format only to empty context
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params), "Context is already formatted");
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formatted");
+	// change data device to wrong one
+	OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
+	OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	params.data_alignment = 0;
+	params.data_device = DEVICE_2;
+
+	// generate keyslot material at the end of luks header
+	OK_(crypt_init(&cd, DMDIR H_DEVICE));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
+	EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
+
+	OK_(crypt_keyslot_get_pbkdf(cd, 7, &pbkdf_tmp));
+	OK_(strcmp(pbkdf_tmp.type, pbkdf.type));
+	if (!_fips_mode) {
+		NULL_(pbkdf_tmp.hash);
+		OK_(!(pbkdf_tmp.max_memory_kb >= 32));
+		OK_(!(pbkdf_tmp.parallel_threads >= 1));
+	} else
+		OK_(strcmp(pbkdf_tmp.hash, pbkdf.hash));
+	OK_(!(pbkdf_tmp.iterations >= 4));
+	EQ_(0, pbkdf_tmp.time_ms); /* not usable in per-keyslot call */
+
+	CRYPT_FREE(cd);
+	OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
+	OK_(set_fast_pbkdf(cd));
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params), "Context is already formatted");
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	CRYPT_FREE(cd);
+	// check active status without header
+	OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, NULL));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	NULL_(crypt_get_type(cd));
+	OK_(strcmp(cipher, crypt_get_cipher(cd)));
+	OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	params.data_alignment = OFFSET_1M;
+	params.data_device = NULL;
+
+	// test uuid mismatch and _init_by_name_and_header
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	EQ_(0, crypt_header_is_detached(cd));
+	CRYPT_FREE(cd);
+	params.data_alignment = 0;
+	params.data_device = DEVICE_2;
+	OK_(crypt_init(&cd, DMDIR H_DEVICE));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+	CRYPT_FREE(cd);
+	// there we've got uuid mismatch
+	OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	NULL_(crypt_get_type(cd));
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
+	EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	FAIL_(crypt_header_is_detached(cd), "no header for mismatched device");
+	CRYPT_FREE(cd);
+
+	params.data_device = NULL;
+
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, &params));
+
+	// even with no keyslots defined it can be activated by volume key
+	OK_(crypt_volume_key_verify(cd, key, key_size));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
+	GE_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_2));
+
+	// now with keyslot
+	EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
+	EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
+	GE_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_2));
+
+	EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
+	OK_(prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
+	OK_(prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
+	EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
+	FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
+	EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
+	EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
+	FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
+	EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
+	EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
+	EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
+	FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
+	FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
+	FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
+	EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
+	OK_(crypt_keyslot_destroy(cd, 1));
+	OK_(crypt_keyslot_destroy(cd, 2));
+	OK_(crypt_keyslot_destroy(cd, 3));
+	OK_(crypt_keyslot_destroy(cd, 4));
+	OK_(crypt_deactivate(cd, CDEVICE_2));
+	_remove_keyfiles();
+
+	FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
+	key[1] = ~key[1];
+	FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
+	key[1] = ~key[1];
+	EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase2, strlen(passphrase2)));
+	EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
+
+	FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
+	FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
+	FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
+	OK_(crypt_keyslot_destroy(cd, 7));
+	EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
+
+	EQ_(6, crypt_keyslot_change_by_passphrase(cd, 6, CRYPT_ANY_SLOT, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
+	EQ_(7, crypt_keyslot_change_by_passphrase(cd, 6, 7, passphrase, strlen(passphrase), passphrase2, strlen(passphrase2)));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
+	EQ_(7, crypt_activate_by_passphrase(cd, NULL, 7, passphrase2, strlen(passphrase2), 0));
+	EQ_(6, crypt_keyslot_change_by_passphrase(cd, CRYPT_ANY_SLOT, 6, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
+
+	EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
+	OK_(crypt_volume_key_verify(cd, key2, key_size));
+	OK_(memcmp(key, key2, key_size));
+
+	OK_(strcmp(cipher, crypt_get_cipher(cd)));
+	OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	EQ_(r_payload_offset, crypt_get_data_offset(cd));
+	OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
+
+	reset_log();
+	OK_(crypt_dump(cd));
+	OK_(!(global_lines != 0));
+	reset_log();
+
+	FAIL_(crypt_dump_json(cd, NULL, 42), "flags be used later");
+	OK_(crypt_dump_json(cd, NULL, 0));
+	OK_(!(global_lines != 0));
+	reset_log();
+	OK_(crypt_dump_json(cd, &tmp_buf, 0));
+	OK_(!tmp_buf);
+	OK_(!(strlen(tmp_buf) != 0));
+
+	FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
+	OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
+	OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
+
+	FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
+	CRYPT_FREE(cd);
+	_cleanup_dmdevices();
+
+	/* LUKSv2 format tests */
+
+	/* very basic test */
+	OK_(crypt_init(&cd, DEVICE_2));
+	crypt_set_iteration_time(cd, 1);
+	FAIL_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, NULL, 0, NULL), "Wrong key size");
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, NULL));
+	CRYPT_FREE(cd);
+	/* some invalid parameters known to cause troubles */
+	OK_(crypt_init(&cd, DEVICE_2));
+	crypt_set_iteration_time(cd, 0); /* wrong for argon2 but we don't know the pbkdf type yet, ignored */
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, NULL));
+	CRYPT_FREE(cd);
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, NULL));
+	EQ_(crypt_keyslot_add_by_volume_key(cd, 0, NULL, key_size, PASSPHRASE, strlen(PASSPHRASE)), 0);
+	CRYPT_FREE(cd);
+
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, NULL, key_size, NULL));
+	FAIL_(crypt_keyslot_add_by_volume_key(cd, CRYPT_ANY_SLOT, key, key_size, PASSPHRASE, strlen(PASSPHRASE)), "VK doesn't match any digest");
+	FAIL_(crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, PASSPHRASE, strlen(PASSPHRASE)), "VK doesn't match any digest");
+	CRYPT_FREE(cd);
+
+	OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, NULL));
+	EQ_(crypt_keyslot_add_by_volume_key(cd, 3, NULL, key_size, PASSPHRASE, strlen(PASSPHRASE)), 3);
+	CRYPT_FREE(cd);
+
+	/*
+	 * Check regression in getting keyslot encryption parameters when
+	 * volume key size is unknown (no active keyslots).
+	 */
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(set_fast_pbkdf(cd));
+	OK_(crypt_format(cd, CRYPT_LUKS2, cipher, cipher_mode, NULL, key, key_size, NULL));
+	EQ_(crypt_keyslot_add_by_volume_key(cd, 0, NULL, key_size, PASSPHRASE, strlen(PASSPHRASE)), 0);
+	/* drop context copy of volume key */
+	CRYPT_FREE(cd);
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(crypt_load(cd, CRYPT_LUKS, NULL));
+	EQ_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key, &key_size, PASSPHRASE, strlen(PASSPHRASE)), 0);
+	OK_(crypt_keyslot_destroy(cd, 0));
+	OK_(set_fast_pbkdf(cd));
+	EQ_(crypt_keyslot_add_by_volume_key(cd, 0, key, key_size, PASSPHRASE, strlen(PASSPHRASE)), 0);
+	CRYPT_FREE(cd);
+
+	_cleanup_dmdevices();
+}
+
+
+static void int_handler(int sig __attribute__((__unused__)))
+{
+	_quit++;
+}
+
+int main(int argc, char *argv[])
+{
+	struct sigaction sa = { .sa_handler = int_handler };
+	int i;
+
+	if (getuid() != 0) {
+		printf("You must be root to run this test.\n");
+		exit(77);
+	}
+#ifndef NO_CRYPTSETUP_PATH
+	if (getenv("CRYPTSETUP_PATH")) {
+		printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
+		exit(77);
+	}
+#endif
+	for (i = 1; i < argc; i++) {
+		if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
+			_verbose = 1;
+		else if (!strcmp("--debug", argv[i]))
+			_debug = _verbose = 1;
+	}
+
+	/* Handle interrupt properly */
+	sigaction(SIGINT, &sa, NULL);
+	sigaction(SIGTERM, &sa, NULL);
+
+	register_cleanup(_cleanup);
+
+	_cleanup();
+	if (_setup()) {
+		printf("Cannot set test devices.\n");
+		_cleanup();
+		exit(77);
+	}
+
+	crypt_set_debug_level(_debug ? CRYPT_DEBUG_JSON : CRYPT_DEBUG_NONE);
+
+	RUN_(AddDeviceLuks2, "Format and use LUKS2 device");
+
+	_cleanup();
+	return 0;
+}
diff --git a/tests/api-test-sm.c b/tests/api-test-sm.c
new file mode 100644
index 0000000..cff59ce
--- /dev/null
+++ b/tests/api-test-sm.c
@@ -0,0 +1,933 @@
+/*
+ * cryptsetup library API check functions
+ *
+ * Copyright (C) 2009-2021 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2009-2021 Milan Broz
+ * Copyright (C) 2016-2021 Ondrej Kozina
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <signal.h>
+#include <sys/stat.h>
+#include <inttypes.h>
+#include <sys/types.h>
+
+#include "api_test.h"
+#include "luks1/luks.h"
+#include "libcryptsetup.h"
+
+#define DEVICE_1_UUID "28632274-8c8a-493f-835b-da802e1c576b"
+#define DEVICE_EMPTY_name "crypt_zero"
+#define DEVICE_EMPTY DMDIR DEVICE_EMPTY_name
+#define DEVICE_ERROR_name "crypt_error"
+#define DEVICE_ERROR DMDIR DEVICE_ERROR_name
+
+#define CDEVICE_1 "ctest1"
+#define CDEVICE_2 "ctest2"
+#define CDEVICE_WRONG "O_o"
+#define H_DEVICE "head_ok"
+#define H_DEVICE_WRONG "head_wr"
+#define L_DEVICE_1S "luks_onesec"
+#define L_DEVICE_0S "luks_zerosec"
+#define L_DEVICE_WRONG "luks_wr"
+#define L_DEVICE_OK "luks_ok"
+#define EVL_HEADER_1 "evil_hdr-luks_hdr_damage"
+#define EVL_HEADER_2 "evil_hdr-payload_overwrite"
+#define EVL_HEADER_3 "evil_hdr-stripes_payload_dmg"
+#define EVL_HEADER_4 "evil_hdr-small_luks_device"
+#define EVL_HEADER_5 "evil_hdr-keyslot_overlap"
+#define VALID_HEADER "valid_header_file"
+#define BACKUP_FILE "csetup_backup_file"
+#define IMAGE1 "compatimage.img"
+#define IMAGE_EMPTY "empty.img"
+
+#define KEYFILE1 "key1.file"
+#define KEY1 "compatkey"
+
+#define KEYFILE2 "key2.file"
+#define KEY2 "0123456789abcdef"
+
+#define PASSPHRASE "blabla"
+#define PASSPHRASE1 "albalb"
+
+#define DEVICE_TEST_UUID "12345678-1234-1234-1234-123456789abc"
+
+#define DEVICE_WRONG "/dev/Ooo_"
+#define DEVICE_CHAR "/dev/zero"
+#define THE_LFILE_TEMPLATE "cryptsetup-tstlp.XXXXXX"
+
+#define LUKS_PHDR_SIZE_B 1024
+
+static int _fips_mode = 0;
+
+static char *DEVICE_1 = NULL;
+static char *DEVICE_2 = NULL;
+static char *DEVICE_3 = NULL;
+
+static char *tmp_file_1 = NULL;
+static char *test_loop_file = NULL;
+
+struct crypt_device *cd = NULL, *cd2 = NULL;
+
+// Helpers
+
+static int get_luks_offsets(int metadata_device,
+			    size_t keylength,
+			    unsigned int alignpayload_sec,
+			    unsigned int alignoffset_sec,
+			    uint64_t *r_header_size,
+			    uint64_t *r_payload_offset)
+{
+	int i;
+	uint64_t current_sector;
+	uint32_t sectors_per_stripes_set;
+
+	if (!keylength) {
+		if (r_header_size)
+		    *r_header_size = 0;
+		if (r_payload_offset)
+		    *r_payload_offset = 0;
+		return -1;
+	}
+
+	sectors_per_stripes_set = DIV_ROUND_UP(keylength*LUKS_STRIPES, TST_SECTOR_SIZE);
+	current_sector = DIV_ROUND_UP_MODULO(DIV_ROUND_UP(LUKS_PHDR_SIZE_B, TST_SECTOR_SIZE),
+			LUKS_ALIGN_KEYSLOTS / TST_SECTOR_SIZE);
+	for (i=0; i < (LUKS_NUMKEYS - 1); i++)
+		current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
+				LUKS_ALIGN_KEYSLOTS / TST_SECTOR_SIZE);
+	if (r_header_size)
+		*r_header_size = current_sector + sectors_per_stripes_set;
+
+	current_sector = DIV_ROUND_UP_MODULO(current_sector + sectors_per_stripes_set,
+				LUKS_ALIGN_KEYSLOTS / TST_SECTOR_SIZE);
+
+	if (r_payload_offset) {
+		if (metadata_device)
+			*r_payload_offset = alignpayload_sec;
+		else
+			*r_payload_offset = DIV_ROUND_UP_MODULO(current_sector, alignpayload_sec)
+				+ alignoffset_sec;
+	}
+
+	return 0;
+}
+
+static void _remove_keyfiles(void)
+{
+	remove(KEYFILE1);
+	remove(KEYFILE2);
+}
+
+#if HAVE_DECL_DM_TASK_RETRY_REMOVE
+#define DM_RETRY "--retry "
+#else
+#define DM_RETRY ""
+#endif
+
+#define DM_NOSTDERR " 2>/dev/null"
+
+static void _cleanup_dmdevices(void)
+{
+	struct stat st;
+
+	if (!stat(DMDIR H_DEVICE, &st))
+		_system("dmsetup remove " DM_RETRY H_DEVICE DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR H_DEVICE_WRONG, &st))
+		_system("dmsetup remove " DM_RETRY H_DEVICE_WRONG DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_0S, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_0S DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_1S, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_1S DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_WRONG, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_WRONG DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR L_DEVICE_OK, &st))
+		_system("dmsetup remove " DM_RETRY L_DEVICE_OK DM_NOSTDERR, 0);
+
+	t_dev_offset = 0;
+}
+
+static void _cleanup(void)
+{
+	struct stat st;
+
+	CRYPT_FREE(cd);
+	CRYPT_FREE(cd2);
+
+	//_system("udevadm settle", 0);
+
+	if (!stat(DMDIR CDEVICE_1, &st))
+		_system("dmsetup remove " DM_RETRY CDEVICE_1 DM_NOSTDERR, 0);
+
+	if (!stat(DMDIR CDEVICE_2, &st))
+		_system("dmsetup remove " DM_RETRY CDEVICE_2 DM_NOSTDERR, 0);
+
+	if (!stat(DEVICE_EMPTY, &st))
+		_system("dmsetup remove " DM_RETRY DEVICE_EMPTY_name DM_NOSTDERR, 0);
+
+	if (!stat(DEVICE_ERROR, &st))
+		_system("dmsetup remove " DM_RETRY DEVICE_ERROR_name DM_NOSTDERR, 0);
+
+	_cleanup_dmdevices();
+
+	if (loop_device(THE_LOOP_DEV))
+		loop_detach(THE_LOOP_DEV);
+
+	if (loop_device(DEVICE_1))
+		loop_detach(DEVICE_1);
+
+	if (loop_device(DEVICE_2))
+		loop_detach(DEVICE_2);
+
+	if (loop_device(DEVICE_3))
+		loop_detach(DEVICE_3);
+
+	_system("rm -f " IMAGE_EMPTY, 0);
+	_system("rm -f " IMAGE1, 0);
+
+	if (test_loop_file)
+		remove(test_loop_file);
+	if (tmp_file_1)
+		remove(tmp_file_1);
+
+	remove(EVL_HEADER_1);
+	remove(EVL_HEADER_2);
+	remove(EVL_HEADER_3);
+	remove(EVL_HEADER_4);
+	remove(EVL_HEADER_5);
+	remove(VALID_HEADER);
+	remove(BACKUP_FILE);
+
+	_remove_keyfiles();
+
+	free(tmp_file_1);
+	free(test_loop_file);
+	free(THE_LOOP_DEV);
+	free(DEVICE_1);
+	free(DEVICE_2);
+	free(DEVICE_3);
+}
+
+static int _setup(void)
+{
+	int fd, ro = 0;
+	char cmd[128];
+
+	test_loop_file = strdup(THE_LFILE_TEMPLATE);
+	if (!test_loop_file)
+		return 1;
+
+	if ((fd=mkstemp(test_loop_file)) == -1) {
+		printf("cannot create temporary file with template %s\n", test_loop_file);
+		return 1;
+	}
+	close(fd);
+	snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
+		 test_loop_file, TST_SECTOR_SIZE, TST_LOOP_FILE_SIZE);
+	if (_system(cmd, 1))
+		return 1;
+
+	fd = loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
+	close(fd);
+
+	tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
+	if (!tmp_file_1)
+		return 1;
+
+	if ((fd=mkstemp(tmp_file_1)) == -1) {
+		printf("cannot create temporary file with template %s\n", tmp_file_1);
+		return 1;
+	}
+	close(fd);
+	snprintf(cmd, sizeof(cmd), "dd if=/dev/zero of=%s bs=%d count=%d 2>/dev/null",
+		 tmp_file_1, TST_SECTOR_SIZE, 10);
+	if (_system(cmd, 1))
+		return 1;
+
+	_system("dmsetup create " DEVICE_EMPTY_name " --table \"0 10000 zero\"", 1);
+	_system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
+
+	_system(" [ ! -e " IMAGE1 " ] && xz -dk " IMAGE1 ".xz", 1);
+	fd = loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
+	close(fd);
+
+	_system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=10 2>/dev/null", 1);
+	fd = loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
+	close(fd);
+
+	/* Keymaterial offset is less than 8 sectors */
+	_system(" [ ! -e " EVL_HEADER_1 " ] && xz -dk " EVL_HEADER_1 ".xz", 1);
+	/* keymaterial offset aims into payload area */
+	_system(" [ ! -e " EVL_HEADER_2 " ] && xz -dk " EVL_HEADER_2 ".xz", 1);
+	/* keymaterial offset is valid, number of stripes causes payload area to be overwritten */
+	_system(" [ ! -e " EVL_HEADER_3 " ] && xz -dk " EVL_HEADER_3 ".xz", 1);
+	/* luks device header for data and header on same device. payloadOffset is greater than
+	 * device size (crypt_load() test) */
+	_system(" [ ! -e " EVL_HEADER_4 " ] && xz -dk " EVL_HEADER_4 ".xz", 1);
+	 /* two keyslots with same offset (overlapping keyslots) */
+	_system(" [ ! -e " EVL_HEADER_5 " ] && xz -dk " EVL_HEADER_5 ".xz", 1);
+	/* valid header: payloadOffset=4096, key_size=32,
+	 * volume_key = bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a */
+	_system(" [ ! -e " VALID_HEADER " ] && xz -dk " VALID_HEADER ".xz", 1);
+
+	/* Prepare tcrypt images */
+	_system("tar xJf tcrypt-images.tar.xz 2>/dev/null", 1);
+
+	_system("modprobe dm-crypt >/dev/null 2>&1", 0);
+	_system("modprobe dm-verity >/dev/null 2>&1", 0);
+
+	_fips_mode = fips_mode();
+	if (_debug)
+		printf("FIPS MODE: %d\n", _fips_mode);
+
+	/* Use default log callback */
+	crypt_set_log_callback(NULL, &global_log_callback, NULL);
+
+	return 0;
+}
+
+static void AddDevicePlain(void)
+{
+	struct crypt_params_plain params = {
+		.hash = "sm3",
+		.skip = 0,
+		.offset = 0,
+		.size = 0
+	};
+	int fd;
+	char key[128], key2[128], path[128];
+
+	const char *passphrase = PASSPHRASE;
+	// hashed hex version of PASSPHRASE
+	const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
+	size_t key_size = strlen(mk_hex) / 2;
+	const char *cipher = "sm4";
+	const char *cipher_mode = "xts-plain64";
+
+	uint64_t size, r_size;
+
+	crypt_decode_key(key, mk_hex, key_size);
+	FAIL_(crypt_init(&cd, ""), "empty device string");
+	FAIL_(crypt_init(&cd, DEVICE_WRONG), "nonexistent device name ");
+	FAIL_(crypt_init(&cd, DEVICE_CHAR), "character device as backing device");
+	OK_(crypt_init(&cd, tmp_file_1));
+	CRYPT_FREE(cd);
+
+	// test crypt_format, crypt_get_cipher, crypt_get_cipher_mode, crypt_get_volume_key_size
+	OK_(crypt_init(&cd,DEVICE_1));
+	params.skip = 3;
+	params.offset = 42;
+	FAIL_(crypt_format(cd,CRYPT_PLAIN,NULL,cipher_mode,NULL,NULL,key_size,&params),"cipher param is null");
+	FAIL_(crypt_format(cd,CRYPT_PLAIN,cipher,NULL,NULL,NULL,key_size,&params),"cipher_mode param is null");
+	OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
+	OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
+	OK_(strcmp(cipher,crypt_get_cipher(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	EQ_(params.skip, crypt_get_iv_offset(cd));
+	EQ_(params.offset, crypt_get_data_offset(cd));
+	params.skip = 0;
+	params.offset = 0;
+
+	// crypt_set_uuid()
+	FAIL_(crypt_set_uuid(cd,DEVICE_1_UUID),"can't set uuid to plain device");
+
+	CRYPT_FREE(cd);
+
+	// default is "plain" hash - no password hash
+	OK_(crypt_init(&cd, DEVICE_1));
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, NULL));
+	FAIL_(crypt_activate_by_volume_key(cd, NULL, key, key_size, 0), "cannot verify key with plain");
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	// test boundaries in offset parameter
+	t_device_size(DEVICE_1,&size);
+	params.hash = NULL;
+	// zero sectors length
+	params.offset = size >> TST_SECTOR_SHIFT;
+	OK_(crypt_init(&cd, DEVICE_1));
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	EQ_(crypt_get_data_offset(cd),params.offset);
+	// device size is 0 sectors
+	FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0), "invalid device size (0 blocks)");
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	// data part of crypt device is of 1 sector size
+	params.offset = (size >> TST_SECTOR_SHIFT) - 1;
+	CRYPT_FREE(cd);
+
+	OK_(crypt_init(&cd, DEVICE_1));
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
+	if (t_device_size(path, &r_size) >= 0)
+		EQ_(r_size >> TST_SECTOR_SHIFT, 1);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	// size > device_size
+	params.offset = 0;
+	params.size = (size >> TST_SECTOR_SHIFT) + 1;
+	crypt_init(&cd, DEVICE_1);
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	CRYPT_FREE(cd);
+
+	// offset == device_size (autodetect size)
+	params.offset = (size >> TST_SECTOR_SHIFT);
+	params.size = 0;
+	crypt_init(&cd, DEVICE_1);
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	CRYPT_FREE(cd);
+
+	// offset == device_size (user defined size)
+	params.offset = (size >> TST_SECTOR_SHIFT);
+	params.size = 123;
+	crypt_init(&cd, DEVICE_1);
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Device too small");
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	CRYPT_FREE(cd);
+
+	// offset+size > device_size
+	params.offset = 42;
+	params.size = (size >> TST_SECTOR_SHIFT) - params.offset + 1;
+	crypt_init(&cd, DEVICE_1);
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	FAIL_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),"Offset and size are beyond device real size");
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	CRYPT_FREE(cd);
+
+	// offset+size == device_size
+	params.offset = 42;
+	params.size = (size >> TST_SECTOR_SHIFT) - params.offset;
+	crypt_init(&cd, DEVICE_1);
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	if (!t_device_size(path, &r_size))
+		EQ_((r_size >> TST_SECTOR_SHIFT),params.size);
+	OK_(crypt_deactivate(cd,CDEVICE_1));
+
+	CRYPT_FREE(cd);
+	params.hash = "sm3";
+	params.offset = 0;
+	params.size = 0;
+	params.skip = 0;
+
+	// Now use hashed password
+	OK_(crypt_init(&cd, DEVICE_1));
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	FAIL_(crypt_activate_by_passphrase(cd, NULL, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0),
+	      "cannot verify passphrase with plain" );
+	OK_(crypt_activate_by_passphrase(cd, CDEVICE_1, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
+
+	// device status check
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	snprintf(path, sizeof(path), "%s/%s", crypt_get_dir(), CDEVICE_1);
+	fd = open(path, O_RDONLY);
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_BUSY);
+	FAIL_(crypt_deactivate(cd, CDEVICE_1), "Device is busy");
+	close(fd);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	CRYPT_FREE(cd);
+
+	// crypt_init_by_name_and_header
+	OK_(crypt_init(&cd,DEVICE_1));
+	OK_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	CRYPT_FREE(cd);
+
+	// init with detached header is not supported
+	OK_(crypt_init_data_device(&cd, DEVICE_2, DEVICE_1));
+	FAIL_(crypt_format(cd, CRYPT_PLAIN, cipher, cipher_mode, NULL, NULL, key_size, &params),
+	      "can't use plain with separate metadata device");
+	CRYPT_FREE(cd);
+
+	FAIL_(crypt_init_by_name_and_header(&cd, CDEVICE_1, H_DEVICE),"can't init plain device by header device");
+	OK_(crypt_init_by_name(&cd, CDEVICE_1));
+	OK_(strcmp(cipher_mode,crypt_get_cipher_mode(cd)));
+	OK_(strcmp(cipher,crypt_get_cipher(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	EQ_(params.skip, crypt_get_iv_offset(cd));
+	EQ_(params.offset, crypt_get_data_offset(cd));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	OK_(crypt_init(&cd,DEVICE_1));
+	OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
+	params.size = 0;
+	params.offset = 0;
+
+	// crypt_set_data_device
+	FAIL_(crypt_set_data_device(cd,H_DEVICE),"can't set data device for plain device");
+	NULL_(crypt_get_metadata_device_name(cd));
+	FAIL_(crypt_header_is_detached(cd), "plain has no header");
+
+	// crypt_get_type
+	OK_(strcmp(crypt_get_type(cd),CRYPT_PLAIN));
+
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+
+	// crypt_resize()
+	OK_(crypt_resize(cd, CDEVICE_1, size >> TST_SECTOR_SHIFT)); // same size
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size, size);
+
+	// size overlaps
+	FAIL_(crypt_resize(cd, CDEVICE_1, (uint64_t)-1),"Backing device is too small");
+	FAIL_(crypt_resize(cd, CDEVICE_1, (size >> TST_SECTOR_SHIFT) + 1),"crypt device overlaps backing device");
+
+	// resize ok
+	OK_(crypt_resize(cd,CDEVICE_1, 123));
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size >> TST_SECTOR_SHIFT, 123);
+	OK_(crypt_resize(cd,CDEVICE_1,0)); // full size (autodetect)
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size, size);
+	OK_(crypt_deactivate(cd,CDEVICE_1));
+	EQ_(crypt_status(cd,CDEVICE_1),CRYPT_INACTIVE);
+	CRYPT_FREE(cd);
+
+	// offset tests
+	OK_(crypt_init(&cd,DEVICE_1));
+	params.offset = 42;
+	params.size = (size >> TST_SECTOR_SHIFT) - params.offset - 10;
+	OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
+	OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size >> TST_SECTOR_SHIFT, params.size);
+	// resize to fill remaining capacity
+	OK_(crypt_resize(cd,CDEVICE_1,params.size + 10));
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size >> TST_SECTOR_SHIFT, params.size + 10);
+
+	// 1 sector beyond real size
+	FAIL_(crypt_resize(cd,CDEVICE_1,params.size + 11), "new device size overlaps backing device"); // with respect to offset
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size >> TST_SECTOR_SHIFT, params.size + 10);
+	GE_(crypt_status(cd,CDEVICE_1),CRYPT_ACTIVE);
+	fd = open(path, O_RDONLY);
+	NOTFAIL_(fd, "Bad loop device.");
+	close(fd);
+
+	// resize to minimal size
+	OK_(crypt_resize(cd,CDEVICE_1, 1)); // minimal device size
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size >> TST_SECTOR_SHIFT, 1);
+	// use size of backing device (autodetect with respect to offset)
+	OK_(crypt_resize(cd,CDEVICE_1,0));
+	if (!t_device_size(path,&r_size))
+		EQ_(r_size >> TST_SECTOR_SHIFT, (size >> TST_SECTOR_SHIFT)- 42);
+	OK_(crypt_deactivate(cd,CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	params.size = 0;
+	params.offset = 0;
+	OK_(crypt_init(&cd,DEVICE_1));
+	OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
+	OK_(crypt_activate_by_volume_key(cd,CDEVICE_1,key,key_size,0));
+
+	// suspend/resume tests
+	FAIL_(crypt_suspend(cd,CDEVICE_1),"cannot suspend plain device");
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	FAIL_(crypt_resume_by_passphrase(cd,CDEVICE_1,CRYPT_ANY_SLOT,passphrase, strlen(passphrase)),"cannot resume plain device");
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+
+	// retrieve volume key check
+	memset(key2, 0, key_size);
+	key_size--;
+	// small buffer
+	FAIL_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)), "small buffer");
+	key_size++;
+	OK_(crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
+	memcpy(key, key2, key_size);
+	OK_(memcmp(key, key2, key_size));
+
+	OK_(strcmp(cipher, crypt_get_cipher(cd)));
+	OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	EQ_(0, crypt_get_data_offset(cd));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+
+	// now with keyfile
+	OK_(prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
+	OK_(prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
+	FAIL_(crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 0), "cannot verify key with plain");
+	EQ_(0, crypt_activate_by_keyfile(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	FAIL_(crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
+	FAIL_(crypt_activate_by_keyfile_device_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, strlen(KEY1) + 1, 0), "cannot seek");
+	EQ_(0, crypt_activate_by_keyfile_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	EQ_(0, crypt_activate_by_keyfile_device_offset(cd, CDEVICE_1, CRYPT_ANY_SLOT, KEYFILE1, 0, 0, 0));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	_remove_keyfiles();
+	CRYPT_FREE(cd);
+
+	OK_(crypt_init(&cd,DEVICE_1));
+	OK_(crypt_format(cd,CRYPT_PLAIN,cipher,cipher_mode,NULL,NULL,key_size,&params));
+
+	// crypt_keyslot_*()
+	FAIL_(crypt_keyslot_add_by_passphrase(cd,CRYPT_ANY_SLOT,passphrase,strlen(passphrase),passphrase,strlen(passphrase)), "can't add keyslot to plain device");
+	FAIL_(crypt_keyslot_add_by_volume_key(cd,CRYPT_ANY_SLOT	,key,key_size,passphrase,strlen(passphrase)),"can't add keyslot to plain device");
+	FAIL_(crypt_keyslot_add_by_keyfile(cd,CRYPT_ANY_SLOT,KEYFILE1,strlen(KEY1),KEYFILE2,strlen(KEY2)),"can't add keyslot to plain device");
+	FAIL_(crypt_keyslot_destroy(cd,1),"can't manipulate keyslots on plain device");
+	EQ_(crypt_keyslot_status(cd, 0), CRYPT_SLOT_INVALID);
+	_remove_keyfiles();
+
+	CRYPT_FREE(cd);
+}
+
+static void AddDeviceLuks(void)
+{
+	enum { OFFSET_1M = 2048 , OFFSET_2M = 4096, OFFSET_4M = 8192, OFFSET_8M = 16384 };
+	struct crypt_params_luks1 params = {
+		.hash = "sm3",
+		.data_alignment = OFFSET_1M, // 4M, data offset will be 4096
+		.data_device = DEVICE_2
+	};
+	char key[128], key2[128], key3[128];
+
+	const char *passphrase = "blabla", *passphrase2 = "nsdkFI&Y#.sd";
+	const char *mk_hex = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1a";
+	const char *mk_hex2 = "bb21158c733229347bd4e681891e213d94c685be6a5b84818afe7a78a6de7a1e";
+	size_t key_size = strlen(mk_hex) / 2;
+	const char *cipher = "sm4";
+	const char *cipher_mode = "xts-plain64";
+	uint64_t r_payload_offset, r_header_size, r_size_1;
+	struct crypt_pbkdf_type pbkdf;
+
+	crypt_decode_key(key, mk_hex, key_size);
+	crypt_decode_key(key3, mk_hex2, key_size);
+
+	// init test devices
+	OK_(get_luks_offsets(1, key_size, 0, 0, &r_header_size, &r_payload_offset));
+	OK_(create_dmdevice_over_loop(H_DEVICE, r_header_size));
+	OK_(create_dmdevice_over_loop(H_DEVICE_WRONG, r_header_size - 1));
+
+	// format
+	OK_(crypt_init(&cd, DMDIR H_DEVICE_WRONG));
+	params.data_alignment = 0;
+	FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Not enough space for keyslots material");
+	CRYPT_FREE(cd);
+
+	// test payload_offset = 0 for encrypted device with external header device
+	OK_(crypt_init(&cd, DMDIR H_DEVICE));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), 0);
+	CRYPT_FREE(cd);
+
+	params.data_alignment = 0;
+	params.data_device = NULL;
+
+	// test payload_offset = 0. format() should look up alignment offset from device topology
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	OK_(!(crypt_get_data_offset(cd) > 0));
+	CRYPT_FREE(cd);
+
+	// set_data_offset has priority, alignment must be 0 or must be compatible
+	params.data_alignment = 0;
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(crypt_set_data_offset(cd, OFFSET_8M));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), OFFSET_8M);
+	CRYPT_FREE(cd);
+
+	// Load gets the value from metadata
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(crypt_set_data_offset(cd, OFFSET_2M));
+	OK_(crypt_load(cd, CRYPT_LUKS1, NULL));
+	EQ_(crypt_get_data_offset(cd), OFFSET_8M);
+	CRYPT_FREE(cd);
+
+	params.data_alignment = OFFSET_4M;
+	OK_(crypt_init(&cd, DEVICE_2));
+	FAIL_(crypt_set_data_offset(cd, OFFSET_2M + 1), "Not aligned to 4096"); // must be aligned to 4k
+	OK_(crypt_set_data_offset(cd, OFFSET_2M));
+	FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Alignment not compatible");
+	OK_(crypt_set_data_offset(cd, OFFSET_4M));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), OFFSET_4M);
+	CRYPT_FREE(cd);
+
+	/*
+	 * test limit values for backing device size
+	 */
+	params.data_alignment = OFFSET_2M;
+	OK_(get_luks_offsets(0, key_size, params.data_alignment, 0, NULL, &r_payload_offset));
+	OK_(create_dmdevice_over_loop(L_DEVICE_0S, r_payload_offset));
+	OK_(create_dmdevice_over_loop(L_DEVICE_1S, r_payload_offset + 1));
+	//OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, r_payload_offset - 1));
+	OK_(create_dmdevice_over_loop(L_DEVICE_WRONG, 2050 - 1)); //FIXME last keyslot - 1 sector
+
+	// 1 sector less than required
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_WRONG));
+	FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params),	"Device too small");
+	CRYPT_FREE(cd);
+
+	// 0 sectors for encrypted area
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_0S));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Encrypted area too small");
+	CRYPT_FREE(cd);
+
+	// 1 sector for encrypted area
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_get_data_offset(cd), params.data_alignment);
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	OK_(t_device_size(DMDIR CDEVICE_1, &r_size_1));
+	EQ_(r_size_1, TST_SECTOR_SIZE);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	EQ_(crypt_status(cd, CDEVICE_1), CRYPT_INACTIVE);
+	// restrict format only to empty context
+	FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Context is already formatted");
+	FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, NULL), "Context is already formatted");
+	// change data device to wrong one
+	OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_0S));
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device too small");
+	OK_(crypt_set_data_device(cd, DMDIR L_DEVICE_1S));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	params.data_alignment = 0;
+	params.data_device = DEVICE_2;
+
+	// generate keyslot material at the end of luks header
+	OK_(crypt_init(&cd, DMDIR H_DEVICE));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	EQ_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), 7);
+	EQ_(crypt_activate_by_passphrase(cd, CDEVICE_1, 7, passphrase, strlen(passphrase) ,0), 7);
+	CRYPT_FREE(cd);
+	OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
+	FAIL_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params), "Context is already formatted");
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	CRYPT_FREE(cd);
+	// check active status without header
+	OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, NULL));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	NULL_(crypt_get_type(cd));
+	OK_(strcmp(cipher, crypt_get_cipher(cd)));
+	OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	CRYPT_FREE(cd);
+
+	params.data_alignment = OFFSET_1M;
+	params.data_device = NULL;
+
+	// test uuid mismatch and _init_by_name_and_header
+	OK_(crypt_init(&cd, DMDIR L_DEVICE_1S));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0));
+	EQ_(0, crypt_header_is_detached(cd));
+	CRYPT_FREE(cd);
+	params.data_alignment = 0;
+	params.data_device = DEVICE_2;
+	OK_(crypt_init(&cd, DMDIR H_DEVICE));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	CRYPT_FREE(cd);
+	// there we've got uuid mismatch
+	OK_(crypt_init_by_name_and_header(&cd, CDEVICE_1, DMDIR H_DEVICE));
+	GE_(crypt_status(cd, CDEVICE_1), CRYPT_ACTIVE);
+	NULL_(crypt_get_type(cd));
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_1, key, key_size, 0), "Device is active");
+	FAIL_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0), "Device is active");
+	EQ_(crypt_status(cd, CDEVICE_2), CRYPT_INACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_1));
+	FAIL_(crypt_header_is_detached(cd), "no header for mismatched device");
+	CRYPT_FREE(cd);
+
+	params.data_device = NULL;
+
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+
+	// even with no keyslots defined it can be activated by volume key
+	OK_(crypt_volume_key_verify(cd, key, key_size));
+	OK_(crypt_activate_by_volume_key(cd, CDEVICE_2, key, key_size, 0));
+	GE_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_2));
+
+	// now with keyslot
+	EQ_(7, crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
+	EQ_(7, crypt_activate_by_passphrase(cd, CDEVICE_2, CRYPT_ANY_SLOT, passphrase, strlen(passphrase), 0));
+	GE_(crypt_status(cd, CDEVICE_2), CRYPT_ACTIVE);
+	OK_(crypt_deactivate(cd, CDEVICE_2));
+
+	crypt_set_iteration_time(cd, 1);
+	EQ_(1, crypt_keyslot_add_by_volume_key(cd, 1, key, key_size, KEY1, strlen(KEY1)));
+
+	// PBKDF info (in LUKS1 slots are the same)
+	FAIL_(crypt_keyslot_get_pbkdf(cd, 1, NULL), "PBKDF struct required");
+	OK_(crypt_keyslot_get_pbkdf(cd, 1, &pbkdf));
+	OK_(strcmp(pbkdf.type, CRYPT_KDF_PBKDF2));
+	OK_(strcmp(pbkdf.hash, params.hash));
+	OK_(pbkdf.iterations < 1000); /* set by minimum iterations above */
+	EQ_(0, pbkdf.max_memory_kb);
+	EQ_(0, pbkdf.parallel_threads);
+	FAIL_(crypt_keyslot_get_pbkdf(cd, 2, &pbkdf), "Keyslot 2 is inactive.");
+
+	OK_(prepare_keyfile(KEYFILE1, KEY1, strlen(KEY1)));
+	OK_(prepare_keyfile(KEYFILE2, KEY2, strlen(KEY2)));
+	EQ_(2, crypt_keyslot_add_by_keyfile(cd, 2, KEYFILE1, 0, KEYFILE2, 0));
+	FAIL_(crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 1, KEYFILE2, 0, 1), "wrong key");
+	EQ_(3, crypt_keyslot_add_by_keyfile_offset(cd, 3, KEYFILE1, 0, 0, KEYFILE2, 0, 1));
+	EQ_(4, crypt_keyslot_add_by_keyfile_offset(cd, 4, KEYFILE2, 0, 1, KEYFILE1, 0, 1));
+	FAIL_(crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2)-1, 0), "key mismatch");
+	EQ_(2, crypt_activate_by_keyfile(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
+	EQ_(3, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE2, 0, 1, 0));
+	EQ_(4, crypt_activate_by_keyfile_offset(cd, NULL, CRYPT_ANY_SLOT, KEYFILE1, 0, 1, 0));
+	FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, strlen(KEY2), 2, 0), "not enough data");
+	FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, strlen(KEY2) + 1, 0), "cannot seek");
+	FAIL_(crypt_activate_by_keyfile_offset(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 2, 0), "wrong key");
+	EQ_(2, crypt_activate_by_keyfile(cd, CDEVICE_2, CRYPT_ANY_SLOT, KEYFILE2, 0, 0));
+	OK_(crypt_keyslot_destroy(cd, 1));
+	OK_(crypt_keyslot_destroy(cd, 2));
+	OK_(crypt_keyslot_destroy(cd, 3));
+	OK_(crypt_keyslot_destroy(cd, 4));
+	OK_(crypt_deactivate(cd, CDEVICE_2));
+	_remove_keyfiles();
+
+	FAIL_(crypt_keyslot_add_by_volume_key(cd, 7, key, key_size, passphrase, strlen(passphrase)), "slot used");
+	key[1] = ~key[1];
+	FAIL_(crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)), "key mismatch");
+	key[1] = ~key[1];
+	EQ_(6, crypt_keyslot_add_by_volume_key(cd, 6, key, key_size, passphrase, strlen(passphrase)));
+	EQ_(CRYPT_SLOT_ACTIVE, crypt_keyslot_status(cd, 6));
+
+	FAIL_(crypt_keyslot_destroy(cd, 8), "invalid keyslot");
+	FAIL_(crypt_keyslot_destroy(cd, CRYPT_ANY_SLOT), "invalid keyslot");
+	FAIL_(crypt_keyslot_destroy(cd, 0), "keyslot not used");
+	OK_(crypt_keyslot_destroy(cd, 7));
+	EQ_(CRYPT_SLOT_INACTIVE, crypt_keyslot_status(cd, 7));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 6));
+
+	EQ_(7, crypt_keyslot_change_by_passphrase(cd, 6, 7, passphrase, strlen(passphrase), passphrase2, strlen(passphrase2)));
+	EQ_(CRYPT_SLOT_ACTIVE_LAST, crypt_keyslot_status(cd, 7));
+	EQ_(7, crypt_activate_by_passphrase(cd, NULL, 7, passphrase2, strlen(passphrase2), 0));
+	EQ_(6, crypt_keyslot_change_by_passphrase(cd, CRYPT_ANY_SLOT, 6, passphrase2, strlen(passphrase2), passphrase, strlen(passphrase)));
+
+	EQ_(6, crypt_volume_key_get(cd, CRYPT_ANY_SLOT, key2, &key_size, passphrase, strlen(passphrase)));
+	OK_(crypt_volume_key_verify(cd, key2, key_size));
+
+	OK_(memcmp(key, key2, key_size));
+
+	OK_(strcmp(cipher, crypt_get_cipher(cd)));
+	OK_(strcmp(cipher_mode, crypt_get_cipher_mode(cd)));
+	EQ_((int)key_size, crypt_get_volume_key_size(cd));
+	EQ_(OFFSET_2M, crypt_get_data_offset(cd));
+	OK_(strcmp(DEVICE_2, crypt_get_device_name(cd)));
+
+	reset_log();
+	OK_(crypt_dump(cd));
+	OK_(!(global_lines != 0));
+	reset_log();
+	FAIL_(crypt_dump_json(cd, NULL, 0), "LUKS1 not supported");
+
+	FAIL_(crypt_set_uuid(cd, "blah"), "wrong UUID format");
+	OK_(crypt_set_uuid(cd, DEVICE_TEST_UUID));
+	OK_(strcmp(DEVICE_TEST_UUID, crypt_get_uuid(cd)));
+
+	FAIL_(crypt_deactivate(cd, CDEVICE_2), "not active");
+	CRYPT_FREE(cd);
+
+	// No benchmark PBKDF2
+	pbkdf.flags = CRYPT_PBKDF_NO_BENCHMARK;
+	pbkdf.hash = "sm3";
+	pbkdf.iterations = 1000;
+	pbkdf.time_ms = 0;
+
+	OK_(crypt_init(&cd, DEVICE_2));
+	OK_(crypt_set_pbkdf_type(cd, &pbkdf));
+	OK_(crypt_format(cd, CRYPT_LUKS1, cipher, cipher_mode, NULL, key, key_size, &params));
+	CRYPT_FREE(cd);
+
+	_cleanup_dmdevices();
+}
+
+
+static void int_handler(int sig __attribute__((__unused__)))
+{
+	_quit++;
+}
+
+int main(int argc, char *argv[])
+{
+	struct sigaction sa = { .sa_handler = int_handler };
+	int i;
+
+	if (getuid() != 0) {
+		printf("You must be root to run this test.\n");
+		exit(77);
+	}
+#ifndef NO_CRYPTSETUP_PATH
+	if (getenv("CRYPTSETUP_PATH")) {
+		printf("Cannot run this test with CRYPTSETUP_PATH set.\n");
+		exit(77);
+	}
+#endif
+	for (i = 1; i < argc; i++) {
+		if (!strcmp("-v", argv[i]) || !strcmp("--verbose", argv[i]))
+			_verbose = 1;
+		else if (!strcmp("--debug", argv[i]))
+			_debug = _verbose = 1;
+	}
+
+	/* Handle interrupt properly */
+	sigaction(SIGINT, &sa, NULL);
+	sigaction(SIGTERM, &sa, NULL);
+
+	register_cleanup(_cleanup);
+
+	_cleanup();
+	if (_setup()) {
+		printf("Cannot set test devices.\n");
+		_cleanup();
+		exit(77);
+	}
+
+	crypt_set_debug_level(_debug ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
+
+	RUN_(AddDevicePlain, "A plain device API creation");
+	RUN_(AddDeviceLuks, "Format and use LUKS device");
+
+	_cleanup();
+	return 0;
+}
-- 
2.27.0

