Encrypt your Linux setup
Nate Drake explores how to use the tools already built into your Linux installation to encrypt your files and shield your devices.
Any grizzled yet privacyconscious Ubuntu users may remember that, back in the day, keeping your data safe was far from easy. The steps involved either supreme mastery of the command line or using an alternate installation CD to encrypt your Ubuntu installation.
Things have changed. When you insert a DVD of Ubuntu and its variants, the graphical installer now offers you the option to encrypt your system using LVM (Logical Volume Manager), which manages disk drives on LUKS (Linux Unified Key Setup), the standard for Linux Hard Drive encryption.
For most users, this is a great way to save time, protect your system and avoid having to learn various confusing acronyms. Once the system installation is complete, your drive with the exception of the / boot partition is encrypted and can only be unlocked with a passphrase. Once you log in, most of the built-in disk managers even enable you to encrypt external drive using LUKS, so that a password is required each time you insert it. The same programs will often offer you the option to change the passwords for drives that are already encrypted.
On the face of it, it would seem that LUKS is purring away merrily under your computer’s proverbial hood and there’s nothing further to be done. This guide is designed for more curious readers. We’ll explore the built-in encryption programs in Linux and you can modify them to suit your requirements, such as choosing your own encryption cipher or adding multiple passwords to a volume.
This tutorial focuses on Cryptsetup, a command line application which comes pre-installed in most Linux distributions. It functions as an interface for the ‘dm-crypt’ module, to enable you to quickly and easily encrypt drives. At its simplest level, you can use Cryptsetup to create plain volumes that are protected by a password only (see boxout, opposite) but you need to specify all the encryption options each time you access the device and, annoyingly, there’s no easy way to change the password.
Luckily, Cryptsetup contains useful tools for creating and modifying LUKS encrypted volumes. Aside from defining a universal standard for hard drive encryption, LUKS volumes contain headers that detail the cipher and hash used, as well as a unique 256-bit salt, which is added to your password for further security. The header also contains an encrypted master key that’s used to unlock the container. This means you can change the password used to access the master key or add further passwords.
To get started with your voyage of discovery with LUKS, we strongly recommend you set up a virtual machine, or at the very least back up your data before proceeding. Once this is done, open your disk management program to view any LUKS-encrypted device, such as the system hard drive. You’ll see the encrypted LUKS volume appears as a separate device, for example /dev/sda5. You can delve further into this using the ‘LuksDump’ command in Terminal: sudo cryptsetup luksDump /dev/sda5
KEEPING YOUR HEADERS
After running the ‘LuksDump’ command to bare open your LUKS volume header, the first thing you can see are the default encryption options used when you instruct Ubuntu to encrypt a drive for you using the installer or the disk utility. In the case of system encryption, Ubuntu uses 256-bit AES with the XTS Block Cipher mode.
The MK (Master Key) salt is also listed, which hugely increases the security of your LUKS volume. Without it, there’s no way to retrieve your data, even with the correct password. For this reason, Cryptsetup enables you to make backups of your LUKS volume headers and restore them, because if the header is damaged, there’s no way to retrieve your encrypted data.
To make a backup of your LUKS header, open Terminal and run the command: cryptsetup luksHeaderBackup <device> --header-backup-file <file>
sudo cryptsetup luksHeaderBackup /dev/sdb1 --header-backup-file /home/ nate/Desktop/backup1
To restore the header, simply run the same command but substitute ‘luksHeaderBackup’ with luksHeaderRestore . Once you’ve safely backed up your header, run the ‘LuksDump’ command above again and scroll down to the Key Slots section. By default, there are eight key slots, numbered 0 to 7. The first keyslot (0) is enabled with the password you chose when you originally encrypted the volume. If you want to give someone else access to your LUKS volume, you can add
another password or key file using Crypsetup’s ‘luksAddKey’ command, for example: sudo cryptsetup luksAddKey /dev/sda5
You’ll need to enter an existing password. Cryptsetup will then ask you to enter the new password twice.
To remove a password from a LUKS volume, use the ‘luksRemoveKey’ option, sudo cryptsetup luksRemoveKey /dev/sda5 . Cryptsetup will ask you to enter the password you wish to remove. Be careful with this feature because, if you delete all the key slots for a LUKS container, you won’t be able to access it again.
Kali Linux actually puts this feature to very good use through a nuke patch for Cryptsetup, whereby if a certain password is entered, all the key slots in the system’s LUKS header are erased (More on this at www.kali.org/tutorials/ nuke-kali-linux-luks). This is done to protect your device’s data from theft or seizure but remember to back your volume header if you make use of it, or else you won’t be able to access your machine either!
PUSHING YOUR LUKS
While Cryptsetup has some excellent default options to get started, you may prefer to have more control over your data. In the first instance, if you don’t wish to encrypt an entire volume or drive you can create a LUKS file container, which can only be unlocked with a password. See the walkthrough over the page for a step-by-step guide on how to do this.
If you use the luksFormat option on its own when creating your container, then Cryptsetup will use the default encryption options. However, if you prefer you can specify these yourself.
sudo cryptsetup -y --cipher serpent-cbc-plain --key-size 256 --hash sha512 luksFormat test1
In the above example, the master key for container test1 will be encrypted with the Serpent cipher algorithm, which was a finalist in the AES (Advanced Encryption Standard) contest. The SHA512 hash is used in place of the default SHA256, too.
Cryptsetup also supports using key files instead of passwords. These are particularly helpful when using plain mode, because files will most probably have a higher degree of entropy than passwords. For LUKS containers, you can add a key file by simply specifying the file location after
luksAddKey , for example:
sudo cryptsetup luksAddKey / dev/sdb1 /home/nate/Pictures/ kitten.jpg
Enter any existing password to add the file to the key slot. To open a LUKS container using a key file simply specify the path using the --keyfile option, for instance:
sudo cryptsetup luksOpen / dev/sdb1 vol1 --key-file / home/nate/Pictures/kitten.jpg
FRESH OUT OF LUKS
In the above example, an existing binary file was used of a kitten taken from Wikimedia. In reality, this would offer very poor security because anyone with a copy of the file could open your LUKS Container. Either create an arbitrary keyfile from random data using or use a file to which only you have access, such as a photo you took yourself.
If you used sudo to create a mount point or format drives, you may find you cannot modify files in your encrypted containers at first as they are assigned to the root user. Run sudo chmod -R 777 / mnt/<your mount point> in Terminal to fix this.
Unlike third-party programs such as Veracrypt, Crypsetup can’t encrypt volumes using cipher cascades like AES-Twofish-Serpent. Using any one of these algorithms on their own will offer adequate security, but for the ultra-paranoid, you can create LUKS containers inside one another if you wish. Simply repeat the steps in the tutorial using different encryption options for each container.
Remember that the strength of your container will rest largely on the amount of entropy your password has. If you have a good memory, try using DiceWare to generate a passphrase ( world.std.com/~reinhold/diceware.html). We recommend the alternative Beale word list because it uses UK spelling and avoids Americanisms.
If you like the convenience of using LUKS for changing passwords but still aren’t happy with everyone knowing you’re encrypting data, you can create a LUKS container with a detached header. The advantage of doing it this way is that, by itself, the container just looks like random data, giving you some degree of plausible deniability. To do this, use the --header option when creating a LUKS volume to specify where to save the header. For example:
sudo cryptsetup luksFormat / dev/sda --header /dev/sdb --align-payload= 0
When mounting LUKS volumes, specify the header location in the same way, for example:
sudo cryptsetup luksOpen / dev/sda --header /dev/sdb Luks
When doing this make sure you encrypt an entire drive or partition, because there’s no plausible reason for you to have large files of seemingly random data on your system!
Ubuntu’s Disk Utility can format drive as LUKS volumes: just type your passphrase and click Format.
Use the luksDump for header information. This uses 256-bit AES encryption that unlocks with a single password.
Enter any valid password to use a keyfile to open your LUKS volumes. Make sure that the file is one you’ve created yourself.