const char *crypt_keyslot_get_encryption(struct crypt_device *cd, int keyslot, size_t *key_size)
{
	const char *cipher;

	if (!cd || !isLUKS(cd->type) || !key_size)
		return NULL;

	if (isLUKS1(cd->type)) {
		if (keyslot != CRYPT_ANY_SLOT &&
		    LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot) < CRYPT_SLOT_ACTIVE)
			return NULL;
		*key_size = crypt_get_volume_key_size(cd);
		return cd->u.luks1.cipher_spec;
	}

	if (keyslot != CRYPT_ANY_SLOT)
		return LUKS2_get_keyslot_cipher(&cd->u.luks2.hdr, keyslot, key_size);

	/* Keyslot encryption was set through crypt_keyslot_set_encryption() */
	if (cd->u.luks2.keyslot_cipher) {
		*key_size = cd->u.luks2.keyslot_key_size;
		return cd->u.luks2.keyslot_cipher;
	}

	/* Try to reuse volume encryption parameters */
	cipher =  LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
	if (!LUKS2_keyslot_cipher_incompatible(cd, cipher)) {
		*key_size = crypt_get_volume_key_size(cd);
		if (*key_size)
			return cipher;
	}

	/* Fallback to default LUKS2 keyslot encryption */
	*key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8;
	return DEFAULT_LUKS2_KEYSLOT_CIPHER;
}
const char *crypt_keyslot_get_encryption(struct crypt_device *cd, int keyslot, size_t *key_size)
{
	const char *cipher;

	if (!cd || !isLUKS(cd->type) || !key_size)
		return NULL;

	if (isLUKS1(cd->type)) {
		if (keyslot != CRYPT_ANY_SLOT &&
		    LUKS_keyslot_info(&cd->u.luks1.hdr, keyslot) < CRYPT_SLOT_ACTIVE)
			return NULL;
		*key_size = crypt_get_volume_key_size(cd);
		return cd->u.luks1.cipher_spec;
	}

	if (keyslot != CRYPT_ANY_SLOT)
		return LUKS2_get_keyslot_cipher(&cd->u.luks2.hdr, keyslot, key_size);

	 
	if (cd->u.luks2.keyslot_cipher) {
		*key_size = cd->u.luks2.keyslot_key_size;
		return cd->u.luks2.keyslot_cipher;
	}

	 
	cipher =  LUKS2_get_cipher(&cd->u.luks2.hdr, CRYPT_DEFAULT_SEGMENT);
	if (!LUKS2_keyslot_cipher_incompatible(cd, cipher)) {
		*key_size = crypt_get_volume_key_size(cd);
		if (*key_size)
			return cipher;
	}

	 
	*key_size = DEFAULT_LUKS2_KEYSLOT_KEYBITS / 8;
	return DEFAULT_LUKS2_KEYSLOT_CIPHER;
}