@@ -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"}
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"}
1421443 . ` dd if=/dev/zero of=plain.disk bs=1M count=10 ` <br >
1431454 . ` mkfs.ext4 plain.disk ` <br >
1441465 . ` mkdir -p mountpoint ` <br >
@@ -175,6 +177,7 @@ provided as a password just in time (default) and or specified as key file
175177managing LUKS volumes.<br >
176178
177179Let's setup a new LUKS volume with a simple passphrase as key protector:<br >
180+ {: start ="12"}
17818112 . ` dd if=/dev/zero of=enc.disk bs=1M count=10 ` <br >
17918213 . ` dd if=/dev/urandom of=disk.key bs=1 count=32 ` <br >
18018314 . ` loopdevice=$(losetup -f) && sudo losetup $loopdevice enc.disk ` <br >
@@ -183,11 +186,13 @@ Let's setup a new LUKS volume with a simple passphrase as key protector:<br>
183186At this point you have setup the luks volume and it should pop a warning about
184187overriding the data. Next let's open the LUKS volume by authenticating with the
185188disk.key and complete the setting up the disk with a filesystem.<br >
189+ {: start ="16"}
18619016 . ` sudo cryptsetup luksOpen --key-file=disk.key $loopdevice enc_volume ` <br >
18719117 . ` sudo mkfs.ext4 -j /dev/mapper/enc_volume ` <br >
18819218 . ` sudo mount /dev/mapper/enc_volume mountpoint ` <br >
189193
190194Now lets create a plain text file again and add user content to it:<br >
195+ {: start ="16"}
19119619 . ` sudo touch mountpoint/plain.txt ` <br >
19219720 . ` sudo chmod 777 mountpoint/plain.txt ` <br >
19319821 . ` sudo echo "This is my plain text" > mountpoint/plain.txt ` <br >
@@ -197,6 +202,7 @@ Now lets create a plain text file again and add user content to it:<br>
197202
198203You will now see that you cannot dump the information from the disk image simply
199204: <br >
205+ {: start ="25"}
20020625 . ` strings enc.disk | grep -i plain ` <br >
201207
202208NOTE:
@@ -219,26 +225,31 @@ b. Unseal the secret in memory and pass it to cryptsetup.<br>
219225
220226Let's start with creating and persisting a sealing object and sealing a random
221227byte sequence as the disk key.<br >
228+ {: start ="26"}
22222926 . ` tpm2_createprimary -Q -C o -c prim.ctx ` <br >
22323027 . ` 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 >
22423128 . ` tpm2_load -Q -C prim.ctx -u seal.pub -r seal.priv -n seal.name -c seal.ctx ` <br >
22523229 . ` tpm2_evictcontrol -C o -c seal.ctx 0x81010001 ` <br >
226233
227234Now lets change the authentication from previously created disk.key to the new
228235sealed secret and after that shred the disk.key since it's no longer useful:<br >
236+ {: start ="30"}
22923730 . ` tpm2_unseal -Q -c 0x81010001 | sudo cryptsetup luksChangeKey enc.disk --key-file disk.key ` <br >
23023831 . ` shred disk.key; rm -f disk.key ` <br >
231239
232240Now let's mount the volume with the new authentication sealed up in the tpm:<br >
241+ {: start ="32"}
23324232 . ` sudo losetup $loopdevice enc.disk ` <br >
23424333 . ` tpm2_unseal -Q -c 0x81010001 |sudo cryptsetup luksOpen --key-file=- $loopdevice enc_volume ` <br >
23524434 . ` sudo mount /dev/mapper/enc_volume mountpoint ` <br >
236245
237246You can now see that disk access is granted with the new secret:<br >
247+ {: start ="35"}
23824835 . ` ls mountpoint `
239249<br >
240250
241251Finally unmount the disk:<br >
252+ {: start ="36"}
24225336 . ` sudo umount mountpoint ` <br >
24325437 . ` sudo cryptsetup remove enc_volume ` <br >
24425538 . ` sudo losetup -d $loopdevice ` <br >
@@ -273,13 +284,15 @@ c. After unsealing the pass-phrase; extend the sealing PCRs so that the
273284pass-phrase cannot be unsealed gain.<br >
274285
275286Let's begin with creating a pcr policy with current value in PCR0 sha256 bank<br >
287+ {: start ="39"}
27628839 . ` tpm2_startauthsession -S session.ctx ` <br >
27728940 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 -L pcr0.sha256.policy ` <br >
27829041 . ` tpm2_flushcontext session.ctx ` <br >
279291
280292Now replace the seal object in TPM NV memory protecting the disk encryption
281293secret with a new one that adds the pcr policy we just created as an
282294authentication mechanism to access the sealed secret.<br >
295+ {: start ="42"}
28329642 . ` 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 >
28429743 . ` tpm2_evictcontrol -C o -c 0x81010001 ` <br >
28529844 . ` 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 >
@@ -289,6 +302,7 @@ Now let's try to mount the encrypted disk again but this time the secret is
289302unsealed off a TPM object whose unsealing operation can only be accessed by
290303satisfying the PCR policy; in other words authenticating by virtue of intended
291304system software state being unchanged as reflected by the PCR value.<br >
305+ {: start ="46"}
29230646 . ` sudo losetup $loopdevice enc.disk ` <br >
29330747 . ` tpm2_startauthsession --policy-session -S session.ctx ` <br >
29430848 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 ` <br >
@@ -299,6 +313,7 @@ directly to the cryptsetup app like this --> "tpm2_unseal -p session:session.ctx
299313However for the purpose of demonstrating flexible PCR in a later section we will
300314make a copy of the unsealed secret at this point to seal it with a new object
301315with flexible pcr policy. This breakdown to two steps<br >
316+ {: start ="49"}
30231749 . ` tpm2_unseal -p session:session.ctx -c 0x81010001 > disk_secret.bkup ` <br >
30331850 . ` cat disk_secret.bkup | sudo cryptsetup luksOpen --key-file=- $loopdevice enc_volume ` <br >
30431951 . ` tpm2_flushcontext session.ctx ` <br >
@@ -314,16 +329,19 @@ consequence of failed policy check and thus a failed unsealing attempt.<br>
314329
315330Let's look at the PCR state prior to extending it and then again after
316331extending: <br >
332+ {: start ="54"}
31733354 . ` tpm2_pcrlist -l sha256:0 ` <br >
31833455 . ` tpm2_pcrextend 0:sha256=0000000000000000000000000000000000000000000000000000000000000000 ` <br >
31933556 . ` tpm2_pcrlist -l sha256:0 ` <br >
320336
321337Now let's try to unseal the sealed disk encryption secret with the dirty
322338PCR:<br >
339+ {: start ="57"}
32334057 . ` tpm2_startauthsession --policy-session -S session.ctx ` <br >
32434158 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 ` <br >
325342The following operation should result in policy check failure preventing the
326343unseal operation:<br >
344+ {: start ="59"}
32734559 . ` tpm2_unseal -p session:session.ctx -c 0x81010001 ` <br >
32834660 . ` tpm2_flushcontext session.ctx ` <br >
329347
@@ -348,6 +366,7 @@ PCR signature. The PCR sets are signed by the system designer and verified by
348366the TPM. This is achieved in following steps:
349367
350368__ a. Get the new set of PCR and sign the pcr policy with signer private key.__ <br >
369+ {: start ="61"}
35137061 . ` tpm2_startauthsession -S session.ctx ` <br >
35237162 . ` tpm2_policypcr -Q -S session.ctx -l sha256:0 -L set2.pcr.policy ` <br >
35337263 . ` tpm2_flushcontext session.ctx ` <br >
@@ -356,9 +375,11 @@ __a. Get the new set of PCR and sign the pcr policy with signer private key.__<b
356375
357376We now need the name which is a digest of the TCG public key format of the
358377public key to include in the policy. We can use the loadexternal tool for this:<br >
378+ {: start ="66"}
35937966 . ` tpm2_loadexternal -G rsa -C o -u signing_key_public.pem -c signing_key.ctx -n signing_key.name ` <br >
360380
361381Let's now create the signer policy:<br >
382+ {: start ="66"}
36238367 . ` tpm2_startauthsession -S session.ctx ` <br >
36338468 . ` tpm2_policyauthorize -S session.ctx -L authorized.policy -n signing_key.name -i set2.pcr.policy ` <br >
36438569 . ` tpm2_flushcontext session.ctx ` <br >
@@ -367,10 +388,12 @@ Let's create a new sealing object with the authorized policy which will also
367388require the sealing secret for which we will use the disk_secret.bkup we created
368389at #49 earlier to avoid rebooting the platform to match the PCR we originally
369390had prior to extending.<br >
391+ {: start ="70"}
37039270 . ` 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 >
371393
372394Let's replace the old persistent sealing object with the one we created
373395above with policy_authorize policy associated with signer public key:<br >
396+ {: start ="71"}
37439771 . ` tpm2_evictcontrol -C o -c 0x81010001 ` <br >
37539872 . ` 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 >
37639973 . ` tpm2_evictcontrol -c auth_pcr_seal_key.ctx 0x81010001 -C o ` <br >
@@ -379,15 +402,18 @@ Let's now sign the pcr_policy with the signer private key:<br>
379402
380403__ b. Load the signer public key to the tpm and verify the signature on the pcr
381404and get the tpm verification tkt:__ <br >
405+ {: start ="75"}
38240675 . ` tpm2_loadexternal -G rsa -C o -u signing_key_public.pem -c signing_key.ctx -n signing_key.name ` <br >
38340776 . ` tpm2_verifysignature -c signing_key.ctx -g sha256 -m set2.pcr.policy -s set2.pcr.signature -t verification.tkt -f rsassa ` <br >
384408
385409__ c. Satisfy the authorized policy and then run policyauthorize:__ <br >
410+ {: start ="77"}
38641177 . ` tpm2_startauthsession --policy-session -S session.ctx ` <br >
38741278 . ` tpm2_policypcr -l sha256:0 -S session.ctx ` <br >
38841379 . ` tpm2_policyauthorize -S session.ctx -i set2.pcr.policy -n signing_key.name -t verification.tkt ` <br >
389414
390415__ d. Pipe unseal output to the cryptsetup application:__ <br >
416+ {: start ="80"}
39141780 . ` sudo losetup $loopdevice enc.disk ` <br >
39241881 . ` tpm2_unseal -p session:session.ctx -c 0x81010001 | sudo cryptsetup luksOpen --key-file=- $loopdevice enc_volume ` <br >
39341982 . ` tpm2_flushcontext session.ctx ` <br >
0 commit comments