For this HOWTO you will need the encfs package installed (Debian and Ubuntu).
Create an empty partition on the disk
We still want to be able to use the key to store some documents, and that way it will just look like an ordinary USB key. So we have to create an empty filesystem on the removable USB key:
# USB_KEY=/dev/sdX
# fdisk $USB_KEY
Command (m for help): o
Building a new DOS disklabel with disk identifier 0xXXXXXX.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-1974271, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1974271, default 1974271): 1974270
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (W95 FAT32)
Make sure that the last sector of the partition is minus one the last one of the disk. Here the last sector of the disk is 1974271, so (1974271-1) = 1974270.
Now write the partition table:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
Now format the new partition:
# mkfs.vfat -F 32 -n Documents ${USB_KEY}1
Random "hidden" passphrase
Now comes the part of the crypto stuff. Create a random passphrase:
# dd if=/dev/urandom bs=1 count=256 > passphrase.bin
Write it to the end of the USB key:
# USB_SEEK=$(($(blockdev --getsize64 $USB_KEY)-256))
# dd if=passphrase.bin of=$USB_KEY bs=1 seek=$USB_SEEK
This way the passphrase won't be stored in a filesystem but directly on the disk, where overwriting it by mistake will be a bit more difficult. It's also a bit harder for an attacker to know that the disk actually contains an encryption passphrase (but just a bit).
You can check that the passphrase was correctly written to the disk with:
# sha1sum passphrase.bin; dd if=/dev/sdg bs=1 skip=$USB_SEEK count=256 2> /dev/null | sha1sum
The two SHA1 sums should match.
At this step I advise you to backup your passphrase, maybe by encoding it using Bubble Babble, printing it and hiding it somewhere. Or just put it in a wallet manager like KeePassX. As you wish.
Encrypted directory creation
If you already have an encrypted EncFS directory, you can skip to the next step. If you don't have an encrypted EncFS directory already here is how you can create one:
$ encfs ~/encrypted ~/decrypted
Creating new encrypted volume.
Please choose from one of the following options:
enter "x" for expert configuration mode,
enter "p" for pre-configured paranoia mode,
anything else, or an empty line will select standard mode.
?>
Press p and Enter.
Paranoia configuration selected.
Configuration finished. The filesystem to be created has
the following properties:
Filesystem cipher: "ssl/aes", version 3:0:2
Filename encoding: "nameio/block", version 3:0:1
Key Size: 256 bits
Block Size: 1024 bytes, including 8 byte MAC header
Each file contains 8 byte header with unique IV data.
Filenames encoded using IV chaining mode.
File data IV is chained to filename IV.
File holes passed through to ciphertext.
-------------------------- WARNING --------------------------
The external initialization-vector chaining option has been
enabled. This option disables the use of hard links on the
filesystem. Without hard links, some programs may not work.
The programs 'mutt' and 'procmail' are known to fail. For
more information, please see the encfs mailing list.
If you would like to choose another configuration setting,
please press CTRL-C now to abort and start over.
Now you will need to enter a password for your filesystem.
You will need to remember this password, as there is absolutely
no recovery mechanism. However, the password can be changed
later using encfsctl.
New Encfs Password:
Enter (twice) a temporary password as we are about to change it. So enter something like abcd or stuff.
Changing the EncFS password
Now we will to change the current password of the encrypted directory to the passphrase we generated and wrote to the USB key before.
$ echo -n "Enter current password: " && read -s PASSWORD && (echo $PASSWORD && base64 -w 0 passphrase.bin) | encfsctl autopasswd ~/decrypted
Type in your current password (the temporary one if you just created the encrypted directory at the previous step), hit Enter, and then EncFS will change the password to the one contained in the file passphrase.bin, encoded with Base64. This encoding is useful as EncFS expects a one-line password and
Now unmount the encrypted directory so that we can try if mounting it with our key works:
$ fusermount -u ~/decrypted
Let's try if we can mount our encrypted directory with our key:
$ base64 -w 0 passphrase.bin | encfs -S ~/encrypted ~/decrypted
And it should work.
Real-word test: mount script
And finally we are ready to mount our encrypted directory using our USB key and this simple script:
#!/bin/sh
if [ "$1" = "" -o "$2" = "" ]
then
echo "Usage: $0 rootDir mountPoint"
exit 0
fi
mountpoint -q "$2" && echo "$1 is already mounted" && exit 1
echo "Trying to get the key from USB keychain ..." >&2
for SFS in /sys/block/sd*; do
DEV=`basename $SFS`
if [ 0`cat $SFS/removable` -eq 1 -a 0`cat $SFS/size` -gt 1 ]
then
SKIP=$((512*$(cat /sys/block/${DEV}/size)-256))
KEY=`dd if=/dev/${DEV} bs=1 skip=$SKIP 2> /dev/null`
echo "> Trying device: $DEV ..." >&2
echo -n "${KEY}" | base64 -w 0 | encfs -S "$1" "$2" \
&& echo "Encrypted directory mounted from keyfile" && exit 0
fi
done
echo "FAILED to find suitable USB keychain ..."
exit 1
The script is simply trying to use all the removable devices as decryption key for encfs. To use it you just have to run:
$ mount_encfs_usb.sh ~/encrypted ~/decrypted
Trying to get the key from USB keychain ...
> Trying device: sdg ...
Encrypted directory mounted from keyfile
And that's the end of this HOWTO :)
This was inspired by this StackOverflow thread and How to setup passwordless disk encryption.