summaryrefslogtreecommitdiffstats
path: root/src/tpm12/tpm_counter.c
blob: 65a2db2adc89cfc9410ed089cb2062f3e07bf57a (plain)
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
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
/********************************************************************************/
/*										*/
/*				Counter Handler					*/
/*			     Written by Ken Goldman				*/
/*		       IBM Thomas J. Watson Research Center			*/
/*	      $Id: tpm_counter.c 4539 2011-04-04 21:44:22Z kgoldman $		*/
/*										*/
//* (c) Copyright IBM Corporation 2006, 2010.					*/
/*										*/
/* 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 <string.h>

#include "tpm_auth.h"
#include "tpm_cryptoh.h"
#include "tpm_debug.h"
#include "tpm_digest.h"
#include "tpm_error.h"
#include "tpm_io.h"
#include "tpm_permanent.h"
#include "tpm_process.h"
#include "tpm_secret.h"

#include "tpm_counter.h"

/*
  Monotonic Counter Resource Handling
*/

/* TPM_Counters_Init() initializes the monotonic counters
 */

void TPM_Counters_Init(TPM_COUNTER_VALUE *monotonicCounters)
{
    uint32_t	i;
    
    for (i = 0 ; i < TPM_MIN_COUNTERS ; i++) {
	TPM_CounterValue_Init(&(monotonicCounters[i]));
    }
    return;
}

/* TPM_Counters_Load() loads the monotonic counters

   deserialize the structure from a 'stream'
   'stream_size' is checked for sufficient data
   returns 0 or error codes
*/

TPM_RESULT TPM_Counters_Load(TPM_COUNTER_VALUE *monotonicCounters,
			     unsigned char **stream,
			     uint32_t *stream_size)
{
    TPM_RESULT	rc = 0;
    uint32_t	i;

    /* load the counters */
    for (i = 0 ; (rc == 0) && (i < TPM_MIN_COUNTERS) ; i++) {
	rc = TPM_CounterValue_Load(&(monotonicCounters[i]), stream, stream_size);
    }
    return rc;
}

TPM_RESULT TPM_Counters_Store(TPM_STORE_BUFFER *sbuffer,
			      TPM_COUNTER_VALUE *monotonicCounters)
{
    TPM_RESULT	rc = 0;
    uint32_t	i;

    /* store the counters */
    for (i = 0 ; (rc == 0) && (i < TPM_MIN_COUNTERS) ; i++)  {
	rc = TPM_CounterValue_Store(sbuffer, &(monotonicCounters[i]));
    }
    return rc;
}

/* TPM_Counters_StoreHandles() stores a count of the created counters and a list of created counter
   handles.
*/

TPM_RESULT TPM_Counters_StoreHandles(TPM_STORE_BUFFER *sbuffer,
				     TPM_COUNTER_VALUE *monotonicCounters)
{
    TPM_RESULT	rc = 0;
    uint16_t	loaded;
    uint32_t	i;

    printf(" TPM_Counters_StoreHandles:\n");
    if (rc == 0) {
	loaded = 0;
	/* count the number of loaded counters */
	for (i = 0 ; i < TPM_MIN_COUNTERS ; i++) {
	    if ((monotonicCounters[i]).valid) {
		loaded++;
	    }
	}
	/* store created handle count */
	rc = TPM_Sbuffer_Append16(sbuffer, loaded); 
    }
    for (i = 0 ; (rc == 0) && (i < TPM_MIN_COUNTERS) ; i++) {
	if ((monotonicCounters[i]).valid) {
	    /* the handle is just the index */
	    rc = TPM_Sbuffer_Append32(sbuffer, i);	/* store it */
	}
    }
    return rc;
}

/* TPM_Counters_GetSpace() returns the number of unused monotonicCounters.
 */

void TPM_Counters_GetSpace(uint32_t *space,
			   TPM_COUNTER_VALUE *monotonicCounters)
{
    uint32_t i;

    printf(" TPM_Counters_GetSpace:\n");
    for (*space = 0 , i = 0 ; i < TPM_MIN_COUNTERS ; i++) {
	if (!(monotonicCounters[i]).valid) {
	    (*space)++;
	}	    
    }
    return;
}

    
/* TPM_Counters_GetNewHandle() checks for space in the monotonicCounters table.

   If there is space, it returns a TPM_COUNTER_VALUE entry in 'tpm_counter_value' and its
   handle in 'countID'.	 The entry is marked 'valid'.

   Returns TPM_RESOURCES if there is no space in the sessions table.  monotonicCounters is not
   altered on error.
*/

TPM_RESULT TPM_Counters_GetNewHandle(TPM_COUNTER_VALUE **tpm_counter_value,
				     TPM_COUNT_ID *countID,
				     TPM_COUNTER_VALUE *monotonicCounters)
{
    TPM_RESULT	rc = 0;
    TPM_BOOL is_space;
    
    printf(" TPM_Counters_GetNewHandle:\n");
    for (*countID = 0, is_space = FALSE ;
	 *countID < TPM_MIN_COUNTERS ;
	 (*countID)++) {
	
	if (!(monotonicCounters[*countID]).valid) {
	    is_space = TRUE;
	    break;
	}	    
    }
    /* NOTE: According to TPMWG email, TPM_COUNT_ID can be an index */
    if (is_space) {
	printf("  TPM_Counters_GetNewHandle: Assigned handle %u\n", *countID);
	*tpm_counter_value = &(monotonicCounters[*countID]);
	(*tpm_counter_value)->valid = TRUE;			/* mark it occupied */
    }
    else {
	printf("TPM_Counters_GetNewHandle: Error, no space in monotonicCounters table\n");
	rc = TPM_RESOURCES;
    }
    return rc;
}

/* TPM_Counters_GetNextCount() searches the monotonicCounters for the maximum count, and returns
   nextCount equal to the incremented maximum count.

   The counter does not have to be valid (created).  It can be invalid (released).
*/

void TPM_Counters_GetNextCount(TPM_ACTUAL_COUNT *nextCount,
			       TPM_COUNTER_VALUE *monotonicCounters)
{
    TPM_COUNT_ID countID;
    TPM_ACTUAL_COUNT maxCount = 0;

    printf(" TPM_Counters_GetNextCount:\n");
    for (countID = 0 ; countID < TPM_MIN_COUNTERS ; countID++) {
	if (monotonicCounters[countID].counter > maxCount) {
	    maxCount = monotonicCounters[countID].counter;
	}
    }
    *nextCount = maxCount + 1;
    printf("  TPM_Counters_GetNextCount: Next count %u\n", *nextCount);
    return;
}

/* TPM_Counters_IsValidId() verifies that countID is in range and a created counter
 */

TPM_RESULT TPM_Counters_IsValidId(TPM_COUNTER_VALUE *monotonicCounters,
				  TPM_COUNT_ID countID)
{
    TPM_RESULT		rc = 0;
   
    printf(" TPM_Counters_IsValidId: countID %u\n", countID);
    /* range check */
    if (rc == 0) {
	if (countID >= TPM_MIN_COUNTERS) {
	    printf("TPM_Counters_IsValidId: Error countID %u out of range\n", countID);
	    rc = TPM_BAD_COUNTER ;
	}
    }
    /* validity (creation) check */
    if (rc == 0) {
	if (!(monotonicCounters[countID].valid)) {
	    printf("TPM_Counters_IsValidId: Error countID %u invalid\n", countID);
	    rc = TPM_BAD_COUNTER ;
	}	    
    }
    return rc;
}


/* TPM_Counters_GetCounterValue() gets the TPM_COUNTER_VALUE associated with the countID.

 */

TPM_RESULT TPM_Counters_GetCounterValue(TPM_COUNTER_VALUE **tpm_counter_value,
					TPM_COUNTER_VALUE *monotonicCounters,
					TPM_COUNT_ID countID)
{
    TPM_RESULT		rc = 0;
    
    printf(" TPM_Counters_GetCounterValue: countID %u\n", countID);
    /* valid counter check */
    if (rc == 0) {
	rc = TPM_Counters_IsValidId(monotonicCounters, countID);
    }
    if (rc == 0) {
	*tpm_counter_value = &(monotonicCounters[countID]);
    }
    return rc;
}

/* TPM_Counters_Release() iterates through all monotonicCounter's, and releases those that are
   created.

   The resource is set invalid, and the authorization data and digest are cleared.

   a. This includes invalidating all currently allocated counters. The result will be no
   currently allocated counters and the new owner will need to allocate counters. The actual
   count value will continue to increase.
*/

TPM_RESULT TPM_Counters_Release(TPM_COUNTER_VALUE *monotonicCounters)
{
    TPM_RESULT	 rc = 0;
    TPM_COUNT_ID i;
    
    printf(" TPM_Counters_Release:\n");
    for (i = 0 ; i < TPM_MIN_COUNTERS ; i++) {
	if (monotonicCounters[i].valid) {
	    /* the actual count value does not reset to zero */
	    printf(" TPM_Counters_Release: Releasing %u\n", i);
	    TPM_Secret_Init(monotonicCounters[i].authData);
	    TPM_Digest_Init(monotonicCounters[i].digest);
	    monotonicCounters[i].valid = FALSE;
	}
    }
    return rc;
}

/* TPM_Counters_GetActiveCounter() gets the active counter based on the value in TPM_STCLEAR_DATA ->
   countID */

void TPM_Counters_GetActiveCounter(TPM_COUNT_ID *activeCounter,
				   TPM_COUNT_ID countID)
{
    if (countID < TPM_MIN_COUNTERS) {
	*activeCounter = countID;
    }
    else {
	*activeCounter = TPM_COUNT_ID_NULL;
    }
}

/*
  TPM_COUNTER_VALUE
*/

/* TPM_CounterValue_Init()

   sets members to default values
   sets all pointers to NULL and sizes to 0
   always succeeds - no return code
*/

void TPM_CounterValue_Init(TPM_COUNTER_VALUE *tpm_counter_value)
{
    printf(" TPM_CounterValue_Init:\n");
    memset(tpm_counter_value->label, 0, TPM_COUNTER_LABEL_SIZE);
    tpm_counter_value->counter = 0;
    TPM_Secret_Init(tpm_counter_value->authData);
    tpm_counter_value->valid = FALSE;
    return;
}

/* TPM_CounterValue_Load()

   deserialize the structure from a 'stream'
   'stream_size' is checked for sufficient data
   returns 0 or error codes
*/

TPM_RESULT TPM_CounterValue_Load(TPM_COUNTER_VALUE *tpm_counter_value,	/* result */
				 unsigned char **stream,		/* pointer to next
									   parameter */
				 uint32_t *stream_size)			/* stream size left */
{
    TPM_RESULT	rc = 0;
    
    printf(" TPM_CounterValue_Load:\n");
    /* check tag */
    if (rc == 0) {	
	rc = TPM_CheckTag(TPM_TAG_COUNTER_VALUE, stream, stream_size);
    }
    /* load label */
    if (rc == 0) {
	rc = TPM_Loadn(tpm_counter_value->label, TPM_COUNTER_LABEL_SIZE, stream, stream_size);
    }
    /* load counter */
    if (rc == 0) {
	rc = TPM_Load32(&(tpm_counter_value->counter), stream, stream_size);
    }	
    /* load authData */
    if (rc == 0) {
	rc = TPM_Secret_Load(tpm_counter_value->authData, stream, stream_size);
    }	
    /* load valid */
    if (rc == 0) {
	rc = TPM_LoadBool(&(tpm_counter_value->valid), stream, stream_size);
    }	
    return rc;
}

/* TPM_CounterValue_Store()
   
   serialize the structure to a stream contained in 'sbuffer'
   returns 0 or error codes

   It is typically used to store the structure in the permanent data file.
*/

TPM_RESULT TPM_CounterValue_Store(TPM_STORE_BUFFER *sbuffer,
				  const TPM_COUNTER_VALUE *tpm_counter_value)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_CounterValue_Store:\n");
    /* store tag, label, counter */
    if (rc == 0) {	
	rc = TPM_CounterValue_StorePublic(sbuffer, tpm_counter_value); 
    }
    /* store authData */
    if (rc == 0) {
	rc = TPM_Secret_Store(sbuffer, tpm_counter_value->authData);
    }	
    /* store valid */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append(sbuffer, &(tpm_counter_value->valid), sizeof(TPM_BOOL));
    }	
    return rc;
}

/* TPM_CounterValue_StorePublic()
   
   serialize the structure to a stream contained in 'sbuffer'
   returns 0 or error codes

   This version only stores the public, externally visible fields: tag, label, counter.	 It is
   typically used to return outgoing parameters.
*/

TPM_RESULT TPM_CounterValue_StorePublic(TPM_STORE_BUFFER *sbuffer,
					const TPM_COUNTER_VALUE *tpm_counter_value)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_CounterValue_StorePublic:\n");
    /* store tag */
    if (rc == 0) {	
	rc = TPM_Sbuffer_Append16(sbuffer, TPM_TAG_COUNTER_VALUE); 
    }
    /* store label */
    if (rc == 0) {	
	rc = TPM_Sbuffer_Append(sbuffer, tpm_counter_value->label, TPM_COUNTER_LABEL_SIZE);
    }
    /* store counter */
    if (rc == 0) {
	rc = TPM_Sbuffer_Append32(sbuffer, tpm_counter_value->counter); 
    }
    return rc;
}

/* TPM_CounterValue_CopyPublic() copies the public, externally visible fields: tag, label, counter.
 */

void TPM_CounterValue_CopyPublic(TPM_COUNTER_VALUE *dst_tpm_counter_value,
				 TPM_COUNTER_VALUE *src_tpm_counter_value)
{
    memcpy(dst_tpm_counter_value->label, src_tpm_counter_value->label, TPM_COUNTER_LABEL_SIZE);
    dst_tpm_counter_value->counter = src_tpm_counter_value->counter;
    return;
}

/* TPM_CounterValue_Set()
   
   Sets the label, counter, and authData members from input parameters, and sets the digest from
   members.
*/

TPM_RESULT TPM_CounterValue_Set(TPM_COUNTER_VALUE *tpm_counter_value,
				TPM_COUNT_ID countID,
				BYTE *label,
				TPM_ACTUAL_COUNT counter,
				TPM_SECRET authData)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_CounterValue_Set:\n");
    tpm_counter_value->counter = counter;
    memcpy(tpm_counter_value->label, label, TPM_COUNTER_LABEL_SIZE);
    TPM_Secret_Copy(tpm_counter_value->authData, authData);
    /* create a hopefully unique digest of the object for the OSAP setup.  The cast is OK here since
       the actual value of the digest is never verified. */
    rc = TPM_SHA1(tpm_counter_value->digest,
		  sizeof(TPM_COUNT_ID), (unsigned char *)&countID, 
		  TPM_COUNTER_LABEL_SIZE, label,
		  TPM_SECRET_SIZE, authData,
		  0, NULL);
    return rc;
 
}

/* TPM_CounterValue_Release() releases a counter.

   The resource is set invalid, and the authorization data and digest are cleared.
*/

TPM_RESULT TPM_CounterValue_Release(TPM_COUNTER_VALUE *tpm_counter_value,
				    TPM_COUNT_ID countID)
{
    TPM_RESULT	rc = 0;

    printf(" TPM_CounterValue_Release: countID %u\n", countID);
    /* sanity check */
    if (rc == 0) {
	if (!tpm_counter_value->valid) {
	    printf("TPM_CounterValue_Release: Error (fatal), countID %u not valid\n", countID);
	    rc = TPM_FAIL;	/* should never occur */
	}
    }
    if (rc == 0) {
	TPM_Secret_Init(tpm_counter_value->authData);
	TPM_Digest_Init(tpm_counter_value->digest);
	tpm_counter_value->valid = FALSE;
    }
    return rc;
}

/*
  Processing Functions
*/

/* 25.1 TPM_CreateCounter rev 98

  This command creates the counter but does not select the counter. Counter creation assigns an
  AuthData value to the counter and sets the counters original start value. The original start value
  is the current internal base value plus one. Setting the new counter to the internal base avoids
  attacks on the system that are attempting to use old counter values.

  This command creates a new monotonic counter. The TPM MUST support a minimum of 4 concurrent 
  counters.
*/

TPM_RESULT TPM_Process_CreateCounter(tpm_state_t *tpm_state,
				     TPM_STORE_BUFFER *response,
				     TPM_TAG tag,
				     uint32_t paramSize,
				     TPM_COMMAND_CODE ordinal,
				     unsigned char *command,
				     TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;			/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    /* input parameters */
    TPM_ENCAUTH encAuth;		/* The encrypted auth data for the new counter */
    BYTE label[TPM_COUNTER_LABEL_SIZE]; /* Label to associate with counter */
    TPM_AUTHHANDLE authHandle;		/* The authorization session handle used for owner
					   authentication. */
    TPM_NONCE nonceOdd;			/* Nonce generated by system associated with authHandle */
    TPM_BOOL continueAuthSession = TRUE;	/* Ignored */
    TPM_AUTHDATA ownerAuth;		/* Authorization ownerAuth. */

    /* processing parameters */
    unsigned char *		inParamStart;		/* starting point of inParam's */
    unsigned char *		inParamEnd;		/* ending point of inParam's */
    TPM_DIGEST			inParamDigest;
    TPM_BOOL			auditStatus = FALSE;	/* audit the ordinal */
    TPM_BOOL			transportEncrypt = FALSE;	/* wrapped in encrypted transport
								   session */
    TPM_BOOL			authHandleValid = FALSE;
    TPM_SECRET			*hmacKey = NULL;
    TPM_AUTH_SESSION_DATA	*auth_session_data = NULL;	/* session data for authHandle */
    TPM_SECRET			a1Auth;
    TPM_ACTUAL_COUNT		nextCount;
    TPM_BOOL			writeAllNV= FALSE;	/* flag to write back NV */
    
    /* output parameters */
    uint32_t		outParamStart;		/* starting point of outParam's */
    uint32_t		outParamEnd;		/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;
    TPM_COUNT_ID	countID = 0;		/* The handle for the counter */
    TPM_COUNTER_VALUE	*counterValue = NULL;	/* The starting counter value */

    printf("TPM_Process_CreateCounter: Ordinal Entry\n");
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get authData */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Authdata_Load(encAuth, &command, &paramSize);
    }
    /* get label */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Loadn(label, TPM_COUNTER_LABEL_SIZE, &command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	TPM_PrintFour("TPM_Process_CreateCounter: label", label);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALL);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag1(tag);
    }
    /* get the 'below the line' authorization parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					ownerAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_CreateCounter: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* 1. Using the authHandle field, validate the owner's AuthData to execute the command and all
       of the incoming parameters. The authorization session MUST be OSAP or DSAP. */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_OSAP,
					      TPM_ET_OWNER,
					      ordinal,
					      NULL,
					      NULL,
					      tpm_state->tpm_permanent_data.ownerAuth);
    }
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* HMAC key */
					inParamDigest,
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					ownerAuth);		/* Authorization digest for input */
    }
    /* 2. Ignore continueAuthSession on input and set continueAuthSession to FALSE on output */
    if (returnCode == TPM_SUCCESS) {
	continueAuthSession = FALSE;
    }
    /* 3. Create a1 by decrypting encAuth according to the ADIP indicated by authHandle. */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthSessionData_Decrypt(a1Auth,
						 NULL,
						 encAuth,
						 auth_session_data,
						 NULL,
						 NULL,
						 FALSE);	/* even and odd */
    }
    /* 4. Validate that there is sufficient internal space in the TPM to create a new counter. If
       there is insufficient space the command returns an error. */
    /* a. The TPM MUST provide storage for a1, TPM_COUNTER_VALUE, countID, and any other internal
       data the TPM needs to associate with the counter */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Counters_GetNewHandle(&counterValue,		/* structure */
					       &countID,		/* index */
					       tpm_state->tpm_permanent_data.monotonicCounter);
    }
    if (returnCode == TPM_SUCCESS) {
	writeAllNV = TRUE;
	/* 5. Increment the max counter value */
	TPM_Counters_GetNextCount(&nextCount,
				  tpm_state->tpm_permanent_data.monotonicCounter);
	/* 6. Set the counter to the max counter value */
	/* 7. Set the counter label to label */
	returnCode = TPM_CounterValue_Set(counterValue,
					  countID,
					  label,
					  nextCount,
					  a1Auth);
	/* 8. Create a countID */
	/* NOTE Done in TPM_Counters_GetNewHandle() */
    }
    /* save the permanent data structure in NVRAM */
    returnCode = TPM_PermanentAll_NVStore(tpm_state,
					  writeAllNV,
					  returnCode);
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_CreateCounter: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* return the countID */
	    returnCode = TPM_Sbuffer_Append32(response, countID);
	}
	if (returnCode == TPM_SUCCESS) {
	    /* Return the TPM_COUNTER_VALUE publicly visible members */
	    returnCode = TPM_CounterValue_StorePublic(response, counterValue);
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_AuthParams_Set(response,
					    *hmacKey,		/* HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, or continueAuthSession is FALSE, terminate the session */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    return rcf;
}

/* 25.2 TPM_IncrementCounter rev 87

  This authorized command increments the indicated counter by one. Once a counter has been
  incremented then all subsequent increments must be for the same handle until a successful
  TPM_Startup(ST_CLEAR) is executed.

  The order for checking validation of the command parameters when no counter is active, keeps an
  attacker from creating a denial-of-service attack.

  This function increments the counter by 1.
  The TPM MAY implement increment throttling to avoid burn problems
*/

TPM_RESULT TPM_Process_IncrementCounter(tpm_state_t *tpm_state,
					TPM_STORE_BUFFER *response,
					TPM_TAG tag,
					uint32_t paramSize,
					TPM_COMMAND_CODE ordinal,
					unsigned char *command,
					TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;				/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    /* input parameters */
    TPM_COUNT_ID countID;		/* The handle of a valid counter */
    TPM_AUTHHANDLE authHandle;		/* The authorization session handle used for counter
					   authorization */
    TPM_NONCE nonceOdd;			/* Nonce generated by system associated with authHandle */
    TPM_BOOL continueAuthSession = TRUE;	/* The continue use flag for the authorization
						   session handle */
    TPM_AUTHDATA counterAuth;		/* The authorization session digest that authorizes the use
					   of countID. HMAC key: countID -> authData */

    /* processing parameters */
    unsigned char *		inParamStart;			/* starting point of inParam's */
    unsigned char *		inParamEnd;			/* ending point of inParam's */
    TPM_DIGEST			inParamDigest;
    TPM_BOOL			auditStatus;		/* audit the ordinal */
    TPM_BOOL			transportEncrypt;	/* wrapped in encrypted transport session */
    TPM_BOOL			authHandleValid = FALSE;
    TPM_SECRET			*hmacKey;
    TPM_AUTH_SESSION_DATA	*auth_session_data = NULL;	/* session data for authHandle */

    /* output parameters */
    uint32_t		outParamStart;	/* starting point of outParam's */
    uint32_t		outParamEnd;	/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;
    TPM_COUNTER_VALUE	*counterValue = NULL;	/* The counter value */

    printf("TPM_Process_IncrementCounter: Ordinal Entry\n");
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&countID, &command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_IncrementCounter: countID %u\n", countID);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALL);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag1(tag);
    }
    /* get the 'below the line' authorization parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					counterAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_IncrementCounter: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* The first check is that either there is no active counter and the countID has been created
       or that the countID is the active counter */
    if (returnCode == TPM_SUCCESS) {
	/* 1. If TPM_STCLEAR_DATA -> countID is NULL */
	if (tpm_state->tpm_stclear_data.countID == TPM_COUNT_ID_NULL) {
	    /* a. Validate that countID is a valid counter, return TPM_BAD_COUNTER on mismatch */
	    returnCode = TPM_Counters_IsValidId(tpm_state->tpm_permanent_data.monotonicCounter,
						countID);
	}
	/* 2. else (TPM_STCLEAR_DATA -> countID is not NULL */
	else {	
	    /* a. If TPM_STCLEAR_DATA -> countID does not equal countID */
	    if (tpm_state->tpm_stclear_data.countID != countID) {
		if (tpm_state->tpm_stclear_data.countID == TPM_COUNT_ID_ILLEGAL) {
		    printf("TPM_Process_IncrementCounter: Error, counter has been released\n");
		}
		else {
		    printf("TPM_Process_IncrementCounter: Error, %u is already active\n",
			   tpm_state->tpm_stclear_data.countID);
		}
		/* i. Return TPM_BAD_COUNTER */
		returnCode = TPM_BAD_COUNTER;
	    }
	}
    }
    /* b. Validate the command parameters using counterAuth */
    /* Get the TPM_COUNTER_VALUE associated with the countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Counters_GetCounterValue(&counterValue,
						  tpm_state->tpm_permanent_data.monotonicCounter,
						  countID);
    }
    /* get the session data */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_NONE,
					      TPM_ET_COUNTER,
					      ordinal,
					      NULL,
					      &(counterValue->authData),	/* OIAP */
					      counterValue->digest);		/* OSAP */
    }
    /* Validate the authorization to use the key pointed to by keyHandle */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* HMAC key */
					inParamDigest,
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					counterAuth);		/* Authorization digest for input */
    }
    if (returnCode == TPM_SUCCESS) {
	/* 1. If TPM_STCLEAR_DATA -> countID is NULL */
	if (tpm_state->tpm_stclear_data.countID == TPM_COUNT_ID_NULL) {
	    /* c. Set TPM_STCLEAR_DATA -> countID to countID */
	    tpm_state->tpm_stclear_data.countID = countID;
	    printf("TPM_Process_IncrementCounter: Setting %u as active counter\n", countID);
	}
    }
    if (returnCode == TPM_SUCCESS) {
	/* 3. Increments the counter by 1 */
	counterValue->counter++;	/* in TPM_PERMANENT_DATA */
	/* save the permanent data structure in NVRAM */
	returnCode = TPM_PermanentAll_NVStore(tpm_state,
					      TRUE,
					      returnCode);
    }
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_IncrementCounter: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* 4. Return new count value in count */
	    returnCode = TPM_CounterValue_StorePublic(response, counterValue);
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_AuthParams_Set(response,
					    *hmacKey,		/* owner HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, or continueAuthSession is FALSE, terminate the session */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    return rcf;
}

/* 25.3 TPM_ReadCounter rev 87

   Reading the counter provides the caller with the current number in the sequence.

   This returns the current value for the counter indicated. The counter MAY be any valid counter.
*/

TPM_RESULT TPM_Process_ReadCounter(tpm_state_t *tpm_state,
				   TPM_STORE_BUFFER *response,
				   TPM_TAG tag,
				   uint32_t paramSize,
				   TPM_COMMAND_CODE ordinal,
				   unsigned char *command,
				   TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;				/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    /* input parameters */
    TPM_COUNT_ID countID;			/* ID value of the counter */

    /* processing parameters */
    unsigned char *		inParamStart;			/* starting point of inParam's */
    unsigned char *		inParamEnd;			/* ending point of inParam's */
    TPM_DIGEST			inParamDigest;
    TPM_BOOL			auditStatus;		/* audit the ordinal */
    TPM_BOOL			transportEncrypt;	/* wrapped in encrypted transport session */

    /* output parameters */
    uint32_t		outParamStart;	/* starting point of outParam's */
    uint32_t		outParamEnd;	/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;

    printf("TPM_Process_ReadCounter: Ordinal Entry\n");
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&countID, &command, &paramSize);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALLOW_NO_OWNER);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag0(tag);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_ReadCounter: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /*
      Processing
    */
    /* 1. Validate that countID points to a valid counter. Return TPM_BAD_COUNTER on error. */
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_ReadCounter: countID %u\n", countID);
	returnCode = TPM_Counters_IsValidId(tpm_state->tpm_permanent_data.monotonicCounter,
					    countID);
    }
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_ReadCounter: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* 2. Return count (directly from TPM_PERMANENT_DATA) */
	    returnCode = TPM_CounterValue_StorePublic
			 (response, &(tpm_state->tpm_permanent_data.monotonicCounter[countID]));
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /*
      cleanup
    */
    return rcf;
}

/* 25.4 TPM_ReleaseCounter rev 87

  This command releases a counter such that no reads or increments of the indicated counter will
  succeed.

  The TPM uses countID to locate a valid counter. 
*/

TPM_RESULT TPM_Process_ReleaseCounter(tpm_state_t *tpm_state,
				      TPM_STORE_BUFFER *response,
				      TPM_TAG tag,
				      uint32_t paramSize,
				      TPM_COMMAND_CODE ordinal,
				      unsigned char *command,
				      TPM_TRANSPORT_INTERNAL *transportInternal)
{
    TPM_RESULT	rcf = 0;				/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    /* input parameters */
    TPM_COUNT_ID countID;		/* ID value of the counter */
    TPM_AUTHHANDLE authHandle;		/* The authorization session handle used for countID
					   authorization */
    TPM_NONCE nonceOdd;			/* Nonce associated with countID */
    TPM_BOOL continueAuthSession = TRUE;	/* Ignored */
    TPM_AUTHDATA counterAuth;		/* The authorization session digest that authorizes the use
					   of countID.	HMAC key: countID -> authData */

    /* processing parameters */
    unsigned char *		inParamStart;		/* starting point of inParam's */
    unsigned char *		inParamEnd;		/* ending point of inParam's */
    TPM_DIGEST			inParamDigest;
    TPM_BOOL			auditStatus = FALSE;		/* audit the ordinal */
    TPM_BOOL			transportEncrypt = FALSE;	/* wrapped in encrypted transport
								   session */
    TPM_BOOL			authHandleValid = FALSE;
    TPM_SECRET			*hmacKey;
    TPM_AUTH_SESSION_DATA	*auth_session_data = NULL;	/* session data for authHandle */
    TPM_COUNTER_VALUE		*counterValue;		/* associated with countID */
    TPM_SECRET			savedAuth;		/* saved copy for response */
    TPM_BOOL			writeAllNV = FALSE;	/* flag to write back NV*/

    /* output parameters */
    uint32_t		outParamStart;	/* starting point of outParam's */
    uint32_t		outParamEnd;	/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;

    printf("TPM_Process_ReleaseCounter: Ordinal Entry\n");
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&countID, &command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_ReleaseCounter: countID %u\n", countID);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALLOW_NO_OWNER);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag1(tag);
    }
    /* get the 'below the line' authorization parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					counterAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_ReleaseCounter: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* 1. Authenticate the command and the parameters using the AuthData pointed to by
       countID. Return TPM_AUTHFAIL on error */
    /* Get the TPM_COUNTER_VALUE associated with the countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Counters_GetCounterValue(&counterValue,
						  tpm_state->tpm_permanent_data.monotonicCounter,
						  countID);
    }
    /* get the session data */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_NONE,
					      TPM_ET_COUNTER,
					      ordinal,
					      NULL,
					      &(counterValue->authData),	/* OIAP */
					      counterValue->digest);		/* OSAP */
    }
    if (returnCode == TPM_SUCCESS) {
	/* make a copy of the HMAC key for the response, since it gets invalidated */
	TPM_Secret_Copy(savedAuth, *hmacKey);
	/* Validate the authorization to use the key pointed to by countID */
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* HMAC key */
					inParamDigest,
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					counterAuth);		/* Authorization digest for input */
    }
    /* 3. The TPM invalidates sessions */
    /* a. MUST invalidate all OSAP sessions associated with the counter */
    /* b. MAY invalidate any other session */
    /* NOTE: Actions reversed because the sessions can't be found after the digest is initialized */
    if (returnCode == TPM_SUCCESS) {
	TPM_AuthSessions_TerminateEntity(&continueAuthSession,
					 authHandle,
					 tpm_state->tpm_stclear_data.authSessions,
					 TPM_ET_COUNTER,		/* TPM_ENTITY_TYPE */
					 &(counterValue->digest));	/* entityDigest */
    }
    /* 2. The TPM invalidates all internal information regarding the counter. This includes
       releasing countID such that any subsequent attempts to use countID will fail. */
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_ReleaseCounter: Releasing counter %u\n", countID);
	returnCode = TPM_CounterValue_Release(counterValue, countID);
    }
    if (returnCode == TPM_SUCCESS) {
	writeAllNV= TRUE;
	/* 4. If TPM_STCLEAR_DATA -> countID equals countID,  */
	if (tpm_state->tpm_stclear_data.countID == countID ) {
	    printf("TPM_Process_ReleaseCounter: Deactivating counter %u\n", countID);
	    /* a. Set TPM_STCLEAR_DATA -> countID to an illegal value (not the NULL value) */
	    tpm_state->tpm_stclear_data.countID = TPM_COUNT_ID_ILLEGAL;
	}
    }
    /* save the permanent data structure in NVRAM */
    returnCode = TPM_PermanentAll_NVStore(tpm_state,
					  writeAllNV,
					  returnCode);
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_ReleaseCounter: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_AuthParams_Set(response,
					    savedAuth,		/* saved countID HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, terminate the session. */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    return rcf;
}

/* 25.5 TPM_ReleaseCounterOwner rev 101

   This command releases a counter such that no reads or increments of the indicated counter will
   succeed.

   This invalidates all information regarding a counter.
*/

TPM_RESULT TPM_Process_ReleaseCounterOwner(tpm_state_t *tpm_state,
					   TPM_STORE_BUFFER *response,
					   TPM_TAG tag,
					   uint32_t paramSize,
					   TPM_COMMAND_CODE ordinal,
					   unsigned char *command,
					   TPM_TRANSPORT_INTERNAL *transportInternal)
{	
    TPM_RESULT	rcf = 0;				/* fatal error precluding response */
    TPM_RESULT	returnCode = TPM_SUCCESS;	/* command return code */

    TPM_COUNT_ID countID;		/* ID value of the counter */
    TPM_AUTHHANDLE authHandle;		/* The authorization session handle used for owner
					   authentication */
    TPM_NONCE nonceOdd;			/* Nonce generated by system associated with authHandle */
    TPM_BOOL continueAuthSession = FALSE;	/* The continue use flag for the authorization
						   session handle */
    TPM_AUTHDATA ownerAuth;		/* The authorization session digest that authorizes the
					   inputs. HMAC key: ownerAuth */
  
    /* processing parameters */
    unsigned char *		inParamStart;		/* starting point of inParam's */
    unsigned char *		inParamEnd;		/* ending point of inParam's */
    TPM_DIGEST			inParamDigest;
    TPM_BOOL			auditStatus = FALSE;	/* audit the ordinal */
    TPM_BOOL			transportEncrypt = TRUE; /* wrapped in encrypted transport session */
    TPM_BOOL			authHandleValid = FALSE;
    TPM_SECRET			*hmacKey = NULL;
    TPM_AUTH_SESSION_DATA	*auth_session_data = NULL;	/* session data for authHandle */
    TPM_COUNTER_VALUE		*counterValue;		/* associated with countID */
    TPM_BOOL			writeAllNV = FALSE;	/* flag to write back NV */

    /* output parameters */
    uint32_t		outParamStart;	/* starting point of outParam's */
    uint32_t		outParamEnd;	/* ending point of outParam's */
    TPM_DIGEST		outParamDigest;

    printf("TPM_Process_ReleaseCounterOwner: Ordinal Entry\n");
    /*
      get inputs
    */
    /* save the starting point of inParam's for authorization and auditing */
    inParamStart = command;
    /* get countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Load32(&countID, &command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	printf("TPM_Process_ReleaseCounterOwner: countID %u\n", countID);
    }
    /* save the ending point of inParam's for authorization and auditing */
    inParamEnd = command;
    /* digest the input parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_GetInParamDigest(inParamDigest,	/* output */
					  &auditStatus,		/* output */
					  &transportEncrypt,	/* output */
					  tpm_state,
					  tag,
					  ordinal,
					  inParamStart,
					  inParamEnd,
					  transportInternal);
    }
    /* check state */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckState(tpm_state, tag, TPM_CHECK_ALL);
    }
    /* check tag */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CheckRequestTag1(tag);
    }
    /* get the 'below the line' authorization parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthParams_Get(&authHandle,
					&authHandleValid,
					nonceOdd,
					&continueAuthSession,
					ownerAuth,
					&command, &paramSize);
    }
    if (returnCode == TPM_SUCCESS) {
	if (paramSize != 0) {
	    printf("TPM_Process_ReleaseCounterOwner: Error, command has %u extra bytes\n",
		   paramSize);
	    returnCode = TPM_BAD_PARAM_SIZE;
	}
    }
    /* do not terminate sessions if the command did not parse correctly */
    if (returnCode != TPM_SUCCESS) {
	authHandleValid = FALSE;
    }
    /*
      Processing
    */
    /* 1. Validate that ownerAuth properly authorizes the command and parameters */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_AuthSessions_GetData(&auth_session_data,
					      &hmacKey,
					      tpm_state,
					      authHandle,
					      TPM_PID_NONE,
					      TPM_ET_OWNER,
					      ordinal,
					      NULL,
					      &(tpm_state->tpm_permanent_data.ownerAuth), /* OIAP */
					      tpm_state->tpm_permanent_data.ownerAuth);	  /* OSAP */
    }
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Authdata_Check(tpm_state,
					*hmacKey,		/* owner HMAC key */
					inParamDigest,
					auth_session_data,	/* authorization session */
					nonceOdd,		/* Nonce generated by system
								   associated with authHandle */
					continueAuthSession,
					ownerAuth);		/* Authorization digest for input */
    }
    /* 2. The TPM uses countID to locate a valid counter. Return TPM_BAD_COUNTER if not found. */
    /* Get the TPM_COUNTER_VALUE associated with the countID */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_Counters_GetCounterValue(&counterValue,
						  tpm_state->tpm_permanent_data.monotonicCounter,
						  countID);
    }
    /* NOTE: Actions reversed because the sessions can't be found after the digest is initialized */
    if (returnCode == TPM_SUCCESS) {
	TPM_AuthSessions_TerminateEntity(&continueAuthSession,
					 authHandle,
					 tpm_state->tpm_stclear_data.authSessions,
					 TPM_ET_COUNTER,		/* TPM_ENTITY_TYPE */
					 &(counterValue->digest));	/* entityDigest */
    }
    /* 3. The TPM invalidates all internal information regarding the counter. This includes
       releasing countID such that any subsequent attempts to use countID will fail. */
    /* NOTE: This function can only return a TPM_FAIL error, so that the failure to store
       TPM_PERMANENT_DATA will already be reported as fatal. */
    if (returnCode == TPM_SUCCESS) {
	returnCode = TPM_CounterValue_Release(counterValue, countID);
    }
    /* 4. The TPM invalidates sessions */
    /* a. MUST invalidate all OSAP sessions associated with the counter */
    /* b. MAY invalidate any other session */
    if (returnCode == TPM_SUCCESS) {
	writeAllNV = TRUE;
	/* 5. If TPM_STCLEAR_DATA -> countID equals countID,  */
	if (tpm_state->tpm_stclear_data.countID == countID ) {
	    printf("TPM_Process_ReleaseCounterOwner: Deactivating counter %u\n", countID);
	    /* a. Set TPM_STCLEAR_DATA -> countID to an illegal value (not the zero value) */
	    tpm_state->tpm_stclear_data.countID = TPM_COUNT_ID_ILLEGAL;
	}
    }
    /* save the permanent data structure in NVRAM */
    returnCode = TPM_PermanentAll_NVStore(tpm_state,
					  writeAllNV,
					  returnCode);
    /*
      response
    */
    /* standard response: tag, (dummy) paramSize, returnCode.  Failure is fatal. */
    if (rcf == 0) {
	printf("TPM_Process_ReleaseCounterOwner: Ordinal returnCode %08x %u\n",
	       returnCode, returnCode);
	rcf = TPM_Sbuffer_StoreInitialResponse(response, tag, returnCode);
    }
    /* success response, append the rest of the parameters.  */
    if (rcf == 0) {
	if (returnCode == TPM_SUCCESS) {
	    /* checkpoint the beginning of the outParam's */
	    outParamStart = response->buffer_current - response->buffer;
	    /* checkpoint the end of the outParam's */
	    outParamEnd = response->buffer_current - response->buffer;
	}
	/* digest the above the line output parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_GetOutParamDigest(outParamDigest,	/* output */
					       auditStatus,	/* input audit status */
					       transportEncrypt,
					       tag,			
					       returnCode,
					       ordinal,		/* command ordinal */
					       response->buffer + outParamStart,	/* start */
					       outParamEnd - outParamStart);	/* length */
	}
	/* calculate and set the below the line parameters */
	if (returnCode == TPM_SUCCESS) {
	    returnCode = TPM_AuthParams_Set(response,
					    *hmacKey,	/* HMAC key */
					    auth_session_data,
					    outParamDigest,
					    nonceOdd,
					    continueAuthSession);
	}
	/* audit if required */
	if ((returnCode == TPM_SUCCESS) && auditStatus) {
	    returnCode = TPM_ProcessAudit(tpm_state,
					  transportEncrypt,
					  inParamDigest,
					  outParamDigest,
					  ordinal);
	}
	/* adjust the initial response */
	rcf = TPM_Sbuffer_StoreFinalResponse(response, returnCode, tpm_state);
    }
    /* if there was an error, terminate the session. */
    if (((rcf != 0) ||
	 ((returnCode != TPM_SUCCESS) && (returnCode != TPM_DEFEND_LOCK_RUNNING)) ||
	 !continueAuthSession) &&
	authHandleValid) {
	TPM_AuthSessions_TerminateHandle(tpm_state->tpm_stclear_data.authSessions, authHandle);
    }
    /*
      cleanup
    */
    return rcf;
}