dm-crypt/Drive preparation

From ArchWiki
< Dm-crypt
Revision as of 18:26, 18 November 2013 by Indigo (talk | contribs) (Discard/TRIM support for solid state disks (SSD): moved to Dm-crypt_with_LUKS/Drive_Preparation)
Jump to: navigation, search

Back to Dm-crypt with LUKS/draft.

Secure erasure of the hard disk drive

Before encrypting a drive, you should perform a secure erase of the disk by overwriting the entire drive with random data. To prevent cryptographic attacks or unwanted File Recovery, this data should be completely indistinguishable from all data later written by dm-crypt.

In deciding which method to use for secure erasure of a hard disk drive, remember that this needs only to be performed once for as long as the drive is used as an encrypted drive.

Generic methods

For detailed instructions on how to erase and prepare a drive consult Securely wipe disk.

LUKS-specific methods

The following methods are specific for dm-crypt/LUKS and are mentioned complementarily, because they can be performed after a partition setup too.

Use LUKS container as pseudorandom number generator

The cryptsetup FAQ mentions a very simple procedure to use an existing dm-crypt-volume to wipe all free space accessible on the underlying block device with random data by acting as a simple pseudorandom number generator. It is also claimed to protect against disclosure of usage patterns.

# dd if=/dev/zero of=/dev/mapper/luks-container

Wipe free space with encrypted file after Installation

The same effect can be achieved if a file is created on each encrypted partition that fills the partition completely after the system is installed, booted and filesystems mounted. That is because encrypted data is indistinguishable from random.

# dd if=/dev/zero of=/file/in/luks-container
# rm /file/in/luks-container

The above process has to be repeated for every container created.

Wipe LUKS keyslots

#cryptsetup luksKillSlot <device> <key slot number>

This will only wipe a single keyslot.

Wipe LUKS header

The partitions formatted with dm-crypt/LUKS contain a header with the cipher and crypt-options used, which is referred to dm-mod when opening the blockdevice. After the header the actual random data partition starts. Hence, when de-commissioning a drive (e.g. sale of PC, switch of drives, etc.) it may be just enough to wipe the header of the partition, rather than overwriting the whole drive - which can be a lengthy process.

Wiping the LUKS header will delete the PBKDF2-encrypted (AES) master key, salts and so on.

Note: It is crucial to write to the LUKS encrypted partition (/dev/sda1 in this example) and not directly to the disks device node. If you did set up encryption as a device-mapper layer on top of others, e.g. LVM on LUKS on RAID then write to RAID respectively.

A header with one single default 256 bit size keyslot is 1024KB in size. It is advised to also overwrite the first 4KB written by dm-crypt, so 1028KB have to be wiped. That is 1052672 Byte.

For zero offset use:

#head -c 1052672 /dev/zero > /dev/sda1; sync

For 512 bit key length (e.g. for aes-xts-plain with 512 bit key) the header is 2MB.

If in doubt, just be generous and overwrite the first 10MB or so.

#dd if=/dev/zero of=/dev/sda1 bs=512 count=20480
Note: With a backup-copy of the header data can get rescued but the filesystem was likely damaged as the first encrypted sectors were overwritten. See further sections on how to make a backup of the crucial header blocks.

When wiping the header with random data everything left on the device is encrypted data. An exception to this may occur for an SSD, because of cache blocks SSDs employ. In theory it may happen that the header was cached in these some time before and that copy may consequently be still available after wiping the original header. For strong security concerns, a secure ATA erase of the SSD should be done (procedure please see the cryptsetup FAQ 5.19).


After the drive has been securely overwritten, it is time to create partitions and begin setting up an encrypted system.

There are multiple ways to create disk partitions:

LUKS can be used with systems that require LVM and/or RAID and is compatible with all regular partitioning standards in Linux.

Creating Disk Partitions

There are two required partitions for a basic encrypted system setup:

root file system
/ Will be encrypted and store all system and user files (/usr, /bin, /var, /home, etc.)
initial boot partition
/boot Will not be encrypted; the bootloader needs to access the /boot directory where it will load the initramfs/encryption modules needed to load the rest of the system which is encrypted (see Mkinitcpio for details). For this reason, /boot needs to reside on its own, unencrypted partition.

This partition layout is encrypted in this example and can be refined according to needs, e.g. by separating partitions or adding an encrypted swap-partition.

Single Disk Systems

If there are additional partitions desired, these can be individually created by defining separate primary or extended/logical partitions. It is possible to encrypt separate partitions (e.g. /home, /var) while leaving others (e.g. /usr) unencrypted as required. However, a standard install would also require separate passphrases or keys to open each encrypted partition during boot.

Multiple Disk Systems

In systems with multiple hard disk drives, the same options exist as a single disk system. After the creation of the /boot partition, the remaining free space on the physical disks can be divided up into their respective partitions at this level. For encrypted partitions that span multiple disks, LUKS must be used with RAID or LVM.

LVM: Logical Volume Manager

The LVM allows for systems that require complex hard disk configurations. Knowledge of using LVM is a requisite to continue with setting up LUKS with LVM later in this article.

Tip: Btrfs has a built-in Subvolume-Feature that fully replaces the need for LVM if no other filesystems are required. An encrypted swap is not possible this way and swap files are not supported by btrfs up to now.