How to remove an unknown key from LUKS with cryptsetup?

My LUKS encrypted drive has 3 passphrases. Two of them are secure (and long), the other one is lost. However, I dimly remember that it was not up to par; it was used during experiments and should have been wiped afterwards. How can I get rid of that key, given I do not know it anymore, but at least the other two?


Solution 1:

The magic option is luksKillSlot.

The point is to figure out which of your three keyslots contains the key to be deleted. If you don't know it yet, you can check this out by trying all the known keys one after another and let cryptsetup tell you which key refers to which slot. The unknown key then refers to the remaining slot.

Check which slots are used (in my case slots 0, 1, and 2 are used). Replace /dev/sdb4 with your actual device:

root@host:~# cryptsetup luksDump /dev/sdb4
LUKS header information for /dev/sdb4
...
Key Slot 0: ENABLED
...
Key Slot 1: ENABLED
...
Key Slot 2: ENABLED
...
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

Now open (=decrypt) your device with your first key and let cryptsetup be verbose so it shows which slot was used to unlock the device:

root@host:~# cryptsetup -v open --type luks /dev/sdb4 someAlias
[enter one of your two known keys]
Key slot 2 unlocked.
Command successful.

Remember which slot (2 in this case) the first key refered to and undo the step:

root@host:~# cryptsetup close someAlias

Repeat with your second known key:

root@host:~# cryptsetup -v open --type luks /dev/sdb4 someAlias
[enter the second of your two known keys]
Key slot 0 unlocked.
Command successful.
root@host:~# cryptsetup close someAlias

Now you know that the two known keys refer to slot 2 and slot 0. So slot 1 must be the one that contains the unknown key. Delete it with:

root@host:~# cryptsetup -v luksKillSlot /dev/sdb4 1
Keyslot 1 is selected for deletion.
Enter any remaining passphrase: 
[enter one of the two known keys]
Key slot 0 unlocked.
Command successful.

Check it out:

root@host:~# cryptsetup luksDump /dev/sdb4
LUKS header information for /dev/sdb4
...
Key Slot 0: ENABLED
...
Key Slot 1: DISABLED
Key Slot 2: ENABLED
...
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED

Solution 2:

I cannot comment, but to extend @PerlDucks answer

Instead of having to open/lock the partition for each key you want to test, you can use cryptsetup open (or cryptsetup luksOpen - old syntax) with --test-passphrase flag, the someAlias then can be omitted.

Example: cryptsetup -v open --test-passphrase --type luks /dev/sdb4

Snippet from man cryptsetup

...
--test-passphrase
    Do not activate the device, just verify passphrase.  This option  is  only  relevant  for 
    open action (the device mapping name is not mandatory if this option is used).
...

According to cryptsetup changelog, this flag is available since 2012-06-18, or for cryptsetup version since 1.5.0-rc2 (that means, it should be available to pretty much everyone today)