@@ -107,6 +107,7 @@ Create the RSA key<br>
1071071 . ` tpm2_createprimary --hierarchy=o --key-algorithm=rsa --key-context=prim.ctx ` <br >
108108
109109Save it to the TPM persistent memory<br >
110+ {: start ="2"}<br >
1101112 . ` tpm2_evictcontrol --hierarchy=o --object-context=prim.ctx 0x81010001 ` <br >
111112
112113NOTE:
@@ -139,6 +140,7 @@ first setup a disk image without encryption and see if we can extract user
139140generated content.<br >
140141
141142Create a disk image and write some content:<br >
143+ {: start ="3"}<br >
1421443 . ` dd if=/dev/zero of=plain.disk bs=1M count=10 ` <br >
1431454 . ` mkfs.ext4 plain.disk ` <br >
1441465 . ` mkdir -p mountpoint ` <br >
@@ -155,7 +157,7 @@ find the plain.txt. To do this simply do the following:<br>
15515711 . ` strings plain.disk ` <br >
156158
157159NOTE:
158- * Having a week authentication mechanism for login control is not mitigated by
160+ * Having a weak authentication mechanism for login control is not mitigated by
159161disk encryption. Mounted encrypted volumes without user intervention are
160162available in clear to the logged in user.
161163* Data assets especially storage must remain confidential either with disk/ file
@@ -183,11 +185,13 @@ Let's setup a new LUKS volume with a simple passphrase as key protector:<br>
183185At this point you have setup the luks volume and it should pop a warning about
184186overriding the data. Next let's open the LUKS volume by authenticating with the
185187disk.key and complete the setting up the disk with a filesystem.<br >
188+ {: start ="16"}<br >
18618916 . ` sudo cryptsetup luksOpen --key-file=disk.key $loopdevice enc_volume ` <br >
18719017 . ` sudo mkfs.ext4 -j /dev/mapper/enc_volume ` <br >
18819118 . ` sudo mount /dev/mapper/enc_volume mountpoint ` <br >
189192
190193Now lets create a plain text file again and add user content to it:<br >
194+ {: start ="19"}<br >
19119519 . ` sudo touch mountpoint/plain.txt ` <br >
19219620 . ` sudo chmod 777 mountpoint/plain.txt ` <br >
19319721 . ` sudo echo "This is my plain text" > mountpoint/plain.txt ` <br >
@@ -197,6 +201,7 @@ Now lets create a plain text file again and add user content to it:<br>
197201
198202You will now see that you cannot dump the information from the disk image simply
199203: <br >
204+ {: start ="25"}<br >
20020525 . ` strings enc.disk | grep -i plain ` <br >
201206
202207NOTE:
@@ -217,35 +222,33 @@ Solution:<br>
217222a. Seal the secret into a TPM device.<br >
218223b. Unseal the secret in memory and pass it to cryptsetup.<br >
219224
220- Please note that there is an upstreaming effort in the cryptsetup
221- [ feature-branch] ( https://gitlab.com/cryptsetup/cryptsetup/-/tree/wip-tpm ) to
222- enable tpm2 as a key protector built natively in to libcryptsetup as well as
223- cryptsetup. It does so by adding a new keyslot-handler internally and extending
224- the libcryptsetup API. In this tutorial however, we will be using tpm2-tools
225- with cryptsetup that does not have built-in tpm2 support.<br >
226-
227225Let's start with creating and persisting a sealing object and sealing a random
228226byte sequence as the disk key.<br >
227+ {: start ="26"}<br >
22922826 . ` tpm2_createprimary -Q -C o -c prim.ctx ` <br >
23022927 . ` dd if=/dev/urandom bs=1 count=32 status=none | tpm2_create -Q -g sha256 -u seal.pub -r seal.priv -i- -C prim.ctx ` <br >
23123028 . ` tpm2_load -Q -C prim.ctx -u seal.pub -r seal.priv -n seal.name -c seal.ctx ` <br >
23223129 . ` tpm2_evictcontrol -C o -c seal.ctx 0x81010001 ` <br >
233232
234233Now lets change the authentication from previously created disk.key to the new
235234sealed secret and after that shred the disk.key since it's no longer useful:<br >
235+ {: start ="30"}
23623630 . ` tpm2_unseal -Q -c 0x81010001 | sudo cryptsetup luksChangeKey enc.disk --key-file disk.key ` <br >
23723731 . ` shred disk.key; rm -f disk.key ` <br >
238238
239239Now let's mount the volume with the new authentication sealed up in the tpm:<br >
240+ {: start ="32"}<br >
24024132 . ` sudo losetup $loopdevice enc.disk ` <br >
24124233 . ` tpm2_unseal -Q -c 0x81010001 |sudo cryptsetup luksOpen --key-file=- $loopdevice enc_volume ` <br >
24224334 . ` sudo mount /dev/mapper/enc_volume mountpoint ` <br >
243244
244245You can now see that disk access is granted with the new secret:<br >
246+ {: start ="35"}<br >
24524735 . ` ls mountpoint `
246248<br >
247249
248250Finally unmount the disk:<br >
251+ {: start ="36"}<br >
24925236 . ` sudo umount mountpoint ` <br >
25025337 . ` sudo cryptsetup remove enc_volume ` <br >
25125438 . ` sudo losetup -d $loopdevice ` <br >
@@ -280,13 +283,15 @@ c. After unsealing the pass-phrase; extend the sealing PCRs so that the
280283pass-phrase cannot be unsealed gain.<br >
281284
282285Let's begin with creating a pcr policy with current value in PCR0 sha256 bank<br >
286+ {: start ="39"}<br >
28328739 . ` tpm2_startauthsession -S session.ctx ` <br >
28428840 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 -L pcr0.sha256.policy ` <br >
28528941 . ` tpm2_flushcontext session.ctx ` <br >
286290
287291Now replace the seal object in TPM NV memory protecting the disk encryption
288292secret with a new one that adds the pcr policy we just created as an
289293authentication mechanism to access the sealed secret.<br >
294+ {: start ="42"}<br >
29029542 . ` tpm2_unseal -c 0x81010001 | tpm2_create -Q -g sha256 -u pcr_seal_key.pub -r pcr_seal_key.priv -i- -C prim.ctx -L pcr0.sha256.policy ` <br >
29129643 . ` tpm2_evictcontrol -C o -c 0x81010001 ` <br >
29229744 . ` tpm2_load -Q -C prim.ctx -u pcr_seal_key.pub -r pcr_seal_key.priv -n pcr_seal_key.name -c pcr_seal_key.ctx ` <br >
@@ -296,6 +301,7 @@ Now let's try to mount the encrypted disk again but this time the secret is
296301unsealed off a TPM object whose unsealing operation can only be accessed by
297302satisfying the PCR policy; in other words authenticating by virtue of intended
298303system software state being unchanged as reflected by the PCR value.<br >
304+ {: start ="46"}<br >
29930546 . ` sudo losetup $loopdevice enc.disk ` <br >
30030647 . ` tpm2_startauthsession --policy-session -S session.ctx ` <br >
30130748 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 ` <br >
@@ -306,6 +312,7 @@ directly to the cryptsetup app like this --> "tpm2_unseal -p session:session.ctx
306312However for the purpose of demonstrating flexible PCR in a later section we will
307313make a copy of the unsealed secret at this point to seal it with a new object
308314with flexible pcr policy. This breakdown to two steps<br >
315+ {: start ="49"}<br >
30931649 . ` tpm2_unseal -p session:session.ctx -c 0x81010001 > disk_secret.bkup ` <br >
31031750 . ` cat disk_secret.bkup | sudo cryptsetup luksOpen --key-file=- $loopdevice enc_volume ` <br >
31131851 . ` tpm2_flushcontext session.ctx ` <br >
@@ -321,16 +328,19 @@ consequence of failed policy check and thus a failed unsealing attempt.<br>
321328
322329Let's look at the PCR state prior to extending it and then again after
323330extending: <br >
331+ {: start ="54"}<br >
32433254 . ` tpm2_pcrlist -l sha256:0 ` <br >
32533355 . ` tpm2_pcrextend 0:sha256=0000000000000000000000000000000000000000000000000000000000000000 ` <br >
32633456 . ` tpm2_pcrlist -l sha256:0 ` <br >
327335
328336Now let's try to unseal the sealed disk encryption secret with the dirty
329337PCR:<br >
338+ {: start ="57"}<br >
33033957 . ` tpm2_startauthsession --policy-session -S session.ctx ` <br >
33134058 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 ` <br >
332341The following operation should result in policy check failure preventing the
333342unseal operation:<br >
343+ {: start ="59"}<br >
33434459 . ` tpm2_unseal -p session:session.ctx -c 0x81010001 ` <br >
33534560 . ` tpm2_flushcontext session.ctx ` <br >
336346
@@ -355,6 +365,7 @@ PCR signature. The PCR sets are signed by the system designer and verified by
355365the TPM. This is achieved in following steps:
356366
357367__ a. Get the new set of PCR and sign the pcr policy with signer private key.__ <br >
368+ {: start ="61"}<br >
35836961 . ` tpm2_startauthsession -S session.ctx ` <br >
35937062 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 -L set2.pcr.policy ` <br >
36037163 . ` tpm2_flushcontext session.ctx ` <br >
@@ -363,9 +374,11 @@ __a. Get the new set of PCR and sign the pcr policy with signer private key.__<b
363374
364375We now need the name which is a digest of the TCG public key format of the
365376public key to include in the policy. We can use the loadexternal tool for this:<br >
377+ {: start ="66"}<br >
36637866 . ` tpm2_loadexternal -G rsa -C o -u signing_key_public.pem -c signing_key.ctx -n signing_key.name ` <br >
367379
368380Let's now create the signer policy:<br >
381+ {: start ="67"}<br >
36938267 . ` tpm2_startauthsession -S session.ctx ` <br >
37038368 . ` tpm2_policyauthorize -S session.ctx -L authorized.policy -n signing_key.name -i set2.pcr.policy ` <br >
37138469 . ` tpm2_flushcontext session.ctx ` <br >
@@ -374,10 +387,12 @@ Let's create a new sealing object with the authorized policy which will also
374387require the sealing secret for which we will use the disk_secret.bkup we created
375388at #49 earlier to avoid rebooting the platform to match the PCR we originally
376389had prior to extending.<br >
390+ {: start ="70"}<br >
37739170 . ` cat disk_secret.bkup | tpm2_create -g sha256 -u auth_pcr_seal_key.pub -r auth_pcr_seal_key.priv -i- -C prim.ctx -L authorized.policy ` <br >
378392
379393Let's replace the old persistent sealing object with the one we created
380394above with policy_authorize policy associated with signer public key:<br >
395+ {: start ="71"}<br >
38139671 . ` tpm2_evictcontrol -C o -c 0x81010001 ` <br >
38239772 . ` tpm2_load -Q -C prim.ctx -u auth_pcr_seal_key.pub -r auth_pcr_seal_key.priv -n auth_pcr_seal_key.name -c auth_pcr_seal_key.ctx ` <br >
38339873 . ` tpm2_evictcontrol -c auth_pcr_seal_key.ctx 0x81010001 -C o ` <br >
@@ -386,15 +401,18 @@ Let's now sign the pcr_policy with the signer private key:<br>
386401
387402__ b. Load the signer public key to the tpm and verify the signature on the pcr
388403and get the tpm verification tkt:__ <br >
404+ {: start ="75"}<br >
38940575 . ` tpm2_loadexternal -G rsa -C o -u signing_key_public.pem -c signing_key.ctx -n signing_key.name ` <br >
39040676 . ` tpm2_verifysignature -c signing_key.ctx -g sha256 -m set2.pcr.policy -s set2.pcr.signature -t verification.tkt -f rsassa ` <br >
391407
392408__ c. Satisfy the authorized policy and then run policyauthorize:__ <br >
409+ {: start ="77"}<br >
39341077 . ` tpm2_startauthsession --policy-session -S session.ctx ` <br >
39441178 . ` tpm2_policypcr -l sha256:0 -S session.ctx ` <br >
39541279 . ` tpm2_policyauthorize -S session.ctx -i set2.pcr.policy -n signing_key.name -t verification.tkt ` <br >
396413
397414__ d. Pipe unseal output to the cryptsetup application:__ <br >
415+ {: start ="80"}<br >
39841680 . ` sudo losetup $loopdevice enc.disk ` <br >
39941781 . ` tpm2_unseal -p session:session.ctx -c 0x81010001 | sudo cryptsetup luksOpen --key-file=- $loopdevice enc_volume ` <br >
40041882 . ` tpm2_flushcontext session.ctx ` <br >
0 commit comments