LUKS Encryption – Storing Keyfile in Encrypted USB Drive

cryptsetupencryptionluks

I already asked once about LUKS unlocking of multiple HDDs in Linux: LUKS and multiple hard drives.

Now I would like to know how to secure store the keyfile used for the automatic unlock of the associated partitions.

My plan is (if possible):

  • Encrypt a small USB drive with LUKS that requires a passphrase

  • Unlock it at boot as the first drive by using the passphrase

  • Mount it to a given mount point, for instance /test (is this possible ?)

  • Now the keyfile can be safely read: /test/keyfile

  • Use the keyfile to unlock other drives without needing to ask password for them

  • LuksClose the USB drive in order to assure a certain degree of safety as soon as other drives have been unlocked

  • Automount /, /usr, /var and other mount-points as usual

Can this work? Basically I store the LUKS keyfile on a password-encrypted LUKS USB drive that only asks for passphrase once, while all other drives can be unlocked without further action. I'm not sure if there is some way to make the USB drive be unlocked first, then be mounted and only then the other drives try to access the keyfile. Furthermore in what concerns automation I suppose /etc/fstab and /etc/crypttab should be accessible BEFORE the other drives can be mounted, but this is not possible if the whole / file system is LUKS encrypted.

Unless there is the possibility of fully manually configure how LUKS works:

  • LuksOpen /dev/sdc1 usb_keyfile

  • mount /dev/mapper/usb_keyfile /keyfile (is this possible ?)

  • LuksOpen –keyfile /keyfile/key /dev/sda1 disk1

  • LuksOpen –keyfile /keyfile/key /dev/sdb1 disk2

  • LuksClose /dev/sdc1

Basically being able to run a shell script just after the required modules have been loaded and disable automatic LUKS password prompt.

Additionnal details

  • Distribution used: Gentoo GNU/Linux (amd64) or Debian GNU/Linux (amd64) because I'd like to apply this procedure to multiple installations

Best Answer

Your approach looks good. Some remarks though:

  • If you want to encrypt rootfs, you'll need to use initrd (to have some minimal unencrypted system that will process the encrypted partitions).

    If the USB device is removable, both initrd and kernel can be stored on the USB to heighten tamper resistance (supposing you make sure the USB won't get into unauthorized hands) - which is usually why one encrypts rootfs. Once both kernel and initrd are on a removable media, you can ensure that nobody changes the kernel (or initrd) from the running system by simply removing the media.

    This is of course not an option if you want to have it inside of a server, but then again the question stands whether it makes sense to have such a device at all and not to use a small partition on one of the hard-drives. If for example all drives in the machine are in RAID, one would probably want to put rootfs on the USB as well. An interesting alternative to an internally connected USB flash device could be a CompactFlash card attached to ATA interface through an adapter, by the way.

    Some distributions offer prepared solutions for encrypted root, some don't - but mostly it's question of putting a couple of lines into initrd script before it tries to mount the "real" root (see for example man pages for pivot_root, usually in sections 2 (syscall) and 8 (bonary), if you're not familiar with the process).

  • remember to backup the keys and passphrases in case your USB drive dies. LUKS follows rather one-sided approach when it comes to damaging its header - once a single sector of its header (key-slot to be more precise) dies, you are unable to mount it. This is to make sure that erasing the header isn't effectively thwarted by block reallocation performed by the device itself (because that's what flash-memory based devices do a lot) - the key is spread over the whole key slot and one needs all of the data to reconstruct it - there is no redundancy. See Clemens Fruwirth's website for deeper discussion.

    That said, maybe a simple encrypted device on the USB would be enough (check section PLAIN MODE in man cryptsetup). Or a file encrypted with e.g. openssl enc. The former might actually be an option even for the encrypted partitions themselves.

Related Question