+ if (volume_key_size > 1024) {
+ log_err(cd, _("Invalid key size."));
+ return -EINVAL;
+ }
+
+ if (uuid) {
+ log_err(cd, _("UUID is not supported for this crypt type."));
+ return -EINVAL;
+ }
+
+ if (cd->metadata_device) {
+ log_err(cd, _("Detached metadata device is not supported for this crypt type."));
+ return -EINVAL;
+ }
+
+ /* For compatibility with old params structure */
+ if (!sector_size)
+ sector_size = SECTOR_SIZE;
+
+ if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE ||
+ NOTPOW2(sector_size)) {
+ log_err(cd, _("Unsupported encryption sector size."));
+ return -EINVAL;
+ }
+
+ if (sector_size > SECTOR_SIZE && !device_size(cd->device, &dev_size)) {
+ if (params && params->offset)
+ dev_size -= (params->offset * SECTOR_SIZE);
+ if (dev_size % sector_size) {
+ log_err(cd, _("Device size is not aligned to requested sector size."));
+ return -EINVAL;
+ }
+ device_set_block_size(crypt_data_device(cd), sector_size);
+ }
+
+ if (!(cd->type = strdup(CRYPT_PLAIN)))
+ return -ENOMEM;
+
+ cd->u.plain.key_size = volume_key_size;
+ cd->volume_key = crypt_alloc_volume_key(volume_key_size, NULL);
+ if (!cd->volume_key)
+ return -ENOMEM;
+
+ if (asprintf(&cd->u.plain.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
+ cd->u.plain.cipher_spec = NULL;
+ return -ENOMEM;
+ }
+ cd->u.plain.cipher = strdup(cipher);
+ cd->u.plain.cipher_mode = cd->u.plain.cipher_spec + strlen(cipher) + 1;
+
+ if (params && params->hash)
+ cd->u.plain.hdr.hash = strdup(params->hash);
+
+ cd->u.plain.hdr.offset = params ? params->offset : 0;
+ cd->u.plain.hdr.skip = params ? params->skip : 0;
+ cd->u.plain.hdr.size = params ? params->size : 0;
+ cd->u.plain.hdr.sector_size = sector_size;
+
+ if (!cd->u.plain.cipher)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static int _crypt_format_luks1(struct crypt_device *cd,
+ const char *cipher,
+ const char *cipher_mode,
+ const char *uuid,
+ const char *volume_key,
+ size_t volume_key_size,
+ struct crypt_params_luks1 *params)
+{
+ int r;
+ unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
+ unsigned long alignment_offset = 0;
+ uint64_t dev_size;
+
+ if (!cipher || !cipher_mode)
+ return -EINVAL;
+
+ if (!crypt_metadata_device(cd)) {
+ log_err(cd, _("Can't format LUKS without device."));
+ return -EINVAL;
+ }
+
+ if (params && cd->data_offset && params->data_alignment &&
+ (cd->data_offset % params->data_alignment)) {
+ log_err(cd, _("Requested data alignment is not compatible with data offset."));
+ return -EINVAL;
+ }
+
+ if (!(cd->type = strdup(CRYPT_LUKS1)))
+ return -ENOMEM;
+
+ if (volume_key)
+ cd->volume_key = crypt_alloc_volume_key(volume_key_size,
+ volume_key);
+ else
+ cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
+
+ if (!cd->volume_key)
+ return -ENOMEM;
+
+ if (verify_pbkdf_params(cd, &cd->pbkdf)) {
+ r = init_pbkdf_type(cd, NULL, CRYPT_LUKS1);
+ if (r)
+ return r;
+ }
+
+ if (params && params->hash && strcmp(params->hash, cd->pbkdf.hash)) {
+ free(CONST_CAST(void*)cd->pbkdf.hash);
+ cd->pbkdf.hash = strdup(params->hash);
+ if (!cd->pbkdf.hash)
+ return -ENOMEM;
+ }
+
+ if (params && params->data_device) {
+ if (!cd->metadata_device)
+ cd->metadata_device = cd->device;
+ else
+ device_free(cd, cd->device);
+ cd->device = NULL;
+ if (device_alloc(cd, &cd->device, params->data_device) < 0)
+ return -ENOMEM;
+ }
+
+ if (params && cd->metadata_device) {
+ /* For detached header the alignment is used directly as data offset */
+ if (!cd->data_offset)
+ cd->data_offset = params->data_alignment;
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ } else if (params && params->data_alignment) {
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ } else
+ device_topology_alignment(cd, cd->device,
+ &required_alignment,
+ &alignment_offset, DEFAULT_DISK_ALIGNMENT);
+
+ r = LUKS_check_cipher(cd, volume_key_size, cipher, cipher_mode);
+ if (r < 0)
+ return r;
+
+ r = LUKS_generate_phdr(&cd->u.luks1.hdr, cd->volume_key, cipher, cipher_mode,
+ cd->pbkdf.hash, uuid,
+ cd->data_offset * SECTOR_SIZE,
+ alignment_offset, required_alignment, cd);
+ if (r < 0)
+ return r;
+
+ r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
+ if (r < 0)
+ return r;
+
+
+ if (asprintf(&cd->u.luks1.cipher_spec, "%s-%s", cipher, cipher_mode) < 0) {
+ cd->u.luks1.cipher_spec = NULL;
+ return -ENOMEM;
+ }
+
+ r = LUKS_wipe_header_areas(&cd->u.luks1.hdr, cd);
+ if (r < 0) {
+ free(cd->u.luks1.cipher_spec);
+ log_err(cd, _("Cannot wipe header on device %s."),
+ mdata_device_path(cd));
+ return r;
+ }
+
+ r = LUKS_write_phdr(&cd->u.luks1.hdr, cd);
+ if (r) {
+ free(cd->u.luks1.cipher_spec);
+ return r;
+ }
+
+ if (!device_size(crypt_data_device(cd), &dev_size) &&
+ dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
+ log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
+ device_path(crypt_data_device(cd)));
+
+ return 0;
+}
+
+static int _crypt_format_luks2(struct crypt_device *cd,
+ const char *cipher,
+ const char *cipher_mode,
+ const char *uuid,
+ const char *volume_key,
+ size_t volume_key_size,
+ struct crypt_params_luks2 *params,
+ bool sector_size_autodetect)
+{
+ int r, integrity_key_size = 0;
+ unsigned long required_alignment = DEFAULT_DISK_ALIGNMENT;
+ unsigned long alignment_offset = 0;
+ unsigned int sector_size;
+ const char *integrity = params ? params->integrity : NULL;
+ uint64_t dev_size;
+ uint32_t dmc_flags;
+
+ cd->u.luks2.hdr.jobj = NULL;
+ cd->u.luks2.keyslot_cipher = NULL;
+
+ if (!cipher || !cipher_mode)
+ return -EINVAL;
+
+ if (!crypt_metadata_device(cd)) {
+ log_err(cd, _("Can't format LUKS without device."));
+ return -EINVAL;
+ }
+
+ if (params && cd->data_offset && params->data_alignment &&
+ (cd->data_offset % params->data_alignment)) {
+ log_err(cd, _("Requested data alignment is not compatible with data offset."));
+ return -EINVAL;
+ }
+
+ if (params && params->sector_size)
+ sector_size_autodetect = false;
+
+ if (params && params->data_device) {
+ if (!cd->metadata_device)
+ cd->metadata_device = cd->device;
+ else
+ device_free(cd, cd->device);
+ cd->device = NULL;
+ if (device_alloc(cd, &cd->device, params->data_device) < 0)
+ return -ENOMEM;
+ }
+
+ if (sector_size_autodetect) {
+ sector_size = device_optimal_encryption_sector_size(cd, crypt_data_device(cd));
+ log_dbg(cd, "Auto-detected optimal encryption sector size for device %s is %d bytes.",
+ device_path(crypt_data_device(cd)), sector_size);
+ } else
+ sector_size = params ? params->sector_size : SECTOR_SIZE;
+
+ if (sector_size < SECTOR_SIZE || sector_size > MAX_SECTOR_SIZE ||
+ NOTPOW2(sector_size)) {
+ log_err(cd, _("Unsupported encryption sector size."));
+ return -EINVAL;
+ }
+ if (sector_size != SECTOR_SIZE && !dm_flags(cd, DM_CRYPT, &dmc_flags) &&
+ !(dmc_flags & DM_SECTOR_SIZE_SUPPORTED)) {
+ if (sector_size_autodetect) {
+ log_dbg(cd, "dm-crypt does not support encryption sector size option. Reverting to 512 bytes.");
+ sector_size = SECTOR_SIZE;
+ } else
+ log_std(cd, _("WARNING: The device activation will fail, dm-crypt is missing "
+ "support for requested encryption sector size.\n"));
+ }
+
+ if (integrity) {
+ if (params->integrity_params) {
+ /* Standalone dm-integrity must not be used */
+ if (params->integrity_params->integrity ||
+ params->integrity_params->integrity_key_size)
+ return -EINVAL;
+ /* FIXME: journal encryption and MAC is here not yet supported */
+ if (params->integrity_params->journal_crypt ||
+ params->integrity_params->journal_integrity)
+ return -ENOTSUP;
+ }
+ if (!INTEGRITY_tag_size(integrity, cipher, cipher_mode)) {
+ if (!strcmp(integrity, "none"))
+ integrity = NULL;
+ else
+ return -EINVAL;
+ }
+ integrity_key_size = INTEGRITY_key_size(integrity);
+ if ((integrity_key_size < 0) || (integrity_key_size >= (int)volume_key_size)) {
+ log_err(cd, _("Volume key is too small for encryption with integrity extensions."));
+ return -EINVAL;
+ }
+ }
+
+ r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
+ if (r < 0)
+ return r;
+
+ if (!(cd->type = strdup(CRYPT_LUKS2)))
+ return -ENOMEM;
+
+ if (volume_key)
+ cd->volume_key = crypt_alloc_volume_key(volume_key_size,
+ volume_key);
+ else
+ cd->volume_key = crypt_generate_volume_key(cd, volume_key_size);
+
+ if (!cd->volume_key)
+ return -ENOMEM;
+
+ if (params && params->pbkdf)
+ r = crypt_set_pbkdf_type(cd, params->pbkdf);
+ else if (verify_pbkdf_params(cd, &cd->pbkdf))
+ r = init_pbkdf_type(cd, NULL, CRYPT_LUKS2);
+
+ if (r < 0)
+ return r;
+
+ if (params && cd->metadata_device) {
+ /* For detached header the alignment is used directly as data offset */
+ if (!cd->data_offset)
+ cd->data_offset = params->data_alignment;
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ } else if (params && params->data_alignment) {
+ required_alignment = params->data_alignment * SECTOR_SIZE;
+ } else
+ device_topology_alignment(cd, cd->device,
+ &required_alignment,
+ &alignment_offset, DEFAULT_DISK_ALIGNMENT);
+
+ r = device_size(crypt_data_device(cd), &dev_size);
+ if (r < 0)
+ goto out;
+
+ if (sector_size_autodetect) {
+ if (cd->data_offset && MISALIGNED(cd->data_offset, sector_size)) {
+ log_dbg(cd, "Data offset not aligned to sector size. Reverting to 512 bytes.");
+ sector_size = SECTOR_SIZE;
+ } else if (MISALIGNED(dev_size - (uint64_t)required_alignment - (uint64_t)alignment_offset, sector_size)) {
+ /* underflow does not affect misalignment checks */
+ log_dbg(cd, "Device size is not aligned to sector size. Reverting to 512 bytes.");
+ sector_size = SECTOR_SIZE;
+ }
+ }
+
+ /* FIXME: allow this later also for normal ciphers (check AF_ALG availability. */
+ if (integrity && !integrity_key_size) {
+ r = crypt_cipher_check_kernel(cipher, cipher_mode, integrity, volume_key_size);
+ if (r < 0) {
+ log_err(cd, _("Cipher %s-%s (key size %zd bits) is not available."),
+ cipher, cipher_mode, volume_key_size * 8);
+ goto out;
+ }
+ }
+
+ if ((!integrity || integrity_key_size) && !crypt_cipher_wrapped_key(cipher, cipher_mode) &&
+ !INTEGRITY_tag_size(NULL, cipher, cipher_mode)) {
+ r = LUKS_check_cipher(cd, volume_key_size - integrity_key_size,
+ cipher, cipher_mode);
+ if (r < 0)
+ goto out;
+ }
+
+ r = LUKS2_generate_hdr(cd, &cd->u.luks2.hdr, cd->volume_key,
+ cipher, cipher_mode,
+ integrity, uuid,
+ sector_size,
+ cd->data_offset * SECTOR_SIZE,
+ alignment_offset,
+ required_alignment,
+ cd->metadata_size, cd->keyslots_size);
+ if (r < 0)
+ goto out;
+
+ if (cd->metadata_size && (cd->metadata_size != LUKS2_metadata_size(&cd->u.luks2.hdr)))
+ log_std(cd, _("WARNING: LUKS2 metadata size changed to %" PRIu64 " bytes.\n"),
+ LUKS2_metadata_size(&cd->u.luks2.hdr));
+
+ if (cd->keyslots_size && (cd->keyslots_size != LUKS2_keyslots_size(&cd->u.luks2.hdr)))
+ log_std(cd, _("WARNING: LUKS2 keyslots area size changed to %" PRIu64 " bytes.\n"),
+ LUKS2_keyslots_size(&cd->u.luks2.hdr));
+
+ if (!integrity && sector_size > SECTOR_SIZE) {
+ dev_size -= (crypt_get_data_offset(cd) * SECTOR_SIZE);
+ if (dev_size % sector_size) {
+ log_err(cd, _("Device size is not aligned to requested sector size."));
+ r = -EINVAL;
+ goto out;
+ }
+ }
+
+ if (params && (params->label || params->subsystem)) {
+ r = LUKS2_hdr_labels(cd, &cd->u.luks2.hdr,
+ params->label, params->subsystem, 0);
+ if (r < 0)
+ goto out;
+ }
+
+ device_set_block_size(crypt_data_device(cd), sector_size);
+
+ r = LUKS2_wipe_header_areas(cd, &cd->u.luks2.hdr, cd->metadata_device != NULL);
+ if (r < 0) {
+ log_err(cd, _("Cannot wipe header on device %s."),
+ mdata_device_path(cd));
+ if (dev_size < LUKS2_hdr_and_areas_size(&cd->u.luks2.hdr))
+ log_err(cd, _("Device %s is too small."), device_path(crypt_metadata_device(cd)));
+ goto out;
+ }
+
+ /* Wipe integrity superblock and create integrity superblock */
+ if (crypt_get_integrity_tag_size(cd)) {
+ r = crypt_wipe_device(cd, crypt_data_device(cd), CRYPT_WIPE_ZERO,
+ crypt_get_data_offset(cd) * SECTOR_SIZE,
+ 8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
+ if (r < 0) {
+ if (r == -EBUSY)
+ log_err(cd, _("Cannot format device %s in use."),
+ data_device_path(cd));
+ else if (r == -EACCES) {
+ log_err(cd, _("Cannot format device %s, permission denied."),
+ data_device_path(cd));
+ r = -EINVAL;
+ } else
+ log_err(cd, _("Cannot wipe header on device %s."),
+ data_device_path(cd));
+
+ goto out;
+ }
+
+ r = INTEGRITY_format(cd, params ? params->integrity_params : NULL, NULL, NULL);
+ if (r)
+ log_err(cd, _("Cannot format integrity for device %s."),
+ data_device_path(cd));
+ }
+
+ if (r < 0)
+ goto out;
+
+ /* override sequence id check with format */
+ r = LUKS2_hdr_write_force(cd, &cd->u.luks2.hdr);
+ if (r < 0) {
+ if (r == -EBUSY)
+ log_err(cd, _("Cannot format device %s in use."),
+ mdata_device_path(cd));
+ else if (r == -EACCES) {
+ log_err(cd, _("Cannot format device %s, permission denied."),
+ mdata_device_path(cd));
+ r = -EINVAL;
+ } else
+ log_err(cd, _("Cannot format device %s."),
+ mdata_device_path(cd));
+ }
+
+out:
+ if (r) {
+ LUKS2_hdr_free(cd, &cd->u.luks2.hdr);
+ return r;
+ }
+
+ /* Device size can be larger now if it is a file container */
+ if (!device_size(crypt_data_device(cd), &dev_size) &&
+ dev_size <= (crypt_get_data_offset(cd) * SECTOR_SIZE))
+ log_std(cd, _("Device %s is too small for activation, there is no remaining space for data.\n"),
+ device_path(crypt_data_device(cd)));
+
+ return 0;
+}
+
+static int _crypt_format_loopaes(struct crypt_device *cd,
+ const char *cipher,
+ const char *uuid,
+ size_t volume_key_size,
+ struct crypt_params_loopaes *params)
+{
+ if (!crypt_metadata_device(cd)) {
+ log_err(cd, _("Can't format LOOPAES without device."));
+ return -EINVAL;
+ }
+
+ if (volume_key_size > 1024) {
+ log_err(cd, _("Invalid key size."));
+ return -EINVAL;
+ }
+
+ if (uuid) {
+ log_err(cd, _("UUID is not supported for this crypt type."));
+ return -EINVAL;
+ }
+
+ if (cd->metadata_device) {
+ log_err(cd, _("Detached metadata device is not supported for this crypt type."));
+ return -EINVAL;
+ }
+
+ if (!(cd->type = strdup(CRYPT_LOOPAES)))
+ return -ENOMEM;
+
+ cd->u.loopaes.key_size = volume_key_size;
+
+ cd->u.loopaes.cipher = strdup(cipher ?: DEFAULT_LOOPAES_CIPHER);
+
+ if (params && params->hash)
+ cd->u.loopaes.hdr.hash = strdup(params->hash);
+
+ cd->u.loopaes.hdr.offset = params ? params->offset : 0;
+ cd->u.loopaes.hdr.skip = params ? params->skip : 0;
+
+ return 0;
+}
+
+static int _crypt_format_verity(struct crypt_device *cd,
+ const char *uuid,
+ struct crypt_params_verity *params)
+{
+ int r = 0, hash_size;
+ uint64_t data_device_size, hash_blocks_size;
+ struct device *fec_device = NULL;
+ char *fec_device_path = NULL, *hash_name = NULL, *root_hash = NULL, *salt = NULL;
+
+ if (!crypt_metadata_device(cd)) {
+ log_err(cd, _("Can't format VERITY without device."));
+ return -EINVAL;
+ }
+
+ if (!params)
+ return -EINVAL;
+
+ if (!params->data_device && !cd->metadata_device)
+ return -EINVAL;
+
+ if (params->hash_type > VERITY_MAX_HASH_TYPE) {
+ log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
+ return -EINVAL;
+ }
+
+ if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
+ VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
+ log_err(cd, _("Unsupported VERITY block size."));
+ return -EINVAL;
+ }
+
+ if (MISALIGNED_512(params->hash_area_offset)) {
+ log_err(cd, _("Unsupported VERITY hash offset."));
+ return -EINVAL;
+ }
+
+ if (MISALIGNED_512(params->fec_area_offset)) {
+ log_err(cd, _("Unsupported VERITY FEC offset."));
+ return -EINVAL;
+ }
+
+ if (!(cd->type = strdup(CRYPT_VERITY)))
+ return -ENOMEM;
+
+ if (params->data_device) {
+ r = crypt_set_data_device(cd, params->data_device);
+ if (r)
+ return r;
+ }
+
+ if (!params->data_size) {
+ r = device_size(cd->device, &data_device_size);
+ if (r < 0)
+ return r;
+
+ cd->u.verity.hdr.data_size = data_device_size / params->data_block_size;
+ } else
+ cd->u.verity.hdr.data_size = params->data_size;
+
+ if (device_is_identical(crypt_metadata_device(cd), crypt_data_device(cd)) > 0 &&
+ (cd->u.verity.hdr.data_size * params->data_block_size) > params->hash_area_offset) {
+ log_err(cd, _("Data area overlaps with hash area."));
+ return -EINVAL;
+ }
+
+ hash_size = crypt_hash_size(params->hash_name);
+ if (hash_size <= 0) {
+ log_err(cd, _("Hash algorithm %s not supported."),
+ params->hash_name);
+ return -EINVAL;
+ }
+ cd->u.verity.root_hash_size = hash_size;
+
+ if (params->fec_device) {
+ fec_device_path = strdup(params->fec_device);
+ if (!fec_device_path)
+ return -ENOMEM;
+ r = device_alloc(cd, &fec_device, params->fec_device);
+ if (r < 0) {
+ r = -ENOMEM;
+ goto out;
+ }
+
+ hash_blocks_size = VERITY_hash_blocks(cd, params) * params->hash_block_size;
+ if (device_is_identical(crypt_metadata_device(cd), fec_device) > 0 &&
+ (params->hash_area_offset + hash_blocks_size) > params->fec_area_offset) {
+ log_err(cd, _("Hash area overlaps with FEC area."));
+ r = -EINVAL;
+ goto out;
+ }
+
+ if (device_is_identical(crypt_data_device(cd), fec_device) > 0 &&
+ (cd->u.verity.hdr.data_size * params->data_block_size) > params->fec_area_offset) {
+ log_err(cd, _("Data area overlaps with FEC area."));
+ r = -EINVAL;
+ goto out;
+ }
+ }
+
+ root_hash = malloc(cd->u.verity.root_hash_size);
+ hash_name = strdup(params->hash_name);
+ salt = malloc(params->salt_size);
+
+ if (!root_hash || !hash_name || !salt) {
+ r = -ENOMEM;
+ goto out;
+ }
+
+ cd->u.verity.hdr.flags = params->flags;
+ cd->u.verity.root_hash = root_hash;
+ cd->u.verity.hdr.hash_name = hash_name;
+ cd->u.verity.hdr.data_device = NULL;
+ cd->u.verity.fec_device = fec_device;
+ cd->u.verity.hdr.fec_device = fec_device_path;
+ cd->u.verity.hdr.fec_roots = params->fec_roots;
+ cd->u.verity.hdr.data_block_size = params->data_block_size;
+ cd->u.verity.hdr.hash_block_size = params->hash_block_size;
+ cd->u.verity.hdr.hash_area_offset = params->hash_area_offset;
+ cd->u.verity.hdr.fec_area_offset = params->fec_area_offset;
+ cd->u.verity.hdr.hash_type = params->hash_type;
+ cd->u.verity.hdr.flags = params->flags;
+ cd->u.verity.hdr.salt_size = params->salt_size;
+ cd->u.verity.hdr.salt = salt;
+
+ if (params->salt)
+ memcpy(salt, params->salt, params->salt_size);
+ else
+ r = crypt_random_get(cd, salt, params->salt_size, CRYPT_RND_SALT);
+ if (r)
+ goto out;
+
+ if (params->flags & CRYPT_VERITY_CREATE_HASH) {
+ r = VERITY_create(cd, &cd->u.verity.hdr,
+ cd->u.verity.root_hash, cd->u.verity.root_hash_size);
+ if (!r && params->fec_device)
+ r = VERITY_FEC_process(cd, &cd->u.verity.hdr, cd->u.verity.fec_device, 0, NULL);
+ if (r)
+ goto out;
+ }
+
+ if (!(params->flags & CRYPT_VERITY_NO_HEADER)) {
+ if (uuid) {
+ if (!(cd->u.verity.uuid = strdup(uuid)))
+ r = -ENOMEM;
+ } else
+ r = VERITY_UUID_generate(&cd->u.verity.uuid);
+
+ if (!r)
+ r = VERITY_write_sb(cd, cd->u.verity.hdr.hash_area_offset,
+ cd->u.verity.uuid,
+ &cd->u.verity.hdr);
+ }
+
+out:
+ if (r) {
+ device_free(cd, fec_device);
+ free(root_hash);
+ free(hash_name);
+ free(fec_device_path);
+ free(salt);
+ }
+
+ return r;
+}
+
+static int _crypt_format_integrity(struct crypt_device *cd,
+ const char *uuid,
+ struct crypt_params_integrity *params)
+{
+ int r;
+ uint32_t integrity_tag_size;
+ char *integrity = NULL, *journal_integrity = NULL, *journal_crypt = NULL;
+ struct volume_key *journal_crypt_key = NULL, *journal_mac_key = NULL;
+
+ if (!params)
+ return -EINVAL;
+
+ if (uuid) {
+ log_err(cd, _("UUID is not supported for this crypt type."));
+ return -EINVAL;
+ }
+
+ r = device_check_access(cd, crypt_metadata_device(cd), DEV_EXCL);
+ if (r < 0)
+ return r;
+
+ /* Wipe first 8 sectors - fs magic numbers etc. */
+ r = crypt_wipe_device(cd, crypt_metadata_device(cd), CRYPT_WIPE_ZERO, 0,
+ 8 * SECTOR_SIZE, 8 * SECTOR_SIZE, NULL, NULL);
+ if (r < 0) {
+ log_err(cd, _("Cannot wipe header on device %s."),
+ mdata_device_path(cd));
+ return r;
+ }
+
+ if (!(cd->type = strdup(CRYPT_INTEGRITY)))
+ return -ENOMEM;
+
+ if (params->journal_crypt_key) {
+ journal_crypt_key = crypt_alloc_volume_key(params->journal_crypt_key_size,
+ params->journal_crypt_key);
+ if (!journal_crypt_key)
+ return -ENOMEM;
+ }
+
+ if (params->journal_integrity_key) {
+ journal_mac_key = crypt_alloc_volume_key(params->journal_integrity_key_size,
+ params->journal_integrity_key);
+ if (!journal_mac_key) {
+ r = -ENOMEM;
+ goto out;
+ }
+ }
+
+ if (params->integrity && !(integrity = strdup(params->integrity))) {
+ r = -ENOMEM;
+ goto out;
+ }
+ if (params->journal_integrity && !(journal_integrity = strdup(params->journal_integrity))) {
+ r = -ENOMEM;
+ goto out;
+ }
+ if (params->journal_crypt && !(journal_crypt = strdup(params->journal_crypt))) {
+ r = -ENOMEM;
+ goto out;
+ }
+
+ integrity_tag_size = INTEGRITY_hash_tag_size(integrity);
+ if (integrity_tag_size > 0 && params->tag_size && integrity_tag_size != params->tag_size)
+ log_std(cd, _("WARNING: Requested tag size %d bytes differs from %s size output (%d bytes).\n"),
+ params->tag_size, integrity, integrity_tag_size);
+
+ if (params->tag_size)
+ integrity_tag_size = params->tag_size;
+
+ cd->u.integrity.journal_crypt_key = journal_crypt_key;
+ cd->u.integrity.journal_mac_key = journal_mac_key;
+ cd->u.integrity.params.journal_size = params->journal_size;
+ cd->u.integrity.params.journal_watermark = params->journal_watermark;
+ cd->u.integrity.params.journal_commit_time = params->journal_commit_time;
+ cd->u.integrity.params.interleave_sectors = params->interleave_sectors;
+ cd->u.integrity.params.buffer_sectors = params->buffer_sectors;
+ cd->u.integrity.params.sector_size = params->sector_size;
+ cd->u.integrity.params.tag_size = integrity_tag_size;
+ cd->u.integrity.params.integrity = integrity;
+ cd->u.integrity.params.journal_integrity = journal_integrity;
+ cd->u.integrity.params.journal_crypt = journal_crypt;
+
+ r = INTEGRITY_format(cd, params, cd->u.integrity.journal_crypt_key, cd->u.integrity.journal_mac_key);
+ if (r)
+ log_err(cd, _("Cannot format integrity for device %s."),
+ mdata_device_path(cd));
+out:
+ if (r) {
+ crypt_free_volume_key(journal_crypt_key);
+ crypt_free_volume_key(journal_mac_key);
+ free(integrity);
+ free(journal_integrity);
+ free(journal_crypt);
+ }
+
+ return r;
+}
+
+static 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,
+ bool sector_size_autodetect)
+{
+ int r;
+
+ if (!cd || !type)
+ return -EINVAL;
+
+ if (cd->type) {
+ log_dbg(cd, "Context already formatted as %s.", cd->type);
+ return -EINVAL;
+ }
+
+ log_dbg(cd, "Formatting device %s as type %s.", mdata_device_path(cd) ?: "(none)", type);
+
+ crypt_reset_null_type(cd);
+
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
+ if (isPLAIN(type))
+ r = _crypt_format_plain(cd, cipher, cipher_mode,
+ uuid, volume_key_size, params);
+ else if (isLUKS1(type))
+ r = _crypt_format_luks1(cd, cipher, cipher_mode,
+ uuid, volume_key, volume_key_size, params);
+ else if (isLUKS2(type))
+ r = _crypt_format_luks2(cd, cipher, cipher_mode,
+ uuid, volume_key, volume_key_size, params, sector_size_autodetect);
+ else if (isLOOPAES(type))
+ r = _crypt_format_loopaes(cd, cipher, uuid, volume_key_size, params);
+ else if (isVERITY(type))
+ r = _crypt_format_verity(cd, uuid, params);
+ else if (isINTEGRITY(type))
+ r = _crypt_format_integrity(cd, uuid, params);
+ else {
+ log_err(cd, _("Unknown crypt device type %s requested."), type);
+ r = -EINVAL;
+ }
+
+ if (r < 0) {
+ crypt_set_null_type(cd);
+ crypt_free_volume_key(cd->volume_key);
+ cd->volume_key = NULL;
+ }
+
+ return r;
+}
+
+CRYPT_SYMBOL_EXPORT_NEW(int, crypt_format, 2, 4,
+ /* crypt_format parameters follows */
+ 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)
+{
+ return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, true);
+}
+
+
+CRYPT_SYMBOL_EXPORT_OLD(int, crypt_format, 2, 0,
+ /* crypt_format parameters follows */
+ 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)
+{
+ return _crypt_format(cd, type, cipher, cipher_mode, uuid, volume_key, volume_key_size, params, false);
+}
+
+int crypt_repair(struct crypt_device *cd,
+ const char *requested_type,
+ void *params __attribute__((unused)))
+{
+ int r;
+
+ if (!cd)
+ return -EINVAL;
+
+ log_dbg(cd, "Trying to repair %s crypt type from device %s.",
+ requested_type ?: "any", mdata_device_path(cd) ?: "(none)");
+
+ if (!crypt_metadata_device(cd))
+ return -EINVAL;
+
+ if (requested_type && !isLUKS(requested_type))
+ return -EINVAL;
+
+ /* Load with repair */
+ r = _crypt_load_luks(cd, requested_type, false, true);
+ if (r < 0)
+ return r;
+
+ /* cd->type and header must be set in context */
+ r = crypt_check_data_device_size(cd);
+ if (r < 0)
+ crypt_set_null_type(cd);
+
+ return r;
+}
+
+/* compare volume keys */
+static int _compare_volume_keys(struct volume_key *svk, unsigned skeyring_only,
+ struct volume_key *tvk, unsigned tkeyring_only)
+{
+ if (!svk && !tvk)
+ return 0;
+ else if (!svk || !tvk)
+ return 1;
+
+ if (svk->keylength != tvk->keylength)
+ return 1;
+
+ if (!skeyring_only && !tkeyring_only)
+ return crypt_backend_memeq(svk->key, tvk->key, svk->keylength);
+
+ if (svk->key_description && tvk->key_description)
+ return strcmp(svk->key_description, tvk->key_description);
+
+ return 0;
+}
+
+static int _compare_device_types(struct crypt_device *cd,
+ const struct crypt_dm_active_device *src,
+ const struct crypt_dm_active_device *tgt)
+{
+ if (!tgt->uuid) {
+ log_dbg(cd, "Missing device uuid in target device.");
+ return -EINVAL;
+ }
+
+ if (isLUKS2(cd->type) && !strncmp("INTEGRITY-", tgt->uuid, strlen("INTEGRITY-"))) {
+ if (crypt_uuid_cmp(tgt->uuid, src->uuid)) {
+ log_dbg(cd, "LUKS UUID mismatch.");
+ return -EINVAL;
+ }
+ } else if (isLUKS(cd->type)) {
+ if (!src->uuid || strncmp(cd->type, tgt->uuid, strlen(cd->type)) ||
+ crypt_uuid_cmp(tgt->uuid, src->uuid)) {
+ log_dbg(cd, "LUKS UUID mismatch.");
+ return -EINVAL;
+ }
+ } else if (isPLAIN(cd->type) || isLOOPAES(cd->type)) {
+ if (strncmp(cd->type, tgt->uuid, strlen(cd->type))) {
+ log_dbg(cd, "Unexpected uuid prefix %s in target device.", tgt->uuid);
+ return -EINVAL;
+ }
+ } else if (!isINTEGRITY(cd->type)) {
+ log_dbg(cd, "Unsupported device type %s for reload.", cd->type ?: "<empty>");
+ return -ENOTSUP;
+ }
+
+ return 0;
+}
+
+static int _compare_crypt_devices(struct crypt_device *cd,
+ const struct dm_target *src,
+ const struct dm_target *tgt)
+{
+ char *src_cipher = NULL, *src_integrity = NULL;
+ int r = -EINVAL;
+
+ /* for crypt devices keys are mandatory */
+ if (!src->u.crypt.vk || !tgt->u.crypt.vk)
+ return -EINVAL;
+
+ /* CIPHER checks */
+ if (!src->u.crypt.cipher || !tgt->u.crypt.cipher)
+ return -EINVAL;
+
+ /*
+ * dm_query_target converts capi cipher specification to dm-crypt format.
+ * We need to do same for cipher specification requested in source
+ * device.
+ */
+ if (crypt_capi_to_cipher(&src_cipher, &src_integrity, src->u.crypt.cipher, src->u.crypt.integrity))
+ return -EINVAL;
+
+ if (strcmp(src_cipher, tgt->u.crypt.cipher)) {
+ log_dbg(cd, "Cipher specs do not match.");
+ goto out;
+ }
+
+ if (tgt->u.crypt.vk->keylength == 0 && crypt_is_cipher_null(tgt->u.crypt.cipher))
+ log_dbg(cd, "Existing device uses cipher null. Skipping key comparison.");
+ else if (_compare_volume_keys(src->u.crypt.vk, 0, tgt->u.crypt.vk, tgt->u.crypt.vk->key_description != NULL)) {
+ log_dbg(cd, "Keys in context and target device do not match.");
+ goto out;
+ }
+
+ if (crypt_strcmp(src_integrity, tgt->u.crypt.integrity)) {
+ log_dbg(cd, "Integrity parameters do not match.");
+ goto out;
+ }
+
+ if (src->u.crypt.offset != tgt->u.crypt.offset ||
+ src->u.crypt.sector_size != tgt->u.crypt.sector_size ||
+ src->u.crypt.iv_offset != tgt->u.crypt.iv_offset ||
+ src->u.crypt.tag_size != tgt->u.crypt.tag_size) {
+ log_dbg(cd, "Integer parameters do not match.");
+ goto out;
+ }
+
+ if (device_is_identical(src->data_device, tgt->data_device) <= 0)
+ log_dbg(cd, "Data devices do not match.");
+ else
+ r = 0;
+
+out:
+ free(src_cipher);
+ free(src_integrity);
+
+ return r;
+}
+
+static int _compare_integrity_devices(struct crypt_device *cd,
+ const struct dm_target *src,
+ const struct dm_target *tgt)
+{
+ /*
+ * some parameters may be implicit (and set in dm-integrity ctor)
+ *
+ * journal_size
+ * journal_watermark
+ * journal_commit_time
+ * buffer_sectors
+ * interleave_sectors
+ */
+
+ /* check remaining integer values that makes sense */
+ if (src->u.integrity.tag_size != tgt->u.integrity.tag_size ||
+ src->u.integrity.offset != tgt->u.integrity.offset ||
+ src->u.integrity.sector_size != tgt->u.integrity.sector_size) {
+ log_dbg(cd, "Integer parameters do not match.");
+ return -EINVAL;
+ }
+
+ if (crypt_strcmp(src->u.integrity.integrity, tgt->u.integrity.integrity) ||
+ crypt_strcmp(src->u.integrity.journal_integrity, tgt->u.integrity.journal_integrity) ||
+ crypt_strcmp(src->u.integrity.journal_crypt, tgt->u.integrity.journal_crypt)) {
+ log_dbg(cd, "Journal parameters do not match.");
+ return -EINVAL;
+ }
+
+ /* unfortunately dm-integrity doesn't support keyring */
+ if (_compare_volume_keys(src->u.integrity.vk, 0, tgt->u.integrity.vk, 0) ||
+ _compare_volume_keys(src->u.integrity.journal_integrity_key, 0, tgt->u.integrity.journal_integrity_key, 0) ||
+ _compare_volume_keys(src->u.integrity.journal_crypt_key, 0, tgt->u.integrity.journal_crypt_key, 0)) {
+ log_dbg(cd, "Journal keys do not match.");
+ return -EINVAL;
+ }
+
+ if (device_is_identical(src->data_device, tgt->data_device) <= 0) {
+ log_dbg(cd, "Data devices do not match.");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+int crypt_compare_dm_devices(struct crypt_device *cd,
+ const struct crypt_dm_active_device *src,
+ const struct crypt_dm_active_device *tgt)
+{
+ int r;
+ const struct dm_target *s, *t;
+
+ if (!src || !tgt)
+ return -EINVAL;
+
+ r = _compare_device_types(cd, src, tgt);
+ if (r)
+ return r;
+
+ s = &src->segment;
+ t = &tgt->segment;
+
+ while (s || t) {
+ if (!s || !t) {
+ log_dbg(cd, "segments count mismatch.");
+ return -EINVAL;
+ }
+ if (s->type != t->type) {
+ log_dbg(cd, "segment type mismatch.");
+ r = -EINVAL;
+ break;
+ }
+
+ switch (s->type) {
+ case DM_CRYPT:
+ r = _compare_crypt_devices(cd, s, t);
+ break;
+ case DM_INTEGRITY:
+ r = _compare_integrity_devices(cd, s, t);
+ break;
+ case DM_LINEAR:
+ r = (s->u.linear.offset == t->u.linear.offset) ? 0 : -EINVAL;
+ break;
+ default:
+ r = -ENOTSUP;
+ }
+
+ if (r)
+ break;
+
+ s = s->next;
+ t = t->next;
+ }
+
+ return r;
+}
+
+static int _reload_device(struct crypt_device *cd, const char *name,
+ struct crypt_dm_active_device *sdmd)
+{
+ int r;
+ struct crypt_dm_active_device tdmd;
+ struct dm_target *src, *tgt = &tdmd.segment;
+
+ if (!cd || !cd->type || !name || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH))
+ return -EINVAL;
+
+ r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
+ DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
+ DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_INTEGRITY_PARAMS |
+ DM_ACTIVE_JOURNAL_CRYPT_KEY | DM_ACTIVE_JOURNAL_MAC_KEY, &tdmd);
+ if (r < 0) {
+ log_err(cd, _("Device %s is not active."), name);
+ return -EINVAL;
+ }
+
+ if (!single_segment(&tdmd) ||
+ (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY) ||
+ (tgt->type == DM_CRYPT && tgt->u.crypt.tag_size)) {
+ r = -ENOTSUP;
+ log_err(cd, _("Unsupported parameters on device %s."), name);
+ goto out;
+ }
+
+ r = crypt_compare_dm_devices(cd, sdmd, &tdmd);
+ if (r) {
+ log_err(cd, _("Mismatching parameters on device %s."), name);
+ goto out;
+ }
+
+ src = &sdmd->segment;
+
+ /* Changing read only flag for active device makes no sense */
+ if (tdmd.flags & CRYPT_ACTIVATE_READONLY)
+ sdmd->flags |= CRYPT_ACTIVATE_READONLY;
+ else
+ sdmd->flags &= ~CRYPT_ACTIVATE_READONLY;
+
+ if (tgt->type == DM_CRYPT && sdmd->flags & CRYPT_ACTIVATE_KEYRING_KEY) {
+ r = crypt_volume_key_set_description(tgt->u.crypt.vk, src->u.crypt.vk->key_description);
+ if (r)
+ goto out;
+ } else if (tgt->type == DM_CRYPT) {
+ crypt_free_volume_key(tgt->u.crypt.vk);
+ tgt->u.crypt.vk = crypt_alloc_volume_key(src->u.crypt.vk->keylength, src->u.crypt.vk->key);
+ if (!tgt->u.crypt.vk) {
+ r = -ENOMEM;
+ goto out;
+ }
+ }
+
+ if (tgt->type == DM_CRYPT)
+ r = device_block_adjust(cd, src->data_device, DEV_OK,
+ src->u.crypt.offset, &sdmd->size, NULL);
+ else if (tgt->type == DM_INTEGRITY)
+ r = device_block_adjust(cd, src->data_device, DEV_OK,
+ src->u.integrity.offset, &sdmd->size, NULL);
+ else
+ r = -EINVAL;
+
+ if (r)
+ goto out;
+
+ tdmd.flags = sdmd->flags;
+ tgt->size = tdmd.size = sdmd->size;
+
+ r = dm_reload_device(cd, name, &tdmd, 0, 1);
+out:
+ dm_targets_free(cd, &tdmd);
+ free(CONST_CAST(void*)tdmd.uuid);
+
+ return r;
+}
+
+static int _reload_device_with_integrity(struct crypt_device *cd,
+ const char *name,
+ const char *iname,
+ const char *ipath,
+ struct crypt_dm_active_device *sdmd,
+ struct crypt_dm_active_device *sdmdi)
+{
+ int r;
+ struct crypt_dm_active_device tdmd, tdmdi = {};
+ struct dm_target *src, *srci, *tgt = &tdmd.segment, *tgti = &tdmdi.segment;
+ struct device *data_device = NULL;
+ bool clear = false;
+
+ if (!cd || !cd->type || !name || !iname || !(sdmd->flags & CRYPT_ACTIVATE_REFRESH))
+ return -EINVAL;
+
+ r = dm_query_device(cd, name, DM_ACTIVE_DEVICE | DM_ACTIVE_CRYPT_CIPHER |
+ DM_ACTIVE_UUID | DM_ACTIVE_CRYPT_KEYSIZE |
+ DM_ACTIVE_CRYPT_KEY, &tdmd);
+ if (r < 0) {
+ log_err(cd, _("Device %s is not active."), name);
+ return -EINVAL;
+ }
+
+ if (!single_segment(&tdmd) || tgt->type != DM_CRYPT || !tgt->u.crypt.tag_size) {
+ log_err(cd, _("Unsupported parameters on device %s."), name);
+ r = -ENOTSUP;
+ goto out;
+ }
+
+ r = dm_query_device(cd, iname, DM_ACTIVE_DEVICE | DM_ACTIVE_UUID, &tdmdi);
+ if (r < 0) {
+ log_err(cd, _("Device %s is not active."), iname);
+ r = -EINVAL;
+ goto out;
+ }
+
+ if (!single_segment(&tdmdi) || tgti->type != DM_INTEGRITY) {
+ log_err(cd, _("Unsupported parameters on device %s."), iname);
+ r = -ENOTSUP;
+ goto out;
+ }
+
+ r = crypt_compare_dm_devices(cd, sdmdi, &tdmdi);
+ if (r) {
+ log_err(cd, _("Mismatching parameters on device %s."), iname);
+ goto out;
+ }
+
+ /* unsupported underneath dm-crypt with auth. encryption */
+ if (sdmdi->segment.u.integrity.meta_device || tdmdi.segment.u.integrity.meta_device)
+ return -ENOTSUP;
+
+ src = &sdmd->segment;
+ srci = &sdmdi->segment;
+
+ r = device_alloc(cd, &data_device, ipath);
+ if (r < 0)
+ goto out;
+
+ r = device_block_adjust(cd, srci->data_device, DEV_OK,
+ srci->u.integrity.offset, &sdmdi->size, NULL);
+ if (r)
+ goto out;
+
+ src->data_device = data_device;
+
+ r = crypt_compare_dm_devices(cd, sdmd, &tdmd);
+ if (r) {
+ log_err(cd, _("Crypt devices mismatch."));
+ goto out;
+ }
+
+ /* Changing read only flag for active device makes no sense */
+ if (tdmd.flags & CRYPT_ACTIVATE_READONLY)
+ sdmd->flags |= CRYPT_ACTIVATE_READONLY;
+ else
+ sdmd->flags &= ~CRYPT_ACTIVATE_READONLY;
+
+ if (tdmdi.flags & CRYPT_ACTIVATE_READONLY)
+ sdmdi->flags |= CRYPT_ACTIVATE_READONLY;
+ else
+ sdmdi->flags &= ~CRYPT_ACTIVATE_READONLY;
+
+ if (sdmd->flags & CRYPT_ACTIVATE_KEYRING_KEY) {
+ r = crypt_volume_key_set_description(tgt->u.crypt.vk, src->u.crypt.vk->key_description);
+ if (r)
+ goto out;
+ } else {
+ crypt_free_volume_key(tgt->u.crypt.vk);
+ tgt->u.crypt.vk = crypt_alloc_volume_key(src->u.crypt.vk->keylength, src->u.crypt.vk->key);
+ if (!tgt->u.crypt.vk) {
+ r = -ENOMEM;
+ goto out;
+ }
+ }
+
+ r = device_block_adjust(cd, src->data_device, DEV_OK,
+ src->u.crypt.offset, &sdmd->size, NULL);
+ if (r)
+ goto out;
+
+ tdmd.flags = sdmd->flags;
+ tdmd.size = sdmd->size;
+
+ if ((r = dm_reload_device(cd, iname, sdmdi, 0, 0))) {
+ log_err(cd, _("Failed to reload device %s."), iname);
+ goto out;
+ }
+
+ if ((r = dm_reload_device(cd, name, &tdmd, 0, 0))) {
+ log_err(cd, _("Failed to reload device %s."), name);
+ clear = true;
+ goto out;
+ }
+
+ if ((r = dm_suspend_device(cd, name, 0))) {
+ log_err(cd, _("Failed to suspend device %s."), name);
+ clear = true;
+ goto out;
+ }
+
+ if ((r = dm_suspend_device(cd, iname, 0))) {
+ log_err(cd, _("Failed to suspend device %s."), iname);
+ clear = true;
+ goto out;
+ }
+
+ if ((r = dm_resume_device(cd, iname, act2dmflags(sdmdi->flags)))) {
+ log_err(cd, _("Failed to resume device %s."), iname);
+ clear = true;
+ goto out;
+ }
+
+ r = dm_resume_device(cd, name, act2dmflags(tdmd.flags));
+ if (!r)
+ goto out;
+
+ /*
+ * This is worst case scenario. We have active underlying dm-integrity device with
+ * new table but dm-crypt resume failed for some reason. Tear everything down and
+ * burn it for good.
+ */
+
+ log_err(cd, _("Fatal error while reloading device %s (on top of device %s)."), name, iname);
+
+ if (dm_error_device(cd, name))
+ log_err(cd, _("Failed to switch device %s to dm-error."), name);
+ if (dm_error_device(cd, iname))
+ log_err(cd, _("Failed to switch device %s to dm-error."), iname);
+out:
+ if (clear) {
+ dm_clear_device(cd, name);
+ dm_clear_device(cd, iname);
+
+ if (dm_status_suspended(cd, name) > 0)
+ dm_resume_device(cd, name, 0);
+ if (dm_status_suspended(cd, iname) > 0)
+ dm_resume_device(cd, iname, 0);
+ }
+
+ dm_targets_free(cd, &tdmd);
+ dm_targets_free(cd, &tdmdi);
+ free(CONST_CAST(void*)tdmdi.uuid);
+ free(CONST_CAST(void*)tdmd.uuid);
+ device_free(cd, data_device);
+
+ return r;
+}
+
+int crypt_resize(struct crypt_device *cd, const char *name, uint64_t new_size)
+{
+ struct crypt_dm_active_device dmdq, dmd = {};
+ struct dm_target *tgt = &dmdq.segment;
+ struct crypt_params_integrity params = {};
+ uint32_t supported_flags = 0;
+ uint64_t old_size;
+ int r;
+
+ /*
+ * FIXME: Also with LUKS2 we must not allow resize when there's
+ * explicit size stored in metadata (length != "dynamic")
+ */
+
+ /* Device context type must be initialized */
+ if (!cd || !cd->type || !name)
+ return -EINVAL;
+
+ if (isTCRYPT(cd->type) || isBITLK(cd->type)) {
+ log_err(cd, _("This operation is not supported for this device type."));
+ return -ENOTSUP;
+ }
+
+ log_dbg(cd, "Resizing device %s to %" PRIu64 " sectors.", name, new_size);
+
+ r = dm_query_device(cd, name, DM_ACTIVE_CRYPT_KEYSIZE | DM_ACTIVE_CRYPT_KEY |
+ DM_ACTIVE_INTEGRITY_PARAMS | DM_ACTIVE_JOURNAL_CRYPT_KEY |
+ DM_ACTIVE_JOURNAL_MAC_KEY, &dmdq);
+ if (r < 0) {
+ log_err(cd, _("Device %s is not active."), name);
+ return -EINVAL;
+ }
+ if (!single_segment(&dmdq) || (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY)) {
+ log_dbg(cd, "Unsupported device table detected in %s.", name);
+ r = -EINVAL;
+ goto out;
+ }
+
+ if ((dmdq.flags & CRYPT_ACTIVATE_KEYRING_KEY) && !crypt_key_in_keyring(cd)) {
+ r = -EPERM;
+ goto out;
+ }
+
+ if (crypt_key_in_keyring(cd)) {
+ if (!isLUKS2(cd->type)) {
+ r = -EINVAL;
+ goto out;
+ }
+ r = LUKS2_key_description_by_segment(cd, &cd->u.luks2.hdr,
+ tgt->u.crypt.vk, CRYPT_DEFAULT_SEGMENT);
+ if (r)
+ goto out;
+
+ dmdq.flags |= CRYPT_ACTIVATE_KEYRING_KEY;
+ }
+
+ if (crypt_loop_device(crypt_get_device_name(cd))) {
+ log_dbg(cd, "Trying to resize underlying loop device %s.",
+ crypt_get_device_name(cd));
+ /* Here we always use default size not new_size */
+ if (crypt_loop_resize(crypt_get_device_name(cd)))
+ log_err(cd, _("Cannot resize loop device."));
+ }
+
+
+ /*
+ * Integrity device metadata are maintained by the kernel. We need to
+ * reload the device (with the same parameters) and let the kernel
+ * calculate the maximum size of integrity device and store it in the
+ * superblock.
+ */
+ if (!new_size && tgt->type == DM_INTEGRITY) {
+ r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd),
+ crypt_get_data_offset(cd) * SECTOR_SIZE, &old_size);
+ if (r < 0)
+ return r;
+
+ dmd.size = dmdq.size;
+ dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH | CRYPT_ACTIVATE_PRIVATE;
+
+ r = crypt_get_integrity_info(cd, ¶ms);
+ if (r)
+ goto out;
+
+ r = dm_integrity_target_set(cd, &dmd.segment, 0, dmdq.segment.size,
+ crypt_metadata_device(cd), crypt_data_device(cd),
+ crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd),
+ crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key,
+ tgt->u.integrity.journal_integrity_key, ¶ms);
+ if (r)
+ goto out;
+ r = _reload_device(cd, name, &dmd);
+ if (r)
+ goto out;
+
+ r = INTEGRITY_data_sectors(cd, crypt_metadata_device(cd),
+ crypt_get_data_offset(cd) * SECTOR_SIZE, &new_size);
+ if (r < 0)
+ return r;
+ log_dbg(cd, "Maximum integrity device size from kernel %" PRIu64, new_size);
+
+ if (old_size == new_size && new_size == dmdq.size &&
+ !dm_flags(cd, tgt->type, &supported_flags) &&
+ !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED))
+ log_std(cd, _("WARNING: Maximum size already set or kernel doesn't support resize.\n"));
+ }
+
+ r = device_block_adjust(cd, crypt_data_device(cd), DEV_OK,
+ crypt_get_data_offset(cd), &new_size, &dmdq.flags);
+ if (r)
+ goto out;
+
+ if (MISALIGNED(new_size, (tgt->type == DM_CRYPT ? tgt->u.crypt.sector_size : tgt->u.integrity.sector_size) >> SECTOR_SHIFT)) {
+ log_err(cd, _("Device size is not aligned to requested sector size."));
+ r = -EINVAL;
+ goto out;
+ }
+
+ if (MISALIGNED(new_size, device_block_size(cd, crypt_data_device(cd)) >> SECTOR_SHIFT)) {
+ log_err(cd, _("Device size is not aligned to device logical block size."));
+ r = -EINVAL;
+ goto out;
+ }
+
+ dmd.uuid = crypt_get_uuid(cd);
+ dmd.size = new_size;
+ dmd.flags = dmdq.flags | CRYPT_ACTIVATE_REFRESH;
+
+ if (tgt->type == DM_CRYPT) {
+ r = dm_crypt_target_set(&dmd.segment, 0, new_size, crypt_data_device(cd),
+ tgt->u.crypt.vk, crypt_get_cipher_spec(cd),
+ crypt_get_iv_offset(cd), crypt_get_data_offset(cd),
+ crypt_get_integrity(cd), crypt_get_integrity_tag_size(cd),
+ crypt_get_sector_size(cd));
+ if (r < 0)
+ goto out;
+ } else if (tgt->type == DM_INTEGRITY) {
+ r = crypt_get_integrity_info(cd, ¶ms);
+ if (r)
+ goto out;
+
+ r = dm_integrity_target_set(cd, &dmd.segment, 0, new_size,
+ crypt_metadata_device(cd), crypt_data_device(cd),
+ crypt_get_integrity_tag_size(cd), crypt_get_data_offset(cd),
+ crypt_get_sector_size(cd), tgt->u.integrity.vk, tgt->u.integrity.journal_crypt_key,
+ tgt->u.integrity.journal_integrity_key, ¶ms);
+ if (r)
+ goto out;
+ }
+
+ if (new_size == dmdq.size) {
+ log_dbg(cd, "Device has already requested size %" PRIu64
+ " sectors.", dmdq.size);
+ r = 0;
+ } else {
+ if (isTCRYPT(cd->type))
+ r = -ENOTSUP;
+ else if (isLUKS2(cd->type))
+ r = LUKS2_unmet_requirements(cd, &cd->u.luks2.hdr, 0, 0);
+ if (!r)
+ r = _reload_device(cd, name, &dmd);
+
+ if (r && tgt->type == DM_INTEGRITY &&
+ !dm_flags(cd, tgt->type, &supported_flags) &&
+ !(supported_flags & DM_INTEGRITY_RESIZE_SUPPORTED))
+ log_err(cd, _("Resize failed, the kernel doesn't support it."));
+ }
+out:
+ dm_targets_free(cd, &dmd);
+ dm_targets_free(cd, &dmdq);
+
+ return r;
+}
+
+int crypt_set_uuid(struct crypt_device *cd, const char *uuid)
+{
+ const char *active_uuid;
+ int r;
+
+ log_dbg(cd, "%s device uuid.", uuid ? "Setting new" : "Refreshing");
+
+ if ((r = onlyLUKS(cd)))
+ return r;
+
+ active_uuid = crypt_get_uuid(cd);
+
+ if (uuid && active_uuid && !strncmp(uuid, active_uuid, UUID_STRING_L)) {
+ log_dbg(cd, "UUID is the same as requested (%s) for device %s.",
+ uuid, mdata_device_path(cd));
+ return 0;
+ }
+
+ if (uuid)
+ log_dbg(cd, "Requested new UUID change to %s for %s.", uuid, mdata_device_path(cd));
+ else
+ log_dbg(cd, "Requested new UUID refresh for %s.", mdata_device_path(cd));
+
+ if (!crypt_confirm(cd, _("Do you really want to change UUID of device?")))
+ return -EPERM;
+
+ if (isLUKS1(cd->type))
+ return LUKS_hdr_uuid_set(&cd->u.luks1.hdr, uuid, cd);
+ else
+ return LUKS2_hdr_uuid(cd, &cd->u.luks2.hdr, uuid);
+}
+
+int crypt_set_label(struct crypt_device *cd, const char *label, const char *subsystem)
+{
+ int r;
+
+ log_dbg(cd, "Setting new labels.");
+
+ if ((r = onlyLUKS2(cd)))
+ return r;
+
+ return LUKS2_hdr_labels(cd, &cd->u.luks2.hdr, label, subsystem, 1);
+}
+
+const char *crypt_get_label(struct crypt_device *cd)
+{
+ if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
+ return NULL;
+
+ return cd->u.luks2.hdr.label;
+}
+
+const char *crypt_get_subsystem(struct crypt_device *cd)
+{
+ if (_onlyLUKS2(cd, CRYPT_CD_QUIET | CRYPT_CD_UNRESTRICTED, 0))
+ return NULL;
+
+ return cd->u.luks2.hdr.subsystem;
+}
+
+int crypt_header_backup(struct crypt_device *cd,
+ const char *requested_type,
+ const char *backup_file)
+{
+ int r;
+
+ if (requested_type && !isLUKS(requested_type))
+ return -EINVAL;
+
+ if (!backup_file)
+ return -EINVAL;
+
+ /* Load with repair */
+ r = _crypt_load_luks(cd, requested_type, false, false);
+ if (r < 0)
+ return r;
+
+ log_dbg(cd, "Requested header backup of device %s (%s) to "
+ "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file);
+
+ if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type)))
+ r = LUKS_hdr_backup(backup_file, cd);
+ else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type)))
+ r = LUKS2_hdr_backup(cd, &cd->u.luks2.hdr, backup_file);
+ else
+ r = -EINVAL;
+
+ return r;
+}
+
+int crypt_header_restore(struct crypt_device *cd,
+ const char *requested_type,
+ const char *backup_file)
+{
+ struct luks_phdr hdr1;
+ struct luks2_hdr hdr2;
+ int r, version;
+
+ if (requested_type && !isLUKS(requested_type))
+ return -EINVAL;
+
+ if (!cd || (cd->type && !isLUKS(cd->type)) || !backup_file)
+ return -EINVAL;
+
+ r = init_crypto(cd);
+ if (r < 0)
+ return r;
+
+ log_dbg(cd, "Requested header restore to device %s (%s) from "
+ "file %s.", mdata_device_path(cd), requested_type ?: "any type", backup_file);
+
+ version = LUKS2_hdr_version_unlocked(cd, backup_file);
+ if (!version ||
+ (requested_type && version == 1 && !isLUKS1(requested_type)) ||
+ (requested_type && version == 2 && !isLUKS2(requested_type))) {
+ log_err(cd, _("Header backup file does not contain compatible LUKS header."));
+ return -EINVAL;
+ }
+
+ memset(&hdr2, 0, sizeof(hdr2));
+
+ if (!cd->type) {
+ if (version == 1)
+ r = LUKS_hdr_restore(backup_file, &hdr1, cd);
+ else
+ r = LUKS2_hdr_restore(cd, &hdr2, backup_file);
+
+ crypt_safe_memzero(&hdr1, sizeof(hdr1));
+ crypt_safe_memzero(&hdr2, sizeof(hdr2));
+ } else if (isLUKS2(cd->type) && (!requested_type || isLUKS2(requested_type))) {
+ r = LUKS2_hdr_restore(cd, &cd->u.luks2.hdr, backup_file);
+ if (r)
+ (void) _crypt_load_luks2(cd, 1, 0);
+ } else if (isLUKS1(cd->type) && (!requested_type || isLUKS1(requested_type)))
+ r = LUKS_hdr_restore(backup_file, &cd->u.luks1.hdr, cd);
+ else
+ r = -EINVAL;
+
+ if (!r)
+ r = _crypt_load_luks(cd, version == 1 ? CRYPT_LUKS1 : CRYPT_LUKS2, false, true);
+
+ return r;
+}
+
+int crypt_header_is_detached(struct crypt_device *cd)
+{
+ int r;
+
+ if (!cd || (cd->type && !isLUKS(cd->type)))
+ return -EINVAL;
+
+ r = device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd));
+ if (r < 0) {
+ log_dbg(cd, "Failed to compare data and metadata devices path.");
+ return r;
+ }
+
+ return r ? 0 : 1;
+}
+
+void crypt_free(struct crypt_device *cd)
+{
+ if (!cd)
+ return;
+
+ log_dbg(cd, "Releasing crypt device %s context.", mdata_device_path(cd) ?: "empty");
+
+ dm_backend_exit(cd);
+ crypt_free_volume_key(cd->volume_key);
+
+ crypt_free_type(cd, NULL);
+
+ device_free(cd, cd->device);
+ device_free(cd, cd->metadata_device);
+
+ free(CONST_CAST(void*)cd->pbkdf.type);
+ free(CONST_CAST(void*)cd->pbkdf.hash);
+
+ /* Some structures can contain keys (TCRYPT), wipe it */
+ crypt_safe_memzero(cd, sizeof(*cd));
+ free(cd);
+}
+
+static char *crypt_get_device_key_description(struct crypt_device *cd, const char *name)
+{
+ char *desc = NULL;
+ struct crypt_dm_active_device dmd;
+ struct dm_target *tgt = &dmd.segment;
+
+ if (dm_query_device(cd, name, DM_ACTIVE_CRYPT_KEY | DM_ACTIVE_CRYPT_KEYSIZE, &dmd) < 0)
+ return NULL;
+
+ if (single_segment(&dmd) && tgt->type == DM_CRYPT &&
+ (dmd.flags & CRYPT_ACTIVATE_KEYRING_KEY) && tgt->u.crypt.vk->key_description)
+ desc = strdup(tgt->u.crypt.vk->key_description);
+
+ dm_targets_free(cd, &dmd);
+
+ return desc;
+}
+
+int crypt_suspend(struct crypt_device *cd,
+ const char *name)
+{
+ char *key_desc;
+ crypt_status_info ci;
+ int r;
+ uint32_t dmflags = DM_SUSPEND_WIPE_KEY;
+
+ /* FIXME: check context uuid matches the dm-crypt device uuid (onlyLUKS branching) */
+
+ if (!cd || !name)
+ return -EINVAL;
+
+ log_dbg(cd, "Suspending volume %s.", name);
+
+ if (cd->type)
+ r = onlyLUKS(cd);
+ else {
+ r = crypt_uuid_type_cmp(cd, CRYPT_LUKS1);
+ if (r < 0)
+ r = crypt_uuid_type_cmp(cd, CRYPT_LUKS2);
+ if (r < 0)
+ log_err(cd, _("This operation is supported only for LUKS device."));
+ }
+
+ if (r < 0)
+ return r;
+
+ ci = crypt_status(NULL, name);
+ if (ci < CRYPT_ACTIVE) {
+ log_err(cd, _("Volume %s is not active."), name);
+ return -EINVAL;
+ }
+
+ dm_backend_init(cd);
+
+ r = dm_status_suspended(cd, name);
+ if (r < 0)
+ goto out;
+
+ if (r) {
+ log_err(cd, _("Volume %s is already suspended."), name);
+ r = -EINVAL;
+ goto out;
+ }
+
+ key_desc = crypt_get_device_key_description(cd, name);
+
+ /* we can't simply wipe wrapped keys */
+ if (crypt_cipher_wrapped_key(crypt_get_cipher(cd), crypt_get_cipher_mode(cd)))
+ dmflags &= ~DM_SUSPEND_WIPE_KEY;
+
+ r = dm_suspend_device(cd, name, dmflags);
+ if (r == -ENOTSUP)
+ log_err(cd, _("Suspend is not supported for device %s."), name);
+ else if (r)
+ log_err(cd, _("Error during suspending device %s."), name);
+ else
+ crypt_drop_keyring_key_by_description(cd, key_desc, LOGON_KEY);
+ free(key_desc);
+out:
+ dm_backend_exit(cd);
+ return r;
+}
+
+/* key must be properly verified */
+static int resume_by_volume_key(struct crypt_device *cd,
+ struct volume_key *vk,
+ const char *name)
+{
+ int digest, r;
+ struct volume_key *zerokey = NULL;
+
+ if (crypt_is_cipher_null(crypt_get_cipher_spec(cd))) {
+ zerokey = crypt_alloc_volume_key(0, NULL);
+ if (!zerokey)
+ return -ENOMEM;
+ vk = zerokey;
+ } else if (crypt_use_keyring_for_vk(cd)) {
+ /* LUKS2 path only */
+ digest = LUKS2_digest_by_segment(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
+ if (digest < 0)
+ return -EINVAL;
+ r = LUKS2_volume_key_load_in_keyring_by_digest(cd, vk, digest);
+ if (r < 0)
+ return r;
+ }
+
+ r = dm_resume_and_reinstate_key(cd, name, vk);
+
+ if (r == -ENOTSUP)
+ log_err(cd, _("Resume is not supported for device %s."), name);
+ else if (r)
+ log_err(cd, _("Error during resuming device %s."), name);
+
+ if (r < 0)
+ crypt_drop_keyring_key(cd, vk);
+
+ crypt_free_volume_key(zerokey);
+
+ return r;
+}
+
+int crypt_resume_by_passphrase(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *passphrase,
+ size_t passphrase_size)
+{
+ struct volume_key *vk = NULL;
+ int r;
+
+ /* FIXME: check context uuid matches the dm-crypt device uuid */
+
+ if (!passphrase || !name)
+ return -EINVAL;
+
+ log_dbg(cd, "Resuming volume %s.", name);
+
+ if ((r = onlyLUKS(cd)))
+ return r;
+
+ r = dm_status_suspended(cd, name);
+ if (r < 0)
+ return r;
+
+ if (!r) {
+ log_err(cd, _("Volume %s is not suspended."), name);
+ return -EINVAL;
+ }
+
+ if (isLUKS1(cd->type))
+ r = LUKS_open_key_with_hdr(keyslot, passphrase, passphrase_size,
+ &cd->u.luks1.hdr, &vk, cd);
+ else
+ r = LUKS2_keyslot_open(cd, keyslot, CRYPT_DEFAULT_SEGMENT, passphrase, passphrase_size, &vk);
+
+ if (r < 0)
+ return r;
+
+ keyslot = r;
+
+ r = resume_by_volume_key(cd, vk, name);
+
+ crypt_free_volume_key(vk);
+ return r < 0 ? r : keyslot;
+}
+
+int crypt_resume_by_keyfile_device_offset(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ uint64_t keyfile_offset)
+{
+ struct volume_key *vk = NULL;
+ char *passphrase_read = NULL;
+ size_t passphrase_size_read;
+ int r;
+
+ /* FIXME: check context uuid matches the dm-crypt device uuid */
+
+ if (!name || !keyfile)
+ return -EINVAL;
+
+ log_dbg(cd, "Resuming volume %s.", name);
+
+ if ((r = onlyLUKS(cd)))
+ return r;
+
+ r = dm_status_suspended(cd, name);
+ if (r < 0)
+ return r;
+
+ if (!r) {
+ log_err(cd, _("Volume %s is not suspended."), name);
+ return -EINVAL;
+ }
+
+ r = crypt_keyfile_device_read(cd, keyfile,
+ &passphrase_read, &passphrase_size_read,
+ keyfile_offset, keyfile_size, 0);
+ if (r < 0)
+ return r;
+
+ if (isLUKS1(cd->type))
+ r = LUKS_open_key_with_hdr(keyslot, passphrase_read, passphrase_size_read,
+ &cd->u.luks1.hdr, &vk, cd);
+ else
+ r = LUKS2_keyslot_open(cd, keyslot, CRYPT_DEFAULT_SEGMENT,
+ passphrase_read, passphrase_size_read, &vk);
+
+ crypt_safe_free(passphrase_read);
+ if (r < 0)
+ return r;
+
+ keyslot = r;
+
+ r = resume_by_volume_key(cd, vk, name);
+
+ crypt_free_volume_key(vk);
+ return r < 0 ? r : keyslot;
+}
+
+int crypt_resume_by_keyfile(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size)
+{
+ return crypt_resume_by_keyfile_device_offset(cd, name, keyslot,
+ keyfile, keyfile_size, 0);
+}
+
+int crypt_resume_by_keyfile_offset(struct crypt_device *cd,
+ const char *name,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ size_t keyfile_offset)
+{
+ return crypt_resume_by_keyfile_device_offset(cd, name, keyslot,
+ keyfile, keyfile_size, keyfile_offset);
+}
+
+int crypt_resume_by_volume_key(struct crypt_device *cd,
+ const char *name,
+ const char *volume_key,
+ size_t volume_key_size)
+{
+ struct volume_key *vk = NULL;
+ int r;
+
+ if (!name || !volume_key)
+ return -EINVAL;
+
+ log_dbg(cd, "Resuming volume %s by volume key.", name);
+
+ if ((r = onlyLUKS(cd)))
+ return r;
+
+ r = dm_status_suspended(cd, name);
+ if (r < 0)
+ return r;
+
+ if (!r) {
+ log_err(cd, _("Volume %s is not suspended."), name);
+ return -EINVAL;
+ }
+
+ vk = crypt_alloc_volume_key(volume_key_size, volume_key);
+ if (!vk)
+ return -ENOMEM;
+
+ if (isLUKS1(cd->type))
+ r = LUKS_verify_volume_key(&cd->u.luks1.hdr, vk);
+ else if (isLUKS2(cd->type))
+ r = LUKS2_digest_verify_by_segment(cd, &cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT, vk);
+ else
+ r = -EINVAL;
+ if (r == -EPERM || r == -ENOENT)
+ log_err(cd, _("Volume key does not match the volume."));
+
+ if (r >= 0)
+ r = resume_by_volume_key(cd, vk, name);
+
+ crypt_free_volume_key(vk);
+ return r;
+}
+
+int crypt_resume_by_token_pin(struct crypt_device *cd, const char *name,
+ const char *type, int token, const char *pin, size_t pin_size,
+ void *usrptr)
+{
+ struct volume_key *vk = NULL;
+ int r, keyslot;
+
+ if (!name)
+ return -EINVAL;
+
+ log_dbg(cd, "Resuming volume %s by token (%s type) %d.",
+ name, type ?: "any", token);
+
+ if ((r = _onlyLUKS2(cd, CRYPT_CD_QUIET, 0)))
+ return r;
+
+ r = dm_status_suspended(cd, name);
+ if (r < 0)
+ return r;
+
+ if (!r) {
+ log_err(cd, _("Volume %s is not suspended."), name);
+ return -EINVAL;
+ }
+
+ r = LUKS2_token_unlock_key(cd, &cd->u.luks2.hdr, token, type,
+ pin, pin_size, CRYPT_DEFAULT_SEGMENT, usrptr, &vk);
+ keyslot = r;
+ if (r >= 0)
+ r = resume_by_volume_key(cd, vk, name);
+
+ crypt_free_volume_key(vk);
+ return r < 0 ? r : keyslot;
+}
+
+/*
+ * Keyslot manipulation
+ */
+int crypt_keyslot_add_by_passphrase(struct crypt_device *cd,
+ int keyslot, // -1 any
+ const char *passphrase,
+ size_t passphrase_size,
+ const char *new_passphrase,
+ size_t new_passphrase_size)
+{
+ int r;
+ struct crypt_keyslot_context kc, new_kc;
+
+ if (!passphrase || !new_passphrase)
+ return -EINVAL;
+
+ crypt_keyslot_unlock_by_passphrase_init_internal(&kc, passphrase, passphrase_size);
+ crypt_keyslot_unlock_by_passphrase_init_internal(&new_kc, new_passphrase, new_passphrase_size);
+
+ r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
+
+ crypt_keyslot_context_destroy_internal(&kc);
+ crypt_keyslot_context_destroy_internal(&new_kc);
+
+ return r;
+}
+
+int crypt_keyslot_change_by_passphrase(struct crypt_device *cd,
+ int keyslot_old,
+ int keyslot_new,
+ const char *passphrase,
+ size_t passphrase_size,
+ const char *new_passphrase,
+ size_t new_passphrase_size)
+{
+ int digest = -1, r, keyslot_new_orig = keyslot_new;
+ struct luks2_keyslot_params params;
+ struct volume_key *vk = NULL;
+
+ if (!passphrase || !new_passphrase)
+ return -EINVAL;
+
+ log_dbg(cd, "Changing passphrase from old keyslot %d to new %d.",
+ keyslot_old, keyslot_new);
+
+ if ((r = onlyLUKS(cd)))
+ return r;
+
+ if (isLUKS1(cd->type))
+ r = LUKS_open_key_with_hdr(keyslot_old, passphrase, passphrase_size,
+ &cd->u.luks1.hdr, &vk, cd);
+ else if (isLUKS2(cd->type)) {
+ r = LUKS2_keyslot_open(cd, keyslot_old, CRYPT_ANY_SEGMENT, passphrase, passphrase_size, &vk);
+ /* will fail for keyslots w/o digest. fix if supported in a future */
+ if (r >= 0) {
+ digest = LUKS2_digest_by_keyslot(&cd->u.luks2.hdr, r);
+ if (digest < 0)
+ r = -EINVAL;
+ }
+ } else
+ r = -EINVAL;
+ if (r < 0)
+ goto out;
+
+ if (keyslot_old != CRYPT_ANY_SLOT && keyslot_old != r) {
+ log_dbg(cd, "Keyslot mismatch.");
+ goto out;
+ }
+ keyslot_old = r;
+
+ if (keyslot_new == CRYPT_ANY_SLOT) {
+ if (isLUKS1(cd->type))
+ keyslot_new = LUKS_keyslot_find_empty(&cd->u.luks1.hdr);
+ else if (isLUKS2(cd->type))
+ keyslot_new = LUKS2_keyslot_find_empty(cd, &cd->u.luks2.hdr, vk->keylength);
+ if (keyslot_new < 0)
+ keyslot_new = keyslot_old;
+ }
+ log_dbg(cd, "Key change, old slot %d, new slot %d.", keyslot_old, keyslot_new);
+
+ if (isLUKS1(cd->type)) {
+ if (keyslot_old == keyslot_new) {
+ log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old);
+ (void)crypt_keyslot_destroy(cd, keyslot_old);
+ }
+ r = LUKS_set_key(keyslot_new, new_passphrase, new_passphrase_size,
+ &cd->u.luks1.hdr, vk, cd);
+ } else if (isLUKS2(cd->type)) {
+ r = LUKS2_keyslot_params_default(cd, &cd->u.luks2.hdr, ¶ms);
+ if (r)
+ goto out;
+
+ if (keyslot_old != keyslot_new) {
+ r = LUKS2_digest_assign(cd, &cd->u.luks2.hdr, keyslot_new, digest, 1, 0);
+ if (r < 0)
+ goto out;
+ r = LUKS2_token_assignment_copy(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new, 0);
+ if (r < 0)
+ goto out;
+ } else {
+ log_dbg(cd, "Key slot %d is going to be overwritten.", keyslot_old);
+ /* FIXME: improve return code so that we can detect area is damaged */
+ r = LUKS2_keyslot_wipe(cd, &cd->u.luks2.hdr, keyslot_old, 1);
+ if (r) {
+ /* (void)crypt_keyslot_destroy(cd, keyslot_old); */
+ r = -EINVAL;
+ goto out;
+ }
+ }
+
+ r = LUKS2_keyslot_store(cd, &cd->u.luks2.hdr,
+ keyslot_new, new_passphrase,
+ new_passphrase_size, vk, ¶ms);
+ if (r < 0)
+ goto out;
+
+ /* Swap old & new so the final keyslot number remains */
+ if (keyslot_new_orig == CRYPT_ANY_SLOT && keyslot_old != keyslot_new) {
+ r = LUKS2_keyslot_swap(cd, &cd->u.luks2.hdr, keyslot_old, keyslot_new);
+ if (r < 0)
+ goto out;
+
+ /* Swap slot id */
+ r = keyslot_old;
+ keyslot_old = keyslot_new;
+ keyslot_new = r;
+ }
+ } else
+ r = -EINVAL;
+
+ if (r >= 0 && keyslot_old != keyslot_new)
+ r = crypt_keyslot_destroy(cd, keyslot_old);
+
+ if (r < 0)
+ log_err(cd, _("Failed to swap new key slot."));
+out:
+ crypt_free_volume_key(vk);
+ if (r < 0) {
+ _luks2_rollback(cd);
+ return r;
+ }
+ return keyslot_new;
+}
+
+int crypt_keyslot_add_by_keyfile_device_offset(struct crypt_device *cd,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ uint64_t keyfile_offset,
+ const char *new_keyfile,
+ size_t new_keyfile_size,
+ uint64_t new_keyfile_offset)
+{
+ int r;
+ struct crypt_keyslot_context kc, new_kc;
+
+ if (!keyfile || !new_keyfile)
+ return -EINVAL;
+
+ crypt_keyslot_unlock_by_keyfile_init_internal(&kc, keyfile, keyfile_size, keyfile_offset);
+ crypt_keyslot_unlock_by_keyfile_init_internal(&new_kc, new_keyfile, new_keyfile_size, new_keyfile_offset);
+
+ r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
+
+ crypt_keyslot_context_destroy_internal(&kc);
+ crypt_keyslot_context_destroy_internal(&new_kc);
+
+ return r;
+}
+
+int crypt_keyslot_add_by_keyfile(struct crypt_device *cd,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ const char *new_keyfile,
+ size_t new_keyfile_size)
+{
+ return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot,
+ keyfile, keyfile_size, 0,
+ new_keyfile, new_keyfile_size, 0);
+}
+
+int crypt_keyslot_add_by_keyfile_offset(struct crypt_device *cd,
+ int keyslot,
+ const char *keyfile,
+ size_t keyfile_size,
+ size_t keyfile_offset,
+ const char *new_keyfile,
+ size_t new_keyfile_size,
+ size_t new_keyfile_offset)
+{
+ return crypt_keyslot_add_by_keyfile_device_offset(cd, keyslot,
+ keyfile, keyfile_size, keyfile_offset,
+ new_keyfile, new_keyfile_size, new_keyfile_offset);
+}
+
+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)
+{
+ int r;
+ struct crypt_keyslot_context kc, new_kc;
+
+ if (!passphrase)
+ return -EINVAL;
+
+ crypt_keyslot_unlock_by_key_init_internal(&kc, volume_key, volume_key_size);
+ crypt_keyslot_unlock_by_passphrase_init_internal(&new_kc, passphrase, passphrase_size);
+
+ r = crypt_keyslot_add_by_keyslot_context(cd, CRYPT_ANY_SLOT, &kc, keyslot, &new_kc, 0);
+
+ crypt_keyslot_context_destroy_internal(&kc);
+ crypt_keyslot_context_destroy_internal(&new_kc);
+
+ return r;
+}
+
+int crypt_keyslot_destroy(struct crypt_device *cd, int keyslot)
+{
+ crypt_keyslot_info ki;
+ int r;
+
+ log_dbg(cd, "Destroying keyslot %d.", keyslot);
+
+ if ((r = _onlyLUKS(cd, CRYPT_CD_UNRESTRICTED)))
+ return r;
+
+ ki = crypt_keyslot_status(cd, keyslot);
+ if (ki == CRYPT_SLOT_INVALID) {
+ log_err(cd, _("Key slot %d is invalid."), keyslot);
+ return -EINVAL;
+ }
+
+ if (isLUKS1(cd->type)) {
+ if (ki == CRYPT_SLOT_INACTIVE) {
+ log_err(cd, _("Keyslot %d is not active."), keyslot);
+ return -EINVAL;
+ }
+ return LUKS_del_key(keyslot, &cd->u.luks1.hdr, cd);
+ }
+
+ return LUKS2_keyslot_wipe(cd, &cd->u.luks2.hdr, keyslot, 0);
+}
+
+static int _check_header_data_overlap(struct crypt_device *cd, const char *name)
+{
+ if (!name || !isLUKS(cd->type))
+ return 0;
+
+ if (device_is_identical(crypt_data_device(cd), crypt_metadata_device(cd)) <= 0)
+ return 0;
+
+ /* FIXME: check real header size */
+ if (crypt_get_data_offset(cd) == 0) {
+ log_err(cd, _("Device header overlaps with data area."));
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int check_devices(struct crypt_device *cd, const char *name, const char *iname, uint32_t *flags)
+{
+ int r;
+
+ if (!flags || !name)
+ return -EINVAL;
+
+ if (iname) {
+ r = dm_status_device(cd, iname);
+ if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH))
+ return -EBUSY;
+ if (r < 0 && r != -ENODEV)
+ return r;
+ if (r == -ENODEV)
+ *flags &= ~CRYPT_ACTIVATE_REFRESH;
+ }
+
+ r = dm_status_device(cd, name);
+ if (r >= 0 && !(*flags & CRYPT_ACTIVATE_REFRESH))
+ return -EBUSY;
+ if (r < 0 && r != -ENODEV)
+ return r;
+ if (r == -ENODEV)
+ *flags &= ~CRYPT_ACTIVATE_REFRESH;
+
+ return 0;
+}
+
+static int _create_device_with_integrity(struct crypt_device *cd,
+ const char *type, const char *name, const char *iname,
+ const char *ipath, struct crypt_dm_active_device *dmd,
+ struct crypt_dm_active_device *dmdi)
+{
+ int r;
+ enum devcheck device_check;
+ struct dm_target *tgt;
+ struct device *device = NULL;
+
+ if (!single_segment(dmd))
+ return -EINVAL;
+
+ tgt = &dmd->segment;
+ if (tgt->type != DM_CRYPT)
+ return -EINVAL;
+
+ device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;
+
+ r = INTEGRITY_activate_dmd_device(cd, iname, CRYPT_INTEGRITY, dmdi, 0);
+ if (r)
+ return r;
+
+ r = device_alloc(cd, &device, ipath);
+ if (r < 0)
+ goto out;
+ tgt->data_device = device;
+
+ r = device_block_adjust(cd, tgt->data_device, device_check,
+ tgt->u.crypt.offset, &dmd->size, &dmd->flags);
+
+ if (!r)
+ r = dm_create_device(cd, name, type, dmd);
+out:
+ if (r < 0)
+ dm_remove_device(cd, iname, 0);
+
+ device_free(cd, device);
+ return r;
+}
+
+static int kernel_keyring_support(void)
+{
+ static unsigned _checked = 0;
+
+ if (!_checked) {
+ _kernel_keyring_supported = keyring_check();
+ _checked = 1;
+ }
+
+ return _kernel_keyring_supported;
+}
+
+static int dmcrypt_keyring_bug(void)
+{
+ uint64_t kversion;
+
+ if (kernel_version(&kversion))
+ return 1;
+ return kversion < compact_version(4,15,0,0);
+}
+
+int create_or_reload_device(struct crypt_device *cd, const char *name,
+ const char *type, struct crypt_dm_active_device *dmd)
+{
+ int r;
+ enum devcheck device_check;
+ struct dm_target *tgt;
+
+ if (!type || !name || !single_segment(dmd))
+ return -EINVAL;
+
+ tgt = &dmd->segment;
+ if (tgt->type != DM_CRYPT && tgt->type != DM_INTEGRITY)
+ return -EINVAL;
+
+ /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
+ r = check_devices(cd, name, NULL, &dmd->flags);
+ if (r)
+ return r;
+
+ if (dmd->flags & CRYPT_ACTIVATE_REFRESH)
+ r = _reload_device(cd, name, dmd);
+ else {
+ if (tgt->type == DM_CRYPT) {
+ device_check = dmd->flags & CRYPT_ACTIVATE_SHARED ? DEV_OK : DEV_EXCL;
+
+ r = device_block_adjust(cd, tgt->data_device, device_check,
+ tgt->u.crypt.offset, &dmd->size, &dmd->flags);
+ if (!r) {
+ tgt->size = dmd->size;
+ r = dm_create_device(cd, name, type, dmd);
+ }
+ } else if (tgt->type == DM_INTEGRITY) {
+ r = device_block_adjust(cd, tgt->data_device, DEV_EXCL,
+ tgt->u.integrity.offset, NULL, &dmd->flags);
+ if (r)
+ return r;
+
+ if (tgt->u.integrity.meta_device) {
+ r = device_block_adjust(cd, tgt->u.integrity.meta_device, DEV_EXCL, 0, NULL, NULL);
+ if (r)
+ return r;
+ }
+
+ r = dm_create_device(cd, name, type, dmd);
+ }
+ }
+
+ return r;
+}
+
+int create_or_reload_device_with_integrity(struct crypt_device *cd, const char *name,
+ const char *type, struct crypt_dm_active_device *dmd,
+ struct crypt_dm_active_device *dmdi)
+{
+ int r;
+ const char *iname = NULL;
+ char *ipath = NULL;
+
+ if (!type || !name || !dmd || !dmdi)
+ return -EINVAL;
+
+ if (asprintf(&ipath, "%s/%s_dif", dm_get_dir(), name) < 0)
+ return -ENOMEM;
+ iname = ipath + strlen(dm_get_dir()) + 1;
+
+ /* drop CRYPT_ACTIVATE_REFRESH flag if any device is inactive */
+ r = check_devices(cd, name, iname, &dmd->flags);