29
29
. /etc/config
30
30
fi
31
31
32
- TRACE_FUNC
33
32
34
33
# Busybox xxd lacks -r, and we get hex dumps from TPM1 commands. This converts
35
34
# a hex dump to binary data using sed and printf
36
35
hex2bin () {
36
+ TRACE_FUNC
37
37
sed ' s/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf
38
38
}
39
39
@@ -43,6 +43,7 @@ hex2bin() {
43
43
# as a file still chokes if the password begins with 'hex:', oddly tpm2-tools
44
44
# accepts 'hex:' in the file content.)
45
45
tpm2_password_hex () {
46
+ TRACE_FUNC
46
47
echo " hex:$( echo -n " $1 " | xxd -p | tr -d ' \n' ) "
47
48
}
48
49
@@ -61,7 +62,7 @@ tpm2_pcrread() {
61
62
62
63
if [ -z " $APPEND " ]; then
63
64
# Don't append - truncate file now so real command always
64
- # appends
65
+ # overwrites
65
66
true > " $file "
66
67
fi
67
68
@@ -79,7 +80,7 @@ tpm1_pcrread() {
79
80
80
81
if [ -z " $APPEND " ]; then
81
82
# Don't append - truncate file now so real command always
82
- # appends
83
+ # overwrites
83
84
true > " $file "
84
85
fi
85
86
@@ -100,11 +101,12 @@ is_hash() {
100
101
101
102
# extend_pcr_state - extend a PCR state value with more hashes or raw data (which is hashed)
102
103
# usage:
103
- # extend_pcr_state <alg> <initial_state > <files/hashes...>
104
+ # extend_pcr_state <alg> <state > <files/hashes...>
104
105
# alg - either 'sha1' or 'sha256' to specify algorithm
105
- # initial_state - a hash value setting the initial state
106
+ # state - a hash value setting the initial state
106
107
# files/hashes... - any number of files or hashes, state is extended once for each item
107
108
extend_pcr_state () {
109
+ TRACE_FUNC
108
110
local alg=" $1 "
109
111
local state=" $2 "
110
112
local next extend
@@ -233,14 +235,20 @@ tpm2_extend() {
233
235
while true ; do
234
236
case " $1 " in
235
237
-ix)
238
+ # store index and shift so -ic and -if can be processed
236
239
index=" $2 "
237
240
shift 2
238
241
;;
239
242
-ic)
243
+ string=$( echo -n " $2 " )
240
244
hash=" $( echo -n " $2 " | sha256sum | cut -d' ' -f1) "
245
+ TRACE_FUNC
246
+ DEBUG " TPM: Will extend PCR[$index ] with hash of string $string "
241
247
shift 2
242
248
;;
243
249
-if)
250
+ TRACE_FUNC
251
+ DEBUG " TPM: Will extend PCR[$index ] with hash of file content $2 "
244
252
hash=" $( sha256sum " $2 " | cut -d' ' -f1) "
245
253
shift 2
246
254
;;
@@ -250,7 +258,10 @@ tpm2_extend() {
250
258
esac
251
259
done
252
260
tpm2 pcrextend " $index :sha256=$hash "
253
- DO_WITH_DEBUG tpm2 pcrread " sha256:$index "
261
+ tpm2 pcrread " sha256:$index "
262
+
263
+ TRACE_FUNC
264
+ DEBUG " TPM: Extended PCR[$index ] with hash $hash "
254
265
}
255
266
256
267
tpm2_counter_read () {
@@ -348,9 +359,9 @@ tpm2_startsession() {
348
359
die " tpm2_flushcontext: unable to flush saved session"
349
360
tpm2 readpublic -Q -c " $PRIMARY_HANDLE " -t " $PRIMARY_HANDLE_FILE "
350
361
# TODO: do the right thing to not have to suppress "WARN: check public portion the tpmkey manually" see https://github.com/linuxboot/heads/pull/1630#issuecomment-2075120429
351
- tpm2 startauthsession -Q -c " $PRIMARY_HANDLE_FILE " --hmac-session -S " $ENC_SESSION_FILE " 2>&1 > /dev/null
362
+ tpm2 startauthsession -Q -c " $PRIMARY_HANDLE_FILE " --hmac-session -S " $ENC_SESSION_FILE " > /dev/null 2>&1
352
363
# TODO: do the right thing to not have to suppress "WARN: check public portion the tpmkey manually" see https://github.com/linuxboot/heads/pull/1630#issuecomment-2075120429
353
- tpm2 startauthsession -Q -c " $PRIMARY_HANDLE_FILE " --hmac-session -S " $DEC_SESSION_FILE " 2>&1 > /dev/null
364
+ tpm2 startauthsession -Q -c " $PRIMARY_HANDLE_FILE " --hmac-session -S " $DEC_SESSION_FILE " > /dev/null 2>&1
354
365
tpm2 sessionconfig -Q --disable-encrypt " $DEC_SESSION_FILE "
355
366
}
356
367
@@ -381,6 +392,7 @@ cleanup_shred() {
381
392
# tpm2_destroy: Destroy a sealed file in the TPM. The mechanism differs by
382
393
# TPM version - TPM2 evicts the file object, so it no longer exists.
383
394
tpm2_destroy () {
395
+ TRACE_FUNC
384
396
index=" $1 " # Index of the sealed file
385
397
size=" $2 " # Size of zeroes to overwrite for TPM1 (unused in TPM2)
386
398
@@ -396,6 +408,7 @@ tpm2_destroy() {
396
408
# TPM version - TPM1 overwrites the file with zeroes, since this can be done
397
409
# without authorization. (Deletion requires authorization.)
398
410
tpm1_destroy () {
411
+ TRACE_FUNC
399
412
index=" $1 " # Index of the sealed file
400
413
size=" $2 " # Size of zeroes to overwrite for TPM1
401
414
@@ -761,6 +774,21 @@ if [ "$CONFIG_TPM2_TOOLS" != "y" ]; then
761
774
shift
762
775
tpm1_destroy " $@ "
763
776
;;
777
+ extend)
778
+ # check if we extend with a hash or a file
779
+ if [ " $4 " = " -if" ]; then
780
+ DEBUG " TPM: Will extend PCR[$3 ] hash content of file $5 "
781
+ hash=" $( sha1sum " $5 " | cut -d' ' -f1) "
782
+ elif [ " $4 " = " -ic" ]; then
783
+ string=$( echo -n " $5 " )
784
+ DEBUG " TPM: Will extend PCR[$3 ] with hash of filename $string "
785
+ hash=" $( echo -n " $5 " | sha1sum | cut -d' ' -f1) "
786
+ fi
787
+
788
+ TRACE_FUNC
789
+ DEBUG " TPM: Extending PCR[$3 ] with hash $hash "
790
+ DO_WITH_DEBUG exec tpm " $@ "
791
+ ;;
764
792
seal)
765
793
shift
766
794
tpm1_seal " $@ "
@@ -799,6 +827,8 @@ calcfuturepcr)
799
827
replay_pcr " sha256" " $@ "
800
828
;;
801
829
extend)
830
+ TRACE_FUNC
831
+ DEBUG " TPM: Extending PCR[$2 ] with $4 "
802
832
tpm2_extend " $@ "
803
833
;;
804
834
counter_read)
0 commit comments