Use the tpm2 chip to decrypt your disk at startup without entering your passphrase.
When using full disk encryption, it’s always frustrating to have to type your password when you boot your computer. Especially when on Windows with bitlocker enabled, you can decrypt using the tpm2 chip.
Credits goes to https://run.tournament.org.il/ubuntu-20-04-and-tpm2-encrypted-system-disk/
Here you will find the procedure that worked at least for me.
Install the required tools
1linux# apt install tpm2-tools
2Lecture des listes de paquets... Fait
3Construction de l'arbre des dépendances... Fait
4Lecture des informations d'état... Fait
5Les paquets supplémentaires suivants seront installés :
6 libtss2-fapi1
7Les NOUVEAUX paquets suivants seront installés :
8 libtss2-fapi1 tpm2-tools
90 mis à jour, 2 nouvellement installés, 0 à enlever et 0 non mis à jour.
10Il est nécessaire de prendre 1 051 ko dans les archives.
11Après cette opération, 2 794 ko d'espace disque supplémentaires seront utilisés.
12Souhaitez-vous continuer ? [O/n] o
13Réception de :1 http://ftp.fr.debian.org/debian bookworm/main amd64 libtss2-fapi1 amd64 3.2.1-3 [311 kB]
14Réception de :2 http://ftp.fr.debian.org/debian bookworm/main amd64 tpm2-tools amd64 5.4-1 [740 kB]
151 051 ko réceptionnés en 0s (7 536 ko/s)
16Sélection du paquet libtss2-fapi1:amd64 précédemment désélectionné.
17(Lecture de la base de données... 264954 fichiers et répertoires déjà installés.)
18Préparation du dépaquetage de .../libtss2-fapi1_3.2.1-3_amd64.deb ...
19Dépaquetage de libtss2-fapi1:amd64 (3.2.1-3) ...
20Sélection du paquet tpm2-tools précédemment désélectionné.
21Préparation du dépaquetage de .../tpm2-tools_5.4-1_amd64.deb ...
22Dépaquetage de tpm2-tools (5.4-1) ...
23Paramétrage de libtss2-fapi1:amd64 (3.2.1-3) ...
24Paramétrage de tpm2-tools (5.4-1) ...
25Traitement des actions différées (« triggers ») pour man-db (2.11.2-2) ...
26Traitement des actions différées (« triggers ») pour libc-bin (2.36-9) ...
Define TPM2 memory space to hold the key
1tpm2_nvdefine -s 64 0x1500016
Generate a random 64 character key and write it on the tpm2 chip
1cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 64 > root.key
2tpm2_nvwrite -i root.key 0x1500016
You can verify that the key in the tpm chip match your root.key
1echo root.key file contents: `cat root.key`
2echo The value stored in TPM: `tpm2_nvread 0x1500016`
3tpm2_nvread 0x1500016 2> /dev/null | diff root.key - && echo The root.key file matches what is stored in the TPM. It is ok to proceed! || echo The root.key file does not match what is stored in the TPM. Do NOT proceed until the two match!
Add the root.key as a valid decrypt key for your luks volume
1cryptsetup luksAddKey /dev/sda3 root.key
You can identify your encrypted partition using lsblk
Update initramfs in order to be kernel update resilient
- Create /usr/local/sbin/tpm2-getkey
1cat << EOF > /usr/local/sbin/tpm2-getkey
2#!/bin/sh
3if [ -f ".tpm2-getkey.tmp" ]; then
4# tmp file exists, meaning we tried the TPM this boot, but it didn’t work for the drive and this must be the second
5# or later pass for the drive. Either the TPM is failed/missing, or has the wrong key stored in it.
6/lib/cryptsetup/askpass "Automatic disk unlock via TPM failed for () Enter passphrase: "
7exit
8fi
9# No tmp, so it is the first time trying the script. Create a tmp file and try the TPM
10touch .tpm2-getkey.tmp
11
12tpm2_nvread 0x1500016
13EOF
14
15chown root: /usr/local/sbin/tpm2-getkey
16chmod 750 /usr/local/sbin/tpm2-getkey
- Create /etc/initramfs-tools/hooks/tpm2-decryptkey
1cat << EOF > /etc/initramfs-tools/hooks/tpm2-decryptkey
2#!/bin/sh
3PREREQ=""
4prereqs()
5{
6echo ""
7}
8case \$1 in
9prereqs)
10prereqs
11exit 0
12;;
13esac
14. /usr/share/initramfs-tools/hook-functions
15copy_exec `which tpm2_nvread`
16copy_exec /usr/lib/x86_64-linux-gnu/libtss2-tcti-device.so.0.0.0
17copy_exec /usr/lib/x86_64-linux-gnu/libtss2-tcti-device.so.0
18copy_exec /lib/cryptsetup/askpass
19exit 0
20EOF
21
22chown root: /etc/initramfs-tools/hooks/tpm2-decryptkey
23chmod 755 /etc/initramfs-tools/hooks/tpm2-decryptkey
- Update /etc/crypttab
1cp /etc/crypttab /etc/crypttab.backup
2sed -i 's%$%,keyscript=/usr/local/sbin/tpm2-getkey%' /etc/crypttab
3cp /boot/initrd.img-$(uname -r) /boot/initrd.img-$(uname -r).orig
4mkinitramfs -o /boot/initrd.img-$(uname -r) $(uname -r)
You are all done, you can reboot !
comments powered by Disqus

