Tag/tag.png

Duplicate Article
This article covers the same material as another article. More info...

Tag/tag.png

Needs Updating
This article needs updating to include the latest versions of Ubuntu. More info...

Tag/tag.png

Candidate for Deletion
This article may not be appropriate for this wiki, and may be deleted. More info...

Tag/tag.png

Unsupported Version
This article applies to an unsupported version of Ubuntu. More info...

Encrypted Swap and Home with LUKS on Ubuntu 6.06 and 5.10

(i) Please refer to EncryptedFilesystems for further documentation.

By Stefano Spinucci: virgo977virgo at <googlemail> dot com

Introduction

Notes

  • Newer Ubuntu versions can do hard disk encryption during installation time. You need to install from alternative install CD. Here are instructions for install time encryption.

  • In this tutorial we assume that:
    • Old (unencrypted) and the new (encrypted) swap is in the partition '/dev/hda2'
    • New home (encrypted) is in the partition '/dev/hda3'

Replace '/dev/hda2' with your real swap partition and '/dev/hda3' with an empty partition that will become your new encrypted home partition.

  • DM-Crypt works by transparently translating (in the kernel) between a physical on-disk partition (which is encrypted) and a logical partition which you can then mount and use as normal; then, for example, to operate on your home partition you must do so by using /dev/mapper/home instead of /dev/hda3.
  • If you are using Windows and you need to encrypt it also, it can be done with free open source tools in Linux friendly manner.

Warnings

Encrypting a partition is a destructive operation; then, your new home partition (/dev/hda3) must be empty, because all data on it will be erased.

Unencrypted data on the old home directory won’t be deleted and will be accessible, for example, with a live CD; then, you shouldn't put any sensitive data on home before encrypting.

Otherwise, if you have sensitive data to delete securely from the old unencrypted home, you should shred the old home directory.

If the partition containing the old home directory is formatted with a journaled file system (JFS, ReiserFS, XFS, Ext3, etc.), you must boot with a live CD and shred the entire partition containing the old home directory.

If the shredded partition is the partition containing the OS, reinstall ubuntu, and finally mount the previously created encrypted home.

References for secure deletion:

Strong Passwords

Remember that a chain is only as strong as its weakest link, and in the encryption chain the password is always the weakest link.

Then, choose a strong password, or your data won't be more secure than without encryption.

References for strong passwords:

Install cryptsetup

Enable 'community maintained' (universe) repository from the Synaptic package manager or modifying the file /etc/apt/sources.list (APT sources list).

Install cryptsetup:

$ sudo apt-get install cryptsetup

Encrypted Home

Unmount (if mounted) /dev/hda3

$ sudo umount /dev/hda3

Check the partition for errors (and wait several minutes...):

$ sudo /sbin/badblocks -c 10240 -s -w -t random -v /dev/hda3

Fill the disk with random data (and wait many more minutes...); /dev/urandom won't be as random as /dev/random, but it is the best practical solution available:

$ sudo dd if=/dev/urandom of=/dev/hda3

Create a LUKS partition:

$ sudo cryptsetup --verify-passphrase --hash=sha256 --cipher=aes-cbc-essiv:sha256 --key-size=256 luksFormat /dev/hda3

NOTE: If you get errors that the kernel may not use dm-crypt, try the command modprobe dm-crypt and retry to create the LUKS partition; if that helps, you may also want to add the module dm-crypt to the file /etc/modules.

Set up the device mapper:

$ sudo cryptsetup luksOpen /dev/hda3 home

Confirm it worked:

$ sudo cryptsetup status home
/dev/mapper/home is active:
  cipher:  aes-cbc-essiv:sha256
  keysize: 256 bits
  device:  /dev/.static/dev/hda3
  offset:  2056 sectors
  size:    20962706 sectors
  mode:    read/write

Create the filesystem (e.g. ext3):

$ sudo mke2fs -j -O dir_index,filetype,sparse_super /dev/mapper/home

Temporary mount, to copy data from old home:

$ sudo mount -t ext3 /dev/mapper/home /mnt

Copy data from old home:

$ sudo cp -axv /home/* /mnt/

Unmount the temporary mount:

$ sudo umount /mnt

Permanent Mounting

Ubuntu 6.06

Insert in /etc/fstab :

# <file system>    <mount point>   <type>   <options>   <dump>  <pass>
/dev/mapper/home   /home           ext3     defaults    1       2

After that, add an entry in /etc/crypttab:

# <target device>   <source device>   <key file>   <options>
home                /dev/hda3         none         luks

Reboot, and the encrypted home is done.

Ubuntu 5.10

Because 'crypttab' in Ubuntu 5.10 doesn't support LUKS encrypted partitions, automatic mounting of home with Ubuntu 5.10 is a bit more difficult.

Create a file named 'cryptinit' in /etc/init.d/ with the following content:

# if this script is executed when home is opened, tries to close it;
# otherwise, tries to open it, for three times, then continue without
# opening it
if [ -b /dev/mapper/home ]; then
    /sbin/cryptsetup luksClose home
else
    i=3
    while [ $i -gt 0 ]; do
        let "i -= 1"
        /sbin/cryptsetup luksOpen /dev/hda3 home && i=0
    done
fi

Make 'cryptinit' executable

$ sudo chmod 755 /etc/init.d/cryptinit

Then, create a symlink to 'cryptinit' in /etc/rcS.d

$ cd /etc/rcS.d
$ sudo ln -s ../init.d/cryptinit S28cryptinit

Insert in /etc/fstab :

# <file system>    <mount point>   <type>   <options>   <dump>   <pass>
/dev/mapper/home   /home           ext3     defaults    1        2

Reboot, and the encrypted home is done.

Notes

With the instructions above about encrypting home you can also encrypt generic data partitions (other than home), and you can permanently mount them in two ways.

The first technique is shown above for mounting home, and requests the password during the loading of the kernel.

The second technique we explain here asks you for the password right at the end of the booting process, at the gnome login:

  • Do not make any modifications to /etc/fstab or /etc/crypttab
  • Add the encrypted partition to /etc/pmount.allow (ie. /dev/hda3)

This will give you the convenience of entering the password at the end of the boot process rather than in the middle. however, a bug means that your encrypted partition will always be called 'usbdisk' whether it is a usbdisk or not.

Manual Mounting and Unmounting

If you have encrypted other partitions than home and you don't want to unlock those partitions on boot, then you need to manually mount and unmount them.

Mounting

Set up the device mapper:

$ cryptsetup luksOpen /dev/hda4 data

Mounting:

$ mount /dev/mapper/data /media/data

Unmounting

Umounting:

$ umount /media/data

Delete the device mapper:

$ cryptsetup luksClose data

Encrypted Swap

Before setting the encrypted swap, the file /etc/fstab should have a swap entry like this:

# <file system>   <mount point>   <type>   <options>   <dump>  <pass>
/dev/hda2         none            swap     sw          0       0

Now just replace in /etc/fstab /dev/hda2 with the new device name /dev/mapper/cswap:

# <file system>     <mount point>   <type>   <options>   <dump>  <pass>
/dev/mapper/cswap   none            swap     sw          0       0

After that, add an entry in /etc/crypttab:

# <target device>   <source device>   <key file>    <options>
cswap               /dev/hda2         /dev/random   swap

Reboot, and that's it! The encrypted swap device is done; confirm it worked:

$ cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/mapper/cswap                       partition       3148700 0       -1

$ sudo cryptsetup status cswap
/dev/mapper/cswap is active:
  cipher:  aes-cbc-plain
  keysize: 256 bits
  device:  /dev/.static/dev/hda2
  offset:  0 sectors
  size:    6297417 sectors
  mode:    read/write

Read the crypttab(5) manpage for more information.

Encrypting with Keyfiles

With LUKS you can encrypt/decrypt with keyfiles instead of passphrases.

You can add a keyfile with the command luksFormat or with the command luksAddKey.

For example, you can add with luksFormat a passphrase on slot 0 and with luksAddKey a keyfile on slot 1; then, you can open your encrypted device with the keyfile and, if you lose the keyfile, you can always use the passphrase.

For better security you can store your keyfiles on a USB stick, maybe encrypting the USB stick with a passphrase.

You can use every file you like as keyfile; for example, to generate a 2048bit random key:

$ dd if=/dev/random of=keyfile bs=1 count=256

Then, to add the generated keyfile to an existing encrypted partition:

The following command will require you to enter two times the passphrase
stored on slot 0...
$ sudo cryptsetup luksAddKey /dev/hda4 keyfile

Finally, to open the encrypted partition with the keyfile:

$ sudo cryptsetup luksOpen /dev/hda4 data --key-file keyfile

If you like to disable (delete) the keyfile on slot 1:

$ sudo cryptsetup luksDelKey /dev/hda4 1

Tools

References


CategorySecurity

EncryptedFilesystemHowto3 (last edited 2013-12-08 02:02:45 by cprofitt)