-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover.sh
executable file
·56 lines (42 loc) · 1.21 KB
/
recover.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env bash
set -xeuo pipefail
trap cleanup EXIT
pushd $(dirname $0)
SCRIPT_PATH=$(pwd)
popd
DISK=/dev/nvme0n1
LUKS_ROOT=luks-root
EFI_PART="$DISK"p1
EFI_MNT=/mnt/boot
FS_ROOT=fs-root
cleanup() {
umount "$EFI_MNT" || true
}
yubi_run() {
nix-shell https://codeload.github.com/sgillespie/nixos-yubikey-luks/tar.gz/master --run "$1"
}
luks() {
KEY_LENGTH=512
ITERATIONS=1000000
LUKS_PART="$DISK"p2
STORAGE=/crypt-storage/default
SLOT=2
SALT_LENGTH=16
CIPHER=capi:xts-aes-plain64
HASH=sha512
mkdir -p "$EFI_MNT"
mount "$EFI_PART" "$EFI_MNT"
salt=$(head -1 "$EFI_MNT$STORAGE")
umount "$EFI_MNT"
read -p "Disk passphrase: " -s k_user
challenge=$(yubi_run "echo -n $salt | openssl dgst -binary -sha512 | rbtohex")
response=$(yubi_run "ykchalresp -2 -x $challenge 2>/dev/null")
k_luks=$(yubi_run "echo -n \"$k_user\" | pbkdf2-sha512 $(($KEY_LENGTH / 8)) $ITERATIONS $response | rbtohex")
yubi_run "echo -n \"$k_luks\" | hextorb | cryptsetup luksOpen $LUKS_PART $LUKS_ROOT --key-file=-"
}
################
cleanup
# Mounts
luks
mount -t btrfs -o noatime,discard,ssd,autodefrag,compress=lzo,space_cache,subvol=subvolume-root "/dev/mapper/$LUKS_ROOT" /mnt
mount "$EFI_PART" "$EFI_MNT"