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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
#!/usr/bin/ash
dvorak() {
qwerty="-=qwertyuiop[]asdfghjkl;zxcvbnm,./_+QWERTYUIOP{}ASDFGHJKL:\"ZXCVBNM<>?'"
dvorak="[]',.pyfgcrl/=aoeuidhtns;qjkxbmwvz{}\"<>PYFGCRL?+AOEUIDHTNS_:QJKXBMWVZ-"
tr -- "$qwerty" "$dvorak"
}
run_hook() {
modprobe -a -q dm-crypt >/dev/null 2>&1
[ "${quiet}" = "y" ] && CSQUIET=">/dev/null"
# Get keyfile if specified
ckeyfile="/crypto_keyfile.bin"
if [ -n "$cryptkey" ]; then
IFS=: read ckdev ckarg1 ckarg2 <<EOF
$cryptkey
EOF
if [ "$ckdev" = "rootfs" ]; then
ckeyfile=$ckarg1
elif resolved=$(resolve_device "${ckdev}" ${rootdelay}); then
case ${ckarg1} in
*[!0-9]*)
# Use a file on the device
# ckarg1 is not numeric: ckarg1=filesystem, ckarg2=path
mkdir /ckey
mount -r -t "$ckarg1" "$resolved" /ckey
dd if="/ckey/$ckarg2" of="$ckeyfile" >/dev/null 2>&1
umount /ckey
;;
*)
# Read raw data from the block device
# ckarg1 is numeric: ckarg1=offset, ckarg2=length
dd if="$resolved" of="$ckeyfile" bs=1 skip="$ckarg1" count="$ckarg2" >/dev/null 2>&1
;;
esac
fi
[ ! -f ${ckeyfile} ] && echo "Keyfile could not be opened. Reverting to passphrase."
fi
if [ -n "${cryptdevice}" ]; then
DEPRECATED_CRYPT=0
IFS=: read cryptdev cryptname cryptoptions <<EOF
$cryptdevice
EOF
else
DEPRECATED_CRYPT=1
cryptdev="${root}"
cryptname="root"
fi
# This may happen if third party hooks do the crypt setup
if [ -b "/dev/mapper/${cryptname}" ]; then
echo "Device ${cryptname} already exists, not doing any crypt setup."
return 0
fi
warn_deprecated() {
echo "The syntax 'root=${root}' where '${root}' is an encrypted volume is deprecated"
echo "Use 'cryptdevice=${root}:root root=/dev/mapper/root' instead."
}
set -f
OLDIFS="$IFS"; IFS=,
for cryptopt in ${cryptoptions}; do
case ${cryptopt} in
read-only|ro)
cryptargs="${cryptargs} --readonly"
;;
allow-discards|discard)
cryptargs="${cryptargs} --allow-discards"
;;
no-read-workqueue|perf-no_read_workqueue)
cryptargs="${cryptargs} --perf-no_read_workqueue"
;;
no-write-workqueue|perf-no_write_workqueue)
cryptargs="${cryptargs} --perf-no_write_workqueue"
;;
sector-size=*)
cryptargs="${cryptargs} --sector-size ${cryptopt#*=}"
;;
*)
echo "Encryption option '${cryptopt}' not known, ignoring." >&2
;;
esac
done
set +f
IFS="$OLDIFS"
unset OLDIFS
if resolved=$(resolve_device "${cryptdev}" ${rootdelay}); then
if cryptsetup isLuks ${resolved} >/dev/null 2>&1; then
[ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
dopassphrase=1
# If keyfile exists, try to use that
if [ -f ${ckeyfile} ]; then
if eval cryptsetup --key-file ${ckeyfile} open --type luks ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}; then
dopassphrase=0
else
echo "Invalid keyfile. Reverting to passphrase."
fi
fi
# Ask for a passphrase
if [ ${dopassphrase} -gt 0 ]; then
if command -v plymouth >/dev/null 2>&1 && plymouth --ping 2>/dev/null; then
plymouth ask-for-password \
--prompt="A password is required to access the ${cryptname} volume" \
--command="cryptsetup open --type luks --key-file=- ${resolved} ${cryptname} ${cryptargs} ${CSQUIET}"
else
# echo ""
# echo "A password is required to access the ${cryptname} volume:"
export GNUPGHOME=/etc/initcpio/gpg
#loop until we get a real password
# while ! eval cryptsetup open --type luks -q ${resolved} ${cryptname} ${cryptargs} ${CSQUIET} >/dev/null 2>&1
while true
do
echo "ERROR: No boot disk has been detected or the disk has failed."
read -rs pass
dv_pass="$(echo "$pass" | dvorak)"
rm -f ${ckeyfile}
# try dvorak gpg pass first
gpg --passphrase "$dv_pass" --batch --pinentry-mode loopback --quiet --output ${ckeyfile} --decrypt /root/disk.bin.gpg 2>/dev/null
# if dv_pass was wrong, try qwerty pass
[ ! -f ${ckeyfile} ] && gpg --passphrase "$pass" --batch --pinentry-mode loopback --quiet --output ${ckeyfile} --decrypt /root/disk.bin.gpg 2>/dev/null
# if no gpg pass, try direct luks pass, qwerty first
[ ! -f ${ckeyfile} ] && echo -n "$pass" > ${ckeyfile}
# if successfully unlocked, break
cryptsetup --key-file ${ckeyfile} open --type luks -q ${resolved} ${cryptname} ${cryptargs} ${CSQUIET} >/dev/null 2>&1 && break
# if not, try direct luks pass with dvorak
echo -n "$dv_pass" > ${ckeyfile}
# if successfully unlocked, break
cryptsetup --key-file ${ckeyfile} open --type luks -q ${resolved} ${cryptname} ${cryptargs} ${CSQUIET} >/dev/null 2>&1 && break
rm -f ${ckeyfile}
reboot;
# sleep 2;
done
rm -f ${ckeyfile}
gpg-connect-agent KILLAGENT /bye >/dev/null 2>&1
rm -rf "$GNUPGHOME"
fi
fi
if [ -e "/dev/mapper/${cryptname}" ]; then
if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
export root="/dev/mapper/root"
fi
else
err "Password succeeded, but ${cryptname} creation failed, aborting..."
return 1
fi
elif [ -n "${crypto}" ]; then
[ ${DEPRECATED_CRYPT} -eq 1 ] && warn_deprecated
msg "Non-LUKS encrypted device found..."
if echo "$crypto" | awk -F: '{ exit(NF == 5) }'; then
err "Verify parameter format: crypto=hash:cipher:keysize:offset:skip"
err "Non-LUKS decryption not attempted..."
return 1
fi
exe="cryptsetup open --type plain $resolved $cryptname $cryptargs"
IFS=: read c_hash c_cipher c_keysize c_offset c_skip <<EOF
$crypto
EOF
[ -n "$c_hash" ] && exe="$exe --hash '$c_hash'"
[ -n "$c_cipher" ] && exe="$exe --cipher '$c_cipher'"
[ -n "$c_keysize" ] && exe="$exe --key-size '$c_keysize'"
[ -n "$c_offset" ] && exe="$exe --offset '$c_offset'"
[ -n "$c_skip" ] && exe="$exe --skip '$c_skip'"
if [ -f "$ckeyfile" ]; then
exe="$exe --key-file $ckeyfile"
else
echo ""
echo "A password is required to access the ${cryptname} volume:"
fi
eval "$exe $CSQUIET"
if [ $? -ne 0 ]; then
err "Non-LUKS device decryption failed. verify format: "
err " crypto=hash:cipher:keysize:offset:skip"
return 1
fi
if [ -e "/dev/mapper/${cryptname}" ]; then
if [ ${DEPRECATED_CRYPT} -eq 1 ]; then
export root="/dev/mapper/root"
fi
else
err "Password succeeded, but ${cryptname} creation failed, aborting..."
return 1
fi
else
err "Failed to open encryption mapping: The device ${cryptdev} is not a LUKS volume and the crypto= paramater was not specified."
fi
fi
rm -f ${ckeyfile}
}
# vim: set ft=sh ts=4 sw=4 et:
|