"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/lib/luks2/luks2_keyslot_luks2.c" (13 Jan 2022, 24482 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "luks2_keyslot_luks2.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2.4.1_vs_2.4.2.

    1 /*
    2  * LUKS - Linux Unified Key Setup v2, LUKS2 type keyslot handler
    3  *
    4  * Copyright (C) 2015-2021 Red Hat, Inc. All rights reserved.
    5  * Copyright (C) 2015-2021 Milan Broz
    6  *
    7  * This program is free software; you can redistribute it and/or
    8  * modify it under the terms of the GNU General Public License
    9  * as published by the Free Software Foundation; either version 2
   10  * of the License, or (at your option) any later version.
   11  *
   12  * This program is distributed in the hope that it will be useful,
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   20  */
   21 
   22 #include "luks2_internal.h"
   23 
   24 /* FIXME: move keyslot encryption to crypto backend */
   25 #include "../luks1/af.h"
   26 
   27 #define LUKS_SALTSIZE 32
   28 #define LUKS_SLOT_ITERATIONS_MIN 1000
   29 #define LUKS_STRIPES 4000
   30 
   31 /* Serialize memory-hard keyslot access: optional workaround for parallel processing */
   32 #define MIN_MEMORY_FOR_SERIALIZE_LOCK_KB 32*1024 /* 32MB */
   33 
   34 static int luks2_encrypt_to_storage(char *src, size_t srcLength,
   35     const char *cipher, const char *cipher_mode,
   36     struct volume_key *vk, unsigned int sector,
   37     struct crypt_device *cd)
   38 {
   39 #ifndef ENABLE_AF_ALG /* Support for old kernel without Crypto API */
   40     return LUKS_encrypt_to_storage(src, srcLength, cipher, cipher_mode, vk, sector, cd);
   41 #else
   42     struct crypt_storage *s;
   43     int devfd, r;
   44     struct device *device = crypt_metadata_device(cd);
   45 
   46     /* Only whole sector writes supported */
   47     if (MISALIGNED_512(srcLength))
   48         return -EINVAL;
   49 
   50     /* Encrypt buffer */
   51     r = crypt_storage_init(&s, SECTOR_SIZE, cipher, cipher_mode, vk->key, vk->keylength, false);
   52     if (r) {
   53         log_err(cd, _("Cannot use %s-%s cipher for keyslot encryption."), cipher, cipher_mode);
   54         return r;
   55     }
   56 
   57     r = crypt_storage_encrypt(s, 0, srcLength, src);
   58     crypt_storage_destroy(s);
   59     if (r) {
   60         log_err(cd, _("IO error while encrypting keyslot."));
   61         return r;
   62     }
   63 
   64     devfd = device_open_locked(cd, device, O_RDWR);
   65     if (devfd >= 0) {
   66         if (write_lseek_blockwise(devfd, device_block_size(cd, device),
   67                       device_alignment(device), src,
   68                       srcLength, sector * SECTOR_SIZE) < 0)
   69             r = -EIO;
   70         else
   71             r = 0;
   72 
   73         device_sync(cd, device);
   74     } else
   75         r = -EIO;
   76 
   77     if (r)
   78         log_err(cd, _("IO error while encrypting keyslot."));
   79 
   80     return r;
   81 #endif
   82 }
   83 
   84 static int luks2_decrypt_from_storage(char *dst, size_t dstLength,
   85     const char *cipher, const char *cipher_mode, struct volume_key *vk,
   86     unsigned int sector, struct crypt_device *cd)
   87 {
   88     struct device *device = crypt_metadata_device(cd);
   89 #ifndef ENABLE_AF_ALG /* Support for old kernel without Crypto API */
   90     int r = device_read_lock(cd, device);
   91     if (r) {
   92         log_err(cd, _("Failed to acquire read lock on device %s."), device_path(device));
   93         return r;
   94     }
   95     r = LUKS_decrypt_from_storage(dst, dstLength, cipher, cipher_mode, vk, sector, cd);
   96     device_read_unlock(cd, crypt_metadata_device(cd));
   97     return r;
   98 #else
   99     struct crypt_storage *s;
  100     int devfd, r;
  101 
  102     /* Only whole sector writes supported */
  103     if (MISALIGNED_512(dstLength))
  104         return -EINVAL;
  105 
  106     r = crypt_storage_init(&s, SECTOR_SIZE, cipher, cipher_mode, vk->key, vk->keylength, false);
  107     if (r) {
  108         log_err(cd, _("Cannot use %s-%s cipher for keyslot encryption."), cipher, cipher_mode);
  109         return r;
  110     }
  111 
  112     r = device_read_lock(cd, device);
  113     if (r) {
  114         log_err(cd, _("Failed to acquire read lock on device %s."),
  115             device_path(device));
  116         crypt_storage_destroy(s);
  117         return r;
  118     }
  119 
  120     devfd = device_open_locked(cd, device, O_RDONLY);
  121     if (devfd >= 0) {
  122         if (read_lseek_blockwise(devfd, device_block_size(cd, device),
  123                      device_alignment(device), dst,
  124                      dstLength, sector * SECTOR_SIZE) < 0)
  125             r = -EIO;
  126         else
  127             r = 0;
  128     } else
  129         r = -EIO;
  130 
  131     device_read_unlock(cd, device);
  132 
  133     /* Decrypt buffer */
  134     if (!r)
  135         r = crypt_storage_decrypt(s, 0, dstLength, dst);
  136     else
  137         log_err(cd, _("IO error while decrypting keyslot."));
  138 
  139     crypt_storage_destroy(s);
  140     return r;
  141 #endif
  142 }
  143 
  144 static int luks2_keyslot_get_pbkdf_params(json_object *jobj_keyslot,
  145                         struct crypt_pbkdf_type *pbkdf, char *salt)
  146 {
  147     json_object *jobj_kdf, *jobj1, *jobj2;
  148     size_t salt_len;
  149 
  150     if (!jobj_keyslot || !pbkdf)
  151         return -EINVAL;
  152 
  153     memset(pbkdf, 0, sizeof(*pbkdf));
  154 
  155     if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf))
  156         return -EINVAL;
  157 
  158     if (!json_object_object_get_ex(jobj_kdf, "type", &jobj1))
  159         return -EINVAL;
  160     pbkdf->type = json_object_get_string(jobj1);
  161     if (!strcmp(pbkdf->type, CRYPT_KDF_PBKDF2)) {
  162         if (!json_object_object_get_ex(jobj_kdf, "hash", &jobj2))
  163             return -EINVAL;
  164         pbkdf->hash = json_object_get_string(jobj2);
  165         if (!json_object_object_get_ex(jobj_kdf, "iterations", &jobj2))
  166             return -EINVAL;
  167         pbkdf->iterations = json_object_get_int(jobj2);
  168         pbkdf->max_memory_kb = 0;
  169         pbkdf->parallel_threads = 0;
  170     } else {
  171         if (!json_object_object_get_ex(jobj_kdf, "time", &jobj2))
  172             return -EINVAL;
  173         pbkdf->iterations = json_object_get_int(jobj2);
  174         if (!json_object_object_get_ex(jobj_kdf, "memory", &jobj2))
  175             return -EINVAL;
  176         pbkdf->max_memory_kb = json_object_get_int(jobj2);
  177         if (!json_object_object_get_ex(jobj_kdf, "cpus", &jobj2))
  178             return -EINVAL;
  179         pbkdf->parallel_threads = json_object_get_int(jobj2);
  180     }
  181 
  182     if (!json_object_object_get_ex(jobj_kdf, "salt", &jobj2))
  183         return -EINVAL;
  184     salt_len = LUKS_SALTSIZE;
  185     if (!base64_decode(json_object_get_string(jobj2),
  186                json_object_get_string_len(jobj2),
  187                salt, &salt_len))
  188         return -EINVAL;
  189     if (salt_len != LUKS_SALTSIZE)
  190         return -EINVAL;
  191 
  192     return 0;
  193 }
  194 
  195 static int luks2_keyslot_set_key(struct crypt_device *cd,
  196     json_object *jobj_keyslot,
  197     const char *password, size_t passwordLen,
  198     const char *volume_key, size_t volume_key_len)
  199 {
  200     struct volume_key *derived_key;
  201     char salt[LUKS_SALTSIZE], cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
  202     char *AfKey = NULL;
  203     const char *af_hash = NULL;
  204     size_t AFEKSize, keyslot_key_len;
  205     json_object *jobj2, *jobj_kdf, *jobj_af, *jobj_area;
  206     uint64_t area_offset;
  207     struct crypt_pbkdf_type pbkdf;
  208     int r;
  209 
  210     if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf) ||
  211         !json_object_object_get_ex(jobj_keyslot, "af", &jobj_af) ||
  212         !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
  213         return -EINVAL;
  214 
  215     /* prevent accidental volume key size change after allocation */
  216     if (!json_object_object_get_ex(jobj_keyslot, "key_size", &jobj2))
  217         return -EINVAL;
  218     if (json_object_get_int(jobj2) != (int)volume_key_len)
  219         return -EINVAL;
  220 
  221     if (!json_object_object_get_ex(jobj_area, "offset", &jobj2))
  222         return -EINVAL;
  223     area_offset = crypt_jobj_get_uint64(jobj2);
  224 
  225     if (!json_object_object_get_ex(jobj_area, "encryption", &jobj2))
  226         return -EINVAL;
  227     r = crypt_parse_name_and_mode(json_object_get_string(jobj2), cipher, NULL, cipher_mode);
  228     if (r < 0)
  229         return r;
  230 
  231     if (!json_object_object_get_ex(jobj_area, "key_size", &jobj2))
  232         return -EINVAL;
  233     keyslot_key_len = json_object_get_int(jobj2);
  234 
  235     if (!json_object_object_get_ex(jobj_af, "hash", &jobj2))
  236         return -EINVAL;
  237     af_hash = json_object_get_string(jobj2);
  238 
  239     if (luks2_keyslot_get_pbkdf_params(jobj_keyslot, &pbkdf, salt))
  240         return -EINVAL;
  241 
  242     /*
  243      * Allocate derived key storage.
  244      */
  245     derived_key = crypt_alloc_volume_key(keyslot_key_len, NULL);
  246     if (!derived_key)
  247         return -ENOMEM;
  248     /*
  249      * Calculate keyslot content, split and store it to keyslot area.
  250      */
  251     log_dbg(cd, "Running keyslot key derivation.");
  252     r = crypt_pbkdf(pbkdf.type, pbkdf.hash, password, passwordLen,
  253             salt, LUKS_SALTSIZE,
  254             derived_key->key, derived_key->keylength,
  255             pbkdf.iterations, pbkdf.max_memory_kb,
  256             pbkdf.parallel_threads);
  257     if (r < 0) {
  258         crypt_free_volume_key(derived_key);
  259         return r;
  260     }
  261 
  262     // FIXME: verity key_size to AFEKSize
  263     AFEKSize = AF_split_sectors(volume_key_len, LUKS_STRIPES) * SECTOR_SIZE;
  264     AfKey = crypt_safe_alloc(AFEKSize);
  265     if (!AfKey) {
  266         crypt_free_volume_key(derived_key);
  267         return -ENOMEM;
  268     }
  269 
  270     r = AF_split(cd, volume_key, AfKey, volume_key_len, LUKS_STRIPES, af_hash);
  271 
  272     if (r == 0) {
  273         log_dbg(cd, "Updating keyslot area [0x%04" PRIx64 "].", area_offset);
  274         /* FIXME: sector_offset should be size_t, fix LUKS_encrypt... accordingly */
  275         r = luks2_encrypt_to_storage(AfKey, AFEKSize, cipher, cipher_mode,
  276                     derived_key, (unsigned)(area_offset / SECTOR_SIZE), cd);
  277     }
  278 
  279     crypt_safe_free(AfKey);
  280     crypt_free_volume_key(derived_key);
  281     if (r < 0)
  282         return r;
  283 
  284     return 0;
  285 }
  286 
  287 static int luks2_keyslot_get_key(struct crypt_device *cd,
  288     json_object *jobj_keyslot,
  289     const char *password, size_t passwordLen,
  290     char *volume_key, size_t volume_key_len)
  291 {
  292     struct volume_key *derived_key;
  293     struct crypt_pbkdf_type pbkdf;
  294     char *AfKey;
  295     size_t AFEKSize;
  296     const char *af_hash = NULL;
  297     char salt[LUKS_SALTSIZE], cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
  298     json_object *jobj2, *jobj_af, *jobj_area;
  299     uint64_t area_offset;
  300     size_t keyslot_key_len;
  301     bool try_serialize_lock = false;
  302     int r;
  303 
  304     if (!json_object_object_get_ex(jobj_keyslot, "af", &jobj_af) ||
  305         !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
  306         return -EINVAL;
  307 
  308     if (luks2_keyslot_get_pbkdf_params(jobj_keyslot, &pbkdf, salt))
  309         return -EINVAL;
  310 
  311     if (!json_object_object_get_ex(jobj_af, "hash", &jobj2))
  312         return -EINVAL;
  313     af_hash = json_object_get_string(jobj2);
  314 
  315     if (!json_object_object_get_ex(jobj_area, "offset", &jobj2))
  316         return -EINVAL;
  317     area_offset = crypt_jobj_get_uint64(jobj2);
  318 
  319     if (!json_object_object_get_ex(jobj_area, "encryption", &jobj2))
  320         return -EINVAL;
  321     r = crypt_parse_name_and_mode(json_object_get_string(jobj2), cipher, NULL, cipher_mode);
  322     if (r < 0)
  323         return r;
  324 
  325     if (!json_object_object_get_ex(jobj_area, "key_size", &jobj2))
  326         return -EINVAL;
  327     keyslot_key_len = json_object_get_int(jobj2);
  328 
  329     /*
  330      * Allocate derived key storage space.
  331      */
  332     derived_key = crypt_alloc_volume_key(keyslot_key_len, NULL);
  333     if (!derived_key)
  334         return -ENOMEM;
  335 
  336     AFEKSize = AF_split_sectors(volume_key_len, LUKS_STRIPES) * SECTOR_SIZE;
  337     AfKey = crypt_safe_alloc(AFEKSize);
  338     if (!AfKey) {
  339         r = -ENOMEM;
  340         goto out;
  341     }
  342 
  343     /*
  344      * If requested, serialize unlocking for memory-hard KDF. Usually NOOP.
  345      */
  346     if (pbkdf.max_memory_kb > MIN_MEMORY_FOR_SERIALIZE_LOCK_KB)
  347         try_serialize_lock = true;
  348     if (try_serialize_lock && (r = crypt_serialize_lock(cd)))
  349         goto out;
  350 
  351     /*
  352      * Calculate derived key, decrypt keyslot content and merge it.
  353      */
  354     log_dbg(cd, "Running keyslot key derivation.");
  355     r = crypt_pbkdf(pbkdf.type, pbkdf.hash, password, passwordLen,
  356             salt, LUKS_SALTSIZE,
  357             derived_key->key, derived_key->keylength,
  358             pbkdf.iterations, pbkdf.max_memory_kb,
  359             pbkdf.parallel_threads);
  360 
  361     if (try_serialize_lock)
  362         crypt_serialize_unlock(cd);
  363 
  364     if (r == 0) {
  365         log_dbg(cd, "Reading keyslot area [0x%04" PRIx64 "].", area_offset);
  366         /* FIXME: sector_offset should be size_t, fix LUKS_decrypt... accordingly */
  367         r = luks2_decrypt_from_storage(AfKey, AFEKSize, cipher, cipher_mode,
  368                       derived_key, (unsigned)(area_offset / SECTOR_SIZE), cd);
  369     }
  370 
  371     if (r == 0)
  372         r = AF_merge(cd, AfKey, volume_key, volume_key_len, LUKS_STRIPES, af_hash);
  373 
  374 out:
  375     crypt_free_volume_key(derived_key);
  376     crypt_safe_free(AfKey);
  377 
  378     return r;
  379 }
  380 
  381 /*
  382  * currently we support update of only:
  383  *
  384  * - af hash function
  385  * - kdf params
  386  */
  387 static int luks2_keyslot_update_json(struct crypt_device *cd,
  388     json_object *jobj_keyslot,
  389     const struct luks2_keyslot_params *params)
  390 {
  391     const struct crypt_pbkdf_type *pbkdf;
  392     json_object *jobj_af, *jobj_area, *jobj_kdf;
  393     char salt[LUKS_SALTSIZE], *salt_base64 = NULL;
  394     int r;
  395 
  396     /* jobj_keyslot is not yet validated */
  397 
  398     if (!json_object_object_get_ex(jobj_keyslot, "af", &jobj_af) ||
  399         !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
  400         return -EINVAL;
  401 
  402     /* update area encryption parameters */
  403     json_object_object_add(jobj_area, "encryption", json_object_new_string(params->area.raw.encryption));
  404     json_object_object_add(jobj_area, "key_size", json_object_new_int(params->area.raw.key_size));
  405 
  406     pbkdf = crypt_get_pbkdf_type(cd);
  407     if (!pbkdf)
  408         return -EINVAL;
  409 
  410     r = crypt_benchmark_pbkdf_internal(cd, CONST_CAST(struct crypt_pbkdf_type *)pbkdf, params->area.raw.key_size);
  411     if (r < 0)
  412         return r;
  413 
  414     /* refresh whole 'kdf' object */
  415     jobj_kdf = json_object_new_object();
  416     if (!jobj_kdf)
  417         return -ENOMEM;
  418     json_object_object_add(jobj_kdf, "type", json_object_new_string(pbkdf->type));
  419     if (!strcmp(pbkdf->type, CRYPT_KDF_PBKDF2)) {
  420         json_object_object_add(jobj_kdf, "hash", json_object_new_string(pbkdf->hash));
  421         json_object_object_add(jobj_kdf, "iterations", json_object_new_int(pbkdf->iterations));
  422     } else {
  423         json_object_object_add(jobj_kdf, "time", json_object_new_int(pbkdf->iterations));
  424         json_object_object_add(jobj_kdf, "memory", json_object_new_int(pbkdf->max_memory_kb));
  425         json_object_object_add(jobj_kdf, "cpus", json_object_new_int(pbkdf->parallel_threads));
  426     }
  427     json_object_object_add(jobj_keyslot, "kdf", jobj_kdf);
  428 
  429     /*
  430      * Regenerate salt and add it in 'kdf' object
  431      */
  432     r = crypt_random_get(cd, salt, LUKS_SALTSIZE, CRYPT_RND_SALT);
  433     if (r < 0)
  434         return r;
  435     base64_encode_alloc(salt, LUKS_SALTSIZE, &salt_base64);
  436     if (!salt_base64)
  437         return -ENOMEM;
  438     json_object_object_add(jobj_kdf, "salt", json_object_new_string(salt_base64));
  439     free(salt_base64);
  440 
  441     /* update 'af' hash */
  442     json_object_object_add(jobj_af, "hash", json_object_new_string(params->af.luks1.hash));
  443 
  444     JSON_DBG(cd, jobj_keyslot, "Keyslot JSON:");
  445     return 0;
  446 }
  447 
  448 static int luks2_keyslot_alloc(struct crypt_device *cd,
  449     int keyslot,
  450     size_t volume_key_len,
  451     const struct luks2_keyslot_params *params)
  452 {
  453     struct luks2_hdr *hdr;
  454     uint64_t area_offset, area_length;
  455     json_object *jobj_keyslots, *jobj_keyslot, *jobj_af, *jobj_area;
  456     int r;
  457 
  458     log_dbg(cd, "Trying to allocate LUKS2 keyslot %d.", keyslot);
  459 
  460     if (!params || params->area_type != LUKS2_KEYSLOT_AREA_RAW ||
  461         params->af_type != LUKS2_KEYSLOT_AF_LUKS1) {
  462         log_dbg(cd, "Invalid LUKS2 keyslot parameters.");
  463         return -EINVAL;
  464     }
  465 
  466     if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
  467         return -EINVAL;
  468 
  469     if (keyslot == CRYPT_ANY_SLOT)
  470         keyslot = LUKS2_keyslot_find_empty(cd, hdr, 0);
  471 
  472     if (keyslot < 0 || keyslot >= LUKS2_KEYSLOTS_MAX)
  473         return -ENOMEM;
  474 
  475     if (LUKS2_get_keyslot_jobj(hdr, keyslot)) {
  476         log_dbg(cd, "Cannot modify already active keyslot %d.", keyslot);
  477         return -EINVAL;
  478     }
  479 
  480     if (!json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots))
  481         return -EINVAL;
  482 
  483     r = LUKS2_find_area_gap(cd, hdr, volume_key_len, &area_offset, &area_length);
  484     if (r < 0) {
  485         log_err(cd, _("No space for new keyslot."));
  486         return r;
  487     }
  488 
  489     jobj_keyslot = json_object_new_object();
  490     json_object_object_add(jobj_keyslot, "type", json_object_new_string("luks2"));
  491     json_object_object_add(jobj_keyslot, "key_size", json_object_new_int(volume_key_len));
  492 
  493     /* AF object */
  494     jobj_af = json_object_new_object();
  495     json_object_object_add(jobj_af, "type", json_object_new_string("luks1"));
  496     json_object_object_add(jobj_af, "stripes", json_object_new_int(params->af.luks1.stripes));
  497     json_object_object_add(jobj_keyslot, "af", jobj_af);
  498 
  499     /* Area object */
  500     jobj_area = json_object_new_object();
  501     json_object_object_add(jobj_area, "type", json_object_new_string("raw"));
  502     json_object_object_add(jobj_area, "offset", crypt_jobj_new_uint64(area_offset));
  503     json_object_object_add(jobj_area, "size", crypt_jobj_new_uint64(area_length));
  504     json_object_object_add(jobj_keyslot, "area", jobj_area);
  505 
  506     json_object_object_add_by_uint(jobj_keyslots, keyslot, jobj_keyslot);
  507 
  508     r = luks2_keyslot_update_json(cd, jobj_keyslot, params);
  509 
  510     if (!r && LUKS2_check_json_size(cd, hdr)) {
  511         log_dbg(cd, "Not enough space in header json area for new keyslot.");
  512         r = -ENOSPC;
  513     }
  514 
  515     if (r)
  516         json_object_object_del_by_uint(jobj_keyslots, keyslot);
  517 
  518     return r;
  519 }
  520 
  521 static int luks2_keyslot_open(struct crypt_device *cd,
  522     int keyslot,
  523     const char *password,
  524     size_t password_len,
  525     char *volume_key,
  526     size_t volume_key_len)
  527 {
  528     struct luks2_hdr *hdr;
  529     json_object *jobj_keyslot;
  530 
  531     log_dbg(cd, "Trying to open LUKS2 keyslot %d.", keyslot);
  532 
  533     if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
  534         return -EINVAL;
  535 
  536     jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
  537     if (!jobj_keyslot)
  538         return -EINVAL;
  539 
  540     return luks2_keyslot_get_key(cd, jobj_keyslot,
  541                      password, password_len,
  542                      volume_key, volume_key_len);
  543 }
  544 
  545 /*
  546  * This function must not modify json.
  547  * It's called after luks2 keyslot validation.
  548  */
  549 static int luks2_keyslot_store(struct crypt_device *cd,
  550     int keyslot,
  551     const char *password,
  552     size_t password_len,
  553     const char *volume_key,
  554     size_t volume_key_len)
  555 {
  556     struct luks2_hdr *hdr;
  557     json_object *jobj_keyslot;
  558     int r;
  559 
  560     log_dbg(cd, "Calculating attributes for LUKS2 keyslot %d.", keyslot);
  561 
  562     if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
  563         return -EINVAL;
  564 
  565     jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
  566     if (!jobj_keyslot)
  567         return -EINVAL;
  568 
  569     r = LUKS2_device_write_lock(cd, hdr, crypt_metadata_device(cd));
  570     if(r)
  571         return r;
  572 
  573     r = luks2_keyslot_set_key(cd, jobj_keyslot,
  574                   password, password_len,
  575                   volume_key, volume_key_len);
  576     if (!r)
  577         r = LUKS2_hdr_write(cd, hdr);
  578 
  579     device_write_unlock(cd, crypt_metadata_device(cd));
  580 
  581     return r < 0 ? r : keyslot;
  582 }
  583 
  584 static int luks2_keyslot_wipe(struct crypt_device *cd, int keyslot)
  585 {
  586     struct luks2_hdr *hdr;
  587 
  588     if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
  589         return -EINVAL;
  590 
  591     /* Remove any reference of deleted keyslot from digests and tokens */
  592     LUKS2_digest_assign(cd, hdr, keyslot, CRYPT_ANY_DIGEST, 0, 0);
  593     LUKS2_token_assign(cd, hdr, keyslot, CRYPT_ANY_TOKEN, 0, 0);
  594 
  595     return 0;
  596 }
  597 
  598 static int luks2_keyslot_dump(struct crypt_device *cd, int keyslot)
  599 {
  600     json_object *jobj_keyslot, *jobj1, *jobj_kdf, *jobj_af, *jobj_area;
  601 
  602     jobj_keyslot = LUKS2_get_keyslot_jobj(crypt_get_hdr(cd, CRYPT_LUKS2), keyslot);
  603     if (!jobj_keyslot)
  604         return -EINVAL;
  605 
  606     if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf) ||
  607         !json_object_object_get_ex(jobj_keyslot, "af", &jobj_af) ||
  608         !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
  609         return -EINVAL;
  610 
  611     json_object_object_get_ex(jobj_area, "encryption", &jobj1);
  612     log_std(cd, "\tCipher:     %s\n", json_object_get_string(jobj1));
  613 
  614     json_object_object_get_ex(jobj_area, "key_size", &jobj1);
  615     log_std(cd, "\tCipher key: %u bits\n", crypt_jobj_get_uint32(jobj1) * 8);
  616 
  617     json_object_object_get_ex(jobj_kdf, "type", &jobj1);
  618     log_std(cd, "\tPBKDF:      %s\n", json_object_get_string(jobj1));
  619 
  620     if (!strcmp(json_object_get_string(jobj1), CRYPT_KDF_PBKDF2)) {
  621         json_object_object_get_ex(jobj_kdf, "hash", &jobj1);
  622         log_std(cd, "\tHash:       %s\n", json_object_get_string(jobj1));
  623 
  624         json_object_object_get_ex(jobj_kdf, "iterations", &jobj1);
  625         log_std(cd, "\tIterations: %" PRIu64 "\n", crypt_jobj_get_uint64(jobj1));
  626     } else {
  627         json_object_object_get_ex(jobj_kdf, "time", &jobj1);
  628         log_std(cd, "\tTime cost:  %" PRIu64 "\n", json_object_get_int64(jobj1));
  629 
  630         json_object_object_get_ex(jobj_kdf, "memory", &jobj1);
  631         log_std(cd, "\tMemory:     %" PRIu64 "\n", json_object_get_int64(jobj1));
  632 
  633         json_object_object_get_ex(jobj_kdf, "cpus", &jobj1);
  634         log_std(cd, "\tThreads:    %" PRIu64 "\n", json_object_get_int64(jobj1));
  635     }
  636     json_object_object_get_ex(jobj_kdf, "salt", &jobj1);
  637     log_std(cd, "\tSalt:       ");
  638     hexprint_base64(cd, jobj1, " ", "            ");
  639 
  640 
  641     json_object_object_get_ex(jobj_af, "stripes", &jobj1);
  642     log_std(cd, "\tAF stripes: %u\n", json_object_get_int(jobj1));
  643 
  644     json_object_object_get_ex(jobj_af, "hash", &jobj1);
  645     log_std(cd, "\tAF hash:    %s\n", json_object_get_string(jobj1));
  646 
  647     json_object_object_get_ex(jobj_area, "offset", &jobj1);
  648     log_std(cd, "\tArea offset:%" PRIu64 " [bytes]\n", crypt_jobj_get_uint64(jobj1));
  649 
  650     json_object_object_get_ex(jobj_area, "size", &jobj1);
  651     log_std(cd, "\tArea length:%" PRIu64 " [bytes]\n", crypt_jobj_get_uint64(jobj1));
  652 
  653     return 0;
  654 }
  655 
  656 static int luks2_keyslot_validate(struct crypt_device *cd, json_object *jobj_keyslot)
  657 {
  658     json_object *jobj_kdf, *jobj_af, *jobj_area, *jobj1;
  659     const char *type;
  660     int count;
  661 
  662     if (!jobj_keyslot)
  663         return -EINVAL;
  664 
  665     if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf) ||
  666         !json_object_object_get_ex(jobj_keyslot, "af", &jobj_af) ||
  667         !json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
  668         return -EINVAL;
  669 
  670     count = json_object_object_length(jobj_kdf);
  671 
  672     jobj1 = json_contains(cd, jobj_kdf, "", "kdf section", "type", json_type_string);
  673     if (!jobj1)
  674         return -EINVAL;
  675     type = json_object_get_string(jobj1);
  676 
  677     if (!strcmp(type, CRYPT_KDF_PBKDF2)) {
  678         if (count != 4 || /* type, salt, hash, iterations only */
  679             !json_contains(cd, jobj_kdf, "kdf type", type, "hash", json_type_string) ||
  680             !json_contains(cd, jobj_kdf, "kdf type", type, "iterations", json_type_int) ||
  681             !json_contains(cd, jobj_kdf, "kdf type", type, "salt", json_type_string))
  682             return -EINVAL;
  683     } else if (!strcmp(type, CRYPT_KDF_ARGON2I) || !strcmp(type, CRYPT_KDF_ARGON2ID)) {
  684         if (count != 5 || /* type, salt, time, memory, cpus only */
  685             !json_contains(cd, jobj_kdf, "kdf type", type, "time", json_type_int) ||
  686             !json_contains(cd, jobj_kdf, "kdf type", type, "memory", json_type_int) ||
  687             !json_contains(cd, jobj_kdf, "kdf type", type, "cpus", json_type_int) ||
  688             !json_contains(cd, jobj_kdf, "kdf type", type, "salt", json_type_string))
  689             return -EINVAL;
  690     }
  691 
  692     if (!json_object_object_get_ex(jobj_af, "type", &jobj1))
  693         return -EINVAL;
  694     if (!strcmp(json_object_get_string(jobj1), "luks1")) {
  695         if (!json_contains(cd, jobj_af, "", "luks1 af", "hash", json_type_string) ||
  696             !json_contains(cd, jobj_af, "", "luks1 af", "stripes", json_type_int))
  697             return -EINVAL;
  698     } else
  699         return -EINVAL;
  700 
  701     // FIXME check numbered
  702     if (!json_object_object_get_ex(jobj_area, "type", &jobj1))
  703         return -EINVAL;
  704     if (!strcmp(json_object_get_string(jobj1), "raw")) {
  705         if (!json_contains(cd, jobj_area, "area", "raw type", "encryption", json_type_string) ||
  706             !json_contains(cd, jobj_area, "area", "raw type", "key_size", json_type_int) ||
  707             !json_contains(cd, jobj_area, "area", "raw type", "offset", json_type_string) ||
  708             !json_contains(cd, jobj_area, "area", "raw type", "size", json_type_string))
  709             return -EINVAL;
  710     } else
  711         return -EINVAL;
  712 
  713     return 0;
  714 }
  715 
  716 static int luks2_keyslot_update(struct crypt_device *cd,
  717     int keyslot,
  718     const struct luks2_keyslot_params *params)
  719 {
  720     struct luks2_hdr *hdr;
  721     json_object *jobj_keyslot;
  722     int r;
  723 
  724     log_dbg(cd, "Updating LUKS2 keyslot %d.", keyslot);
  725 
  726     if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
  727         return -EINVAL;
  728 
  729     jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
  730     if (!jobj_keyslot)
  731         return -EINVAL;
  732 
  733     r = luks2_keyslot_update_json(cd, jobj_keyslot, params);
  734 
  735     if (!r && LUKS2_check_json_size(cd, hdr)) {
  736         log_dbg(cd, "Not enough space in header json area for updated keyslot %d.", keyslot);
  737         r = -ENOSPC;
  738     }
  739 
  740     return r;
  741 }
  742 
  743 static void luks2_keyslot_repair(struct crypt_device *cd __attribute__((unused)), json_object *jobj_keyslot)
  744 {
  745     const char *type;
  746     json_object *jobj_kdf, *jobj_type;
  747 
  748     if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf) ||
  749         !json_object_is_type(jobj_kdf, json_type_object))
  750         return;
  751 
  752     if (!json_object_object_get_ex(jobj_kdf, "type", &jobj_type) ||
  753         !json_object_is_type(jobj_type, json_type_string))
  754         return;
  755 
  756     type = json_object_get_string(jobj_type);
  757 
  758     if (!strcmp(type, CRYPT_KDF_PBKDF2)) {
  759         /* type, salt, hash, iterations only */
  760         json_object_object_foreach(jobj_kdf, key, val) {
  761             UNUSED(val);
  762             if (!strcmp(key, "type") || !strcmp(key, "salt") ||
  763                 !strcmp(key, "hash") || !strcmp(key, "iterations"))
  764                     continue;
  765             json_object_object_del(jobj_kdf, key);
  766         }
  767     } else if (!strcmp(type, CRYPT_KDF_ARGON2I) || !strcmp(type, CRYPT_KDF_ARGON2ID)) {
  768         /* type, salt, time, memory, cpus only */
  769         json_object_object_foreach(jobj_kdf, key, val) {
  770             UNUSED(val);
  771             if (!strcmp(key, "type") || !strcmp(key, "salt") ||
  772                 !strcmp(key, "time") || !strcmp(key, "memory") ||
  773                 !strcmp(key, "cpus"))
  774                     continue;
  775             json_object_object_del(jobj_kdf, key);
  776         }
  777     }
  778 }
  779 
  780 const keyslot_handler luks2_keyslot = {
  781     .name  = "luks2",
  782     .alloc  = luks2_keyslot_alloc,
  783     .update = luks2_keyslot_update,
  784     .open  = luks2_keyslot_open,
  785     .store = luks2_keyslot_store,
  786     .wipe  = luks2_keyslot_wipe,
  787     .dump  = luks2_keyslot_dump,
  788     .validate = luks2_keyslot_validate,
  789     .repair = luks2_keyslot_repair
  790 };