Today we’ll go over creating LUKS-encrypted partitions with cryptsetup. LUKS (Linux Unified Key Setup) is a block device encryption format that is the standard on Linux systems. Also, because it stores all the necessary data in the partition header, it’s easy to migrate partitions.
To get an overview of all the cryptographic ciphers that the system can use, look in /proc/crypto:
cat /proc/crypto | grep name
name : crc32
name : __ghash
name : ghash
name : __ghash
name : xts(aes)
name : lrw(aes)
name : __xts-aes-aesni
name : __lrw-aes-aesni
name : pcbc(aes)
name : rfc4106(gcm(aes))
name : __gcm-aes-aesni
name : ctr(aes)
name : __ctr-aes-aesni
name : cbc(aes)
name : __ecb-aes-aesni
name : ecb(aes)
name : __cbc-aes-aesni
name : __ecb-aes-aesni
name : __aes-aesni
name : aes
name : crct10dif
name : crct10dif
name : crc32c
name : hmac(sha256)
name : hmac(sha1)
name : lzo
name : crc32c
name : aes
name : sha224
name : sha256
name : sha1
name : md5
name : sha224
name : sha256
name : sha1
name : aes
For this demo, I will be using a 200MB partition called /dev/sdb1. The below command initializes the partition as a LUKS device and you have to configure a passphrase at this step.
123456789
cryptsetup luksFormat /dev/sdb1
WARNING!
========
This will overwrite data on /dev/sdb1 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
Format the partition with the filesystem of your choice:
123456789101112131415161718192021
mkfs -t ext4 /dev/mapper/hidden
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
50800 inodes, 202752 blocks
10137 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
I made a folder for mounting the new encrypted partition and mounted it:
1234567
mount -v /dev/mapper/hidden /mnt/hidden
mount: /mnt/hidden does not contain SELinux labels.
You just mounted an file system that supports labels which does not
contain labels, onto an SELinux box. It is likely that confined
applications will generate AVC messages and not be allowed access to
this file system. For more details see restorecon(8) and mount(8).
mount: /dev/mapper/hidden mounted on /mnt/hidden.
Device refers to the block device or its UUID. The entries between tags are optional. In my case, the entry would look like this (the UUID is of /dev/sdb1)
Now you will be prompted at boot for the password. If you want to use a key for automatic unlocking, create a key file (here a random key of 4096 bytes length):
1234
dd if=/dev/urandom of=/root/key bs=1024 count=4
4+0 records in
4+0 records out
4096 bytes (4.1 kB) copied, 0.00100253 s, 4.1 MB/s
Make it only readable by root with chmod 400 /root/key
Add the key for the encrypted volume:
12345
cryptsetup -v luksAddKey /dev/sdb1 /root/key
Enter any existing passphrase:
Key slot 0 unlocked.
Key slot 0 unlocked.
Command successful.