Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811

/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004-2007
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <langinfo.h>
#include <iconv.h>
#include <wchar.h>
#include <errno.h>

#include "trousers/tss.h"
#include "trousers_types.h"
#include "trousers/trousers.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "obj.h"
#include "tcs_tsp.h"

void
Trspi_UnloadBlob_NONCE(UINT64 *offset, BYTE *blob, TPM_NONCE *n)
{
	if (!n) {
		(*offset) += TPM_SHA1_160_HASH_LEN;
		return;
	}

	Trspi_UnloadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, n->nonce);
}

void
Trspi_LoadBlob_NONCE(UINT64 *offset, BYTE *blob, TPM_NONCE *n)
{
	Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, n->nonce);
}

void
Trspi_LoadBlob_DIGEST(UINT64 *offset, BYTE *blob, TPM_DIGEST *digest)
{
	Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, digest->digest);
}

void
Trspi_UnloadBlob_DIGEST(UINT64 *offset, BYTE *blob, TPM_DIGEST *digest)
{
	if (!digest) {
		(*offset) += TPM_SHA1_160_HASH_LEN;
		return;
	}

	Trspi_UnloadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, digest->digest);
}

void
Trspi_LoadBlob_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_PUBKEY *pubKey)
{
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &pubKey->algorithmParms);
	Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &pubKey->pubKey);
}

TSS_RESULT
Trspi_UnloadBlob_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_PUBKEY *pubKey)
{
	TSS_RESULT result;

	if (!pubKey) {
		(void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
		(void)Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, NULL);

		return TSS_SUCCESS;
	}

	if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &pubKey->algorithmParms)))
		return result;
	if ((result = Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, &pubKey->pubKey))) {
		free(pubKey->pubKey.key);
		free(pubKey->algorithmParms.parms);
		pubKey->pubKey.key = NULL;
		pubKey->pubKey.keyLength = 0;
		pubKey->algorithmParms.parms = NULL;
		pubKey->algorithmParms.parmSize = 0;
		return result;
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob(UINT64 *offset, size_t size, BYTE *to, BYTE *from)
{
	if (size == 0)
		return;

	if (to)
		memcpy(&to[(*offset)], from, size);
	(*offset) += size;
}

void
Trspi_UnloadBlob(UINT64 *offset, size_t size, BYTE *from, BYTE *to)
{
	if (size <= 0)
		return;

	if (to)
		memcpy(to, &from[*offset], size);
	(*offset) += size;
}

void
Trspi_LoadBlob_BYTE(UINT64 *offset, BYTE data, BYTE *blob)
{
	if (blob)
		blob[*offset] = data;
	(*offset)++;
}

void
Trspi_UnloadBlob_BYTE(UINT64 *offset, BYTE *dataOut, BYTE *blob)
{
	if (dataOut)
		*dataOut = blob[*offset];
	(*offset)++;
}

void
Trspi_LoadBlob_BOOL(UINT64 *offset, TSS_BOOL data, BYTE *blob)
{
	if (blob)
		blob[*offset] = (BYTE) data;
	(*offset)++;
}

void
Trspi_UnloadBlob_BOOL(UINT64 *offset, TSS_BOOL *dataOut, BYTE *blob)
{
	if (dataOut)
		*dataOut = blob[*offset];
	(*offset)++;
}

void
Trspi_LoadBlob_UINT64(UINT64 *offset, UINT64 in, BYTE *blob)
{
	if (blob)
		UINT64ToArray(in, &blob[*offset]);
	(*offset) += sizeof(UINT64);
}

void
Trspi_LoadBlob_UINT32(UINT64 *offset, UINT32 in, BYTE *blob)
{
	if (blob)
		UINT32ToArray(in, &blob[*offset]);
	(*offset) += sizeof(UINT32);
}

void
Trspi_LoadBlob_UINT16(UINT64 *offset, UINT16 in, BYTE *blob)
{
	if (blob)
		UINT16ToArray(in, &blob[*offset]);
	(*offset) += sizeof(UINT16);
}

void
Trspi_UnloadBlob_UINT64(UINT64 *offset, UINT64 *out, BYTE *blob)
{
	if (out)
		*out = Decode_UINT64(&blob[*offset]);
	(*offset) += sizeof(UINT64);
}

void
Trspi_UnloadBlob_UINT32(UINT64 *offset, UINT32 *out, BYTE *blob)
{
	if (out)
		*out = Decode_UINT32(&blob[*offset]);
	(*offset) += sizeof(UINT32);
}

void
Trspi_UnloadBlob_UINT16(UINT64 *offset, UINT16 *out, BYTE *blob)
{
	if (out)
		*out = Decode_UINT16(&blob[*offset]);
	(*offset) += sizeof(UINT16);
}

void
Trspi_LoadBlob_RSA_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_RSA_KEY_PARMS *parms)
{
	Trspi_LoadBlob_UINT32(offset, parms->keyLength, blob);
	Trspi_LoadBlob_UINT32(offset, parms->numPrimes, blob);
	Trspi_LoadBlob_UINT32(offset, parms->exponentSize, blob);

	if (parms->exponentSize > 0)
		Trspi_LoadBlob(offset, parms->exponentSize, blob, parms->exponent);
}

void
Trspi_UnloadBlob_TSS_VERSION(UINT64 *offset, BYTE *blob, TSS_VERSION *out)
{
	if (!out) {
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_BYTE(offset, &out->bMajor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->bMinor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->bRevMajor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->bRevMinor, blob);
}

void
Trspi_LoadBlob_TSS_VERSION(UINT64 *offset, BYTE *blob, TSS_VERSION version)
{
	Trspi_LoadBlob_BYTE(offset, version.bMajor, blob);
	Trspi_LoadBlob_BYTE(offset, version.bMinor, blob);
	Trspi_LoadBlob_BYTE(offset, version.bRevMajor, blob);
	Trspi_LoadBlob_BYTE(offset, version.bRevMinor, blob);
}

void
Trspi_UnloadBlob_TCPA_VERSION(UINT64 *offset, BYTE *blob, TCPA_VERSION *out)
{
	if (!out) {
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_BYTE(offset, &out->major, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->minor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->revMajor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->revMinor, blob);
}

void
Trspi_LoadBlob_TCPA_VERSION(UINT64 *offset, BYTE *blob, TCPA_VERSION version)
{
	Trspi_LoadBlob_BYTE(offset, version.major, blob);
	Trspi_LoadBlob_BYTE(offset, version.minor, blob);
	Trspi_LoadBlob_BYTE(offset, version.revMajor, blob);
	Trspi_LoadBlob_BYTE(offset, version.revMinor, blob);
}

TSS_RESULT
Trspi_UnloadBlob_PCR_INFO(UINT64 *offset, BYTE *blob, TCPA_PCR_INFO *pcr)
{
	TSS_RESULT result;

	if (!pcr) {
		(void)Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);

		return TSS_SUCCESS;
	}

	if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection)))
		return result;
	Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
	Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_PCR_INFO(UINT64 *offset, BYTE *blob, TCPA_PCR_INFO *pcr)
{
	Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection);
	Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
	Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
}

TSS_RESULT
Trspi_UnloadBlob_PCR_INFO_LONG(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_LONG *pcr)
{
	TSS_RESULT result;

	if (!pcr) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
		Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &pcr->tag, blob);
	Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtCreation, blob);
	Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtRelease, blob);
	if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->creationPCRSelection)))
		return result;
	if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->releasePCRSelection)))
		return result;
	Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
	Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_PCR_INFO_LONG(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_LONG *pcr)
{
	Trspi_LoadBlob_UINT16(offset, pcr->tag, blob);
	Trspi_LoadBlob_BYTE(offset, pcr->localityAtCreation, blob);
	Trspi_LoadBlob_BYTE(offset, pcr->localityAtRelease, blob);
	Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->creationPCRSelection);
	Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->releasePCRSelection);
	Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
	Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
}

TSS_RESULT
Trspi_UnloadBlob_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_SHORT *pcr)
{
	TSS_RESULT result;

	if (!pcr) {
		Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);

		return TSS_SUCCESS;
	}

	if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection)))
		return result;
	Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtRelease, blob);
	Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_SHORT *pcr)
{
	Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection);
	Trspi_LoadBlob_BYTE(offset, pcr->localityAtRelease, blob);
	Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
}

TSS_RESULT
Trspi_UnloadBlob_PCR_SELECTION(UINT64 *offset, BYTE *blob, TCPA_PCR_SELECTION *pcr)
{
	if (!pcr) {
		UINT16 sizeOfSelect;

		Trspi_UnloadBlob_UINT16(offset, &sizeOfSelect, blob);
		Trspi_UnloadBlob(offset, sizeOfSelect, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &pcr->sizeOfSelect, blob);

	if (pcr->sizeOfSelect > 0) {
		pcr->pcrSelect = calloc(1, pcr->sizeOfSelect);
		if (pcr->pcrSelect == NULL) {
			LogError("malloc of %u bytes failed.", pcr->sizeOfSelect);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}

		Trspi_UnloadBlob(offset, pcr->sizeOfSelect, blob, pcr->pcrSelect);
	} else {
		pcr->pcrSelect = NULL;
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_PCR_SELECTION(UINT64 *offset, BYTE *blob, TCPA_PCR_SELECTION *pcr)
{
	UINT16 i;

	Trspi_LoadBlob_UINT16(offset, pcr->sizeOfSelect, blob);
	for (i = 0; i < pcr->sizeOfSelect; i++)
		Trspi_LoadBlob_BYTE(offset, pcr->pcrSelect[i], blob);
}

void
Trspi_LoadBlob_KEY12(UINT64 *offset, BYTE *blob, TPM_KEY12 *key)
{
	Trspi_LoadBlob_UINT16(offset, key->tag, blob);
	Trspi_LoadBlob_UINT16(offset, key->fill, blob);
	Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
	Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
	Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
	Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
	Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
	Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
	Trspi_LoadBlob_UINT32(offset, key->encSize, blob);
	Trspi_LoadBlob(offset, key->encSize, blob, key->encData);
}

void
Trspi_LoadBlob_KEY(UINT64 *offset, BYTE *blob, TCPA_KEY *key)
{
	Trspi_LoadBlob_TCPA_VERSION(offset, blob, key->ver);
	Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
	Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
	Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
	Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
	Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
	Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
	Trspi_LoadBlob_UINT32(offset, key->encSize, blob);
	Trspi_LoadBlob(offset, key->encSize, blob, key->encData);
}

void
Trspi_LoadBlob_KEY_FLAGS(UINT64 *offset, BYTE *blob, TCPA_KEY_FLAGS *flags)
{
	Trspi_LoadBlob_UINT32(offset, *flags, blob);
}

void
Trspi_UnloadBlob_KEY_FLAGS(UINT64 *offset, BYTE *blob, TCPA_KEY_FLAGS *flags)
{
	Trspi_UnloadBlob_UINT32(offset, flags, blob);
}

void
Trspi_LoadBlob_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_KEY_PARMS *keyInfo)
{
	Trspi_LoadBlob_UINT32(offset, keyInfo->algorithmID, blob);
	Trspi_LoadBlob_UINT16(offset, keyInfo->encScheme, blob);
	Trspi_LoadBlob_UINT16(offset, keyInfo->sigScheme, blob);
	Trspi_LoadBlob_UINT32(offset, keyInfo->parmSize, blob);

	if (keyInfo->parmSize > 0)
		Trspi_LoadBlob(offset, keyInfo->parmSize, blob, keyInfo->parms);
}

void
Trspi_LoadBlob_STORE_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_STORE_PUBKEY *store)
{
	Trspi_LoadBlob_UINT32(offset, store->keyLength, blob);
	Trspi_LoadBlob(offset, store->keyLength, blob, store->key);
}

void
Trspi_LoadBlob_UUID(UINT64 *offset, BYTE *blob, TSS_UUID uuid)
{
	Trspi_LoadBlob_UINT32(offset, uuid.ulTimeLow, blob);
	Trspi_LoadBlob_UINT16(offset, uuid.usTimeMid, blob);
	Trspi_LoadBlob_UINT16(offset, uuid.usTimeHigh, blob);
	Trspi_LoadBlob_BYTE(offset, uuid.bClockSeqHigh, blob);
	Trspi_LoadBlob_BYTE(offset, uuid.bClockSeqLow, blob);
	Trspi_LoadBlob(offset, 6, blob, uuid.rgbNode);
}

void
Trspi_UnloadBlob_UUID(UINT64 *offset, BYTE *blob, TSS_UUID *uuid)
{
	if (!uuid) {
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob(offset, 6, blob, NULL);

		return;
	}

	memset(uuid, 0, sizeof(TSS_UUID));
	Trspi_UnloadBlob_UINT32(offset, &uuid->ulTimeLow, blob);
	Trspi_UnloadBlob_UINT16(offset, &uuid->usTimeMid, blob);
	Trspi_UnloadBlob_UINT16(offset, &uuid->usTimeHigh, blob);
	Trspi_UnloadBlob_BYTE(offset, &uuid->bClockSeqHigh, blob);
	Trspi_UnloadBlob_BYTE(offset, &uuid->bClockSeqLow, blob);
	Trspi_UnloadBlob(offset, 6, blob, uuid->rgbNode);
}

TSS_RESULT
Trspi_UnloadBlob_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_KEY_PARMS *keyParms)
{
	if (!keyParms) {
		UINT32 parmSize;

		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, &parmSize, blob);

		(*offset) += parmSize;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &keyParms->algorithmID, blob);
	Trspi_UnloadBlob_UINT16(offset, &keyParms->encScheme, blob);
	Trspi_UnloadBlob_UINT16(offset, &keyParms->sigScheme, blob);
	Trspi_UnloadBlob_UINT32(offset, &keyParms->parmSize, blob);

	if (keyParms->parmSize > 0) {
		keyParms->parms = malloc(keyParms->parmSize);
		if (keyParms->parms == NULL) {
			LogError("malloc of %u bytes failed.", keyParms->parmSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, keyParms->parmSize, blob, keyParms->parms);
	} else {
		keyParms->parms = NULL;
	}

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_KEY12(UINT64 *offset, BYTE *blob, TPM_KEY12 *key)
{
	TSS_RESULT result;

	if (!key) {
		UINT32 PCRInfoSize, encSize;

		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_KEY_FLAGS(offset, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &PCRInfoSize, blob);
		Trspi_UnloadBlob(offset, PCRInfoSize, blob, NULL);
		Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &encSize, blob);
		Trspi_UnloadBlob(offset, encSize, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &key->tag, blob);
	Trspi_UnloadBlob_UINT16(offset, &key->fill, blob);
	Trspi_UnloadBlob_UINT16(offset, &key->keyUsage, blob);
	Trspi_UnloadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
	Trspi_UnloadBlob_BYTE(offset, &key->authDataUsage, blob);
	if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, (BYTE *) blob, &key->algorithmParms)))
		return result;
	Trspi_UnloadBlob_UINT32(offset, &key->PCRInfoSize, blob);

	if (key->PCRInfoSize > 0) {
		key->PCRInfo = malloc(key->PCRInfoSize);
		if (key->PCRInfo == NULL) {
			LogError("malloc of %d bytes failed.", key->PCRInfoSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
	} else {
		key->PCRInfo = NULL;
	}

	if ((result = Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, &key->pubKey)))
		return result;
	Trspi_UnloadBlob_UINT32(offset, &key->encSize, blob);

	if (key->encSize > 0) {
		key->encData = malloc(key->encSize);
		if (key->encData == NULL) {
			LogError("malloc of %d bytes failed.", key->encSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->encSize, blob, key->encData);
	} else {
		key->encData = NULL;
	}

	return result;
}

TSS_RESULT
Trspi_UnloadBlob_KEY(UINT64 *offset, BYTE *blob, TCPA_KEY *key)
{
	TSS_RESULT result;

	if (!key) {
		UINT32 PCRInfoSize, encSize;

		Trspi_UnloadBlob_TCPA_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_KEY_FLAGS(offset, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &PCRInfoSize, blob);
		Trspi_UnloadBlob(offset, PCRInfoSize, blob, NULL);
		Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &encSize, blob);
		Trspi_UnloadBlob(offset, encSize, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_TCPA_VERSION(offset, blob, &key->ver);
	Trspi_UnloadBlob_UINT16(offset, &key->keyUsage, blob);
	Trspi_UnloadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
	Trspi_UnloadBlob_BYTE(offset, &key->authDataUsage, blob);
	if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, (BYTE *) blob, &key->algorithmParms)))
		return result;
	Trspi_UnloadBlob_UINT32(offset, &key->PCRInfoSize, blob);

	if (key->PCRInfoSize > 0) {
		key->PCRInfo = malloc(key->PCRInfoSize);
		if (key->PCRInfo == NULL) {
			LogError("malloc of %d bytes failed.", key->PCRInfoSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
	} else {
		key->PCRInfo = NULL;
	}

	if ((result = Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, &key->pubKey)))
		return result;
	Trspi_UnloadBlob_UINT32(offset, &key->encSize, blob);

	if (key->encSize > 0) {
		key->encData = malloc(key->encSize);
		if (key->encData == NULL) {
			LogError("malloc of %d bytes failed.", key->encSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->encSize, blob, key->encData);
	} else {
		key->encData = NULL;
	}

	return result;
}

TSS_RESULT
Trspi_UnloadBlob_STORE_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_STORE_PUBKEY *store)
{
	if (!store) {
		UINT32 keyLength;

		Trspi_UnloadBlob_UINT32(offset, &keyLength, blob);
		Trspi_UnloadBlob(offset, keyLength, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &store->keyLength, blob);

	if (store->keyLength > 0) {
		store->key = malloc(store->keyLength);
		if (store->key == NULL) {
			LogError("malloc of %d bytes failed.", store->keyLength);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, store->keyLength, blob, store->key);
	} else {
		store->key = NULL;
	}

	return TSS_SUCCESS;
}

void
Trspi_UnloadBlob_VERSION(UINT64 *offset, BYTE *blob, TCPA_VERSION *out)
{
	if (!out) {
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_BYTE(offset, &out->major, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->minor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->revMajor, blob);
	Trspi_UnloadBlob_BYTE(offset, &out->revMinor, blob);
}

TSS_RESULT
Trspi_UnloadBlob_KM_KEYINFO(UINT64 *offset, BYTE *blob, TSS_KM_KEYINFO *info)
{
	if (!info) {
		UINT32 ulVendorDataLength;

		Trspi_UnloadBlob_TSS_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UUID(offset, blob, NULL);
		Trspi_UnloadBlob_UUID(offset, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BOOL(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, &ulVendorDataLength, blob);

		(*offset) += ulVendorDataLength;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_TSS_VERSION(offset, blob, &info->versionInfo);
	Trspi_UnloadBlob_UUID(offset, blob, &info->keyUUID);
	Trspi_UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
	Trspi_UnloadBlob_BYTE(offset, &info->bAuthDataUsage, blob);
	Trspi_UnloadBlob_BOOL(offset, &info->fIsLoaded, blob);
	Trspi_UnloadBlob_UINT32(offset, &info->ulVendorDataLength, blob);
	if (info->ulVendorDataLength > 0){
		/* allocate space for vendor data */
		info->rgbVendorData = malloc(info->ulVendorDataLength);
		if (info->rgbVendorData == NULL) {
			LogError("malloc of %u bytes failed.", info->ulVendorDataLength);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}

		Trspi_UnloadBlob(offset, info->ulVendorDataLength, blob, info->rgbVendorData);
	} else
		info->rgbVendorData = NULL;

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_KM_KEYINFO2(UINT64 *offset, BYTE *blob, TSS_KM_KEYINFO2 *info)
{
	if (!info) {
		UINT32 ulVendorDataLength;

		Trspi_UnloadBlob_TSS_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UUID(offset, blob, NULL);
		Trspi_UnloadBlob_UUID(offset, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_BOOL(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, &ulVendorDataLength, blob);

		(*offset) += ulVendorDataLength;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_TSS_VERSION(offset, blob, &info->versionInfo);
	Trspi_UnloadBlob_UUID(offset, blob, &info->keyUUID);
	Trspi_UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
	Trspi_UnloadBlob_BYTE(offset, &info->bAuthDataUsage, blob);
	/* Takes data regarding the new 2 fields of TSS_KM_KEYINFO2 */
	Trspi_UnloadBlob_UINT32(offset, &info->persistentStorageType, blob);
	Trspi_UnloadBlob_UINT32(offset, &info->persistentStorageTypeParent, blob);
	Trspi_UnloadBlob_BOOL(offset, &info->fIsLoaded, blob);
	Trspi_UnloadBlob_UINT32(offset, &info->ulVendorDataLength, blob);
	if (info->ulVendorDataLength > 0) {
		/* allocate space for vendor data */
		info->rgbVendorData = malloc(info->ulVendorDataLength);
		if (info->rgbVendorData == NULL) {
			LogError("malloc of %u bytes failed.", info->ulVendorDataLength);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}

		Trspi_UnloadBlob(offset, info->ulVendorDataLength, blob, info->rgbVendorData);
	} else
		info->rgbVendorData = NULL;

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_PCR_EVENT(UINT64 *offset, BYTE *blob, TSS_PCR_EVENT *event)
{
	Trspi_LoadBlob_TCPA_VERSION(offset, blob, *(TCPA_VERSION *)(&event->versionInfo));
	Trspi_LoadBlob_UINT32(offset, event->ulPcrIndex, blob);
	Trspi_LoadBlob_UINT32(offset, event->eventType, blob);

	Trspi_LoadBlob_UINT32(offset, event->ulPcrValueLength, blob);
	if (event->ulPcrValueLength > 0)
		Trspi_LoadBlob(offset, event->ulPcrValueLength, blob, event->rgbPcrValue);

	Trspi_LoadBlob_UINT32(offset, event->ulEventLength, blob);
	if (event->ulEventLength > 0)
		Trspi_LoadBlob(offset, event->ulEventLength, blob, event->rgbEvent);

}

TSS_RESULT
Trspi_UnloadBlob_PCR_EVENT(UINT64 *offset, BYTE *blob, TSS_PCR_EVENT *event)
{
	if (!event) {
		UINT32 ulPcrValueLength, ulEventLength;

		Trspi_UnloadBlob_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		Trspi_UnloadBlob_UINT32(offset, &ulPcrValueLength, blob);
		(*offset) += ulPcrValueLength;

		Trspi_UnloadBlob_UINT32(offset, &ulEventLength, blob);
		(*offset) += ulEventLength;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&(event->versionInfo));
	Trspi_UnloadBlob_UINT32(offset, &event->ulPcrIndex, blob);
	Trspi_UnloadBlob_UINT32(offset, &event->eventType, blob);

	Trspi_UnloadBlob_UINT32(offset, &event->ulPcrValueLength, blob);
	if (event->ulPcrValueLength > 0) {
		event->rgbPcrValue = malloc(event->ulPcrValueLength);
		if (event->rgbPcrValue == NULL) {
			LogError("malloc of %u bytes failed.", event->ulPcrValueLength);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}

		Trspi_UnloadBlob(offset, event->ulPcrValueLength, blob, event->rgbPcrValue);
	} else {
		event->rgbPcrValue = NULL;
	}

	Trspi_UnloadBlob_UINT32(offset, &event->ulEventLength, blob);
	if (event->ulEventLength > 0) {
		event->rgbEvent = malloc(event->ulEventLength);
		if (event->rgbEvent == NULL) {
			LogError("malloc of %d bytes failed.", event->ulEventLength);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}

		Trspi_UnloadBlob(offset, event->ulEventLength, blob, event->rgbEvent);
	} else {
		event->rgbEvent = NULL;
	}

	return TSS_SUCCESS;
}

/* loads a blob with the info needed to hash when creating the private key area
 * of a TPM_KEY(12) from an external source
 */
void
Trspi_LoadBlob_PRIVKEY_DIGEST12(UINT64 *offset, BYTE *blob, TPM_KEY12 *key)
{
	Trspi_LoadBlob_UINT16(offset, key->tag, blob);
	Trspi_LoadBlob_UINT16(offset, key->fill, blob);
	Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
	Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
	Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);

	Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
	/* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
	if (key->PCRInfoSize != 0)
		Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);

	Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
	/* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */
}

void
Trspi_LoadBlob_PRIVKEY_DIGEST(UINT64 *offset, BYTE *blob, TCPA_KEY *key)
{
	Trspi_LoadBlob_TCPA_VERSION(offset, blob, key->ver);
	Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
	Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
	Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);

	Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
	/* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
	if (key->PCRInfoSize != 0)
		Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);

	Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
	/* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */
}

void
Trspi_LoadBlob_SYMMETRIC_KEY(UINT64 *offset, BYTE *blob, TCPA_SYMMETRIC_KEY *key)
{
	Trspi_LoadBlob_UINT32(offset, key->algId, blob);
	Trspi_LoadBlob_UINT16(offset, key->encScheme, blob);
	Trspi_LoadBlob_UINT16(offset, key->size, blob);

	if (key->size > 0)
		Trspi_LoadBlob(offset, key->size, blob, key->data);
}

TSS_RESULT
Trspi_UnloadBlob_SYMMETRIC_KEY(UINT64 *offset, BYTE *blob, TCPA_SYMMETRIC_KEY *key)
{
	if (!key) {
		UINT16 size;

		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, &size, blob);
		(*offset) += size;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &key->algId, blob);
	Trspi_UnloadBlob_UINT16(offset, &key->encScheme, blob);
	Trspi_UnloadBlob_UINT16(offset, &key->size, blob);

	if (key->size > 0) {
		key->data = malloc(key->size);
		if (key->data == NULL) {
			key->size = 0;
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->size, blob, key->data);
	} else {
		key->data = NULL;
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_IDENTITY_REQ(UINT64 *offset, BYTE *blob, TCPA_IDENTITY_REQ *req)
{
	Trspi_LoadBlob_UINT32(offset, req->asymSize, blob);
	Trspi_LoadBlob_UINT32(offset, req->symSize, blob);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &req->asymAlgorithm);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &req->symAlgorithm);
	Trspi_LoadBlob(offset, req->asymSize, blob, req->asymBlob);
	Trspi_LoadBlob(offset, req->symSize, blob, req->symBlob);
}

void
Trspi_LoadBlob_CHANGEAUTH_VALIDATE(UINT64 *offset, BYTE *blob, TPM_CHANGEAUTH_VALIDATE *caValidate)
{
	Trspi_LoadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, caValidate->newAuthSecret.authdata);
	Trspi_LoadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, caValidate->n1.nonce);
}

TSS_RESULT
Trspi_UnloadBlob_IDENTITY_REQ(UINT64 *offset, BYTE *blob, TCPA_IDENTITY_REQ *req)
{
	TSS_RESULT result;

	if (!req) {
		UINT32 asymSize, symSize;

		Trspi_UnloadBlob_UINT32(offset, &asymSize, blob);
		Trspi_UnloadBlob_UINT32(offset, &symSize, blob);
		(void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
		(void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);

		(*offset) += asymSize;
		(*offset) += symSize;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &req->asymSize, blob);
	Trspi_UnloadBlob_UINT32(offset, &req->symSize, blob);
	if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &req->asymAlgorithm)))
		return result;
	if ((Trspi_UnloadBlob_KEY_PARMS(offset, blob, &req->symAlgorithm))) {
		free(req->asymAlgorithm.parms);
		req->asymAlgorithm.parmSize = 0;
		return result;
	}

	if (req->asymSize > 0) {
		req->asymBlob = malloc(req->asymSize);
		if (req->asymBlob == NULL) {
			req->asymSize = 0;
			req->asymAlgorithm.parmSize = 0;
			free(req->asymAlgorithm.parms);
			req->symAlgorithm.parmSize = 0;
			free(req->symAlgorithm.parms);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, req->asymSize, blob, req->asymBlob);
	} else {
		req->asymBlob = NULL;
	}

	if (req->symSize > 0) {
		req->symBlob = malloc(req->symSize);
		if (req->symBlob == NULL) {
			req->symSize = 0;
			req->asymSize = 0;
			free(req->asymBlob);
			req->asymBlob = NULL;
			req->asymAlgorithm.parmSize = 0;
			free(req->asymAlgorithm.parms);
			req->symAlgorithm.parmSize = 0;
			free(req->symAlgorithm.parms);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, req->symSize, blob, req->symBlob);
	} else {
		req->symBlob = NULL;
	}

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_IDENTITY_PROOF(UINT64 *offset, BYTE *blob, TCPA_IDENTITY_PROOF *proof)
{
	TSS_RESULT result;

	if (!proof) {
		UINT32 labelSize, identityBindingSize, endorsementSize, platformSize;
		UINT32 conformanceSize;

		Trspi_UnloadBlob_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &labelSize, blob);
		Trspi_UnloadBlob_UINT32(offset, &identityBindingSize, blob);
		Trspi_UnloadBlob_UINT32(offset, &endorsementSize, blob);
		Trspi_UnloadBlob_UINT32(offset, &platformSize, blob);
		Trspi_UnloadBlob_UINT32(offset, &conformanceSize, blob);

		(void)Trspi_UnloadBlob_PUBKEY(offset, blob, NULL);

		(*offset) += labelSize;
		(*offset) += identityBindingSize;
		(*offset) += endorsementSize;
		(*offset) += platformSize;
		(*offset) += conformanceSize;

		return TSS_SUCCESS;
	}

	/* helps when an error occurs */
	memset(proof, 0, sizeof(TCPA_IDENTITY_PROOF));

	Trspi_UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&proof->ver);
	Trspi_UnloadBlob_UINT32(offset, &proof->labelSize, blob);
	Trspi_UnloadBlob_UINT32(offset, &proof->identityBindingSize, blob);
	Trspi_UnloadBlob_UINT32(offset, &proof->endorsementSize, blob);
	Trspi_UnloadBlob_UINT32(offset, &proof->platformSize, blob);
	Trspi_UnloadBlob_UINT32(offset, &proof->conformanceSize, blob);

	if ((result = Trspi_UnloadBlob_PUBKEY(offset, blob,
					      &proof->identityKey))) {
		proof->labelSize = 0;
		proof->identityBindingSize = 0;
		proof->endorsementSize = 0;
		proof->platformSize = 0;
		proof->conformanceSize = 0;
		return result;
	}

	if (proof->labelSize > 0) {
		proof->labelArea = malloc(proof->labelSize);
		if (proof->labelArea == NULL) {
			result = TSPERR(TSS_E_OUTOFMEMORY);
			goto error;
		}
		Trspi_UnloadBlob(offset, proof->labelSize, blob, proof->labelArea);
	} else {
		proof->labelArea = NULL;
	}

	if (proof->identityBindingSize > 0) {
		proof->identityBinding = malloc(proof->identityBindingSize);
		if (proof->identityBinding == NULL) {
			result = TSPERR(TSS_E_OUTOFMEMORY);
			goto error;
		}
		Trspi_UnloadBlob(offset, proof->identityBindingSize, blob,
				 proof->identityBinding);
	} else {
		proof->identityBinding = NULL;
	}

	if (proof->endorsementSize > 0) {
		proof->endorsementCredential = malloc(proof->endorsementSize);
		if (proof->endorsementCredential == NULL) {
			result = TSPERR(TSS_E_OUTOFMEMORY);
			goto error;
		}
		Trspi_UnloadBlob(offset, proof->endorsementSize, blob,
				 proof->endorsementCredential);
	} else {
		proof->endorsementCredential = NULL;
	}

	if (proof->platformSize > 0) {
		proof->platformCredential = malloc(proof->platformSize);
		if (proof->platformCredential == NULL) {
			result = TSPERR(TSS_E_OUTOFMEMORY);
			goto error;
		}
		Trspi_UnloadBlob(offset, proof->platformSize, blob,
				 proof->platformCredential);
	} else {
		proof->platformCredential = NULL;
	}

	if (proof->conformanceSize > 0) {
		proof->conformanceCredential = malloc(proof->conformanceSize);
		if (proof->conformanceCredential == NULL) {
			result = TSPERR(TSS_E_OUTOFMEMORY);
			goto error;
		}
		Trspi_UnloadBlob(offset, proof->conformanceSize, blob,
				 proof->conformanceCredential);
	} else {
		proof->conformanceCredential = NULL;
	}

	return TSS_SUCCESS;
error:
	proof->labelSize = 0;
	proof->identityBindingSize = 0;
	proof->endorsementSize = 0;
	proof->platformSize = 0;
	proof->conformanceSize = 0;
	free(proof->labelArea);
	proof->labelArea = NULL;
	free(proof->identityBinding);
	proof->identityBinding = NULL;
	free(proof->endorsementCredential);
	proof->endorsementCredential = NULL;
	free(proof->conformanceCredential);
	proof->conformanceCredential = NULL;
	/* free identityKey */
	free(proof->identityKey.pubKey.key);
	free(proof->identityKey.algorithmParms.parms);
	proof->identityKey.pubKey.key = NULL;
	proof->identityKey.pubKey.keyLength = 0;
	proof->identityKey.algorithmParms.parms = NULL;
	proof->identityKey.algorithmParms.parmSize = 0;

	return result;
}

void
Trspi_LoadBlob_SYM_CA_ATTESTATION(UINT64 *offset, BYTE *blob, TCPA_SYM_CA_ATTESTATION *sym)
{
	Trspi_LoadBlob_UINT32(offset, sym->credSize, blob);
	Trspi_LoadBlob_KEY_PARMS(offset, blob, &sym->algorithm);
	Trspi_LoadBlob(offset, sym->credSize, blob, sym->credential);
}

TSS_RESULT
Trspi_UnloadBlob_SYM_CA_ATTESTATION(UINT64 *offset, BYTE *blob, TCPA_SYM_CA_ATTESTATION *sym)
{
	TSS_RESULT result;

	if (!sym) {
		UINT32 credSize;

		Trspi_UnloadBlob_UINT32(offset, &credSize, blob);
		(void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);

		(*offset) += credSize;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &sym->credSize, blob);
	if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &sym->algorithm))) {
		sym->credSize = 0;
		return result;
	}

	if (sym->credSize > 0) {
		if ((sym->credential = malloc(sym->credSize)) == NULL) {
			free(sym->algorithm.parms);
			sym->algorithm.parmSize = 0;
			sym->credSize = 0;
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, sym->credSize, blob, sym->credential);
	} else {
		sym->credential = NULL;
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_ASYM_CA_CONTENTS(UINT64 *offset, BYTE *blob, TCPA_ASYM_CA_CONTENTS *asym)
{
	Trspi_LoadBlob_SYMMETRIC_KEY(offset, blob, &asym->sessionKey);
	Trspi_LoadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob,
		       (BYTE *)&asym->idDigest);
}

TSS_RESULT
Trspi_UnloadBlob_ASYM_CA_CONTENTS(UINT64 *offset, BYTE *blob, TCPA_ASYM_CA_CONTENTS *asym)
{
	TSS_RESULT result;

	if (!asym) {
		(void)Trspi_UnloadBlob_SYMMETRIC_KEY(offset, blob, NULL);
		Trspi_UnloadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, NULL);

		return TSS_SUCCESS;
	}

	if ((result = Trspi_UnloadBlob_SYMMETRIC_KEY(offset, blob, &asym->sessionKey)))
		return result;

	Trspi_UnloadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, (BYTE *)&asym->idDigest);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_BOUND_DATA(UINT64 *offset, TCPA_BOUND_DATA bd, UINT32 payloadLength, BYTE *blob)
{
	Trspi_LoadBlob_TCPA_VERSION(offset, blob, bd.ver);
	Trspi_LoadBlob(offset, 1, blob, &bd.payload);
	Trspi_LoadBlob(offset, payloadLength, blob, bd.payloadData);
}

/* function to mimic strerror with TSS error codes */
char *
Trspi_Error_String(TSS_RESULT r)
{
	/* Check the return code to see if it is common to all layers.
	 * If so, return it.
	 */
	switch (TSS_ERROR_CODE(r)) {
		case TSS_SUCCESS:			return "Success";
		default:
			break;
	}

	/* The return code is either unknown, or specific to a layer */
	if (TSS_ERROR_LAYER(r) == TSS_LAYER_TPM) {
		switch (TSS_ERROR_CODE(r)) {
			case TPM_E_AUTHFAIL:			return "Authentication failed";
			case TPM_E_BAD_PARAMETER:		return "Bad Parameter";
			case TPM_E_BADINDEX:			return "Bad memory index";
			case TPM_E_AUDITFAILURE:		return "Audit failure";
			case TPM_E_CLEAR_DISABLED:		return "Clear has been disabled";
			case TPM_E_DEACTIVATED:			return "TPM is deactivated";
			case TPM_E_DISABLED:			return "TPM is disabled";
			case TPM_E_FAIL:			return "Operation failed";
			case TPM_E_BAD_ORDINAL:			return "Ordinal was unknown or inconsistent";
			case TPM_E_INSTALL_DISABLED:		return "Owner install disabled";
			case TPM_E_INVALID_KEYHANDLE:		return "Invalid keyhandle";
			case TPM_E_KEYNOTFOUND:			return "Key not found";
			case TPM_E_INAPPROPRIATE_ENC:		return "Bad encryption scheme";
			case TPM_E_MIGRATEFAIL:			return "Migration authorization failed";
			case TPM_E_INVALID_PCR_INFO:		return "PCR information uninterpretable";
			case TPM_E_NOSPACE:			return "No space to load key";
			case TPM_E_NOSRK:			return "No SRK";
			case TPM_E_NOTSEALED_BLOB:		return "Encrypted blob invalid";
			case TPM_E_OWNER_SET:			return "Owner already set";
			case TPM_E_RESOURCES:			return "Insufficient TPM resources";
			case TPM_E_SHORTRANDOM:			return "Random string too short";
			case TPM_E_SIZE:			return "TPM out of space";
			case TPM_E_WRONGPCRVAL:			return "Wrong PCR value";
			case TPM_E_BAD_PARAM_SIZE:		return "Bad input size";
			case TPM_E_SHA_THREAD:			return "No existing SHA-1 thread";
			case TPM_E_SHA_ERROR:			return "SHA-1 error";
			case TPM_E_FAILEDSELFTEST:		return "Self-test failed, TPM shutdown";
			case TPM_E_AUTH2FAIL:			return "Second authorization session failed";
			case TPM_E_BADTAG:			return "Invalid tag";
			case TPM_E_IOERROR:			return "I/O error";
			case TPM_E_ENCRYPT_ERROR:		return "Encryption error";
			case TPM_E_DECRYPT_ERROR:		return "Decryption error";
			case TPM_E_INVALID_AUTHHANDLE:		return "Invalid authorization handle";
			case TPM_E_NO_ENDORSEMENT:		return "No EK";
			case TPM_E_INVALID_KEYUSAGE:		return "Invalid key usage";
			case TPM_E_WRONG_ENTITYTYPE:		return "Invalid entity type";
			case TPM_E_INVALID_POSTINIT:		return "Invalid POST init sequence";
			case TPM_E_INAPPROPRIATE_SIG:		return "Invalid signature format";
			case TPM_E_BAD_KEY_PROPERTY:		return "Unsupported key parameters";
			case TPM_E_BAD_MIGRATION:		return "Invalid migration properties";
			case TPM_E_BAD_SCHEME:			return "Invalid signature or encryption scheme";
			case TPM_E_BAD_DATASIZE:		return "Invalid data size";
			case TPM_E_BAD_MODE:			return "Bad mode parameter";
			case TPM_E_BAD_PRESENCE:		return "Bad physical presence value";
			case TPM_E_BAD_VERSION:			return "Invalid version";
			case TPM_E_NO_WRAP_TRANSPORT:		return "TPM does not allow for wrapped transport sessions";
			case TPM_E_AUDITFAIL_UNSUCCESSFUL:	return "TPM audit construction failed and the underlying command was returning a failure code also";
			case TPM_E_AUDITFAIL_SUCCESSFUL:	return "TPM audit construction failed and the underlying command was returning success";
			case TPM_E_NOTRESETABLE:		return "Attempt to reset a PCR register that does not have the resettable attribute";
			case TPM_E_NOTLOCAL:			return "Attempt to reset a PCR register that requires locality and locality modifier not part of command transport";
			case TPM_E_BAD_TYPE:			return "Make identity blob not properly typed";
			case TPM_E_INVALID_RESOURCE:		return "When saving context identified resource type does not match actual resource";
			case TPM_E_NOTFIPS:			return "TPM is attempting to execute a command only available when in FIPS mode";
			case TPM_E_INVALID_FAMILY:		return "Command is attempting to use an invalid family ID";
			case TPM_E_NO_NV_PERMISSION:		return "Permission to manipulate the NV storage is not available";
			case TPM_E_REQUIRES_SIGN:		return "Operation requires a signed command";
			case TPM_E_KEY_NOTSUPPORTED:		return "Wrong operation to load an NV key";
			case TPM_E_AUTH_CONFLICT:		return "NV_LoadKey blob requires both owner and blob authorization";
			case TPM_E_AREA_LOCKED:			return "NV area is locked and not writable";
			case TPM_E_BAD_LOCALITY:		return "Locality is incorrect for attempted operation";
			case TPM_E_READ_ONLY:			return "NV area is read only and can't be written to";
			case TPM_E_PER_NOWRITE:			return "There is no protection on write to NV area";
			case TPM_E_FAMILYCOUNT:			return "Family count value does not match";
			case TPM_E_WRITE_LOCKED:		return "NV area has already been written to";
			case TPM_E_BAD_ATTRIBUTES:		return "NV area attributes conflict";
			case TPM_E_INVALID_STRUCTURE:		return "Structure tag and version are invalid or inconsistent";
			case TPM_E_KEY_OWNER_CONTROL:		return "Key is under control of TPM Owner and can only be evicted by TPM Owner";
			case TPM_E_BAD_COUNTER:			return "Counter handle is incorrect";
			case TPM_E_NOT_FULLWRITE:		return "Write is not a complete write of area";
			case TPM_E_CONTEXT_GAP:			return "Gap between saved context counts is too large";
			case TPM_E_MAXNVWRITES:			return "Maximum number of NV writes without an owner has been exceeded";
			case TPM_E_NOOPERATOR:			return "No operator AuthData value is set";
			case TPM_E_RESOURCEMISSING:		return "Resource pointed to by context is not loaded";
			case TPM_E_DELEGATE_LOCK:		return "Delegate administration is locked";
			case TPM_E_DELEGATE_FAMILY:		return "Attempt to manage a family other then delegated family";
			case TPM_E_DELEGATE_ADMIN:		return "Delegation table management not enabled";
			case TPM_E_TRANSPORT_NOTEXCLUSIVE:	return "A command was executed outside of an exclusive transport session";
			case TPM_E_OWNER_CONTROL:		return "Attempt to context save an owner evict-controlled key";
			case TPM_E_DAA_RESOURCES:		return "DAA command has no resources available to execute command";
			case TPM_E_DAA_INPUT_DATA0:		return "Consistency check on DAA parameter inputData0 has failed";
			case TPM_E_DAA_INPUT_DATA1:		return "Consistency check on DAA parameter inputData1 has failed";
			case TPM_E_DAA_ISSUER_SETTINGS:		return "Consistency check on DAA_issuerSettings has failed";
			case TPM_E_DAA_TPM_SETTINGS:		return "Consistency check on DAA_tpmSpecific has failed";
			case TPM_E_DAA_STAGE:			return "Atomic process indicated by submitted DAA command is not expected process";
			case TPM_E_DAA_ISSUER_VALIDITY:		return "Issuer's validity check has detected an inconsistency";
			case TPM_E_DAA_WRONG_W:			return "Consistency check on w has failed";
			case TPM_E_BAD_HANDLE:			return "Handle is incorrect";
			case TPM_E_BAD_DELEGATE:		return "Delegation is not correct";
			case TPM_E_BADCONTEXT:			return "Context blob is invalid";
			case TPM_E_TOOMANYCONTEXTS:		return "Too many contexts held by TPM";
			case TPM_E_MA_TICKET_SIGNATURE:		return "Migration authority signature validation failure";
			case TPM_E_MA_DESTINATION:		return "Migration destination not authenticated";
			case TPM_E_MA_SOURCE:			return "Migration source incorrect";
			case TPM_E_MA_AUTHORITY:		return "Incorrect migration authority";
			case TPM_E_PERMANENTEK:			return "Attempt to revoke EK but EK is not revocable";
			case TPM_E_BAD_SIGNATURE:		return "Bad signature of CMK ticket";
			case TPM_E_NOCONTEXTSPACE:		return "No room in context list for additional contexts";
			case TPM_E_RETRY:			return "TPM busy: Retry command at a later time";
			case TPM_E_NEEDS_SELFTEST:		return "SelfTestFull has not been run";
			case TPM_E_DOING_SELFTEST:		return "TPM is currently executing a full selftest";
			case TPM_E_DEFEND_LOCK_RUNNING:		return "TPM is defending against dictionary attacks and is in some time-out period";
			case TPM_E_DISABLED_CMD:		return "The TPM target command has been disabled";
			default:				return "Unknown error";
		}
	} else if (TSS_ERROR_LAYER(r) == TSS_LAYER_TDDL) {
		switch (TSS_ERROR_CODE(r)) {
			case TSS_E_FAIL:			return "General failure";
			case TSS_E_BAD_PARAMETER:		return "Bad parameter";
			case TSS_E_INTERNAL_ERROR:		return "Internal software error";
			case TSS_E_NOTIMPL:			return "Not implemented";
			case TSS_E_PS_KEY_NOTFOUND:		return "Key not found in persistent storage";
			case TSS_E_KEY_ALREADY_REGISTERED:	return "UUID already registered";
			case TSS_E_CANCELED:			return "The action was cancelled by request";
			case TSS_E_TIMEOUT:			return "The operation has timed out";
			case TSS_E_OUTOFMEMORY:			return "Out of memory";
			case TSS_E_TPM_UNEXPECTED:		return "Unexpected TPM output";
			case TSS_E_COMM_FAILURE:		return "Communication failure";
			case TSS_E_TPM_UNSUPPORTED_FEATURE:	return "Unsupported feature";
			case TDDL_E_COMPONENT_NOT_FOUND:	return "Connection to TPM device failed";
			case TDDL_E_ALREADY_OPENED:		return "Device already opened";
			case TDDL_E_BADTAG:			return "Invalid or unsupported capability";
			case TDDL_E_INSUFFICIENT_BUFFER:	return "Receive buffer too small";
			case TDDL_E_COMMAND_COMPLETED:		return "Command has already completed";
			case TDDL_E_COMMAND_ABORTED:		return "TPM aborted processing of command";
			case TDDL_E_ALREADY_CLOSED:		return "Device driver already closed";
			case TDDL_E_IOERROR:			return "I/O error";
			default:				return "Unknown";
		}
	} else if (TSS_ERROR_LAYER(r) == TSS_LAYER_TCS) {
		switch (TSS_ERROR_CODE(r)) {
			case TSS_E_FAIL:			return "General failure";
			case TSS_E_BAD_PARAMETER:		return "Bad parameter";
			case TSS_E_INTERNAL_ERROR:		return "Internal software error";
			case TSS_E_NOTIMPL:			return "Not implemented";
			case TSS_E_PS_KEY_NOTFOUND:		return "Key not found in persistent storage";
			case TSS_E_KEY_ALREADY_REGISTERED:	return "UUID already registered";
			case TSS_E_CANCELED:			return "The action was cancelled by request";
			case TSS_E_TIMEOUT:			return "The operation has timed out";
			case TSS_E_OUTOFMEMORY:			return "Out of memory";
			case TSS_E_TPM_UNEXPECTED:		return "Unexpected TPM output";
			case TSS_E_COMM_FAILURE:		return "Communication failure";
			case TSS_E_TPM_UNSUPPORTED_FEATURE:	return "Unsupported feature";
			case TCS_E_KEY_MISMATCH:		return "UUID does not match key handle";
			case TCS_E_KM_LOADFAILED:		return "Key load failed: parent key requires authorization";
			case TCS_E_KEY_CONTEXT_RELOAD:		return "Reload of key context failed";
			case TCS_E_BAD_INDEX:			return "Bad memory index";
			case TCS_E_INVALID_CONTEXTHANDLE:	return "Invalid context handle";
			case TCS_E_INVALID_KEYHANDLE:		return "Invalid key handle";
			case TCS_E_INVALID_AUTHHANDLE:		return "Invalid authorization session handle";
			case TCS_E_INVALID_AUTHSESSION:		return "Authorization session has been closed by TPM";
			case TCS_E_INVALID_KEY:			return "Invalid key";
			default:				return "Unknown";
		}
	} else {
		switch (TSS_ERROR_CODE(r)) {
			case TSS_E_FAIL:			return "General failure";
			case TSS_E_BAD_PARAMETER:		return "Bad parameter";
			case TSS_E_INTERNAL_ERROR:		return "Internal software error";
			case TSS_E_NOTIMPL:			return "Not implemented";
			case TSS_E_PS_KEY_NOTFOUND:		return "Key not found in persistent storage";
			case TSS_E_KEY_ALREADY_REGISTERED:	return "UUID already registered";
			case TSS_E_CANCELED:			return "The action was cancelled by request";
			case TSS_E_TIMEOUT:			return "The operation has timed out";
			case TSS_E_OUTOFMEMORY:			return "Out of memory";
			case TSS_E_TPM_UNEXPECTED:		return "Unexpected TPM output";
			case TSS_E_COMM_FAILURE:		return "Communication failure";
			case TSS_E_TPM_UNSUPPORTED_FEATURE:	return "Unsupported feature";
			case TSS_E_INVALID_OBJECT_TYPE:		return "Object type not valid for this operation";
			case TSS_E_INVALID_OBJECT_INITFLAG:	return "Wrong flag information for object creation";
			case TSS_E_INVALID_HANDLE:		return "Invalid handle";
			case TSS_E_NO_CONNECTION:		return "Core service connection doesn't exist";
			case TSS_E_CONNECTION_FAILED:		return "Core service connection failed";
			case TSS_E_CONNECTION_BROKEN:		return "Communication with core services failed";
			case TSS_E_HASH_INVALID_ALG:		return "Invalid hash algorithm";
			case TSS_E_HASH_INVALID_LENGTH:		return "Hash length is inconsistent with algorithm";
			case TSS_E_HASH_NO_DATA:		return "Hash object has no internal hash value";
			case TSS_E_SILENT_CONTEXT:		return "A silent context requires user input";
			case TSS_E_INVALID_ATTRIB_FLAG:		return "Flag value for attrib-functions inconsistent";
			case TSS_E_INVALID_ATTRIB_SUBFLAG:	return "Sub-flag value for attrib-functions inconsistent";
			case TSS_E_INVALID_ATTRIB_DATA:		return "Data for attrib-functions invalid";
			case TSS_E_NO_PCRS_SET:			return "No PCR registers are selected or set";
			case TSS_E_KEY_NOT_LOADED:		return "The addressed key is not currently loaded";
			case TSS_E_KEY_NOT_SET:			return "No key informatio is currently available";
			case TSS_E_VALIDATION_FAILED:		return "Internal validation of data failed";
			case TSS_E_TSP_AUTHREQUIRED:		return "Authorization is required";
			case TSS_E_TSP_AUTH2REQUIRED:		return "Multiple authorizations are required";
			case TSS_E_TSP_AUTHFAIL:		return "Authorization failed";
			case TSS_E_TSP_AUTH2FAIL:		return "Multiple authorization failed";
			case TSS_E_KEY_NO_MIGRATION_POLICY:	return "Addressed key has no migration policy";
			case TSS_E_POLICY_NO_SECRET:		return "No secret information available for the address policy";
			case TSS_E_INVALID_OBJ_ACCESS:		return "Accessed object is in an inconsistent state";
			case TSS_E_INVALID_ENCSCHEME:		return "Invalid encryption scheme";
			case TSS_E_INVALID_SIGSCHEME:		return "Invalid signature scheme";
			case TSS_E_ENC_INVALID_LENGTH:		return "Invalid length for encrypted data object";
			case TSS_E_ENC_NO_DATA:			return "Encrypted data object contains no data";
			case TSS_E_ENC_INVALID_TYPE:		return "Invalid type for encrypted data object";
			case TSS_E_INVALID_KEYUSAGE:		return "Invalid usage of key";
			case TSS_E_VERIFICATION_FAILED:		return "Internal validation of data failed";
			case TSS_E_HASH_NO_IDENTIFIER:		return "Hash algorithm identifier not set";
			case TSS_E_NV_AREA_EXIST:		return "NVRAM area already exists";
			case TSS_E_NV_AREA_NOT_EXIST:		return "NVRAM area does not exist";
			default:				return "Unknown";
		}
	}
}

char *
Trspi_Error_Layer(TSS_RESULT r)
{
	switch (TSS_ERROR_LAYER(r)) {
		case TSS_LAYER_TPM:	return "tpm";
		case TSS_LAYER_TDDL:	return "tddl";
		case TSS_LAYER_TCS:	return "tcs";
		case TSS_LAYER_TSP:	return "tsp";
		default:		return "unknown";
	}
}

TSS_RESULT
Trspi_Error_Code(TSS_RESULT r)
{
	return TSS_ERROR_CODE(r);
}

static int
hacky_strlen(char *codeset, BYTE *string)
{
	BYTE *ptr = string;
	int len = 0;

	if (strcmp("UTF-16", codeset) == 0) {
		while (!(ptr[0] == '\0' && ptr[1] == '\0')) {
			len += 2;
			ptr += 2;
		}
	} else if (strcmp("UTF-32", codeset) == 0) {
		while (!(ptr[0] == '\0' && ptr[1] == '\0' &&
			 ptr[2] == '\0' && ptr[3] == '\0')) {
			len += 4;
			ptr += 4;
		}
	} else {
		/* default to 8bit chars */
		while (*ptr++ != '\0') {
			len++;
		}
	}

	return len;
}

static inline int
char_width(char *codeset)
{
	if (strcmp("UTF-16", codeset) == 0) {
		return 2;
	} else if (strcmp("UTF-32", codeset) == 0) {
		return 4;
	}

	return 1;
}

#define MAX_BUF_SIZE	4096

BYTE *
Trspi_Native_To_UNICODE(BYTE *string, unsigned *size)
{
	char *ret, *outbuf, tmpbuf[MAX_BUF_SIZE] = { 0, };
	BSD_CONST char *ptr;
	unsigned len = 0, tmplen;
	iconv_t cd = 0;
	size_t rc, outbytesleft, inbytesleft;

	if (string == NULL)
		goto alloc_string;

	if ((cd = iconv_open("UTF-16LE", nl_langinfo(CODESET))) == (iconv_t)-1) {
		LogDebug("iconv_open: %s", strerror(errno));
		return NULL;
	}

	if ((tmplen = hacky_strlen(nl_langinfo(CODESET), string)) == 0) {
		LogDebug("hacky_strlen returned 0");
		goto alloc_string;
	}

	do {
		len++;
		outbytesleft = len;
		inbytesleft = tmplen;
		outbuf = tmpbuf;
		ptr = (char *)string;
		errno = 0;

		rc = iconv(cd, (BSD_CONST char **)&ptr, &inbytesleft, &outbuf, &outbytesleft);
	} while (rc == (size_t)-1 && errno == E2BIG);

	if (len > MAX_BUF_SIZE) {
		LogDebug("string too long.");
		iconv_close(cd);
		return NULL;
	}

alloc_string:
	/* add terminating bytes of the correct width */
	len += char_width("UTF-16");
	if ((ret = calloc(1, len)) == NULL) {
		LogDebug("malloc of %u bytes failed.", len);
		iconv_close(cd);
		return NULL;
	}

	memcpy(ret, &tmpbuf, len);
	if (size)
		*size = len;

	if (cd)
		iconv_close(cd);

	return (BYTE *)ret;

}

BYTE *
Trspi_UNICODE_To_Native(BYTE *string, unsigned *size)
{
	char *ret, *outbuf, tmpbuf[MAX_BUF_SIZE] = { 0, };
	BSD_CONST char *ptr;
	unsigned len = 0, tmplen;
	iconv_t cd;
	size_t rc, outbytesleft, inbytesleft;

	if (string == NULL) {
		if (size)
			*size = 0;
		return NULL;
	}

	if ((cd = iconv_open(nl_langinfo(CODESET), "UTF-16LE")) == (iconv_t)-1) {
		LogDebug("iconv_open: %s", strerror(errno));
		return NULL;
	}

	if ((tmplen = hacky_strlen("UTF-16", string)) == 0) {
		LogDebug("hacky_strlen returned 0");
		iconv_close(cd);
		return 0;
	}

	do {
		len++;
		outbytesleft = len;
		inbytesleft = tmplen;
		outbuf = tmpbuf;
		ptr = (char *)string;
		errno = 0;

		rc = iconv(cd, (BSD_CONST char **)&ptr, &inbytesleft, &outbuf, &outbytesleft);
	} while (rc == (size_t)-1 && errno == E2BIG);

	/* add terminating bytes of the correct width */
	len += char_width(nl_langinfo(CODESET));
	if (len > MAX_BUF_SIZE) {
		LogDebug("string too long.");
		iconv_close(cd);
		return NULL;
	}

	if ((ret = calloc(1, len)) == NULL) {
		LogDebug("malloc of %d bytes failed.", len);
		iconv_close(cd);
		return NULL;
	}

	memcpy(ret, &tmpbuf, len);
	if (size)
		*size = len;
	iconv_close(cd);

	return (BYTE *)ret;
}

/* Functions to support incremental hashing */
TSS_RESULT
Trspi_Hash_UINT16(Trspi_HashCtx *c, UINT16 i)
{
	BYTE bytes[sizeof(UINT16)];

	UINT16ToArray(i, bytes);
	return Trspi_HashUpdate(c, sizeof(UINT16), bytes);
}

TSS_RESULT
Trspi_Hash_UINT32(Trspi_HashCtx *c, UINT32 i)
{
	BYTE bytes[sizeof(UINT32)];

	UINT32ToArray(i, bytes);
	return Trspi_HashUpdate(c, sizeof(UINT32), bytes);
}

TSS_RESULT
Trspi_Hash_UINT64(Trspi_HashCtx *c, UINT64 i)
{
	BYTE bytes[sizeof(UINT64)];

	UINT64ToArray(i, bytes);
	return Trspi_HashUpdate(c, sizeof(UINT64), bytes);
}

TSS_RESULT
Trspi_Hash_BYTE(Trspi_HashCtx *c, BYTE data)
{
	return Trspi_HashUpdate(c, sizeof(BYTE), &data);
}

TSS_RESULT
Trspi_Hash_BOOL(Trspi_HashCtx *c, TSS_BOOL data)
{
	return Trspi_HashUpdate(c, (UINT32)sizeof(TSS_BOOL), (BYTE *)&data);
}

TSS_RESULT
Trspi_Hash_VERSION(Trspi_HashCtx *c, TSS_VERSION *version)
{
	TSS_RESULT result;

	result = Trspi_Hash_BYTE(c, version->bMajor);
	result |= Trspi_Hash_BYTE(c, version->bMinor);
	result |= Trspi_Hash_BYTE(c, version->bRevMajor);
	result |= Trspi_Hash_BYTE(c, version->bRevMinor);

	return result;
}

TSS_RESULT
Trspi_Hash_DAA_PK(Trspi_HashCtx *c, TSS_DAA_PK *pk)
{
	UINT32 i;
	TSS_RESULT result;

	result = Trspi_Hash_VERSION(c, &pk->versionInfo);

	result |= Trspi_Hash_UINT32(c, pk->modulusLength);
	result |= Trspi_HashUpdate(c, pk->modulusLength, pk->modulus);

	result |= Trspi_Hash_UINT32(c, pk->capitalSLength);
	result |= Trspi_HashUpdate(c, pk->capitalSLength, pk->capitalS);

	result |= Trspi_Hash_UINT32(c, pk->capitalZLength);
	result |= Trspi_HashUpdate(c, pk->capitalZLength, pk->capitalZ);

	result |= Trspi_Hash_UINT32(c, pk->capitalR0Length);
	result |= Trspi_HashUpdate(c, pk->capitalR0Length, pk->capitalR0);

	result |= Trspi_Hash_UINT32(c, pk->capitalR1Length);
	result |= Trspi_HashUpdate(c, pk->capitalR1Length, pk->capitalR1);

	result |= Trspi_Hash_UINT32(c, pk->gammaLength);
	result |= Trspi_HashUpdate(c, pk->gammaLength, pk->gamma);

	result |= Trspi_Hash_UINT32(c, pk->capitalGammaLength);
	result |= Trspi_HashUpdate(c, pk->capitalGammaLength, pk->capitalGamma);

	result |= Trspi_Hash_UINT32(c, pk->rhoLength);
	result |= Trspi_HashUpdate(c, pk->rhoLength, pk->rho);

	for (i = 0; i < pk->capitalYLength; i++)
		result |= Trspi_HashUpdate(c, pk->capitalYLength2, pk->capitalY[i]);

	result |= Trspi_Hash_UINT32(c, pk->capitalYPlatformLength);

	result |= Trspi_Hash_UINT32(c, pk->issuerBaseNameLength);
	result |= Trspi_HashUpdate(c, pk->issuerBaseNameLength, pk->issuerBaseName);

	return result;
}

TSS_RESULT
Trspi_Hash_RSA_KEY_PARMS(Trspi_HashCtx *c, TCPA_RSA_KEY_PARMS *parms)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, parms->keyLength);
	result |= Trspi_Hash_UINT32(c, parms->numPrimes);
	result |= Trspi_Hash_UINT32(c, parms->exponentSize);

	if (parms->exponentSize > 0)
		result |= Trspi_HashUpdate(c, parms->exponentSize, parms->exponent);

	return result;
}

TSS_RESULT
Trspi_Hash_STORE_PUBKEY(Trspi_HashCtx *c, TCPA_STORE_PUBKEY *store)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, store->keyLength);
	result |= Trspi_HashUpdate(c, store->keyLength, store->key);

	return result;
}

TSS_RESULT
Trspi_Hash_KEY_PARMS(Trspi_HashCtx *c, TCPA_KEY_PARMS *keyInfo)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, keyInfo->algorithmID);
	result |= Trspi_Hash_UINT16(c, keyInfo->encScheme);
	result |= Trspi_Hash_UINT16(c, keyInfo->sigScheme);
	result |= Trspi_Hash_UINT32(c, keyInfo->parmSize);

	if (keyInfo->parmSize > 0)
		result |= Trspi_HashUpdate(c, keyInfo->parmSize, keyInfo->parms);

	return result;
}

TSS_RESULT
Trspi_Hash_PUBKEY(Trspi_HashCtx *c, TCPA_PUBKEY *pubKey)
{
	TSS_RESULT result;

	result = Trspi_Hash_KEY_PARMS(c, &pubKey->algorithmParms);
	result |= Trspi_Hash_STORE_PUBKEY(c, &pubKey->pubKey);

	return result;
}

TSS_RESULT
Trspi_Hash_STORED_DATA(Trspi_HashCtx *c, TCPA_STORED_DATA *data)
{
	TSS_RESULT result;

	result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&data->ver);
	result |= Trspi_Hash_UINT32(c, data->sealInfoSize);
	result |= Trspi_HashUpdate(c, data->sealInfoSize, data->sealInfo);
	result |= Trspi_Hash_UINT32(c, data->encDataSize);
	result |= Trspi_HashUpdate(c, data->encDataSize, data->encData);

	return result;
}

TSS_RESULT
Trspi_Hash_PCR_SELECTION(Trspi_HashCtx *c, TCPA_PCR_SELECTION *pcr)
{
	TSS_RESULT result;
	UINT16 i;

	result = Trspi_Hash_UINT16(c, pcr->sizeOfSelect);

	for (i = 0; i < pcr->sizeOfSelect; i++)
		result |= Trspi_Hash_BYTE(c, pcr->pcrSelect[i]);

	return result;
}

TSS_RESULT
Trspi_Hash_KEY_FLAGS(Trspi_HashCtx *c, TCPA_KEY_FLAGS *flags)
{
	return Trspi_Hash_UINT32(c, *flags);
}

TSS_RESULT
Trspi_Hash_KEY12(Trspi_HashCtx *c, TPM_KEY12 *key)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, key->tag);
	result |= Trspi_Hash_UINT16(c, key->fill);
	result |= Trspi_Hash_UINT16(c, key->keyUsage);
	result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
	result |= Trspi_Hash_BYTE(c, key->authDataUsage);
	result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);
	result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
	result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);
	result |= Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
	result |= Trspi_Hash_UINT32(c, key->encSize);
	result |= Trspi_HashUpdate(c, key->encSize, key->encData);

	return result;
}

TSS_RESULT
Trspi_Hash_KEY(Trspi_HashCtx *c, TCPA_KEY *key)
{
	TSS_RESULT result;

	result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&key->ver);
	result |= Trspi_Hash_UINT16(c, key->keyUsage);
	result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
	result |= Trspi_Hash_BYTE(c, key->authDataUsage);
	result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);
	result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
	result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);
	result |= Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
	result |= Trspi_Hash_UINT32(c, key->encSize);
	result |= Trspi_HashUpdate(c, key->encSize, key->encData);

	return result;
}

TSS_RESULT
Trspi_Hash_UUID(Trspi_HashCtx *c, TSS_UUID uuid)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, uuid.ulTimeLow);
	result |= Trspi_Hash_UINT16(c, uuid.usTimeMid);
	result |= Trspi_Hash_UINT16(c, uuid.usTimeHigh);
	result |= Trspi_Hash_BYTE(c, uuid.bClockSeqHigh);
	result |= Trspi_Hash_BYTE(c, uuid.bClockSeqLow);
	result |= Trspi_HashUpdate(c, sizeof(uuid.rgbNode), uuid.rgbNode);

	return result;
}

TSS_RESULT
Trspi_Hash_PCR_EVENT(Trspi_HashCtx *c, TSS_PCR_EVENT *event)
{
	TSS_RESULT result;

	result = Trspi_Hash_VERSION(c, &event->versionInfo);
	result |= Trspi_Hash_UINT32(c, event->ulPcrIndex);
	result |= Trspi_Hash_UINT32(c, event->eventType);

	Trspi_Hash_UINT32(c, event->ulPcrValueLength);
	if (event->ulPcrValueLength > 0)
		result |= Trspi_HashUpdate(c, event->ulPcrValueLength, event->rgbPcrValue);

	result |= Trspi_Hash_UINT32(c, event->ulEventLength);
	if (event->ulEventLength > 0)
		result |= Trspi_HashUpdate(c, event->ulEventLength, event->rgbEvent);


	return result;
}

TSS_RESULT
Trspi_Hash_PRIVKEY_DIGEST12(Trspi_HashCtx *c, TPM_KEY12 *key)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, key->tag);
	result |= Trspi_Hash_UINT16(c, key->fill);
	result |= Trspi_Hash_UINT16(c, key->keyUsage);
	result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
	result |= Trspi_Hash_BYTE(c, key->authDataUsage);
	result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);

	result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
	/* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
	if (key->PCRInfoSize != 0)
		result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);

	Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
	/* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */

	return result;
}

TSS_RESULT
Trspi_Hash_PRIVKEY_DIGEST(Trspi_HashCtx *c, TCPA_KEY *key)
{
	TSS_RESULT result;

	result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&key->ver);
	result |= Trspi_Hash_UINT16(c, key->keyUsage);
	result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
	result |= Trspi_Hash_BYTE(c, key->authDataUsage);
	result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);

	result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
	/* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
	if (key->PCRInfoSize != 0)
		result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);

	Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
	/* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */

	return result;
}

TSS_RESULT
Trspi_Hash_SYMMETRIC_KEY(Trspi_HashCtx *c, TCPA_SYMMETRIC_KEY *key)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, key->algId);
	result |= Trspi_Hash_UINT16(c, key->encScheme);
	result |= Trspi_Hash_UINT16(c, key->size);

	if (key->size > 0)
		result |= Trspi_HashUpdate(c, key->size, key->data);

	return result;
}

TSS_RESULT
Trspi_Hash_IDENTITY_REQ(Trspi_HashCtx *c, TCPA_IDENTITY_REQ *req)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, req->asymSize);
	result |= Trspi_Hash_UINT32(c, req->symSize);
	result |= Trspi_Hash_KEY_PARMS(c, &req->asymAlgorithm);
	result |= Trspi_Hash_KEY_PARMS(c, &req->symAlgorithm);
	result |= Trspi_HashUpdate(c, req->asymSize, req->asymBlob);
	result |= Trspi_HashUpdate(c, req->symSize, req->symBlob);

	return result;
}

TSS_RESULT
Trspi_Hash_CHANGEAUTH_VALIDATE(Trspi_HashCtx *c, TPM_CHANGEAUTH_VALIDATE *caValidate)
{
	TSS_RESULT result;

	result = Trspi_HashUpdate(c, TCPA_SHA1_160_HASH_LEN, caValidate->newAuthSecret.authdata);
	result |= Trspi_HashUpdate(c, TCPA_SHA1_160_HASH_LEN, caValidate->n1.nonce);

	return result;
}

TSS_RESULT
Trspi_Hash_SYM_CA_ATTESTATION(Trspi_HashCtx *c, TCPA_SYM_CA_ATTESTATION *sym)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, sym->credSize);
	result |= Trspi_Hash_KEY_PARMS(c, &sym->algorithm);
	result |= Trspi_HashUpdate(c, sym->credSize, sym->credential);

	return result;
}

TSS_RESULT
Trspi_Hash_ASYM_CA_CONTENTS(Trspi_HashCtx *c, TCPA_ASYM_CA_CONTENTS *asym)
{
	TSS_RESULT result;

	result = Trspi_Hash_SYMMETRIC_KEY(c, &asym->sessionKey);
	result |= Trspi_HashUpdate(c, TCPA_SHA1_160_HASH_LEN, (BYTE *)&asym->idDigest);

	return result;
}

TSS_RESULT
Trspi_Hash_BOUND_DATA(Trspi_HashCtx *c, TCPA_BOUND_DATA *bd, UINT32 payloadLength)
{
	TSS_RESULT result;

	result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&bd->ver);
	result |= Trspi_Hash_BYTE(c, bd->payload);
	result |= Trspi_HashUpdate(c, payloadLength, bd->payloadData);

	return result;
}

TSS_RESULT
Trspi_Hash_TRANSPORT_AUTH(Trspi_HashCtx *c, TPM_TRANSPORT_AUTH *a)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, a->tag);
	result |= Trspi_HashUpdate(c, TPM_SHA1_160_HASH_LEN, a->authData.authdata);

	return result;
}

TSS_RESULT
Trspi_Hash_TRANSPORT_LOG_IN(Trspi_HashCtx *c, TPM_TRANSPORT_LOG_IN *l)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, l->tag);
	result |= Trspi_Hash_DIGEST(c, l->parameters.digest);
	result |= Trspi_Hash_DIGEST(c, l->pubKeyHash.digest);

	return result;
}

TSS_RESULT
Trspi_Hash_TRANSPORT_LOG_OUT(Trspi_HashCtx *c, TPM_TRANSPORT_LOG_OUT *l)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, l->tag);
	result |= Trspi_Hash_CURRENT_TICKS(c, &l->currentTicks);
	result |= Trspi_Hash_DIGEST(c, l->parameters.digest);
	result |= Trspi_Hash_UINT32(c, l->locality);

	return result;
}

TSS_RESULT
Trspi_Hash_CURRENT_TICKS(Trspi_HashCtx *c, TPM_CURRENT_TICKS *t)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, t->tag);
	result |= Trspi_Hash_UINT64(c, t->currentTicks);
	result |= Trspi_Hash_UINT16(c, t->tickRate);
	result |= Trspi_Hash_NONCE(c, t->tickNonce.nonce);

	return result;
}

TSS_RESULT
Trspi_Hash_SIGN_INFO(Trspi_HashCtx *c, TPM_SIGN_INFO *s)
{
	TSS_RESULT result;

	result = Trspi_Hash_UINT16(c, s->tag);
	result |= Trspi_HashUpdate(c, 4, s->fixed);
	result |= Trspi_Hash_NONCE(c, s->replay.nonce);
	result |= Trspi_Hash_UINT32(c, s->dataLen);
	result |= Trspi_HashUpdate(c, s->dataLen, s->data);

	return result;
}

void
Trspi_UnloadBlob_COUNTER_VALUE(UINT64 *offset, BYTE *blob, TPM_COUNTER_VALUE *ctr)
{
	if (!ctr) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		/* '4' is hard-coded in the spec */
		Trspi_UnloadBlob(offset, 4, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_UINT16(offset, &ctr->tag, blob);
	/* '4' is hard-coded in the spec */
	Trspi_UnloadBlob(offset, 4, blob, (BYTE *)&ctr->label);
	Trspi_UnloadBlob_UINT32(offset, &ctr->counter, blob);
}

void
Trspi_LoadBlob_COUNTER_VALUE(UINT64 *offset, BYTE *blob, TPM_COUNTER_VALUE *ctr)
{
	Trspi_LoadBlob_UINT16(offset, ctr->tag, blob);
	Trspi_LoadBlob(offset, 4, blob, (BYTE *)&ctr->label);
	Trspi_LoadBlob_UINT32(offset, ctr->counter, blob);
}

void
Trspi_UnloadBlob_CURRENT_TICKS(UINT64 *offset, BYTE *blob, TPM_CURRENT_TICKS *ticks)
{
	if (!ticks) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT64(offset, NULL, blob);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob(offset, sizeof(TPM_NONCE), blob, NULL);

		return;
	}

	Trspi_UnloadBlob_UINT16(offset, &ticks->tag, blob);
	Trspi_UnloadBlob_UINT64(offset, &ticks->currentTicks, blob);
	Trspi_UnloadBlob_UINT16(offset, &ticks->tickRate, blob);
	Trspi_UnloadBlob(offset, sizeof(TPM_NONCE), blob, (BYTE *)&ticks->tickNonce);
}

void
Trspi_UnloadBlob_TRANSPORT_PUBLIC(UINT64 *offset, BYTE *blob, TPM_TRANSPORT_PUBLIC *t)
{
	Trspi_UnloadBlob_UINT16(offset, &t->tag, blob);
	Trspi_UnloadBlob_UINT32(offset, &t->transAttributes, blob);
	Trspi_UnloadBlob_UINT32(offset, &t->algId, blob);
	Trspi_UnloadBlob_UINT16(offset, &t->encScheme, blob);
}

void
Trspi_LoadBlob_TRANSPORT_PUBLIC(UINT64 *offset, BYTE *blob, TPM_TRANSPORT_PUBLIC *t)
{
	Trspi_LoadBlob_UINT16(offset, t->tag, blob);
	Trspi_LoadBlob_UINT32(offset, t->transAttributes, blob);
	Trspi_LoadBlob_UINT32(offset, t->algId, blob);
	Trspi_LoadBlob_UINT16(offset, t->encScheme, blob);
}

void
Trspi_LoadBlob_TRANSPORT_AUTH(UINT64 *offset, BYTE *blob, TPM_TRANSPORT_AUTH *t)
{
	Trspi_LoadBlob_UINT16(offset, t->tag, blob);
	Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, t->authData.authdata);
}

void
Trspi_LoadBlob_SIGN_INFO(UINT64 *offset, BYTE *blob, TPM_SIGN_INFO *s)
{
	Trspi_LoadBlob_UINT16(offset, s->tag, blob);
	Trspi_LoadBlob(offset, 4, blob, s->fixed);
	Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, s->replay.nonce);
	Trspi_LoadBlob_UINT32(offset, s->dataLen, blob);
	Trspi_LoadBlob(offset, s->dataLen, blob, s->data);
}

TSS_RESULT
Trspi_UnloadBlob_CERTIFY_INFO(UINT64 *offset, BYTE *blob, TPM_CERTIFY_INFO *c)
{
	TSS_RESULT result;

	if (!c) {
		UINT32 pcrInfoSize;

		Trspi_UnloadBlob_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
		Trspi_UnloadBlob_NONCE(offset, blob, NULL);
		Trspi_UnloadBlob_BOOL(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, &pcrInfoSize, blob);

		(*offset) += pcrInfoSize;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_VERSION(offset, blob, &c->version);
	Trspi_UnloadBlob_UINT16(offset, &c->keyUsage, blob);
	Trspi_UnloadBlob_UINT32(offset, &c->keyFlags, blob);
	Trspi_UnloadBlob_BYTE(offset, &c->authDataUsage, blob);
	if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &c->algorithmParms)))
		return result;
	Trspi_UnloadBlob_DIGEST(offset, blob, &c->pubkeyDigest);
	Trspi_UnloadBlob_NONCE(offset, blob, &c->data);
	Trspi_UnloadBlob_BOOL(offset, (TSS_BOOL *)&c->parentPCRStatus, blob);
	Trspi_UnloadBlob_UINT32(offset, &c->PCRInfoSize, blob);
        if (c->PCRInfoSize != 0) {
                c->PCRInfo = malloc(sizeof(TPM_PCR_INFO));
                if (c->PCRInfo == NULL) {
                        LogError("malloc of %zd bytes failed.", sizeof(TPM_PCR_INFO));
                        return TSPERR(TSS_E_OUTOFMEMORY);
                }
        } else {
                c->PCRInfo = NULL;
        }
        Trspi_UnloadBlob_PCR_INFO(offset, blob, (TPM_PCR_INFO *)c->PCRInfo);

	return TSS_SUCCESS;
}

void
Trspi_UnloadBlob_TPM_FAMILY_LABEL(UINT64 *offset, BYTE *blob, TPM_FAMILY_LABEL *label)
{
	if (!label) {
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_BYTE(offset, &label->label, blob);
}

void
Trspi_LoadBlob_TPM_FAMILY_LABEL(UINT64 *offset, BYTE *blob, TPM_FAMILY_LABEL *label)
{
	Trspi_LoadBlob_BYTE(offset, label->label, blob);
}

void
Trspi_UnloadBlob_TPM_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TPM_FAMILY_TABLE_ENTRY *entry)
{
	if (!entry) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_TPM_FAMILY_LABEL(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_UINT16(offset, &entry->tag, blob);
	Trspi_UnloadBlob_TPM_FAMILY_LABEL(offset, blob, &entry->label);
	Trspi_UnloadBlob_UINT32(offset, &entry->familyID, blob);
	Trspi_UnloadBlob_UINT32(offset, &entry->verificationCount, blob);
	Trspi_UnloadBlob_UINT32(offset, &entry->flags, blob);
}

void
Trspi_LoadBlob_TPM_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TPM_FAMILY_TABLE_ENTRY *entry)
{
	Trspi_LoadBlob_UINT16(offset, entry->tag, blob);
	Trspi_LoadBlob_TPM_FAMILY_LABEL(offset, blob, &entry->label);
	Trspi_LoadBlob_UINT32(offset, entry->familyID, blob);
	Trspi_LoadBlob_UINT32(offset, entry->verificationCount, blob);
	Trspi_LoadBlob_UINT32(offset, entry->flags, blob);
}

void
Trspi_UnloadBlob_TPM_DELEGATE_LABEL(UINT64 *offset, BYTE *blob, TPM_DELEGATE_LABEL *label)
{
	if (!label) {
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_BYTE(offset, &label->label, blob);
}

void
Trspi_LoadBlob_TPM_DELEGATE_LABEL(UINT64 *offset, BYTE *blob, TPM_DELEGATE_LABEL *label)
{
	Trspi_LoadBlob_BYTE(offset, label->label, blob);
}

void
Trspi_UnloadBlob_TPM_DELEGATIONS(UINT64 *offset, BYTE *blob, TPM_DELEGATIONS *delegations)
{
	if (!delegations) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_UINT16(offset, &delegations->tag, blob);
	Trspi_UnloadBlob_UINT32(offset, &delegations->delegateType, blob);
	Trspi_UnloadBlob_UINT32(offset, &delegations->per1, blob);
	Trspi_UnloadBlob_UINT32(offset, &delegations->per2, blob);
}

void
Trspi_LoadBlob_TPM_DELEGATIONS(UINT64 *offset, BYTE *blob, TPM_DELEGATIONS *delegations)
{
	Trspi_LoadBlob_UINT16(offset, delegations->tag, blob);
	Trspi_LoadBlob_UINT32(offset, delegations->delegateType, blob);
	Trspi_LoadBlob_UINT32(offset, delegations->per1, blob);
	Trspi_LoadBlob_UINT32(offset, delegations->per2, blob);
}

TSS_RESULT
Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(UINT64 *offset, BYTE *blob, TPM_DELEGATE_PUBLIC *pub)
{
	TSS_RESULT result;

	if (!pub) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_TPM_DELEGATE_LABEL(offset, blob, NULL);
		(void)Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, NULL);
		Trspi_UnloadBlob_TPM_DELEGATIONS(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &pub->tag, blob);
	Trspi_UnloadBlob_TPM_DELEGATE_LABEL(offset, blob, &pub->label);
	if ((result = Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, &pub->pcrInfo)))
		return result;
	Trspi_UnloadBlob_TPM_DELEGATIONS(offset, blob, &pub->permissions);
	Trspi_UnloadBlob_UINT32(offset, &pub->familyID, blob);
	Trspi_UnloadBlob_UINT32(offset, &pub->verificationCount, blob);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(UINT64 *offset, BYTE *blob, TPM_DELEGATE_PUBLIC *pub)
{
	Trspi_LoadBlob_UINT16(offset, pub->tag, blob);
	Trspi_LoadBlob_TPM_DELEGATE_LABEL(offset, blob, &pub->label);
	Trspi_LoadBlob_PCR_INFO_SHORT(offset, blob, &pub->pcrInfo);
	Trspi_LoadBlob_TPM_DELEGATIONS(offset, blob, &pub->permissions);
	Trspi_LoadBlob_UINT32(offset, pub->familyID, blob);
	Trspi_LoadBlob_UINT32(offset, pub->verificationCount, blob);
}

TSS_RESULT
Trspi_UnloadBlob_TPM_DELEGATE_OWNER_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_OWNER_BLOB *owner)
{
	TSS_RESULT result;

	if (!owner) {
		UINT32 additionalSize, sensitiveSize;

		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		(void)Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &additionalSize, blob);
		(void)Trspi_UnloadBlob(offset, additionalSize, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &sensitiveSize, blob);
		(void)Trspi_UnloadBlob(offset, sensitiveSize, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &owner->tag, blob);
	if ((result = Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &owner->pub)))
		return result;
	Trspi_UnloadBlob_DIGEST(offset, blob, &owner->integrityDigest);
	Trspi_UnloadBlob_UINT32(offset, &owner->additionalSize, blob);
	if (owner->additionalSize > 0) {
		owner->additionalArea = malloc(owner->additionalSize);
		if (owner->additionalArea == NULL) {
			LogError("malloc of %u bytes failed.", owner->additionalSize);
			free(owner->pub.pcrInfo.pcrSelection.pcrSelect);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, owner->additionalSize, blob, owner->additionalArea);
	}
	Trspi_UnloadBlob_UINT32(offset, &owner->sensitiveSize, blob);
	if (owner->sensitiveSize > 0) {
		owner->sensitiveArea = malloc(owner->sensitiveSize);
		if (owner->sensitiveArea == NULL) {
			LogError("malloc of %u bytes failed.", owner->sensitiveSize);
			free(owner->pub.pcrInfo.pcrSelection.pcrSelect);
			free(owner->additionalArea);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, owner->sensitiveSize, blob, owner->sensitiveArea);
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_TPM_DELEGATE_OWNER_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_OWNER_BLOB *owner)
{
	Trspi_LoadBlob_UINT16(offset, owner->tag, blob);
	Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &owner->pub);
	Trspi_LoadBlob_DIGEST(offset, blob, &owner->integrityDigest);
	Trspi_LoadBlob_UINT32(offset, owner->additionalSize, blob);
	Trspi_LoadBlob(offset, owner->additionalSize, blob, owner->additionalArea);
	Trspi_LoadBlob_UINT32(offset, owner->sensitiveSize, blob);
	Trspi_LoadBlob(offset, owner->sensitiveSize, blob, owner->sensitiveArea);
}

TSS_RESULT
Trspi_UnloadBlob_TPM_DELEGATE_KEY_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_KEY_BLOB *key)
{
	TSS_RESULT result;

	if (!key) {
		UINT32 additionalSize, sensitiveSize;

		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		(void)Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &additionalSize, blob);
		(void)Trspi_UnloadBlob(offset, additionalSize, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &sensitiveSize, blob);
		(void)Trspi_UnloadBlob(offset, sensitiveSize, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &key->tag, blob);
	if ((result = Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &key->pub)))
		return result;
	Trspi_UnloadBlob_DIGEST(offset, blob, &key->integrityDigest);
	Trspi_UnloadBlob_DIGEST(offset, blob, &key->pubKeyDigest);
	Trspi_UnloadBlob_UINT32(offset, &key->additionalSize, blob);
	if (key->additionalSize > 0) {
		key->additionalArea = malloc(key->additionalSize);
		if (key->additionalArea == NULL) {
			LogError("malloc of %u bytes failed.", key->additionalSize);
			free(key->pub.pcrInfo.pcrSelection.pcrSelect);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->additionalSize, blob, key->additionalArea);
	}
	Trspi_UnloadBlob_UINT32(offset, &key->sensitiveSize, blob);
	if (key->sensitiveSize > 0) {
		key->sensitiveArea = malloc(key->sensitiveSize);
		if (key->sensitiveArea == NULL) {
			LogError("malloc of %u bytes failed.", key->sensitiveSize);
			free(key->pub.pcrInfo.pcrSelection.pcrSelect);
			free(key->additionalArea);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, key->sensitiveSize, blob, key->sensitiveArea);
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_TPM_DELEGATE_KEY_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_KEY_BLOB *key)
{
	Trspi_LoadBlob_UINT16(offset, key->tag, blob);
	Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &key->pub);
	Trspi_LoadBlob_DIGEST(offset, blob, &key->integrityDigest);
	Trspi_LoadBlob_DIGEST(offset, blob, &key->pubKeyDigest);
	Trspi_LoadBlob_UINT32(offset, key->additionalSize, blob);
	Trspi_LoadBlob(offset, key->additionalSize, blob, key->additionalArea);
	Trspi_LoadBlob_UINT32(offset, key->sensitiveSize, blob);
	Trspi_LoadBlob(offset, key->sensitiveSize, blob, key->sensitiveArea);
}

void
Trspi_UnloadBlob_TSS_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TSS_FAMILY_TABLE_ENTRY *entry)
{
	if (!entry) {
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_BOOL(offset, NULL, blob);
		Trspi_UnloadBlob_BOOL(offset, NULL, blob);

		return;
	}

	Trspi_UnloadBlob_UINT32(offset, &entry->familyID, blob);
	Trspi_UnloadBlob_BYTE(offset, &entry->label, blob);
	Trspi_UnloadBlob_UINT32(offset, &entry->verificationCount, blob);
	Trspi_UnloadBlob_BOOL(offset, &entry->enabled, blob);
	Trspi_UnloadBlob_BOOL(offset, &entry->locked, blob);
}

void
Trspi_LoadBlob_TSS_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TSS_FAMILY_TABLE_ENTRY *entry)
{
	Trspi_LoadBlob_UINT32(offset, entry->familyID, blob);
	Trspi_LoadBlob_BYTE(offset, entry->label, blob);
	Trspi_LoadBlob_UINT32(offset, entry->verificationCount, blob);
	Trspi_LoadBlob_BOOL(offset, entry->enabled, blob);
	Trspi_LoadBlob_BOOL(offset, entry->locked, blob);
}

TSS_RESULT
Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TSS_PCR_INFO_SHORT *pcr)
{
	if (!pcr) {
		UINT32 sizeOfSelect, sizeOfDigestAtRelease;

		Trspi_UnloadBlob_UINT32(offset, &sizeOfSelect, blob);
		(void)Trspi_UnloadBlob(offset, sizeOfSelect, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, &sizeOfDigestAtRelease, blob);
		(void)Trspi_UnloadBlob(offset, sizeOfDigestAtRelease, blob, NULL);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &pcr->sizeOfSelect, blob);
	if (pcr->sizeOfSelect > 0) {
		pcr->selection = malloc(pcr->sizeOfSelect);
		if (pcr->selection == NULL) {
			LogError("malloc of %u bytes failed.", pcr->sizeOfSelect);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, pcr->sizeOfSelect, blob, pcr->selection);
	} else {
		pcr->selection = NULL;
	}
	Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtRelease, blob);
	Trspi_UnloadBlob_UINT32(offset, &pcr->sizeOfDigestAtRelease, blob);
	if (pcr->sizeOfDigestAtRelease > 0) {
		pcr->digestAtRelease = malloc(pcr->sizeOfDigestAtRelease);
		if (pcr->digestAtRelease == NULL) {
			LogError("malloc of %u bytes failed.", pcr->sizeOfDigestAtRelease);
			free(pcr->selection);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(offset, pcr->sizeOfDigestAtRelease, blob, pcr->digestAtRelease);
	} else {
		pcr->digestAtRelease = NULL;
	}

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_TSS_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TSS_PCR_INFO_SHORT *pcr)
{
	Trspi_LoadBlob_UINT32(offset, pcr->sizeOfSelect, blob);
	Trspi_LoadBlob(offset, pcr->sizeOfSelect, blob, pcr->selection);
	Trspi_LoadBlob_BYTE(offset, pcr->localityAtRelease, blob);
	Trspi_LoadBlob_UINT32(offset, pcr->sizeOfDigestAtRelease, blob);
	Trspi_LoadBlob(offset, pcr->sizeOfDigestAtRelease, blob, pcr->digestAtRelease);
}

TSS_RESULT
Trspi_UnloadBlob_TSS_DELEGATION_TABLE_ENTRY(UINT64 *offset, BYTE *blob,
					    TSS_DELEGATION_TABLE_ENTRY *entry)
{
	TSS_RESULT result;

	if (!entry) {
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		(void)Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, &entry->tableIndex, blob);
	Trspi_UnloadBlob_BYTE(offset, &entry->label, blob);
	if ((result = Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(offset, blob, &entry->pcrInfo)))
		return result;
	Trspi_UnloadBlob_UINT32(offset, &entry->per1, blob);
	Trspi_UnloadBlob_UINT32(offset, &entry->per2, blob);
	Trspi_UnloadBlob_UINT32(offset, &entry->familyID, blob);
	Trspi_UnloadBlob_UINT32(offset, &entry->verificationCount, blob);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_TSS_DELEGATION_TABLE_ENTRY(UINT64 *offset, BYTE *blob,
					  TSS_DELEGATION_TABLE_ENTRY *entry)
{
	Trspi_LoadBlob_UINT32(offset, entry->tableIndex, blob);
	Trspi_LoadBlob_BYTE(offset, entry->label, blob);
	Trspi_LoadBlob_TSS_PCR_INFO_SHORT(offset, blob, &entry->pcrInfo);
	Trspi_LoadBlob_UINT32(offset, entry->per1, blob);
	Trspi_LoadBlob_UINT32(offset, entry->per2, blob);
	Trspi_LoadBlob_UINT32(offset, entry->familyID, blob);
	Trspi_LoadBlob_UINT32(offset, entry->verificationCount, blob);
}

TSS_RESULT
Trspi_UnloadBlob_PCR_COMPOSITE(UINT64 *offset, BYTE *blob, TCPA_PCR_COMPOSITE *out)
{
	TSS_RESULT result;

	if (!out) {
		UINT32 valueSize;

		Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
		Trspi_UnloadBlob_UINT32(offset, &valueSize, blob);
		Trspi_UnloadBlob(offset, valueSize, blob, NULL);

		return TSS_SUCCESS;
	}

	if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &out->select)))
		return result;

	Trspi_UnloadBlob_UINT32(offset, &out->valueSize, blob);
	out->pcrValue = malloc(out->valueSize);
	if (out->pcrValue == NULL) {
		LogError("malloc of %u bytes failed.", out->valueSize);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	Trspi_UnloadBlob(offset, out->valueSize, blob, (BYTE *)out->pcrValue);

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_MIGRATIONKEYAUTH(UINT64 *offset, BYTE *blob, TPM_MIGRATIONKEYAUTH *migAuth)
{
	TSS_RESULT result;

	if (!migAuth) {
		(void)Trspi_UnloadBlob_PUBKEY(offset, blob, NULL);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_DIGEST(offset, blob, NULL);

		return TSS_SUCCESS;
	}

	if ((result = Trspi_UnloadBlob_PUBKEY(offset, blob, &migAuth->migrationKey)))
		return result;

	Trspi_UnloadBlob_UINT16(offset, &migAuth->migrationScheme, blob);
	Trspi_UnloadBlob_DIGEST(offset, blob, &migAuth->digest);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_MIGRATIONKEYAUTH(UINT64 *offset, BYTE *blob, TPM_MIGRATIONKEYAUTH *migAuth)
{
	Trspi_LoadBlob_PUBKEY(offset, blob, &migAuth->migrationKey);
	Trspi_LoadBlob_UINT16(offset, migAuth->migrationScheme, blob);
	Trspi_LoadBlob_DIGEST(offset, blob, &migAuth->digest);
}

void
Trspi_LoadBlob_MSA_COMPOSITE(UINT64 *offset, BYTE *blob, TPM_MSA_COMPOSITE *msaComp)
{
	UINT32 i;

	Trspi_LoadBlob_UINT32(offset, msaComp->MSAlist, blob);
	for (i = 0; i < msaComp->MSAlist; i++)
		Trspi_LoadBlob_DIGEST(offset, blob, &msaComp->migAuthDigest[i]);
}

void
Trspi_LoadBlob_CMK_AUTH(UINT64 *offset, BYTE *blob, TPM_CMK_AUTH *cmkAuth)
{
	Trspi_LoadBlob_DIGEST(offset, blob, &cmkAuth->migrationAuthorityDigest);
	Trspi_LoadBlob_DIGEST(offset, blob, &cmkAuth->destinationKeyDigest);
	Trspi_LoadBlob_DIGEST(offset, blob, &cmkAuth->sourceKeyDigest);
}

TSS_RESULT
Trspi_Hash_MSA_COMPOSITE(Trspi_HashCtx *c, TPM_MSA_COMPOSITE *m)
{
	UINT32 i;
	TPM_DIGEST *digest;
	TSS_RESULT result;

	result = Trspi_Hash_UINT32(c, m->MSAlist);
	digest = m->migAuthDigest;
	for (i = 0; i < m->MSAlist; i++) {
		result |= Trspi_Hash_DIGEST(c, digest->digest);
		digest++;
	}

	return result;
}

TSS_RESULT
Trspi_UnloadBlob_TSS_PLATFORM_CLASS(UINT64 *offset, BYTE *blob, TSS_PLATFORM_CLASS *platClass)
{
	if (!platClass){
		UINT32 classURISize;

		Trspi_UnloadBlob_UINT32(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, &classURISize, blob);
		Trspi_UnloadBlob(offset, classURISize, blob, NULL);

		return TSS_SUCCESS;
	}
	Trspi_UnloadBlob_UINT32(offset, &platClass->platformClassSimpleIdentifier, blob);
	Trspi_UnloadBlob_UINT32(offset, &platClass->platformClassURISize, blob);

	platClass->pPlatformClassURI = malloc(platClass->platformClassURISize);
	if (platClass->pPlatformClassURI == NULL) {
		LogError("malloc of %u bytes failed.", platClass->platformClassURISize);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	Trspi_UnloadBlob(offset, platClass->platformClassURISize, blob,
			 (BYTE *)platClass->pPlatformClassURI);

	return TSS_SUCCESS;
}

void
Trspi_LoadBlob_CAP_VERSION_INFO(UINT64 *offset, BYTE *blob, TPM_CAP_VERSION_INFO *v)
{
	Trspi_LoadBlob_UINT16(offset, v->tag, blob);
	Trspi_LoadBlob_TCPA_VERSION(offset, blob, *(TCPA_VERSION *)(&v->version));
	Trspi_LoadBlob_UINT16(offset, v->specLevel, blob);
	Trspi_LoadBlob_BYTE(offset, v->errataRev, blob);
	Trspi_LoadBlob(offset, sizeof(v->tpmVendorID), blob, v->tpmVendorID);
	Trspi_LoadBlob_UINT16(offset, v->vendorSpecificSize, blob);
	Trspi_LoadBlob(offset, v->vendorSpecificSize, blob, v->vendorSpecific);
}

TSS_RESULT
Trspi_UnloadBlob_CAP_VERSION_INFO(UINT64 *offset, BYTE *blob, TPM_CAP_VERSION_INFO *v)
{
	if (!v) {
		UINT16 vendorSpecificSize;

		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_VERSION(offset, blob, NULL);
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob(offset, 4, blob, NULL);
		Trspi_UnloadBlob_UINT16(offset, &vendorSpecificSize, blob);

		(*offset) += vendorSpecificSize;

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &v->tag, blob);
	Trspi_UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&v->version);
	Trspi_UnloadBlob_UINT16(offset, &v->specLevel, blob);
	Trspi_UnloadBlob_BYTE(offset, &v->errataRev, blob);
	Trspi_UnloadBlob(offset, sizeof(v->tpmVendorID), blob, v->tpmVendorID);
	Trspi_UnloadBlob_UINT16(offset, &v->vendorSpecificSize, blob);

	if (v->vendorSpecificSize > 0) {
		if ((v->vendorSpecific = malloc(v->vendorSpecificSize)) == NULL) {
			LogError("malloc of %u bytes failed.", v->vendorSpecificSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}

		Trspi_UnloadBlob(offset, v->vendorSpecificSize, blob, v->vendorSpecific);
	} else {
		v->vendorSpecific = NULL;
	}

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_NV_INDEX(UINT64 *offset, BYTE *blob, TPM_NV_INDEX *v)
{
	if (!v) {
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT32(offset, v, blob);

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_NV_ATTRIBUTES(UINT64 *offset, BYTE *blob, TPM_NV_ATTRIBUTES *v)
{
	if (!v) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &v->tag, blob);
	Trspi_UnloadBlob_UINT32(offset, &v->attributes, blob);

	return TSS_SUCCESS;
}

TSS_RESULT
Trspi_UnloadBlob_NV_DATA_PUBLIC(UINT64 *offset, BYTE *blob, TPM_NV_DATA_PUBLIC *v)
{
	if (!v) {
		Trspi_UnloadBlob_UINT16(offset, NULL, blob);
		Trspi_UnloadBlob_NV_INDEX(offset, blob, NULL);
		Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, NULL);
		Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, NULL);
		Trspi_UnloadBlob_NV_ATTRIBUTES(offset, blob, NULL);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_BYTE(offset, NULL, blob);
		Trspi_UnloadBlob_UINT32(offset, NULL, blob);

		return TSS_SUCCESS;
	}

	Trspi_UnloadBlob_UINT16(offset, &v->tag, blob);
	Trspi_UnloadBlob_NV_INDEX(offset, blob, &v->nvIndex);
	Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, &v->pcrInfoRead);
	Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, &v->pcrInfoWrite);
	Trspi_UnloadBlob_NV_ATTRIBUTES(offset, blob, &v->permission);
	Trspi_UnloadBlob_BYTE(offset, &v->bReadSTClear, blob);
	Trspi_UnloadBlob_BYTE(offset, &v->bWriteSTClear, blob);
	Trspi_UnloadBlob_BYTE(offset, &v->bWriteDefine, blob);
	Trspi_UnloadBlob_UINT32(offset, &v->dataSize, blob);

	return TSS_SUCCESS;
}