-
Notifications
You must be signed in to change notification settings - Fork 37
/
eltt2.c
1742 lines (1494 loc) · 61 KB
/
eltt2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @brief Embedded Linux TPM Toolbox 2 (ELTT2)
* @details eltt2.c implements some basic methods to communicate with the Infineon TPM 2.0 without the TDDL lib.
* @file eltt2.c
* @copyright Copyright (c) 2014 - 2017 Infineon Technologies AG ( www.infineon.com ).\n
* All rights reserved.\n
* \n
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:\n
* \n
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.\n
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.\n
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.\n
* \n
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "eltt2.h"
/**
* @brief Main entry point of the application.
* @details Handles the command line input and starts the communication with the TPM.
* @param [in] argc Counter for input parameters.
* @param [in] **argv Input parameters.
* @return One of the listed return codes, the TPM return code or the error code stored in the global errno system variable.
* @retval EXIT_SUCCESS In case of success.
* @retval ERR_BAD_CMD In case an invalid command line option.
* @retval value of errno In case of memory allocation error.
* @retval tpmtool_transmit All error codes from tpmtool_transmit.
* @retval return_error_handling All error codes from return_error_handling.
* @retval response_print All error codes from response_print.
* @retval create_hash_sequence All error codes from create_hash_sequence.
* @retval hexstr_to_bytearray All error codes from hexstr_to_bytearray.
* @retval pcr_extend All error codes from pcr_extend.
* @retval get_random All error codes from get_random.
* @retval pcr_read All error codes from pcr_read.
* @retval create_hash All error codes from create_hash.
* @retval pcr_reset All error codes from pcr_reset.
*/
int main(int argc, char **argv)
{
// ---------- Local declarations ----------
int ret_val = EXIT_SUCCESS; // Return value.
uint8_t *tpm_response_buf = NULL; // Buffer for TPM response.
ssize_t tpm_response_buf_size = 0; // Size of tpm_response_buf.
int i = 0; // Command line parsing counter.
int option = 0; // Command line option.
uint8_t *input_bytes = NULL; // Custom command bytes for transmit in case of command line options -b and -E.
size_t input_bytes_size = 0; // Size of input_bytes.
int no_transmission = 0; // Flag to skip the transmission call, e.g. in case of command line option -h.
int tpm_error = 0; // Flag to indicate whether a TPM response has returned a TPM error code or not.
hash_algo_enum hash_algo = ALG_NULL; // Variable to indicate the selected hash algorithm.
// ---------- Program flow ----------
printf("\n");
do // Begin of DO WHILE(FALSE) for error handling.
{
// ---------- Allocate memory for buffer containing TPM response ----------
tpm_response_buf_size = TPM_RESP_MAX_SIZE;
tpm_response_buf = malloc(tpm_response_buf_size);
MALLOC_ERROR_CHECK(tpm_response_buf);
memset(tpm_response_buf, 0xFF, tpm_response_buf_size);
// ---------- Check for command line parameters ----------
if (1 == argc)
{
fprintf(stderr, "ELTT needs an option. Use '-h' for displaying help.\n");
ret_val = ERR_BAD_CMD;
break;
}
// ---------- Command line parsing with getopt ----------
opterr = 0; // Disable getopt error messages in case of unknown parameters; we want to use our own error messages.
// Loop through parameters with getopt.
while (-1 != (option = getopt(argc, argv, "cgvhTa:A:b:d:e:E:G:l:r:R:s:S:t:u:z:")))
{
switch (option)
{
case 'a': // TPM2_HashSequenceStart SHA-1/256/384
case 'A': // TPM2_HashSequenceStart SHA-256
HASH_ALG_PARSER('a', 3);
ret_val = create_hash_sequence(optarg, hash_algo, tpm_response_buf, &tpm_response_buf_size);
break;
case 'b': // Enter your own command bytes
// Allocate the input buffer for hexstr_to_bytearray and tpmtool_transmit.
input_bytes_size = strlen(optarg) / HEX_BYTE_STRING_LENGTH + strlen(optarg) % HEX_BYTE_STRING_LENGTH; // 2 characters == 1 byte => size of input command bytes: length of input string / 2.
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0xFF, input_bytes_size);
// Convert the command line input to bytes.
ret_val = hexstr_to_bytearray(optarg, input_bytes, input_bytes_size);
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
case 'c': // TPM_CC_ReadClock
ret_val = tpmtool_transmit(tpm_cc_readclock, sizeof(tpm_cc_readclock), tpm_response_buf, &tpm_response_buf_size);
break;
case 'd': // TPM_CC_Shutdown
if (0 == strcasecmp(optarg, "clear"))
{
ret_val = tpmtool_transmit(tpm_cc_shutdown_clear, sizeof(tpm_cc_shutdown_clear), tpm_response_buf, &tpm_response_buf_size);
}
else if (0 == strcasecmp(optarg, "state"))
{
ret_val = tpmtool_transmit(tpm_cc_shutdown_state, sizeof(tpm_cc_shutdown_state), tpm_response_buf, &tpm_response_buf_size);
}
else
{
ret_val = ERR_BAD_CMD;
fprintf(stderr, "Unknown option. Use '-h' for more information.\n");
}
break;
case 'e': // PCR_Extend SHA-1/256/384
case 'E': // PCR_Extend SHA-256
if (4 > argc)
{
ret_val = ERR_BAD_CMD;
fprintf(stderr, "The command '-%c' needs minimum two arguments. Use '-h' for more information.\n", option);
// Set the argument count to the next option for error handling.
optind += 2;
break;
}
HASH_ALG_PARSER('e', 4);
// Allocate the input buffer for pcr_extend and tpmtool_transmit.
if (ALG_SHA1 == hash_algo)
{
input_bytes_size = sizeof(tpm2_pcr_extend) + TPM_SHA1_DIGEST_SIZE;
}
else if (ALG_SHA256 == hash_algo)
{
input_bytes_size = sizeof(tpm2_pcr_extend) + TPM_SHA256_DIGEST_SIZE;
}
else
{
input_bytes_size = sizeof(tpm2_pcr_extend) + TPM_SHA384_DIGEST_SIZE;
}
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0, input_bytes_size);
// Create PCR_Extend TPM request.
ret_val = pcr_extend(optarg, argv[optind], input_bytes, input_bytes_size, hash_algo);
// Set the argument count to the next option for error handling.
optind++;
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
case 'g': // TPM_CC_GetCapability
ret_val = tpmtool_transmit(tpm2_getcapability_fixed, sizeof(tpm2_getcapability_fixed), tpm_response_buf, &tpm_response_buf_size);
break;
case 'v': // TPM_CC_GetCapability
ret_val = tpmtool_transmit(tpm2_getcapability_var, sizeof(tpm2_getcapability_var), tpm_response_buf, &tpm_response_buf_size);
break;
case 'G': // TPM_CC_GetRandom
// Allocate the input buffer for get_random and tpmtool_transmit.
input_bytes_size = (sizeof(tpm2_getrandom));
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0, input_bytes_size);
// Create GetRandom TPM request.
ret_val = get_random(optarg, input_bytes);
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
case 'h': // Help
print_help();
// Set flag to skip any TPM transmission
no_transmission = 1;
break;
case 'l': // PCR_Allocate SHA-1/256/384
HASH_ALG_PARSER('l', -1);
// Allocate the input buffer for pcr_read and tpmtool_transmit.
input_bytes_size = sizeof(tpm2_pcr_allocate);
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0, input_bytes_size);
// Create PCR_Allocate TPM request.
ret_val = pcr_allocate(input_bytes, hash_algo);
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
case 'r': // PCR_Read SHA-1/256/384
case 'R': // PCR_Read SHA-256
HASH_ALG_PARSER('r', 3);
// Allocate the input buffer for pcr_read and tpmtool_transmit.
input_bytes_size = sizeof(tpm2_pcr_read);
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0, input_bytes_size);
// Create PCR_Read TPM request.
ret_val = pcr_read(optarg, input_bytes, hash_algo);
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
case 's': // Hash SHA-1/256/384
case 'S': // Hash SHA-256
HASH_ALG_PARSER('s', 3);
// Allocate the input buffer for create_hash and tpmtool_transmit.
input_bytes_size = strlen(optarg) / HEX_BYTE_STRING_LENGTH + strlen(optarg) % HEX_BYTE_STRING_LENGTH + sizeof(tpm2_hash);
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0, input_bytes_size);
// Create Hash TPM request.
ret_val = create_hash(optarg, hash_algo, input_bytes, input_bytes_size);
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
case 't': // TPM2_SelfTest
if (0 == strcasecmp(optarg, "not_full"))
{
ret_val = tpmtool_transmit(tpm2_self_test, sizeof(tpm2_self_test), tpm_response_buf, &tpm_response_buf_size);
}
else if (0 == strcasecmp(optarg, "full"))
{
ret_val = tpmtool_transmit(tpm2_self_test_full, sizeof(tpm2_self_test_full), tpm_response_buf, &tpm_response_buf_size);
}
else if (0 == strcasecmp(optarg, "incremental"))
{
ret_val = tpmtool_transmit(tpm2_self_test_incremental, sizeof(tpm2_self_test_incremental), tpm_response_buf, &tpm_response_buf_size);
}
else
{
ret_val = ERR_BAD_CMD;
fprintf(stderr, "Unknown option. Use '-h' for more information.\n");
}
break;
case 'T': // TPM_CC_GetTestResult
ret_val = tpmtool_transmit(tpm_cc_get_test_result, sizeof(tpm_cc_get_test_result), tpm_response_buf, &tpm_response_buf_size);
break;
case 'u': // TPM2_Startup
if (0 == strcasecmp(optarg, "clear"))
{
ret_val = tpmtool_transmit(tpm2_startup_clear, sizeof(tpm2_startup_clear), tpm_response_buf, &tpm_response_buf_size);
}
else if (0 == strcasecmp(optarg, "state"))
{
ret_val = tpmtool_transmit(tpm2_startup_state, sizeof(tpm2_startup_state), tpm_response_buf, &tpm_response_buf_size);
}
else
{
ret_val = ERR_BAD_CMD;
fprintf(stderr, "Unknown option. Use '-h' for more information.\n");
}
break;
case 'z': // PCR_Reset
// Allocate the input buffer for pcr_reset and tpmtool_transmit
input_bytes_size = sizeof(tpm2_pcr_reset);
input_bytes = malloc(input_bytes_size);
MALLOC_ERROR_CHECK(input_bytes);
memset(input_bytes, 0, input_bytes_size);
// Create PCR_Reset TPM request.
ret_val = pcr_reset(optarg, input_bytes);
RET_VAL_CHECK(ret_val);
// Send bytes to TPM.
ret_val = tpmtool_transmit(input_bytes, input_bytes_size, tpm_response_buf, &tpm_response_buf_size);
break;
default:
if ('a' == optopt || 'A' == optopt || 'b' == optopt || 'e' == optopt || 'E' == optopt || 'G' == optopt ||
'l' == optopt || 'r' == optopt || 'R' == optopt || 's' == optopt || 'S' == optopt || 'z' == optopt)
{
// Error output if arguments are missing.
fprintf(stderr, "Option '-%c' requires additional arguments. Use '-h' for more information.\n", optopt);
ret_val = ERR_BAD_CMD;
}
else if ('d' == optopt)
{
// TPM shutdown default option without parameter (default is tpm_cc_shutdown_clear).
ret_val = tpmtool_transmit(tpm_cc_shutdown_clear, sizeof(tpm_cc_shutdown_clear), tpm_response_buf, &tpm_response_buf_size);
option='d'; // for response_print handler
}
else if ('t' == optopt)
{
// TPM shutdown default option without parameter (default is tpm2_self_test).
ret_val = tpmtool_transmit(tpm2_self_test, sizeof(tpm2_self_test), tpm_response_buf, &tpm_response_buf_size);
option='t'; // for response_print handler
}
else if ('u' == optopt)
{
// TPM startup default option without parameter (default is tpm2_startup_clear).
ret_val = tpmtool_transmit(tpm2_startup_clear, sizeof(tpm2_startup_clear), tpm_response_buf, &tpm_response_buf_size);
option='u'; // for response_print handler
}
else if (isprint(optopt))
{
// Unknown parameter.
fprintf(stderr, "Unknown option '-%c'. Use '-h' for more information.\n", optopt);
ret_val = ERR_BAD_CMD;
}
else
{
// Non-printable character.
fprintf(stderr, "Invalid command line character. Use '-h' for more information.\n");
ret_val = ERR_BAD_CMD;
}
break;
} // End of switch.
// ---------- Output and error handling ----------
// Check for transmission errors or skipped transmission.
if (EXIT_SUCCESS != ret_val || 1 == no_transmission)
{
// Exit command line parameter parsing loop.
break;
}
// Transmission has been successful, now get TPM return code from TPM response.
ret_val = return_error_handling(tpm_response_buf);
if (EXIT_SUCCESS != ret_val) // Check for errors
{
// Set flag to indicate a TPM error.
tpm_error = 1;
// Go out of command line parameter parsing loop.
break;
}
// Print TPM response
ret_val = response_print(tpm_response_buf, tpm_response_buf_size, option);
RET_VAL_CHECK(ret_val);
// Free memory for next command line option
MEMSET_FREE(input_bytes, input_bytes_size);
} // End of while (command line parameter parsing loop).
// If no error has occurred so far, handle remaining unknown parameters, if present.
RET_VAL_CHECK(ret_val); // If we do not check and break here in case of an error, we would override the previous error
for (i = optind; i < argc; i++)
{
ret_val = ERR_BAD_CMD;
fprintf(stderr, "Non-option argument '%s'. Use '-h' for more information.\n", argv[i]);
}
} while (0); // End of DO WHILE FALSE loop.
// Check for non-TPM error.
if (EXIT_SUCCESS != ret_val && 1 != tpm_error)
{
fprintf(stderr, "Unexpected error: 0x%08X\n", ret_val);
}
// Map TPM return value 0x100 (TPM_RC_INITIALIZE) to 0x101 (TPM_RC_FAILURE), since in case you
// run ELTT 2 in a python script only the lowest byte of the return code is actually being returned.
// But since the lowest byte of 0x100 is 0x00 (== TPM_RC_SUCCESS), python would not be able to
// distinguish between 0x000 and 0x100 as return code, therefore we need the mapping.
if (TPM_RC_INITIALIZE == ret_val)
{
ret_val = TPM_RC_FAILURE;
}
// ---------- Cleanup ----------
MEMSET_FREE(tpm_response_buf, tpm_response_buf_size);
MEMSET_FREE(input_bytes, input_bytes_size);
printf("\n");
return ret_val;
}
int tpmtool_transmit(const uint8_t *buf, ssize_t length, uint8_t *response, ssize_t *resp_length)
{
// ---------- Transmit command given in buf to device with handle given in dev_tpm ----------
int ret_val = EXIT_SUCCESS; // Return value.
int dev_tpm = -1; // TPM device handle.
ssize_t transmit_size = 0; // Amount of bytes sent to / received from the TPM.
do
{
// Check input parameters.
NULL_POINTER_CHECK(buf);
NULL_POINTER_CHECK(response);
NULL_POINTER_CHECK(resp_length);
if (0 >= length)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter 'length' must be larger than 0.");
break;
}
if (TPM_REQ_MAX_SIZE < length)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter 'length' must be smaller than or equal to %u.", TPM_REQ_MAX_SIZE);
break;
}
if (TPM_CMD_HEADER_SIZE >= *resp_length)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter '*resp_length' must be at least %u.", TPM_CMD_HEADER_SIZE);
break;
}
if (TPM_RESP_MAX_SIZE < *resp_length)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter '*resp_length' must be smaller than or equal to %u.", TPM_RESP_MAX_SIZE);
break;
}
memset(response, 0, *resp_length);
// ---------- Open TPM device ----------
dev_tpm = open("/dev/tpm0", O_RDWR);
if (-1 == dev_tpm)
{
ret_val = errno;
fprintf(stderr, "Error opening the device.\n");
break;
}
// Send request data to TPM.
transmit_size = write(dev_tpm, buf, length);
if (transmit_size == ERR_COMMUNICATION || length != transmit_size)
{
ret_val = errno;
fprintf(stderr, "Error sending request to TPM.\n");
break;
}
// Read the TPM response header.
transmit_size = read(dev_tpm, response, TPM_RESP_MAX_SIZE);
if (transmit_size == ERR_COMMUNICATION)
{
ret_val = errno;
fprintf(stderr, "Error reading response from TPM.\n");
break;
}
// Update response buffer length with value of data length returned by TPM.
*resp_length = transmit_size;
} while (0);
// ---------- Close TPM device ----------
if (-1 != dev_tpm)
{
// Close file handle.
close(dev_tpm);
// Invalidate file handle.
dev_tpm = -1;
}
return ret_val;
}
static int return_error_handling(uint8_t *response_buf)
{
int ret_val = EXIT_SUCCESS; // Return value.
uint64_t tpm_rc = 0; // Return code from TPM header.
do
{
NULL_POINTER_CHECK(response_buf);
ret_val = buf_to_uint64(response_buf, 6, sizeof(uint32_t), &tpm_rc, TPM_RESP_MAX_SIZE); // TPM Return code type is 32 bit unsigned int!
RET_VAL_CHECK(ret_val);
// Assign TPM return code to ret_val.
ret_val = tpm_rc;
if (TPM_RC_SUCCESS != tpm_rc)
{
fprintf(stderr, "TPM Error 0x%.8" PRIx64 ": ", tpm_rc);
}
// Handle some known TPM return codes.
switch (tpm_rc)
{
case TPM_RC_SUCCESS: // 0x00
break;
case TPM_RC_BAD_TAG: // 0x1E
fprintf(stderr, "The tag value sent to for a command is invalid.\n");
break;
case TPM_RC_SIZE: // 0x95
fprintf(stderr, "Structure is the wrong size.\n");
break;
case TPM_RC_INITIALIZE: // 0x100
fprintf(stderr, "TPM not initialized.\n");
break;
case TPM_RC_LOCALITY: // 0x907
fprintf(stderr, "Bad locality.\n");
break;
default:
fprintf(stderr, "See TPM Library Specification for more information.\n");
break;
}
} while (0);
return ret_val;
}
static int response_print(uint8_t *response_buf, size_t resp_size, int option)
{
int ret_val = EXIT_SUCCESS; // Return value.
do
{
NULL_POINTER_CHECK(response_buf);
if (0 >= resp_size)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter 'resp_size' must be larger than 0.");
break;
}
if (TPM_RESP_MAX_SIZE < resp_size)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter 'resp_size' must be smaller than or equal to %u.\n", TPM_RESP_MAX_SIZE);
break;
}
switch (option)
{
case 'a':
case 'A': // Print the returned hash number.
ret_val = print_response_buf(response_buf, resp_size, PRINT_RESPONSE_HASH_WITHOUT_HEADER, PRINT_RESPONSE_HASH);
break;
case 'b': // Print the response value in hex.
case 'e':
case 'E': // Print the PCR extend response.
ret_val = print_response_buf(response_buf, resp_size, PRINT_RESPONSE_WITH_HEADER, PRINT_RESPONSE_HEADERBLOCKS);
break;
case 'c': // Print the TPM clock values.
ret_val = print_clock_info(response_buf);
break;
case 'd': // Print "Shutdown works as expected."
printf("Shutdown works as expected.\n");
break;
case 'g': // Print the fixed capability flags.
ret_val = print_capability_flags(response_buf, PT_FIXED_SELECTOR);
break;
case 'v': // Print the variable capability flags.
ret_val = print_capability_flags(response_buf, PT_VAR_SELECTOR);
break;
case 'G': // Print the returned random value in hex.
printf("Random value:\n");
ret_val = print_response_buf(response_buf, resp_size, PRINT_RESPONSE_WITHOUT_HEADER, PRINT_RESPONSE_HEX_BLOCK);
break;
case 'r':
case 'R': // Print the response value in hex.
ret_val = print_response_buf(response_buf, resp_size, PRINT_RESPONSE_PCR_WITHOUT_HEADER, PRINT_RESPONSE_CLEAR);
break;
case 's':
case 'S': // Print the returned hash number.
ret_val = print_response_buf(response_buf, resp_size, PRINT_RESPONSE_WITHOUT_HEADER, PRINT_RESPONSE_HASH);
break;
case 't': // Print "Successfully tested. Works as expected."
printf("Successfully tested. Works as expected.\n");
break;
case 'T': // Print response value hex without the header.
printf("Test Result: ");
ret_val = print_response_buf(response_buf, resp_size, PRINT_RESPONSE_WITHOUT_HEADER, PRINT_RESPONSE_CLEAR);
break;
case 'u': // Print "Startup works as expected."
printf("Startup works as expected.\n");
break;
default: // Print "Done."
printf("Done.\n");
break;
}
printf("\n");
} while (0);
return ret_val;
}
static int print_response_buf(uint8_t *response_buf, size_t resp_size, uint32_t offset, int format)
{
int ret_val = EXIT_SUCCESS; // Return value.
uint32_t i = 0; // Loop variable.
uint64_t data_size = 0; // Size of response data.
do
{
NULL_POINTER_CHECK(response_buf);
if (0 >= resp_size)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter 'resp_size' must be larger than 0.");
break;
}
if (TPM_RESP_MAX_SIZE < resp_size)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Value of parameter 'resp_size' must be smaller than or equal to %u.\n", TPM_RESP_MAX_SIZE);
break;
}
if (resp_size <= offset)
{
ret_val = EINVAL;
fprintf(stderr, "Bad parameter. Offset %u cannot be equal or larger than input buffer size %zu.\n", offset, resp_size);
break;
}
switch (format)
{
case PRINT_RESPONSE_CLEAR:
for (i = 0; i < resp_size - offset; i++)
{
printf("%02X ", response_buf[i + offset]);
}
break;
case PRINT_RESPONSE_HEADERBLOCKS:
if (TPM_CMD_HEADER_SIZE > resp_size)
{
ret_val = EINVAL;
fprintf(stderr, "Response size is too small.\n");
break;
}
printf("TPM Response:\n");
for (i = 0; i < resp_size - offset; i++)
{
printf("%02X ", response_buf[i + offset]);
if (i == 1) // Bytes 0 and 1 are TPM TAG.
{
printf(" TPM TAG\n");
}
else if (i == 5) // Bytes 2 to 5 are the response length.
{
printf(" RESPONSE SIZE\n");
}
else if (i == 9) // Last 4 bytes in header are the TPM return code.
{
printf(" RETURN CODE\n");
if (i + 1 < resp_size - offset)
{
printf(" Command-specific response Data:\n");
}
}
else if (i >= TPM_CMD_HEADER_SIZE && (i+1) % 10 == 0) // After all header bytes have been printed, start new line after every 10 bytes of data.
{
printf("\n");
}
}
break;
case PRINT_RESPONSE_HEX_BLOCK:
for (i = 0; i < resp_size - offset; i++)
{
if (i % 16 == 0)
{
printf("\n0x%08X: ", i);
}
printf("0x%02X ", response_buf[i + offset]);
}
break;
case PRINT_RESPONSE_HASH:
ret_val = buf_to_uint64(response_buf, offset - sizeof(uint16_t), sizeof(uint16_t), &data_size, resp_size); // Data size actually is only an uint16_t and should always be stored right before the actual data
if (data_size > resp_size - offset)
{
ret_val = EINVAL;
fprintf(stderr, "Invalid response data size.\n");
break;
}
for (i = 0; i < data_size; i++)
{
if (i % 8 == 0)
{
printf("\n0x%08X: ", i);
}
printf("%02X ", response_buf[i + offset]);
}
break;
default:
ret_val = EINVAL;
fprintf(stderr, "Unknown output format.\n");
break;
}
} while (0);
return ret_val;
}
static void print_help()
{
printf("'-a [hash algorithm] <data bytes>': Hash Sequence SHA-1/256/384 [default: SHA-1]\n");
printf(" -> Hash algorithm: Enter hash algorithm like 'sha1', 'sha256', 'sha384'\n");
printf(" Data bytes: Enter a byte sequence like '0F56...' for {0x0f, 0x56, ...}\n");
printf("'-A <data bytes>': Hash Sequence SHA-256\n");
printf(" -> Data bytes: Enter a byte sequence like '0F56...' for {0x0f, 0x56, ...}\n");
printf("'-b <command bytes>': Enter your own TPM command\n");
printf(" -> Command bytes: Enter your command bytes in hex like '0f56...' for {0x0f, 0x56, ...}\n");
printf("'-c': Read Clock\n");
printf("'-d <shutdown type>': Shutdown\n");
printf(" -> Shutdown types: clear [default], state\n");
printf("'-e [hash algorithm] <PCR index> <PCR digest>': PCR Extend SHA-1/256/384 [default: SHA-1]\n");
printf(" -> Hash algorithm: Enter hash algorithm like 'sha1', 'sha256', 'sha384'\n");
printf(" PCR index: Enter the PCR index in hex like '17' for 0x17\n");
printf(" PCR digest: Enter the value to extend the PCR with in hex like '0f56...' for {0x0f, 0x56, ...}\n");
printf("'-E <PCR index> <PCR digest>': PCR Extend SHA-256\n");
printf(" -> PCR index: Enter the PCR index in hex like '17' for 0x17\n");
printf(" PCR digest: Enter the value to extend the PCR with in hex like '0f56...' for {0x0f, 0x56, ...}\n");
printf("'-g': Get fixed capability values\n");
printf("'-v': Get variable capability values\n");
printf("'-G <byte count>': Get Random\n");
printf(" -> Enter desired number of random bytes in hex like '20' for 0x20 (=32 bytes, maximum)\n");
printf("'-h': Help\n");
printf("'-l <hash algorithm>': PCR allocate SHA-1/256/384\n");
printf(" -> Hash algorithm: Enter hash algorithm like 'sha1', 'sha256', 'sha384'\n");
printf("'-r [hash algorithm] <PCR index>': PCR Read SHA-1/256/384 [default: SHA-1]\n");
printf(" -> Hash algorithm: Enter hash algorithm like 'sha1', 'sha256', 'sha384'\n");
printf(" PCR index: Enter PCR number in hex like '17' for 0x17\n");
printf("'-R <PCR index>': PCR Read SHA-256\n");
printf(" -> PCR index: Enter PCR number in hex like '17' for 0x17\n");
printf("'-s [hash algorithm] <data bytes>': Hash SHA-1/256/384 [default: SHA-1]\n");
printf(" -> Hash algorithm: Enter hash algorithm like 'sha1', 'sha256', 'sha384'\n");
printf(" Data bytes: Enter a byte sequence like '0F56...' for {0x0f, 0x56, ...}\n");
printf("'-S <data bytes>': Hash SHA-256\n");
printf(" -> Data bytes: Enter a byte sequence like '0F56...' for {0x0f, 0x56, ...}\n");
printf("'-t <selftest type>': SelfTest\n");
printf(" -> Selftest type: not_full [default], full, incremental\n");
printf("'-T': Get Test Result\n");
printf("'-u <startup type>': Startup\n");
printf(" -> Startup types: clear [default], state\n");
printf("'-z <PCR index>': PCR Reset SHA-1, SHA-256, and SHA-384\n");
printf(" -> PCR index: Enter PCR number in hex like '17' for 0x17\n");
}
static int print_capability_flags(uint8_t *response_buf, uint8_t cap_selector)
{
int ret_val = EXIT_SUCCESS; // Return value.
uint64_t propertyValue = 0; // Value of the read property.
uint64_t propertyKey = 0; // Key of the property.
int tmp = 0; // Temporary buffer.
do
{
NULL_POINTER_CHECK(response_buf);
if(cap_selector == PT_FIXED_SELECTOR)
{
printf("\nTPM capability information of fixed properties:\n");
printf("=========================================================\n");
for(int x = 0x13; x<(TPM_RESP_MAX_SIZE-8); x+=8)
{ //Iterate over each property key/value pair
ret_val = buf_to_uint64(response_buf, x, 4, &propertyKey, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
ret_val = buf_to_uint64(response_buf, x+4, 4, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
switch(propertyKey)
{
case 0x100:
printf("TPM_PT_FAMILY_INDICATOR: %c%c%c%c\n", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
break;
case 0x100+1:
printf("TPM_PT_LEVEL: %" PRIu64 "\n", propertyValue);
break;
case 0x100+2:
printf("TPM_PT_REVISION: %" PRIu64 "\n", propertyValue);
break;
case 0x100+3:
printf("TPM_PT_DAY_OF_YEAR: %" PRIu64 "\n", propertyValue);
break;
case 0x100+4:
printf("TPM_PT_YEAR: %" PRIu64 "\n", propertyValue);
break;
case 0x100+5:
printf("TPM_PT_MANUFACTURER: %c%c%c%c\n", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
break;
case 0x100+6:
printf("TPM_PT_VENDOR_STRING: ");
printf("%c%c%c%c", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
break;
case 0x100+7: // it is assumed that TPM_PT_VENDOR_STRING_2 follows _1
printf("%c%c%c%c", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
break;
case 0x100+8:
printf("%c%c%c%c", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
break;
case 0x100+9:
printf("%c%c%c%c\n", response_buf[x+4], response_buf[x+5], response_buf[x+6], response_buf[x+7]);
break;
case 0x100+10:
printf("TPM_PT_VENDOR_TPM_TYPE: %" PRIu64 "\n", propertyValue);
break;
case 0x100+11:
// special handling for firmware version XX.xx.xxxx.x
ret_val = buf_to_uint64(response_buf, x+4, 2, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
printf("TPM_PT_FIRMWARE_VERSION: %" PRIu64 "", propertyValue);
ret_val = buf_to_uint64(response_buf, x+6, 2, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
printf(".%" PRIu64 "", propertyValue);
break;
case 0x100+12:
// special handling for firmware version XX.xx.xxxx.x
ret_val = buf_to_uint64(response_buf, x+4, 2, &propertyValue, TPM_RESP_MAX_SIZE); // Check for output version.
RET_VAL_CHECK(ret_val);
if (2 <= propertyValue) // Infineon custom format
{
ret_val = buf_to_uint64(response_buf, x+5, 2, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
printf(".%" PRIu64 "", propertyValue);
ret_val = buf_to_uint64(response_buf, x+7, 1, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
printf(".%" PRIu64 "\n", propertyValue);
}
else
{
ret_val = buf_to_uint64(response_buf, x+4, 4, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
printf(".%" PRIu64 "\n", propertyValue);
}
break;
case 0x100+24:
printf("\nTPM_PT_MEMORY:\n");
printf("=========================================================\n");
tmp = ((propertyValue & (1<<0)) == 0? 0:1); // Check bit 0 value.
printf("Shared RAM: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<1)) == 0? 0:1); // Check bit 1 value.
printf("Shared NV: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<2)) == 0? 0:1); // Check bit 2 value.
printf("Object Copied To Ram: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
//bit 31:3 = reserved
break;
case 0x200:
printf("\nTPM_PT_PERMANENT:\n");
printf("=========================================================\n");
tmp = ((propertyValue & (1<<0)) == 0? 0:1); // Check bit 0 value.
printf("Owner Auth Set: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<1)) == 0? 0:1); // Check bit 1 value.
printf("Sendorsement Auth Set: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<2)) == 0? 0:1); // Check bit 2 value.
printf("Lockout Auth Set: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
//bit 7:3 = reserved
tmp = ((propertyValue & (1<<8)) == 0? 0:1); // Check bit 8 value.
printf("Disable Clear: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<9)) == 0? 0:1); // Check bit 9 value.
printf("In Lockout: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<10)) == 0? 0:1); // Check bit 10 value.
printf("TPM Generated EPS: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
//bit 31:11 = reserved
break;
default:
// Unknown attribute - ignore
break;
}
}
}
else if (cap_selector == PT_VAR_SELECTOR)
{
NULL_POINTER_CHECK(response_buf);
printf("\nTPM capability information of variable properties:\n");
for(int x = 0x13; x<TPM_RESP_MAX_SIZE-8; x+=8)
{ //Iterate over each property key/value pair
ret_val = buf_to_uint64(response_buf, x, 4, &propertyKey, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
ret_val = buf_to_uint64(response_buf, x+4, 4, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
switch(propertyKey)
{
case 0x201:
printf("\nTPM_PT_STARTUP_CLEAR:\n");
printf("=========================================================\n");
tmp = ((propertyValue & (1<<0)) == 0? 0:1); // Check bit 0 value.
printf("Ph Enable: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<1)) == 0? 0:1); // Check bit 1 value.
printf("Sh Enable: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
tmp = ((propertyValue & (1<<2)) == 0? 0:1); // Check bit 2 value.
printf("Eh Enable: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
//bit 30:3 = reserved
tmp = ((propertyValue & (1U<<31)) == 0? 0:1); // Check bit 31 value.
printf("Orderly: %i %s", (tmp), ((tmp)? "SET\n" : "CLEAR\n"));
break;
default:
// Unknown attribute - ignore
break;
}
}
}
} while (0);
return ret_val;
}
static int print_clock_info(uint8_t *response_buf)
{
int ret_val = EXIT_SUCCESS; // Return value.
uint64_t propertyValue = 0; // Value of the read property.
uint64_t tmp_value = 0; // Helper variable for calculating actual values.
uint64_t sec = 0; // Value for seconds.
uint64_t tmp = 0; // buf_to_uint64 return value.
do
{
NULL_POINTER_CHECK(response_buf);
printf("\nClock info:\n");
printf("=========================================================\n");
ret_val = buf_to_uint64(response_buf, TPM_CMD_HEADER_SIZE, 8, &propertyValue, TPM_RESP_MAX_SIZE);
RET_VAL_CHECK(ret_val);
printf("Time since the last TPM_Init:\n%" PRIu64 " ms = ", propertyValue);
sec = propertyValue / MILISECOND_TO_SECOND;
tmp_value = sec / YEAR_SECONDS;
printf("%" PRIu64 " y, ", tmp_value);
tmp_value = (sec % YEAR_SECONDS) / DAY_SECONDS;
printf("%" PRIu64 " d, ", tmp_value);
tmp_value = ((sec % YEAR_SECONDS) % DAY_SECONDS) / HOUR_SECONDS;
printf("%" PRIu64 " h, ", tmp_value);
tmp_value = (((sec % YEAR_SECONDS) % DAY_SECONDS) % HOUR_SECONDS) / MINUTE_SECONDS;
printf("%" PRIu64 " min, ", tmp_value);