Dm-crypt och fullständig diskkryptering

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Dm-crypt full disk encryption and the translation is 4% complete.
Outdated translations are marked like this.
Other languages:
Resources

Den här artikeln diskuterar de många aspekter av att använda Dm-crypt för (fullständig) diskkryptering. Den är främst tänkt som ett tillägg för den information som inte redan diskuteras i Dm-crypt, genkernel, eller initramfs-wiki-sidor.

Important
Alla ytterligare säkerhetsstrategier som diskuteras i den här artikeln är valfria. Några aspekter som presenteras här diskuteras enbart därför att de är ofta förekommande frågor, eller högst personliga säkerhetsövertygelser. Målet med denna artikel är således att ge en, förhoppningsvis, objektiv diskussion. cryptsetup FAQ-artikeln är starkt rekommenderad som en extra informationskälla.

Vilken chiffer:hash-kombination?

Standardchiffer för LUKS är nuförtiden aes-xts-plain64, dvs AES som chiffer och XTS som ett sätt att verka. Detta bör ändras endast under särskillda omständigheter. Standarden är ett rimligt val säkerhetsmässigt, och helt klart prestationsmässigt, då det kan ge 2-3 GiB/sek kryptering/dekryptering tid på CPU:er med AES-NI. XTS använder två AES-nycklar, härav möjliga nyckelstorlekar på -s 256 och -s 512.

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
Warning
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.
Note
Additional information on how to use cryptsetup luksFormat is given on the dm-crypt wiki page.
Note
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 ...
Warning
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
Note
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. Such a key file is readily supported by genkernel.

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 ...
Note
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

Warning
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 ...

Warning
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.

Genkernel

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
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.

Dracut

The sys-kernel/dracut package was ported from the RedHat project and serves a similar tool for generating an initramfs. Some modules may be desired, please refer to dracut. Generally, the following command will generate a usable default initramfs.

root #dracut -a crypt

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 root=UUID=<root filesystem uuid> rd.luks.uuid=<encrypted partition uuid>
initrd /boot/initramfs-3.4.0-gentoo.img
Note
Dracut will match UUIDs that are not complete as long as they are not ambiguous, so it is possible to only enter the first stanza for brevity/clarity desired
Note
Note TRIM support may be enabled if the encrypted device is capable (ie. SSD) with rd.luks.allow-discards but please read first about the security implications of this outlined in the --allow-discards section of the cryptsetup wiki.

For a comprehensive list of luks options within dracut please see the section in the dracut manual.

Mkinitramfs-LL

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
'"`UNIQ--pre-00000008-QINU`"'
Note
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