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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
|
/********************************************************************************/
/* */
/* TPM2 Measurement Log Common Routines */
/* Written by Ken Goldman */
/* IBM Thomas J. Watson Research Center */
/* */
/* (c) Copyright IBM Corporation 2016 - 2020. */
/* */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without */
/* modification, are permitted provided that the following conditions are */
/* met: */
/* */
/* Redistributions of source code must retain the above copyright notice, */
/* this list of conditions and the following disclaimer. */
/* */
/* 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. */
/* */
/* Neither the names of the IBM Corporation nor the names of its */
/* contributors may be used to endorse or promote products derived from */
/* this software without specific prior written permission. */
/* */
/* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ibmtss/tssprint.h>
#include <ibmtss/Unmarshal_fp.h>
#include <ibmtss/tssmarshal.h>
#include <ibmtss/tsserror.h>
#ifndef TPM_TSS_NOCRYPTO
#include <ibmtss/tsscryptoh.h>
#include <ibmtss/tsscrypto.h>
#endif /* TPM_TSS_NOCRYPTO */
#include <ibmtss/tssutils.h>
#include "eventlib.h"
#ifndef TPM_TSS_NOFILE
#ifdef TPM_TPM20
static uint16_t Uint16_Convert(uint16_t in);
#endif
static uint32_t Uint32_Convert(uint32_t in);
#endif /* TPM_TSS_NOFILE */
static TPM_RC UINT16LE_Unmarshal(uint16_t *target, BYTE **buffer, uint32_t *size);
static TPM_RC UINT32LE_Unmarshal(uint32_t *target, BYTE **buffer, uint32_t *size);
static void TSS_EVENT_EventType_Trace(uint32_t eventType);
static TPM_RC TSS_SpecIdEventAlgorithmSize_Unmarshal(TCG_EfiSpecIdEventAlgorithmSize *algSize,
uint8_t **buffer,
uint32_t *size);
static void TSS_SpecIdEventAlgorithmSize_Trace(TCG_EfiSpecIdEventAlgorithmSize *algSize);
static TPM_RC TSS_TPML_DIGEST_VALUES_LE_Unmarshalu(TPML_DIGEST_VALUES *target,
BYTE **buffer,
uint32_t *size);
static TPM_RC TSS_TPMT_HA_LE_Unmarshalu(TPMT_HA *target, BYTE **buffer,
uint32_t *size, BOOL allowNull);
static TPM_RC TSS_TPMI_ALG_HASH_LE_Unmarshalu(TPMI_ALG_HASH *target,
BYTE **buffer, uint32_t *size,
BOOL allowNull);
static TPM_RC TSS_TPM_ALG_ID_LE_Unmarshalu(TPM_ALG_ID *target,
BYTE **buffer, uint32_t *size);
static TPM_RC TSS_TPMT_HA_LE_Marshalu(const TPMT_HA *source, uint16_t *written,
BYTE **buffer, uint32_t *size);
static TPM_RC TSS_TPML_DIGEST_VALUES_LE_Marshalu(const TPML_DIGEST_VALUES *source,
uint16_t *written, BYTE **buffer,
uint32_t *size);
/* TSS_EVENT_Line_Read() reads a TPM 1.2 SHA-1 event line from a binary file inFile.
*/
#ifndef TPM_TSS_NOFILE
int TSS_EVENT_Line_Read(TCG_PCR_EVENT *event,
int *endOfFile,
FILE *inFile)
{
int rc = 0;
size_t readSize;
*endOfFile = FALSE;
/* read the PCR index */
if (rc == 0) {
readSize = fread(&(event->pcrIndex),
sizeof(((TCG_PCR_EVENT *)NULL)->pcrIndex), 1, inFile);
if (readSize != 1) {
if (feof(inFile)) {
*endOfFile = TRUE;
}
else {
printf("TSS_EVENT_Line_Read: Error, could not read pcrIndex, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->pcrIndex = Uint32_Convert(event->pcrIndex);
}
/* read the event type */
if (!*endOfFile && (rc == 0)) {
readSize = fread(&(event->eventType),
sizeof(((TCG_PCR_EVENT *)NULL)->eventType), 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT_Line_Read: Error, could not read eventType, returned %lu\n",
(unsigned long) readSize);
rc = TSS_RC_BAD_PROPERTY_VALUE;
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->eventType = Uint32_Convert(event->eventType);
}
/* read the digest */
if (!*endOfFile && (rc == 0)) {
readSize = fread(&(event->digest),
sizeof(((TCG_PCR_EVENT *)NULL)->digest), 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT_Line_Read: Error, could not read digest, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* read the event data size */
if (!*endOfFile && (rc == 0)) {
readSize = fread(&(event->eventDataSize),
sizeof(((TCG_PCR_EVENT *)NULL)->eventDataSize), 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT_Line_Read: Error, could not read event data size, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->eventDataSize = Uint32_Convert(event->eventDataSize);
}
/* bounds check the event data length */
if (!*endOfFile && (rc == 0)) {
if (event->eventDataSize > sizeof(((TCG_PCR_EVENT *)NULL)->event)) {
printf("TSS_EVENT_Line_Read: Error, event data length too big: %u\n",
event->eventDataSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* read the event */
if (!*endOfFile && (rc == 0)) {
memset(event->event , 0, sizeof(((TCG_PCR_EVENT *)NULL)->event));
readSize = fread(&(event->event),
event->eventDataSize, 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT_Line_Read: Error, could not read event, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
return rc;
}
#endif /* TPM_TSS_NOFILE */
/* TSS_EVENT_Line_Marshal() marshals a TCG_PCR_EVENT structure */
TPM_RC TSS_EVENT_Line_Marshal(TCG_PCR_EVENT *source,
uint16_t *written, uint8_t **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = TSS_UINT32_Marshalu(&source->pcrIndex, written, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Marshalu(&source->eventType, written, buffer, size);
}
if (rc == 0) {
rc = TSS_Array_Marshalu(source->digest, SHA1_DIGEST_SIZE, written, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Marshalu(&source->eventDataSize, written, buffer, size);
}
if (rc == 0) {
rc = TSS_Array_Marshalu(source->event, source->eventDataSize, written, buffer, size);
}
return rc;
}
/* TSS_EVENT_Line_Unmarshal() unmarshals a TCG_PCR_EVENT2 structure
*/
TPM_RC TSS_EVENT_Line_Unmarshal(TCG_PCR_EVENT *target, BYTE **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = TSS_UINT32_Unmarshalu(&target->pcrIndex, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Unmarshalu(&target->eventType, buffer, size);
}
if (rc == 0) {
rc = TSS_Array_Unmarshalu((uint8_t *)target->digest, SHA1_DIGEST_SIZE, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Unmarshalu(&target->eventDataSize, buffer, size);
}
if (rc == 0) {
if (target->eventDataSize > sizeof(target->event)) {
rc = TPM_RC_SIZE;
}
}
if (rc == 0) {
rc = TSS_Array_Unmarshalu((uint8_t *)target->event, target->eventDataSize, buffer, size);
}
return rc;
}
/*
* TSS_EVENT_Line_LE_Unmarshal() Unmarshal LE buffer into a target TCG_PCR_EVENT
*/
TPM_RC TSS_EVENT_Line_LE_Unmarshal(TCG_PCR_EVENT *target, BYTE **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = UINT32LE_Unmarshal(&target->pcrIndex, buffer, size);
}
if (rc == 0) {
rc = UINT32LE_Unmarshal(&target->eventType, buffer, size);
}
if (rc == 0) {
rc = TSS_Array_Unmarshalu((uint8_t *)target->digest, SHA1_DIGEST_SIZE, buffer, size);
}
if (rc == 0) {
rc = UINT32LE_Unmarshal(&target->eventDataSize, buffer, size);
}
if (rc == 0) {
if (target->eventDataSize > sizeof(target->event)) {
rc = TPM_RC_SIZE;
}
}
if (rc == 0) {
rc = TSS_Array_Unmarshalu((uint8_t *)target->event, target->eventDataSize, buffer, size);
}
return rc;
}
#ifndef TPM_TSS_NOCRYPTO
/* TSS_EVENT_PCR_Extend() extends PCR digest with the digest from the TCG_PCR_EVENT event log
entry.
*/
TPM_RC TSS_EVENT_PCR_Extend(TPMT_HA pcrs[IMPLEMENTATION_PCR],
TCG_PCR_EVENT *event)
{
TPM_RC rc = 0;
/* validate PCR number */
if (rc == 0) {
if (event->pcrIndex >= IMPLEMENTATION_PCR) {
printf("ERROR: TSS_EVENT_PCR_Extend: PCR number %u out of range\n", event->pcrIndex);
rc = TSS_RC_BAD_PROPERTY_VALUE;
}
}
/* process each event hash algorithm */
if (rc == 0) {
pcrs[event->pcrIndex].hashAlg = TPM_ALG_SHA1; /* should already be initialized */
if (rc == 0) {
rc = TSS_Hash_Generate(&pcrs[event->pcrIndex],
SHA1_DIGEST_SIZE, (uint8_t *)&pcrs[event->pcrIndex].digest,
SHA1_DIGEST_SIZE, &event->digest,
0, NULL);
}
}
return rc;
}
#endif /* TPM_TSS_NOCRYPTO */
void TSS_EVENT_Line_Trace(TCG_PCR_EVENT *event)
{
printf("TSS_EVENT_Line_Trace: PCR index %u\n", event->pcrIndex);
TSS_EVENT_EventType_Trace(event->eventType);
TSS_PrintAll("TSS_EVENT_Line_Trace: PCR",
event->digest, sizeof(((TCG_PCR_EVENT *)NULL)->digest));
TSS_PrintAll("TSS_EVENT_Line_Trace: event",
event->event, event->eventDataSize);
if (event->eventType == EV_IPL) { /* this event appears to be printable strings */
printf(" %.*s\n", event->eventDataSize, event->event);
}
return;
}
/* TSS_SpecIdEvent_Unmarshal() unmarshals the TCG_EfiSpecIDEvent structure.
The size and buffer are not moved, since this is the only structure in the event.
*/
TPM_RC TSS_SpecIdEvent_Unmarshal(TCG_EfiSpecIDEvent *specIdEvent,
uint32_t eventSize,
uint8_t *event)
{
TPM_RC rc = 0;
uint32_t size = eventSize; /* copy, because size and buffer are not moved */
uint8_t *buffer = event;
uint32_t i;
if (rc == 0) {
rc = TSS_Array_Unmarshalu(specIdEvent->signature, sizeof(specIdEvent->signature),
&buffer, &size);
}
if (rc == 0) {
rc = UINT32LE_Unmarshal(&(specIdEvent->platformClass), &buffer, &size);
}
if (rc == 0) {
rc = TSS_UINT8_Unmarshalu(&(specIdEvent->specVersionMinor), &buffer, &size);
}
if (rc == 0) {
rc = TSS_UINT8_Unmarshalu(&(specIdEvent->specVersionMajor), &buffer, &size);
}
if (rc == 0) {
rc = TSS_UINT8_Unmarshalu(&(specIdEvent->specErrata), &buffer, &size);
}
if (rc == 0) {
rc = TSS_UINT8_Unmarshalu(&(specIdEvent->uintnSize), &buffer, &size);
}
if (rc == 0) {
rc = UINT32LE_Unmarshal(&(specIdEvent->numberOfAlgorithms), &buffer, &size);
}
for (i = 0 ; (rc == 0) && (i < specIdEvent->numberOfAlgorithms) ; i++) {
rc = TSS_SpecIdEventAlgorithmSize_Unmarshal(&(specIdEvent->digestSizes[i]),
&buffer, &size);
}
if (rc == 0) {
rc = TSS_UINT8_Unmarshalu(&(specIdEvent->vendorInfoSize), &buffer, &size);
}
#if 0 /* NOTE: Can never fail because vendorInfoSize is uint8_t and vendorInfo is 0xff bytes */
if (rc == 0) {
if (specIdEvent->vendorInfoSize > sizeof(specIdEvent->vendorInfo)) {
rc = TPM_RC_SIZE;
}
}
#endif
if (rc == 0) {
rc = TSS_Array_Unmarshalu(specIdEvent->vendorInfo, specIdEvent->vendorInfoSize,
&buffer, &size);
}
return rc;
}
/* TSS_SpecIdEventAlgorithmSize_Unmarshal() unmarshals the TCG_EfiSpecIdEventAlgorithmSize
structure */
static TPM_RC TSS_SpecIdEventAlgorithmSize_Unmarshal(TCG_EfiSpecIdEventAlgorithmSize *algSize,
uint8_t **buffer,
uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = UINT16LE_Unmarshal(&(algSize->algorithmId), buffer, size);
}
if (rc == 0) {
rc = UINT16LE_Unmarshal(&(algSize->digestSize), buffer, size);
}
if (rc == 0) {
uint16_t mappedDigestSize = TSS_GetDigestSize(algSize->algorithmId);
if (mappedDigestSize != 0) {
if (mappedDigestSize != algSize->digestSize) {
printf("TSS_SpecIdEventAlgorithmSize_Unmarshal: "
"Error, inconsistent digest size, algorithm %04x size %u\n",
algSize->algorithmId, algSize->digestSize);
rc = TSS_RC_BAD_PROPERTY_VALUE;
}
}
}
return rc;
}
void TSS_SpecIdEvent_Trace(TCG_EfiSpecIDEvent *specIdEvent)
{
uint32_t i;
/* normal case */
if (specIdEvent->signature[15] == '\0') {
printf("TSS_SpecIdEvent_Trace: signature: %s\n", specIdEvent->signature);
}
/* error case */
else {
TSS_PrintAll("TSS_SpecIdEvent_Trace: signature",
specIdEvent->signature, sizeof(specIdEvent->signature));
}
printf("TSS_SpecIdEvent_Trace: platformClass %08x\n", specIdEvent->platformClass);
printf("TSS_SpecIdEvent_Trace: specVersionMinor %02x\n", specIdEvent->specVersionMinor);
printf("TSS_SpecIdEvent_Trace: specVersionMajor %02x\n", specIdEvent->specVersionMajor);
printf("TSS_SpecIdEvent_Trace: specErrata %02x\n", specIdEvent->specErrata);
printf("TSS_SpecIdEvent_Trace: uintnSize %02x\n", specIdEvent->uintnSize);
printf("TSS_SpecIdEvent_Trace: numberOfAlgorithms %u\n", specIdEvent->numberOfAlgorithms);
for (i = 0 ; (i < specIdEvent->numberOfAlgorithms) ; i++) {
TSS_SpecIdEventAlgorithmSize_Trace(&(specIdEvent->digestSizes[i]));
}
/* try for a printable string */
if (specIdEvent->vendorInfo[specIdEvent->vendorInfoSize-1] == '\0') {
printf("TSS_SpecIdEvent_Trace: vendorInfo: %s\n", specIdEvent->vendorInfo);
}
/* if not, trace the bytes */
else {
TSS_PrintAll("TSS_SpecIdEvent_Trace: vendorInfo",
specIdEvent->vendorInfo, specIdEvent->vendorInfoSize);
}
return;
}
static void TSS_SpecIdEventAlgorithmSize_Trace(TCG_EfiSpecIdEventAlgorithmSize *algSize)
{
printf("TSS_SpecIdEventAlgorithmSize_Trace: algorithmId %04x\n", algSize->algorithmId);
printf("TSS_SpecIdEventAlgorithmSize_Trace: digestSize %u\n", algSize->digestSize);
return;
}
#ifdef TPM_TPM20
#ifndef TPM_TSS_NOFILE
/* TSS_EVENT2_Line_Read() reads a TPM2 event line from a binary file inFile.
*/
int TSS_EVENT2_Line_Read(TCG_PCR_EVENT2 *event,
int *endOfFile,
FILE *inFile)
{
int rc = 0;
size_t readSize;
uint32_t maxCount;
uint32_t count;
*endOfFile = FALSE;
/* read the PCR index */
if (rc == 0) {
readSize = fread(&(event->pcrIndex),
sizeof(((TCG_PCR_EVENT2 *)NULL)->pcrIndex), 1, inFile);
if (readSize != 1) {
if (feof(inFile)) {
*endOfFile = TRUE;
}
else {
printf("TSS_EVENT2_Line_Read: Error, could not read pcrIndex, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->pcrIndex = Uint32_Convert(event->pcrIndex);
}
/* read the event type */
if (!*endOfFile && (rc == 0)) {
readSize = fread(&(event->eventType),
sizeof(((TCG_PCR_EVENT2 *)NULL)->eventType), 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT2_Line_Read: Error, could not read eventType, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->eventType = Uint32_Convert(event->eventType);
}
/* read the TPML_DIGEST_VALUES count */
if (!*endOfFile && (rc == 0)) {
maxCount = sizeof((TPML_DIGEST_VALUES *)NULL)->digests / sizeof(TPMT_HA);
readSize = fread(&(event->digests.count),
sizeof(((TPML_DIGEST_VALUES *)NULL)->count), 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT2_Line_Read: Error, could not read digest count, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->digests.count = Uint32_Convert(event->digests.count);
}
/* range check the digest count */
if (!*endOfFile && (rc == 0)) {
if (event->digests.count > maxCount) {
printf("TSS_EVENT2_Line_Read: Error, digest count %u is greater than structure %u\n",
event->digests.count, maxCount);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
else if (event->digests.count == 0) {
printf("TSS_EVENT2_Line_Read: Error, digest count is zero\n");
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* read all the TPMT_HA, loop through all the digest algorithms */
for (count = 0 ; !*endOfFile && (count < event->digests.count) ; count++) {
uint16_t digestSize;
/* read the digest algorithm */
if (rc == 0) {
readSize = fread(&(event->digests.digests[count].hashAlg),
sizeof((TPMT_HA *)NULL)->hashAlg, 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT2_Line_Read: "
"Error, could not read digest algorithm, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* do the endian conversion of the hash algorithm from stream to uint16_t */
if (rc == 0) {
event->digests.digests[count].hashAlg =
Uint16_Convert(event->digests.digests[count].hashAlg);
}
/* map from the digest algorithm to the digest length */
if (rc == 0) {
digestSize = TSS_GetDigestSize(event->digests.digests[count].hashAlg);
if (digestSize == 0) {
printf("TSS_EVENT2_Line_Read: Error, unknown digest algorithm %04x*\n",
event->digests.digests[count].hashAlg);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* read the digest */
if (rc == 0) {
readSize = fread((uint8_t *)&(event->digests.digests[count].digest),
digestSize, 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT2_Line_Read: Error, could not read digest, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
}
/* read the event size */
if (!*endOfFile && (rc == 0)) {
readSize = fread(&(event->eventSize),
sizeof(((TCG_PCR_EVENT2 *)NULL)->eventSize), 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT2_Line_Read: Error, could not read event size, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* do the endian conversion from stream to uint32_t */
if (!*endOfFile && (rc == 0)) {
event->eventSize = Uint32_Convert(event->eventSize);
}
/* bounds check the event size */
if (!*endOfFile && (rc == 0)) {
if (event->eventSize > sizeof(((TCG_PCR_EVENT2 *)NULL)->event)) {
printf("TSS_EVENT2_Line_Read: Error, event size too big: %u\n",
event->eventSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
/* read the event */
if (!*endOfFile && (event->eventSize > 0) && (rc == 0)) {
memset(event->event , 0, sizeof(((TCG_PCR_EVENT2 *)NULL)->event));
readSize = fread(&(event->event),
event->eventSize, 1, inFile);
if (readSize != 1) {
printf("TSS_EVENT2_Line_Read: Error, could not read event, returned %lu\n",
(unsigned long)readSize);
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
return rc;
}
#endif /* TPM_TSS_NOFILE */
/* TSS_EVENT2_Line_Marshal() marshals a TCG_PCR_EVENT2 structure */
TPM_RC TSS_EVENT2_Line_Marshal(TCG_PCR_EVENT2 *source,
uint16_t *written, uint8_t **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = TSS_UINT32_Marshalu(&source->pcrIndex, written, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Marshalu(&source->eventType, written, buffer, size);
}
if (rc == 0) {
rc = TSS_TPML_DIGEST_VALUES_Marshalu(&source->digests, written, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Marshalu(&source->eventSize, written, buffer, size);
}
if (rc == 0) {
rc = TSS_Array_Marshalu((uint8_t *)source->event, source->eventSize, written, buffer, size);
}
return rc;
}
/*
* TSS_EVENT2_Line_LE_Marshal() Marshals a TSS_EVENT2 structure from HBO into LE
* and saves to buffer.
*/
TPM_RC TSS_EVENT2_Line_LE_Marshal(TCG_PCR_EVENT2 *source, uint16_t *written,
uint8_t **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = TSS_UINT32LE_Marshal(&source->pcrIndex, written, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32LE_Marshal(&source->eventType, written, buffer, size);
}
if (rc == 0) {
rc = TSS_TPML_DIGEST_VALUES_LE_Marshalu(&source->digests, written, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32LE_Marshal(&source->eventSize, written, buffer, size);
}
if (rc == 0) {
rc = TSS_Array_Marshalu((uint8_t *)source->event, source->eventSize, written, buffer, size);
}
return rc;
}
/* TSS_EVENT2_Line_Unmarshal() unmarshals a TCG_PCR_EVENT2 structure */
TPM_RC TSS_EVENT2_Line_Unmarshal(TCG_PCR_EVENT2 *target, BYTE **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = TSS_UINT32_Unmarshalu(&target->pcrIndex, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Unmarshalu(&target->eventType, buffer, size);
}
if (rc == 0) {
rc = TSS_TPML_DIGEST_VALUES_Unmarshalu(&target->digests, buffer, size);
}
if (rc == 0) {
rc = TSS_UINT32_Unmarshalu(&target->eventSize, buffer, size);
}
if (rc == 0) {
if (target->eventSize > sizeof(target->event)) {
rc = TPM_RC_SIZE;
}
}
if (rc == 0) {
rc = TSS_Array_Unmarshalu((uint8_t *)target->event, target->eventSize, buffer, size);
}
return rc;
}
/*
* TSS_EVENT2_Line_LE_Unmarshal() Unmarshals an LE eventlog buffer and save to
* the target TCG_PCR_EVENT2
*/
TPM_RC TSS_EVENT2_Line_LE_Unmarshal(TCG_PCR_EVENT2 *target, BYTE **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = UINT32LE_Unmarshal(&target->pcrIndex, buffer, size);
}
if (rc == 0) {
rc = UINT32LE_Unmarshal(&target->eventType, buffer, size);
}
if (rc == 0) {
rc = TSS_TPML_DIGEST_VALUES_LE_Unmarshalu(&target->digests, buffer, size);
}
if (rc == 0) {
rc = UINT32LE_Unmarshal(&target->eventSize, buffer, size);
}
if (rc == 0) {
if (target->eventSize > sizeof(target->event)) {
rc = TPM_RC_SIZE;
}
}
if (rc == 0) {
rc = TSS_Array_Unmarshalu((uint8_t *)target->event, target->eventSize, buffer, size);
}
return rc;
}
#ifndef TPM_TSS_NOCRYPTO
/* TSS_EVENT2_PCR_Extend() extends PCR digests with the digest from the TCG_PCR_EVENT2 event log
entry.
*/
TPM_RC TSS_EVENT2_PCR_Extend(TPMT_HA pcrs[HASH_COUNT][IMPLEMENTATION_PCR],
TCG_PCR_EVENT2 *event2)
{
TPM_RC rc = 0;
uint32_t i; /* iterator though hash algorithms */
uint32_t bankNum = 0; /* iterator though PCR hash banks */
/* validate PCR number */
if (rc == 0) {
if (event2->pcrIndex >= IMPLEMENTATION_PCR) {
printf("ERROR: TSS_EVENT2_PCR_Extend: PCR number %u out of range\n", event2->pcrIndex);
rc = 1;
}
}
/* validate event count */
if (rc == 0) {
uint32_t maxCount = sizeof(((TPML_DIGEST_VALUES *)NULL)->digests) / sizeof(TPMT_HA);
if (event2->digests.count > maxCount) {
printf("ERROR: TSS_EVENT2_PCR_Extend: PCR count %u out of range, max %u\n",
event2->digests.count, maxCount);
rc = 1;
}
}
/* process each event hash algorithm */
for (i = 0; (rc == 0) && (i < event2->digests.count) ; i++) {
/* find the matching PCR bank */
for (bankNum = 0 ; (rc == 0) && (bankNum < event2->digests.count) ; bankNum++) {
if (pcrs[bankNum][0].hashAlg == event2->digests.digests[i].hashAlg) {
uint16_t digestSize;
if (rc == 0) {
digestSize = TSS_GetDigestSize(event2->digests.digests[i].hashAlg);
if (digestSize == 0) {
printf("ERROR: TSS_EVENT2_PCR_Extend: hash algorithm %04hx unknown\n",
event2->digests.digests[i].hashAlg);
rc = 1;
}
}
if (rc == 0) {
rc = TSS_Hash_Generate(&pcrs[bankNum][event2->pcrIndex],
digestSize,
(uint8_t *)&pcrs[bankNum][event2->pcrIndex].digest,
digestSize,
&event2->digests.digests[i].digest,
0, NULL);
}
}
}
}
return rc;
}
#endif /* TPM_TSS_NOCRYPTO */
#endif /* TPM_TPM20 */
#ifndef TPM_TSS_NOFILE
#ifdef TPM_TPM20
/* Uint16_Convert() converts a little endian uint16_t (from an input stream) to host byte order
*/
static uint16_t Uint16_Convert(uint16_t in)
{
uint16_t out = 0;
unsigned char *inb = (unsigned char *)∈
/* little endian input */
out = (inb[0] << 0) |
(inb[1] << 8);
return out;
}
#endif
/* Uint32_Convert() converts a little endian uint32_t (from an input stream) to host byte order
*/
static uint32_t Uint32_Convert(uint32_t in)
{
uint32_t out = 0;
unsigned char *inb = (unsigned char *)∈
/* little endian input */
out = (inb[0] << 0) |
(inb[1] << 8) |
(inb[2] << 16) |
(inb[3] << 24);
return out;
}
#endif /* TPM_TSS_NOFILE */
/* UINT16LE_Unmarshal() unmarshals a little endian 2-byte array from buffer into a HBO uint16_t */
static TPM_RC
UINT16LE_Unmarshal(uint16_t *target, BYTE **buffer, uint32_t *size)
{
if (*size < sizeof(uint16_t)) {
return TPM_RC_INSUFFICIENT;
}
*target = ((uint16_t)((*buffer)[0]) << 0) |
((uint16_t)((*buffer)[1]) << 8);
*buffer += sizeof(uint16_t);
*size -= sizeof(uint16_t);
return TPM_RC_SUCCESS;
}
/* UINT32LE_Unmarshal() unmarshals a little endian 4-byte array from buffer into a HBO uint32_t */
static TPM_RC
UINT32LE_Unmarshal(uint32_t *target, BYTE **buffer, uint32_t *size)
{
if (*size < sizeof(uint32_t)) {
return TPM_RC_INSUFFICIENT;
}
*target = ((uint32_t)((*buffer)[0]) << 0) |
((uint32_t)((*buffer)[1]) << 8) |
((uint32_t)((*buffer)[2]) << 16) |
((uint32_t)((*buffer)[3]) << 24);
*buffer += sizeof(uint32_t);
*size -= sizeof(uint32_t);
return TPM_RC_SUCCESS;
}
void TSS_EVENT2_Line_Trace(TCG_PCR_EVENT2 *event)
{
uint32_t count;
uint16_t digestSize;
printf("TSS_EVENT2_Line_Trace: PCR index %u\n", event->pcrIndex);
TSS_EVENT_EventType_Trace(event->eventType);
printf("TSS_EVENT2_Line_Trace: digest count %u\n", event->digests.count);
for (count = 0 ; count < event->digests.count ; count++) {
printf("TSS_EVENT2_Line_Trace: digest %u algorithm %04x\n",
count, event->digests.digests[count].hashAlg);
digestSize = TSS_GetDigestSize(event->digests.digests[count].hashAlg);
TSS_PrintAll("TSS_EVENT2_Line_Trace: PCR",
(uint8_t *)&event->digests.digests[count].digest, digestSize);
}
TSS_PrintAll("TSS_EVENT2_Line_Trace: event",
event->event, event->eventSize);
return;
}
/* tables to map eventType to text */
typedef struct {
uint32_t eventType;
const char *text;
} EVENT_TYPE_TABLE;
const EVENT_TYPE_TABLE eventTypeTable [] = {
{EV_PREBOOT_CERT, "EV_PREBOOT_CERT"},
{EV_POST_CODE, "EV_POST_CODE"},
{EV_UNUSED, "EV_UNUSED"},
{EV_NO_ACTION, "EV_NO_ACTION"},
{EV_SEPARATOR, "EV_SEPARATOR"},
{EV_ACTION, "EV_ACTION"},
{EV_EVENT_TAG, "EV_EVENT_TAG"},
{EV_S_CRTM_CONTENTS, "EV_S_CRTM_CONTENTS"},
{EV_S_CRTM_VERSION, "EV_S_CRTM_VERSION"},
{EV_CPU_MICROCODE, "EV_CPU_MICROCODE"},
{EV_PLATFORM_CONFIG_FLAGS, "EV_PLATFORM_CONFIG_FLAGS"},
{EV_TABLE_OF_DEVICES, "EV_TABLE_OF_DEVICES"},
{EV_COMPACT_HASH, "EV_COMPACT_HASH"},
{EV_IPL, "EV_IPL"},
{EV_IPL_PARTITION_DATA, "EV_IPL_PARTITION_DATA"},
{EV_NONHOST_CODE, "EV_NONHOST_CODE"},
{EV_NONHOST_CONFIG, "EV_NONHOST_CONFIG"},
{EV_NONHOST_INFO, "EV_NONHOST_INFO"},
{EV_OMIT_BOOT_DEVICE_EVENTS, "EV_OMIT_BOOT_DEVICE_EVENTS"},
{EV_EFI_EVENT_BASE, "EV_EFI_EVENT_BASE"},
{EV_EFI_VARIABLE_DRIVER_CONFIG, "EV_EFI_VARIABLE_DRIVER_CONFIG"},
{EV_EFI_VARIABLE_BOOT, "EV_EFI_VARIABLE_BOOT"},
{EV_EFI_BOOT_SERVICES_APPLICATION, "EV_EFI_BOOT_SERVICES_APPLICATION"},
{EV_EFI_BOOT_SERVICES_DRIVER, "EV_EFI_BOOT_SERVICES_DRIVER"},
{EV_EFI_RUNTIME_SERVICES_DRIVER, "EV_EFI_RUNTIME_SERVICES_DRIVER"},
{EV_EFI_GPT_EVENT, "EV_EFI_GPT_EVENT"},
{EV_EFI_ACTION, "EV_EFI_ACTION"},
{EV_EFI_PLATFORM_FIRMWARE_BLOB, "EV_EFI_PLATFORM_FIRMWARE_BLOB"},
{EV_EFI_HANDOFF_TABLES, "EV_EFI_HANDOFF_TABLES"},
{EV_EFI_HCRTM_EVENT, "EV_EFI_HCRTM_EVENT"},
{EV_EFI_VARIABLE_AUTHORITY, "EV_EFI_VARIABLE_AUTHORITY"}
};
static void TSS_EVENT_EventType_Trace(uint32_t eventType)
{
size_t i;
for (i = 0 ; i < sizeof(eventTypeTable) / sizeof(EVENT_TYPE_TABLE) ; i++) {
if (eventTypeTable[i].eventType == eventType) {
printf("TSS_EVENT_EventType_Trace: %08x %s\n",
eventTypeTable[i].eventType, eventTypeTable[i].text);
return;
}
}
printf("TSS_EVENT_EventType_Trace: %08x Unknown\n", eventType);
return;
}
const char *TSS_EVENT_EventTypeToString(uint32_t eventType)
{
const char *crc = NULL;
size_t i;
for (i = 0 ; i < sizeof(eventTypeTable) / sizeof(EVENT_TYPE_TABLE) ; i++) {
if (eventTypeTable[i].eventType == eventType) {
crc = eventTypeTable[i].text;
}
}
if (crc == NULL) {
crc = "Unknown event type";
}
return crc;
}
/*
* TSS_TPML_DIGEST_VALUES_LE_Unmarshalu() Unmarshals TPML_DIGEST_VALUES struct
* from a LE buffer into HBO data structure. This is similar to
* TSS_TPML_DIGEST_VALUES_Unmarshalu but it unrmarshals TPML_DIGEST_VALUES's
* count and the digests array members from LE instead of HBO.
*/
static TPM_RC
TSS_TPML_DIGEST_VALUES_LE_Unmarshalu(TPML_DIGEST_VALUES *target, BYTE **buffer, uint32_t *size)
{
TPM_RC rc = TPM_RC_SUCCESS;
uint32_t i;
if (rc == TPM_RC_SUCCESS) {
rc = UINT32LE_Unmarshal(&target->count, buffer, size);
}
if (rc == TPM_RC_SUCCESS) {
if (target->count > HASH_COUNT) {
rc = TPM_RC_SIZE;
}
}
for (i = 0 ; (rc == TPM_RC_SUCCESS) && (i < target->count) ; i++) {
rc = TSS_TPMT_HA_LE_Unmarshalu(&target->digests[i], buffer, size, NO);
}
return rc;
}
/*
* TSS_TPMT_HA_LE_Unmarshalu() Unmarshals a TPMT_HA data from LE to HBO. This is
* similar to TSS_TPMT_HA_Unmarshalu but differs specificaly for unmarshalling
* hashAlg member from LE instead of from HBO.
*/
static TPM_RC
TSS_TPMT_HA_LE_Unmarshalu(TPMT_HA *target, BYTE **buffer, uint32_t *size, BOOL allowNull)
{
TPM_RC rc = TPM_RC_SUCCESS;
if (rc == TPM_RC_SUCCESS) {
rc = TSS_TPMI_ALG_HASH_LE_Unmarshalu(&target->hashAlg, buffer, size, allowNull);
}
if (rc == TPM_RC_SUCCESS) {
rc = TSS_TPMU_HA_Unmarshalu(&target->digest, buffer, size, target->hashAlg);
}
return rc;
}
/*
* TSS_TPMI_ALG_HASH_LE_Unmarshalu() Unmarshals TPMI_ALG_HASH from a LE buffer
* into HBO data structure. This is similar to TSS_TPMI_ALG_HASH_Unmarshalu but
* unmarshals TPMI_ALG_HASH from LE instead of HBO.
*/
static TPM_RC
TSS_TPMI_ALG_HASH_LE_Unmarshalu(TPMI_ALG_HASH *target, BYTE **buffer, uint32_t *size, BOOL allowNull)
{
TPM_RC rc = TPM_RC_SUCCESS;
allowNull = allowNull;
if (rc == TPM_RC_SUCCESS) {
rc = TSS_TPM_ALG_ID_LE_Unmarshalu(target, buffer, size);
}
return rc;
}
/*
* TSS_TPM_ALG_ID_LE_Unmarshalu() Unrmarshals TPM_ALG_ID from LE buffer. This is
* simlar to TSS_TPM_ALG_ID_Unmarshalu but unmarshals from LE instead of HBO.
*/
static TPM_RC
TSS_TPM_ALG_ID_LE_Unmarshalu(TPM_ALG_ID *target, BYTE **buffer,
uint32_t *size)
{
TPM_RC rc = TPM_RC_SUCCESS;
if (rc == TPM_RC_SUCCESS) {
rc = UINT16LE_Unmarshal(target, buffer, size);
}
return rc;
}
/* TSS_TPML_DIGEST_VALUES_LE_Marshalu() Similar to TSS_TPML_DIGEST_VALUES_Marshalu
* for TSS EVENT2 this marshals count to buffer in LE endianess.
*/
static TPM_RC
TSS_TPML_DIGEST_VALUES_LE_Marshalu(const TPML_DIGEST_VALUES *source,
uint16_t *written, BYTE **buffer,
uint32_t *size)
{
TPM_RC rc = 0;
uint32_t i;
if (rc == 0) {
rc = TSS_UINT32LE_Marshal(&source->count, written, buffer, size);
}
for (i = 0 ; i < source->count ; i++) {
if (rc == 0) {
rc = TSS_TPMT_HA_LE_Marshalu(&source->digests[i], written, buffer, size);
}
}
return rc;
}
/* TSS_TPMT_HA_LE_Marshalu() Similar to TSS_TPMT_HA_Marshalu for TSS EVENT2,
* this saves hashAlg attr as little endian into buffer.
*/
static TPM_RC
TSS_TPMT_HA_LE_Marshalu(const TPMT_HA *source, uint16_t *written,
BYTE **buffer, uint32_t *size)
{
TPM_RC rc = 0;
if (rc == 0) {
rc = TSS_UINT16LE_Marshalu(&source->hashAlg, written, buffer, size);
}
if (rc == 0) {
rc = TSS_TPMU_HA_Marshalu(&source->digest, written, buffer, size,
source->hashAlg);
}
return rc;
}
/*
* TSS_UINT32LE_Marshal() Marshals uint32_t from HBO into LE in the given buffer.
*/
TPM_RC
TSS_UINT32LE_Marshal(const UINT32 *source, uint16_t *written, BYTE **buffer,
uint32_t *size)
{
TPM_RC rc = 0;
if (buffer != NULL) {
if ((size == NULL) || (*size >= sizeof(uint32_t))) {
(*buffer)[0] = (BYTE)((*source >> 0) & 0xff);
(*buffer)[1] = (BYTE)((*source >> 8) & 0xff);
(*buffer)[2] = (BYTE)((*source >> 16) & 0xff);
(*buffer)[3] = (BYTE)((*source >> 24) & 0xff);
*buffer += sizeof(uint32_t);
if (size != NULL) {
*size -= sizeof(uint32_t);
}
}
else {
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
*written += sizeof(uint32_t);
return rc;
}
/*
* UINT16LE_Marshal() Marshals uint16_t from HBO into LE in the given buffer.
*/
TPM_RC
TSS_UINT16LE_Marshalu(const UINT16 *source, uint16_t *written, BYTE **buffer,
uint32_t *size)
{
TPM_RC rc = 0;
if (buffer != NULL) {
if ((size == NULL) || (*size >= sizeof(uint16_t))) {
(*buffer)[0] = (BYTE)((*source >> 0) & 0xff);
(*buffer)[1] = (BYTE)((*source >> 8) & 0xff);
*buffer += sizeof(uint16_t);
if (size != NULL) {
*size -= sizeof(uint16_t);
}
}
else {
rc = TSS_RC_INSUFFICIENT_BUFFER;
}
}
*written += sizeof(uint16_t);
return rc;
}
|