static int reencrypt_verify_and_upload_keys(struct crypt_device *cd, struct luks2_hdr *hdr, int digest_old, int digest_new, struct volume_key *vks)
{
	int r;
	struct volume_key *vk;

	if (digest_new >= 0) {
		vk = crypt_volume_key_by_id(vks, digest_new);
		if (!vk)
			return -ENOENT;
		else {
			if (LUKS2_digest_verify_by_digest(cd, hdr, digest_new, vk) != digest_new)
				return -EINVAL;

			if (crypt_use_keyring_for_vk(cd) && !crypt_is_cipher_null(reencrypt_segment_cipher_new(hdr)) &&
			    (r = LUKS2_volume_key_load_in_keyring_by_digest(cd, hdr, vk, crypt_volume_key_get_id(vk))))
				return r;
		}
	}

	if (digest_old >= 0 && digest_old != digest_new) {
		vk = crypt_volume_key_by_id(vks, digest_old);
		if (!vk) {
			r = -ENOENT;
			goto err;
		} else {
			if (LUKS2_digest_verify_by_digest(cd, hdr, digest_old, vk) != digest_old) {
				r = -EINVAL;
				goto err;
			}
			if (crypt_use_keyring_for_vk(cd) && !crypt_is_cipher_null(reencrypt_segment_cipher_old(hdr)) &&
			    (r = LUKS2_volume_key_load_in_keyring_by_digest(cd, hdr, vk, crypt_volume_key_get_id(vk))))
				goto err;
		}
	}

	return 0;
err:
	crypt_drop_keyring_key(cd, vks);
	return r;
}
static int reencrypt_verify_and_upload_keys(struct crypt_device *cd, struct luks2_hdr *hdr, int digest_old, int digest_new, struct volume_key *vks)
{
	int r;
	struct volume_key *vk;

	if (digest_new >= 0) {
		vk = crypt_volume_key_by_id(vks, digest_new);
		if (!vk)
			return -ENOENT;
		else {
			if (LUKS2_digest_verify_by_digest(cd, hdr, digest_new, vk) != digest_new)
				return -EINVAL;

			if (crypt_use_keyring_for_vk(cd) && !crypt_is_cipher_null(reencrypt_segment_cipher_new(hdr)) &&
			    (r = LUKS2_volume_key_load_in_keyring_by_digest(cd, hdr, vk, crypt_volume_key_get_id(vk))))
				return r;
		}
	}

	if (digest_old >= 0 && digest_old != digest_new) {
		vk = crypt_volume_key_by_id(vks, digest_old);
		if (!vk) {
			r = -ENOENT;
			goto err;
		} else {
			if (LUKS2_digest_verify_by_digest(cd, hdr, digest_old, vk) != digest_old) {
				r = -EINVAL;
				goto err;
			}
			if (crypt_use_keyring_for_vk(cd) && !crypt_is_cipher_null(reencrypt_segment_cipher_old(hdr)) &&
			    (r = LUKS2_volume_key_load_in_keyring_by_digest(cd, hdr, vk, crypt_volume_key_get_id(vk))))
				goto err;
		}
	}

	return 0;
err:
	crypt_drop_keyring_key(cd, vks);
	return r;
}