Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| doc:howto:disk.encryption [2016/06/04 08:40] – [Disk Encryption] sshambar | docs:guide-user:storage:disk.encryption [2023/07/24 23:25] (current) – [Disk Encryption] crass | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Disk Encryption ====== | ||
| + | You may want to encrypt your external disk to improve privacy (in case other people have physical access to your router) or so that you can securely reuse the disk later for another purpose if it's flash (see [[http:// | ||
| + | |||
| + | Install encryption packages: | ||
| + | |||
| + | <code bash> | ||
| + | opkg install kmod-crypto-ecb kmod-crypto-xts kmod-crypto-seqiv kmod-crypto-misc kmod-crypto-user cryptsetup | ||
| + | </ | ||
| + | |||
| + | Install ext4 packages: | ||
| + | |||
| + | <code bash> | ||
| + | opkg install kmod-fs-ext4 e2fsprogs | ||
| + | </ | ||
| + | |||
| + | There are different ways of handling the encryption key. | ||
| + | In this example we generate a new random key on every mount. | ||
| + | |||
| + | | {{: | ||
| + | |||
| + | The following command will create a standard encrypted container on the device or partition '' | ||
| + | WARNING: This will destroy anything on '' | ||
| + | |||
| + | <code bash> | ||
| + | cryptsetup luksFormat [encrypted-device] | ||
| + | </ | ||
| + | |||
| + | This step may take a long time while ''/ | ||
| + | The security of the passphrase is based on it's strength and the number of iterations it is hashed... as the CPU on embedded systems is usually slow, it's advisable to force the use of a higher iteration count for simple passphrases: | ||
| + | Note: higher values will increase the time it takes to map the device, not access it once it's mounted). | ||
| + | |||
| + | To use the encrypted container, you must map a decrypted device... this must be done before the device can be formatted or mounted (eg. after each reboot). | ||
| + | The following command creates a mapping called '' | ||
| + | |||
| + | <code bash> | ||
| + | cryptsetup open [encrypted-device] [map-name] | ||
| + | </ | ||
| + | |||
| + | Format and mount the (now available) decrypted device. | ||
| + | '' | ||
| + | |||
| + | <code bash> | ||
| + | mkfs.ext4 / | ||
| + | mount / | ||
| + | </ | ||
| + | |||
| + | On a fresh reboot, you just need for perform the mapping and mount (Note: the mapping will require a passphrase) | ||
| + | |||
| + | <code bash> | ||
| + | cryptsetup open [encrypted-device] [map-name] | ||
| + | mount / | ||
| + | </ | ||
| + | |||
| + | Unmount: | ||
| + | |||
| + | <code bash> | ||
| + | umount [mount-point] | ||
| + | cryptsetup close [map-name] | ||
| + | </ | ||
| + | |||
| + | Automated: | ||
| + | |||
| + | The following script can be used to automate decrypting and mounting removable storage that is encrypted by using entries in ''/ | ||
| + | <code bash> | ||
| + | cryptsetup luksDump [encrypted-device] | ||
| + | </ | ||
| + | |||
| + | '' | ||
| + | <code bash> | ||
| + | dd if=/ | ||
| + | </ | ||
| + | |||
| + | This will create a key-file that is filled with 4096 bytes of random data. Add this key-file to the LUKS device: | ||
| + | <code bash> | ||
| + | cryptsetup luksAddKey [encrypted-device] [path/ | ||
| + | </ | ||
| + | |||
| + | You will be prompted for the passphrase from above. | ||
| + | |||
| + | <code bash> | ||
| + | cryptsetup luksDump [encrypted-device] | ||
| + | </ | ||
| + | '' | ||
| + | '' | ||
| + | |||
| + | '' | ||
| + | <code bash> | ||
| + | block info | ||
| + | </ | ||
| + | |||
| + | '' | ||
| + | |||
| + | <code bash install-decrypt.sh> | ||
| + | cat << " | ||
| + | # note: this needs ash installed | ||
| + | ash / | ||
| + | EOF | ||
| + | |||
| + | cat << " | ||
| + | #!/bin/sh | ||
| + | # Perform tasks when called by BLOCK hotplug (/ | ||
| + | # CC0: 21JUL18 by WaLLy3K, updated 09AUG18 | ||
| + | # Further adapted for OpenWRT 18.06 by jmm on 2018-09-04 | ||
| + | # Further adapted for OpenWRT 21.02.2 by mdpc on 2022-12-30 | ||
| + | # Further adapted for OpenWRT 22.03 by crass on 2023-07-24 | ||
| + | # * remove dependency on awk | ||
| + | # * allow specifying alternate path to crypttab and alternate root for keyfile | ||
| + | # https:// | ||
| + | |||
| + | # Hotplug Vars: $ACTION (add/ | ||
| + | |||
| + | # logger -s "start decrypt luks" $DEVNAME $ACTION | ||
| + | |||
| + | if [ -z " | ||
| + | DEVNAME=" | ||
| + | fi | ||
| + | |||
| + | msg() { | ||
| + | echo " | ||
| + | } | ||
| + | |||
| + | if [ " | ||
| + | #only do something if a device is being added | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | if [[ " | ||
| + | #/ | ||
| + | # in / | ||
| + | block mount | ||
| + | exit 0 | ||
| + | fi | ||
| + | |||
| + | # Determine whether drive needs to be decrypted | ||
| + | CRYPTTAB=${CRYPTTAB: | ||
| + | if [[ ! -r " | ||
| + | msg " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | [ -e /dev/fd ] || ln -s / | ||
| + | |||
| + | #IFS=: read BID_DEVNAME BID_RAW < <(block info "/ | ||
| + | BID_DEVNAME=$(block info "/ | ||
| + | BID_RAW=$(block info "/ | ||
| + | if [[ -n " | ||
| + | eval " | ||
| + | fi | ||
| + | |||
| + | if [[ -n " | ||
| + | CT_RAW=" | ||
| + | fi | ||
| + | |||
| + | if [[ -z " | ||
| + | CT_RAW=" | ||
| + | fi | ||
| + | |||
| + | if [[ -z " | ||
| + | CT_RAW=" | ||
| + | fi | ||
| + | |||
| + | if [[ -z " | ||
| + | # No crypttab entry found for this device | ||
| + | exit 1 | ||
| + | fi | ||
| + | #read CT_LABEL _ CT_KEYFILE CT_TYPE < <(echo $CT_RAW) | ||
| + | CT_LABEL=$(echo $CT_RAW | (read V _; echo $V)) | ||
| + | CT_KEYFILE=$(echo $CT_RAW | (read _ _ V _; echo $V)) | ||
| + | CT_TYPE=$(echo $CT_RAW | (read _ _ _ V; echo $V)) | ||
| + | |||
| + | if [[ -e "/ | ||
| + | msg "Drive already decrypted: $CT_LABEL" | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | # Error Handling | ||
| + | if [[ ! -e " | ||
| + | msg " | ||
| + | exit 1 | ||
| + | fi | ||
| + | if [[ ! " | ||
| + | msg " | ||
| + | exit 1 | ||
| + | fi | ||
| + | |||
| + | msg " | ||
| + | cryptsetup open "/ | ||
| + | CS_EXIT=" | ||
| + | case " | ||
| + | 0) if [ -e "/ | ||
| + | msg "Drive decrypted: $CT_LABEL" | ||
| + | else | ||
| + | msg "Drive not found after decrypting: $CT_LABEL" | ||
| + | exit 1 | ||
| + | fi;; | ||
| + | 5) msg " | ||
| + | *) msg " | ||
| + | esac | ||
| + | |||
| + | exit 0 | ||
| + | EOF | ||
| + | |||
| + | chmod +x / | ||
| + | </ | ||
| + | |||
| + | The above script does not unmount or remove ''/ | ||
| + | |||
| + | ===== Example ===== | ||
| + | A video demonstration on OpenWrt 14.07 Barrier Breaker using LUKS: [[https:// | ||