dm-crypt full disk encryption

From Gentoo Wiki
(Redirected from DM-Crypt LUKS)
Jump to:navigation Jump to:search
This page contains changes which are not marked for translation.
Other languages:


This article discusses several aspects of using dm-crypt for (full) disk encryption. It is primarily meant as a source of supplementary information not already discussed within the dm-crypt, genkernel, or initramfs wiki pages.

All additional security strategies discussed in this article are optional. Some aspects presented here are solely discussed because they are routinely subject of questions, or highly personal security beliefs. The aim of this article is thus to provide a, hopefully, objective discussion. The cryptsetup FAQ is a highly recommended additional source of information.

Which cipher:hash combination?

The default cipher for LUKS is nowadays aes-xts-plain64, i.e. AES as cipher and XTS as mode of operation. This should be changed only under very rare circumstances. The default is a very reasonable choice security wise and by far the best choice performance wise that can deliver between 2-3 GiB/s encryption/decryption speed on CPUs with AES-NI.

The AES-NI availability can be verified as:

user $grep -m1 'aes' /proc/cpuinfo

XTS uses two AES keys, hence possible key sizes are -s 256 and -s 512.

The default choice of cipher and key size can be overriden by the command line parameters -c and -s, for example

root #cryptsetup luksFormat -c aes-xts-plain64 -s 512 ...
root #cryptsetup luksFormat -c aes-cbc-essiv:sha256 -s 256 ...

To know more about the performance on a given set of cipher and mode of operation a benchmark can be run:

root #cryptsetup -c ... -s ... benchmark

Issuing the command without -c and -s runs the benchmark for a number of different choices. For example, a (slightly shortened) output for a mid-2014 Intel Core i7 CPU might look like:

root #cryptsetup benchmark
# Tests are approximate using memory only (no storage IO).
#  Algorithm | Key |  Encryption |  Decryption
     aes-cbc   256b   469.7 MiB/s  2093.2 MiB/s
 serpent-cbc   256b    85.3 MiB/s   523.3 MiB/s
 twofish-cbc   256b   173.4 MiB/s   340.2 MiB/s
     aes-xts   256b  2386.7 MiB/s  2387.4 MiB/s
 serpent-xts   256b   548.6 MiB/s   531.1 MiB/s
 twofish-xts   256b   320.0 MiB/s   342.3 MiB/s
dm-crypt supports many different combinations of ciphers, modes of operation, and IV modes DMCrypt wiki. Some of these are not as secure as others, some are considered to be highly insecure (for example ECB). See Wikipedia more about cryptographic ciphers and modes of operation. If in doubt, stick with the default parameters.
Additional information on how to use cryptsetup luksFormat is given on the dm-crypt wiki page.
Some cipher choices have an additional hash specification of the form cipher:hash, for example aes-cbc-essiv:sha256. Such a hash specification is used by some mode of operations (like cbc-essiv) for IV generation. Mode of operations with plain64, however, do not use such a supplied hash specification (for generating an IV). In this case the hash specification should be omitted.

What choice of hash for key derivation?

LUKS uses PBKDF2 for key derivation. In essence, the supplied passphrase by the user is combined with a salt and hashed a specified number of rounds. This key stretching makes the password more secure against brute force attacks. The hash function used in PBKDF2 can be set via -h. The default is sha256 and can (depending on taste) be changed to another secure hash algorithm. The total number of iterations is determined by the speed of the current hardware and can be influenced by setting the number of milliseconds that shall be spent in PBKDF2 passphrase processing by --iter-time. To increase the default from 2s to 3s and use sha512 one could for example use:

root #cryptsetup luksFormat ... -h sha512 --iter-time 3000 ...
It is not recommended to lower the number of PBKDF2 iterations below the default value per the upstream FAQ.

On passphrases, detached LUKS headers, and (encrypted) keyfiles

Use a reasonably long passphrase (use, e.g., 8-12 common random words, see xkcd on that subject) in combination with PBKDF2 for key stretching.

Additional protection against brute force attacks can be achieved by setting up a an external USB flash drive to store essential decryption information (like a keyfile, or the LUKS header itself). The flash drive then has the equivalent function of a physical key; opening the encrypted partition is only possible if both, flash drive and passphrase, are provided. However, this comes with a significant downside in terms of complexity, for example for setting up full disk encryption, or potential to lose decryption keys (by losing the USB flash drive).

Detached LUKS header

It is possible to encrypt a partition with detached LUKS header (where all information about password derivation is stored) that is stored at (physically) different location, e.g., a USB flash drive. This leaves an attacker that is not in possession of the flash drive with no information about key derivation and encryption algorithms used. This makes brute force attacks potentially more difficult.

The following commands first create a file luks-header with a fixed size of 5MB. Then, a detached LUKS header is written to the file. (See the dm-crypt wiki page for further information on how to use cryptsetup luksFormat, or cryptsetup open commands.)

root #truncate -s 5M /path/to/luks-header
root #cryptsetup luksFormat ... --header /path/to/luks-header ...

To check that the header was written successfully, run cryptsetup luksDump /path/to/luks-header.

In order to open the encrypted device, issue:

root #cryptsetup open ... --header /path/to/luks-header
A detached LUKS header is currently not supported by genkernel.

Generating a GnuPG encrypted keyfile

The more traditional approach to use a USB flash drive is to store a GnuPG encrypted keyfile (that contains sufficient entropy) on it.

The following commands create a GnuPG encrypted keyfile of 512 bits and set up an encrypted partition with it:

root #dd if=/dev/urandom count=64 | gpg --symmetric --cipher-algo aes --armor > /path/to/key.gpg
root #gpg --decrypt /path/to/key.gpg | cryptsetup luksFormat ...

In order to open the encrypted device,

root #gpg --decrypt /path/to/key.gpg | cryptsetup open ...
dm-crypt, of course, also works with clear, non-encrypted keyfiles, see the dm-crypt wiki page. The usage of clear keyfiles stored on the root partition to "chain open" encrypted partitions is discussed further down below.

Preparing disks

Extra precautions must be taken if the strategy outlined in this section is applied to an SSD.

It is sometimes recommended to overwrite the whole disk with random numbers prior to setting up disk encryption. The rationale behind this recommendation is that reading an encrypted disk should leak as little information as possible. But if unused blocks (by the file system) are still all zero, an adversary can recover some high level information by determining which (encryption) blocks are likely used (because they contain random looking data) and which are unused (because they are, for example, all zero). The number and location of likely used encryption blocks can reveal information such as disk usage, the file system in use, or likely average file size.

Thus, a somewhat effective (but time consuming) counter measure is to overwrite the disk with random data before using it. An efficient way to do generate large amounts of random data quickly is to use a cryptsetup mapping. For example, in order to overwrite /dev/sdXX, use

root #cryptsetup open --type=plain --key-file=/dev/urandom /dev/sdXX delete_sdXX
root #dd if=/dev/zero of=/dev/mapper/delete_sdXX bs=1M status=progress
root #cryptsetup close delete_sdXX

After that, format the disk with cryptsetup luksFormat ...

Overwriting a whole SSD with random data will mark every block as used and can, depending on manufacturer and model, severely degrade wear leveling. This might have potentially disastrous effects on disk lifetime. As mitigation, it is sometimes recommended to leave a substantial portion of the disk, around 10 - 20%, unused to have enough empty blocks for wear leveling. This can be done by creating a suitably sized, unformatted partition, and properly discarding it with blkdiscard. Please note that mounting a LUKS container with the --allow-discards option will transparently forward discards to the SSD and would contradict above setup.

Dm-crypt on SSDs and hybrid drives

For additional information about security aspects of using dm-crypt on SSDs and hybrid drives, have a look at the cryptsetup FAQ.

Cryptsetup can transparently forward discard operations to an SSD. This feature is activated by using the --allow-discards option in combination with cryptsetup open. Enabling discards on an encrypted SSD can be a measure to ensure effective wear leveling and longevity, especially if the full disk is encrypted. For an in detail discussion about the security implications, have a look at the cryptsetup FAQ and the man page of cryptsetup.

Generating an initramfs

After encrypting system or disk(s), one will need an initramfs so that rootfs can be mounted in there and then pass the control to real init. There are a few generic initramfs builder that can be used to accomplish the task such as dracut, mkinitcpio (there's a thread in the forums and an ebuild: sys-kernel/mkinitramfs-ll) or even genkernel (or the next variant) which has LUKS support.


The following example will use sys-kernel/genkernel to build only an initramfs (not an entire kernel) and enable support for luks:

root #genkernel --luks --lvm initramfs

For a more complete set of explanations refer to the comments in /etc/genkernel.conf itself or to the output of man genkernel.

The initrd will require parameters to tell it how to decrypt the drive, and they are supplied the same way as other kernel parameters. For example:

FILE grub.conf
title Gentoo Linux 3.4.0-gentoo
root (hd0,0)
kernel /boot/kernel-3.4.0-gentoo crypt_root=UUID=<encrypted partition uuid> root=/dev/mapper/root
initrd /boot/initramfs-3.4.0-gentoo
Note TRIM support may be enabled if the encrypted device is capable (ie. SSD) with root_trim=yes but please read first about the security implications of this outlined in the --allow-discards section of the cryptsetup wiki.

Further information can be found in the genkernel article.


Administrators may instead refer to Dracut for a working set up using dracut.

If you're using dm-crypt full disk encryption with Dracut without systemd, be cautious when specifying the parameter in GRUB. There have been reports of issues when this parameter is set in GRUB or systemd-boot. It's recommended to place this parameter in the /etc/dracut.conf as the kernel cmdline parameter instead. For example:
FILE /etc/dracut.conf

The sys-kernel/dracut package was ported from the RedHat project and serves as a tool for generating an initramfs.

When using Distribution kernels, then Dracut is already installed and used.

In order to use it to boot the root filesystem from an encrypted drive, some configuration needs to be done. The package sys-fs/cryptsetup is required to be installed on the system before generating the image.

Here's an example config:

FILE /etc/dracut.conf.d/luks.conf
kernel_cmdline="rd.luks.uuid=<encrypted drive UUID> root=UUID=<decrypted drive UUID>"
add_dracutmodules+=" crypt "

If the drive is an SSD, add this to kernel_cmdline to allow trim/Discard:

rd.luks.allow-discards=<encrypted drive UUID>

In order to get the UUIDs, discover the device paths using:

root #fdisk -l

From here, pick the path of the encrypted partition, then run:

root #blkid /dev/xxx

Copy the UUID and add it to the dracut config.

Now find the decrypted drive UUID. If the device isn't decrypted, do it manually using the command:

root #cryptsetup luksOpen /dev/xxx myname

/dev/xxx is the same one picked from the earlier commands.

Then to get the decrypted drive UUID run:

root #blkid /dev/mapper/myname

Copy the UUID and add it to the Dracut config.

If the device is already decrypted, there should be a file in /dev/mapper with a possible name starting with luks-. Use that filename for the command above.

After creating the config file, don't forget to create the image using:

root #dracut

If the image already exists, run this command to overwrite the old image:

root #dracut --force

For a comprehensive list of LUKS options within dracut please see the section in the link [1].


The unofficial sys-kernel/mkinitramfs-ll (found in tokiclover's bar-overlay) is a lightweight and modular variant of the previous well known initramfs generating tools which comes with udev free dependency. It depends only on busybox with mdev by default and depends on extra packages for additional functionalities. So, nor bash, coreutils nor util-linux is bundled into the initramfs. Extra flexibilities are offered as well, like the possibility to have DM-Crypt LUKS on top of LVM or vice versa, btrfs or ZFS on top of DM-Crypt LUKS, DM-Crypt LUKS on top of RAID, detached header (to a device or a file) for dm-crypt LUKS et al.

root #mkinitramfs-ll --luks --lvm --firmware=iwlwifi-5000

Use the following GRUB2 configuration excerpt to get going for LVM/LUKS and regular key file on a removable device setup.

FILE /etc/default/grub
GRUB_CMDLINE_LINUX="root=<VG-LV> luks=reg:LABEL=PENDRIVE:/key.reg lvm=<MAPPING>-UUID=<uuid>"
There is an optional environment variable in the configuration file (mkinitramfs-ll.conf) to set environment variables that have the same effect as the the kernel command line arguments that let boot with a kernel cmdline free kernel. This default cmdline (as environment variables), however, can be disabled at run time if necessary by appending env=No cmdline argument.

See also