Keyfiles
Outcome: a vault that a file can unlock — made, proven, and removed again — and a clear view of when that is a good idea. Everything here ran in the lab; transcript t04.
What a keyfile is
A keyfile is a file whose bytes are the key material for a slot, instead of a typed passphrase. It occupies a slot exactly like a passphrase does, is wrapped around the same master key, and is subject to the same rules: it can be added, removed, and killed by the machinery of chapter 6. The differences are all in the handling:
- A passphrase is something you know; a keyfile is something you have. It can be long and perfectly random in a way no typable secret can — 4096 random bytes carries more entropy than any sentence.
- Anything that can read the file can open the vault. There is no two-second Argon2 wall in front of copying a file. The keyfile's protection is file permissions and physical control, full stop.
- Keyfiles unlock machines, not memories: their natural home is a second disk that should open at boot without a human typing (chapter 9), or a USB stick that is the key to a laptop.
Making one and adding it
Random bytes, strict permissions,
then the same luksAddKey as before,
with the new key named as a file:
dd if=/dev/urandom of=vault-key.bin \
bs=1 count=4096
chmod 400 vault-key.bin
cryptsetup luksAddKey \
--key-file pass1.txt vault.img \
vault-key.bin
The lab proved the result immediately:
cryptsetup open --test-passphrase \
--key-file vault-key.bin vault.img
Exit 0: the file alone unlocks. The
dump showed the keyfile living in
slot 1, indistinguishable from a
passphrase slot — because at the
header level there is no difference
to show. A text passphrase can also
live in a file (the lab's pass*.txt
fixtures do exactly that); cryptsetup
does not care whether the bytes came
from a keyboard-shaped file or a
random one. Which leads to the trap.
The newline trap, demonstrated
pass1.txt holds the lab's passphrase
with no trailing newline, written
by printf '%s'. The lab made a
second file with the same visible
text the way most people would — with
an editor, or echo, both of which
end the line — and tested it:
$ cryptsetup open --test-passphrase \
--key-file pass1-nl.txt vault.img
No key available with this passphrase.
One invisible byte, \n, makes a
different key. cryptsetup reads a key
file's bytes exactly; it does not
trim, tidy or forgive. If you script
with passphrase files, write them the
printf '%s' way, and if a keyfile
"should work" and doesn't, count its
bytes before you doubt anything else:
wc -c on the file and on the phrase
will disagree by exactly the size of
your problem.
Housekeeping, because the file is the key
- Permissions are the lock.
chmod 400(or 600 at most), owned by root, on a filesystem that is itself protected. A keyfile on the same unencrypted disk as the vault is a key taped to the safe: fine for opening a second disk at boot, useless for the disk it sits on. - Back it up like a key, not like
data. A lost keyfile is a dead
slot, exactly as final as a forgotten
passphrase (chapter 6, rule 1). A
copy in a sealed envelope or a
password manager's attachment store
is a reasonable spare; a copy in
cloud sync named
disk-key.binis a decision you have made about your threat model, so make it on purpose. - Removing it is chapter 6 again:
luksRemoveKey --key-file vault-key.bin vault.imgnames the slot by the file itself. The lab did exactly this to leave its practice vault with one slot (t04) — after which the file is just 4096 random bytes again, and can be deleted like any other secret: overwritten, not merely unlinked, if the medium allows it.