int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
	int keyslot_old,
	int keyslot_new,
	const char *password,
	size_t password_len,
	struct volume_key **vks)
{
	struct volume_key *vk = NULL;
	int digest_old, digest_new, r = -EINVAL;
	struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2);

	digest_old = LUKS2_reencrypt_digest_old(hdr);
	if (digest_old >= 0) {
		log_dbg(cd, "Trying to unlock volume key (digest: %d) using keyslot %d.", digest_old, keyslot_old);
		r = LUKS2_keyslot_open_by_digest(cd, hdr, keyslot_old, digest_old, password, password_len, &vk);
		if (r < 0)
			goto out;
		crypt_volume_key_add_next(vks, vk);
	}

	digest_new = LUKS2_reencrypt_digest_new(hdr);
	if (digest_new >= 0 && digest_old != digest_new) {
		log_dbg(cd, "Trying to unlock volume key (digest: %d) using keyslot %d.", digest_new, keyslot_new);
		r = LUKS2_keyslot_open_by_digest(cd, hdr, keyslot_new, digest_new, password, password_len, &vk);
		if (r < 0)
			goto out;
		crypt_volume_key_add_next(vks, vk);
	}
out:
	if (r < 0) {
		crypt_free_volume_key(*vks);
		*vks = NULL;

		if (r == -ENOMEM)
			log_err(cd, _("Not enough available memory to open a keyslot."));
		else if (r != -EPERM && r != -ENOENT)
			log_err(cd, _("Keyslot open failed."));
	}
	return r;
}
int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
	int keyslot_old,
	int keyslot_new,
	const char *password,
	size_t password_len,
	struct volume_key **vks)
{
	struct volume_key *vk = NULL;
	int digest_old, digest_new, r = -EINVAL;
	struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2);

	digest_old = LUKS2_reencrypt_digest_old(hdr);
	if (digest_old >= 0) {
		log_dbg(cd, "Trying to unlock volume key (digest: %d) using keyslot %d.", digest_old, keyslot_old);
		r = LUKS2_keyslot_open_by_digest(cd, hdr, keyslot_old, digest_old, password, password_len, &vk);
		if (r < 0)
			goto out;
		crypt_volume_key_add_next(vks, vk);
	}

	digest_new = LUKS2_reencrypt_digest_new(hdr);
	if (digest_new >= 0 && digest_old != digest_new) {
		log_dbg(cd, "Trying to unlock volume key (digest: %d) using keyslot %d.", digest_new, keyslot_new);
		r = LUKS2_keyslot_open_by_digest(cd, hdr, keyslot_new, digest_new, password, password_len, &vk);
		if (r < 0)
			goto out;
		crypt_volume_key_add_next(vks, vk);
	}
out:
	if (r < 0) {
		crypt_free_volume_key(*vks);
		*vks = NULL;

		if (r == -ENOMEM)
			log_err(cd, _("Not enough available memory to open a keyslot."));
		else if (r != -EPERM && r != -ENOENT)
			log_err(cd, _("Keyslot open failed."));
	}
	return r;
}