Difference between revisions of "Dm-crypt/Drive preparation"

From ArchWiki
Jump to: navigation, search
(LUKS-specific methods: renaming to dm-crypt as they are generally applicable; adjusting content for it)
m (dm-crypt temporary container as pseudorandom number generator: wording)
Line 28: Line 28:
Disk /dev/mapper/container doesn't contain a valid partition table}}
Disk /dev/mapper/container doesn't contain a valid partition table}}
Finally, fill it with pseudorandom (/encrypted data)   
Finally, fill it with pseudorandom (encrypted data), a use of {{/dev/urandom}} is not required as the encryption cipher is used for randomness:  
{{hc|# dd <nowiki>if=/dev/zero of=</nowiki>/dev/mapper/container|
{{hc|# dd <nowiki>if=/dev/zero of=</nowiki>/dev/mapper/container|
dd: writing to ‘/dev/mapper/container’: No space left on device}}
dd: writing to ‘/dev/mapper/container’: No space left on device}}
Now the next step is [[#Partitioning]].  
Now the next step is [[#Partitioning]].
==== Wipe free space with encrypted file after installation ====
==== Wipe free space with encrypted file after installation ====

Revision as of 21:30, 3 December 2013

Back to Dm-crypt.

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.

Tip: The process of filling an encrypted drive can take over a day to complete on a multi-terrabyte disk. It is therefore suggested to do it from another installation rather than the Arch installation media.

Generic methods

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

dm-crypt specific methods

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

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.

dm-crypt temporary container as pseudorandom number generator

First, create a temporary encrypted container on the partition, e.g. using default parameters

# cryptsetup open --type plain /dev/sdX container

Second, check it exists

# fdisk -l
Disk /dev/mapper/container: 1000 MB, 1000277504 bytes
Disk /dev/mapper/container doesn't contain a valid partition table

Finally, fill it with pseudorandom (encrypted data), a use of Dm-crypt/Drive preparation/dev/urandom is not required as the encryption cipher is used for randomness:

# dd if=/dev/zero of=/dev/mapper/container
dd: writing to ‘/dev/mapper/container’: No space left on device

Now the next step is #Partitioning.

Wipe free space with encrypted file after installation

The same effect can be achieved if a file is created on an 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/container
# rm /file/in/container

The above process has to be repeated for every partition blockdevice created.

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 re-installing on an already randomised drive, or de-commissioning one (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.