/*
* loop-AES compatible volume handling
*
- * Copyright (C) 2011, Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2011-2012, Red Hat, Inc. 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
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
-#include "crypto_backend.h"
+#include "libcryptsetup.h"
#include "loopaes.h"
+#include "internal.h"
static const char *get_hash(unsigned int key_size)
{
- char *hash;
+ const char *hash;
switch (key_size) {
case 16: hash = "sha256"; break;
return r;
}
-static int hash_keys(struct volume_key **vk,
+static int hash_keys(struct crypt_device *cd,
+ struct volume_key **vk,
+ const char *hash_override,
const char **input_keys,
unsigned int keys_count,
unsigned int key_len_output)
{
const char *hash_name;
char tweak, *key_ptr;
- int r, i, key_len_input;
+ unsigned i, key_len_input;
+ int r;
- hash_name = get_hash(key_len_output);
+ hash_name = hash_override ?: get_hash(key_len_output);
tweak = get_tweak(keys_count);
key_len_input = strlen(input_keys[0]);
- if (!keys_count || !key_len_output || !hash_name || !key_len_input)
+ if (!keys_count || !key_len_output || !hash_name || !key_len_input) {
+ log_err(cd, _("Key processing error (using hash %s).\n"),
+ hash_name ?: "[none]");
return -EINVAL;
+ }
*vk = crypt_alloc_volume_key(key_len_output * keys_count, NULL);
if (!*vk)
int LOOPAES_parse_keyfile(struct crypt_device *cd,
struct volume_key **vk,
+ const char *hash,
unsigned int *keys_count,
char *buffer,
size_t buffer_len)
{
const char *keys[LOOPAES_KEYS_MAX];
- int i, key_index, key_len, offset;
+ unsigned i, key_index, key_len, offset;
log_dbg("Parsing loop-AES keyfile of size %d.", buffer_len);
}
*keys_count = key_index;
- return hash_keys(vk, keys, key_index, crypt_get_volume_key_size(cd));
+ return hash_keys(cd, vk, hash, keys, key_index, crypt_get_volume_key_size(cd));
}
int LOOPAES_activate(struct crypt_device *cd,
struct volume_key *vk,
uint32_t flags)
{
- uint64_t size, offset;
+ char *cipher = NULL;
uint32_t req_flags;
- char *cipher;
- const char *device;
- int read_only, r;
+ int r;
+ struct crypt_dm_active_device dmd = {
+ .target = DM_CRYPT,
+ .uuid = crypt_get_uuid(cd),
+ .size = 0,
+ .flags = flags,
+ .data_device = crypt_get_device_name(cd),
+ .u.crypt = {
+ .cipher = NULL,
+ .vk = vk,
+ .offset = crypt_get_data_offset(cd),
+ .iv_offset = crypt_get_iv_offset(cd),
+ }
+ };
- size = 0;
- /* Initial IV (skip) is always the same as offset */
- offset = crypt_get_data_offset(cd);
- device = crypt_get_device_name(cd);
- read_only = flags & CRYPT_ACTIVATE_READONLY;
- r = device_check_and_adjust(cd, device, 1, &size, &offset, &read_only);
+ r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
+ &dmd.size, &dmd.u.crypt.offset, &flags);
if (r)
return r;
if (r < 0)
return -ENOMEM;
- log_dbg("Trying to activate loop-AES device %s using cipher %s.", name, cipher);
- r = dm_create_device(name, device,
- cipher, CRYPT_LOOPAES,
- crypt_get_uuid(cd),
- size, offset, offset, vk->keylength, vk->key,
- read_only, 0);
+ dmd.u.crypt.cipher = cipher;
+ log_dbg("Trying to activate loop-AES device %s using cipher %s.",
+ name, dmd.u.crypt.cipher);
+
+ r = dm_create_device(name, CRYPT_LOOPAES, &dmd, 0);
if (!r && !(dm_flags() & req_flags)) {
log_err(cd, _("Kernel doesn't support loop-AES compatible mapping.\n"));