Utilise la puce tpm2 pour déchiffrer ton disque au démarrage sans avoir à saisir ta phrase de passe.

Quand on utilise le chiffrement intégral du disque, c’est toujours frustrant de devoir taper son mot de passe à chaque démarrage de l’ordinateur. Surtout que sous Windows avec bitlocker activé, tu peux déchiffrer en utilisant la puce tpm2.

Crédits à https://run.tournament.org.il/ubuntu-20-04-and-tpm2-encrypted-system-disk/

Voici la procédure qui a fonctionné, du moins pour moi.

Installer les outils nécessaires

 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) ...

Définir un espace mémoire TPM2 pour stocker la clé

1tpm2_nvdefine -s 64 0x1500016

Générer une clé aléatoire de 64 caractères et l’écrire sur la puce tpm2

1cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 64 > root.key
2tpm2_nvwrite -i root.key 0x1500016

Tu peux vérifier que la clé dans la puce tpm correspond à ton 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!

Ajouter root.key comme clé de déchiffrement valide pour ton volume luks

1cryptsetup luksAddKey /dev/sda3 root.key

Tu peux identifier ta partition chiffrée avec lsblk

Mettre à jour l’initramfs pour rester résilient aux mises à jour du noyau

  • Créer /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
  • Créer /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
  • Mettre à jour /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)

Et voilà, c’est terminé, tu peux redémarrer !

comments powered by Disqus