/*
* cryptsetup-reencrypt - crypt utility for offline re-encryption
*
- * Copyright (C) 2012, Milan Broz All rights reserved.
* Copyright (C) 2012, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2012-2015, Milan Broz All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
+ * 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#define PACKAGE_REENC "crypt_reencrypt"
-
-#define _LARGEFILE64_SOURCE
-#define _FILE_OFFSET_BITS 64
-#define SECTOR_SIZE 512
-#define ROUND_SECTOR(x) (((x) + SECTOR_SIZE - 1) / SECTOR_SIZE)
-#define NO_UUID "cafecafe-cafe-cafe-cafe-cafecafeeeee"
-#define MAX_BCK_SECTORS 8192
-
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <stdarg.h>
-#include <inttypes.h>
-#include <errno.h>
-#include <unistd.h>
-#include <sys/stat.h>
+#include "cryptsetup.h"
#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/fs.h>
#include <arpa/inet.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <popt.h>
-#include "cryptsetup.h"
+#define PACKAGE_REENC "crypt_reencrypt"
+
+#define NO_UUID "cafecafe-cafe-cafe-cafe-cafecafeeeee"
+#define MAX_BCK_SECTORS 8192
-static int opt_verbose = 0;
-static int opt_debug = 0;
static const char *opt_cipher = NULL;
static const char *opt_hash = NULL;
static const char *opt_key_file = NULL;
static long opt_keyfile_size = 0;
static long opt_keyfile_offset = 0;
static int opt_iteration_time = 1000;
-static int opt_batch_mode = 0;
static int opt_version_mode = 0;
static int opt_random = 0;
static int opt_urandom = 0;
static int opt_key_slot = CRYPT_ANY_SLOT;
static int opt_key_size = 0;
static int opt_new = 0;
+static int opt_keep_key = 0;
+static int opt_decrypt = 0;
static const char *opt_reduce_size_str = NULL;
static uint64_t opt_reduce_size = 0;
static const char **action_argv;
-static volatile int quit = 0;
-
#define MAX_SLOT 8
struct reenc_ctx {
char *device;
char *device_uuid;
uint64_t device_size; /* overrided by parameter */
- uint64_t device_size_real;
+ uint64_t device_size_new_real;
+ uint64_t device_size_org_real;
uint64_t device_offset;
uint64_t device_shift;
int in_progress:1;
enum { FORWARD = 0, BACKWARD = 1 } reencrypt_direction;
+ enum { REENCRYPT = 0, ENCRYPT = 1, DECRYPT = 2 } reencrypt_mode;
char header_file_org[PATH_MAX];
char header_file_new[PATH_MAX];
char crypt_path_org[PATH_MAX];
char crypt_path_new[PATH_MAX];
int log_fd;
- char *log_buf;
+ char log_buf[SECTOR_SIZE];
struct {
char *password;
int keyslot;
struct timeval start_time, end_time;
- uint64_t restart_bytes;
+ uint64_t resume_bytes;
};
char MAGIC[] = {'L','U','K','S', 0xba, 0xbe};
CHECK_OPEN,
} header_magic;
-__attribute__((format(printf, 5, 6)))
-static void clogger(struct crypt_device *cd, int level, const char *file,
- int line, const char *format, ...)
-{
- va_list argp;
- char *target = NULL;
-
- va_start(argp, format);
-
- if (vasprintf(&target, format, argp) > 0) {
- if (level >= 0) {
- crypt_log(cd, level, target);
- } else if (opt_debug)
- printf("# %s\n", target);
- }
-
- va_end(argp);
- free(target);
-}
-
-static void _log(int level, const char *msg, void *usrptr __attribute__((unused)))
-{
- switch(level) {
-
- case CRYPT_LOG_NORMAL:
- fputs(msg, stdout);
- break;
- case CRYPT_LOG_VERBOSE:
- if (opt_verbose)
- fputs(msg, stdout);
- break;
- case CRYPT_LOG_ERROR:
- fputs(msg, stderr);
- break;
- case CRYPT_LOG_DEBUG:
- if (opt_debug)
- printf("# %s\n", msg);
- break;
- default:
- fprintf(stderr, "Internal error on logging class for msg: %s", msg);
- break;
- }
-}
-
static void _quiet_log(int level, const char *msg, void *usrptr)
{
if (!opt_debug)
return;
- _log(level, msg, usrptr);
-}
-
-static void int_handler(int sig __attribute__((__unused__)))
-{
- quit++;
-}
-
-static void set_int_block(int block)
-{
- sigset_t signals_open;
-
- sigemptyset(&signals_open);
- sigaddset(&signals_open, SIGINT);
- sigaddset(&signals_open, SIGTERM);
- sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
-}
-
-static void set_int_handler(void)
-{
- struct sigaction sigaction_open;
-
- memset(&sigaction_open, 0, sizeof(struct sigaction));
- sigaction_open.sa_handler = int_handler;
- sigaction(SIGINT, &sigaction_open, 0);
- sigaction(SIGTERM, &sigaction_open, 0);
- set_int_block(0);
+ tool_log(level, msg, usrptr);
}
/* The difference in seconds between two times in "timeval" format. */
return alignment;
}
+static size_t pagesize(void)
+{
+ long r = sysconf(_SC_PAGESIZE);
+ return r < 0 ? 4096 : (size_t)r;
+}
+
/* Depends on the first two fields of LUKS1 header format, magic and version */
static int device_check(struct reenc_ctx *rc, header_magic set_magic)
{
int r, devfd;
ssize_t s;
uint16_t version;
+ size_t buf_size = pagesize();
devfd = open(rc->device, O_RDWR | O_EXCL | O_DIRECT);
if (devfd == -1) {
goto out;
}
- if (posix_memalign((void *)&buf, alignment(devfd), SECTOR_SIZE)) {
+ if (posix_memalign((void *)&buf, alignment(devfd), buf_size)) {
log_err(_("Allocation of aligned memory failed.\n"));
r = -ENOMEM;
goto out;
}
- s = read(devfd, buf, SECTOR_SIZE);
- if (s < 0 || s != SECTOR_SIZE) {
+ s = read(devfd, buf, buf_size);
+ if (s < 0 || s != (ssize_t)buf_size) {
log_err(_("Cannot read device %s.\n"), rc->device);
- close(devfd);
- return -EIO;
+ r = -EIO;
+ goto out;
}
/* Be sure that we do not process new version of header */
if (!r) {
if (lseek(devfd, 0, SEEK_SET) == -1)
goto out;
- s = write(devfd, buf, SECTOR_SIZE);
- if (s < 0 || s != SECTOR_SIZE) {
+ s = write(devfd, buf, buf_size);
+ if (s < 0 || s != (ssize_t)buf_size) {
log_err(_("Cannot write device %s.\n"), rc->device);
r = -EIO;
}
log_dbg("LUKS signature check failed for %s.", rc->device);
out:
if (buf)
- memset(buf, 0, SECTOR_SIZE);
+ memset(buf, 0, buf_size);
free(buf);
close(devfd);
return r;
memset(rc->log_buf, 0, SECTOR_SIZE);
snprintf(rc->log_buf, SECTOR_SIZE, "# LUKS reencryption log, DO NOT EDIT OR DELETE.\n"
- "version = %d\nUUID = %s\ndirection = %d\n"
+ "version = %d\nUUID = %s\ndirection = %d\nmode = %d\n"
"offset = %" PRIu64 "\nshift = %" PRIu64 "\n# EOF\n",
- 1, rc->device_uuid, rc->reencrypt_direction,
+ 2, rc->device_uuid, rc->reencrypt_direction, rc->reencrypt_mode,
rc->device_offset, rc->device_shift);
- lseek(rc->log_fd, 0, SEEK_SET);
+ if (lseek(rc->log_fd, 0, SEEK_SET) == -1)
+ return -EIO;
+
r = write(rc->log_fd, rc->log_buf, SECTOR_SIZE);
if (r < 0 || r != SECTOR_SIZE) {
log_err(_("Cannot write reencryption log file.\n"));
return 0;
if (sscanf(line, "version = %d", &i) == 1) {
- if (i != 1) {
+ if (i < 1 || i > 2) {
log_dbg("Log: Unexpected version = %i", i);
return -EINVAL;
}
} else if (sscanf(line, "shift = %" PRIu64, &u64) == 1) {
log_dbg("Log: shift = %" PRIu64, u64);
rc->device_shift = u64;
+ } else if (sscanf(line, "mode = %d", &i) == 1) { /* added in v2 */
+ log_dbg("Log: mode = %i", i);
+ rc->reencrypt_mode = i;
+ if (rc->reencrypt_mode != REENCRYPT &&
+ rc->reencrypt_mode != ENCRYPT &&
+ rc->reencrypt_mode != DECRYPT)
+ return -EINVAL;
} else
return -EINVAL;
log_dbg("Closing LUKS reencryption log file %s.", rc->log_file);
if (rc->log_fd != -1)
close(rc->log_fd);
- free(rc->log_buf);
- rc->log_buf = NULL;
}
static int open_log(struct reenc_ctx *rc)
{
- int flags, create_new;
- struct stat st;
-
- if (!stat(rc->log_file, &st))
- create_new = 0;
- else if (errno == ENOENT)
- create_new = 1;
- else
- return -EINVAL;
-
- if (create_new) {
- log_dbg("Creating LUKS reencryption log file %s.", rc->log_file);
- flags = opt_directio ? O_RDWR|O_CREAT|O_DIRECT : O_RDWR|O_CREAT;
- rc->log_fd = open(rc->log_file, flags, S_IRUSR|S_IWUSR);
- if (rc->log_fd == -1)
- return -EINVAL;
- } else {
- log_std(_("Log file %s exists, restarting reencryption.\n"), rc->log_file);
- flags = opt_directio ? O_RDWR|O_DIRECT : O_RDWR;
- rc->log_fd = open(rc->log_file, flags);
- if (rc->log_fd == -1)
- return -EINVAL;
+ int flags = opt_fsync ? O_SYNC : 0;
+
+ rc->log_fd = open(rc->log_file, O_RDWR|O_EXCL|O_CREAT|flags, S_IRUSR|S_IWUSR);
+ if (rc->log_fd != -1) {
+ log_dbg("Created LUKS reencryption log file %s.", rc->log_file);
+ } else if (errno == EEXIST) {
+ log_std(_("Log file %s exists, resuming reencryption.\n"), rc->log_file);
+ rc->log_fd = open(rc->log_file, O_RDWR|flags);
rc->in_progress = 1;
}
- if (posix_memalign((void *)&rc->log_buf, alignment(rc->log_fd), SECTOR_SIZE)) {
- log_err(_("Allocation of aligned memory failed.\n"));
- close_log(rc);
- return -ENOMEM;
- }
+ if (rc->log_fd == -1)
+ return -EINVAL;
- if (create_new && write_log(rc) < 0) {
+ if (!rc->in_progress && write_log(rc) < 0) {
close_log(rc);
return -EIO;
}
(r = crypt_set_data_device(cd, rc->device)))
goto out;
+ log_verbose(_("Activating temporary device using old LUKS header.\n"));
if ((r = crypt_activate_by_passphrase(cd, rc->header_file_org,
opt_key_slot, rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen,
CRYPT_ACTIVATE_READONLY|CRYPT_ACTIVATE_PRIVATE)) < 0)
(r = crypt_set_data_device(cd_new, rc->device)))
goto out;
+ log_verbose(_("Activating temporary device using new LUKS header.\n"));
if ((r = crypt_activate_by_passphrase(cd_new, rc->header_file_new,
opt_key_slot, rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen,
CRYPT_ACTIVATE_SHARED|CRYPT_ACTIVATE_PRIVATE)) < 0)
static int create_new_header(struct reenc_ctx *rc, const char *cipher,
const char *cipher_mode, const char *uuid,
- int key_size, struct crypt_params_luks1 *params)
+ const char *key, int key_size,
+ struct crypt_params_luks1 *params)
{
struct crypt_device *cd_new = NULL;
int i, r;
crypt_set_iteration_time(cd_new, opt_iteration_time);
if ((r = crypt_format(cd_new, CRYPT_LUKS1, cipher, cipher_mode,
- uuid, NULL, key_size, params)))
+ uuid, key, key_size, params)))
goto out;
log_verbose(_("New LUKS header for device %s created.\n"), rc->device);
struct crypt_device *cd = NULL;
struct crypt_params_luks1 params = {0};
char cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
+ char *old_key = NULL;
+ size_t old_key_size;
int r;
log_dbg("Creating LUKS header backup for device %s.", rc->device);
goto out;
log_verbose(_("LUKS header backup of device %s created.\n"), rc->device);
+ /* For decrypt, new header will be fake one, so we are done here. */
+ if (rc->reencrypt_mode == DECRYPT)
+ goto out;
+
if ((r = create_empty_header(rc->header_file_new, rc->header_file_org,
crypt_get_data_offset(cd))))
goto out;
}
}
+ if (opt_keep_key) {
+ log_dbg("Keeping key from old header.");
+ old_key_size = crypt_get_volume_key_size(cd);
+ old_key = crypt_safe_alloc(old_key_size);
+ if (!old_key) {
+ r = -ENOMEM;
+ goto out;
+ }
+ r = crypt_volume_key_get(cd, CRYPT_ANY_SLOT, old_key, &old_key_size,
+ rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen);
+ if (r < 0)
+ goto out;
+ }
+
r = create_new_header(rc,
opt_cipher ? cipher : crypt_get_cipher(cd),
opt_cipher ? cipher_mode : crypt_get_cipher_mode(cd),
crypt_get_uuid(cd),
+ old_key,
opt_key_size ? opt_key_size / 8 : crypt_get_volume_key_size(cd),
¶ms);
out:
crypt_free(cd);
+ crypt_safe_free(old_key);
if (r)
log_err(_("Creation of LUKS backup headers failed.\n"));
return r;
struct crypt_device *cd_new = NULL;
struct crypt_params_luks1 params = {0};
char cipher [MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
-
+ const char *header_file_fake;
int r;
- log_dbg("Creating fake (cipher_null) header for original device.");
+ log_dbg("Creating fake (cipher_null) header for %s device.",
+ (rc->reencrypt_mode == DECRYPT) ? "new" : "original");
+
+ header_file_fake = (rc->reencrypt_mode == DECRYPT) ? rc->header_file_new : rc->header_file_org;
if (!opt_key_size)
opt_key_size = DEFAULT_LUKS1_KEYBITS;
}
}
- r = create_empty_header(rc->header_file_org, NULL, 0);
+ r = create_empty_header(header_file_fake, NULL, MAX_BCK_SECTORS);
if (r < 0)
return r;
params.data_alignment = 0;
params.data_device = rc->device;
- r = crypt_init(&cd_new, rc->header_file_org);
+ r = crypt_init(&cd_new, header_file_fake);
if (r < 0)
return r;
if (r < 0)
goto out;
- r = crypt_keyslot_add_by_volume_key(cd_new, 0, NULL, 0,
- rc->p[0].password, rc->p[0].passwordLen);
+ r = crypt_keyslot_add_by_volume_key(cd_new, rc->keyslot, NULL, 0,
+ rc->p[rc->keyslot].password, rc->p[rc->keyslot].passwordLen);
if (r < 0)
goto out;
+ /* The real header is backup header created in backup_luks_headers() */
+ if (rc->reencrypt_mode == DECRYPT)
+ goto out;
+
r = create_empty_header(rc->header_file_new, rc->header_file_org, 0);
if (r < 0)
goto out;
r = create_new_header(rc,
opt_cipher ? cipher : DEFAULT_LUKS1_CIPHER,
opt_cipher ? cipher_mode : DEFAULT_LUKS1_MODE,
- NULL,
+ NULL, NULL,
(opt_key_size ? opt_key_size : DEFAULT_LUKS1_KEYBITS) / 8,
¶ms);
out:
if (!tdiff)
return;
- mbytes = (bytes - rc->restart_bytes) / 1024 / 1024;
+ mbytes = (bytes - rc->resume_bytes) / 1024 / 1024;
mib = (double)(mbytes) / tdiff;
if (!mib)
return;
+ /* FIXME: calculate this from last minute only and remaining space */
eta = (unsigned long long)(rc->device_size / 1024 / 1024 / mib - tdiff);
/* vt100 code clear line */
final ? "\n" :"");
}
+static ssize_t read_buf(int fd, void *buf, size_t count)
+{
+ size_t read_size = 0;
+ ssize_t s;
+
+ do {
+ /* This expects that partial read is aligned in buffer */
+ s = read(fd, buf, count - read_size);
+ if (s == -1 && errno != EINTR)
+ return s;
+ if (s == 0)
+ return (ssize_t)read_size;
+ if (s > 0) {
+ if (s != (ssize_t)count)
+ log_dbg("Partial read %zd / %zu.", s, count);
+ read_size += (size_t)s;
+ buf = (uint8_t*)buf + s;
+ }
+ } while (read_size != count);
+
+ return (ssize_t)count;
+}
+
static int copy_data_forward(struct reenc_ctx *rc, int fd_old, int fd_new,
size_t block_size, void *buf, uint64_t *bytes)
{
return -EIO;
}
- rc->restart_bytes = *bytes = rc->device_offset;
+ rc->resume_bytes = *bytes = rc->device_offset;
if (write_log(rc) < 0)
return -EIO;
while (!quit && rc->device_offset < rc->device_size) {
- s1 = read(fd_old, buf, block_size);
+ s1 = read_buf(fd_old, buf, block_size);
if (s1 < 0 || ((size_t)s1 != block_size &&
(rc->device_offset + s1) != rc->device_size)) {
- log_dbg("Read error, expecting %d, got %d.",
- (int)block_size, (int)s1);
+ log_dbg("Read error, expecting %zu, got %zd.",
+ block_size, s1);
return -EIO;
}
s2 = write(fd_new, buf, s1);
if (s2 < 0) {
- log_dbg("Write error, expecting %d, got %d.",
- (int)block_size, (int)s2);
+ log_dbg("Write error, expecting %zu, got %zd.",
+ block_size, s2);
return -EIO;
}
if (!rc->in_progress) {
rc->device_offset = rc->device_size;
- rc->restart_bytes = 0;
+ rc->resume_bytes = 0;
*bytes = 0;
} else {
- rc->restart_bytes = rc->device_size - rc->device_offset;
- *bytes = rc->restart_bytes;
+ rc->resume_bytes = rc->device_size - rc->device_offset;
+ *bytes = rc->resume_bytes;
}
if (write_log(rc) < 0)
return -EIO;
}
- s1 = read(fd_old, buf, working_block);
+ s1 = read_buf(fd_old, buf, working_block);
if (s1 < 0 || (s1 != working_block)) {
- log_dbg("Read error, expecting %d, got %d.",
- (int)block_size, (int)s1);
+ log_dbg("Read error, expecting %zu, got %zd.",
+ block_size, s1);
return -EIO;
}
s2 = write(fd_new, buf, working_block);
if (s2 < 0) {
- log_dbg("Write error, expecting %d, got %d.",
- (int)block_size, (int)s2);
+ log_dbg("Write error, expecting %zu, got %zd.",
+ block_size, s2);
return -EIO;
}
return quit ? -EAGAIN : 0;
}
+static void zero_rest_of_device(int fd, size_t block_size, void *buf,
+ uint64_t *bytes, uint64_t offset)
+{
+ ssize_t s1, s2;
+
+ log_dbg("Zeroing rest of device.");
+
+ if (lseek64(fd, offset, SEEK_SET) < 0) {
+ log_dbg(_("Cannot seek to device offset.\n"));
+ return;
+ }
+
+ memset(buf, 0, block_size);
+ s1 = block_size;
+
+ while (!quit && *bytes) {
+ if (*bytes < s1)
+ s1 = *bytes;
+
+ s2 = write(fd, buf, s1);
+ if (s2 < 0) {
+ log_dbg("Write error, expecting %zu, got %zd.",
+ block_size, s2);
+ return;
+ }
+
+ if (opt_fsync && fsync(fd) < 0) {
+ log_dbg("Write error, fsync.");
+ return;
+ }
+
+ *bytes -= s2;
+ }
+}
+
static int copy_data(struct reenc_ctx *rc)
{
size_t block_size = opt_bsize * 1024 * 1024;
goto out;
}
- /* Check size */
- if (ioctl(fd_new, BLKGETSIZE64, &rc->device_size_real) < 0) {
+ if (ioctl(fd_old, BLKGETSIZE64, &rc->device_size_org_real) < 0) {
log_err(_("Cannot get device size.\n"));
goto out;
}
- rc->device_size = opt_device_size ?: rc->device_size_real;
+ if (ioctl(fd_new, BLKGETSIZE64, &rc->device_size_new_real) < 0) {
+ log_err(_("Cannot get device size.\n"));
+ goto out;
+ }
+
+ if (opt_device_size)
+ rc->device_size = opt_device_size;
+ else if (rc->reencrypt_mode == DECRYPT)
+ rc->device_size = rc->device_size_org_real;
+ else
+ rc->device_size = rc->device_size_new_real;
if (posix_memalign((void *)&buf, alignment(fd_new), block_size)) {
log_err(_("Allocation of aligned memory failed.\n"));
goto out;
}
- set_int_handler();
+ set_int_handler(0);
gettimeofday(&rc->start_time, NULL);
if (rc->reencrypt_direction == FORWARD)
else
r = copy_data_backward(rc, fd_old, fd_new, block_size, buf, &bytes);
- set_int_block(1);
print_progress(rc, bytes, 1);
+ /* Zero (wipe) rest of now plain-only device when decrypting.
+ * (To not leave any sign of encryption here.) */
+ if (!r && rc->reencrypt_mode == DECRYPT &&
+ rc->device_size_new_real > rc->device_size_org_real) {
+ bytes = rc->device_size_new_real - rc->device_size_org_real;
+ zero_rest_of_device(fd_new, block_size, buf, &bytes, rc->device_size_org_real);
+ }
+
+ set_int_block(1);
+
if (r == -EAGAIN)
log_err(_("Interrupted by a signal.\n"));
else if (r < 0)
static int init_passphrase1(struct reenc_ctx *rc, struct crypt_device *cd,
const char *msg, int slot_to_check, int check)
{
- int r = -EINVAL, slot, retry_count;
-
- slot = (slot_to_check == CRYPT_ANY_SLOT) ? 0 : slot_to_check;
+ char *password;
+ int r = -EINVAL, retry_count;
+ size_t passwordLen;
retry_count = opt_tries ?: 1;
while (retry_count--) {
- set_int_handler();
- r = crypt_get_key(msg, &rc->p[slot].password,
- &rc->p[slot].passwordLen,
+ set_int_handler(0);
+ r = crypt_get_key(msg, &password, &passwordLen,
0, 0, NULL /*opt_key_file*/,
0, 0, cd);
if (r < 0)
set_int_block(1);
if (check)
r = crypt_activate_by_passphrase(cd, NULL, slot_to_check,
- rc->p[slot].password, rc->p[slot].passwordLen, 0);
+ password, passwordLen, 0);
else
- r = slot;
+ r = (slot_to_check == CRYPT_ANY_SLOT) ? 0 : slot_to_check;
if (r < 0) {
- crypt_safe_free(rc->p[slot].password);
- rc->p[slot].password = NULL;
- rc->p[slot].passwordLen = 0;
+ crypt_safe_free(password);
+ password = NULL;
+ passwordLen = 0;
}
if (r < 0 && r != -EPERM)
return r;
if (r >= 0) {
- rc->keyslot = slot;
+ rc->keyslot = r;
+ rc->p[r].password = password;
+ rc->p[r].passwordLen = passwordLen;
break;
}
log_err(_("No key available with this passphrase.\n"));
}
+
+ password = NULL;
+ passwordLen = 0;
+
return r;
}
static int init_keyfile(struct reenc_ctx *rc, struct crypt_device *cd, int slot_check)
{
- int r, slot;
+ char *password;
+ int r;
+ size_t passwordLen;
- slot = (slot_check == CRYPT_ANY_SLOT) ? 0 : slot_check;
- r = crypt_get_key(NULL, &rc->p[slot].password, &rc->p[slot].passwordLen,
- opt_keyfile_offset, opt_keyfile_size, opt_key_file, 0, 0, cd);
+ r = crypt_get_key(NULL, &password, &passwordLen, opt_keyfile_offset,
+ opt_keyfile_size, opt_key_file, 0, 0, cd);
if (r < 0)
return r;
- r = crypt_activate_by_passphrase(cd, NULL, slot_check,
- rc->p[slot].password, rc->p[slot].passwordLen, 0);
+ r = crypt_activate_by_passphrase(cd, NULL, slot_check, password,
+ passwordLen, 0);
/*
* Allow keyslot only if it is last slot or if user explicitly
- * specify whch slot to use (IOW others will be disabled).
+ * specify which slot to use (IOW others will be disabled).
*/
if (r >= 0 && opt_key_slot == CRYPT_ANY_SLOT &&
crypt_keyslot_status(cd, r) != CRYPT_SLOT_ACTIVE_LAST) {
}
if (r < 0) {
- crypt_safe_free(rc->p[slot].password);
- rc->p[slot].password = NULL;
- rc->p[slot].passwordLen = 0;
+ crypt_safe_free(password);
if (r == -EPERM)
log_err(_("No key available with this passphrase.\n"));
- return r;
- } else
- rc->keyslot = slot;
+ } else {
+ rc->keyslot = r;
+ rc->p[r].password = password;
+ rc->p[r].passwordLen = passwordLen;
+ }
+
+ password = NULL;
+ passwordLen = 0;
return r;
}
log_dbg("Passhrases initialization.");
- if (opt_new && !rc->in_progress) {
- r = init_passphrase1(rc, cd, _("Enter new LUKS passphrase: "), 0, 0);
+ if (rc->reencrypt_mode == ENCRYPT && !rc->in_progress) {
+ r = init_passphrase1(rc, cd, _("Enter new passphrase: "), opt_key_slot, 0);
return r > 0 ? 0 : r;
}
return r;
}
+ if (opt_key_slot != CRYPT_ANY_SLOT)
+ snprintf(msg, sizeof(msg),
+ _("Enter passphrase for key slot %u: "), opt_key_slot);
+ else
+ snprintf(msg, sizeof(msg), _("Enter any existing passphrase: "));
+
if (opt_key_file) {
r = init_keyfile(rc, cd, opt_key_slot);
- } else if (rc->in_progress) {
- r = init_passphrase1(rc, cd, _("Enter any LUKS passphrase: "),
- CRYPT_ANY_SLOT, 1);
+ } else if (rc->in_progress ||
+ opt_key_slot != CRYPT_ANY_SLOT ||
+ rc->reencrypt_mode == DECRYPT) {
+ r = init_passphrase1(rc, cd, msg, opt_key_slot, 1);
} else for (i = 0; i < MAX_SLOT; i++) {
ki = crypt_keyslot_status(cd, i);
if (ki != CRYPT_SLOT_ACTIVE && ki != CRYPT_SLOT_ACTIVE_LAST)
continue;
- snprintf(msg, sizeof(msg), _("Enter LUKS passphrase for key slot %u: "), i);
+ snprintf(msg, sizeof(msg), _("Enter passphrase for key slot %u: "), i);
r = init_passphrase1(rc, cd, msg, i, 1);
if (r < 0)
break;
rc->reencrypt_direction = BACKWARD;
rc->device_offset = (uint64_t)~0;
}
+
+ if (opt_new)
+ rc->reencrypt_mode = ENCRYPT;
+ else if (opt_decrypt)
+ rc->reencrypt_mode = DECRYPT;
+ else
+ rc->reencrypt_mode = REENCRYPT;
}
return 0;
static int run_reencrypt(const char *device)
{
int r = -EINVAL;
- struct reenc_ctx rc = {};
+ static struct reenc_ctx rc = {};
if (initialize_context(&rc, device))
goto out;
log_dbg("Running reencryption.");
if (!rc.in_progress) {
- if (opt_new) {
- if ((r = initialize_passphrase(&rc, rc.device)) ||
- (r = backup_fake_header(&rc)))
- goto out;
- } else if ((r = initialize_passphrase(&rc, rc.device)) ||
- (r = backup_luks_headers(&rc)) ||
- (r = device_check(&rc, MAKE_UNUSABLE)))
+ if ((r = initialize_passphrase(&rc, rc.device)))
goto out;
+
+ if (rc.reencrypt_mode == ENCRYPT) {
+ /* Create fake header for exising device */
+ if ((r = backup_fake_header(&rc)))
+ goto out;
+ } else {
+ if ((r = backup_luks_headers(&rc)))
+ goto out;
+ /* Create fake header for decrypted device */
+ if (rc.reencrypt_mode == DECRYPT &&
+ (r = backup_fake_header(&rc)))
+ goto out;
+ if ((r = device_check(&rc, MAKE_UNUSABLE)))
+ goto out;
+ }
} else {
if ((r = initialize_passphrase(&rc, rc.header_file_new)))
goto out;
}
- if ((r = activate_luks_headers(&rc)))
- goto out;
+ if (!opt_keep_key) {
+ log_dbg("Running data area reencryption.");
+ if ((r = activate_luks_headers(&rc)))
+ goto out;
- if ((r = copy_data(&rc)))
- goto out;
+ if ((r = copy_data(&rc)))
+ goto out;
+ } else
+ log_dbg("Keeping existing key, skipping data area reencryption.");
- r = restore_luks_header(&rc);
+ // FIXME: fix error path above to not skip this
+ if (rc.reencrypt_mode != DECRYPT)
+ r = restore_luks_header(&rc);
out:
destroy_context(&rc);
return r;
}
-static __attribute__ ((noreturn)) void usage(poptContext popt_context,
- int exitcode, const char *error,
- const char *more)
-{
- poptPrintUsage(popt_context, stderr, 0);
- if (error)
- log_err("%s: %s\n", more, error);
- poptFreeContext(popt_context);
- exit(exitcode);
-}
-
static void help(poptContext popt_context,
enum poptCallbackReason reason __attribute__((unused)),
struct poptOption *key,
usage(popt_context, EXIT_SUCCESS, NULL, NULL);
}
-static void _dbg_version_and_cmd(int argc, const char **argv)
-{
- int i;
-
- log_std("# %s %s processing \"", PACKAGE_REENC, PACKAGE_VERSION);
- for (i = 0; i < argc; i++) {
- if (i)
- log_std(" ");
- log_std("%s", argv[i]);
- }
- log_std("\"\n");
-}
-
int main(int argc, const char **argv)
{
static struct poptOption popt_help_options[] = {
{ "cipher", 'c', POPT_ARG_STRING, &opt_cipher, 0, N_("The cipher used to encrypt the disk (see /proc/crypto)"), NULL },
{ "key-size", 's', POPT_ARG_INT, &opt_key_size, 0, N_("The size of the encryption key"), N_("BITS") },
{ "hash", 'h', POPT_ARG_STRING, &opt_hash, 0, N_("The hash used to create the encryption key from the passphrase"), NULL },
+ { "keep-key", '\0', POPT_ARG_NONE, &opt_keep_key, 0, N_("Do not change key, no data area reencryption."), NULL },
{ "key-file", 'd', POPT_ARG_STRING, &opt_key_file, 0, N_("Read the key from a file."), NULL },
{ "iter-time", 'i', POPT_ARG_INT, &opt_iteration_time, 0, N_("PBKDF2 iteration time for LUKS (in ms)"), N_("msecs") },
{ "batch-mode", 'q', POPT_ARG_NONE, &opt_batch_mode, 0, N_("Do not ask for confirmation"), NULL },
{ "tries", 'T', POPT_ARG_INT, &opt_tries, 0, N_("How often the input of the passphrase can be retried"), NULL },
{ "use-random", '\0', POPT_ARG_NONE, &opt_random, 0, N_("Use /dev/random for generating volume key."), NULL },
{ "use-urandom", '\0', POPT_ARG_NONE, &opt_urandom, 0, N_("Use /dev/urandom for generating volume key."), NULL },
- { "use-directio", '\0', POPT_ARG_NONE, &opt_directio, 0, N_("Use direct-io when accesing devices."), NULL },
+ { "use-directio", '\0', POPT_ARG_NONE, &opt_directio, 0, N_("Use direct-io when accessing devices."), NULL },
{ "use-fsync", '\0', POPT_ARG_NONE, &opt_fsync, 0, N_("Use fsync after each block."), NULL },
{ "write-log", '\0', POPT_ARG_NONE, &opt_write_log, 0, N_("Update log file after every block."), NULL },
{ "key-slot", 'S', POPT_ARG_INT, &opt_key_slot, 0, N_("Use only this slot (others will be disabled)."), NULL },
{ "keyfile-size", 'l', POPT_ARG_LONG, &opt_keyfile_size, 0, N_("Limits the read from keyfile"), N_("bytes") },
{ "reduce-device-size",'\0', POPT_ARG_STRING, &opt_reduce_size_str, 0, N_("Reduce data device size (move data offset). DANGEROUS!"), N_("bytes") },
{ "device-size", '\0', POPT_ARG_STRING, &opt_device_size_str, 0, N_("Use only specified device size (ignore rest of device). DANGEROUS!"), N_("bytes") },
- { "new", 'N', POPT_ARG_NONE,&opt_new, 0, N_("Create new header on not encrypted device."), NULL },
+ { "new", 'N', POPT_ARG_NONE, &opt_new, 0, N_("Create new header on not encrypted device."), NULL },
+ { "decrypt", '\0', POPT_ARG_NONE, &opt_decrypt, 0, N_("Permanently decrypt device (remove encryption)."), NULL },
POPT_TABLEEND
};
poptContext popt_context;
int r;
- crypt_set_log_callback(NULL, _log, NULL);
+ crypt_set_log_callback(NULL, tool_log, NULL);
set_int_block(1);
if (opt_bsize < 1 || opt_bsize > 64)
usage(popt_context, EXIT_FAILURE,
- _("Only values between 1MiB and 64 MiB allowed for reencryption block size."),
+ _("Only values between 1 MiB and 64 MiB allowed for reencryption block size."),
poptGetInvocationName(popt_context));
if (opt_key_size % 8)
usage(popt_context, EXIT_FAILURE, _("Option --new must be used together with --reduce-device-size."),
poptGetInvocationName(popt_context));
+ if (opt_keep_key && ((!opt_hash && !opt_iteration_time) || opt_cipher || opt_new))
+ usage(popt_context, EXIT_FAILURE, _("Option --keep-key can be used only with --hash or --iter-time."),
+ poptGetInvocationName(popt_context));
+
+ if (opt_new && opt_decrypt)
+ usage(popt_context, EXIT_FAILURE, _("Option --new cannot be used together with --decrypt."),
+ poptGetInvocationName(popt_context));
+
+ if (opt_decrypt && (opt_cipher || opt_hash || opt_reduce_size || opt_keep_key || opt_device_size))
+ usage(popt_context, EXIT_FAILURE, _("Option --decrypt is incompatible with specified parameters."),
+ poptGetInvocationName(popt_context));
+
if (opt_debug) {
opt_verbose = 1;
crypt_set_debug_level(-1);
- _dbg_version_and_cmd(argc, argv);
+ dbg_version_and_cmd(argc, argv);
}
r = run_reencrypt(action_argv[0]);
poptFreeContext(popt_context);
- /* Translate exit code to simple codes */
- switch (r) {
- case 0: r = EXIT_SUCCESS; break;
- case -EEXIST:
- case -EBUSY: r = 5; break;
- case -ENOTBLK:
- case -ENODEV: r = 4; break;
- case -ENOMEM: r = 3; break;
- case -EPERM: r = 2; break;
- default: r = EXIT_FAILURE;
- }
- return r;
+ return translate_errno(r);
}