Rewrite dm query/create function backend.
[platform/upstream/cryptsetup.git] / lib / loopaes / loopaes.c
index 5e7ad20..5bddc2c 100644 (file)
@@ -27,7 +27,7 @@
 
 static const char *get_hash(unsigned int key_size)
 {
-       char *hash;
+       const char *hash;
 
        switch (key_size) {
                case 16: hash = "sha256"; break;
@@ -54,30 +54,40 @@ static int hash_key(const char *src, size_t src_len,
                    const char *hash_name)
 {
        struct crypt_hash *hd = NULL;
+       int r;
 
        if (crypt_hash_init(&hd, hash_name))
                return -EINVAL;
-       crypt_hash_write(hd, src, src_len);
-       crypt_hash_final(hd, dst, dst_len);
+
+       r = crypt_hash_write(hd, src, src_len);
+       if (!r)
+               r = crypt_hash_final(hd, dst, dst_len);
+
        crypt_hash_destroy(hd);
-       return 0;
+       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)
@@ -100,7 +110,7 @@ static int hash_keys(struct volume_key **vk,
        return r;
 }
 
-static int keyfile_is_gpg(char *buffer, unsigned int buffer_len)
+static int keyfile_is_gpg(char *buffer, size_t buffer_len)
 {
        int r = 0;
        int index = buffer_len < 100 ? buffer_len - 1 : 100;
@@ -115,24 +125,22 @@ static int keyfile_is_gpg(char *buffer, unsigned int buffer_len)
 
 int LOOPAES_parse_keyfile(struct crypt_device *cd,
                          struct volume_key **vk,
+                         const char *hash,
                          unsigned int *keys_count,
                          char *buffer,
-                         unsigned int buffer_len)
-
+                         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);
 
-       if (buffer_len < LOOPAES_KEYFILE_MINSIZE) {
-               log_err(cd, _("Incompatible loop-AES keyfile detected.\n"));
+       if (!buffer_len)
                return -EINVAL;
-       }
 
        if (keyfile_is_gpg(buffer, buffer_len)) {
-               log_err(cd, "Detected not yet supported GPG encrypted keyfile.\n");
-               log_std(cd, "Please use gpg --decrypt <KEYFILE> | cryptsetup --keyfile=- ...\n");
+               log_err(cd, _("Detected not yet supported GPG encrypted keyfile.\n"));
+               log_std(cd, _("Please use gpg --decrypt <KEYFILE> | cryptsetup --keyfile=- ...\n"));
                return -EINVAL;
        }
 
@@ -169,7 +177,7 @@ int LOOPAES_parse_keyfile(struct crypt_device *cd,
        }
 
        *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,
@@ -177,37 +185,45 @@ int LOOPAES_activate(struct crypt_device *cd,
                     const char *base_cipher,
                     unsigned int keys_count,
                     struct volume_key *vk,
+                    uint64_t skip,
                     uint32_t flags)
 {
-       uint64_t size, offset;
-       char *cipher;
-       const char *device;
-       int read_only, r;
-
-       size = 0;
-       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);
+       char *cipher = NULL;
+       uint32_t req_flags;
+       int r;
+       struct crypt_dm_active_device dmd = {
+               .device = crypt_get_device_name(cd),
+               .cipher = NULL,
+               .uuid   = crypt_get_uuid(cd),
+               .key    = vk->key,
+               .key_size = vk->keylength,
+               .offset = crypt_get_data_offset(cd),
+               .iv_offset = skip,
+               .size   = 0,
+               .flags  = flags
+       };
+
+
+       r = device_check_and_adjust(cd, dmd.device, DEV_EXCL, &dmd.size, &dmd.offset, &flags);
        if (r)
                return r;
 
-       if (keys_count == 1)
-               r = asprintf(&cipher, "%s-%s", base_cipher, "cbc-plain");
-       else
+       if (keys_count == 1) {
+               req_flags = DM_PLAIN64_SUPPORTED;
+               r = asprintf(&cipher, "%s-%s", base_cipher, "cbc-plain64");
+       } else {
+               req_flags = DM_LMK_SUPPORTED;
                r = asprintf(&cipher, "%s:%d-%s", base_cipher, 64, "cbc-lmk");
+       }
        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, 0, offset, vk->keylength, vk->key,
-                            read_only, 0);
+       dmd.cipher = cipher;
+       log_dbg("Trying to activate loop-AES device %s using cipher %s.", name, dmd.cipher);
+
+       r = dm_create_device(name, CRYPT_LOOPAES, &dmd, 0);
 
-       if (!r && keys_count != 1 && !(dm_flags() & DM_LMK_SUPPORTED)) {
+       if (!r && !(dm_flags() & req_flags)) {
                log_err(cd, _("Kernel doesn't support loop-AES compatible mapping.\n"));
                r = -ENOTSUP;
        }