What is LUKS and how does it work?

When sensitive information is available on any computer storage, certain measures have to be taken to keep it away from the prying eyes. And instead of just putting it behind an average password, one can convert this data into unreadable text, so that only an authorized person could decipher it and read the original content. This method of protection is widely known as encryption. On Linux, it can be implemented through LUKS, the kernel’s standard solution serving to encrypt entire disks. Let’s look closer at this technology and its working principles.

Last update: June 02, 2023

Time to read: 5 min

What is LUKS?

LUKS (Linux Unified Key Setup) is by far the most common format for block device encryption used across various Linux-based systems. It is fully open-source, operates at the kernel level and employs its device mapper subsystem via the dm-crypt module. The first version of LUKS, which was later named LUKS1, was released in 2005. In 2018 its specification was enhanced with the support for some additional features, and this newer version is usually referred to as LUKS2.

By encoding the information stored on a desktop/laptop internal disk drive or any portable USB storage device, LUKS prevents it from being accessed unless the user is able to provide the correct authentication details. This is also a popular choice for protection of NAS units, especially ones produced by QNAP.

LUKS can be used to encrypt any partition, which may sit not only on a single drive, but also on a multi-drive RAID set or even an LVM volume. For this purpose, it offers various combinations of encryption algorithms (including AES, Serpent, Twofish), encryption modes (ECB; CBC, XTS), and hash functions (SHA-1, SHA-256, SHA-512, RIPEMD160, etc.). The standard setting is AES in the XTS mode of operation, with the key size of 512 bits. However, the developers of different Linux distributions and manufacturers of Linux-powered hardware may opt for their own encryption defaults.

All LUKS partitions share a similar layout. The most crucial information related to encryption, such as the utilized algorithm and the keys, resides in the LUKS header, a special area established at the beginning of the encrypted device. As an alternative, it is also possible to create a "detached" LUKS header, that will be stored in a completely different location, for example, on a removable USB flash drive. The header is an essential structural element of a LUKS partition. If it becomes corrupted, for example, due to improper re-partitioning, the data cannot be decrypted and gets irreversibly lost. That is why creating its backup copies is a sensible practice.

Next follow the so-called "key material" area and the ciphered user data itself. The primary key used for encryption (aka the Master Key) is randomly generated by the system. This key, in turn, gets encrypted with the help of the user’s key, or multiple keys – in case more valid users have access to the storage. Those encoded variants of the Master Key are stored in special key slots of the "key material" area. There may be up to eight slots, and each slot is unlocked by a key of a particular user. The user’s keys are themselves get secured by cryptographic hash algorithms (like PBKDF2), whereas the Master Key is separated into "anti-forensic" stripes.

The user’s key, which decrypts the Master Key and accordingly unlocks the storage, can be represented by:

  • a passphrase – a sequence of characters, maximum 512 in length, supplied by the user and requested during the system startup or access to an external device encrypted by LUKS;
  • a key file – a file whose content can be used as the passphrase to unlock a LUKS volume. When stored on a removable device, such as a USB stick, the medium then plays the role of a physical key, so that decryption should be possible as long it is attached to the computer. Such a file may comprise a simple passphrase, a block of random characters or even binary data, like an image, music or video.

How does it get activated on a device?

Since the functionality of LUKS is built directly into the Linux kernel, enabling it is fairly easy under any Linux distribution:

  • LUKS can be activated during the system re-/installation process by checking the corresponding encryption option. This method is optimal, as it implies the encryption of all partitions, including swap space, and thus allows achieving maximum protection. The /boot partition together with GRUB does not get encrypted though. It is placed onto a separate partition to provide the ability to boot the system after its installation.
  • Encryption can be performed on a partition basis with the help of cryptsetup, a command-line utility for setting up encrypted file systems using Device Mapper and the dm-crypt target.
  • On supported NAS appliances, a NAS volume can be encrypted upon creation, provided that the user selects the corresponding checkbox.

LUKS on QNAP NAS

The majority of Network Attached Storage products manufactured by QNAP offer the feature of full-disk encryption. An encrypted partition can be created on an individual drive, JBOD configuration, or RAID group based on the cryptsetup mechanism provided by LUKS and the 256-bit AES algorithm in the CBC mode. However, this implementation of LUKS has a notable difference from that of standard Linux.

If a user chooses to enable encryption, they are prompted to define a passphrase of 8–32 characters in length. This passphrase is meant for encryption of the LUKS Master Key and is first transformed by a QNAP utility called storage_util, which applies to it the MD5 hash function with the YCCaQNAP salt.

The resulting encryption key can be saved as a 256-byte file with the *.key extension right away during the procedure or afterward.

In order to unlock such a volume, the user has to input the original passphrase or upload the exported *.key file.

The described technology is supported for data recovery and access by the following software products: