#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/types.h>
static int format_and_add_keyslots(const char *path)
{
struct crypt_device *cd;
int r;
if (r < 0) {
printf("crypt_init() failed for %s.\n", path);
return r;
}
printf("Device %s will be formatted as a LUKS device after 5 seconds.\n"
"Press CTRL+C now if you want to cancel this operation.\n", path);
sleep(5);
"aes",
"xts-plain64",
NULL,
NULL,
512 / 8,
NULL);
if (r < 0) {
return r;
}
NULL,
0,
"foo",
3);
if (r < 0) {
printf("Adding keyslot failed.\n");
return r;
}
printf("The first keyslot is initialized.\n");
"foo", 3,
"bar", 3);
if (r < 0) {
printf("Adding keyslot failed.\n");
return r;
}
printf("The second keyslot is initialized.\n");
return 0;
}
static int activate_and_check_status(const char *path, const char *device_name)
{
struct crypt_device *cd;
int r;
if (r < 0) {
printf("crypt_init() failed for %s.\n", path);
return r;
}
NULL);
if (r < 0) {
return r;
}
device_name,
"foo", 3,
if (r < 0) {
printf("Device %s activation failed.\n", device_name);
return r;
}
if (r < 0) {
printf("Get info about active device %s failed.\n", device_name);
return r;
}
printf("Active device parameters for %s:\n"
"\tDevice offset (in sectors): %" PRIu64 "\n"
"\tIV offset (in sectors) : %" PRIu64 "\n"
"\tdevice size (in sectors) : %" PRIu64 "\n"
"\tread-only flag : %s\n",
device_name, cad.offset, cad.iv_offset, cad.size,
return 0;
}
static int handle_active_device(const char *device_name)
{
struct crypt_device *cd;
int r;
if (r < 0) {
printf("crypt_init_by_name() failed for %s.\n", device_name);
return r;
}
printf("Device %s is still active.\n", device_name);
else {
printf("Something failed perhaps, device %s is not active.\n", device_name);
return -1;
}
if (r < 0) {
printf("crypt_deactivate() failed.\n");
return r;
}
printf("Device %s is now deactivated.\n", device_name);
return 0;
}
int main(int argc, char **argv)
{
if (geteuid()) {
printf("Using of libcryptsetup requires super user privileges.\n");
return 1;
}
if (argc != 2) {
printf("usage: ./crypt_luks_usage <path>\n"
"<path> refers to either a regular file or a block device.\n"
" WARNING: the file or device will be wiped.\n");
return 2;
}
if (format_and_add_keyslots(argv[1]))
return 3;
if (activate_and_check_status(argv[1], "example_device"))
return 4;
if (handle_active_device("example_device"))
return 5;
return 0;
}
int crypt_format(struct crypt_device *cd, const char *type, const char *cipher, const char *cipher_mode, const char *uuid, const char *volume_key, size_t volume_key_size, void *params)
int crypt_load(struct crypt_device *cd, const char *requested_type, void *params)
int crypt_activate_by_passphrase(struct crypt_device *cd, const char *name, int keyslot, const char *passphrase, size_t passphrase_size, uint32_t flags)
int crypt_deactivate(struct crypt_device *cd, const char *name)
#define CRYPT_ACTIVATE_READONLY
Definition libcryptsetup.h:1465
int crypt_get_active_device(struct crypt_device *cd, const char *name, struct crypt_active_device *cad)
const char * crypt_get_cipher_mode(struct crypt_device *cd)
const char * crypt_get_device_name(struct crypt_device *cd)
crypt_status_info crypt_status(struct crypt_device *cd, const char *name)
const char * crypt_get_uuid(struct crypt_device *cd)
const char * crypt_get_cipher(struct crypt_device *cd)
@ CRYPT_ACTIVE
Definition libcryptsetup.h:1923
int crypt_init_by_name(struct crypt_device **cd, const char *name)
void crypt_free(struct crypt_device *cd)
int crypt_init(struct crypt_device **cd, const char *device)
#define CRYPT_ANY_SLOT
Definition libcryptsetup.h:1038
int crypt_keyslot_add_by_passphrase(struct crypt_device *cd, int keyslot, const char *passphrase, size_t passphrase_size, const char *new_passphrase, size_t new_passphrase_size)
const char * crypt_get_dir(void)
int crypt_keyslot_add_by_volume_key(struct crypt_device *cd, int keyslot, const char *volume_key, size_t volume_key_size, const char *passphrase, size_t passphrase_size)
#define CRYPT_LUKS
Definition libcryptsetup.h:436
const char * crypt_get_type(struct crypt_device *cd)
#define CRYPT_LUKS2
Definition libcryptsetup.h:421
Definition libcryptsetup.h:1524