Home | History | Annotate | Download | only in tpm2
      1 // Copyright 2015 The Chromium OS Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // THIS CODE IS GENERATED - DO NOT MODIFY!
      6 
      7 #include "tpm_generated.h"
      8 
      9 UINT16 uint8_t_Marshal(uint8_t* source, BYTE** buffer, INT32* size) {
     10   uint8_t value_net = *source;
     11   if (!size || *size < sizeof(uint8_t)) {
     12     return 0;  // Nothing has been marshaled.
     13   }
     14   switch (sizeof(uint8_t)) {
     15     case 2:
     16       value_net = htobe16(*source);
     17       break;
     18     case 4:
     19       value_net = htobe32(*source);
     20       break;
     21     case 8:
     22       value_net = htobe64(*source);
     23       break;
     24     default:
     25       break;
     26   }
     27   memcpy(*buffer, &value_net, sizeof(uint8_t));
     28   *buffer += sizeof(uint8_t);
     29   *size -= sizeof(uint8_t);
     30   return sizeof(uint8_t);
     31 }
     32 
     33 TPM_RC uint8_t_Unmarshal(uint8_t* target, BYTE** buffer, INT32* size) {
     34   uint8_t value_net = 0;
     35   if (!size || *size < sizeof(uint8_t)) {
     36     return TPM_RC_INSUFFICIENT;
     37   }
     38   memcpy(&value_net, *buffer, sizeof(uint8_t));
     39   switch (sizeof(uint8_t)) {
     40     case 2:
     41       *target = be16toh(value_net);
     42       break;
     43     case 4:
     44       *target = be32toh(value_net);
     45       break;
     46     case 8:
     47       *target = be64toh(value_net);
     48       break;
     49     default:
     50       *target = value_net;
     51   }
     52   *buffer += sizeof(uint8_t);
     53   *size -= sizeof(uint8_t);
     54   return TPM_RC_SUCCESS;
     55 }
     56 
     57 UINT16 int8_t_Marshal(int8_t* source, BYTE** buffer, INT32* size) {
     58   int8_t value_net = *source;
     59   if (!size || *size < sizeof(int8_t)) {
     60     return 0;  // Nothing has been marshaled.
     61   }
     62   switch (sizeof(int8_t)) {
     63     case 2:
     64       value_net = htobe16(*source);
     65       break;
     66     case 4:
     67       value_net = htobe32(*source);
     68       break;
     69     case 8:
     70       value_net = htobe64(*source);
     71       break;
     72     default:
     73       break;
     74   }
     75   memcpy(*buffer, &value_net, sizeof(int8_t));
     76   *buffer += sizeof(int8_t);
     77   *size -= sizeof(int8_t);
     78   return sizeof(int8_t);
     79 }
     80 
     81 TPM_RC int8_t_Unmarshal(int8_t* target, BYTE** buffer, INT32* size) {
     82   int8_t value_net = 0;
     83   if (!size || *size < sizeof(int8_t)) {
     84     return TPM_RC_INSUFFICIENT;
     85   }
     86   memcpy(&value_net, *buffer, sizeof(int8_t));
     87   switch (sizeof(int8_t)) {
     88     case 2:
     89       *target = be16toh(value_net);
     90       break;
     91     case 4:
     92       *target = be32toh(value_net);
     93       break;
     94     case 8:
     95       *target = be64toh(value_net);
     96       break;
     97     default:
     98       *target = value_net;
     99   }
    100   *buffer += sizeof(int8_t);
    101   *size -= sizeof(int8_t);
    102   return TPM_RC_SUCCESS;
    103 }
    104 
    105 UINT16 uint16_t_Marshal(uint16_t* source, BYTE** buffer, INT32* size) {
    106   uint16_t value_net = *source;
    107   if (!size || *size < sizeof(uint16_t)) {
    108     return 0;  // Nothing has been marshaled.
    109   }
    110   switch (sizeof(uint16_t)) {
    111     case 2:
    112       value_net = htobe16(*source);
    113       break;
    114     case 4:
    115       value_net = htobe32(*source);
    116       break;
    117     case 8:
    118       value_net = htobe64(*source);
    119       break;
    120     default:
    121       break;
    122   }
    123   memcpy(*buffer, &value_net, sizeof(uint16_t));
    124   *buffer += sizeof(uint16_t);
    125   *size -= sizeof(uint16_t);
    126   return sizeof(uint16_t);
    127 }
    128 
    129 TPM_RC uint16_t_Unmarshal(uint16_t* target, BYTE** buffer, INT32* size) {
    130   uint16_t value_net = 0;
    131   if (!size || *size < sizeof(uint16_t)) {
    132     return TPM_RC_INSUFFICIENT;
    133   }
    134   memcpy(&value_net, *buffer, sizeof(uint16_t));
    135   switch (sizeof(uint16_t)) {
    136     case 2:
    137       *target = be16toh(value_net);
    138       break;
    139     case 4:
    140       *target = be32toh(value_net);
    141       break;
    142     case 8:
    143       *target = be64toh(value_net);
    144       break;
    145     default:
    146       *target = value_net;
    147   }
    148   *buffer += sizeof(uint16_t);
    149   *size -= sizeof(uint16_t);
    150   return TPM_RC_SUCCESS;
    151 }
    152 
    153 UINT16 int16_t_Marshal(int16_t* source, BYTE** buffer, INT32* size) {
    154   int16_t value_net = *source;
    155   if (!size || *size < sizeof(int16_t)) {
    156     return 0;  // Nothing has been marshaled.
    157   }
    158   switch (sizeof(int16_t)) {
    159     case 2:
    160       value_net = htobe16(*source);
    161       break;
    162     case 4:
    163       value_net = htobe32(*source);
    164       break;
    165     case 8:
    166       value_net = htobe64(*source);
    167       break;
    168     default:
    169       break;
    170   }
    171   memcpy(*buffer, &value_net, sizeof(int16_t));
    172   *buffer += sizeof(int16_t);
    173   *size -= sizeof(int16_t);
    174   return sizeof(int16_t);
    175 }
    176 
    177 TPM_RC int16_t_Unmarshal(int16_t* target, BYTE** buffer, INT32* size) {
    178   int16_t value_net = 0;
    179   if (!size || *size < sizeof(int16_t)) {
    180     return TPM_RC_INSUFFICIENT;
    181   }
    182   memcpy(&value_net, *buffer, sizeof(int16_t));
    183   switch (sizeof(int16_t)) {
    184     case 2:
    185       *target = be16toh(value_net);
    186       break;
    187     case 4:
    188       *target = be32toh(value_net);
    189       break;
    190     case 8:
    191       *target = be64toh(value_net);
    192       break;
    193     default:
    194       *target = value_net;
    195   }
    196   *buffer += sizeof(int16_t);
    197   *size -= sizeof(int16_t);
    198   return TPM_RC_SUCCESS;
    199 }
    200 
    201 UINT16 uint32_t_Marshal(uint32_t* source, BYTE** buffer, INT32* size) {
    202   uint32_t value_net = *source;
    203   if (!size || *size < sizeof(uint32_t)) {
    204     return 0;  // Nothing has been marshaled.
    205   }
    206   switch (sizeof(uint32_t)) {
    207     case 2:
    208       value_net = htobe16(*source);
    209       break;
    210     case 4:
    211       value_net = htobe32(*source);
    212       break;
    213     case 8:
    214       value_net = htobe64(*source);
    215       break;
    216     default:
    217       break;
    218   }
    219   memcpy(*buffer, &value_net, sizeof(uint32_t));
    220   *buffer += sizeof(uint32_t);
    221   *size -= sizeof(uint32_t);
    222   return sizeof(uint32_t);
    223 }
    224 
    225 TPM_RC uint32_t_Unmarshal(uint32_t* target, BYTE** buffer, INT32* size) {
    226   uint32_t value_net = 0;
    227   if (!size || *size < sizeof(uint32_t)) {
    228     return TPM_RC_INSUFFICIENT;
    229   }
    230   memcpy(&value_net, *buffer, sizeof(uint32_t));
    231   switch (sizeof(uint32_t)) {
    232     case 2:
    233       *target = be16toh(value_net);
    234       break;
    235     case 4:
    236       *target = be32toh(value_net);
    237       break;
    238     case 8:
    239       *target = be64toh(value_net);
    240       break;
    241     default:
    242       *target = value_net;
    243   }
    244   *buffer += sizeof(uint32_t);
    245   *size -= sizeof(uint32_t);
    246   return TPM_RC_SUCCESS;
    247 }
    248 
    249 UINT16 int32_t_Marshal(int32_t* source, BYTE** buffer, INT32* size) {
    250   int32_t value_net = *source;
    251   if (!size || *size < sizeof(int32_t)) {
    252     return 0;  // Nothing has been marshaled.
    253   }
    254   switch (sizeof(int32_t)) {
    255     case 2:
    256       value_net = htobe16(*source);
    257       break;
    258     case 4:
    259       value_net = htobe32(*source);
    260       break;
    261     case 8:
    262       value_net = htobe64(*source);
    263       break;
    264     default:
    265       break;
    266   }
    267   memcpy(*buffer, &value_net, sizeof(int32_t));
    268   *buffer += sizeof(int32_t);
    269   *size -= sizeof(int32_t);
    270   return sizeof(int32_t);
    271 }
    272 
    273 TPM_RC int32_t_Unmarshal(int32_t* target, BYTE** buffer, INT32* size) {
    274   int32_t value_net = 0;
    275   if (!size || *size < sizeof(int32_t)) {
    276     return TPM_RC_INSUFFICIENT;
    277   }
    278   memcpy(&value_net, *buffer, sizeof(int32_t));
    279   switch (sizeof(int32_t)) {
    280     case 2:
    281       *target = be16toh(value_net);
    282       break;
    283     case 4:
    284       *target = be32toh(value_net);
    285       break;
    286     case 8:
    287       *target = be64toh(value_net);
    288       break;
    289     default:
    290       *target = value_net;
    291   }
    292   *buffer += sizeof(int32_t);
    293   *size -= sizeof(int32_t);
    294   return TPM_RC_SUCCESS;
    295 }
    296 
    297 UINT16 uint64_t_Marshal(uint64_t* source, BYTE** buffer, INT32* size) {
    298   uint64_t value_net = *source;
    299   if (!size || *size < sizeof(uint64_t)) {
    300     return 0;  // Nothing has been marshaled.
    301   }
    302   switch (sizeof(uint64_t)) {
    303     case 2:
    304       value_net = htobe16(*source);
    305       break;
    306     case 4:
    307       value_net = htobe32(*source);
    308       break;
    309     case 8:
    310       value_net = htobe64(*source);
    311       break;
    312     default:
    313       break;
    314   }
    315   memcpy(*buffer, &value_net, sizeof(uint64_t));
    316   *buffer += sizeof(uint64_t);
    317   *size -= sizeof(uint64_t);
    318   return sizeof(uint64_t);
    319 }
    320 
    321 TPM_RC uint64_t_Unmarshal(uint64_t* target, BYTE** buffer, INT32* size) {
    322   uint64_t value_net = 0;
    323   if (!size || *size < sizeof(uint64_t)) {
    324     return TPM_RC_INSUFFICIENT;
    325   }
    326   memcpy(&value_net, *buffer, sizeof(uint64_t));
    327   switch (sizeof(uint64_t)) {
    328     case 2:
    329       *target = be16toh(value_net);
    330       break;
    331     case 4:
    332       *target = be32toh(value_net);
    333       break;
    334     case 8:
    335       *target = be64toh(value_net);
    336       break;
    337     default:
    338       *target = value_net;
    339   }
    340   *buffer += sizeof(uint64_t);
    341   *size -= sizeof(uint64_t);
    342   return TPM_RC_SUCCESS;
    343 }
    344 
    345 UINT16 int64_t_Marshal(int64_t* source, BYTE** buffer, INT32* size) {
    346   int64_t value_net = *source;
    347   if (!size || *size < sizeof(int64_t)) {
    348     return 0;  // Nothing has been marshaled.
    349   }
    350   switch (sizeof(int64_t)) {
    351     case 2:
    352       value_net = htobe16(*source);
    353       break;
    354     case 4:
    355       value_net = htobe32(*source);
    356       break;
    357     case 8:
    358       value_net = htobe64(*source);
    359       break;
    360     default:
    361       break;
    362   }
    363   memcpy(*buffer, &value_net, sizeof(int64_t));
    364   *buffer += sizeof(int64_t);
    365   *size -= sizeof(int64_t);
    366   return sizeof(int64_t);
    367 }
    368 
    369 TPM_RC int64_t_Unmarshal(int64_t* target, BYTE** buffer, INT32* size) {
    370   int64_t value_net = 0;
    371   if (!size || *size < sizeof(int64_t)) {
    372     return TPM_RC_INSUFFICIENT;
    373   }
    374   memcpy(&value_net, *buffer, sizeof(int64_t));
    375   switch (sizeof(int64_t)) {
    376     case 2:
    377       *target = be16toh(value_net);
    378       break;
    379     case 4:
    380       *target = be32toh(value_net);
    381       break;
    382     case 8:
    383       *target = be64toh(value_net);
    384       break;
    385     default:
    386       *target = value_net;
    387   }
    388   *buffer += sizeof(int64_t);
    389   *size -= sizeof(int64_t);
    390   return TPM_RC_SUCCESS;
    391 }
    392 
    393 UINT16 BYTE_Marshal(BYTE* source, BYTE** buffer, INT32* size) {
    394   return uint8_t_Marshal(source, buffer, size);
    395 }
    396 
    397 TPM_RC BYTE_Unmarshal(BYTE* target, BYTE** buffer, INT32* size) {
    398   return uint8_t_Unmarshal(target, buffer, size);
    399 }
    400 
    401 UINT16 INT16_Marshal(INT16* source, BYTE** buffer, INT32* size) {
    402   return int16_t_Marshal(source, buffer, size);
    403 }
    404 
    405 TPM_RC INT16_Unmarshal(INT16* target, BYTE** buffer, INT32* size) {
    406   return int16_t_Unmarshal(target, buffer, size);
    407 }
    408 
    409 UINT16 INT32_Marshal(INT32* source, BYTE** buffer, INT32* size) {
    410   return int32_t_Marshal(source, buffer, size);
    411 }
    412 
    413 TPM_RC INT32_Unmarshal(INT32* target, BYTE** buffer, INT32* size) {
    414   return int32_t_Unmarshal(target, buffer, size);
    415 }
    416 
    417 UINT16 INT64_Marshal(INT64* source, BYTE** buffer, INT32* size) {
    418   return int64_t_Marshal(source, buffer, size);
    419 }
    420 
    421 TPM_RC INT64_Unmarshal(INT64* target, BYTE** buffer, INT32* size) {
    422   return int64_t_Unmarshal(target, buffer, size);
    423 }
    424 
    425 UINT16 INT8_Marshal(INT8* source, BYTE** buffer, INT32* size) {
    426   return int8_t_Marshal(source, buffer, size);
    427 }
    428 
    429 TPM_RC INT8_Unmarshal(INT8* target, BYTE** buffer, INT32* size) {
    430   return int8_t_Unmarshal(target, buffer, size);
    431 }
    432 
    433 UINT16 UINT16_Marshal(UINT16* source, BYTE** buffer, INT32* size) {
    434   return uint16_t_Marshal(source, buffer, size);
    435 }
    436 
    437 TPM_RC UINT16_Unmarshal(UINT16* target, BYTE** buffer, INT32* size) {
    438   return uint16_t_Unmarshal(target, buffer, size);
    439 }
    440 
    441 UINT16 TPM2B_ATTEST_Marshal(TPM2B_ATTEST* source, BYTE** buffer, INT32* size) {
    442   UINT16 total_size = 0;
    443   INT32 i;
    444   total_size += UINT16_Marshal(&source->t.size, buffer, size);
    445   for (i = 0; i < source->t.size; ++i) {
    446     total_size += BYTE_Marshal(&source->t.attestationData[i], buffer, size);
    447   }
    448   return total_size;
    449 }
    450 
    451 TPM_RC TPM2B_ATTEST_Unmarshal(TPM2B_ATTEST* target,
    452                               BYTE** buffer,
    453                               INT32* size) {
    454   TPM_RC result;
    455   INT32 i;
    456   result = UINT16_Unmarshal(&target->t.size, buffer, size);
    457   if (result != TPM_RC_SUCCESS) {
    458     return result;
    459   }
    460   if (target->t.size == 0) {
    461     return TPM_RC_SUCCESS;
    462   }
    463   if (target->t.size > sizeof(TPMS_ATTEST)) {
    464     return TPM_RC_SIZE;
    465   }
    466   for (i = 0; i < target->t.size; ++i) {
    467     result = BYTE_Unmarshal(&target->t.attestationData[i], buffer, size);
    468     if (result != TPM_RC_SUCCESS) {
    469       return result;
    470     }
    471   }
    472   return TPM_RC_SUCCESS;
    473 }
    474 
    475 UINT16 TPM2B_DIGEST_Marshal(TPM2B_DIGEST* source, BYTE** buffer, INT32* size) {
    476   UINT16 total_size = 0;
    477   INT32 i;
    478   total_size += UINT16_Marshal(&source->t.size, buffer, size);
    479   for (i = 0; i < source->t.size; ++i) {
    480     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
    481   }
    482   return total_size;
    483 }
    484 
    485 TPM_RC TPM2B_DIGEST_Unmarshal(TPM2B_DIGEST* target,
    486                               BYTE** buffer,
    487                               INT32* size) {
    488   TPM_RC result;
    489   INT32 i;
    490   result = UINT16_Unmarshal(&target->t.size, buffer, size);
    491   if (result != TPM_RC_SUCCESS) {
    492     return result;
    493   }
    494   if (target->t.size == 0) {
    495     return TPM_RC_SUCCESS;
    496   }
    497   if (target->t.size > sizeof(TPMU_HA)) {
    498     return TPM_RC_SIZE;
    499   }
    500   for (i = 0; i < target->t.size; ++i) {
    501     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
    502     if (result != TPM_RC_SUCCESS) {
    503       return result;
    504     }
    505   }
    506   return TPM_RC_SUCCESS;
    507 }
    508 
    509 UINT16 TPM2B_AUTH_Marshal(TPM2B_AUTH* source, BYTE** buffer, INT32* size) {
    510   return TPM2B_DIGEST_Marshal(source, buffer, size);
    511 }
    512 
    513 TPM_RC TPM2B_AUTH_Unmarshal(TPM2B_AUTH* target, BYTE** buffer, INT32* size) {
    514   return TPM2B_DIGEST_Unmarshal(target, buffer, size);
    515 }
    516 
    517 UINT16 TPM2B_CONTEXT_DATA_Marshal(TPM2B_CONTEXT_DATA* source,
    518                                   BYTE** buffer,
    519                                   INT32* size) {
    520   UINT16 total_size = 0;
    521   INT32 i;
    522   total_size += UINT16_Marshal(&source->t.size, buffer, size);
    523   for (i = 0; i < source->t.size; ++i) {
    524     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
    525   }
    526   return total_size;
    527 }
    528 
    529 TPM_RC TPM2B_CONTEXT_DATA_Unmarshal(TPM2B_CONTEXT_DATA* target,
    530                                     BYTE** buffer,
    531                                     INT32* size) {
    532   TPM_RC result;
    533   INT32 i;
    534   result = UINT16_Unmarshal(&target->t.size, buffer, size);
    535   if (result != TPM_RC_SUCCESS) {
    536     return result;
    537   }
    538   if (target->t.size == 0) {
    539     return TPM_RC_SUCCESS;
    540   }
    541   if (target->t.size > sizeof(TPMS_CONTEXT_DATA)) {
    542     return TPM_RC_SIZE;
    543   }
    544   for (i = 0; i < target->t.size; ++i) {
    545     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
    546     if (result != TPM_RC_SUCCESS) {
    547       return result;
    548     }
    549   }
    550   return TPM_RC_SUCCESS;
    551 }
    552 
    553 UINT16 TPM2B_CONTEXT_SENSITIVE_Marshal(TPM2B_CONTEXT_SENSITIVE* source,
    554                                        BYTE** buffer,
    555                                        INT32* size) {
    556   UINT16 total_size = 0;
    557   INT32 i;
    558   total_size += UINT16_Marshal(&source->t.size, buffer, size);
    559   for (i = 0; i < source->t.size; ++i) {
    560     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
    561   }
    562   return total_size;
    563 }
    564 
    565 TPM_RC TPM2B_CONTEXT_SENSITIVE_Unmarshal(TPM2B_CONTEXT_SENSITIVE* target,
    566                                          BYTE** buffer,
    567                                          INT32* size) {
    568   TPM_RC result;
    569   INT32 i;
    570   result = UINT16_Unmarshal(&target->t.size, buffer, size);
    571   if (result != TPM_RC_SUCCESS) {
    572     return result;
    573   }
    574   if (target->t.size == 0) {
    575     return TPM_RC_SUCCESS;
    576   }
    577   if (target->t.size > MAX_CONTEXT_SIZE) {
    578     return TPM_RC_SIZE;
    579   }
    580   for (i = 0; i < target->t.size; ++i) {
    581     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
    582     if (result != TPM_RC_SUCCESS) {
    583       return result;
    584     }
    585   }
    586   return TPM_RC_SUCCESS;
    587 }
    588 
    589 UINT16 TPM_ALG_ID_Marshal(TPM_ALG_ID* source, BYTE** buffer, INT32* size) {
    590   return uint16_t_Marshal(source, buffer, size);
    591 }
    592 
    593 TPM_RC TPM_ALG_ID_Unmarshal(TPM_ALG_ID* target, BYTE** buffer, INT32* size) {
    594   TPM_RC result;
    595   result = uint16_t_Unmarshal(target, buffer, size);
    596   if (result != TPM_RC_SUCCESS) {
    597     return result;
    598   }
    599 #ifdef TPM_ALG_ERROR
    600   if (*target == TPM_ALG_ERROR) {
    601     return TPM_RC_SUCCESS;
    602   }
    603 #endif
    604 #ifdef TPM_ALG_RSA
    605   if (*target == TPM_ALG_RSA) {
    606     return TPM_RC_SUCCESS;
    607   }
    608 #endif
    609 #ifdef TPM_ALG_SHA
    610   if (*target == TPM_ALG_SHA) {
    611     return TPM_RC_SUCCESS;
    612   }
    613 #endif
    614 #ifdef TPM_ALG_SHA1
    615   if (*target == TPM_ALG_SHA1) {
    616     return TPM_RC_SUCCESS;
    617   }
    618 #endif
    619 #ifdef TPM_ALG_HMAC
    620   if (*target == TPM_ALG_HMAC) {
    621     return TPM_RC_SUCCESS;
    622   }
    623 #endif
    624 #ifdef TPM_ALG_AES
    625   if (*target == TPM_ALG_AES) {
    626     return TPM_RC_SUCCESS;
    627   }
    628 #endif
    629 #ifdef TPM_ALG_MGF1
    630   if (*target == TPM_ALG_MGF1) {
    631     return TPM_RC_SUCCESS;
    632   }
    633 #endif
    634 #ifdef TPM_ALG_KEYEDHASH
    635   if (*target == TPM_ALG_KEYEDHASH) {
    636     return TPM_RC_SUCCESS;
    637   }
    638 #endif
    639 #ifdef TPM_ALG_XOR
    640   if (*target == TPM_ALG_XOR) {
    641     return TPM_RC_SUCCESS;
    642   }
    643 #endif
    644 #ifdef TPM_ALG_SHA256
    645   if (*target == TPM_ALG_SHA256) {
    646     return TPM_RC_SUCCESS;
    647   }
    648 #endif
    649 #ifdef TPM_ALG_SHA384
    650   if (*target == TPM_ALG_SHA384) {
    651     return TPM_RC_SUCCESS;
    652   }
    653 #endif
    654 #ifdef TPM_ALG_SHA512
    655   if (*target == TPM_ALG_SHA512) {
    656     return TPM_RC_SUCCESS;
    657   }
    658 #endif
    659 #ifdef TPM_ALG_NULL
    660   if (*target == TPM_ALG_NULL) {
    661     return TPM_RC_SUCCESS;
    662   }
    663 #endif
    664 #ifdef TPM_ALG_SM3_256
    665   if (*target == TPM_ALG_SM3_256) {
    666     return TPM_RC_SUCCESS;
    667   }
    668 #endif
    669 #ifdef TPM_ALG_SM4
    670   if (*target == TPM_ALG_SM4) {
    671     return TPM_RC_SUCCESS;
    672   }
    673 #endif
    674 #ifdef TPM_ALG_RSASSA
    675   if (*target == TPM_ALG_RSASSA) {
    676     return TPM_RC_SUCCESS;
    677   }
    678 #endif
    679 #ifdef TPM_ALG_RSAES
    680   if (*target == TPM_ALG_RSAES) {
    681     return TPM_RC_SUCCESS;
    682   }
    683 #endif
    684 #ifdef TPM_ALG_RSAPSS
    685   if (*target == TPM_ALG_RSAPSS) {
    686     return TPM_RC_SUCCESS;
    687   }
    688 #endif
    689 #ifdef TPM_ALG_OAEP
    690   if (*target == TPM_ALG_OAEP) {
    691     return TPM_RC_SUCCESS;
    692   }
    693 #endif
    694 #ifdef TPM_ALG_ECDSA
    695   if (*target == TPM_ALG_ECDSA) {
    696     return TPM_RC_SUCCESS;
    697   }
    698 #endif
    699 #ifdef TPM_ALG_ECDH
    700   if (*target == TPM_ALG_ECDH) {
    701     return TPM_RC_SUCCESS;
    702   }
    703 #endif
    704 #ifdef TPM_ALG_ECDAA
    705   if (*target == TPM_ALG_ECDAA) {
    706     return TPM_RC_SUCCESS;
    707   }
    708 #endif
    709 #ifdef TPM_ALG_SM2
    710   if (*target == TPM_ALG_SM2) {
    711     return TPM_RC_SUCCESS;
    712   }
    713 #endif
    714 #ifdef TPM_ALG_ECSCHNORR
    715   if (*target == TPM_ALG_ECSCHNORR) {
    716     return TPM_RC_SUCCESS;
    717   }
    718 #endif
    719 #ifdef TPM_ALG_ECMQV
    720   if (*target == TPM_ALG_ECMQV) {
    721     return TPM_RC_SUCCESS;
    722   }
    723 #endif
    724 #ifdef TPM_ALG_KDF1_SP800_56A
    725   if (*target == TPM_ALG_KDF1_SP800_56A) {
    726     return TPM_RC_SUCCESS;
    727   }
    728 #endif
    729 #ifdef TPM_ALG_KDF2
    730   if (*target == TPM_ALG_KDF2) {
    731     return TPM_RC_SUCCESS;
    732   }
    733 #endif
    734 #ifdef TPM_ALG_KDF1_SP800_108
    735   if (*target == TPM_ALG_KDF1_SP800_108) {
    736     return TPM_RC_SUCCESS;
    737   }
    738 #endif
    739 #ifdef TPM_ALG_ECC
    740   if (*target == TPM_ALG_ECC) {
    741     return TPM_RC_SUCCESS;
    742   }
    743 #endif
    744 #ifdef TPM_ALG_SYMCIPHER
    745   if (*target == TPM_ALG_SYMCIPHER) {
    746     return TPM_RC_SUCCESS;
    747   }
    748 #endif
    749 #ifdef TPM_ALG_CAMELLIA
    750   if (*target == TPM_ALG_CAMELLIA) {
    751     return TPM_RC_SUCCESS;
    752   }
    753 #endif
    754 #ifdef TPM_ALG_CTR
    755   if (*target == TPM_ALG_CTR) {
    756     return TPM_RC_SUCCESS;
    757   }
    758 #endif
    759 #ifdef TPM_ALG_OFB
    760   if (*target == TPM_ALG_OFB) {
    761     return TPM_RC_SUCCESS;
    762   }
    763 #endif
    764 #ifdef TPM_ALG_CBC
    765   if (*target == TPM_ALG_CBC) {
    766     return TPM_RC_SUCCESS;
    767   }
    768 #endif
    769 #ifdef TPM_ALG_CFB
    770   if (*target == TPM_ALG_CFB) {
    771     return TPM_RC_SUCCESS;
    772   }
    773 #endif
    774 #ifdef TPM_ALG_ECB
    775   if (*target == TPM_ALG_ECB) {
    776     return TPM_RC_SUCCESS;
    777   }
    778 #endif
    779   return TPM_RC_VALUE;
    780 }
    781 
    782 UINT16 TPM2B_DATA_Marshal(TPM2B_DATA* source, BYTE** buffer, INT32* size) {
    783   UINT16 total_size = 0;
    784   INT32 i;
    785   total_size += UINT16_Marshal(&source->t.size, buffer, size);
    786   for (i = 0; i < source->t.size; ++i) {
    787     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
    788   }
    789   return total_size;
    790 }
    791 
    792 TPM_RC TPM2B_DATA_Unmarshal(TPM2B_DATA* target, BYTE** buffer, INT32* size) {
    793   TPM_RC result;
    794   INT32 i;
    795   result = UINT16_Unmarshal(&target->t.size, buffer, size);
    796   if (result != TPM_RC_SUCCESS) {
    797     return result;
    798   }
    799   if (target->t.size == 0) {
    800     return TPM_RC_SUCCESS;
    801   }
    802   if (target->t.size > sizeof(TPMT_HA)) {
    803     return TPM_RC_SIZE;
    804   }
    805   for (i = 0; i < target->t.size; ++i) {
    806     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
    807     if (result != TPM_RC_SUCCESS) {
    808       return result;
    809     }
    810   }
    811   return TPM_RC_SUCCESS;
    812 }
    813 
    814 UINT16 TPMA_LOCALITY_Marshal(TPMA_LOCALITY* source,
    815                              BYTE** buffer,
    816                              INT32* size) {
    817   return uint8_t_Marshal((uint8_t*)source, buffer, size);
    818 }
    819 
    820 TPM_RC TPMA_LOCALITY_Unmarshal(TPMA_LOCALITY* target,
    821                                BYTE** buffer,
    822                                INT32* size) {
    823   TPM_RC result;
    824   result = uint8_t_Unmarshal((uint8_t*)target, buffer, size);
    825   if (result != TPM_RC_SUCCESS) {
    826     return result;
    827   }
    828   return TPM_RC_SUCCESS;
    829 }
    830 
    831 UINT16 TPM2B_NAME_Marshal(TPM2B_NAME* source, BYTE** buffer, INT32* size) {
    832   UINT16 total_size = 0;
    833   INT32 i;
    834   total_size += UINT16_Marshal(&source->t.size, buffer, size);
    835   for (i = 0; i < source->t.size; ++i) {
    836     total_size += BYTE_Marshal(&source->t.name[i], buffer, size);
    837   }
    838   return total_size;
    839 }
    840 
    841 TPM_RC TPM2B_NAME_Unmarshal(TPM2B_NAME* target, BYTE** buffer, INT32* size) {
    842   TPM_RC result;
    843   INT32 i;
    844   result = UINT16_Unmarshal(&target->t.size, buffer, size);
    845   if (result != TPM_RC_SUCCESS) {
    846     return result;
    847   }
    848   if (target->t.size == 0) {
    849     return TPM_RC_SUCCESS;
    850   }
    851   if (target->t.size > sizeof(TPMU_NAME)) {
    852     return TPM_RC_SIZE;
    853   }
    854   for (i = 0; i < target->t.size; ++i) {
    855     result = BYTE_Unmarshal(&target->t.name[i], buffer, size);
    856     if (result != TPM_RC_SUCCESS) {
    857       return result;
    858     }
    859   }
    860   return TPM_RC_SUCCESS;
    861 }
    862 
    863 UINT16 TPMI_ALG_HASH_Marshal(TPMI_ALG_HASH* source,
    864                              BYTE** buffer,
    865                              INT32* size) {
    866   return uint16_t_Marshal(source, buffer, size);
    867 }
    868 
    869 TPM_RC TPMI_ALG_HASH_Unmarshal(TPMI_ALG_HASH* target,
    870                                BYTE** buffer,
    871                                INT32* size,
    872                                BOOL allow_conditional_value) {
    873   TPM_RC result;
    874   BOOL has_valid_value = FALSE;
    875   result = uint16_t_Unmarshal(target, buffer, size);
    876   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
    877     return result;
    878   }
    879   if (*target == TPM_ALG_NULL) {
    880     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_HASH;
    881   }
    882   switch (*target) {
    883 #ifdef TPM_ALG_SHA
    884     case TPM_ALG_SHA:
    885 #endif
    886 #ifdef TPM_ALG_SHA1
    887     case TPM_ALG_SHA1:
    888 #endif
    889 #ifdef TPM_ALG_SHA256
    890     case TPM_ALG_SHA256:
    891 #endif
    892 #ifdef TPM_ALG_SHA384
    893     case TPM_ALG_SHA384:
    894 #endif
    895 #ifdef TPM_ALG_SHA512
    896     case TPM_ALG_SHA512:
    897 #endif
    898 #ifdef TPM_ALG_SM3_256
    899     case TPM_ALG_SM3_256:
    900 #endif
    901       has_valid_value = TRUE;
    902       break;
    903   }
    904   if (!has_valid_value) {
    905     return TPM_RC_HASH;
    906   }
    907   return TPM_RC_SUCCESS;
    908 }
    909 
    910 UINT16 UINT8_Marshal(UINT8* source, BYTE** buffer, INT32* size) {
    911   return uint8_t_Marshal(source, buffer, size);
    912 }
    913 
    914 TPM_RC UINT8_Unmarshal(UINT8* target, BYTE** buffer, INT32* size) {
    915   return uint8_t_Unmarshal(target, buffer, size);
    916 }
    917 
    918 UINT16 TPMS_PCR_SELECTION_Marshal(TPMS_PCR_SELECTION* source,
    919                                   BYTE** buffer,
    920                                   INT32* size) {
    921   UINT16 total_size = 0;
    922   INT32 i;
    923   total_size += TPMI_ALG_HASH_Marshal(&source->hash, buffer, size);
    924   total_size += UINT8_Marshal(&source->sizeofSelect, buffer, size);
    925   for (i = 0; i < source->sizeofSelect; ++i) {
    926     total_size += BYTE_Marshal(&source->pcrSelect[i], buffer, size);
    927   }
    928   return total_size;
    929 }
    930 
    931 TPM_RC TPMS_PCR_SELECTION_Unmarshal(TPMS_PCR_SELECTION* target,
    932                                     BYTE** buffer,
    933                                     INT32* size) {
    934   TPM_RC result;
    935   INT32 i;
    936   result = TPMI_ALG_HASH_Unmarshal(&target->hash, buffer, size, FALSE);
    937   if (result != TPM_RC_SUCCESS) {
    938     return result;
    939   }
    940   result = UINT8_Unmarshal(&target->sizeofSelect, buffer, size);
    941   if (result != TPM_RC_SUCCESS) {
    942     return result;
    943   }
    944   if (target->sizeofSelect > PCR_SELECT_MAX) {
    945     return TPM_RC_VALUE;
    946   }
    947   if (target->sizeofSelect < PCR_SELECT_MIN) {
    948     return TPM_RC_VALUE;
    949   }
    950   for (i = 0; i < target->sizeofSelect; ++i) {
    951     result = BYTE_Unmarshal(&target->pcrSelect[i], buffer, size);
    952     if (result != TPM_RC_SUCCESS) {
    953       return result;
    954     }
    955   }
    956   return TPM_RC_SUCCESS;
    957 }
    958 
    959 UINT16 UINT32_Marshal(UINT32* source, BYTE** buffer, INT32* size) {
    960   return uint32_t_Marshal(source, buffer, size);
    961 }
    962 
    963 TPM_RC UINT32_Unmarshal(UINT32* target, BYTE** buffer, INT32* size) {
    964   return uint32_t_Unmarshal(target, buffer, size);
    965 }
    966 
    967 UINT16 TPML_PCR_SELECTION_Marshal(TPML_PCR_SELECTION* source,
    968                                   BYTE** buffer,
    969                                   INT32* size) {
    970   UINT16 total_size = 0;
    971   INT32 i;
    972   total_size += UINT32_Marshal(&source->count, buffer, size);
    973   for (i = 0; i < source->count; ++i) {
    974     total_size +=
    975         TPMS_PCR_SELECTION_Marshal(&source->pcrSelections[i], buffer, size);
    976   }
    977   return total_size;
    978 }
    979 
    980 TPM_RC TPML_PCR_SELECTION_Unmarshal(TPML_PCR_SELECTION* target,
    981                                     BYTE** buffer,
    982                                     INT32* size) {
    983   TPM_RC result;
    984   INT32 i;
    985   result = UINT32_Unmarshal(&target->count, buffer, size);
    986   if (result != TPM_RC_SUCCESS) {
    987     return result;
    988   }
    989   if (target->count > HASH_COUNT) {
    990     return TPM_RC_SIZE;
    991   }
    992   for (i = 0; i < target->count; ++i) {
    993     result =
    994         TPMS_PCR_SELECTION_Unmarshal(&target->pcrSelections[i], buffer, size);
    995     if (result != TPM_RC_SUCCESS) {
    996       return result;
    997     }
    998   }
    999   return TPM_RC_SUCCESS;
   1000 }
   1001 
   1002 UINT16 TPMS_CREATION_DATA_Marshal(TPMS_CREATION_DATA* source,
   1003                                   BYTE** buffer,
   1004                                   INT32* size) {
   1005   UINT16 total_size = 0;
   1006   total_size += TPML_PCR_SELECTION_Marshal(&source->pcrSelect, buffer, size);
   1007   total_size += TPM2B_DIGEST_Marshal(&source->pcrDigest, buffer, size);
   1008   total_size += TPMA_LOCALITY_Marshal(&source->locality, buffer, size);
   1009   total_size += TPM_ALG_ID_Marshal(&source->parentNameAlg, buffer, size);
   1010   total_size += TPM2B_NAME_Marshal(&source->parentName, buffer, size);
   1011   total_size += TPM2B_NAME_Marshal(&source->parentQualifiedName, buffer, size);
   1012   total_size += TPM2B_DATA_Marshal(&source->outsideInfo, buffer, size);
   1013   return total_size;
   1014 }
   1015 
   1016 TPM_RC TPMS_CREATION_DATA_Unmarshal(TPMS_CREATION_DATA* target,
   1017                                     BYTE** buffer,
   1018                                     INT32* size) {
   1019   TPM_RC result;
   1020   result = TPML_PCR_SELECTION_Unmarshal(&target->pcrSelect, buffer, size);
   1021   if (result != TPM_RC_SUCCESS) {
   1022     return result;
   1023   }
   1024   result = TPM2B_DIGEST_Unmarshal(&target->pcrDigest, buffer, size);
   1025   if (result != TPM_RC_SUCCESS) {
   1026     return result;
   1027   }
   1028   result = TPMA_LOCALITY_Unmarshal(&target->locality, buffer, size);
   1029   if (result != TPM_RC_SUCCESS) {
   1030     return result;
   1031   }
   1032   result = TPM_ALG_ID_Unmarshal(&target->parentNameAlg, buffer, size);
   1033   if (result != TPM_RC_SUCCESS) {
   1034     return result;
   1035   }
   1036   result = TPM2B_NAME_Unmarshal(&target->parentName, buffer, size);
   1037   if (result != TPM_RC_SUCCESS) {
   1038     return result;
   1039   }
   1040   result = TPM2B_NAME_Unmarshal(&target->parentQualifiedName, buffer, size);
   1041   if (result != TPM_RC_SUCCESS) {
   1042     return result;
   1043   }
   1044   result = TPM2B_DATA_Unmarshal(&target->outsideInfo, buffer, size);
   1045   if (result != TPM_RC_SUCCESS) {
   1046     return result;
   1047   }
   1048   return TPM_RC_SUCCESS;
   1049 }
   1050 
   1051 UINT16 TPM2B_CREATION_DATA_Marshal(TPM2B_CREATION_DATA* source,
   1052                                    BYTE** buffer,
   1053                                    INT32* size) {
   1054   UINT16 total_size = 0;
   1055   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1056   total_size +=
   1057       TPMS_CREATION_DATA_Marshal(&source->t.creationData, buffer, size);
   1058   {
   1059     BYTE* size_location = *buffer - total_size;
   1060     INT32 size_field_size = sizeof(UINT16);
   1061     UINT16 payload_size = total_size - (UINT16)size_field_size;
   1062     UINT16_Marshal(&payload_size, &size_location, &size_field_size);
   1063   }
   1064   return total_size;
   1065 }
   1066 
   1067 TPM_RC TPM2B_CREATION_DATA_Unmarshal(TPM2B_CREATION_DATA* target,
   1068                                      BYTE** buffer,
   1069                                      INT32* size) {
   1070   TPM_RC result;
   1071   UINT32 start_size = *size;
   1072   UINT32 struct_size;
   1073   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1074   if (result != TPM_RC_SUCCESS) {
   1075     return result;
   1076   }
   1077   if (target->t.size == 0) {
   1078     return TPM_RC_SIZE;
   1079   }
   1080   result = TPMS_CREATION_DATA_Unmarshal(&target->t.creationData, buffer, size);
   1081   if (result != TPM_RC_SUCCESS) {
   1082     return result;
   1083   }
   1084   struct_size = start_size - *size - sizeof(target->t.size);
   1085   if (struct_size != target->t.size) {
   1086     return TPM_RC_SIZE;
   1087   }
   1088   return TPM_RC_SUCCESS;
   1089 }
   1090 
   1091 UINT16 TPM2B_DIGEST_VALUES_Marshal(TPM2B_DIGEST_VALUES* source,
   1092                                    BYTE** buffer,
   1093                                    INT32* size) {
   1094   UINT16 total_size = 0;
   1095   INT32 i;
   1096   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1097   for (i = 0; i < source->t.size; ++i) {
   1098     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1099   }
   1100   return total_size;
   1101 }
   1102 
   1103 TPM_RC TPM2B_DIGEST_VALUES_Unmarshal(TPM2B_DIGEST_VALUES* target,
   1104                                      BYTE** buffer,
   1105                                      INT32* size) {
   1106   TPM_RC result;
   1107   INT32 i;
   1108   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1109   if (result != TPM_RC_SUCCESS) {
   1110     return result;
   1111   }
   1112   if (target->t.size == 0) {
   1113     return TPM_RC_SUCCESS;
   1114   }
   1115   if (target->t.size > sizeof(TPML_DIGEST_VALUES)) {
   1116     return TPM_RC_SIZE;
   1117   }
   1118   for (i = 0; i < target->t.size; ++i) {
   1119     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1120     if (result != TPM_RC_SUCCESS) {
   1121       return result;
   1122     }
   1123   }
   1124   return TPM_RC_SUCCESS;
   1125 }
   1126 
   1127 UINT16 TPM2B_ECC_PARAMETER_Marshal(TPM2B_ECC_PARAMETER* source,
   1128                                    BYTE** buffer,
   1129                                    INT32* size) {
   1130   UINT16 total_size = 0;
   1131   INT32 i;
   1132   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1133   for (i = 0; i < source->t.size; ++i) {
   1134     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1135   }
   1136   return total_size;
   1137 }
   1138 
   1139 TPM_RC TPM2B_ECC_PARAMETER_Unmarshal(TPM2B_ECC_PARAMETER* target,
   1140                                      BYTE** buffer,
   1141                                      INT32* size) {
   1142   TPM_RC result;
   1143   INT32 i;
   1144   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1145   if (result != TPM_RC_SUCCESS) {
   1146     return result;
   1147   }
   1148   if (target->t.size == 0) {
   1149     return TPM_RC_SUCCESS;
   1150   }
   1151   if (target->t.size > MAX_ECC_KEY_BYTES) {
   1152     return TPM_RC_SIZE;
   1153   }
   1154   for (i = 0; i < target->t.size; ++i) {
   1155     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1156     if (result != TPM_RC_SUCCESS) {
   1157       return result;
   1158     }
   1159   }
   1160   return TPM_RC_SUCCESS;
   1161 }
   1162 
   1163 UINT16 TPMS_ECC_POINT_Marshal(TPMS_ECC_POINT* source,
   1164                               BYTE** buffer,
   1165                               INT32* size) {
   1166   UINT16 total_size = 0;
   1167   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->x, buffer, size);
   1168   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->y, buffer, size);
   1169   return total_size;
   1170 }
   1171 
   1172 TPM_RC TPMS_ECC_POINT_Unmarshal(TPMS_ECC_POINT* target,
   1173                                 BYTE** buffer,
   1174                                 INT32* size) {
   1175   TPM_RC result;
   1176   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->x, buffer, size);
   1177   if (result != TPM_RC_SUCCESS) {
   1178     return result;
   1179   }
   1180   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->y, buffer, size);
   1181   if (result != TPM_RC_SUCCESS) {
   1182     return result;
   1183   }
   1184   return TPM_RC_SUCCESS;
   1185 }
   1186 
   1187 UINT16 TPM2B_ECC_POINT_Marshal(TPM2B_ECC_POINT* source,
   1188                                BYTE** buffer,
   1189                                INT32* size) {
   1190   UINT16 total_size = 0;
   1191   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1192   total_size += TPMS_ECC_POINT_Marshal(&source->t.point, buffer, size);
   1193   {
   1194     BYTE* size_location = *buffer - total_size;
   1195     INT32 size_field_size = sizeof(UINT16);
   1196     UINT16 payload_size = total_size - (UINT16)size_field_size;
   1197     UINT16_Marshal(&payload_size, &size_location, &size_field_size);
   1198   }
   1199   return total_size;
   1200 }
   1201 
   1202 TPM_RC TPM2B_ECC_POINT_Unmarshal(TPM2B_ECC_POINT* target,
   1203                                  BYTE** buffer,
   1204                                  INT32* size) {
   1205   TPM_RC result;
   1206   UINT32 start_size = *size;
   1207   UINT32 struct_size;
   1208   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1209   if (result != TPM_RC_SUCCESS) {
   1210     return result;
   1211   }
   1212   if (target->t.size == 0) {
   1213     return TPM_RC_SIZE;
   1214   }
   1215   result = TPMS_ECC_POINT_Unmarshal(&target->t.point, buffer, size);
   1216   if (result != TPM_RC_SUCCESS) {
   1217     return result;
   1218   }
   1219   struct_size = start_size - *size - sizeof(target->t.size);
   1220   if (struct_size != target->t.size) {
   1221     return TPM_RC_SIZE;
   1222   }
   1223   return TPM_RC_SUCCESS;
   1224 }
   1225 
   1226 UINT16 TPM2B_ENCRYPTED_SECRET_Marshal(TPM2B_ENCRYPTED_SECRET* source,
   1227                                       BYTE** buffer,
   1228                                       INT32* size) {
   1229   UINT16 total_size = 0;
   1230   INT32 i;
   1231   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1232   for (i = 0; i < source->t.size; ++i) {
   1233     total_size += BYTE_Marshal(&source->t.secret[i], buffer, size);
   1234   }
   1235   return total_size;
   1236 }
   1237 
   1238 TPM_RC TPM2B_ENCRYPTED_SECRET_Unmarshal(TPM2B_ENCRYPTED_SECRET* target,
   1239                                         BYTE** buffer,
   1240                                         INT32* size) {
   1241   TPM_RC result;
   1242   INT32 i;
   1243   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1244   if (result != TPM_RC_SUCCESS) {
   1245     return result;
   1246   }
   1247   if (target->t.size == 0) {
   1248     return TPM_RC_SUCCESS;
   1249   }
   1250   if (target->t.size > sizeof(TPMU_ENCRYPTED_SECRET)) {
   1251     return TPM_RC_SIZE;
   1252   }
   1253   for (i = 0; i < target->t.size; ++i) {
   1254     result = BYTE_Unmarshal(&target->t.secret[i], buffer, size);
   1255     if (result != TPM_RC_SUCCESS) {
   1256       return result;
   1257     }
   1258   }
   1259   return TPM_RC_SUCCESS;
   1260 }
   1261 
   1262 UINT16 TPM2B_EVENT_Marshal(TPM2B_EVENT* source, BYTE** buffer, INT32* size) {
   1263   UINT16 total_size = 0;
   1264   INT32 i;
   1265   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1266   for (i = 0; i < source->t.size; ++i) {
   1267     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1268   }
   1269   return total_size;
   1270 }
   1271 
   1272 TPM_RC TPM2B_EVENT_Unmarshal(TPM2B_EVENT* target, BYTE** buffer, INT32* size) {
   1273   TPM_RC result;
   1274   INT32 i;
   1275   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1276   if (result != TPM_RC_SUCCESS) {
   1277     return result;
   1278   }
   1279   if (target->t.size == 0) {
   1280     return TPM_RC_SUCCESS;
   1281   }
   1282   if (target->t.size > 1024) {
   1283     return TPM_RC_SIZE;
   1284   }
   1285   for (i = 0; i < target->t.size; ++i) {
   1286     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1287     if (result != TPM_RC_SUCCESS) {
   1288       return result;
   1289     }
   1290   }
   1291   return TPM_RC_SUCCESS;
   1292 }
   1293 
   1294 UINT16 TPM2B_ID_OBJECT_Marshal(TPM2B_ID_OBJECT* source,
   1295                                BYTE** buffer,
   1296                                INT32* size) {
   1297   UINT16 total_size = 0;
   1298   INT32 i;
   1299   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1300   for (i = 0; i < source->t.size; ++i) {
   1301     total_size += BYTE_Marshal(&source->t.credential[i], buffer, size);
   1302   }
   1303   return total_size;
   1304 }
   1305 
   1306 TPM_RC TPM2B_ID_OBJECT_Unmarshal(TPM2B_ID_OBJECT* target,
   1307                                  BYTE** buffer,
   1308                                  INT32* size) {
   1309   TPM_RC result;
   1310   INT32 i;
   1311   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1312   if (result != TPM_RC_SUCCESS) {
   1313     return result;
   1314   }
   1315   if (target->t.size == 0) {
   1316     return TPM_RC_SUCCESS;
   1317   }
   1318   if (target->t.size > sizeof(_ID_OBJECT)) {
   1319     return TPM_RC_SIZE;
   1320   }
   1321   for (i = 0; i < target->t.size; ++i) {
   1322     result = BYTE_Unmarshal(&target->t.credential[i], buffer, size);
   1323     if (result != TPM_RC_SUCCESS) {
   1324       return result;
   1325     }
   1326   }
   1327   return TPM_RC_SUCCESS;
   1328 }
   1329 
   1330 UINT16 TPM2B_IV_Marshal(TPM2B_IV* source, BYTE** buffer, INT32* size) {
   1331   UINT16 total_size = 0;
   1332   INT32 i;
   1333   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1334   for (i = 0; i < source->t.size; ++i) {
   1335     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1336   }
   1337   return total_size;
   1338 }
   1339 
   1340 TPM_RC TPM2B_IV_Unmarshal(TPM2B_IV* target, BYTE** buffer, INT32* size) {
   1341   TPM_RC result;
   1342   INT32 i;
   1343   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1344   if (result != TPM_RC_SUCCESS) {
   1345     return result;
   1346   }
   1347   if (target->t.size == 0) {
   1348     return TPM_RC_SUCCESS;
   1349   }
   1350   if (target->t.size > MAX_SYM_BLOCK_SIZE) {
   1351     return TPM_RC_SIZE;
   1352   }
   1353   for (i = 0; i < target->t.size; ++i) {
   1354     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1355     if (result != TPM_RC_SUCCESS) {
   1356       return result;
   1357     }
   1358   }
   1359   return TPM_RC_SUCCESS;
   1360 }
   1361 
   1362 UINT16 TPM2B_MAX_BUFFER_Marshal(TPM2B_MAX_BUFFER* source,
   1363                                 BYTE** buffer,
   1364                                 INT32* size) {
   1365   UINT16 total_size = 0;
   1366   INT32 i;
   1367   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1368   for (i = 0; i < source->t.size; ++i) {
   1369     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1370   }
   1371   return total_size;
   1372 }
   1373 
   1374 TPM_RC TPM2B_MAX_BUFFER_Unmarshal(TPM2B_MAX_BUFFER* target,
   1375                                   BYTE** buffer,
   1376                                   INT32* size) {
   1377   TPM_RC result;
   1378   INT32 i;
   1379   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1380   if (result != TPM_RC_SUCCESS) {
   1381     return result;
   1382   }
   1383   if (target->t.size == 0) {
   1384     return TPM_RC_SUCCESS;
   1385   }
   1386   if (target->t.size > MAX_DIGEST_BUFFER) {
   1387     return TPM_RC_SIZE;
   1388   }
   1389   for (i = 0; i < target->t.size; ++i) {
   1390     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1391     if (result != TPM_RC_SUCCESS) {
   1392       return result;
   1393     }
   1394   }
   1395   return TPM_RC_SUCCESS;
   1396 }
   1397 
   1398 UINT16 TPM2B_MAX_NV_BUFFER_Marshal(TPM2B_MAX_NV_BUFFER* source,
   1399                                    BYTE** buffer,
   1400                                    INT32* size) {
   1401   UINT16 total_size = 0;
   1402   INT32 i;
   1403   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1404   for (i = 0; i < source->t.size; ++i) {
   1405     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1406   }
   1407   return total_size;
   1408 }
   1409 
   1410 TPM_RC TPM2B_MAX_NV_BUFFER_Unmarshal(TPM2B_MAX_NV_BUFFER* target,
   1411                                      BYTE** buffer,
   1412                                      INT32* size) {
   1413   TPM_RC result;
   1414   INT32 i;
   1415   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1416   if (result != TPM_RC_SUCCESS) {
   1417     return result;
   1418   }
   1419   if (target->t.size == 0) {
   1420     return TPM_RC_SUCCESS;
   1421   }
   1422   if (target->t.size > MAX_NV_BUFFER_SIZE) {
   1423     return TPM_RC_SIZE;
   1424   }
   1425   for (i = 0; i < target->t.size; ++i) {
   1426     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1427     if (result != TPM_RC_SUCCESS) {
   1428       return result;
   1429     }
   1430   }
   1431   return TPM_RC_SUCCESS;
   1432 }
   1433 
   1434 UINT16 TPM2B_NONCE_Marshal(TPM2B_NONCE* source, BYTE** buffer, INT32* size) {
   1435   return TPM2B_DIGEST_Marshal(source, buffer, size);
   1436 }
   1437 
   1438 TPM_RC TPM2B_NONCE_Unmarshal(TPM2B_NONCE* target, BYTE** buffer, INT32* size) {
   1439   return TPM2B_DIGEST_Unmarshal(target, buffer, size);
   1440 }
   1441 
   1442 UINT16 TPMI_RH_NV_INDEX_Marshal(TPMI_RH_NV_INDEX* source,
   1443                                 BYTE** buffer,
   1444                                 INT32* size) {
   1445   return uint32_t_Marshal(source, buffer, size);
   1446 }
   1447 
   1448 TPM_RC TPMI_RH_NV_INDEX_Unmarshal(TPMI_RH_NV_INDEX* target,
   1449                                   BYTE** buffer,
   1450                                   INT32* size) {
   1451   TPM_RC result;
   1452   BOOL has_valid_value = FALSE;
   1453   result = uint32_t_Unmarshal(target, buffer, size);
   1454   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   1455     return result;
   1456   }
   1457   if ((*target >= NV_INDEX_FIRST) && (*target <= NV_INDEX_LAST)) {
   1458     has_valid_value = TRUE;
   1459   }
   1460   if (!has_valid_value) {
   1461     return TPM_RC_VALUE;
   1462   }
   1463   return TPM_RC_SUCCESS;
   1464 }
   1465 
   1466 UINT16 TPMA_NV_Marshal(TPMA_NV* source, BYTE** buffer, INT32* size) {
   1467   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   1468 }
   1469 
   1470 TPM_RC TPMA_NV_Unmarshal(TPMA_NV* target, BYTE** buffer, INT32* size) {
   1471   TPM_RC result;
   1472   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   1473   if (result != TPM_RC_SUCCESS) {
   1474     return result;
   1475   }
   1476   if (target->reserved7_9 != 0) {
   1477     return TPM_RC_RESERVED_BITS;
   1478   }
   1479   if (target->reserved20_24 != 0) {
   1480     return TPM_RC_RESERVED_BITS;
   1481   }
   1482   return TPM_RC_SUCCESS;
   1483 }
   1484 
   1485 UINT16 TPMS_NV_PUBLIC_Marshal(TPMS_NV_PUBLIC* source,
   1486                               BYTE** buffer,
   1487                               INT32* size) {
   1488   UINT16 total_size = 0;
   1489   total_size += TPMI_RH_NV_INDEX_Marshal(&source->nvIndex, buffer, size);
   1490   total_size += TPMI_ALG_HASH_Marshal(&source->nameAlg, buffer, size);
   1491   total_size += TPMA_NV_Marshal(&source->attributes, buffer, size);
   1492   total_size += TPM2B_DIGEST_Marshal(&source->authPolicy, buffer, size);
   1493   total_size += UINT16_Marshal(&source->dataSize, buffer, size);
   1494   return total_size;
   1495 }
   1496 
   1497 TPM_RC TPMS_NV_PUBLIC_Unmarshal(TPMS_NV_PUBLIC* target,
   1498                                 BYTE** buffer,
   1499                                 INT32* size) {
   1500   TPM_RC result;
   1501   result = TPMI_RH_NV_INDEX_Unmarshal(&target->nvIndex, buffer, size);
   1502   if (result != TPM_RC_SUCCESS) {
   1503     return result;
   1504   }
   1505   result = TPMI_ALG_HASH_Unmarshal(&target->nameAlg, buffer, size, FALSE);
   1506   if (result != TPM_RC_SUCCESS) {
   1507     return result;
   1508   }
   1509   result = TPMA_NV_Unmarshal(&target->attributes, buffer, size);
   1510   if (result != TPM_RC_SUCCESS) {
   1511     return result;
   1512   }
   1513   result = TPM2B_DIGEST_Unmarshal(&target->authPolicy, buffer, size);
   1514   if (result != TPM_RC_SUCCESS) {
   1515     return result;
   1516   }
   1517   result = UINT16_Unmarshal(&target->dataSize, buffer, size);
   1518   if (result != TPM_RC_SUCCESS) {
   1519     return result;
   1520   }
   1521   if (target->dataSize > MAX_NV_INDEX_SIZE) {
   1522     return TPM_RC_SIZE;
   1523   }
   1524   return TPM_RC_SUCCESS;
   1525 }
   1526 
   1527 UINT16 TPM2B_NV_PUBLIC_Marshal(TPM2B_NV_PUBLIC* source,
   1528                                BYTE** buffer,
   1529                                INT32* size) {
   1530   UINT16 total_size = 0;
   1531   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1532   total_size += TPMS_NV_PUBLIC_Marshal(&source->t.nvPublic, buffer, size);
   1533   {
   1534     BYTE* size_location = *buffer - total_size;
   1535     INT32 size_field_size = sizeof(UINT16);
   1536     UINT16 payload_size = total_size - (UINT16)size_field_size;
   1537     UINT16_Marshal(&payload_size, &size_location, &size_field_size);
   1538   }
   1539   return total_size;
   1540 }
   1541 
   1542 TPM_RC TPM2B_NV_PUBLIC_Unmarshal(TPM2B_NV_PUBLIC* target,
   1543                                  BYTE** buffer,
   1544                                  INT32* size) {
   1545   TPM_RC result;
   1546   UINT32 start_size = *size;
   1547   UINT32 struct_size;
   1548   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1549   if (result != TPM_RC_SUCCESS) {
   1550     return result;
   1551   }
   1552   if (target->t.size == 0) {
   1553     return TPM_RC_SIZE;
   1554   }
   1555   result = TPMS_NV_PUBLIC_Unmarshal(&target->t.nvPublic, buffer, size);
   1556   if (result != TPM_RC_SUCCESS) {
   1557     return result;
   1558   }
   1559   struct_size = start_size - *size - sizeof(target->t.size);
   1560   if (struct_size != target->t.size) {
   1561     return TPM_RC_SIZE;
   1562   }
   1563   return TPM_RC_SUCCESS;
   1564 }
   1565 
   1566 UINT16 TPM2B_OPERAND_Marshal(TPM2B_OPERAND* source,
   1567                              BYTE** buffer,
   1568                              INT32* size) {
   1569   return TPM2B_DIGEST_Marshal(source, buffer, size);
   1570 }
   1571 
   1572 TPM_RC TPM2B_OPERAND_Unmarshal(TPM2B_OPERAND* target,
   1573                                BYTE** buffer,
   1574                                INT32* size) {
   1575   return TPM2B_DIGEST_Unmarshal(target, buffer, size);
   1576 }
   1577 
   1578 UINT16 TPM2B_PRIVATE_Marshal(TPM2B_PRIVATE* source,
   1579                              BYTE** buffer,
   1580                              INT32* size) {
   1581   UINT16 total_size = 0;
   1582   INT32 i;
   1583   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1584   for (i = 0; i < source->t.size; ++i) {
   1585     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1586   }
   1587   return total_size;
   1588 }
   1589 
   1590 TPM_RC TPM2B_PRIVATE_Unmarshal(TPM2B_PRIVATE* target,
   1591                                BYTE** buffer,
   1592                                INT32* size) {
   1593   TPM_RC result;
   1594   INT32 i;
   1595   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1596   if (result != TPM_RC_SUCCESS) {
   1597     return result;
   1598   }
   1599   if (target->t.size == 0) {
   1600     return TPM_RC_SUCCESS;
   1601   }
   1602   if (target->t.size > sizeof(_PRIVATE)) {
   1603     return TPM_RC_SIZE;
   1604   }
   1605   for (i = 0; i < target->t.size; ++i) {
   1606     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1607     if (result != TPM_RC_SUCCESS) {
   1608       return result;
   1609     }
   1610   }
   1611   return TPM_RC_SUCCESS;
   1612 }
   1613 
   1614 UINT16 TPM2B_PRIVATE_KEY_RSA_Marshal(TPM2B_PRIVATE_KEY_RSA* source,
   1615                                      BYTE** buffer,
   1616                                      INT32* size) {
   1617   UINT16 total_size = 0;
   1618   INT32 i;
   1619   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1620   for (i = 0; i < source->t.size; ++i) {
   1621     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1622   }
   1623   return total_size;
   1624 }
   1625 
   1626 TPM_RC TPM2B_PRIVATE_KEY_RSA_Unmarshal(TPM2B_PRIVATE_KEY_RSA* target,
   1627                                        BYTE** buffer,
   1628                                        INT32* size) {
   1629   TPM_RC result;
   1630   INT32 i;
   1631   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1632   if (result != TPM_RC_SUCCESS) {
   1633     return result;
   1634   }
   1635   if (target->t.size == 0) {
   1636     return TPM_RC_SUCCESS;
   1637   }
   1638   if (target->t.size > MAX_RSA_KEY_BYTES / 2) {
   1639     return TPM_RC_SIZE;
   1640   }
   1641   for (i = 0; i < target->t.size; ++i) {
   1642     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1643     if (result != TPM_RC_SUCCESS) {
   1644       return result;
   1645     }
   1646   }
   1647   return TPM_RC_SUCCESS;
   1648 }
   1649 
   1650 UINT16 TPM2B_PRIVATE_VENDOR_SPECIFIC_Marshal(
   1651     TPM2B_PRIVATE_VENDOR_SPECIFIC* source,
   1652     BYTE** buffer,
   1653     INT32* size) {
   1654   UINT16 total_size = 0;
   1655   INT32 i;
   1656   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1657   for (i = 0; i < source->t.size; ++i) {
   1658     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1659   }
   1660   return total_size;
   1661 }
   1662 
   1663 TPM_RC TPM2B_PRIVATE_VENDOR_SPECIFIC_Unmarshal(
   1664     TPM2B_PRIVATE_VENDOR_SPECIFIC* target,
   1665     BYTE** buffer,
   1666     INT32* size) {
   1667   TPM_RC result;
   1668   INT32 i;
   1669   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1670   if (result != TPM_RC_SUCCESS) {
   1671     return result;
   1672   }
   1673   if (target->t.size == 0) {
   1674     return TPM_RC_SUCCESS;
   1675   }
   1676   if (target->t.size > PRIVATE_VENDOR_SPECIFIC_BYTES) {
   1677     return TPM_RC_SIZE;
   1678   }
   1679   for (i = 0; i < target->t.size; ++i) {
   1680     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1681     if (result != TPM_RC_SUCCESS) {
   1682       return result;
   1683     }
   1684   }
   1685   return TPM_RC_SUCCESS;
   1686 }
   1687 
   1688 UINT16 TPMA_OBJECT_Marshal(TPMA_OBJECT* source, BYTE** buffer, INT32* size) {
   1689   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   1690 }
   1691 
   1692 TPM_RC TPMA_OBJECT_Unmarshal(TPMA_OBJECT* target, BYTE** buffer, INT32* size) {
   1693   TPM_RC result;
   1694   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   1695   if (result != TPM_RC_SUCCESS) {
   1696     return result;
   1697   }
   1698   if (target->reserved0 != 0) {
   1699     return TPM_RC_RESERVED_BITS;
   1700   }
   1701   if (target->reserved3 != 0) {
   1702     return TPM_RC_RESERVED_BITS;
   1703   }
   1704   if (target->reserved8_9 != 0) {
   1705     return TPM_RC_RESERVED_BITS;
   1706   }
   1707   if (target->reserved12_15 != 0) {
   1708     return TPM_RC_RESERVED_BITS;
   1709   }
   1710   if (target->reserved19_31 != 0) {
   1711     return TPM_RC_RESERVED_BITS;
   1712   }
   1713   return TPM_RC_SUCCESS;
   1714 }
   1715 
   1716 UINT16 TPMI_ALG_PUBLIC_Marshal(TPMI_ALG_PUBLIC* source,
   1717                                BYTE** buffer,
   1718                                INT32* size) {
   1719   return uint16_t_Marshal(source, buffer, size);
   1720 }
   1721 
   1722 TPM_RC TPMI_ALG_PUBLIC_Unmarshal(TPMI_ALG_PUBLIC* target,
   1723                                  BYTE** buffer,
   1724                                  INT32* size) {
   1725   TPM_RC result;
   1726   BOOL has_valid_value = FALSE;
   1727   result = uint16_t_Unmarshal(target, buffer, size);
   1728   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   1729     return result;
   1730   }
   1731   switch (*target) {
   1732 #ifdef TPM_ALG_RSA
   1733     case TPM_ALG_RSA:
   1734 #endif
   1735 #ifdef TPM_ALG_KEYEDHASH
   1736     case TPM_ALG_KEYEDHASH:
   1737 #endif
   1738 #ifdef TPM_ALG_ECC
   1739     case TPM_ALG_ECC:
   1740 #endif
   1741 #ifdef TPM_ALG_SYMCIPHER
   1742     case TPM_ALG_SYMCIPHER:
   1743 #endif
   1744       has_valid_value = TRUE;
   1745       break;
   1746   }
   1747   if (!has_valid_value) {
   1748     return TPM_RC_TYPE;
   1749   }
   1750   return TPM_RC_SUCCESS;
   1751 }
   1752 
   1753 UINT16 TPM2B_PUBLIC_KEY_RSA_Marshal(TPM2B_PUBLIC_KEY_RSA* source,
   1754                                     BYTE** buffer,
   1755                                     INT32* size) {
   1756   UINT16 total_size = 0;
   1757   INT32 i;
   1758   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   1759   for (i = 0; i < source->t.size; ++i) {
   1760     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   1761   }
   1762   return total_size;
   1763 }
   1764 
   1765 TPM_RC TPM2B_PUBLIC_KEY_RSA_Unmarshal(TPM2B_PUBLIC_KEY_RSA* target,
   1766                                       BYTE** buffer,
   1767                                       INT32* size) {
   1768   TPM_RC result;
   1769   INT32 i;
   1770   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   1771   if (result != TPM_RC_SUCCESS) {
   1772     return result;
   1773   }
   1774   if (target->t.size == 0) {
   1775     return TPM_RC_SUCCESS;
   1776   }
   1777   if (target->t.size > MAX_RSA_KEY_BYTES) {
   1778     return TPM_RC_SIZE;
   1779   }
   1780   for (i = 0; i < target->t.size; ++i) {
   1781     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   1782     if (result != TPM_RC_SUCCESS) {
   1783       return result;
   1784     }
   1785   }
   1786   return TPM_RC_SUCCESS;
   1787 }
   1788 
   1789 UINT16 TPMU_PUBLIC_ID_Marshal(TPMU_PUBLIC_ID* source,
   1790                               BYTE** buffer,
   1791                               INT32* size,
   1792                               UINT32 selector) {
   1793   switch (selector) {
   1794 #ifdef TPM_ALG_KEYEDHASH
   1795     case TPM_ALG_KEYEDHASH:
   1796       return TPM2B_DIGEST_Marshal((TPM2B_DIGEST*)&source->keyedHash, buffer,
   1797                                   size);
   1798 #endif
   1799 #ifdef TPM_ALG_SYMCIPHER
   1800     case TPM_ALG_SYMCIPHER:
   1801       return TPM2B_DIGEST_Marshal((TPM2B_DIGEST*)&source->sym, buffer, size);
   1802 #endif
   1803 #ifdef TPM_ALG_RSA
   1804     case TPM_ALG_RSA:
   1805       return TPM2B_PUBLIC_KEY_RSA_Marshal((TPM2B_PUBLIC_KEY_RSA*)&source->rsa,
   1806                                           buffer, size);
   1807 #endif
   1808 #ifdef TPM_ALG_ECC
   1809     case TPM_ALG_ECC:
   1810       return TPMS_ECC_POINT_Marshal((TPMS_ECC_POINT*)&source->ecc, buffer,
   1811                                     size);
   1812 #endif
   1813   }
   1814   return 0;
   1815 }
   1816 
   1817 TPM_RC TPMU_PUBLIC_ID_Unmarshal(TPMU_PUBLIC_ID* target,
   1818                                 BYTE** buffer,
   1819                                 INT32* size,
   1820                                 UINT32 selector) {
   1821   switch (selector) {
   1822 #ifdef TPM_ALG_KEYEDHASH
   1823     case TPM_ALG_KEYEDHASH:
   1824       return TPM2B_DIGEST_Unmarshal((TPM2B_DIGEST*)&target->keyedHash, buffer,
   1825                                     size);
   1826 #endif
   1827 #ifdef TPM_ALG_SYMCIPHER
   1828     case TPM_ALG_SYMCIPHER:
   1829       return TPM2B_DIGEST_Unmarshal((TPM2B_DIGEST*)&target->sym, buffer, size);
   1830 #endif
   1831 #ifdef TPM_ALG_RSA
   1832     case TPM_ALG_RSA:
   1833       return TPM2B_PUBLIC_KEY_RSA_Unmarshal((TPM2B_PUBLIC_KEY_RSA*)&target->rsa,
   1834                                             buffer, size);
   1835 #endif
   1836 #ifdef TPM_ALG_ECC
   1837     case TPM_ALG_ECC:
   1838       return TPMS_ECC_POINT_Unmarshal((TPMS_ECC_POINT*)&target->ecc, buffer,
   1839                                       size);
   1840 #endif
   1841   }
   1842   return TPM_RC_SELECTOR;
   1843 }
   1844 
   1845 UINT16 TPM_KEY_BITS_Marshal(TPM_KEY_BITS* source, BYTE** buffer, INT32* size) {
   1846   return uint16_t_Marshal(source, buffer, size);
   1847 }
   1848 
   1849 TPM_RC TPM_KEY_BITS_Unmarshal(TPM_KEY_BITS* target,
   1850                               BYTE** buffer,
   1851                               INT32* size) {
   1852   return uint16_t_Unmarshal(target, buffer, size);
   1853 }
   1854 
   1855 UINT16 TPMI_AES_KEY_BITS_Marshal(TPMI_AES_KEY_BITS* source,
   1856                                  BYTE** buffer,
   1857                                  INT32* size) {
   1858   return TPM_KEY_BITS_Marshal(source, buffer, size);
   1859 }
   1860 
   1861 TPM_RC TPMI_AES_KEY_BITS_Unmarshal(TPMI_AES_KEY_BITS* target,
   1862                                    BYTE** buffer,
   1863                                    INT32* size) {
   1864   TPM_RC result;
   1865   uint16_t supported_values[] = AES_KEY_SIZES_BITS;
   1866   size_t length = sizeof(supported_values) / sizeof(supported_values[0]);
   1867   size_t i;
   1868   BOOL is_supported_value = FALSE;
   1869   result = TPM_KEY_BITS_Unmarshal(target, buffer, size);
   1870   if (result != TPM_RC_SUCCESS) {
   1871     return result;
   1872   }
   1873   for (i = 0; i < length; ++i) {
   1874     if (*target == supported_values[i]) {
   1875       is_supported_value = TRUE;
   1876       break;
   1877     }
   1878   }
   1879   if (!is_supported_value) {
   1880     return TPM_RC_VALUE;
   1881   }
   1882   return TPM_RC_SUCCESS;
   1883 }
   1884 
   1885 UINT16 TPMI_SM4_KEY_BITS_Marshal(TPMI_SM4_KEY_BITS* source,
   1886                                  BYTE** buffer,
   1887                                  INT32* size) {
   1888   return TPM_KEY_BITS_Marshal(source, buffer, size);
   1889 }
   1890 
   1891 TPM_RC TPMI_SM4_KEY_BITS_Unmarshal(TPMI_SM4_KEY_BITS* target,
   1892                                    BYTE** buffer,
   1893                                    INT32* size) {
   1894   TPM_RC result;
   1895   uint16_t supported_values[] = SM4_KEY_SIZES_BITS;
   1896   size_t length = sizeof(supported_values) / sizeof(supported_values[0]);
   1897   size_t i;
   1898   BOOL is_supported_value = FALSE;
   1899   result = TPM_KEY_BITS_Unmarshal(target, buffer, size);
   1900   if (result != TPM_RC_SUCCESS) {
   1901     return result;
   1902   }
   1903   for (i = 0; i < length; ++i) {
   1904     if (*target == supported_values[i]) {
   1905       is_supported_value = TRUE;
   1906       break;
   1907     }
   1908   }
   1909   if (!is_supported_value) {
   1910     return TPM_RC_VALUE;
   1911   }
   1912   return TPM_RC_SUCCESS;
   1913 }
   1914 
   1915 UINT16 TPMI_CAMELLIA_KEY_BITS_Marshal(TPMI_CAMELLIA_KEY_BITS* source,
   1916                                       BYTE** buffer,
   1917                                       INT32* size) {
   1918   return TPM_KEY_BITS_Marshal(source, buffer, size);
   1919 }
   1920 
   1921 TPM_RC TPMI_CAMELLIA_KEY_BITS_Unmarshal(TPMI_CAMELLIA_KEY_BITS* target,
   1922                                         BYTE** buffer,
   1923                                         INT32* size) {
   1924   TPM_RC result;
   1925   uint16_t supported_values[] = CAMELLIA_KEY_SIZES_BITS;
   1926   size_t length = sizeof(supported_values) / sizeof(supported_values[0]);
   1927   size_t i;
   1928   BOOL is_supported_value = FALSE;
   1929   result = TPM_KEY_BITS_Unmarshal(target, buffer, size);
   1930   if (result != TPM_RC_SUCCESS) {
   1931     return result;
   1932   }
   1933   for (i = 0; i < length; ++i) {
   1934     if (*target == supported_values[i]) {
   1935       is_supported_value = TRUE;
   1936       break;
   1937     }
   1938   }
   1939   if (!is_supported_value) {
   1940     return TPM_RC_VALUE;
   1941   }
   1942   return TPM_RC_SUCCESS;
   1943 }
   1944 
   1945 UINT16 TPMU_SYM_KEY_BITS_Marshal(TPMU_SYM_KEY_BITS* source,
   1946                                  BYTE** buffer,
   1947                                  INT32* size,
   1948                                  UINT32 selector) {
   1949   switch (selector) {
   1950 #ifdef TPM_ALG_AES
   1951     case TPM_ALG_AES:
   1952       return TPMI_AES_KEY_BITS_Marshal((TPMI_AES_KEY_BITS*)&source->aes, buffer,
   1953                                        size);
   1954 #endif
   1955 #ifdef TPM_ALG_SM4
   1956     case TPM_ALG_SM4:
   1957       return TPMI_SM4_KEY_BITS_Marshal((TPMI_SM4_KEY_BITS*)&source->sm4, buffer,
   1958                                        size);
   1959 #endif
   1960 #ifdef TPM_ALG_CAMELLIA
   1961     case TPM_ALG_CAMELLIA:
   1962       return TPMI_CAMELLIA_KEY_BITS_Marshal(
   1963           (TPMI_CAMELLIA_KEY_BITS*)&source->camellia, buffer, size);
   1964 #endif
   1965 #ifdef TPM_ALG_XOR
   1966     case TPM_ALG_XOR:
   1967       return TPMI_ALG_HASH_Marshal((TPMI_ALG_HASH*)&source->xor_, buffer, size);
   1968 #endif
   1969 #ifdef TPM_ALG_NULL
   1970     case TPM_ALG_NULL:
   1971       return 0;
   1972 #endif
   1973   }
   1974   return 0;
   1975 }
   1976 
   1977 TPM_RC TPMU_SYM_KEY_BITS_Unmarshal(TPMU_SYM_KEY_BITS* target,
   1978                                    BYTE** buffer,
   1979                                    INT32* size,
   1980                                    UINT32 selector) {
   1981   switch (selector) {
   1982 #ifdef TPM_ALG_AES
   1983     case TPM_ALG_AES:
   1984       return TPMI_AES_KEY_BITS_Unmarshal((TPMI_AES_KEY_BITS*)&target->aes,
   1985                                          buffer, size);
   1986 #endif
   1987 #ifdef TPM_ALG_SM4
   1988     case TPM_ALG_SM4:
   1989       return TPMI_SM4_KEY_BITS_Unmarshal((TPMI_SM4_KEY_BITS*)&target->sm4,
   1990                                          buffer, size);
   1991 #endif
   1992 #ifdef TPM_ALG_CAMELLIA
   1993     case TPM_ALG_CAMELLIA:
   1994       return TPMI_CAMELLIA_KEY_BITS_Unmarshal(
   1995           (TPMI_CAMELLIA_KEY_BITS*)&target->camellia, buffer, size);
   1996 #endif
   1997 #ifdef TPM_ALG_XOR
   1998     case TPM_ALG_XOR:
   1999       return TPMI_ALG_HASH_Unmarshal(&target->xor_, buffer, size, FALSE);
   2000 #endif
   2001 #ifdef TPM_ALG_NULL
   2002     case TPM_ALG_NULL:
   2003       return TPM_RC_SUCCESS;
   2004 #endif
   2005   }
   2006   return TPM_RC_SELECTOR;
   2007 }
   2008 
   2009 UINT16 TPMI_ALG_SYM_MODE_Marshal(TPMI_ALG_SYM_MODE* source,
   2010                                  BYTE** buffer,
   2011                                  INT32* size) {
   2012   return uint16_t_Marshal(source, buffer, size);
   2013 }
   2014 
   2015 TPM_RC TPMI_ALG_SYM_MODE_Unmarshal(TPMI_ALG_SYM_MODE* target,
   2016                                    BYTE** buffer,
   2017                                    INT32* size,
   2018                                    BOOL allow_conditional_value) {
   2019   TPM_RC result;
   2020   BOOL has_valid_value = FALSE;
   2021   result = uint16_t_Unmarshal(target, buffer, size);
   2022   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   2023     return result;
   2024   }
   2025   if (*target == TPM_ALG_NULL) {
   2026     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_MODE;
   2027   }
   2028   switch (*target) {
   2029 #ifdef TPM_ALG_CTR
   2030     case TPM_ALG_CTR:
   2031 #endif
   2032 #ifdef TPM_ALG_OFB
   2033     case TPM_ALG_OFB:
   2034 #endif
   2035 #ifdef TPM_ALG_CBC
   2036     case TPM_ALG_CBC:
   2037 #endif
   2038 #ifdef TPM_ALG_CFB
   2039     case TPM_ALG_CFB:
   2040 #endif
   2041 #ifdef TPM_ALG_ECB
   2042     case TPM_ALG_ECB:
   2043 #endif
   2044       has_valid_value = TRUE;
   2045       break;
   2046   }
   2047   if (!has_valid_value) {
   2048     return TPM_RC_MODE;
   2049   }
   2050   return TPM_RC_SUCCESS;
   2051 }
   2052 
   2053 UINT16 TPMU_SYM_MODE_Marshal(TPMU_SYM_MODE* source,
   2054                              BYTE** buffer,
   2055                              INT32* size,
   2056                              UINT32 selector) {
   2057   switch (selector) {
   2058 #ifdef TPM_ALG_AES
   2059     case TPM_ALG_AES:
   2060       return TPMI_ALG_SYM_MODE_Marshal((TPMI_ALG_SYM_MODE*)&source->aes, buffer,
   2061                                        size);
   2062 #endif
   2063 #ifdef TPM_ALG_SM4
   2064     case TPM_ALG_SM4:
   2065       return TPMI_ALG_SYM_MODE_Marshal((TPMI_ALG_SYM_MODE*)&source->sm4, buffer,
   2066                                        size);
   2067 #endif
   2068 #ifdef TPM_ALG_CAMELLIA
   2069     case TPM_ALG_CAMELLIA:
   2070       return TPMI_ALG_SYM_MODE_Marshal((TPMI_ALG_SYM_MODE*)&source->camellia,
   2071                                        buffer, size);
   2072 #endif
   2073 #ifdef TPM_ALG_XOR
   2074     case TPM_ALG_XOR:
   2075       return 0;
   2076 #endif
   2077 #ifdef TPM_ALG_NULL
   2078     case TPM_ALG_NULL:
   2079       return 0;
   2080 #endif
   2081   }
   2082   return 0;
   2083 }
   2084 
   2085 TPM_RC TPMU_SYM_MODE_Unmarshal(TPMU_SYM_MODE* target,
   2086                                BYTE** buffer,
   2087                                INT32* size,
   2088                                UINT32 selector) {
   2089   switch (selector) {
   2090 #ifdef TPM_ALG_AES
   2091     case TPM_ALG_AES:
   2092       return TPMI_ALG_SYM_MODE_Unmarshal(&target->aes, buffer, size, FALSE);
   2093 #endif
   2094 #ifdef TPM_ALG_SM4
   2095     case TPM_ALG_SM4:
   2096       return TPMI_ALG_SYM_MODE_Unmarshal(&target->sm4, buffer, size, FALSE);
   2097 #endif
   2098 #ifdef TPM_ALG_CAMELLIA
   2099     case TPM_ALG_CAMELLIA:
   2100       return TPMI_ALG_SYM_MODE_Unmarshal(&target->camellia, buffer, size,
   2101                                          FALSE);
   2102 #endif
   2103 #ifdef TPM_ALG_XOR
   2104     case TPM_ALG_XOR:
   2105       return TPM_RC_SUCCESS;
   2106 #endif
   2107 #ifdef TPM_ALG_NULL
   2108     case TPM_ALG_NULL:
   2109       return TPM_RC_SUCCESS;
   2110 #endif
   2111   }
   2112   return TPM_RC_SELECTOR;
   2113 }
   2114 
   2115 UINT16 TPMI_ALG_SYM_OBJECT_Marshal(TPMI_ALG_SYM_OBJECT* source,
   2116                                    BYTE** buffer,
   2117                                    INT32* size) {
   2118   return uint16_t_Marshal(source, buffer, size);
   2119 }
   2120 
   2121 TPM_RC TPMI_ALG_SYM_OBJECT_Unmarshal(TPMI_ALG_SYM_OBJECT* target,
   2122                                      BYTE** buffer,
   2123                                      INT32* size,
   2124                                      BOOL allow_conditional_value) {
   2125   TPM_RC result;
   2126   BOOL has_valid_value = FALSE;
   2127   result = uint16_t_Unmarshal(target, buffer, size);
   2128   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   2129     return result;
   2130   }
   2131   if (*target == TPM_ALG_NULL) {
   2132     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_SYMMETRIC;
   2133   }
   2134   switch (*target) {
   2135 #ifdef TPM_ALG_AES
   2136     case TPM_ALG_AES:
   2137 #endif
   2138 #ifdef TPM_ALG_SM4
   2139     case TPM_ALG_SM4:
   2140 #endif
   2141 #ifdef TPM_ALG_CAMELLIA
   2142     case TPM_ALG_CAMELLIA:
   2143 #endif
   2144       has_valid_value = TRUE;
   2145       break;
   2146   }
   2147   if (!has_valid_value) {
   2148     return TPM_RC_SYMMETRIC;
   2149   }
   2150   return TPM_RC_SUCCESS;
   2151 }
   2152 
   2153 UINT16 TPMT_SYM_DEF_OBJECT_Marshal(TPMT_SYM_DEF_OBJECT* source,
   2154                                    BYTE** buffer,
   2155                                    INT32* size) {
   2156   UINT16 total_size = 0;
   2157   total_size += TPMI_ALG_SYM_OBJECT_Marshal(&source->algorithm, buffer, size);
   2158   total_size += TPMU_SYM_KEY_BITS_Marshal(&source->keyBits, buffer, size,
   2159                                           source->algorithm);
   2160   total_size +=
   2161       TPMU_SYM_MODE_Marshal(&source->mode, buffer, size, source->algorithm);
   2162   return total_size;
   2163 }
   2164 
   2165 TPM_RC TPMT_SYM_DEF_OBJECT_Unmarshal(TPMT_SYM_DEF_OBJECT* target,
   2166                                      BYTE** buffer,
   2167                                      INT32* size) {
   2168   TPM_RC result;
   2169   result =
   2170       TPMI_ALG_SYM_OBJECT_Unmarshal(&target->algorithm, buffer, size, TRUE);
   2171   if (result != TPM_RC_SUCCESS) {
   2172     return result;
   2173   }
   2174   result = TPMU_SYM_KEY_BITS_Unmarshal(&target->keyBits, buffer, size,
   2175                                        target->algorithm);
   2176   if (result != TPM_RC_SUCCESS) {
   2177     return result;
   2178   }
   2179   result =
   2180       TPMU_SYM_MODE_Unmarshal(&target->mode, buffer, size, target->algorithm);
   2181   if (result != TPM_RC_SUCCESS) {
   2182     return result;
   2183   }
   2184   return TPM_RC_SUCCESS;
   2185 }
   2186 
   2187 UINT16 TPMI_ALG_RSA_SCHEME_Marshal(TPMI_ALG_RSA_SCHEME* source,
   2188                                    BYTE** buffer,
   2189                                    INT32* size) {
   2190   return uint16_t_Marshal(source, buffer, size);
   2191 }
   2192 
   2193 TPM_RC TPMI_ALG_RSA_SCHEME_Unmarshal(TPMI_ALG_RSA_SCHEME* target,
   2194                                      BYTE** buffer,
   2195                                      INT32* size,
   2196                                      BOOL allow_conditional_value) {
   2197   TPM_RC result;
   2198   BOOL has_valid_value = FALSE;
   2199   result = uint16_t_Unmarshal(target, buffer, size);
   2200   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   2201     return result;
   2202   }
   2203   if (*target == TPM_ALG_NULL) {
   2204     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   2205   }
   2206   switch (*target) {
   2207 #ifdef TPM_ALG_RSAES
   2208     case TPM_ALG_RSAES:
   2209 #endif
   2210 #ifdef TPM_ALG_OAEP
   2211     case TPM_ALG_OAEP:
   2212 #endif
   2213 #ifdef TPM_ALG_RSASSA
   2214     case TPM_ALG_RSASSA:
   2215 #endif
   2216 #ifdef TPM_ALG_RSAPSS
   2217     case TPM_ALG_RSAPSS:
   2218 #endif
   2219       has_valid_value = TRUE;
   2220       break;
   2221   }
   2222   if (!has_valid_value) {
   2223     return TPM_RC_VALUE;
   2224   }
   2225   return TPM_RC_SUCCESS;
   2226 }
   2227 
   2228 UINT16 TPMS_SCHEME_HASH_Marshal(TPMS_SCHEME_HASH* source,
   2229                                 BYTE** buffer,
   2230                                 INT32* size) {
   2231   UINT16 total_size = 0;
   2232   total_size += TPMI_ALG_HASH_Marshal(&source->hashAlg, buffer, size);
   2233   return total_size;
   2234 }
   2235 
   2236 TPM_RC TPMS_SCHEME_HASH_Unmarshal(TPMS_SCHEME_HASH* target,
   2237                                   BYTE** buffer,
   2238                                   INT32* size) {
   2239   TPM_RC result;
   2240   result = TPMI_ALG_HASH_Unmarshal(&target->hashAlg, buffer, size, FALSE);
   2241   if (result != TPM_RC_SUCCESS) {
   2242     return result;
   2243   }
   2244   return TPM_RC_SUCCESS;
   2245 }
   2246 
   2247 UINT16 TPMS_SIG_SCHEME_RSAPSS_Marshal(TPMS_SIG_SCHEME_RSAPSS* source,
   2248                                       BYTE** buffer,
   2249                                       INT32* size) {
   2250   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2251 }
   2252 
   2253 TPM_RC TPMS_SIG_SCHEME_RSAPSS_Unmarshal(TPMS_SIG_SCHEME_RSAPSS* target,
   2254                                         BYTE** buffer,
   2255                                         INT32* size) {
   2256   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2257 }
   2258 
   2259 UINT16 TPMS_SIG_SCHEME_SM2_Marshal(TPMS_SIG_SCHEME_SM2* source,
   2260                                    BYTE** buffer,
   2261                                    INT32* size) {
   2262   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2263 }
   2264 
   2265 TPM_RC TPMS_SIG_SCHEME_SM2_Unmarshal(TPMS_SIG_SCHEME_SM2* target,
   2266                                      BYTE** buffer,
   2267                                      INT32* size) {
   2268   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2269 }
   2270 
   2271 UINT16 TPMS_SIG_SCHEME_ECSCHNORR_Marshal(TPMS_SIG_SCHEME_ECSCHNORR* source,
   2272                                          BYTE** buffer,
   2273                                          INT32* size) {
   2274   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2275 }
   2276 
   2277 TPM_RC TPMS_SIG_SCHEME_ECSCHNORR_Unmarshal(TPMS_SIG_SCHEME_ECSCHNORR* target,
   2278                                            BYTE** buffer,
   2279                                            INT32* size) {
   2280   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2281 }
   2282 
   2283 UINT16 TPMS_SCHEME_ECDAA_Marshal(TPMS_SCHEME_ECDAA* source,
   2284                                  BYTE** buffer,
   2285                                  INT32* size) {
   2286   UINT16 total_size = 0;
   2287   total_size += TPMI_ALG_HASH_Marshal(&source->hashAlg, buffer, size);
   2288   total_size += UINT16_Marshal(&source->count, buffer, size);
   2289   return total_size;
   2290 }
   2291 
   2292 TPM_RC TPMS_SCHEME_ECDAA_Unmarshal(TPMS_SCHEME_ECDAA* target,
   2293                                    BYTE** buffer,
   2294                                    INT32* size) {
   2295   TPM_RC result;
   2296   result = TPMI_ALG_HASH_Unmarshal(&target->hashAlg, buffer, size, FALSE);
   2297   if (result != TPM_RC_SUCCESS) {
   2298     return result;
   2299   }
   2300   result = UINT16_Unmarshal(&target->count, buffer, size);
   2301   if (result != TPM_RC_SUCCESS) {
   2302     return result;
   2303   }
   2304   return TPM_RC_SUCCESS;
   2305 }
   2306 
   2307 UINT16 TPMS_SIG_SCHEME_ECDAA_Marshal(TPMS_SIG_SCHEME_ECDAA* source,
   2308                                      BYTE** buffer,
   2309                                      INT32* size) {
   2310   return TPMS_SCHEME_ECDAA_Marshal(source, buffer, size);
   2311 }
   2312 
   2313 TPM_RC TPMS_SIG_SCHEME_ECDAA_Unmarshal(TPMS_SIG_SCHEME_ECDAA* target,
   2314                                        BYTE** buffer,
   2315                                        INT32* size) {
   2316   return TPMS_SCHEME_ECDAA_Unmarshal(target, buffer, size);
   2317 }
   2318 
   2319 UINT16 TPMS_KEY_SCHEME_ECDH_Marshal(TPMS_KEY_SCHEME_ECDH* source,
   2320                                     BYTE** buffer,
   2321                                     INT32* size) {
   2322   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2323 }
   2324 
   2325 TPM_RC TPMS_KEY_SCHEME_ECDH_Unmarshal(TPMS_KEY_SCHEME_ECDH* target,
   2326                                       BYTE** buffer,
   2327                                       INT32* size) {
   2328   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2329 }
   2330 
   2331 UINT16 TPMS_KEY_SCHEME_ECMQV_Marshal(TPMS_KEY_SCHEME_ECMQV* source,
   2332                                      BYTE** buffer,
   2333                                      INT32* size) {
   2334   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2335 }
   2336 
   2337 TPM_RC TPMS_KEY_SCHEME_ECMQV_Unmarshal(TPMS_KEY_SCHEME_ECMQV* target,
   2338                                        BYTE** buffer,
   2339                                        INT32* size) {
   2340   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2341 }
   2342 
   2343 UINT16 TPMS_SIG_SCHEME_RSASSA_Marshal(TPMS_SIG_SCHEME_RSASSA* source,
   2344                                       BYTE** buffer,
   2345                                       INT32* size) {
   2346   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2347 }
   2348 
   2349 TPM_RC TPMS_SIG_SCHEME_RSASSA_Unmarshal(TPMS_SIG_SCHEME_RSASSA* target,
   2350                                         BYTE** buffer,
   2351                                         INT32* size) {
   2352 #if defined(SUPPORT_PADDING_ONLY_RSASSA) && SUPPORT_PADDING_ONLY_RSASSA == YES
   2353   return TPMI_ALG_HASH_Unmarshal(&target->hashAlg, buffer, size, TRUE);
   2354 #else
   2355   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2356 #endif
   2357 }
   2358 
   2359 UINT16 TPMS_ENC_SCHEME_OAEP_Marshal(TPMS_ENC_SCHEME_OAEP* source,
   2360                                     BYTE** buffer,
   2361                                     INT32* size) {
   2362   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2363 }
   2364 
   2365 TPM_RC TPMS_ENC_SCHEME_OAEP_Unmarshal(TPMS_ENC_SCHEME_OAEP* target,
   2366                                       BYTE** buffer,
   2367                                       INT32* size) {
   2368   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2369 }
   2370 
   2371 UINT16 TPMS_EMPTY_Marshal(TPMS_EMPTY* source, BYTE** buffer, INT32* size) {
   2372   UINT16 total_size = 0;
   2373   return total_size;
   2374 }
   2375 
   2376 TPM_RC TPMS_EMPTY_Unmarshal(TPMS_EMPTY* target, BYTE** buffer, INT32* size) {
   2377   TPM_RC result;
   2378   (void)result;
   2379 
   2380   return TPM_RC_SUCCESS;
   2381 }
   2382 
   2383 UINT16 TPMS_ENC_SCHEME_RSAES_Marshal(TPMS_ENC_SCHEME_RSAES* source,
   2384                                      BYTE** buffer,
   2385                                      INT32* size) {
   2386   return TPMS_EMPTY_Marshal(source, buffer, size);
   2387 }
   2388 
   2389 TPM_RC TPMS_ENC_SCHEME_RSAES_Unmarshal(TPMS_ENC_SCHEME_RSAES* target,
   2390                                        BYTE** buffer,
   2391                                        INT32* size) {
   2392   return TPMS_EMPTY_Unmarshal(target, buffer, size);
   2393 }
   2394 
   2395 UINT16 TPMS_SIG_SCHEME_ECDSA_Marshal(TPMS_SIG_SCHEME_ECDSA* source,
   2396                                      BYTE** buffer,
   2397                                      INT32* size) {
   2398   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2399 }
   2400 
   2401 TPM_RC TPMS_SIG_SCHEME_ECDSA_Unmarshal(TPMS_SIG_SCHEME_ECDSA* target,
   2402                                        BYTE** buffer,
   2403                                        INT32* size) {
   2404   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2405 }
   2406 
   2407 UINT16 TPMU_ASYM_SCHEME_Marshal(TPMU_ASYM_SCHEME* source,
   2408                                 BYTE** buffer,
   2409                                 INT32* size,
   2410                                 UINT32 selector) {
   2411   switch (selector) {
   2412 #ifdef TPM_ALG_ECDH
   2413     case TPM_ALG_ECDH:
   2414       return TPMS_KEY_SCHEME_ECDH_Marshal((TPMS_KEY_SCHEME_ECDH*)&source->ecdh,
   2415                                           buffer, size);
   2416 #endif
   2417 #ifdef TPM_ALG_ECMQV
   2418     case TPM_ALG_ECMQV:
   2419       return TPMS_KEY_SCHEME_ECMQV_Marshal(
   2420           (TPMS_KEY_SCHEME_ECMQV*)&source->ecmqv, buffer, size);
   2421 #endif
   2422 #ifdef TPM_ALG_RSASSA
   2423     case TPM_ALG_RSASSA:
   2424       return TPMS_SIG_SCHEME_RSASSA_Marshal(
   2425           (TPMS_SIG_SCHEME_RSASSA*)&source->rsassa, buffer, size);
   2426 #endif
   2427 #ifdef TPM_ALG_RSAPSS
   2428     case TPM_ALG_RSAPSS:
   2429       return TPMS_SIG_SCHEME_RSAPSS_Marshal(
   2430           (TPMS_SIG_SCHEME_RSAPSS*)&source->rsapss, buffer, size);
   2431 #endif
   2432 #ifdef TPM_ALG_ECDSA
   2433     case TPM_ALG_ECDSA:
   2434       return TPMS_SIG_SCHEME_ECDSA_Marshal(
   2435           (TPMS_SIG_SCHEME_ECDSA*)&source->ecdsa, buffer, size);
   2436 #endif
   2437 #ifdef TPM_ALG_ECDAA
   2438     case TPM_ALG_ECDAA:
   2439       return TPMS_SIG_SCHEME_ECDAA_Marshal(
   2440           (TPMS_SIG_SCHEME_ECDAA*)&source->ecdaa, buffer, size);
   2441 #endif
   2442 #ifdef TPM_ALG_SM2
   2443     case TPM_ALG_SM2:
   2444       return TPMS_SIG_SCHEME_SM2_Marshal((TPMS_SIG_SCHEME_SM2*)&source->sm2,
   2445                                          buffer, size);
   2446 #endif
   2447 #ifdef TPM_ALG_ECSCHNORR
   2448     case TPM_ALG_ECSCHNORR:
   2449       return TPMS_SIG_SCHEME_ECSCHNORR_Marshal(
   2450           (TPMS_SIG_SCHEME_ECSCHNORR*)&source->ecschnorr, buffer, size);
   2451 #endif
   2452 #ifdef TPM_ALG_RSAES
   2453     case TPM_ALG_RSAES:
   2454       return TPMS_ENC_SCHEME_RSAES_Marshal(
   2455           (TPMS_ENC_SCHEME_RSAES*)&source->rsaes, buffer, size);
   2456 #endif
   2457 #ifdef TPM_ALG_OAEP
   2458     case TPM_ALG_OAEP:
   2459       return TPMS_ENC_SCHEME_OAEP_Marshal((TPMS_ENC_SCHEME_OAEP*)&source->oaep,
   2460                                           buffer, size);
   2461 #endif
   2462 #ifdef TPM_ALG_NULL
   2463     case TPM_ALG_NULL:
   2464       return 0;
   2465 #endif
   2466   }
   2467   return 0;
   2468 }
   2469 
   2470 TPM_RC TPMU_ASYM_SCHEME_Unmarshal(TPMU_ASYM_SCHEME* target,
   2471                                   BYTE** buffer,
   2472                                   INT32* size,
   2473                                   UINT32 selector) {
   2474   switch (selector) {
   2475 #ifdef TPM_ALG_ECDH
   2476     case TPM_ALG_ECDH:
   2477       return TPMS_KEY_SCHEME_ECDH_Unmarshal(
   2478           (TPMS_KEY_SCHEME_ECDH*)&target->ecdh, buffer, size);
   2479 #endif
   2480 #ifdef TPM_ALG_ECMQV
   2481     case TPM_ALG_ECMQV:
   2482       return TPMS_KEY_SCHEME_ECMQV_Unmarshal(
   2483           (TPMS_KEY_SCHEME_ECMQV*)&target->ecmqv, buffer, size);
   2484 #endif
   2485 #ifdef TPM_ALG_RSASSA
   2486     case TPM_ALG_RSASSA:
   2487       return TPMS_SIG_SCHEME_RSASSA_Unmarshal(
   2488           (TPMS_SIG_SCHEME_RSASSA*)&target->rsassa, buffer, size);
   2489 #endif
   2490 #ifdef TPM_ALG_RSAPSS
   2491     case TPM_ALG_RSAPSS:
   2492       return TPMS_SIG_SCHEME_RSAPSS_Unmarshal(
   2493           (TPMS_SIG_SCHEME_RSAPSS*)&target->rsapss, buffer, size);
   2494 #endif
   2495 #ifdef TPM_ALG_ECDSA
   2496     case TPM_ALG_ECDSA:
   2497       return TPMS_SIG_SCHEME_ECDSA_Unmarshal(
   2498           (TPMS_SIG_SCHEME_ECDSA*)&target->ecdsa, buffer, size);
   2499 #endif
   2500 #ifdef TPM_ALG_ECDAA
   2501     case TPM_ALG_ECDAA:
   2502       return TPMS_SIG_SCHEME_ECDAA_Unmarshal(
   2503           (TPMS_SIG_SCHEME_ECDAA*)&target->ecdaa, buffer, size);
   2504 #endif
   2505 #ifdef TPM_ALG_SM2
   2506     case TPM_ALG_SM2:
   2507       return TPMS_SIG_SCHEME_SM2_Unmarshal((TPMS_SIG_SCHEME_SM2*)&target->sm2,
   2508                                            buffer, size);
   2509 #endif
   2510 #ifdef TPM_ALG_ECSCHNORR
   2511     case TPM_ALG_ECSCHNORR:
   2512       return TPMS_SIG_SCHEME_ECSCHNORR_Unmarshal(
   2513           (TPMS_SIG_SCHEME_ECSCHNORR*)&target->ecschnorr, buffer, size);
   2514 #endif
   2515 #ifdef TPM_ALG_RSAES
   2516     case TPM_ALG_RSAES:
   2517       return TPMS_ENC_SCHEME_RSAES_Unmarshal(
   2518           (TPMS_ENC_SCHEME_RSAES*)&target->rsaes, buffer, size);
   2519 #endif
   2520 #ifdef TPM_ALG_OAEP
   2521     case TPM_ALG_OAEP:
   2522       return TPMS_ENC_SCHEME_OAEP_Unmarshal(
   2523           (TPMS_ENC_SCHEME_OAEP*)&target->oaep, buffer, size);
   2524 #endif
   2525 #ifdef TPM_ALG_NULL
   2526     case TPM_ALG_NULL:
   2527       return TPM_RC_SUCCESS;
   2528 #endif
   2529   }
   2530   return TPM_RC_SELECTOR;
   2531 }
   2532 
   2533 UINT16 TPMT_RSA_SCHEME_Marshal(TPMT_RSA_SCHEME* source,
   2534                                BYTE** buffer,
   2535                                INT32* size) {
   2536   UINT16 total_size = 0;
   2537   total_size += TPMI_ALG_RSA_SCHEME_Marshal(&source->scheme, buffer, size);
   2538   total_size +=
   2539       TPMU_ASYM_SCHEME_Marshal(&source->details, buffer, size, source->scheme);
   2540   return total_size;
   2541 }
   2542 
   2543 TPM_RC TPMT_RSA_SCHEME_Unmarshal(TPMT_RSA_SCHEME* target,
   2544                                  BYTE** buffer,
   2545                                  INT32* size) {
   2546   TPM_RC result;
   2547   result = TPMI_ALG_RSA_SCHEME_Unmarshal(&target->scheme, buffer, size, TRUE);
   2548   if (result != TPM_RC_SUCCESS) {
   2549     return result;
   2550   }
   2551   result = TPMU_ASYM_SCHEME_Unmarshal(&target->details, buffer, size,
   2552                                       target->scheme);
   2553   if (result != TPM_RC_SUCCESS) {
   2554     return result;
   2555   }
   2556   return TPM_RC_SUCCESS;
   2557 }
   2558 
   2559 UINT16 TPMI_RSA_KEY_BITS_Marshal(TPMI_RSA_KEY_BITS* source,
   2560                                  BYTE** buffer,
   2561                                  INT32* size) {
   2562   return uint16_t_Marshal(source, buffer, size);
   2563 }
   2564 
   2565 TPM_RC TPMI_RSA_KEY_BITS_Unmarshal(TPMI_RSA_KEY_BITS* target,
   2566                                    BYTE** buffer,
   2567                                    INT32* size) {
   2568   TPM_RC result;
   2569   uint16_t supported_values[] = RSA_KEY_SIZES_BITS;
   2570   size_t length = sizeof(supported_values) / sizeof(supported_values[0]);
   2571   size_t i;
   2572   BOOL is_supported_value = FALSE;
   2573   result = uint16_t_Unmarshal(target, buffer, size);
   2574   if (result != TPM_RC_SUCCESS) {
   2575     return result;
   2576   }
   2577   for (i = 0; i < length; ++i) {
   2578     if (*target == supported_values[i]) {
   2579       is_supported_value = TRUE;
   2580       break;
   2581     }
   2582   }
   2583   if (!is_supported_value) {
   2584     return TPM_RC_VALUE;
   2585   }
   2586   return TPM_RC_SUCCESS;
   2587 }
   2588 
   2589 UINT16 TPMS_RSA_PARMS_Marshal(TPMS_RSA_PARMS* source,
   2590                               BYTE** buffer,
   2591                               INT32* size) {
   2592   UINT16 total_size = 0;
   2593   total_size += TPMT_SYM_DEF_OBJECT_Marshal(&source->symmetric, buffer, size);
   2594   total_size += TPMT_RSA_SCHEME_Marshal(&source->scheme, buffer, size);
   2595   total_size += TPMI_RSA_KEY_BITS_Marshal(&source->keyBits, buffer, size);
   2596   total_size += UINT32_Marshal(&source->exponent, buffer, size);
   2597   return total_size;
   2598 }
   2599 
   2600 TPM_RC TPMS_RSA_PARMS_Unmarshal(TPMS_RSA_PARMS* target,
   2601                                 BYTE** buffer,
   2602                                 INT32* size) {
   2603   TPM_RC result;
   2604   result = TPMT_SYM_DEF_OBJECT_Unmarshal(&target->symmetric, buffer, size);
   2605   if (result != TPM_RC_SUCCESS) {
   2606     return result;
   2607   }
   2608   result = TPMT_RSA_SCHEME_Unmarshal(&target->scheme, buffer, size);
   2609   if (result != TPM_RC_SUCCESS) {
   2610     return result;
   2611   }
   2612   result = TPMI_RSA_KEY_BITS_Unmarshal(&target->keyBits, buffer, size);
   2613   if (result != TPM_RC_SUCCESS) {
   2614     return result;
   2615   }
   2616   result = UINT32_Unmarshal(&target->exponent, buffer, size);
   2617   if (result != TPM_RC_SUCCESS) {
   2618     return result;
   2619   }
   2620   return TPM_RC_SUCCESS;
   2621 }
   2622 
   2623 UINT16 TPMS_SYMCIPHER_PARMS_Marshal(TPMS_SYMCIPHER_PARMS* source,
   2624                                     BYTE** buffer,
   2625                                     INT32* size) {
   2626   UINT16 total_size = 0;
   2627   total_size += TPMT_SYM_DEF_OBJECT_Marshal(&source->sym, buffer, size);
   2628   return total_size;
   2629 }
   2630 
   2631 TPM_RC TPMS_SYMCIPHER_PARMS_Unmarshal(TPMS_SYMCIPHER_PARMS* target,
   2632                                       BYTE** buffer,
   2633                                       INT32* size) {
   2634   TPM_RC result;
   2635   result = TPMT_SYM_DEF_OBJECT_Unmarshal(&target->sym, buffer, size);
   2636   if (result != TPM_RC_SUCCESS) {
   2637     return result;
   2638   }
   2639   return TPM_RC_SUCCESS;
   2640 }
   2641 
   2642 UINT16 TPMI_ALG_ASYM_SCHEME_Marshal(TPMI_ALG_ASYM_SCHEME* source,
   2643                                     BYTE** buffer,
   2644                                     INT32* size) {
   2645   return uint16_t_Marshal(source, buffer, size);
   2646 }
   2647 
   2648 TPM_RC TPMI_ALG_ASYM_SCHEME_Unmarshal(TPMI_ALG_ASYM_SCHEME* target,
   2649                                       BYTE** buffer,
   2650                                       INT32* size,
   2651                                       BOOL allow_conditional_value) {
   2652   TPM_RC result;
   2653   BOOL has_valid_value = FALSE;
   2654   result = uint16_t_Unmarshal(target, buffer, size);
   2655   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   2656     return result;
   2657   }
   2658   if (*target == TPM_ALG_NULL) {
   2659     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   2660   }
   2661   switch (*target) {
   2662 #ifdef TPM_ALG_ECDH
   2663     case TPM_ALG_ECDH:
   2664 #endif
   2665 #ifdef TPM_ALG_ECMQV
   2666     case TPM_ALG_ECMQV:
   2667 #endif
   2668 #ifdef TPM_ALG_RSASSA
   2669     case TPM_ALG_RSASSA:
   2670 #endif
   2671 #ifdef TPM_ALG_RSAPSS
   2672     case TPM_ALG_RSAPSS:
   2673 #endif
   2674 #ifdef TPM_ALG_ECDSA
   2675     case TPM_ALG_ECDSA:
   2676 #endif
   2677 #ifdef TPM_ALG_ECDAA
   2678     case TPM_ALG_ECDAA:
   2679 #endif
   2680 #ifdef TPM_ALG_SM2
   2681     case TPM_ALG_SM2:
   2682 #endif
   2683 #ifdef TPM_ALG_ECSCHNORR
   2684     case TPM_ALG_ECSCHNORR:
   2685 #endif
   2686 #ifdef TPM_ALG_RSAES
   2687     case TPM_ALG_RSAES:
   2688 #endif
   2689 #ifdef TPM_ALG_OAEP
   2690     case TPM_ALG_OAEP:
   2691 #endif
   2692       has_valid_value = TRUE;
   2693       break;
   2694   }
   2695   if (!has_valid_value) {
   2696     return TPM_RC_VALUE;
   2697   }
   2698   return TPM_RC_SUCCESS;
   2699 }
   2700 
   2701 UINT16 TPMT_ASYM_SCHEME_Marshal(TPMT_ASYM_SCHEME* source,
   2702                                 BYTE** buffer,
   2703                                 INT32* size) {
   2704   UINT16 total_size = 0;
   2705   total_size += TPMI_ALG_ASYM_SCHEME_Marshal(&source->scheme, buffer, size);
   2706   total_size +=
   2707       TPMU_ASYM_SCHEME_Marshal(&source->details, buffer, size, source->scheme);
   2708   return total_size;
   2709 }
   2710 
   2711 TPM_RC TPMT_ASYM_SCHEME_Unmarshal(TPMT_ASYM_SCHEME* target,
   2712                                   BYTE** buffer,
   2713                                   INT32* size) {
   2714   TPM_RC result;
   2715   result = TPMI_ALG_ASYM_SCHEME_Unmarshal(&target->scheme, buffer, size, TRUE);
   2716   if (result != TPM_RC_SUCCESS) {
   2717     return result;
   2718   }
   2719   result = TPMU_ASYM_SCHEME_Unmarshal(&target->details, buffer, size,
   2720                                       target->scheme);
   2721   if (result != TPM_RC_SUCCESS) {
   2722     return result;
   2723   }
   2724   return TPM_RC_SUCCESS;
   2725 }
   2726 
   2727 UINT16 TPMS_ASYM_PARMS_Marshal(TPMS_ASYM_PARMS* source,
   2728                                BYTE** buffer,
   2729                                INT32* size) {
   2730   UINT16 total_size = 0;
   2731   total_size += TPMT_SYM_DEF_OBJECT_Marshal(&source->symmetric, buffer, size);
   2732   total_size += TPMT_ASYM_SCHEME_Marshal(&source->scheme, buffer, size);
   2733   return total_size;
   2734 }
   2735 
   2736 TPM_RC TPMS_ASYM_PARMS_Unmarshal(TPMS_ASYM_PARMS* target,
   2737                                  BYTE** buffer,
   2738                                  INT32* size) {
   2739   TPM_RC result;
   2740   result = TPMT_SYM_DEF_OBJECT_Unmarshal(&target->symmetric, buffer, size);
   2741   if (result != TPM_RC_SUCCESS) {
   2742     return result;
   2743   }
   2744   result = TPMT_ASYM_SCHEME_Unmarshal(&target->scheme, buffer, size);
   2745   if (result != TPM_RC_SUCCESS) {
   2746     return result;
   2747   }
   2748   return TPM_RC_SUCCESS;
   2749 }
   2750 
   2751 UINT16 TPMI_ALG_KDF_Marshal(TPMI_ALG_KDF* source, BYTE** buffer, INT32* size) {
   2752   return uint16_t_Marshal(source, buffer, size);
   2753 }
   2754 
   2755 TPM_RC TPMI_ALG_KDF_Unmarshal(TPMI_ALG_KDF* target,
   2756                               BYTE** buffer,
   2757                               INT32* size,
   2758                               BOOL allow_conditional_value) {
   2759   TPM_RC result;
   2760   BOOL has_valid_value = FALSE;
   2761   result = uint16_t_Unmarshal(target, buffer, size);
   2762   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   2763     return result;
   2764   }
   2765   if (*target == TPM_ALG_NULL) {
   2766     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_KDF;
   2767   }
   2768   switch (*target) {
   2769 #ifdef TPM_ALG_MGF1
   2770     case TPM_ALG_MGF1:
   2771 #endif
   2772 #ifdef TPM_ALG_KDF1_SP800_56A
   2773     case TPM_ALG_KDF1_SP800_56A:
   2774 #endif
   2775 #ifdef TPM_ALG_KDF2
   2776     case TPM_ALG_KDF2:
   2777 #endif
   2778 #ifdef TPM_ALG_KDF1_SP800_108
   2779     case TPM_ALG_KDF1_SP800_108:
   2780 #endif
   2781       has_valid_value = TRUE;
   2782       break;
   2783   }
   2784   if (!has_valid_value) {
   2785     return TPM_RC_KDF;
   2786   }
   2787   return TPM_RC_SUCCESS;
   2788 }
   2789 
   2790 UINT16 TPMS_SCHEME_KDF1_SP800_108_Marshal(TPMS_SCHEME_KDF1_SP800_108* source,
   2791                                           BYTE** buffer,
   2792                                           INT32* size) {
   2793   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2794 }
   2795 
   2796 TPM_RC TPMS_SCHEME_KDF1_SP800_108_Unmarshal(TPMS_SCHEME_KDF1_SP800_108* target,
   2797                                             BYTE** buffer,
   2798                                             INT32* size) {
   2799   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2800 }
   2801 
   2802 UINT16 TPMS_SCHEME_KDF2_Marshal(TPMS_SCHEME_KDF2* source,
   2803                                 BYTE** buffer,
   2804                                 INT32* size) {
   2805   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2806 }
   2807 
   2808 TPM_RC TPMS_SCHEME_KDF2_Unmarshal(TPMS_SCHEME_KDF2* target,
   2809                                   BYTE** buffer,
   2810                                   INT32* size) {
   2811   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2812 }
   2813 
   2814 UINT16 TPMS_SCHEME_KDF1_SP800_56A_Marshal(TPMS_SCHEME_KDF1_SP800_56A* source,
   2815                                           BYTE** buffer,
   2816                                           INT32* size) {
   2817   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2818 }
   2819 
   2820 TPM_RC TPMS_SCHEME_KDF1_SP800_56A_Unmarshal(TPMS_SCHEME_KDF1_SP800_56A* target,
   2821                                             BYTE** buffer,
   2822                                             INT32* size) {
   2823   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2824 }
   2825 
   2826 UINT16 TPMS_SCHEME_MGF1_Marshal(TPMS_SCHEME_MGF1* source,
   2827                                 BYTE** buffer,
   2828                                 INT32* size) {
   2829   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   2830 }
   2831 
   2832 TPM_RC TPMS_SCHEME_MGF1_Unmarshal(TPMS_SCHEME_MGF1* target,
   2833                                   BYTE** buffer,
   2834                                   INT32* size) {
   2835   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   2836 }
   2837 
   2838 UINT16 TPMU_KDF_SCHEME_Marshal(TPMU_KDF_SCHEME* source,
   2839                                BYTE** buffer,
   2840                                INT32* size,
   2841                                UINT32 selector) {
   2842   switch (selector) {
   2843 #ifdef TPM_ALG_MGF1
   2844     case TPM_ALG_MGF1:
   2845       return TPMS_SCHEME_MGF1_Marshal((TPMS_SCHEME_MGF1*)&source->mgf1, buffer,
   2846                                       size);
   2847 #endif
   2848 #ifdef TPM_ALG_KDF1_SP800_56A
   2849     case TPM_ALG_KDF1_SP800_56A:
   2850       return TPMS_SCHEME_KDF1_SP800_56A_Marshal(
   2851           (TPMS_SCHEME_KDF1_SP800_56A*)&source->kdf1_sp800_56a, buffer, size);
   2852 #endif
   2853 #ifdef TPM_ALG_KDF2
   2854     case TPM_ALG_KDF2:
   2855       return TPMS_SCHEME_KDF2_Marshal((TPMS_SCHEME_KDF2*)&source->kdf2, buffer,
   2856                                       size);
   2857 #endif
   2858 #ifdef TPM_ALG_KDF1_SP800_108
   2859     case TPM_ALG_KDF1_SP800_108:
   2860       return TPMS_SCHEME_KDF1_SP800_108_Marshal(
   2861           (TPMS_SCHEME_KDF1_SP800_108*)&source->kdf1_sp800_108, buffer, size);
   2862 #endif
   2863 #ifdef TPM_ALG_NULL
   2864     case TPM_ALG_NULL:
   2865       return 0;
   2866 #endif
   2867   }
   2868   return 0;
   2869 }
   2870 
   2871 TPM_RC TPMU_KDF_SCHEME_Unmarshal(TPMU_KDF_SCHEME* target,
   2872                                  BYTE** buffer,
   2873                                  INT32* size,
   2874                                  UINT32 selector) {
   2875   switch (selector) {
   2876 #ifdef TPM_ALG_MGF1
   2877     case TPM_ALG_MGF1:
   2878       return TPMS_SCHEME_MGF1_Unmarshal((TPMS_SCHEME_MGF1*)&target->mgf1,
   2879                                         buffer, size);
   2880 #endif
   2881 #ifdef TPM_ALG_KDF1_SP800_56A
   2882     case TPM_ALG_KDF1_SP800_56A:
   2883       return TPMS_SCHEME_KDF1_SP800_56A_Unmarshal(
   2884           (TPMS_SCHEME_KDF1_SP800_56A*)&target->kdf1_sp800_56a, buffer, size);
   2885 #endif
   2886 #ifdef TPM_ALG_KDF2
   2887     case TPM_ALG_KDF2:
   2888       return TPMS_SCHEME_KDF2_Unmarshal((TPMS_SCHEME_KDF2*)&target->kdf2,
   2889                                         buffer, size);
   2890 #endif
   2891 #ifdef TPM_ALG_KDF1_SP800_108
   2892     case TPM_ALG_KDF1_SP800_108:
   2893       return TPMS_SCHEME_KDF1_SP800_108_Unmarshal(
   2894           (TPMS_SCHEME_KDF1_SP800_108*)&target->kdf1_sp800_108, buffer, size);
   2895 #endif
   2896 #ifdef TPM_ALG_NULL
   2897     case TPM_ALG_NULL:
   2898       return TPM_RC_SUCCESS;
   2899 #endif
   2900   }
   2901   return TPM_RC_SELECTOR;
   2902 }
   2903 
   2904 UINT16 TPMT_KDF_SCHEME_Marshal(TPMT_KDF_SCHEME* source,
   2905                                BYTE** buffer,
   2906                                INT32* size) {
   2907   UINT16 total_size = 0;
   2908   total_size += TPMI_ALG_KDF_Marshal(&source->scheme, buffer, size);
   2909   total_size +=
   2910       TPMU_KDF_SCHEME_Marshal(&source->details, buffer, size, source->scheme);
   2911   return total_size;
   2912 }
   2913 
   2914 TPM_RC TPMT_KDF_SCHEME_Unmarshal(TPMT_KDF_SCHEME* target,
   2915                                  BYTE** buffer,
   2916                                  INT32* size) {
   2917   TPM_RC result;
   2918   result = TPMI_ALG_KDF_Unmarshal(&target->scheme, buffer, size, TRUE);
   2919   if (result != TPM_RC_SUCCESS) {
   2920     return result;
   2921   }
   2922   result =
   2923       TPMU_KDF_SCHEME_Unmarshal(&target->details, buffer, size, target->scheme);
   2924   if (result != TPM_RC_SUCCESS) {
   2925     return result;
   2926   }
   2927   return TPM_RC_SUCCESS;
   2928 }
   2929 
   2930 UINT16 TPMI_ALG_ECC_SCHEME_Marshal(TPMI_ALG_ECC_SCHEME* source,
   2931                                    BYTE** buffer,
   2932                                    INT32* size) {
   2933   return uint16_t_Marshal(source, buffer, size);
   2934 }
   2935 
   2936 TPM_RC TPMI_ALG_ECC_SCHEME_Unmarshal(TPMI_ALG_ECC_SCHEME* target,
   2937                                      BYTE** buffer,
   2938                                      INT32* size,
   2939                                      BOOL allow_conditional_value) {
   2940   TPM_RC result;
   2941   BOOL has_valid_value = FALSE;
   2942   result = uint16_t_Unmarshal(target, buffer, size);
   2943   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   2944     return result;
   2945   }
   2946   if (*target == TPM_ALG_NULL) {
   2947     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_SCHEME;
   2948   }
   2949   switch (*target) {
   2950 #ifdef TPM_ALG_ECDSA
   2951     case TPM_ALG_ECDSA:
   2952 #endif
   2953 #ifdef TPM_ALG_ECDAA
   2954     case TPM_ALG_ECDAA:
   2955 #endif
   2956 #ifdef TPM_ALG_SM2
   2957     case TPM_ALG_SM2:
   2958 #endif
   2959 #ifdef TPM_ALG_ECSCHNORR
   2960     case TPM_ALG_ECSCHNORR:
   2961 #endif
   2962 #ifdef TPM_ALG_ECDH
   2963     case TPM_ALG_ECDH:
   2964 #endif
   2965 #ifdef TPM_ALG_ECMQV
   2966     case TPM_ALG_ECMQV:
   2967 #endif
   2968       has_valid_value = TRUE;
   2969       break;
   2970   }
   2971   if (!has_valid_value) {
   2972     return TPM_RC_SCHEME;
   2973   }
   2974   return TPM_RC_SUCCESS;
   2975 }
   2976 
   2977 UINT16 TPMT_ECC_SCHEME_Marshal(TPMT_ECC_SCHEME* source,
   2978                                BYTE** buffer,
   2979                                INT32* size) {
   2980   UINT16 total_size = 0;
   2981   total_size += TPMI_ALG_ECC_SCHEME_Marshal(&source->scheme, buffer, size);
   2982   total_size +=
   2983       TPMU_ASYM_SCHEME_Marshal(&source->details, buffer, size, source->scheme);
   2984   return total_size;
   2985 }
   2986 
   2987 TPM_RC TPMT_ECC_SCHEME_Unmarshal(TPMT_ECC_SCHEME* target,
   2988                                  BYTE** buffer,
   2989                                  INT32* size) {
   2990   TPM_RC result;
   2991   result = TPMI_ALG_ECC_SCHEME_Unmarshal(&target->scheme, buffer, size, TRUE);
   2992   if (result != TPM_RC_SUCCESS) {
   2993     return result;
   2994   }
   2995   result = TPMU_ASYM_SCHEME_Unmarshal(&target->details, buffer, size,
   2996                                       target->scheme);
   2997   if (result != TPM_RC_SUCCESS) {
   2998     return result;
   2999   }
   3000   return TPM_RC_SUCCESS;
   3001 }
   3002 
   3003 UINT16 TPMI_ECC_CURVE_Marshal(TPMI_ECC_CURVE* source,
   3004                               BYTE** buffer,
   3005                               INT32* size) {
   3006   return uint16_t_Marshal(source, buffer, size);
   3007 }
   3008 
   3009 TPM_RC TPMI_ECC_CURVE_Unmarshal(TPMI_ECC_CURVE* target,
   3010                                 BYTE** buffer,
   3011                                 INT32* size) {
   3012   TPM_RC result;
   3013   uint16_t supported_values[] = ECC_CURVES;
   3014   size_t length = sizeof(supported_values) / sizeof(supported_values[0]);
   3015   size_t i;
   3016   BOOL is_supported_value = FALSE;
   3017   result = uint16_t_Unmarshal(target, buffer, size);
   3018   if (result != TPM_RC_SUCCESS) {
   3019     return result;
   3020   }
   3021   for (i = 0; i < length; ++i) {
   3022     if (*target == supported_values[i]) {
   3023       is_supported_value = TRUE;
   3024       break;
   3025     }
   3026   }
   3027   if (!is_supported_value) {
   3028     return TPM_RC_CURVE;
   3029   }
   3030   return TPM_RC_SUCCESS;
   3031 }
   3032 
   3033 UINT16 TPMS_ECC_PARMS_Marshal(TPMS_ECC_PARMS* source,
   3034                               BYTE** buffer,
   3035                               INT32* size) {
   3036   UINT16 total_size = 0;
   3037   total_size += TPMT_SYM_DEF_OBJECT_Marshal(&source->symmetric, buffer, size);
   3038   total_size += TPMT_ECC_SCHEME_Marshal(&source->scheme, buffer, size);
   3039   total_size += TPMI_ECC_CURVE_Marshal(&source->curveID, buffer, size);
   3040   total_size += TPMT_KDF_SCHEME_Marshal(&source->kdf, buffer, size);
   3041   return total_size;
   3042 }
   3043 
   3044 TPM_RC TPMS_ECC_PARMS_Unmarshal(TPMS_ECC_PARMS* target,
   3045                                 BYTE** buffer,
   3046                                 INT32* size) {
   3047   TPM_RC result;
   3048   result = TPMT_SYM_DEF_OBJECT_Unmarshal(&target->symmetric, buffer, size);
   3049   if (result != TPM_RC_SUCCESS) {
   3050     return result;
   3051   }
   3052   result = TPMT_ECC_SCHEME_Unmarshal(&target->scheme, buffer, size);
   3053   if (result != TPM_RC_SUCCESS) {
   3054     return result;
   3055   }
   3056   result = TPMI_ECC_CURVE_Unmarshal(&target->curveID, buffer, size);
   3057   if (result != TPM_RC_SUCCESS) {
   3058     return result;
   3059   }
   3060   result = TPMT_KDF_SCHEME_Unmarshal(&target->kdf, buffer, size);
   3061   if (result != TPM_RC_SUCCESS) {
   3062     return result;
   3063   }
   3064   return TPM_RC_SUCCESS;
   3065 }
   3066 
   3067 UINT16 TPMI_ALG_KEYEDHASH_SCHEME_Marshal(TPMI_ALG_KEYEDHASH_SCHEME* source,
   3068                                          BYTE** buffer,
   3069                                          INT32* size) {
   3070   return uint16_t_Marshal(source, buffer, size);
   3071 }
   3072 
   3073 TPM_RC TPMI_ALG_KEYEDHASH_SCHEME_Unmarshal(TPMI_ALG_KEYEDHASH_SCHEME* target,
   3074                                            BYTE** buffer,
   3075                                            INT32* size,
   3076                                            BOOL allow_conditional_value) {
   3077   TPM_RC result;
   3078   BOOL has_valid_value = FALSE;
   3079   result = uint16_t_Unmarshal(target, buffer, size);
   3080   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3081     return result;
   3082   }
   3083   if (*target == TPM_ALG_NULL) {
   3084     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   3085   }
   3086   switch (*target) {
   3087 #ifdef TPM_ALG_HMAC
   3088     case TPM_ALG_HMAC:
   3089 #endif
   3090 #ifdef TPM_ALG_XOR
   3091     case TPM_ALG_XOR:
   3092 #endif
   3093       has_valid_value = TRUE;
   3094       break;
   3095   }
   3096   if (!has_valid_value) {
   3097     return TPM_RC_VALUE;
   3098   }
   3099   return TPM_RC_SUCCESS;
   3100 }
   3101 
   3102 UINT16 TPMS_SCHEME_HMAC_Marshal(TPMS_SCHEME_HMAC* source,
   3103                                 BYTE** buffer,
   3104                                 INT32* size) {
   3105   return TPMS_SCHEME_HASH_Marshal(source, buffer, size);
   3106 }
   3107 
   3108 TPM_RC TPMS_SCHEME_HMAC_Unmarshal(TPMS_SCHEME_HMAC* target,
   3109                                   BYTE** buffer,
   3110                                   INT32* size) {
   3111   return TPMS_SCHEME_HASH_Unmarshal(target, buffer, size);
   3112 }
   3113 
   3114 UINT16 TPMS_SCHEME_XOR_Marshal(TPMS_SCHEME_XOR* source,
   3115                                BYTE** buffer,
   3116                                INT32* size) {
   3117   UINT16 total_size = 0;
   3118   total_size += TPMI_ALG_HASH_Marshal(&source->hashAlg, buffer, size);
   3119   total_size += TPMI_ALG_KDF_Marshal(&source->kdf, buffer, size);
   3120   return total_size;
   3121 }
   3122 
   3123 TPM_RC TPMS_SCHEME_XOR_Unmarshal(TPMS_SCHEME_XOR* target,
   3124                                  BYTE** buffer,
   3125                                  INT32* size) {
   3126   TPM_RC result;
   3127   result = TPMI_ALG_HASH_Unmarshal(&target->hashAlg, buffer, size, TRUE);
   3128   if (result != TPM_RC_SUCCESS) {
   3129     return result;
   3130   }
   3131   result = TPMI_ALG_KDF_Unmarshal(&target->kdf, buffer, size, FALSE);
   3132   if (result != TPM_RC_SUCCESS) {
   3133     return result;
   3134   }
   3135   return TPM_RC_SUCCESS;
   3136 }
   3137 
   3138 UINT16 TPMU_SCHEME_KEYEDHASH_Marshal(TPMU_SCHEME_KEYEDHASH* source,
   3139                                      BYTE** buffer,
   3140                                      INT32* size,
   3141                                      UINT32 selector) {
   3142   switch (selector) {
   3143 #ifdef TPM_ALG_HMAC
   3144     case TPM_ALG_HMAC:
   3145       return TPMS_SCHEME_HMAC_Marshal((TPMS_SCHEME_HMAC*)&source->hmac, buffer,
   3146                                       size);
   3147 #endif
   3148 #ifdef TPM_ALG_XOR
   3149     case TPM_ALG_XOR:
   3150       return TPMS_SCHEME_XOR_Marshal((TPMS_SCHEME_XOR*)&source->xor_, buffer,
   3151                                      size);
   3152 #endif
   3153 #ifdef TPM_ALG_NULL
   3154     case TPM_ALG_NULL:
   3155       return 0;
   3156 #endif
   3157   }
   3158   return 0;
   3159 }
   3160 
   3161 TPM_RC TPMU_SCHEME_KEYEDHASH_Unmarshal(TPMU_SCHEME_KEYEDHASH* target,
   3162                                        BYTE** buffer,
   3163                                        INT32* size,
   3164                                        UINT32 selector) {
   3165   switch (selector) {
   3166 #ifdef TPM_ALG_HMAC
   3167     case TPM_ALG_HMAC:
   3168       return TPMS_SCHEME_HMAC_Unmarshal((TPMS_SCHEME_HMAC*)&target->hmac,
   3169                                         buffer, size);
   3170 #endif
   3171 #ifdef TPM_ALG_XOR
   3172     case TPM_ALG_XOR:
   3173       return TPMS_SCHEME_XOR_Unmarshal((TPMS_SCHEME_XOR*)&target->xor_, buffer,
   3174                                        size);
   3175 #endif
   3176 #ifdef TPM_ALG_NULL
   3177     case TPM_ALG_NULL:
   3178       return TPM_RC_SUCCESS;
   3179 #endif
   3180   }
   3181   return TPM_RC_SELECTOR;
   3182 }
   3183 
   3184 UINT16 TPMT_KEYEDHASH_SCHEME_Marshal(TPMT_KEYEDHASH_SCHEME* source,
   3185                                      BYTE** buffer,
   3186                                      INT32* size) {
   3187   UINT16 total_size = 0;
   3188   total_size +=
   3189       TPMI_ALG_KEYEDHASH_SCHEME_Marshal(&source->scheme, buffer, size);
   3190   total_size += TPMU_SCHEME_KEYEDHASH_Marshal(&source->details, buffer, size,
   3191                                               source->scheme);
   3192   return total_size;
   3193 }
   3194 
   3195 TPM_RC TPMT_KEYEDHASH_SCHEME_Unmarshal(TPMT_KEYEDHASH_SCHEME* target,
   3196                                        BYTE** buffer,
   3197                                        INT32* size) {
   3198   TPM_RC result;
   3199   result =
   3200       TPMI_ALG_KEYEDHASH_SCHEME_Unmarshal(&target->scheme, buffer, size, TRUE);
   3201   if (result != TPM_RC_SUCCESS) {
   3202     return result;
   3203   }
   3204   result = TPMU_SCHEME_KEYEDHASH_Unmarshal(&target->details, buffer, size,
   3205                                            target->scheme);
   3206   if (result != TPM_RC_SUCCESS) {
   3207     return result;
   3208   }
   3209   return TPM_RC_SUCCESS;
   3210 }
   3211 
   3212 UINT16 TPMS_KEYEDHASH_PARMS_Marshal(TPMS_KEYEDHASH_PARMS* source,
   3213                                     BYTE** buffer,
   3214                                     INT32* size) {
   3215   UINT16 total_size = 0;
   3216   total_size += TPMT_KEYEDHASH_SCHEME_Marshal(&source->scheme, buffer, size);
   3217   return total_size;
   3218 }
   3219 
   3220 TPM_RC TPMS_KEYEDHASH_PARMS_Unmarshal(TPMS_KEYEDHASH_PARMS* target,
   3221                                       BYTE** buffer,
   3222                                       INT32* size) {
   3223   TPM_RC result;
   3224   result = TPMT_KEYEDHASH_SCHEME_Unmarshal(&target->scheme, buffer, size);
   3225   if (result != TPM_RC_SUCCESS) {
   3226     return result;
   3227   }
   3228   return TPM_RC_SUCCESS;
   3229 }
   3230 
   3231 UINT16 TPMU_PUBLIC_PARMS_Marshal(TPMU_PUBLIC_PARMS* source,
   3232                                  BYTE** buffer,
   3233                                  INT32* size,
   3234                                  UINT32 selector) {
   3235   switch (selector) {
   3236 #ifdef TPM_ALG_KEYEDHASH
   3237     case TPM_ALG_KEYEDHASH:
   3238       return TPMS_KEYEDHASH_PARMS_Marshal(
   3239           (TPMS_KEYEDHASH_PARMS*)&source->keyedHashDetail, buffer, size);
   3240 #endif
   3241 #ifdef TPM_ALG_SYMCIPHER
   3242     case TPM_ALG_SYMCIPHER:
   3243       return TPMS_SYMCIPHER_PARMS_Marshal(
   3244           (TPMS_SYMCIPHER_PARMS*)&source->symDetail, buffer, size);
   3245 #endif
   3246 #ifdef TPM_ALG_RSA
   3247     case TPM_ALG_RSA:
   3248       return TPMS_RSA_PARMS_Marshal((TPMS_RSA_PARMS*)&source->rsaDetail, buffer,
   3249                                     size);
   3250 #endif
   3251 #ifdef TPM_ALG_ECC
   3252     case TPM_ALG_ECC:
   3253       return TPMS_ECC_PARMS_Marshal((TPMS_ECC_PARMS*)&source->eccDetail, buffer,
   3254                                     size);
   3255 #endif
   3256   }
   3257   return 0;
   3258 }
   3259 
   3260 TPM_RC TPMU_PUBLIC_PARMS_Unmarshal(TPMU_PUBLIC_PARMS* target,
   3261                                    BYTE** buffer,
   3262                                    INT32* size,
   3263                                    UINT32 selector) {
   3264   switch (selector) {
   3265 #ifdef TPM_ALG_KEYEDHASH
   3266     case TPM_ALG_KEYEDHASH:
   3267       return TPMS_KEYEDHASH_PARMS_Unmarshal(
   3268           (TPMS_KEYEDHASH_PARMS*)&target->keyedHashDetail, buffer, size);
   3269 #endif
   3270 #ifdef TPM_ALG_SYMCIPHER
   3271     case TPM_ALG_SYMCIPHER:
   3272       return TPMS_SYMCIPHER_PARMS_Unmarshal(
   3273           (TPMS_SYMCIPHER_PARMS*)&target->symDetail, buffer, size);
   3274 #endif
   3275 #ifdef TPM_ALG_RSA
   3276     case TPM_ALG_RSA:
   3277       return TPMS_RSA_PARMS_Unmarshal((TPMS_RSA_PARMS*)&target->rsaDetail,
   3278                                       buffer, size);
   3279 #endif
   3280 #ifdef TPM_ALG_ECC
   3281     case TPM_ALG_ECC:
   3282       return TPMS_ECC_PARMS_Unmarshal((TPMS_ECC_PARMS*)&target->eccDetail,
   3283                                       buffer, size);
   3284 #endif
   3285   }
   3286   return TPM_RC_SELECTOR;
   3287 }
   3288 
   3289 UINT16 TPMT_PUBLIC_Marshal(TPMT_PUBLIC* source, BYTE** buffer, INT32* size) {
   3290   UINT16 total_size = 0;
   3291   total_size += TPMI_ALG_PUBLIC_Marshal(&source->type, buffer, size);
   3292   total_size += TPMI_ALG_HASH_Marshal(&source->nameAlg, buffer, size);
   3293   total_size += TPMA_OBJECT_Marshal(&source->objectAttributes, buffer, size);
   3294   total_size += TPM2B_DIGEST_Marshal(&source->authPolicy, buffer, size);
   3295   total_size += TPMU_PUBLIC_PARMS_Marshal(&source->parameters, buffer, size,
   3296                                           source->type);
   3297   total_size +=
   3298       TPMU_PUBLIC_ID_Marshal(&source->unique, buffer, size, source->type);
   3299   return total_size;
   3300 }
   3301 
   3302 TPM_RC TPMT_PUBLIC_Unmarshal(TPMT_PUBLIC* target, BYTE** buffer, INT32* size) {
   3303   TPM_RC result;
   3304   result = TPMI_ALG_PUBLIC_Unmarshal(&target->type, buffer, size);
   3305   if (result != TPM_RC_SUCCESS) {
   3306     return result;
   3307   }
   3308   result = TPMI_ALG_HASH_Unmarshal(&target->nameAlg, buffer, size, TRUE);
   3309   if (result != TPM_RC_SUCCESS) {
   3310     return result;
   3311   }
   3312   result = TPMA_OBJECT_Unmarshal(&target->objectAttributes, buffer, size);
   3313   if (result != TPM_RC_SUCCESS) {
   3314     return result;
   3315   }
   3316   result = TPM2B_DIGEST_Unmarshal(&target->authPolicy, buffer, size);
   3317   if (result != TPM_RC_SUCCESS) {
   3318     return result;
   3319   }
   3320   result = TPMU_PUBLIC_PARMS_Unmarshal(&target->parameters, buffer, size,
   3321                                        target->type);
   3322   if (result != TPM_RC_SUCCESS) {
   3323     return result;
   3324   }
   3325   result =
   3326       TPMU_PUBLIC_ID_Unmarshal(&target->unique, buffer, size, target->type);
   3327   if (result != TPM_RC_SUCCESS) {
   3328     return result;
   3329   }
   3330   return TPM_RC_SUCCESS;
   3331 }
   3332 
   3333 UINT16 TPM2B_PUBLIC_Marshal(TPM2B_PUBLIC* source, BYTE** buffer, INT32* size) {
   3334   UINT16 total_size = 0;
   3335   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   3336   total_size += TPMT_PUBLIC_Marshal(&source->t.publicArea, buffer, size);
   3337   {
   3338     BYTE* size_location = *buffer - total_size;
   3339     INT32 size_field_size = sizeof(UINT16);
   3340     UINT16 payload_size = total_size - (UINT16)size_field_size;
   3341     UINT16_Marshal(&payload_size, &size_location, &size_field_size);
   3342   }
   3343   return total_size;
   3344 }
   3345 
   3346 TPM_RC TPM2B_PUBLIC_Unmarshal(TPM2B_PUBLIC* target,
   3347                               BYTE** buffer,
   3348                               INT32* size) {
   3349   TPM_RC result;
   3350   UINT32 start_size = *size;
   3351   UINT32 struct_size;
   3352   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   3353   if (result != TPM_RC_SUCCESS) {
   3354     return result;
   3355   }
   3356   if (target->t.size == 0) {
   3357     return TPM_RC_SIZE;
   3358   }
   3359   result = TPMT_PUBLIC_Unmarshal(&target->t.publicArea, buffer, size);
   3360   if (result != TPM_RC_SUCCESS) {
   3361     return result;
   3362   }
   3363   struct_size = start_size - *size - sizeof(target->t.size);
   3364   if (struct_size != target->t.size) {
   3365     return TPM_RC_SIZE;
   3366   }
   3367   return TPM_RC_SUCCESS;
   3368 }
   3369 
   3370 UINT16 TPM2B_SENSITIVE_DATA_Marshal(TPM2B_SENSITIVE_DATA* source,
   3371                                     BYTE** buffer,
   3372                                     INT32* size) {
   3373   UINT16 total_size = 0;
   3374   INT32 i;
   3375   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   3376   for (i = 0; i < source->t.size; ++i) {
   3377     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   3378   }
   3379   return total_size;
   3380 }
   3381 
   3382 TPM_RC TPM2B_SENSITIVE_DATA_Unmarshal(TPM2B_SENSITIVE_DATA* target,
   3383                                       BYTE** buffer,
   3384                                       INT32* size) {
   3385   TPM_RC result;
   3386   INT32 i;
   3387   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   3388   if (result != TPM_RC_SUCCESS) {
   3389     return result;
   3390   }
   3391   if (target->t.size == 0) {
   3392     return TPM_RC_SUCCESS;
   3393   }
   3394   if (target->t.size > MAX_SYM_DATA) {
   3395     return TPM_RC_SIZE;
   3396   }
   3397   for (i = 0; i < target->t.size; ++i) {
   3398     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   3399     if (result != TPM_RC_SUCCESS) {
   3400       return result;
   3401     }
   3402   }
   3403   return TPM_RC_SUCCESS;
   3404 }
   3405 
   3406 UINT16 TPM2B_SYM_KEY_Marshal(TPM2B_SYM_KEY* source,
   3407                              BYTE** buffer,
   3408                              INT32* size) {
   3409   UINT16 total_size = 0;
   3410   INT32 i;
   3411   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   3412   for (i = 0; i < source->t.size; ++i) {
   3413     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   3414   }
   3415   return total_size;
   3416 }
   3417 
   3418 TPM_RC TPM2B_SYM_KEY_Unmarshal(TPM2B_SYM_KEY* target,
   3419                                BYTE** buffer,
   3420                                INT32* size) {
   3421   TPM_RC result;
   3422   INT32 i;
   3423   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   3424   if (result != TPM_RC_SUCCESS) {
   3425     return result;
   3426   }
   3427   if (target->t.size == 0) {
   3428     return TPM_RC_SUCCESS;
   3429   }
   3430   if (target->t.size > MAX_SYM_KEY_BYTES) {
   3431     return TPM_RC_SIZE;
   3432   }
   3433   for (i = 0; i < target->t.size; ++i) {
   3434     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   3435     if (result != TPM_RC_SUCCESS) {
   3436       return result;
   3437     }
   3438   }
   3439   return TPM_RC_SUCCESS;
   3440 }
   3441 
   3442 UINT16 TPMU_SENSITIVE_COMPOSITE_Marshal(TPMU_SENSITIVE_COMPOSITE* source,
   3443                                         BYTE** buffer,
   3444                                         INT32* size,
   3445                                         UINT32 selector) {
   3446   switch (selector) {
   3447 #ifdef TPM_ALG_RSA
   3448     case TPM_ALG_RSA:
   3449       return TPM2B_PRIVATE_KEY_RSA_Marshal((TPM2B_PRIVATE_KEY_RSA*)&source->rsa,
   3450                                            buffer, size);
   3451 #endif
   3452 #ifdef TPM_ALG_ECC
   3453     case TPM_ALG_ECC:
   3454       return TPM2B_ECC_PARAMETER_Marshal((TPM2B_ECC_PARAMETER*)&source->ecc,
   3455                                          buffer, size);
   3456 #endif
   3457 #ifdef TPM_ALG_KEYEDHASH
   3458     case TPM_ALG_KEYEDHASH:
   3459       return TPM2B_SENSITIVE_DATA_Marshal((TPM2B_SENSITIVE_DATA*)&source->bits,
   3460                                           buffer, size);
   3461 #endif
   3462 #ifdef TPM_ALG_SYMCIPHER
   3463     case TPM_ALG_SYMCIPHER:
   3464       return TPM2B_SYM_KEY_Marshal((TPM2B_SYM_KEY*)&source->sym, buffer, size);
   3465 #endif
   3466   }
   3467   return 0;
   3468 }
   3469 
   3470 TPM_RC TPMU_SENSITIVE_COMPOSITE_Unmarshal(TPMU_SENSITIVE_COMPOSITE* target,
   3471                                           BYTE** buffer,
   3472                                           INT32* size,
   3473                                           UINT32 selector) {
   3474   switch (selector) {
   3475 #ifdef TPM_ALG_RSA
   3476     case TPM_ALG_RSA:
   3477       return TPM2B_PRIVATE_KEY_RSA_Unmarshal(
   3478           (TPM2B_PRIVATE_KEY_RSA*)&target->rsa, buffer, size);
   3479 #endif
   3480 #ifdef TPM_ALG_ECC
   3481     case TPM_ALG_ECC:
   3482       return TPM2B_ECC_PARAMETER_Unmarshal((TPM2B_ECC_PARAMETER*)&target->ecc,
   3483                                            buffer, size);
   3484 #endif
   3485 #ifdef TPM_ALG_KEYEDHASH
   3486     case TPM_ALG_KEYEDHASH:
   3487       return TPM2B_SENSITIVE_DATA_Unmarshal(
   3488           (TPM2B_SENSITIVE_DATA*)&target->bits, buffer, size);
   3489 #endif
   3490 #ifdef TPM_ALG_SYMCIPHER
   3491     case TPM_ALG_SYMCIPHER:
   3492       return TPM2B_SYM_KEY_Unmarshal((TPM2B_SYM_KEY*)&target->sym, buffer,
   3493                                      size);
   3494 #endif
   3495   }
   3496   return TPM_RC_SELECTOR;
   3497 }
   3498 
   3499 UINT16 TPMT_SENSITIVE_Marshal(TPMT_SENSITIVE* source,
   3500                               BYTE** buffer,
   3501                               INT32* size) {
   3502   UINT16 total_size = 0;
   3503   total_size += TPMI_ALG_PUBLIC_Marshal(&source->sensitiveType, buffer, size);
   3504   total_size += TPM2B_AUTH_Marshal(&source->authValue, buffer, size);
   3505   total_size += TPM2B_DIGEST_Marshal(&source->seedValue, buffer, size);
   3506   total_size += TPMU_SENSITIVE_COMPOSITE_Marshal(&source->sensitive, buffer,
   3507                                                  size, source->sensitiveType);
   3508   return total_size;
   3509 }
   3510 
   3511 TPM_RC TPMT_SENSITIVE_Unmarshal(TPMT_SENSITIVE* target,
   3512                                 BYTE** buffer,
   3513                                 INT32* size) {
   3514   TPM_RC result;
   3515   result = TPMI_ALG_PUBLIC_Unmarshal(&target->sensitiveType, buffer, size);
   3516   if (result != TPM_RC_SUCCESS) {
   3517     return result;
   3518   }
   3519   result = TPM2B_AUTH_Unmarshal(&target->authValue, buffer, size);
   3520   if (result != TPM_RC_SUCCESS) {
   3521     return result;
   3522   }
   3523   result = TPM2B_DIGEST_Unmarshal(&target->seedValue, buffer, size);
   3524   if (result != TPM_RC_SUCCESS) {
   3525     return result;
   3526   }
   3527   result = TPMU_SENSITIVE_COMPOSITE_Unmarshal(&target->sensitive, buffer, size,
   3528                                               target->sensitiveType);
   3529   if (result != TPM_RC_SUCCESS) {
   3530     return result;
   3531   }
   3532   return TPM_RC_SUCCESS;
   3533 }
   3534 
   3535 UINT16 TPM2B_SENSITIVE_Marshal(TPM2B_SENSITIVE* source,
   3536                                BYTE** buffer,
   3537                                INT32* size) {
   3538   UINT16 total_size = 0;
   3539   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   3540   total_size += TPMT_SENSITIVE_Marshal(&source->t.sensitiveArea, buffer, size);
   3541   return total_size;
   3542 }
   3543 
   3544 TPM_RC TPM2B_SENSITIVE_Unmarshal(TPM2B_SENSITIVE* target,
   3545                                  BYTE** buffer,
   3546                                  INT32* size) {
   3547   TPM_RC result;
   3548   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   3549   if (result != TPM_RC_SUCCESS) {
   3550     return result;
   3551   }
   3552   if (target->t.size == 0) {
   3553     return TPM_RC_SUCCESS;
   3554   }
   3555   result = TPMT_SENSITIVE_Unmarshal(&target->t.sensitiveArea, buffer, size);
   3556   if (result != TPM_RC_SUCCESS) {
   3557     return result;
   3558   }
   3559   return TPM_RC_SUCCESS;
   3560 }
   3561 
   3562 UINT16 TPMS_SENSITIVE_CREATE_Marshal(TPMS_SENSITIVE_CREATE* source,
   3563                                      BYTE** buffer,
   3564                                      INT32* size) {
   3565   UINT16 total_size = 0;
   3566   total_size += TPM2B_AUTH_Marshal(&source->userAuth, buffer, size);
   3567   total_size += TPM2B_SENSITIVE_DATA_Marshal(&source->data, buffer, size);
   3568   return total_size;
   3569 }
   3570 
   3571 TPM_RC TPMS_SENSITIVE_CREATE_Unmarshal(TPMS_SENSITIVE_CREATE* target,
   3572                                        BYTE** buffer,
   3573                                        INT32* size) {
   3574   TPM_RC result;
   3575   result = TPM2B_AUTH_Unmarshal(&target->userAuth, buffer, size);
   3576   if (result != TPM_RC_SUCCESS) {
   3577     return result;
   3578   }
   3579   result = TPM2B_SENSITIVE_DATA_Unmarshal(&target->data, buffer, size);
   3580   if (result != TPM_RC_SUCCESS) {
   3581     return result;
   3582   }
   3583   return TPM_RC_SUCCESS;
   3584 }
   3585 
   3586 UINT16 TPM2B_SENSITIVE_CREATE_Marshal(TPM2B_SENSITIVE_CREATE* source,
   3587                                       BYTE** buffer,
   3588                                       INT32* size) {
   3589   UINT16 total_size = 0;
   3590   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   3591   total_size +=
   3592       TPMS_SENSITIVE_CREATE_Marshal(&source->t.sensitive, buffer, size);
   3593   {
   3594     BYTE* size_location = *buffer - total_size;
   3595     INT32 size_field_size = sizeof(UINT16);
   3596     UINT16 payload_size = total_size - (UINT16)size_field_size;
   3597     UINT16_Marshal(&payload_size, &size_location, &size_field_size);
   3598   }
   3599   return total_size;
   3600 }
   3601 
   3602 TPM_RC TPM2B_SENSITIVE_CREATE_Unmarshal(TPM2B_SENSITIVE_CREATE* target,
   3603                                         BYTE** buffer,
   3604                                         INT32* size) {
   3605   TPM_RC result;
   3606   UINT32 start_size = *size;
   3607   UINT32 struct_size;
   3608   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   3609   if (result != TPM_RC_SUCCESS) {
   3610     return result;
   3611   }
   3612   if (target->t.size == 0) {
   3613     return TPM_RC_SIZE;
   3614   }
   3615   result = TPMS_SENSITIVE_CREATE_Unmarshal(&target->t.sensitive, buffer, size);
   3616   if (result != TPM_RC_SUCCESS) {
   3617     return result;
   3618   }
   3619   struct_size = start_size - *size - sizeof(target->t.size);
   3620   if (struct_size != target->t.size) {
   3621     return TPM_RC_SIZE;
   3622   }
   3623   return TPM_RC_SUCCESS;
   3624 }
   3625 
   3626 UINT16 TPM2B_TIMEOUT_Marshal(TPM2B_TIMEOUT* source,
   3627                              BYTE** buffer,
   3628                              INT32* size) {
   3629   UINT16 total_size = 0;
   3630   INT32 i;
   3631   total_size += UINT16_Marshal(&source->t.size, buffer, size);
   3632   for (i = 0; i < source->t.size; ++i) {
   3633     total_size += BYTE_Marshal(&source->t.buffer[i], buffer, size);
   3634   }
   3635   return total_size;
   3636 }
   3637 
   3638 TPM_RC TPM2B_TIMEOUT_Unmarshal(TPM2B_TIMEOUT* target,
   3639                                BYTE** buffer,
   3640                                INT32* size) {
   3641   TPM_RC result;
   3642   INT32 i;
   3643   result = UINT16_Unmarshal(&target->t.size, buffer, size);
   3644   if (result != TPM_RC_SUCCESS) {
   3645     return result;
   3646   }
   3647   if (target->t.size == 0) {
   3648     return TPM_RC_SUCCESS;
   3649   }
   3650   if (target->t.size > sizeof(UINT64)) {
   3651     return TPM_RC_SIZE;
   3652   }
   3653   for (i = 0; i < target->t.size; ++i) {
   3654     result = BYTE_Unmarshal(&target->t.buffer[i], buffer, size);
   3655     if (result != TPM_RC_SUCCESS) {
   3656       return result;
   3657     }
   3658   }
   3659   return TPM_RC_SUCCESS;
   3660 }
   3661 
   3662 UINT16 TPMA_ALGORITHM_Marshal(TPMA_ALGORITHM* source,
   3663                               BYTE** buffer,
   3664                               INT32* size) {
   3665   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   3666 }
   3667 
   3668 TPM_RC TPMA_ALGORITHM_Unmarshal(TPMA_ALGORITHM* target,
   3669                                 BYTE** buffer,
   3670                                 INT32* size) {
   3671   TPM_RC result;
   3672   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   3673   if (result != TPM_RC_SUCCESS) {
   3674     return result;
   3675   }
   3676   if (target->reserved4_7 != 0) {
   3677     return TPM_RC_RESERVED_BITS;
   3678   }
   3679   if (target->reserved11_31 != 0) {
   3680     return TPM_RC_RESERVED_BITS;
   3681   }
   3682   return TPM_RC_SUCCESS;
   3683 }
   3684 
   3685 UINT16 TPMA_CC_Marshal(TPMA_CC* source, BYTE** buffer, INT32* size) {
   3686   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   3687 }
   3688 
   3689 TPM_RC TPMA_CC_Unmarshal(TPMA_CC* target, BYTE** buffer, INT32* size) {
   3690   TPM_RC result;
   3691   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   3692   if (result != TPM_RC_SUCCESS) {
   3693     return result;
   3694   }
   3695   if (target->reserved16_21 != 0) {
   3696     return TPM_RC_RESERVED_BITS;
   3697   }
   3698   return TPM_RC_SUCCESS;
   3699 }
   3700 
   3701 UINT16 TPMA_MEMORY_Marshal(TPMA_MEMORY* source, BYTE** buffer, INT32* size) {
   3702   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   3703 }
   3704 
   3705 TPM_RC TPMA_MEMORY_Unmarshal(TPMA_MEMORY* target, BYTE** buffer, INT32* size) {
   3706   TPM_RC result;
   3707   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   3708   if (result != TPM_RC_SUCCESS) {
   3709     return result;
   3710   }
   3711   if (target->reserved3_31 != 0) {
   3712     return TPM_RC_RESERVED_BITS;
   3713   }
   3714   return TPM_RC_SUCCESS;
   3715 }
   3716 
   3717 UINT16 TPMA_PERMANENT_Marshal(TPMA_PERMANENT* source,
   3718                               BYTE** buffer,
   3719                               INT32* size) {
   3720   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   3721 }
   3722 
   3723 TPM_RC TPMA_PERMANENT_Unmarshal(TPMA_PERMANENT* target,
   3724                                 BYTE** buffer,
   3725                                 INT32* size) {
   3726   TPM_RC result;
   3727   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   3728   if (result != TPM_RC_SUCCESS) {
   3729     return result;
   3730   }
   3731   if (target->reserved3_7 != 0) {
   3732     return TPM_RC_RESERVED_BITS;
   3733   }
   3734   if (target->reserved11_31 != 0) {
   3735     return TPM_RC_RESERVED_BITS;
   3736   }
   3737   return TPM_RC_SUCCESS;
   3738 }
   3739 
   3740 UINT16 TPMA_SESSION_Marshal(TPMA_SESSION* source, BYTE** buffer, INT32* size) {
   3741   return uint8_t_Marshal((uint8_t*)source, buffer, size);
   3742 }
   3743 
   3744 TPM_RC TPMA_SESSION_Unmarshal(TPMA_SESSION* target,
   3745                               BYTE** buffer,
   3746                               INT32* size) {
   3747   TPM_RC result;
   3748   result = uint8_t_Unmarshal((uint8_t*)target, buffer, size);
   3749   if (result != TPM_RC_SUCCESS) {
   3750     return result;
   3751   }
   3752   if (target->reserved3_4 != 0) {
   3753     return TPM_RC_RESERVED_BITS;
   3754   }
   3755   return TPM_RC_SUCCESS;
   3756 }
   3757 
   3758 UINT16 TPMA_STARTUP_CLEAR_Marshal(TPMA_STARTUP_CLEAR* source,
   3759                                   BYTE** buffer,
   3760                                   INT32* size) {
   3761   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   3762 }
   3763 
   3764 TPM_RC TPMA_STARTUP_CLEAR_Unmarshal(TPMA_STARTUP_CLEAR* target,
   3765                                     BYTE** buffer,
   3766                                     INT32* size) {
   3767   TPM_RC result;
   3768   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   3769   if (result != TPM_RC_SUCCESS) {
   3770     return result;
   3771   }
   3772   if (target->reserved4_30 != 0) {
   3773     return TPM_RC_RESERVED_BITS;
   3774   }
   3775   return TPM_RC_SUCCESS;
   3776 }
   3777 
   3778 UINT16 TPMI_ALG_ASYM_Marshal(TPMI_ALG_ASYM* source,
   3779                              BYTE** buffer,
   3780                              INT32* size) {
   3781   return uint16_t_Marshal(source, buffer, size);
   3782 }
   3783 
   3784 TPM_RC TPMI_ALG_ASYM_Unmarshal(TPMI_ALG_ASYM* target,
   3785                                BYTE** buffer,
   3786                                INT32* size,
   3787                                BOOL allow_conditional_value) {
   3788   TPM_RC result;
   3789   BOOL has_valid_value = FALSE;
   3790   result = uint16_t_Unmarshal(target, buffer, size);
   3791   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3792     return result;
   3793   }
   3794   if (*target == TPM_ALG_NULL) {
   3795     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_ASYMMETRIC;
   3796   }
   3797   switch (*target) {
   3798 #ifdef TPM_ALG_RSA
   3799     case TPM_ALG_RSA:
   3800 #endif
   3801 #ifdef TPM_ALG_ECC
   3802     case TPM_ALG_ECC:
   3803 #endif
   3804       has_valid_value = TRUE;
   3805       break;
   3806   }
   3807   if (!has_valid_value) {
   3808     return TPM_RC_ASYMMETRIC;
   3809   }
   3810   return TPM_RC_SUCCESS;
   3811 }
   3812 
   3813 UINT16 TPMI_ALG_RSA_DECRYPT_Marshal(TPMI_ALG_RSA_DECRYPT* source,
   3814                                     BYTE** buffer,
   3815                                     INT32* size) {
   3816   return uint16_t_Marshal(source, buffer, size);
   3817 }
   3818 
   3819 TPM_RC TPMI_ALG_RSA_DECRYPT_Unmarshal(TPMI_ALG_RSA_DECRYPT* target,
   3820                                       BYTE** buffer,
   3821                                       INT32* size,
   3822                                       BOOL allow_conditional_value) {
   3823   TPM_RC result;
   3824   BOOL has_valid_value = FALSE;
   3825   result = uint16_t_Unmarshal(target, buffer, size);
   3826   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3827     return result;
   3828   }
   3829   if (*target == TPM_ALG_NULL) {
   3830     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   3831   }
   3832   switch (*target) {
   3833 #ifdef TPM_ALG_RSAES
   3834     case TPM_ALG_RSAES:
   3835 #endif
   3836 #ifdef TPM_ALG_OAEP
   3837     case TPM_ALG_OAEP:
   3838 #endif
   3839       has_valid_value = TRUE;
   3840       break;
   3841   }
   3842   if (!has_valid_value) {
   3843     return TPM_RC_VALUE;
   3844   }
   3845   return TPM_RC_SUCCESS;
   3846 }
   3847 
   3848 UINT16 TPMI_ALG_SIG_SCHEME_Marshal(TPMI_ALG_SIG_SCHEME* source,
   3849                                    BYTE** buffer,
   3850                                    INT32* size) {
   3851   return uint16_t_Marshal(source, buffer, size);
   3852 }
   3853 
   3854 TPM_RC TPMI_ALG_SIG_SCHEME_Unmarshal(TPMI_ALG_SIG_SCHEME* target,
   3855                                      BYTE** buffer,
   3856                                      INT32* size,
   3857                                      BOOL allow_conditional_value) {
   3858   TPM_RC result;
   3859   BOOL has_valid_value = FALSE;
   3860   result = uint16_t_Unmarshal(target, buffer, size);
   3861   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3862     return result;
   3863   }
   3864   if (*target == TPM_ALG_NULL) {
   3865     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_SCHEME;
   3866   }
   3867   switch (*target) {
   3868 #ifdef TPM_ALG_RSASSA
   3869     case TPM_ALG_RSASSA:
   3870 #endif
   3871 #ifdef TPM_ALG_RSAPSS
   3872     case TPM_ALG_RSAPSS:
   3873 #endif
   3874 #ifdef TPM_ALG_ECDSA
   3875     case TPM_ALG_ECDSA:
   3876 #endif
   3877 #ifdef TPM_ALG_ECDAA
   3878     case TPM_ALG_ECDAA:
   3879 #endif
   3880 #ifdef TPM_ALG_SM2
   3881     case TPM_ALG_SM2:
   3882 #endif
   3883 #ifdef TPM_ALG_ECSCHNORR
   3884     case TPM_ALG_ECSCHNORR:
   3885 #endif
   3886 #ifdef TPM_ALG_HMAC
   3887     case TPM_ALG_HMAC:
   3888 #endif
   3889       has_valid_value = TRUE;
   3890       break;
   3891   }
   3892   if (!has_valid_value) {
   3893     return TPM_RC_SCHEME;
   3894   }
   3895   return TPM_RC_SUCCESS;
   3896 }
   3897 
   3898 UINT16 TPMI_ALG_SYM_Marshal(TPMI_ALG_SYM* source, BYTE** buffer, INT32* size) {
   3899   return uint16_t_Marshal(source, buffer, size);
   3900 }
   3901 
   3902 TPM_RC TPMI_ALG_SYM_Unmarshal(TPMI_ALG_SYM* target,
   3903                               BYTE** buffer,
   3904                               INT32* size,
   3905                               BOOL allow_conditional_value) {
   3906   TPM_RC result;
   3907   BOOL has_valid_value = FALSE;
   3908   result = uint16_t_Unmarshal(target, buffer, size);
   3909   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3910     return result;
   3911   }
   3912   if (*target == TPM_ALG_NULL) {
   3913     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_SYMMETRIC;
   3914   }
   3915   switch (*target) {
   3916 #ifdef TPM_ALG_AES
   3917     case TPM_ALG_AES:
   3918 #endif
   3919 #ifdef TPM_ALG_SM4
   3920     case TPM_ALG_SM4:
   3921 #endif
   3922 #ifdef TPM_ALG_CAMELLIA
   3923     case TPM_ALG_CAMELLIA:
   3924 #endif
   3925 #ifdef TPM_ALG_XOR
   3926     case TPM_ALG_XOR:
   3927 #endif
   3928       has_valid_value = TRUE;
   3929       break;
   3930   }
   3931   if (!has_valid_value) {
   3932     return TPM_RC_SYMMETRIC;
   3933   }
   3934   return TPM_RC_SUCCESS;
   3935 }
   3936 
   3937 UINT16 TPMI_DH_CONTEXT_Marshal(TPMI_DH_CONTEXT* source,
   3938                                BYTE** buffer,
   3939                                INT32* size) {
   3940   return uint32_t_Marshal(source, buffer, size);
   3941 }
   3942 
   3943 TPM_RC TPMI_DH_CONTEXT_Unmarshal(TPMI_DH_CONTEXT* target,
   3944                                  BYTE** buffer,
   3945                                  INT32* size) {
   3946   TPM_RC result;
   3947   BOOL has_valid_value = FALSE;
   3948   result = uint32_t_Unmarshal(target, buffer, size);
   3949   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3950     return result;
   3951   }
   3952   if ((*target >= HMAC_SESSION_FIRST) && (*target <= HMAC_SESSION_LAST)) {
   3953     has_valid_value = TRUE;
   3954   }
   3955   if ((*target >= POLICY_SESSION_FIRST) && (*target <= POLICY_SESSION_LAST)) {
   3956     has_valid_value = TRUE;
   3957   }
   3958   if ((*target >= TRANSIENT_FIRST) && (*target <= TRANSIENT_LAST)) {
   3959     has_valid_value = TRUE;
   3960   }
   3961   if (!has_valid_value) {
   3962     return TPM_RC_VALUE;
   3963   }
   3964   return TPM_RC_SUCCESS;
   3965 }
   3966 
   3967 UINT16 TPMI_DH_ENTITY_Marshal(TPMI_DH_ENTITY* source,
   3968                               BYTE** buffer,
   3969                               INT32* size) {
   3970   return uint32_t_Marshal(source, buffer, size);
   3971 }
   3972 
   3973 TPM_RC TPMI_DH_ENTITY_Unmarshal(TPMI_DH_ENTITY* target,
   3974                                 BYTE** buffer,
   3975                                 INT32* size,
   3976                                 BOOL allow_conditional_value) {
   3977   TPM_RC result;
   3978   BOOL has_valid_value = FALSE;
   3979   result = uint32_t_Unmarshal(target, buffer, size);
   3980   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   3981     return result;
   3982   }
   3983   if (*target == TPM_RH_NULL) {
   3984     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   3985   }
   3986   switch (*target) {
   3987     case TPM_RH_OWNER:
   3988     case TPM_RH_ENDORSEMENT:
   3989     case TPM_RH_PLATFORM:
   3990     case TPM_RH_LOCKOUT:
   3991       has_valid_value = TRUE;
   3992       break;
   3993   }
   3994   if ((*target >= TRANSIENT_FIRST) && (*target <= TRANSIENT_LAST)) {
   3995     has_valid_value = TRUE;
   3996   }
   3997   if ((*target >= PERSISTENT_FIRST) && (*target <= PERSISTENT_LAST)) {
   3998     has_valid_value = TRUE;
   3999   }
   4000   if ((*target >= NV_INDEX_FIRST) && (*target <= NV_INDEX_LAST)) {
   4001     has_valid_value = TRUE;
   4002   }
   4003   if ((*target >= PCR_FIRST) && (*target <= PCR_LAST)) {
   4004     has_valid_value = TRUE;
   4005   }
   4006   if ((*target >= TPM_RH_AUTH_00) && (*target <= TPM_RH_AUTH_FF)) {
   4007     has_valid_value = TRUE;
   4008   }
   4009   if (!has_valid_value) {
   4010     return TPM_RC_VALUE;
   4011   }
   4012   return TPM_RC_SUCCESS;
   4013 }
   4014 
   4015 UINT16 TPMI_DH_OBJECT_Marshal(TPMI_DH_OBJECT* source,
   4016                               BYTE** buffer,
   4017                               INT32* size) {
   4018   return uint32_t_Marshal(source, buffer, size);
   4019 }
   4020 
   4021 TPM_RC TPMI_DH_OBJECT_Unmarshal(TPMI_DH_OBJECT* target,
   4022                                 BYTE** buffer,
   4023                                 INT32* size,
   4024                                 BOOL allow_conditional_value) {
   4025   TPM_RC result;
   4026   BOOL has_valid_value = FALSE;
   4027   result = uint32_t_Unmarshal(target, buffer, size);
   4028   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4029     return result;
   4030   }
   4031   if (*target == TPM_RH_NULL) {
   4032     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4033   }
   4034   if ((*target >= TRANSIENT_FIRST) && (*target <= TRANSIENT_LAST)) {
   4035     has_valid_value = TRUE;
   4036   }
   4037   if ((*target >= PERSISTENT_FIRST) && (*target <= PERSISTENT_LAST)) {
   4038     has_valid_value = TRUE;
   4039   }
   4040   if (!has_valid_value) {
   4041     return TPM_RC_VALUE;
   4042   }
   4043   return TPM_RC_SUCCESS;
   4044 }
   4045 
   4046 UINT16 TPMI_DH_PCR_Marshal(TPMI_DH_PCR* source, BYTE** buffer, INT32* size) {
   4047   return uint32_t_Marshal(source, buffer, size);
   4048 }
   4049 
   4050 TPM_RC TPMI_DH_PCR_Unmarshal(TPMI_DH_PCR* target,
   4051                              BYTE** buffer,
   4052                              INT32* size,
   4053                              BOOL allow_conditional_value) {
   4054   TPM_RC result;
   4055   BOOL has_valid_value = FALSE;
   4056   result = uint32_t_Unmarshal(target, buffer, size);
   4057   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4058     return result;
   4059   }
   4060   if (*target == TPM_RH_NULL) {
   4061     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4062   }
   4063   if ((*target >= PCR_FIRST) && (*target <= PCR_LAST)) {
   4064     has_valid_value = TRUE;
   4065   }
   4066   if (!has_valid_value) {
   4067     return TPM_RC_VALUE;
   4068   }
   4069   return TPM_RC_SUCCESS;
   4070 }
   4071 
   4072 UINT16 TPMI_DH_PERSISTENT_Marshal(TPMI_DH_PERSISTENT* source,
   4073                                   BYTE** buffer,
   4074                                   INT32* size) {
   4075   return uint32_t_Marshal(source, buffer, size);
   4076 }
   4077 
   4078 TPM_RC TPMI_DH_PERSISTENT_Unmarshal(TPMI_DH_PERSISTENT* target,
   4079                                     BYTE** buffer,
   4080                                     INT32* size) {
   4081   TPM_RC result;
   4082   BOOL has_valid_value = FALSE;
   4083   result = uint32_t_Unmarshal(target, buffer, size);
   4084   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4085     return result;
   4086   }
   4087   if ((*target >= PERSISTENT_FIRST) && (*target <= PERSISTENT_LAST)) {
   4088     has_valid_value = TRUE;
   4089   }
   4090   if (!has_valid_value) {
   4091     return TPM_RC_VALUE;
   4092   }
   4093   return TPM_RC_SUCCESS;
   4094 }
   4095 
   4096 UINT16 TPMI_ECC_KEY_EXCHANGE_Marshal(TPMI_ECC_KEY_EXCHANGE* source,
   4097                                      BYTE** buffer,
   4098                                      INT32* size) {
   4099   return uint16_t_Marshal(source, buffer, size);
   4100 }
   4101 
   4102 TPM_RC TPMI_ECC_KEY_EXCHANGE_Unmarshal(TPMI_ECC_KEY_EXCHANGE* target,
   4103                                        BYTE** buffer,
   4104                                        INT32* size,
   4105                                        BOOL allow_conditional_value) {
   4106   TPM_RC result;
   4107   BOOL has_valid_value = FALSE;
   4108   result = uint16_t_Unmarshal(target, buffer, size);
   4109   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4110     return result;
   4111   }
   4112   if (*target == TPM_ALG_NULL) {
   4113     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_SCHEME;
   4114   }
   4115   switch (*target) {
   4116 #ifdef TPM_ALG_ECDH
   4117     case TPM_ALG_ECDH:
   4118 #endif
   4119 #ifdef TPM_ALG_ECMQV
   4120     case TPM_ALG_ECMQV:
   4121 #endif
   4122 #ifdef TPM_ALG_SM2
   4123     case TPM_ALG_SM2:
   4124 #endif
   4125       has_valid_value = TRUE;
   4126       break;
   4127   }
   4128   if (!has_valid_value) {
   4129     return TPM_RC_SCHEME;
   4130   }
   4131   return TPM_RC_SUCCESS;
   4132 }
   4133 
   4134 UINT16 TPMI_RH_CLEAR_Marshal(TPMI_RH_CLEAR* source,
   4135                              BYTE** buffer,
   4136                              INT32* size) {
   4137   return uint32_t_Marshal(source, buffer, size);
   4138 }
   4139 
   4140 TPM_RC TPMI_RH_CLEAR_Unmarshal(TPMI_RH_CLEAR* target,
   4141                                BYTE** buffer,
   4142                                INT32* size) {
   4143   TPM_RC result;
   4144   BOOL has_valid_value = FALSE;
   4145   result = uint32_t_Unmarshal(target, buffer, size);
   4146   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4147     return result;
   4148   }
   4149   switch (*target) {
   4150     case TPM_RH_LOCKOUT:
   4151     case TPM_RH_PLATFORM:
   4152       has_valid_value = TRUE;
   4153       break;
   4154   }
   4155   if (!has_valid_value) {
   4156     return TPM_RC_VALUE;
   4157   }
   4158   return TPM_RC_SUCCESS;
   4159 }
   4160 
   4161 UINT16 TPMI_RH_ENABLES_Marshal(TPMI_RH_ENABLES* source,
   4162                                BYTE** buffer,
   4163                                INT32* size) {
   4164   return uint32_t_Marshal(source, buffer, size);
   4165 }
   4166 
   4167 TPM_RC TPMI_RH_ENABLES_Unmarshal(TPMI_RH_ENABLES* target,
   4168                                  BYTE** buffer,
   4169                                  INT32* size,
   4170                                  BOOL allow_conditional_value) {
   4171   TPM_RC result;
   4172   BOOL has_valid_value = FALSE;
   4173   result = uint32_t_Unmarshal(target, buffer, size);
   4174   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4175     return result;
   4176   }
   4177   if (*target == TPM_RH_NULL) {
   4178     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4179   }
   4180   switch (*target) {
   4181     case TPM_RH_OWNER:
   4182     case TPM_RH_PLATFORM:
   4183     case TPM_RH_ENDORSEMENT:
   4184     case TPM_RH_PLATFORM_NV:
   4185       has_valid_value = TRUE;
   4186       break;
   4187   }
   4188   if (!has_valid_value) {
   4189     return TPM_RC_VALUE;
   4190   }
   4191   return TPM_RC_SUCCESS;
   4192 }
   4193 
   4194 UINT16 TPMI_RH_ENDORSEMENT_Marshal(TPMI_RH_ENDORSEMENT* source,
   4195                                    BYTE** buffer,
   4196                                    INT32* size) {
   4197   return uint32_t_Marshal(source, buffer, size);
   4198 }
   4199 
   4200 TPM_RC TPMI_RH_ENDORSEMENT_Unmarshal(TPMI_RH_ENDORSEMENT* target,
   4201                                      BYTE** buffer,
   4202                                      INT32* size,
   4203                                      BOOL allow_conditional_value) {
   4204   TPM_RC result;
   4205   BOOL has_valid_value = FALSE;
   4206   result = uint32_t_Unmarshal(target, buffer, size);
   4207   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4208     return result;
   4209   }
   4210   if (*target == TPM_RH_NULL) {
   4211     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4212   }
   4213   switch (*target) {
   4214     case TPM_RH_ENDORSEMENT:
   4215       has_valid_value = TRUE;
   4216       break;
   4217   }
   4218   if (!has_valid_value) {
   4219     return TPM_RC_VALUE;
   4220   }
   4221   return TPM_RC_SUCCESS;
   4222 }
   4223 
   4224 UINT16 TPMI_RH_HIERARCHY_Marshal(TPMI_RH_HIERARCHY* source,
   4225                                  BYTE** buffer,
   4226                                  INT32* size) {
   4227   return uint32_t_Marshal(source, buffer, size);
   4228 }
   4229 
   4230 TPM_RC TPMI_RH_HIERARCHY_Unmarshal(TPMI_RH_HIERARCHY* target,
   4231                                    BYTE** buffer,
   4232                                    INT32* size,
   4233                                    BOOL allow_conditional_value) {
   4234   TPM_RC result;
   4235   BOOL has_valid_value = FALSE;
   4236   result = uint32_t_Unmarshal(target, buffer, size);
   4237   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4238     return result;
   4239   }
   4240   if (*target == TPM_RH_NULL) {
   4241     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4242   }
   4243   switch (*target) {
   4244     case TPM_RH_OWNER:
   4245     case TPM_RH_PLATFORM:
   4246     case TPM_RH_ENDORSEMENT:
   4247       has_valid_value = TRUE;
   4248       break;
   4249   }
   4250   if (!has_valid_value) {
   4251     return TPM_RC_VALUE;
   4252   }
   4253   return TPM_RC_SUCCESS;
   4254 }
   4255 
   4256 UINT16 TPMI_RH_HIERARCHY_AUTH_Marshal(TPMI_RH_HIERARCHY_AUTH* source,
   4257                                       BYTE** buffer,
   4258                                       INT32* size) {
   4259   return uint32_t_Marshal(source, buffer, size);
   4260 }
   4261 
   4262 TPM_RC TPMI_RH_HIERARCHY_AUTH_Unmarshal(TPMI_RH_HIERARCHY_AUTH* target,
   4263                                         BYTE** buffer,
   4264                                         INT32* size) {
   4265   TPM_RC result;
   4266   BOOL has_valid_value = FALSE;
   4267   result = uint32_t_Unmarshal(target, buffer, size);
   4268   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4269     return result;
   4270   }
   4271   switch (*target) {
   4272     case TPM_RH_OWNER:
   4273     case TPM_RH_PLATFORM:
   4274     case TPM_RH_ENDORSEMENT:
   4275     case TPM_RH_LOCKOUT:
   4276       has_valid_value = TRUE;
   4277       break;
   4278   }
   4279   if (!has_valid_value) {
   4280     return TPM_RC_VALUE;
   4281   }
   4282   return TPM_RC_SUCCESS;
   4283 }
   4284 
   4285 UINT16 TPMI_RH_LOCKOUT_Marshal(TPMI_RH_LOCKOUT* source,
   4286                                BYTE** buffer,
   4287                                INT32* size) {
   4288   return uint32_t_Marshal(source, buffer, size);
   4289 }
   4290 
   4291 TPM_RC TPMI_RH_LOCKOUT_Unmarshal(TPMI_RH_LOCKOUT* target,
   4292                                  BYTE** buffer,
   4293                                  INT32* size) {
   4294   TPM_RC result;
   4295   BOOL has_valid_value = FALSE;
   4296   result = uint32_t_Unmarshal(target, buffer, size);
   4297   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4298     return result;
   4299   }
   4300   switch (*target) {
   4301     case TPM_RH_LOCKOUT:
   4302       has_valid_value = TRUE;
   4303       break;
   4304   }
   4305   if (!has_valid_value) {
   4306     return TPM_RC_VALUE;
   4307   }
   4308   return TPM_RC_SUCCESS;
   4309 }
   4310 
   4311 UINT16 TPMI_RH_NV_AUTH_Marshal(TPMI_RH_NV_AUTH* source,
   4312                                BYTE** buffer,
   4313                                INT32* size) {
   4314   return uint32_t_Marshal(source, buffer, size);
   4315 }
   4316 
   4317 TPM_RC TPMI_RH_NV_AUTH_Unmarshal(TPMI_RH_NV_AUTH* target,
   4318                                  BYTE** buffer,
   4319                                  INT32* size) {
   4320   TPM_RC result;
   4321   BOOL has_valid_value = FALSE;
   4322   result = uint32_t_Unmarshal(target, buffer, size);
   4323   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4324     return result;
   4325   }
   4326   switch (*target) {
   4327     case TPM_RH_PLATFORM:
   4328     case TPM_RH_OWNER:
   4329       has_valid_value = TRUE;
   4330       break;
   4331   }
   4332   if ((*target >= NV_INDEX_FIRST) && (*target <= NV_INDEX_LAST)) {
   4333     has_valid_value = TRUE;
   4334   }
   4335   if (!has_valid_value) {
   4336     return TPM_RC_VALUE;
   4337   }
   4338   return TPM_RC_SUCCESS;
   4339 }
   4340 
   4341 UINT16 TPMI_RH_OWNER_Marshal(TPMI_RH_OWNER* source,
   4342                              BYTE** buffer,
   4343                              INT32* size) {
   4344   return uint32_t_Marshal(source, buffer, size);
   4345 }
   4346 
   4347 TPM_RC TPMI_RH_OWNER_Unmarshal(TPMI_RH_OWNER* target,
   4348                                BYTE** buffer,
   4349                                INT32* size,
   4350                                BOOL allow_conditional_value) {
   4351   TPM_RC result;
   4352   BOOL has_valid_value = FALSE;
   4353   result = uint32_t_Unmarshal(target, buffer, size);
   4354   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4355     return result;
   4356   }
   4357   if (*target == TPM_RH_NULL) {
   4358     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4359   }
   4360   switch (*target) {
   4361     case TPM_RH_OWNER:
   4362       has_valid_value = TRUE;
   4363       break;
   4364   }
   4365   if (!has_valid_value) {
   4366     return TPM_RC_VALUE;
   4367   }
   4368   return TPM_RC_SUCCESS;
   4369 }
   4370 
   4371 UINT16 TPMI_RH_PLATFORM_Marshal(TPMI_RH_PLATFORM* source,
   4372                                 BYTE** buffer,
   4373                                 INT32* size) {
   4374   return uint32_t_Marshal(source, buffer, size);
   4375 }
   4376 
   4377 TPM_RC TPMI_RH_PLATFORM_Unmarshal(TPMI_RH_PLATFORM* target,
   4378                                   BYTE** buffer,
   4379                                   INT32* size) {
   4380   TPM_RC result;
   4381   BOOL has_valid_value = FALSE;
   4382   result = uint32_t_Unmarshal(target, buffer, size);
   4383   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4384     return result;
   4385   }
   4386   switch (*target) {
   4387     case TPM_RH_PLATFORM:
   4388       has_valid_value = TRUE;
   4389       break;
   4390   }
   4391   if (!has_valid_value) {
   4392     return TPM_RC_VALUE;
   4393   }
   4394   return TPM_RC_SUCCESS;
   4395 }
   4396 
   4397 UINT16 TPMI_RH_PROVISION_Marshal(TPMI_RH_PROVISION* source,
   4398                                  BYTE** buffer,
   4399                                  INT32* size) {
   4400   return uint32_t_Marshal(source, buffer, size);
   4401 }
   4402 
   4403 TPM_RC TPMI_RH_PROVISION_Unmarshal(TPMI_RH_PROVISION* target,
   4404                                    BYTE** buffer,
   4405                                    INT32* size) {
   4406   TPM_RC result;
   4407   BOOL has_valid_value = FALSE;
   4408   result = uint32_t_Unmarshal(target, buffer, size);
   4409   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4410     return result;
   4411   }
   4412   switch (*target) {
   4413     case TPM_RH_OWNER:
   4414     case TPM_RH_PLATFORM:
   4415       has_valid_value = TRUE;
   4416       break;
   4417   }
   4418   if (!has_valid_value) {
   4419     return TPM_RC_VALUE;
   4420   }
   4421   return TPM_RC_SUCCESS;
   4422 }
   4423 
   4424 UINT16 TPMI_SH_AUTH_SESSION_Marshal(TPMI_SH_AUTH_SESSION* source,
   4425                                     BYTE** buffer,
   4426                                     INT32* size) {
   4427   return uint32_t_Marshal(source, buffer, size);
   4428 }
   4429 
   4430 TPM_RC TPMI_SH_AUTH_SESSION_Unmarshal(TPMI_SH_AUTH_SESSION* target,
   4431                                       BYTE** buffer,
   4432                                       INT32* size,
   4433                                       BOOL allow_conditional_value) {
   4434   TPM_RC result;
   4435   BOOL has_valid_value = FALSE;
   4436   result = uint32_t_Unmarshal(target, buffer, size);
   4437   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4438     return result;
   4439   }
   4440   if (*target == TPM_RS_PW) {
   4441     return allow_conditional_value ? TPM_RC_SUCCESS : TPM_RC_VALUE;
   4442   }
   4443   if ((*target >= HMAC_SESSION_FIRST) && (*target <= HMAC_SESSION_LAST)) {
   4444     has_valid_value = TRUE;
   4445   }
   4446   if ((*target >= POLICY_SESSION_FIRST) && (*target <= POLICY_SESSION_LAST)) {
   4447     has_valid_value = TRUE;
   4448   }
   4449   if (!has_valid_value) {
   4450     return TPM_RC_VALUE;
   4451   }
   4452   return TPM_RC_SUCCESS;
   4453 }
   4454 
   4455 UINT16 TPMI_SH_HMAC_Marshal(TPMI_SH_HMAC* source, BYTE** buffer, INT32* size) {
   4456   return uint32_t_Marshal(source, buffer, size);
   4457 }
   4458 
   4459 TPM_RC TPMI_SH_HMAC_Unmarshal(TPMI_SH_HMAC* target,
   4460                               BYTE** buffer,
   4461                               INT32* size) {
   4462   TPM_RC result;
   4463   BOOL has_valid_value = FALSE;
   4464   result = uint32_t_Unmarshal(target, buffer, size);
   4465   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4466     return result;
   4467   }
   4468   if ((*target >= HMAC_SESSION_FIRST) && (*target <= HMAC_SESSION_LAST)) {
   4469     has_valid_value = TRUE;
   4470   }
   4471   if (!has_valid_value) {
   4472     return TPM_RC_VALUE;
   4473   }
   4474   return TPM_RC_SUCCESS;
   4475 }
   4476 
   4477 UINT16 TPMI_SH_POLICY_Marshal(TPMI_SH_POLICY* source,
   4478                               BYTE** buffer,
   4479                               INT32* size) {
   4480   return uint32_t_Marshal(source, buffer, size);
   4481 }
   4482 
   4483 TPM_RC TPMI_SH_POLICY_Unmarshal(TPMI_SH_POLICY* target,
   4484                                 BYTE** buffer,
   4485                                 INT32* size) {
   4486   TPM_RC result;
   4487   BOOL has_valid_value = FALSE;
   4488   result = uint32_t_Unmarshal(target, buffer, size);
   4489   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4490     return result;
   4491   }
   4492   if ((*target >= POLICY_SESSION_FIRST) && (*target <= POLICY_SESSION_LAST)) {
   4493     has_valid_value = TRUE;
   4494   }
   4495   if (!has_valid_value) {
   4496     return TPM_RC_VALUE;
   4497   }
   4498   return TPM_RC_SUCCESS;
   4499 }
   4500 
   4501 UINT16 TPMI_ST_ATTEST_Marshal(TPMI_ST_ATTEST* source,
   4502                               BYTE** buffer,
   4503                               INT32* size) {
   4504   return uint16_t_Marshal(source, buffer, size);
   4505 }
   4506 
   4507 TPM_RC TPMI_ST_ATTEST_Unmarshal(TPMI_ST_ATTEST* target,
   4508                                 BYTE** buffer,
   4509                                 INT32* size) {
   4510   TPM_RC result;
   4511   BOOL has_valid_value = FALSE;
   4512   result = uint16_t_Unmarshal(target, buffer, size);
   4513   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4514     return result;
   4515   }
   4516   switch (*target) {
   4517     case TPM_ST_ATTEST_CERTIFY:
   4518     case TPM_ST_ATTEST_QUOTE:
   4519     case TPM_ST_ATTEST_SESSION_AUDIT:
   4520     case TPM_ST_ATTEST_COMMAND_AUDIT:
   4521     case TPM_ST_ATTEST_TIME:
   4522     case TPM_ST_ATTEST_CREATION:
   4523     case TPM_ST_ATTEST_NV:
   4524       has_valid_value = TRUE;
   4525       break;
   4526   }
   4527   if (!has_valid_value) {
   4528     return TPM_RC_VALUE;
   4529   }
   4530   return TPM_RC_SUCCESS;
   4531 }
   4532 
   4533 UINT16 TPMI_ST_COMMAND_TAG_Marshal(TPMI_ST_COMMAND_TAG* source,
   4534                                    BYTE** buffer,
   4535                                    INT32* size) {
   4536   return uint16_t_Marshal(source, buffer, size);
   4537 }
   4538 
   4539 TPM_RC TPMI_ST_COMMAND_TAG_Unmarshal(TPMI_ST_COMMAND_TAG* target,
   4540                                      BYTE** buffer,
   4541                                      INT32* size) {
   4542   TPM_RC result;
   4543   BOOL has_valid_value = FALSE;
   4544   result = uint16_t_Unmarshal(target, buffer, size);
   4545   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4546     return result;
   4547   }
   4548   switch (*target) {
   4549     case TPM_ST_NO_SESSIONS:
   4550     case TPM_ST_SESSIONS:
   4551       has_valid_value = TRUE;
   4552       break;
   4553   }
   4554   if (!has_valid_value) {
   4555     return TPM_RC_BAD_TAG;
   4556   }
   4557   return TPM_RC_SUCCESS;
   4558 }
   4559 
   4560 UINT16 TPMI_YES_NO_Marshal(TPMI_YES_NO* source, BYTE** buffer, INT32* size) {
   4561   return uint8_t_Marshal(source, buffer, size);
   4562 }
   4563 
   4564 TPM_RC TPMI_YES_NO_Unmarshal(TPMI_YES_NO* target, BYTE** buffer, INT32* size) {
   4565   TPM_RC result;
   4566   BOOL has_valid_value = FALSE;
   4567   result = uint8_t_Unmarshal(target, buffer, size);
   4568   if ((result != TPM_RC_VALUE) && (result != TPM_RC_SUCCESS)) {
   4569     return result;
   4570   }
   4571   switch (*target) {
   4572     case NO:
   4573     case YES:
   4574       has_valid_value = TRUE;
   4575       break;
   4576   }
   4577   if (!has_valid_value) {
   4578     return TPM_RC_VALUE;
   4579   }
   4580   return TPM_RC_SUCCESS;
   4581 }
   4582 
   4583 UINT16 TPML_ALG_Marshal(TPML_ALG* source, BYTE** buffer, INT32* size) {
   4584   UINT16 total_size = 0;
   4585   INT32 i;
   4586   total_size += UINT32_Marshal(&source->count, buffer, size);
   4587   for (i = 0; i < source->count; ++i) {
   4588     total_size += TPM_ALG_ID_Marshal(&source->algorithms[i], buffer, size);
   4589   }
   4590   return total_size;
   4591 }
   4592 
   4593 TPM_RC TPML_ALG_Unmarshal(TPML_ALG* target, BYTE** buffer, INT32* size) {
   4594   TPM_RC result;
   4595   INT32 i;
   4596   result = UINT32_Unmarshal(&target->count, buffer, size);
   4597   if (result != TPM_RC_SUCCESS) {
   4598     return result;
   4599   }
   4600   if (target->count > MAX_ALG_LIST_SIZE) {
   4601     return TPM_RC_SIZE;
   4602   }
   4603   for (i = 0; i < target->count; ++i) {
   4604     result = TPM_ALG_ID_Unmarshal(&target->algorithms[i], buffer, size);
   4605     if (result != TPM_RC_SUCCESS) {
   4606       return result;
   4607     }
   4608   }
   4609   return TPM_RC_SUCCESS;
   4610 }
   4611 
   4612 UINT16 TPMS_ALG_PROPERTY_Marshal(TPMS_ALG_PROPERTY* source,
   4613                                  BYTE** buffer,
   4614                                  INT32* size) {
   4615   UINT16 total_size = 0;
   4616   total_size += TPM_ALG_ID_Marshal(&source->alg, buffer, size);
   4617   total_size += TPMA_ALGORITHM_Marshal(&source->algProperties, buffer, size);
   4618   return total_size;
   4619 }
   4620 
   4621 TPM_RC TPMS_ALG_PROPERTY_Unmarshal(TPMS_ALG_PROPERTY* target,
   4622                                    BYTE** buffer,
   4623                                    INT32* size) {
   4624   TPM_RC result;
   4625   result = TPM_ALG_ID_Unmarshal(&target->alg, buffer, size);
   4626   if (result != TPM_RC_SUCCESS) {
   4627     return result;
   4628   }
   4629   result = TPMA_ALGORITHM_Unmarshal(&target->algProperties, buffer, size);
   4630   if (result != TPM_RC_SUCCESS) {
   4631     return result;
   4632   }
   4633   return TPM_RC_SUCCESS;
   4634 }
   4635 
   4636 UINT16 TPML_ALG_PROPERTY_Marshal(TPML_ALG_PROPERTY* source,
   4637                                  BYTE** buffer,
   4638                                  INT32* size) {
   4639   UINT16 total_size = 0;
   4640   INT32 i;
   4641   total_size += UINT32_Marshal(&source->count, buffer, size);
   4642   for (i = 0; i < source->count; ++i) {
   4643     total_size +=
   4644         TPMS_ALG_PROPERTY_Marshal(&source->algProperties[i], buffer, size);
   4645   }
   4646   return total_size;
   4647 }
   4648 
   4649 TPM_RC TPML_ALG_PROPERTY_Unmarshal(TPML_ALG_PROPERTY* target,
   4650                                    BYTE** buffer,
   4651                                    INT32* size) {
   4652   TPM_RC result;
   4653   INT32 i;
   4654   result = UINT32_Unmarshal(&target->count, buffer, size);
   4655   if (result != TPM_RC_SUCCESS) {
   4656     return result;
   4657   }
   4658   if (target->count > MAX_CAP_ALGS) {
   4659     return TPM_RC_SIZE;
   4660   }
   4661   for (i = 0; i < target->count; ++i) {
   4662     result =
   4663         TPMS_ALG_PROPERTY_Unmarshal(&target->algProperties[i], buffer, size);
   4664     if (result != TPM_RC_SUCCESS) {
   4665       return result;
   4666     }
   4667   }
   4668   return TPM_RC_SUCCESS;
   4669 }
   4670 
   4671 UINT16 TPM_CC_Marshal(TPM_CC* source, BYTE** buffer, INT32* size) {
   4672   return uint32_t_Marshal(source, buffer, size);
   4673 }
   4674 
   4675 TPM_RC TPM_CC_Unmarshal(TPM_CC* target, BYTE** buffer, INT32* size) {
   4676   TPM_RC result;
   4677   result = uint32_t_Unmarshal(target, buffer, size);
   4678   if (result != TPM_RC_SUCCESS) {
   4679     return result;
   4680   }
   4681 #ifdef TPM_CC_FIRST
   4682   if (*target == TPM_CC_FIRST) {
   4683     return TPM_RC_SUCCESS;
   4684   }
   4685 #endif
   4686 #ifdef TPM_CC_PP_FIRST
   4687   if (*target == TPM_CC_PP_FIRST) {
   4688     return TPM_RC_SUCCESS;
   4689   }
   4690 #endif
   4691 #ifdef TPM_CC_NV_UndefineSpaceSpecial
   4692   if (*target == TPM_CC_NV_UndefineSpaceSpecial) {
   4693     return TPM_RC_SUCCESS;
   4694   }
   4695 #endif
   4696 #ifdef TPM_CC_EvictControl
   4697   if (*target == TPM_CC_EvictControl) {
   4698     return TPM_RC_SUCCESS;
   4699   }
   4700 #endif
   4701 #ifdef TPM_CC_HierarchyControl
   4702   if (*target == TPM_CC_HierarchyControl) {
   4703     return TPM_RC_SUCCESS;
   4704   }
   4705 #endif
   4706 #ifdef TPM_CC_NV_UndefineSpace
   4707   if (*target == TPM_CC_NV_UndefineSpace) {
   4708     return TPM_RC_SUCCESS;
   4709   }
   4710 #endif
   4711 #ifdef TPM_CC_ChangeEPS
   4712   if (*target == TPM_CC_ChangeEPS) {
   4713     return TPM_RC_SUCCESS;
   4714   }
   4715 #endif
   4716 #ifdef TPM_CC_ChangePPS
   4717   if (*target == TPM_CC_ChangePPS) {
   4718     return TPM_RC_SUCCESS;
   4719   }
   4720 #endif
   4721 #ifdef TPM_CC_Clear
   4722   if (*target == TPM_CC_Clear) {
   4723     return TPM_RC_SUCCESS;
   4724   }
   4725 #endif
   4726 #ifdef TPM_CC_ClearControl
   4727   if (*target == TPM_CC_ClearControl) {
   4728     return TPM_RC_SUCCESS;
   4729   }
   4730 #endif
   4731 #ifdef TPM_CC_ClockSet
   4732   if (*target == TPM_CC_ClockSet) {
   4733     return TPM_RC_SUCCESS;
   4734   }
   4735 #endif
   4736 #ifdef TPM_CC_HierarchyChangeAuth
   4737   if (*target == TPM_CC_HierarchyChangeAuth) {
   4738     return TPM_RC_SUCCESS;
   4739   }
   4740 #endif
   4741 #ifdef TPM_CC_NV_DefineSpace
   4742   if (*target == TPM_CC_NV_DefineSpace) {
   4743     return TPM_RC_SUCCESS;
   4744   }
   4745 #endif
   4746 #ifdef TPM_CC_PCR_Allocate
   4747   if (*target == TPM_CC_PCR_Allocate) {
   4748     return TPM_RC_SUCCESS;
   4749   }
   4750 #endif
   4751 #ifdef TPM_CC_PCR_SetAuthPolicy
   4752   if (*target == TPM_CC_PCR_SetAuthPolicy) {
   4753     return TPM_RC_SUCCESS;
   4754   }
   4755 #endif
   4756 #ifdef TPM_CC_PP_Commands
   4757   if (*target == TPM_CC_PP_Commands) {
   4758     return TPM_RC_SUCCESS;
   4759   }
   4760 #endif
   4761 #ifdef TPM_CC_SetPrimaryPolicy
   4762   if (*target == TPM_CC_SetPrimaryPolicy) {
   4763     return TPM_RC_SUCCESS;
   4764   }
   4765 #endif
   4766 #ifdef TPM_CC_FieldUpgradeStart
   4767   if (*target == TPM_CC_FieldUpgradeStart) {
   4768     return TPM_RC_SUCCESS;
   4769   }
   4770 #endif
   4771 #ifdef TPM_CC_ClockRateAdjust
   4772   if (*target == TPM_CC_ClockRateAdjust) {
   4773     return TPM_RC_SUCCESS;
   4774   }
   4775 #endif
   4776 #ifdef TPM_CC_CreatePrimary
   4777   if (*target == TPM_CC_CreatePrimary) {
   4778     return TPM_RC_SUCCESS;
   4779   }
   4780 #endif
   4781 #ifdef TPM_CC_NV_GlobalWriteLock
   4782   if (*target == TPM_CC_NV_GlobalWriteLock) {
   4783     return TPM_RC_SUCCESS;
   4784   }
   4785 #endif
   4786 #ifdef TPM_CC_PP_LAST
   4787   if (*target == TPM_CC_PP_LAST) {
   4788     return TPM_RC_SUCCESS;
   4789   }
   4790 #endif
   4791 #ifdef TPM_CC_GetCommandAuditDigest
   4792   if (*target == TPM_CC_GetCommandAuditDigest) {
   4793     return TPM_RC_SUCCESS;
   4794   }
   4795 #endif
   4796 #ifdef TPM_CC_NV_Increment
   4797   if (*target == TPM_CC_NV_Increment) {
   4798     return TPM_RC_SUCCESS;
   4799   }
   4800 #endif
   4801 #ifdef TPM_CC_NV_SetBits
   4802   if (*target == TPM_CC_NV_SetBits) {
   4803     return TPM_RC_SUCCESS;
   4804   }
   4805 #endif
   4806 #ifdef TPM_CC_NV_Extend
   4807   if (*target == TPM_CC_NV_Extend) {
   4808     return TPM_RC_SUCCESS;
   4809   }
   4810 #endif
   4811 #ifdef TPM_CC_NV_Write
   4812   if (*target == TPM_CC_NV_Write) {
   4813     return TPM_RC_SUCCESS;
   4814   }
   4815 #endif
   4816 #ifdef TPM_CC_NV_WriteLock
   4817   if (*target == TPM_CC_NV_WriteLock) {
   4818     return TPM_RC_SUCCESS;
   4819   }
   4820 #endif
   4821 #ifdef TPM_CC_DictionaryAttackLockReset
   4822   if (*target == TPM_CC_DictionaryAttackLockReset) {
   4823     return TPM_RC_SUCCESS;
   4824   }
   4825 #endif
   4826 #ifdef TPM_CC_DictionaryAttackParameters
   4827   if (*target == TPM_CC_DictionaryAttackParameters) {
   4828     return TPM_RC_SUCCESS;
   4829   }
   4830 #endif
   4831 #ifdef TPM_CC_NV_ChangeAuth
   4832   if (*target == TPM_CC_NV_ChangeAuth) {
   4833     return TPM_RC_SUCCESS;
   4834   }
   4835 #endif
   4836 #ifdef TPM_CC_PCR_Event
   4837   if (*target == TPM_CC_PCR_Event) {
   4838     return TPM_RC_SUCCESS;
   4839   }
   4840 #endif
   4841 #ifdef TPM_CC_PCR_Reset
   4842   if (*target == TPM_CC_PCR_Reset) {
   4843     return TPM_RC_SUCCESS;
   4844   }
   4845 #endif
   4846 #ifdef TPM_CC_SequenceComplete
   4847   if (*target == TPM_CC_SequenceComplete) {
   4848     return TPM_RC_SUCCESS;
   4849   }
   4850 #endif
   4851 #ifdef TPM_CC_SetAlgorithmSet
   4852   if (*target == TPM_CC_SetAlgorithmSet) {
   4853     return TPM_RC_SUCCESS;
   4854   }
   4855 #endif
   4856 #ifdef TPM_CC_SetCommandCodeAuditStatus
   4857   if (*target == TPM_CC_SetCommandCodeAuditStatus) {
   4858     return TPM_RC_SUCCESS;
   4859   }
   4860 #endif
   4861 #ifdef TPM_CC_FieldUpgradeData
   4862   if (*target == TPM_CC_FieldUpgradeData) {
   4863     return TPM_RC_SUCCESS;
   4864   }
   4865 #endif
   4866 #ifdef TPM_CC_IncrementalSelfTest
   4867   if (*target == TPM_CC_IncrementalSelfTest) {
   4868     return TPM_RC_SUCCESS;
   4869   }
   4870 #endif
   4871 #ifdef TPM_CC_SelfTest
   4872   if (*target == TPM_CC_SelfTest) {
   4873     return TPM_RC_SUCCESS;
   4874   }
   4875 #endif
   4876 #ifdef TPM_CC_Startup
   4877   if (*target == TPM_CC_Startup) {
   4878     return TPM_RC_SUCCESS;
   4879   }
   4880 #endif
   4881 #ifdef TPM_CC_Shutdown
   4882   if (*target == TPM_CC_Shutdown) {
   4883     return TPM_RC_SUCCESS;
   4884   }
   4885 #endif
   4886 #ifdef TPM_CC_StirRandom
   4887   if (*target == TPM_CC_StirRandom) {
   4888     return TPM_RC_SUCCESS;
   4889   }
   4890 #endif
   4891 #ifdef TPM_CC_ActivateCredential
   4892   if (*target == TPM_CC_ActivateCredential) {
   4893     return TPM_RC_SUCCESS;
   4894   }
   4895 #endif
   4896 #ifdef TPM_CC_Certify
   4897   if (*target == TPM_CC_Certify) {
   4898     return TPM_RC_SUCCESS;
   4899   }
   4900 #endif
   4901 #ifdef TPM_CC_PolicyNV
   4902   if (*target == TPM_CC_PolicyNV) {
   4903     return TPM_RC_SUCCESS;
   4904   }
   4905 #endif
   4906 #ifdef TPM_CC_CertifyCreation
   4907   if (*target == TPM_CC_CertifyCreation) {
   4908     return TPM_RC_SUCCESS;
   4909   }
   4910 #endif
   4911 #ifdef TPM_CC_Duplicate
   4912   if (*target == TPM_CC_Duplicate) {
   4913     return TPM_RC_SUCCESS;
   4914   }
   4915 #endif
   4916 #ifdef TPM_CC_GetTime
   4917   if (*target == TPM_CC_GetTime) {
   4918     return TPM_RC_SUCCESS;
   4919   }
   4920 #endif
   4921 #ifdef TPM_CC_GetSessionAuditDigest
   4922   if (*target == TPM_CC_GetSessionAuditDigest) {
   4923     return TPM_RC_SUCCESS;
   4924   }
   4925 #endif
   4926 #ifdef TPM_CC_NV_Read
   4927   if (*target == TPM_CC_NV_Read) {
   4928     return TPM_RC_SUCCESS;
   4929   }
   4930 #endif
   4931 #ifdef TPM_CC_NV_ReadLock
   4932   if (*target == TPM_CC_NV_ReadLock) {
   4933     return TPM_RC_SUCCESS;
   4934   }
   4935 #endif
   4936 #ifdef TPM_CC_ObjectChangeAuth
   4937   if (*target == TPM_CC_ObjectChangeAuth) {
   4938     return TPM_RC_SUCCESS;
   4939   }
   4940 #endif
   4941 #ifdef TPM_CC_PolicySecret
   4942   if (*target == TPM_CC_PolicySecret) {
   4943     return TPM_RC_SUCCESS;
   4944   }
   4945 #endif
   4946 #ifdef TPM_CC_Rewrap
   4947   if (*target == TPM_CC_Rewrap) {
   4948     return TPM_RC_SUCCESS;
   4949   }
   4950 #endif
   4951 #ifdef TPM_CC_Create
   4952   if (*target == TPM_CC_Create) {
   4953     return TPM_RC_SUCCESS;
   4954   }
   4955 #endif
   4956 #ifdef TPM_CC_ECDH_ZGen
   4957   if (*target == TPM_CC_ECDH_ZGen) {
   4958     return TPM_RC_SUCCESS;
   4959   }
   4960 #endif
   4961 #ifdef TPM_CC_HMAC
   4962   if (*target == TPM_CC_HMAC) {
   4963     return TPM_RC_SUCCESS;
   4964   }
   4965 #endif
   4966 #ifdef TPM_CC_Import
   4967   if (*target == TPM_CC_Import) {
   4968     return TPM_RC_SUCCESS;
   4969   }
   4970 #endif
   4971 #ifdef TPM_CC_Load
   4972   if (*target == TPM_CC_Load) {
   4973     return TPM_RC_SUCCESS;
   4974   }
   4975 #endif
   4976 #ifdef TPM_CC_Quote
   4977   if (*target == TPM_CC_Quote) {
   4978     return TPM_RC_SUCCESS;
   4979   }
   4980 #endif
   4981 #ifdef TPM_CC_RSA_Decrypt
   4982   if (*target == TPM_CC_RSA_Decrypt) {
   4983     return TPM_RC_SUCCESS;
   4984   }
   4985 #endif
   4986 #ifdef TPM_CC_HMAC_Start
   4987   if (*target == TPM_CC_HMAC_Start) {
   4988     return TPM_RC_SUCCESS;
   4989   }
   4990 #endif
   4991 #ifdef TPM_CC_SequenceUpdate
   4992   if (*target == TPM_CC_SequenceUpdate) {
   4993     return TPM_RC_SUCCESS;
   4994   }
   4995 #endif
   4996 #ifdef TPM_CC_Sign
   4997   if (*target == TPM_CC_Sign) {
   4998     return TPM_RC_SUCCESS;
   4999   }
   5000 #endif
   5001 #ifdef TPM_CC_Unseal
   5002   if (*target == TPM_CC_Unseal) {
   5003     return TPM_RC_SUCCESS;
   5004   }
   5005 #endif
   5006 #ifdef TPM_CC_PolicySigned
   5007   if (*target == TPM_CC_PolicySigned) {
   5008     return TPM_RC_SUCCESS;
   5009   }
   5010 #endif
   5011 #ifdef TPM_CC_ContextLoad
   5012   if (*target == TPM_CC_ContextLoad) {
   5013     return TPM_RC_SUCCESS;
   5014   }
   5015 #endif
   5016 #ifdef TPM_CC_ContextSave
   5017   if (*target == TPM_CC_ContextSave) {
   5018     return TPM_RC_SUCCESS;
   5019   }
   5020 #endif
   5021 #ifdef TPM_CC_ECDH_KeyGen
   5022   if (*target == TPM_CC_ECDH_KeyGen) {
   5023     return TPM_RC_SUCCESS;
   5024   }
   5025 #endif
   5026 #ifdef TPM_CC_EncryptDecrypt
   5027   if (*target == TPM_CC_EncryptDecrypt) {
   5028     return TPM_RC_SUCCESS;
   5029   }
   5030 #endif
   5031 #ifdef TPM_CC_FlushContext
   5032   if (*target == TPM_CC_FlushContext) {
   5033     return TPM_RC_SUCCESS;
   5034   }
   5035 #endif
   5036 #ifdef TPM_CC_LoadExternal
   5037   if (*target == TPM_CC_LoadExternal) {
   5038     return TPM_RC_SUCCESS;
   5039   }
   5040 #endif
   5041 #ifdef TPM_CC_MakeCredential
   5042   if (*target == TPM_CC_MakeCredential) {
   5043     return TPM_RC_SUCCESS;
   5044   }
   5045 #endif
   5046 #ifdef TPM_CC_NV_ReadPublic
   5047   if (*target == TPM_CC_NV_ReadPublic) {
   5048     return TPM_RC_SUCCESS;
   5049   }
   5050 #endif
   5051 #ifdef TPM_CC_PolicyAuthorize
   5052   if (*target == TPM_CC_PolicyAuthorize) {
   5053     return TPM_RC_SUCCESS;
   5054   }
   5055 #endif
   5056 #ifdef TPM_CC_PolicyAuthValue
   5057   if (*target == TPM_CC_PolicyAuthValue) {
   5058     return TPM_RC_SUCCESS;
   5059   }
   5060 #endif
   5061 #ifdef TPM_CC_PolicyCommandCode
   5062   if (*target == TPM_CC_PolicyCommandCode) {
   5063     return TPM_RC_SUCCESS;
   5064   }
   5065 #endif
   5066 #ifdef TPM_CC_PolicyCounterTimer
   5067   if (*target == TPM_CC_PolicyCounterTimer) {
   5068     return TPM_RC_SUCCESS;
   5069   }
   5070 #endif
   5071 #ifdef TPM_CC_PolicyCpHash
   5072   if (*target == TPM_CC_PolicyCpHash) {
   5073     return TPM_RC_SUCCESS;
   5074   }
   5075 #endif
   5076 #ifdef TPM_CC_PolicyLocality
   5077   if (*target == TPM_CC_PolicyLocality) {
   5078     return TPM_RC_SUCCESS;
   5079   }
   5080 #endif
   5081 #ifdef TPM_CC_PolicyNameHash
   5082   if (*target == TPM_CC_PolicyNameHash) {
   5083     return TPM_RC_SUCCESS;
   5084   }
   5085 #endif
   5086 #ifdef TPM_CC_PolicyOR
   5087   if (*target == TPM_CC_PolicyOR) {
   5088     return TPM_RC_SUCCESS;
   5089   }
   5090 #endif
   5091 #ifdef TPM_CC_PolicyTicket
   5092   if (*target == TPM_CC_PolicyTicket) {
   5093     return TPM_RC_SUCCESS;
   5094   }
   5095 #endif
   5096 #ifdef TPM_CC_ReadPublic
   5097   if (*target == TPM_CC_ReadPublic) {
   5098     return TPM_RC_SUCCESS;
   5099   }
   5100 #endif
   5101 #ifdef TPM_CC_RSA_Encrypt
   5102   if (*target == TPM_CC_RSA_Encrypt) {
   5103     return TPM_RC_SUCCESS;
   5104   }
   5105 #endif
   5106 #ifdef TPM_CC_StartAuthSession
   5107   if (*target == TPM_CC_StartAuthSession) {
   5108     return TPM_RC_SUCCESS;
   5109   }
   5110 #endif
   5111 #ifdef TPM_CC_VerifySignature
   5112   if (*target == TPM_CC_VerifySignature) {
   5113     return TPM_RC_SUCCESS;
   5114   }
   5115 #endif
   5116 #ifdef TPM_CC_ECC_Parameters
   5117   if (*target == TPM_CC_ECC_Parameters) {
   5118     return TPM_RC_SUCCESS;
   5119   }
   5120 #endif
   5121 #ifdef TPM_CC_FirmwareRead
   5122   if (*target == TPM_CC_FirmwareRead) {
   5123     return TPM_RC_SUCCESS;
   5124   }
   5125 #endif
   5126 #ifdef TPM_CC_GetCapability
   5127   if (*target == TPM_CC_GetCapability) {
   5128     return TPM_RC_SUCCESS;
   5129   }
   5130 #endif
   5131 #ifdef TPM_CC_GetRandom
   5132   if (*target == TPM_CC_GetRandom) {
   5133     return TPM_RC_SUCCESS;
   5134   }
   5135 #endif
   5136 #ifdef TPM_CC_GetTestResult
   5137   if (*target == TPM_CC_GetTestResult) {
   5138     return TPM_RC_SUCCESS;
   5139   }
   5140 #endif
   5141 #ifdef TPM_CC_Hash
   5142   if (*target == TPM_CC_Hash) {
   5143     return TPM_RC_SUCCESS;
   5144   }
   5145 #endif
   5146 #ifdef TPM_CC_PCR_Read
   5147   if (*target == TPM_CC_PCR_Read) {
   5148     return TPM_RC_SUCCESS;
   5149   }
   5150 #endif
   5151 #ifdef TPM_CC_PolicyPCR
   5152   if (*target == TPM_CC_PolicyPCR) {
   5153     return TPM_RC_SUCCESS;
   5154   }
   5155 #endif
   5156 #ifdef TPM_CC_PolicyRestart
   5157   if (*target == TPM_CC_PolicyRestart) {
   5158     return TPM_RC_SUCCESS;
   5159   }
   5160 #endif
   5161 #ifdef TPM_CC_ReadClock
   5162   if (*target == TPM_CC_ReadClock) {
   5163     return TPM_RC_SUCCESS;
   5164   }
   5165 #endif
   5166 #ifdef TPM_CC_PCR_Extend
   5167   if (*target == TPM_CC_PCR_Extend) {
   5168     return TPM_RC_SUCCESS;
   5169   }
   5170 #endif
   5171 #ifdef TPM_CC_PCR_SetAuthValue
   5172   if (*target == TPM_CC_PCR_SetAuthValue) {
   5173     return TPM_RC_SUCCESS;
   5174   }
   5175 #endif
   5176 #ifdef TPM_CC_NV_Certify
   5177   if (*target == TPM_CC_NV_Certify) {
   5178     return TPM_RC_SUCCESS;
   5179   }
   5180 #endif
   5181 #ifdef TPM_CC_EventSequenceComplete
   5182   if (*target == TPM_CC_EventSequenceComplete) {
   5183     return TPM_RC_SUCCESS;
   5184   }
   5185 #endif
   5186 #ifdef TPM_CC_HashSequenceStart
   5187   if (*target == TPM_CC_HashSequenceStart) {
   5188     return TPM_RC_SUCCESS;
   5189   }
   5190 #endif
   5191 #ifdef TPM_CC_PolicyPhysicalPresence
   5192   if (*target == TPM_CC_PolicyPhysicalPresence) {
   5193     return TPM_RC_SUCCESS;
   5194   }
   5195 #endif
   5196 #ifdef TPM_CC_PolicyDuplicationSelect
   5197   if (*target == TPM_CC_PolicyDuplicationSelect) {
   5198     return TPM_RC_SUCCESS;
   5199   }
   5200 #endif
   5201 #ifdef TPM_CC_PolicyGetDigest
   5202   if (*target == TPM_CC_PolicyGetDigest) {
   5203     return TPM_RC_SUCCESS;
   5204   }
   5205 #endif
   5206 #ifdef TPM_CC_TestParms
   5207   if (*target == TPM_CC_TestParms) {
   5208     return TPM_RC_SUCCESS;
   5209   }
   5210 #endif
   5211 #ifdef TPM_CC_Commit
   5212   if (*target == TPM_CC_Commit) {
   5213     return TPM_RC_SUCCESS;
   5214   }
   5215 #endif
   5216 #ifdef TPM_CC_PolicyPassword
   5217   if (*target == TPM_CC_PolicyPassword) {
   5218     return TPM_RC_SUCCESS;
   5219   }
   5220 #endif
   5221 #ifdef TPM_CC_ZGen_2Phase
   5222   if (*target == TPM_CC_ZGen_2Phase) {
   5223     return TPM_RC_SUCCESS;
   5224   }
   5225 #endif
   5226 #ifdef TPM_CC_EC_Ephemeral
   5227   if (*target == TPM_CC_EC_Ephemeral) {
   5228     return TPM_RC_SUCCESS;
   5229   }
   5230 #endif
   5231 #ifdef TPM_CC_PolicyNvWritten
   5232   if (*target == TPM_CC_PolicyNvWritten) {
   5233     return TPM_RC_SUCCESS;
   5234   }
   5235 #endif
   5236 #ifdef TPM_CC_LAST
   5237   if (*target == TPM_CC_LAST) {
   5238     return TPM_RC_SUCCESS;
   5239   }
   5240 #endif
   5241   return TPM_RC_COMMAND_CODE;
   5242 }
   5243 
   5244 UINT16 TPML_CC_Marshal(TPML_CC* source, BYTE** buffer, INT32* size) {
   5245   UINT16 total_size = 0;
   5246   INT32 i;
   5247   total_size += UINT32_Marshal(&source->count, buffer, size);
   5248   for (i = 0; i < source->count; ++i) {
   5249     total_size += TPM_CC_Marshal(&source->commandCodes[i], buffer, size);
   5250   }
   5251   return total_size;
   5252 }
   5253 
   5254 TPM_RC TPML_CC_Unmarshal(TPML_CC* target, BYTE** buffer, INT32* size) {
   5255   TPM_RC result;
   5256   INT32 i;
   5257   result = UINT32_Unmarshal(&target->count, buffer, size);
   5258   if (result != TPM_RC_SUCCESS) {
   5259     return result;
   5260   }
   5261   if (target->count > MAX_CAP_CC) {
   5262     return TPM_RC_SIZE;
   5263   }
   5264   for (i = 0; i < target->count; ++i) {
   5265     result = TPM_CC_Unmarshal(&target->commandCodes[i], buffer, size);
   5266     if (result != TPM_RC_SUCCESS) {
   5267       return result;
   5268     }
   5269   }
   5270   return TPM_RC_SUCCESS;
   5271 }
   5272 
   5273 UINT16 TPML_CCA_Marshal(TPML_CCA* source, BYTE** buffer, INT32* size) {
   5274   UINT16 total_size = 0;
   5275   INT32 i;
   5276   total_size += UINT32_Marshal(&source->count, buffer, size);
   5277   for (i = 0; i < source->count; ++i) {
   5278     total_size += TPMA_CC_Marshal(&source->commandAttributes[i], buffer, size);
   5279   }
   5280   return total_size;
   5281 }
   5282 
   5283 TPM_RC TPML_CCA_Unmarshal(TPML_CCA* target, BYTE** buffer, INT32* size) {
   5284   TPM_RC result;
   5285   INT32 i;
   5286   result = UINT32_Unmarshal(&target->count, buffer, size);
   5287   if (result != TPM_RC_SUCCESS) {
   5288     return result;
   5289   }
   5290   if (target->count > MAX_CAP_CC) {
   5291     return TPM_RC_SIZE;
   5292   }
   5293   for (i = 0; i < target->count; ++i) {
   5294     result = TPMA_CC_Unmarshal(&target->commandAttributes[i], buffer, size);
   5295     if (result != TPM_RC_SUCCESS) {
   5296       return result;
   5297     }
   5298   }
   5299   return TPM_RC_SUCCESS;
   5300 }
   5301 
   5302 UINT16 TPML_DIGEST_Marshal(TPML_DIGEST* source, BYTE** buffer, INT32* size) {
   5303   UINT16 total_size = 0;
   5304   INT32 i;
   5305   total_size += UINT32_Marshal(&source->count, buffer, size);
   5306   for (i = 0; i < source->count; ++i) {
   5307     total_size += TPM2B_DIGEST_Marshal(&source->digests[i], buffer, size);
   5308   }
   5309   return total_size;
   5310 }
   5311 
   5312 TPM_RC TPML_DIGEST_Unmarshal(TPML_DIGEST* target, BYTE** buffer, INT32* size) {
   5313   TPM_RC result;
   5314   INT32 i;
   5315   result = UINT32_Unmarshal(&target->count, buffer, size);
   5316   if (result != TPM_RC_SUCCESS) {
   5317     return result;
   5318   }
   5319   if (target->count > 8) {
   5320     return TPM_RC_SIZE;
   5321   }
   5322   if (target->count < 2) {
   5323     return TPM_RC_SIZE;
   5324   }
   5325   for (i = 0; i < target->count; ++i) {
   5326     result = TPM2B_DIGEST_Unmarshal(&target->digests[i], buffer, size);
   5327     if (result != TPM_RC_SUCCESS) {
   5328       return result;
   5329     }
   5330   }
   5331   return TPM_RC_SUCCESS;
   5332 }
   5333 
   5334 UINT16 TPMU_HA_Marshal(TPMU_HA* source,
   5335                        BYTE** buffer,
   5336                        INT32* size,
   5337                        UINT32 selector) {
   5338   INT32 i;
   5339   UINT16 total_size = 0;
   5340   switch (selector) {
   5341 #ifdef TPM_ALG_SHA
   5342     case TPM_ALG_SHA:
   5343       for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
   5344         total_size += BYTE_Marshal(&source->sha[i], buffer, size);
   5345       }
   5346       return total_size;
   5347 #endif
   5348 #ifdef TPM_ALG_SHA1
   5349     case TPM_ALG_SHA1:
   5350       for (i = 0; i < SHA1_DIGEST_SIZE; ++i) {
   5351         total_size += BYTE_Marshal(&source->sha1[i], buffer, size);
   5352       }
   5353       return total_size;
   5354 #endif
   5355 #ifdef TPM_ALG_SHA256
   5356     case TPM_ALG_SHA256:
   5357       for (i = 0; i < SHA256_DIGEST_SIZE; ++i) {
   5358         total_size += BYTE_Marshal(&source->sha256[i], buffer, size);
   5359       }
   5360       return total_size;
   5361 #endif
   5362 #ifdef TPM_ALG_SHA384
   5363     case TPM_ALG_SHA384:
   5364       for (i = 0; i < SHA384_DIGEST_SIZE; ++i) {
   5365         total_size += BYTE_Marshal(&source->sha384[i], buffer, size);
   5366       }
   5367       return total_size;
   5368 #endif
   5369 #ifdef TPM_ALG_SHA512
   5370     case TPM_ALG_SHA512:
   5371       for (i = 0; i < SHA512_DIGEST_SIZE; ++i) {
   5372         total_size += BYTE_Marshal(&source->sha512[i], buffer, size);
   5373       }
   5374       return total_size;
   5375 #endif
   5376 #ifdef TPM_ALG_SM3_256
   5377     case TPM_ALG_SM3_256:
   5378       for (i = 0; i < SM3_256_DIGEST_SIZE; ++i) {
   5379         total_size += BYTE_Marshal(&source->sm3_256[i], buffer, size);
   5380       }
   5381       return total_size;
   5382 #endif
   5383 #ifdef TPM_ALG_NULL
   5384     case TPM_ALG_NULL:
   5385       return 0;
   5386 #endif
   5387   }
   5388   return 0;
   5389 }
   5390 
   5391 TPM_RC TPMU_HA_Unmarshal(TPMU_HA* target,
   5392                          BYTE** buffer,
   5393                          INT32* size,
   5394                          UINT32 selector) {
   5395   switch (selector) {
   5396     INT32 i;
   5397     TPM_RC result = TPM_RC_SUCCESS;
   5398 #ifdef TPM_ALG_SHA
   5399     case TPM_ALG_SHA:
   5400       for (i = 0; i < SHA_DIGEST_SIZE; ++i) {
   5401         result = BYTE_Unmarshal(&target->sha[i], buffer, size);
   5402         if (result != TPM_RC_SUCCESS) {
   5403           return result;
   5404         }
   5405       }
   5406       return TPM_RC_SUCCESS;
   5407 #endif
   5408 #ifdef TPM_ALG_SHA1
   5409     case TPM_ALG_SHA1:
   5410       for (i = 0; i < SHA1_DIGEST_SIZE; ++i) {
   5411         result = BYTE_Unmarshal(&target->sha1[i], buffer, size);
   5412         if (result != TPM_RC_SUCCESS) {
   5413           return result;
   5414         }
   5415       }
   5416       return TPM_RC_SUCCESS;
   5417 #endif
   5418 #ifdef TPM_ALG_SHA256
   5419     case TPM_ALG_SHA256:
   5420       for (i = 0; i < SHA256_DIGEST_SIZE; ++i) {
   5421         result = BYTE_Unmarshal(&target->sha256[i], buffer, size);
   5422         if (result != TPM_RC_SUCCESS) {
   5423           return result;
   5424         }
   5425       }
   5426       return TPM_RC_SUCCESS;
   5427 #endif
   5428 #ifdef TPM_ALG_SHA384
   5429     case TPM_ALG_SHA384:
   5430       for (i = 0; i < SHA384_DIGEST_SIZE; ++i) {
   5431         result = BYTE_Unmarshal(&target->sha384[i], buffer, size);
   5432         if (result != TPM_RC_SUCCESS) {
   5433           return result;
   5434         }
   5435       }
   5436       return TPM_RC_SUCCESS;
   5437 #endif
   5438 #ifdef TPM_ALG_SHA512
   5439     case TPM_ALG_SHA512:
   5440       for (i = 0; i < SHA512_DIGEST_SIZE; ++i) {
   5441         result = BYTE_Unmarshal(&target->sha512[i], buffer, size);
   5442         if (result != TPM_RC_SUCCESS) {
   5443           return result;
   5444         }
   5445       }
   5446       return TPM_RC_SUCCESS;
   5447 #endif
   5448 #ifdef TPM_ALG_SM3_256
   5449     case TPM_ALG_SM3_256:
   5450       for (i = 0; i < SM3_256_DIGEST_SIZE; ++i) {
   5451         result = BYTE_Unmarshal(&target->sm3_256[i], buffer, size);
   5452         if (result != TPM_RC_SUCCESS) {
   5453           return result;
   5454         }
   5455       }
   5456       return TPM_RC_SUCCESS;
   5457 #endif
   5458 #ifdef TPM_ALG_NULL
   5459     case TPM_ALG_NULL:
   5460       return TPM_RC_SUCCESS;
   5461 #endif
   5462   }
   5463   return TPM_RC_SELECTOR;
   5464 }
   5465 
   5466 UINT16 TPMT_HA_Marshal(TPMT_HA* source, BYTE** buffer, INT32* size) {
   5467   UINT16 total_size = 0;
   5468   total_size += TPMI_ALG_HASH_Marshal(&source->hashAlg, buffer, size);
   5469   total_size += TPMU_HA_Marshal(&source->digest, buffer, size, source->hashAlg);
   5470   return total_size;
   5471 }
   5472 
   5473 TPM_RC TPMT_HA_Unmarshal(TPMT_HA* target, BYTE** buffer, INT32* size) {
   5474   TPM_RC result;
   5475   result = TPMI_ALG_HASH_Unmarshal(&target->hashAlg, buffer, size, TRUE);
   5476   if (result != TPM_RC_SUCCESS) {
   5477     return result;
   5478   }
   5479   result = TPMU_HA_Unmarshal(&target->digest, buffer, size, target->hashAlg);
   5480   if (result != TPM_RC_SUCCESS) {
   5481     return result;
   5482   }
   5483   return TPM_RC_SUCCESS;
   5484 }
   5485 
   5486 UINT16 TPML_DIGEST_VALUES_Marshal(TPML_DIGEST_VALUES* source,
   5487                                   BYTE** buffer,
   5488                                   INT32* size) {
   5489   UINT16 total_size = 0;
   5490   INT32 i;
   5491   total_size += UINT32_Marshal(&source->count, buffer, size);
   5492   for (i = 0; i < source->count; ++i) {
   5493     total_size += TPMT_HA_Marshal(&source->digests[i], buffer, size);
   5494   }
   5495   return total_size;
   5496 }
   5497 
   5498 TPM_RC TPML_DIGEST_VALUES_Unmarshal(TPML_DIGEST_VALUES* target,
   5499                                     BYTE** buffer,
   5500                                     INT32* size) {
   5501   TPM_RC result;
   5502   INT32 i;
   5503   result = UINT32_Unmarshal(&target->count, buffer, size);
   5504   if (result != TPM_RC_SUCCESS) {
   5505     return result;
   5506   }
   5507   if (target->count > HASH_COUNT) {
   5508     return TPM_RC_SIZE;
   5509   }
   5510   for (i = 0; i < target->count; ++i) {
   5511     result = TPMT_HA_Unmarshal(&target->digests[i], buffer, size);
   5512     if (result != TPM_RC_SUCCESS) {
   5513       return result;
   5514     }
   5515   }
   5516   return TPM_RC_SUCCESS;
   5517 }
   5518 
   5519 UINT16 TPM_ECC_CURVE_Marshal(TPM_ECC_CURVE* source,
   5520                              BYTE** buffer,
   5521                              INT32* size) {
   5522   return uint16_t_Marshal(source, buffer, size);
   5523 }
   5524 
   5525 TPM_RC TPM_ECC_CURVE_Unmarshal(TPM_ECC_CURVE* target,
   5526                                BYTE** buffer,
   5527                                INT32* size) {
   5528   TPM_RC result;
   5529   result = uint16_t_Unmarshal(target, buffer, size);
   5530   if (result != TPM_RC_SUCCESS) {
   5531     return result;
   5532   }
   5533   if (*target == TPM_ECC_NONE) {
   5534     return TPM_RC_SUCCESS;
   5535   }
   5536   if (*target == TPM_ECC_NIST_P192) {
   5537     return TPM_RC_SUCCESS;
   5538   }
   5539   if (*target == TPM_ECC_NIST_P224) {
   5540     return TPM_RC_SUCCESS;
   5541   }
   5542   if (*target == TPM_ECC_NIST_P256) {
   5543     return TPM_RC_SUCCESS;
   5544   }
   5545   if (*target == TPM_ECC_NIST_P384) {
   5546     return TPM_RC_SUCCESS;
   5547   }
   5548   if (*target == TPM_ECC_NIST_P521) {
   5549     return TPM_RC_SUCCESS;
   5550   }
   5551   if (*target == TPM_ECC_BN_P256) {
   5552     return TPM_RC_SUCCESS;
   5553   }
   5554   if (*target == TPM_ECC_BN_P638) {
   5555     return TPM_RC_SUCCESS;
   5556   }
   5557   if (*target == TPM_ECC_SM2_P256) {
   5558     return TPM_RC_SUCCESS;
   5559   }
   5560   return TPM_RC_CURVE;
   5561 }
   5562 
   5563 UINT16 TPML_ECC_CURVE_Marshal(TPML_ECC_CURVE* source,
   5564                               BYTE** buffer,
   5565                               INT32* size) {
   5566   UINT16 total_size = 0;
   5567   INT32 i;
   5568   total_size += UINT32_Marshal(&source->count, buffer, size);
   5569   for (i = 0; i < source->count; ++i) {
   5570     total_size += TPM_ECC_CURVE_Marshal(&source->eccCurves[i], buffer, size);
   5571   }
   5572   return total_size;
   5573 }
   5574 
   5575 TPM_RC TPML_ECC_CURVE_Unmarshal(TPML_ECC_CURVE* target,
   5576                                 BYTE** buffer,
   5577                                 INT32* size) {
   5578   TPM_RC result;
   5579   INT32 i;
   5580   result = UINT32_Unmarshal(&target->count, buffer, size);
   5581   if (result != TPM_RC_SUCCESS) {
   5582     return result;
   5583   }
   5584   if (target->count > MAX_ECC_CURVES) {
   5585     return TPM_RC_SIZE;
   5586   }
   5587   for (i = 0; i < target->count; ++i) {
   5588     result = TPM_ECC_CURVE_Unmarshal(&target->eccCurves[i], buffer, size);
   5589     if (result != TPM_RC_SUCCESS) {
   5590       return result;
   5591     }
   5592   }
   5593   return TPM_RC_SUCCESS;
   5594 }
   5595 
   5596 UINT16 TPM_HANDLE_Marshal(TPM_HANDLE* source, BYTE** buffer, INT32* size) {
   5597   return uint32_t_Marshal(source, buffer, size);
   5598 }
   5599 
   5600 TPM_RC TPM_HANDLE_Unmarshal(TPM_HANDLE* target, BYTE** buffer, INT32* size) {
   5601   return uint32_t_Unmarshal(target, buffer, size);
   5602 }
   5603 
   5604 UINT16 TPML_HANDLE_Marshal(TPML_HANDLE* source, BYTE** buffer, INT32* size) {
   5605   UINT16 total_size = 0;
   5606   INT32 i;
   5607   total_size += UINT32_Marshal(&source->count, buffer, size);
   5608   for (i = 0; i < source->count; ++i) {
   5609     total_size += TPM_HANDLE_Marshal(&source->handle[i], buffer, size);
   5610   }
   5611   return total_size;
   5612 }
   5613 
   5614 TPM_RC TPML_HANDLE_Unmarshal(TPML_HANDLE* target, BYTE** buffer, INT32* size) {
   5615   TPM_RC result;
   5616   INT32 i;
   5617   result = UINT32_Unmarshal(&target->count, buffer, size);
   5618   if (result != TPM_RC_SUCCESS) {
   5619     return result;
   5620   }
   5621   if (target->count > MAX_CAP_HANDLES) {
   5622     return TPM_RC_SIZE;
   5623   }
   5624   for (i = 0; i < target->count; ++i) {
   5625     result = TPM_HANDLE_Unmarshal(&target->handle[i], buffer, size);
   5626     if (result != TPM_RC_SUCCESS) {
   5627       return result;
   5628     }
   5629   }
   5630   return TPM_RC_SUCCESS;
   5631 }
   5632 
   5633 UINT16 TPM_PT_Marshal(TPM_PT* source, BYTE** buffer, INT32* size) {
   5634   return uint32_t_Marshal(source, buffer, size);
   5635 }
   5636 
   5637 TPM_RC TPM_PT_Unmarshal(TPM_PT* target, BYTE** buffer, INT32* size) {
   5638   TPM_RC result;
   5639   result = uint32_t_Unmarshal(target, buffer, size);
   5640   if (result != TPM_RC_SUCCESS) {
   5641     return result;
   5642   }
   5643   if (*target == TPM_PT_NONE) {
   5644     return TPM_RC_SUCCESS;
   5645   }
   5646   if (*target == PT_GROUP) {
   5647     return TPM_RC_SUCCESS;
   5648   }
   5649   if (*target == PT_FIXED) {
   5650     return TPM_RC_SUCCESS;
   5651   }
   5652   if (*target == TPM_PT_FAMILY_INDICATOR) {
   5653     return TPM_RC_SUCCESS;
   5654   }
   5655   if (*target == TPM_PT_LEVEL) {
   5656     return TPM_RC_SUCCESS;
   5657   }
   5658   if (*target == TPM_PT_REVISION) {
   5659     return TPM_RC_SUCCESS;
   5660   }
   5661   if (*target == TPM_PT_DAY_OF_YEAR) {
   5662     return TPM_RC_SUCCESS;
   5663   }
   5664   if (*target == TPM_PT_YEAR) {
   5665     return TPM_RC_SUCCESS;
   5666   }
   5667   if (*target == TPM_PT_MANUFACTURER) {
   5668     return TPM_RC_SUCCESS;
   5669   }
   5670   if (*target == TPM_PT_VENDOR_STRING_1) {
   5671     return TPM_RC_SUCCESS;
   5672   }
   5673   if (*target == TPM_PT_VENDOR_STRING_2) {
   5674     return TPM_RC_SUCCESS;
   5675   }
   5676   if (*target == TPM_PT_VENDOR_STRING_3) {
   5677     return TPM_RC_SUCCESS;
   5678   }
   5679   if (*target == TPM_PT_VENDOR_STRING_4) {
   5680     return TPM_RC_SUCCESS;
   5681   }
   5682   if (*target == TPM_PT_VENDOR_TPM_TYPE) {
   5683     return TPM_RC_SUCCESS;
   5684   }
   5685   if (*target == TPM_PT_FIRMWARE_VERSION_1) {
   5686     return TPM_RC_SUCCESS;
   5687   }
   5688   if (*target == TPM_PT_FIRMWARE_VERSION_2) {
   5689     return TPM_RC_SUCCESS;
   5690   }
   5691   if (*target == TPM_PT_INPUT_BUFFER) {
   5692     return TPM_RC_SUCCESS;
   5693   }
   5694   if (*target == TPM_PT_HR_TRANSIENT_MIN) {
   5695     return TPM_RC_SUCCESS;
   5696   }
   5697   if (*target == TPM_PT_HR_PERSISTENT_MIN) {
   5698     return TPM_RC_SUCCESS;
   5699   }
   5700   if (*target == TPM_PT_HR_LOADED_MIN) {
   5701     return TPM_RC_SUCCESS;
   5702   }
   5703   if (*target == TPM_PT_ACTIVE_SESSIONS_MAX) {
   5704     return TPM_RC_SUCCESS;
   5705   }
   5706   if (*target == TPM_PT_PCR_COUNT) {
   5707     return TPM_RC_SUCCESS;
   5708   }
   5709   if (*target == TPM_PT_PCR_SELECT_MIN) {
   5710     return TPM_RC_SUCCESS;
   5711   }
   5712   if (*target == TPM_PT_CONTEXT_GAP_MAX) {
   5713     return TPM_RC_SUCCESS;
   5714   }
   5715   if (*target == TPM_PT_NV_COUNTERS_MAX) {
   5716     return TPM_RC_SUCCESS;
   5717   }
   5718   if (*target == TPM_PT_NV_INDEX_MAX) {
   5719     return TPM_RC_SUCCESS;
   5720   }
   5721   if (*target == TPM_PT_MEMORY) {
   5722     return TPM_RC_SUCCESS;
   5723   }
   5724   if (*target == TPM_PT_CLOCK_UPDATE) {
   5725     return TPM_RC_SUCCESS;
   5726   }
   5727   if (*target == TPM_PT_CONTEXT_HASH) {
   5728     return TPM_RC_SUCCESS;
   5729   }
   5730   if (*target == TPM_PT_CONTEXT_SYM) {
   5731     return TPM_RC_SUCCESS;
   5732   }
   5733   if (*target == TPM_PT_CONTEXT_SYM_SIZE) {
   5734     return TPM_RC_SUCCESS;
   5735   }
   5736   if (*target == TPM_PT_ORDERLY_COUNT) {
   5737     return TPM_RC_SUCCESS;
   5738   }
   5739   if (*target == TPM_PT_MAX_COMMAND_SIZE) {
   5740     return TPM_RC_SUCCESS;
   5741   }
   5742   if (*target == TPM_PT_MAX_RESPONSE_SIZE) {
   5743     return TPM_RC_SUCCESS;
   5744   }
   5745   if (*target == TPM_PT_MAX_DIGEST) {
   5746     return TPM_RC_SUCCESS;
   5747   }
   5748   if (*target == TPM_PT_MAX_OBJECT_CONTEXT) {
   5749     return TPM_RC_SUCCESS;
   5750   }
   5751   if (*target == TPM_PT_MAX_SESSION_CONTEXT) {
   5752     return TPM_RC_SUCCESS;
   5753   }
   5754   if (*target == TPM_PT_PS_FAMILY_INDICATOR) {
   5755     return TPM_RC_SUCCESS;
   5756   }
   5757   if (*target == TPM_PT_PS_LEVEL) {
   5758     return TPM_RC_SUCCESS;
   5759   }
   5760   if (*target == TPM_PT_PS_REVISION) {
   5761     return TPM_RC_SUCCESS;
   5762   }
   5763   if (*target == TPM_PT_PS_DAY_OF_YEAR) {
   5764     return TPM_RC_SUCCESS;
   5765   }
   5766   if (*target == TPM_PT_PS_YEAR) {
   5767     return TPM_RC_SUCCESS;
   5768   }
   5769   if (*target == TPM_PT_SPLIT_MAX) {
   5770     return TPM_RC_SUCCESS;
   5771   }
   5772   if (*target == TPM_PT_TOTAL_COMMANDS) {
   5773     return TPM_RC_SUCCESS;
   5774   }
   5775   if (*target == TPM_PT_LIBRARY_COMMANDS) {
   5776     return TPM_RC_SUCCESS;
   5777   }
   5778   if (*target == TPM_PT_VENDOR_COMMANDS) {
   5779     return TPM_RC_SUCCESS;
   5780   }
   5781   if (*target == TPM_PT_NV_BUFFER_MAX) {
   5782     return TPM_RC_SUCCESS;
   5783   }
   5784   if (*target == PT_VAR) {
   5785     return TPM_RC_SUCCESS;
   5786   }
   5787   if (*target == TPM_PT_PERMANENT) {
   5788     return TPM_RC_SUCCESS;
   5789   }
   5790   if (*target == TPM_PT_STARTUP_CLEAR) {
   5791     return TPM_RC_SUCCESS;
   5792   }
   5793   if (*target == TPM_PT_HR_NV_INDEX) {
   5794     return TPM_RC_SUCCESS;
   5795   }
   5796   if (*target == TPM_PT_HR_LOADED) {
   5797     return TPM_RC_SUCCESS;
   5798   }
   5799   if (*target == TPM_PT_HR_LOADED_AVAIL) {
   5800     return TPM_RC_SUCCESS;
   5801   }
   5802   if (*target == TPM_PT_HR_ACTIVE) {
   5803     return TPM_RC_SUCCESS;
   5804   }
   5805   if (*target == TPM_PT_HR_ACTIVE_AVAIL) {
   5806     return TPM_RC_SUCCESS;
   5807   }
   5808   if (*target == TPM_PT_HR_TRANSIENT_AVAIL) {
   5809     return TPM_RC_SUCCESS;
   5810   }
   5811   if (*target == TPM_PT_HR_PERSISTENT) {
   5812     return TPM_RC_SUCCESS;
   5813   }
   5814   if (*target == TPM_PT_HR_PERSISTENT_AVAIL) {
   5815     return TPM_RC_SUCCESS;
   5816   }
   5817   if (*target == TPM_PT_NV_COUNTERS) {
   5818     return TPM_RC_SUCCESS;
   5819   }
   5820   if (*target == TPM_PT_NV_COUNTERS_AVAIL) {
   5821     return TPM_RC_SUCCESS;
   5822   }
   5823   if (*target == TPM_PT_ALGORITHM_SET) {
   5824     return TPM_RC_SUCCESS;
   5825   }
   5826   if (*target == TPM_PT_LOADED_CURVES) {
   5827     return TPM_RC_SUCCESS;
   5828   }
   5829   if (*target == TPM_PT_LOCKOUT_COUNTER) {
   5830     return TPM_RC_SUCCESS;
   5831   }
   5832   if (*target == TPM_PT_MAX_AUTH_FAIL) {
   5833     return TPM_RC_SUCCESS;
   5834   }
   5835   if (*target == TPM_PT_LOCKOUT_INTERVAL) {
   5836     return TPM_RC_SUCCESS;
   5837   }
   5838   if (*target == TPM_PT_LOCKOUT_RECOVERY) {
   5839     return TPM_RC_SUCCESS;
   5840   }
   5841   if (*target == TPM_PT_NV_WRITE_RECOVERY) {
   5842     return TPM_RC_SUCCESS;
   5843   }
   5844   if (*target == TPM_PT_AUDIT_COUNTER_0) {
   5845     return TPM_RC_SUCCESS;
   5846   }
   5847   if (*target == TPM_PT_AUDIT_COUNTER_1) {
   5848     return TPM_RC_SUCCESS;
   5849   }
   5850   return TPM_RC_VALUE;
   5851 }
   5852 
   5853 UINT16 TPMS_TAGGED_PCR_SELECT_Marshal(TPMS_TAGGED_PCR_SELECT* source,
   5854                                       BYTE** buffer,
   5855                                       INT32* size) {
   5856   UINT16 total_size = 0;
   5857   INT32 i;
   5858   total_size += TPM_PT_Marshal(&source->tag, buffer, size);
   5859   total_size += UINT8_Marshal(&source->sizeofSelect, buffer, size);
   5860   for (i = 0; i < source->sizeofSelect; ++i) {
   5861     total_size += BYTE_Marshal(&source->pcrSelect[i], buffer, size);
   5862   }
   5863   return total_size;
   5864 }
   5865 
   5866 TPM_RC TPMS_TAGGED_PCR_SELECT_Unmarshal(TPMS_TAGGED_PCR_SELECT* target,
   5867                                         BYTE** buffer,
   5868                                         INT32* size) {
   5869   TPM_RC result;
   5870   INT32 i;
   5871   result = TPM_PT_Unmarshal(&target->tag, buffer, size);
   5872   if (result != TPM_RC_SUCCESS) {
   5873     return result;
   5874   }
   5875   result = UINT8_Unmarshal(&target->sizeofSelect, buffer, size);
   5876   if (result != TPM_RC_SUCCESS) {
   5877     return result;
   5878   }
   5879   if (target->sizeofSelect > PCR_SELECT_MAX) {
   5880     return TPM_RC_VALUE;
   5881   }
   5882   if (target->sizeofSelect < PCR_SELECT_MIN) {
   5883     return TPM_RC_VALUE;
   5884   }
   5885   for (i = 0; i < target->sizeofSelect; ++i) {
   5886     result = BYTE_Unmarshal(&target->pcrSelect[i], buffer, size);
   5887     if (result != TPM_RC_SUCCESS) {
   5888       return result;
   5889     }
   5890   }
   5891   return TPM_RC_SUCCESS;
   5892 }
   5893 
   5894 UINT16 TPML_TAGGED_PCR_PROPERTY_Marshal(TPML_TAGGED_PCR_PROPERTY* source,
   5895                                         BYTE** buffer,
   5896                                         INT32* size) {
   5897   UINT16 total_size = 0;
   5898   INT32 i;
   5899   total_size += UINT32_Marshal(&source->count, buffer, size);
   5900   for (i = 0; i < source->count; ++i) {
   5901     total_size +=
   5902         TPMS_TAGGED_PCR_SELECT_Marshal(&source->pcrProperty[i], buffer, size);
   5903   }
   5904   return total_size;
   5905 }
   5906 
   5907 TPM_RC TPML_TAGGED_PCR_PROPERTY_Unmarshal(TPML_TAGGED_PCR_PROPERTY* target,
   5908                                           BYTE** buffer,
   5909                                           INT32* size) {
   5910   TPM_RC result;
   5911   INT32 i;
   5912   result = UINT32_Unmarshal(&target->count, buffer, size);
   5913   if (result != TPM_RC_SUCCESS) {
   5914     return result;
   5915   }
   5916   if (target->count > MAX_PCR_PROPERTIES) {
   5917     return TPM_RC_SIZE;
   5918   }
   5919   for (i = 0; i < target->count; ++i) {
   5920     result =
   5921         TPMS_TAGGED_PCR_SELECT_Unmarshal(&target->pcrProperty[i], buffer, size);
   5922     if (result != TPM_RC_SUCCESS) {
   5923       return result;
   5924     }
   5925   }
   5926   return TPM_RC_SUCCESS;
   5927 }
   5928 
   5929 UINT16 TPMS_TAGGED_PROPERTY_Marshal(TPMS_TAGGED_PROPERTY* source,
   5930                                     BYTE** buffer,
   5931                                     INT32* size) {
   5932   UINT16 total_size = 0;
   5933   total_size += TPM_PT_Marshal(&source->property, buffer, size);
   5934   total_size += UINT32_Marshal(&source->value, buffer, size);
   5935   return total_size;
   5936 }
   5937 
   5938 TPM_RC TPMS_TAGGED_PROPERTY_Unmarshal(TPMS_TAGGED_PROPERTY* target,
   5939                                       BYTE** buffer,
   5940                                       INT32* size) {
   5941   TPM_RC result;
   5942   result = TPM_PT_Unmarshal(&target->property, buffer, size);
   5943   if (result != TPM_RC_SUCCESS) {
   5944     return result;
   5945   }
   5946   result = UINT32_Unmarshal(&target->value, buffer, size);
   5947   if (result != TPM_RC_SUCCESS) {
   5948     return result;
   5949   }
   5950   return TPM_RC_SUCCESS;
   5951 }
   5952 
   5953 UINT16 TPML_TAGGED_TPM_PROPERTY_Marshal(TPML_TAGGED_TPM_PROPERTY* source,
   5954                                         BYTE** buffer,
   5955                                         INT32* size) {
   5956   UINT16 total_size = 0;
   5957   INT32 i;
   5958   total_size += UINT32_Marshal(&source->count, buffer, size);
   5959   for (i = 0; i < source->count; ++i) {
   5960     total_size +=
   5961         TPMS_TAGGED_PROPERTY_Marshal(&source->tpmProperty[i], buffer, size);
   5962   }
   5963   return total_size;
   5964 }
   5965 
   5966 TPM_RC TPML_TAGGED_TPM_PROPERTY_Unmarshal(TPML_TAGGED_TPM_PROPERTY* target,
   5967                                           BYTE** buffer,
   5968                                           INT32* size) {
   5969   TPM_RC result;
   5970   INT32 i;
   5971   result = UINT32_Unmarshal(&target->count, buffer, size);
   5972   if (result != TPM_RC_SUCCESS) {
   5973     return result;
   5974   }
   5975   if (target->count > MAX_TPM_PROPERTIES) {
   5976     return TPM_RC_SIZE;
   5977   }
   5978   for (i = 0; i < target->count; ++i) {
   5979     result =
   5980         TPMS_TAGGED_PROPERTY_Unmarshal(&target->tpmProperty[i], buffer, size);
   5981     if (result != TPM_RC_SUCCESS) {
   5982       return result;
   5983     }
   5984   }
   5985   return TPM_RC_SUCCESS;
   5986 }
   5987 
   5988 UINT16 TPMS_ALGORITHM_DESCRIPTION_Marshal(TPMS_ALGORITHM_DESCRIPTION* source,
   5989                                           BYTE** buffer,
   5990                                           INT32* size) {
   5991   UINT16 total_size = 0;
   5992   total_size += TPM_ALG_ID_Marshal(&source->alg, buffer, size);
   5993   total_size += TPMA_ALGORITHM_Marshal(&source->attributes, buffer, size);
   5994   return total_size;
   5995 }
   5996 
   5997 TPM_RC TPMS_ALGORITHM_DESCRIPTION_Unmarshal(TPMS_ALGORITHM_DESCRIPTION* target,
   5998                                             BYTE** buffer,
   5999                                             INT32* size) {
   6000   TPM_RC result;
   6001   result = TPM_ALG_ID_Unmarshal(&target->alg, buffer, size);
   6002   if (result != TPM_RC_SUCCESS) {
   6003     return result;
   6004   }
   6005   result = TPMA_ALGORITHM_Unmarshal(&target->attributes, buffer, size);
   6006   if (result != TPM_RC_SUCCESS) {
   6007     return result;
   6008   }
   6009   return TPM_RC_SUCCESS;
   6010 }
   6011 
   6012 UINT16 TPMS_ALGORITHM_DETAIL_ECC_Marshal(TPMS_ALGORITHM_DETAIL_ECC* source,
   6013                                          BYTE** buffer,
   6014                                          INT32* size) {
   6015   UINT16 total_size = 0;
   6016   total_size += TPM_ECC_CURVE_Marshal(&source->curveID, buffer, size);
   6017   total_size += UINT16_Marshal(&source->keySize, buffer, size);
   6018   total_size += TPMT_KDF_SCHEME_Marshal(&source->kdf, buffer, size);
   6019   total_size += TPMT_ECC_SCHEME_Marshal(&source->sign, buffer, size);
   6020   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->p, buffer, size);
   6021   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->a, buffer, size);
   6022   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->b, buffer, size);
   6023   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->gX, buffer, size);
   6024   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->gY, buffer, size);
   6025   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->n, buffer, size);
   6026   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->h, buffer, size);
   6027   return total_size;
   6028 }
   6029 
   6030 TPM_RC TPMS_ALGORITHM_DETAIL_ECC_Unmarshal(TPMS_ALGORITHM_DETAIL_ECC* target,
   6031                                            BYTE** buffer,
   6032                                            INT32* size) {
   6033   TPM_RC result;
   6034   result = TPM_ECC_CURVE_Unmarshal(&target->curveID, buffer, size);
   6035   if (result != TPM_RC_SUCCESS) {
   6036     return result;
   6037   }
   6038   result = UINT16_Unmarshal(&target->keySize, buffer, size);
   6039   if (result != TPM_RC_SUCCESS) {
   6040     return result;
   6041   }
   6042   result = TPMT_KDF_SCHEME_Unmarshal(&target->kdf, buffer, size);
   6043   if (result != TPM_RC_SUCCESS) {
   6044     return result;
   6045   }
   6046   result = TPMT_ECC_SCHEME_Unmarshal(&target->sign, buffer, size);
   6047   if (result != TPM_RC_SUCCESS) {
   6048     return result;
   6049   }
   6050   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->p, buffer, size);
   6051   if (result != TPM_RC_SUCCESS) {
   6052     return result;
   6053   }
   6054   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->a, buffer, size);
   6055   if (result != TPM_RC_SUCCESS) {
   6056     return result;
   6057   }
   6058   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->b, buffer, size);
   6059   if (result != TPM_RC_SUCCESS) {
   6060     return result;
   6061   }
   6062   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->gX, buffer, size);
   6063   if (result != TPM_RC_SUCCESS) {
   6064     return result;
   6065   }
   6066   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->gY, buffer, size);
   6067   if (result != TPM_RC_SUCCESS) {
   6068     return result;
   6069   }
   6070   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->n, buffer, size);
   6071   if (result != TPM_RC_SUCCESS) {
   6072     return result;
   6073   }
   6074   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->h, buffer, size);
   6075   if (result != TPM_RC_SUCCESS) {
   6076     return result;
   6077   }
   6078   return TPM_RC_SUCCESS;
   6079 }
   6080 
   6081 UINT16 UINT64_Marshal(UINT64* source, BYTE** buffer, INT32* size) {
   6082   return uint64_t_Marshal(source, buffer, size);
   6083 }
   6084 
   6085 TPM_RC UINT64_Unmarshal(UINT64* target, BYTE** buffer, INT32* size) {
   6086   return uint64_t_Unmarshal(target, buffer, size);
   6087 }
   6088 
   6089 UINT16 TPM_GENERATED_Marshal(TPM_GENERATED* source,
   6090                              BYTE** buffer,
   6091                              INT32* size) {
   6092   return uint32_t_Marshal(source, buffer, size);
   6093 }
   6094 
   6095 TPM_RC TPM_GENERATED_Unmarshal(TPM_GENERATED* target,
   6096                                BYTE** buffer,
   6097                                INT32* size) {
   6098   TPM_RC result;
   6099   result = uint32_t_Unmarshal(target, buffer, size);
   6100   if (result != TPM_RC_SUCCESS) {
   6101     return result;
   6102   }
   6103   if (*target == TPM_GENERATED_VALUE) {
   6104     return TPM_RC_SUCCESS;
   6105   }
   6106   return TPM_RC_VALUE;
   6107 }
   6108 
   6109 UINT16 TPMS_CREATION_INFO_Marshal(TPMS_CREATION_INFO* source,
   6110                                   BYTE** buffer,
   6111                                   INT32* size) {
   6112   UINT16 total_size = 0;
   6113   total_size += TPM2B_NAME_Marshal(&source->objectName, buffer, size);
   6114   total_size += TPM2B_DIGEST_Marshal(&source->creationHash, buffer, size);
   6115   return total_size;
   6116 }
   6117 
   6118 TPM_RC TPMS_CREATION_INFO_Unmarshal(TPMS_CREATION_INFO* target,
   6119                                     BYTE** buffer,
   6120                                     INT32* size) {
   6121   TPM_RC result;
   6122   result = TPM2B_NAME_Unmarshal(&target->objectName, buffer, size);
   6123   if (result != TPM_RC_SUCCESS) {
   6124     return result;
   6125   }
   6126   result = TPM2B_DIGEST_Unmarshal(&target->creationHash, buffer, size);
   6127   if (result != TPM_RC_SUCCESS) {
   6128     return result;
   6129   }
   6130   return TPM_RC_SUCCESS;
   6131 }
   6132 
   6133 UINT16 TPMS_COMMAND_AUDIT_INFO_Marshal(TPMS_COMMAND_AUDIT_INFO* source,
   6134                                        BYTE** buffer,
   6135                                        INT32* size) {
   6136   UINT16 total_size = 0;
   6137   total_size += UINT64_Marshal(&source->auditCounter, buffer, size);
   6138   total_size += TPM_ALG_ID_Marshal(&source->digestAlg, buffer, size);
   6139   total_size += TPM2B_DIGEST_Marshal(&source->auditDigest, buffer, size);
   6140   total_size += TPM2B_DIGEST_Marshal(&source->commandDigest, buffer, size);
   6141   return total_size;
   6142 }
   6143 
   6144 TPM_RC TPMS_COMMAND_AUDIT_INFO_Unmarshal(TPMS_COMMAND_AUDIT_INFO* target,
   6145                                          BYTE** buffer,
   6146                                          INT32* size) {
   6147   TPM_RC result;
   6148   result = UINT64_Unmarshal(&target->auditCounter, buffer, size);
   6149   if (result != TPM_RC_SUCCESS) {
   6150     return result;
   6151   }
   6152   result = TPM_ALG_ID_Unmarshal(&target->digestAlg, buffer, size);
   6153   if (result != TPM_RC_SUCCESS) {
   6154     return result;
   6155   }
   6156   result = TPM2B_DIGEST_Unmarshal(&target->auditDigest, buffer, size);
   6157   if (result != TPM_RC_SUCCESS) {
   6158     return result;
   6159   }
   6160   result = TPM2B_DIGEST_Unmarshal(&target->commandDigest, buffer, size);
   6161   if (result != TPM_RC_SUCCESS) {
   6162     return result;
   6163   }
   6164   return TPM_RC_SUCCESS;
   6165 }
   6166 
   6167 UINT16 TPMS_QUOTE_INFO_Marshal(TPMS_QUOTE_INFO* source,
   6168                                BYTE** buffer,
   6169                                INT32* size) {
   6170   UINT16 total_size = 0;
   6171   total_size += TPML_PCR_SELECTION_Marshal(&source->pcrSelect, buffer, size);
   6172   total_size += TPM2B_DIGEST_Marshal(&source->pcrDigest, buffer, size);
   6173   return total_size;
   6174 }
   6175 
   6176 TPM_RC TPMS_QUOTE_INFO_Unmarshal(TPMS_QUOTE_INFO* target,
   6177                                  BYTE** buffer,
   6178                                  INT32* size) {
   6179   TPM_RC result;
   6180   result = TPML_PCR_SELECTION_Unmarshal(&target->pcrSelect, buffer, size);
   6181   if (result != TPM_RC_SUCCESS) {
   6182     return result;
   6183   }
   6184   result = TPM2B_DIGEST_Unmarshal(&target->pcrDigest, buffer, size);
   6185   if (result != TPM_RC_SUCCESS) {
   6186     return result;
   6187   }
   6188   return TPM_RC_SUCCESS;
   6189 }
   6190 
   6191 UINT16 TPMS_CERTIFY_INFO_Marshal(TPMS_CERTIFY_INFO* source,
   6192                                  BYTE** buffer,
   6193                                  INT32* size) {
   6194   UINT16 total_size = 0;
   6195   total_size += TPM2B_NAME_Marshal(&source->name, buffer, size);
   6196   total_size += TPM2B_NAME_Marshal(&source->qualifiedName, buffer, size);
   6197   return total_size;
   6198 }
   6199 
   6200 TPM_RC TPMS_CERTIFY_INFO_Unmarshal(TPMS_CERTIFY_INFO* target,
   6201                                    BYTE** buffer,
   6202                                    INT32* size) {
   6203   TPM_RC result;
   6204   result = TPM2B_NAME_Unmarshal(&target->name, buffer, size);
   6205   if (result != TPM_RC_SUCCESS) {
   6206     return result;
   6207   }
   6208   result = TPM2B_NAME_Unmarshal(&target->qualifiedName, buffer, size);
   6209   if (result != TPM_RC_SUCCESS) {
   6210     return result;
   6211   }
   6212   return TPM_RC_SUCCESS;
   6213 }
   6214 
   6215 UINT16 TPMS_SESSION_AUDIT_INFO_Marshal(TPMS_SESSION_AUDIT_INFO* source,
   6216                                        BYTE** buffer,
   6217                                        INT32* size) {
   6218   UINT16 total_size = 0;
   6219   total_size += TPMI_YES_NO_Marshal(&source->exclusiveSession, buffer, size);
   6220   total_size += TPM2B_DIGEST_Marshal(&source->sessionDigest, buffer, size);
   6221   return total_size;
   6222 }
   6223 
   6224 TPM_RC TPMS_SESSION_AUDIT_INFO_Unmarshal(TPMS_SESSION_AUDIT_INFO* target,
   6225                                          BYTE** buffer,
   6226                                          INT32* size) {
   6227   TPM_RC result;
   6228   result = TPMI_YES_NO_Unmarshal(&target->exclusiveSession, buffer, size);
   6229   if (result != TPM_RC_SUCCESS) {
   6230     return result;
   6231   }
   6232   result = TPM2B_DIGEST_Unmarshal(&target->sessionDigest, buffer, size);
   6233   if (result != TPM_RC_SUCCESS) {
   6234     return result;
   6235   }
   6236   return TPM_RC_SUCCESS;
   6237 }
   6238 
   6239 UINT16 TPMS_CLOCK_INFO_Marshal(TPMS_CLOCK_INFO* source,
   6240                                BYTE** buffer,
   6241                                INT32* size) {
   6242   UINT16 total_size = 0;
   6243   total_size += UINT64_Marshal(&source->clock, buffer, size);
   6244   total_size += UINT32_Marshal(&source->resetCount, buffer, size);
   6245   total_size += UINT32_Marshal(&source->restartCount, buffer, size);
   6246   total_size += TPMI_YES_NO_Marshal(&source->safe, buffer, size);
   6247   return total_size;
   6248 }
   6249 
   6250 TPM_RC TPMS_CLOCK_INFO_Unmarshal(TPMS_CLOCK_INFO* target,
   6251                                  BYTE** buffer,
   6252                                  INT32* size) {
   6253   TPM_RC result;
   6254   result = UINT64_Unmarshal(&target->clock, buffer, size);
   6255   if (result != TPM_RC_SUCCESS) {
   6256     return result;
   6257   }
   6258   result = UINT32_Unmarshal(&target->resetCount, buffer, size);
   6259   if (result != TPM_RC_SUCCESS) {
   6260     return result;
   6261   }
   6262   result = UINT32_Unmarshal(&target->restartCount, buffer, size);
   6263   if (result != TPM_RC_SUCCESS) {
   6264     return result;
   6265   }
   6266   result = TPMI_YES_NO_Unmarshal(&target->safe, buffer, size);
   6267   if (result != TPM_RC_SUCCESS) {
   6268     return result;
   6269   }
   6270   return TPM_RC_SUCCESS;
   6271 }
   6272 
   6273 UINT16 TPMS_TIME_INFO_Marshal(TPMS_TIME_INFO* source,
   6274                               BYTE** buffer,
   6275                               INT32* size) {
   6276   UINT16 total_size = 0;
   6277   total_size += UINT64_Marshal(&source->time, buffer, size);
   6278   total_size += TPMS_CLOCK_INFO_Marshal(&source->clockInfo, buffer, size);
   6279   return total_size;
   6280 }
   6281 
   6282 TPM_RC TPMS_TIME_INFO_Unmarshal(TPMS_TIME_INFO* target,
   6283                                 BYTE** buffer,
   6284                                 INT32* size) {
   6285   TPM_RC result;
   6286   result = UINT64_Unmarshal(&target->time, buffer, size);
   6287   if (result != TPM_RC_SUCCESS) {
   6288     return result;
   6289   }
   6290   result = TPMS_CLOCK_INFO_Unmarshal(&target->clockInfo, buffer, size);
   6291   if (result != TPM_RC_SUCCESS) {
   6292     return result;
   6293   }
   6294   return TPM_RC_SUCCESS;
   6295 }
   6296 
   6297 UINT16 TPMS_TIME_ATTEST_INFO_Marshal(TPMS_TIME_ATTEST_INFO* source,
   6298                                      BYTE** buffer,
   6299                                      INT32* size) {
   6300   UINT16 total_size = 0;
   6301   total_size += TPMS_TIME_INFO_Marshal(&source->time, buffer, size);
   6302   total_size += UINT64_Marshal(&source->firmwareVersion, buffer, size);
   6303   return total_size;
   6304 }
   6305 
   6306 TPM_RC TPMS_TIME_ATTEST_INFO_Unmarshal(TPMS_TIME_ATTEST_INFO* target,
   6307                                        BYTE** buffer,
   6308                                        INT32* size) {
   6309   TPM_RC result;
   6310   result = TPMS_TIME_INFO_Unmarshal(&target->time, buffer, size);
   6311   if (result != TPM_RC_SUCCESS) {
   6312     return result;
   6313   }
   6314   result = UINT64_Unmarshal(&target->firmwareVersion, buffer, size);
   6315   if (result != TPM_RC_SUCCESS) {
   6316     return result;
   6317   }
   6318   return TPM_RC_SUCCESS;
   6319 }
   6320 
   6321 UINT16 TPMS_NV_CERTIFY_INFO_Marshal(TPMS_NV_CERTIFY_INFO* source,
   6322                                     BYTE** buffer,
   6323                                     INT32* size) {
   6324   UINT16 total_size = 0;
   6325   total_size += TPM2B_NAME_Marshal(&source->indexName, buffer, size);
   6326   total_size += UINT16_Marshal(&source->offset, buffer, size);
   6327   total_size += TPM2B_MAX_NV_BUFFER_Marshal(&source->nvContents, buffer, size);
   6328   return total_size;
   6329 }
   6330 
   6331 TPM_RC TPMS_NV_CERTIFY_INFO_Unmarshal(TPMS_NV_CERTIFY_INFO* target,
   6332                                       BYTE** buffer,
   6333                                       INT32* size) {
   6334   TPM_RC result;
   6335   result = TPM2B_NAME_Unmarshal(&target->indexName, buffer, size);
   6336   if (result != TPM_RC_SUCCESS) {
   6337     return result;
   6338   }
   6339   result = UINT16_Unmarshal(&target->offset, buffer, size);
   6340   if (result != TPM_RC_SUCCESS) {
   6341     return result;
   6342   }
   6343   result = TPM2B_MAX_NV_BUFFER_Unmarshal(&target->nvContents, buffer, size);
   6344   if (result != TPM_RC_SUCCESS) {
   6345     return result;
   6346   }
   6347   return TPM_RC_SUCCESS;
   6348 }
   6349 
   6350 UINT16 TPMU_ATTEST_Marshal(TPMU_ATTEST* source,
   6351                            BYTE** buffer,
   6352                            INT32* size,
   6353                            UINT32 selector) {
   6354   switch (selector) {
   6355     case TPM_ST_ATTEST_CERTIFY:
   6356       return TPMS_CERTIFY_INFO_Marshal((TPMS_CERTIFY_INFO*)&source->certify,
   6357                                        buffer, size);
   6358     case TPM_ST_ATTEST_CREATION:
   6359       return TPMS_CREATION_INFO_Marshal((TPMS_CREATION_INFO*)&source->creation,
   6360                                         buffer, size);
   6361     case TPM_ST_ATTEST_QUOTE:
   6362       return TPMS_QUOTE_INFO_Marshal((TPMS_QUOTE_INFO*)&source->quote, buffer,
   6363                                      size);
   6364     case TPM_ST_ATTEST_COMMAND_AUDIT:
   6365       return TPMS_COMMAND_AUDIT_INFO_Marshal(
   6366           (TPMS_COMMAND_AUDIT_INFO*)&source->commandAudit, buffer, size);
   6367     case TPM_ST_ATTEST_SESSION_AUDIT:
   6368       return TPMS_SESSION_AUDIT_INFO_Marshal(
   6369           (TPMS_SESSION_AUDIT_INFO*)&source->sessionAudit, buffer, size);
   6370     case TPM_ST_ATTEST_TIME:
   6371       return TPMS_TIME_ATTEST_INFO_Marshal(
   6372           (TPMS_TIME_ATTEST_INFO*)&source->time, buffer, size);
   6373     case TPM_ST_ATTEST_NV:
   6374       return TPMS_NV_CERTIFY_INFO_Marshal((TPMS_NV_CERTIFY_INFO*)&source->nv,
   6375                                           buffer, size);
   6376   }
   6377   return 0;
   6378 }
   6379 
   6380 TPM_RC TPMU_ATTEST_Unmarshal(TPMU_ATTEST* target,
   6381                              BYTE** buffer,
   6382                              INT32* size,
   6383                              UINT32 selector) {
   6384   switch (selector) {
   6385     case TPM_ST_ATTEST_CERTIFY:
   6386       return TPMS_CERTIFY_INFO_Unmarshal((TPMS_CERTIFY_INFO*)&target->certify,
   6387                                          buffer, size);
   6388     case TPM_ST_ATTEST_CREATION:
   6389       return TPMS_CREATION_INFO_Unmarshal(
   6390           (TPMS_CREATION_INFO*)&target->creation, buffer, size);
   6391     case TPM_ST_ATTEST_QUOTE:
   6392       return TPMS_QUOTE_INFO_Unmarshal((TPMS_QUOTE_INFO*)&target->quote, buffer,
   6393                                        size);
   6394     case TPM_ST_ATTEST_COMMAND_AUDIT:
   6395       return TPMS_COMMAND_AUDIT_INFO_Unmarshal(
   6396           (TPMS_COMMAND_AUDIT_INFO*)&target->commandAudit, buffer, size);
   6397     case TPM_ST_ATTEST_SESSION_AUDIT:
   6398       return TPMS_SESSION_AUDIT_INFO_Unmarshal(
   6399           (TPMS_SESSION_AUDIT_INFO*)&target->sessionAudit, buffer, size);
   6400     case TPM_ST_ATTEST_TIME:
   6401       return TPMS_TIME_ATTEST_INFO_Unmarshal(
   6402           (TPMS_TIME_ATTEST_INFO*)&target->time, buffer, size);
   6403     case TPM_ST_ATTEST_NV:
   6404       return TPMS_NV_CERTIFY_INFO_Unmarshal((TPMS_NV_CERTIFY_INFO*)&target->nv,
   6405                                             buffer, size);
   6406   }
   6407   return TPM_RC_SELECTOR;
   6408 }
   6409 
   6410 UINT16 TPMS_ATTEST_Marshal(TPMS_ATTEST* source, BYTE** buffer, INT32* size) {
   6411   UINT16 total_size = 0;
   6412   total_size += TPM_GENERATED_Marshal(&source->magic, buffer, size);
   6413   total_size += TPMI_ST_ATTEST_Marshal(&source->type, buffer, size);
   6414   total_size += TPM2B_NAME_Marshal(&source->qualifiedSigner, buffer, size);
   6415   total_size += TPM2B_DATA_Marshal(&source->extraData, buffer, size);
   6416   total_size += TPMS_CLOCK_INFO_Marshal(&source->clockInfo, buffer, size);
   6417   total_size += UINT64_Marshal(&source->firmwareVersion, buffer, size);
   6418   total_size +=
   6419       TPMU_ATTEST_Marshal(&source->attested, buffer, size, source->type);
   6420   return total_size;
   6421 }
   6422 
   6423 TPM_RC TPMS_ATTEST_Unmarshal(TPMS_ATTEST* target, BYTE** buffer, INT32* size) {
   6424   TPM_RC result;
   6425   result = TPM_GENERATED_Unmarshal(&target->magic, buffer, size);
   6426   if (result != TPM_RC_SUCCESS) {
   6427     return result;
   6428   }
   6429   result = TPMI_ST_ATTEST_Unmarshal(&target->type, buffer, size);
   6430   if (result != TPM_RC_SUCCESS) {
   6431     return result;
   6432   }
   6433   result = TPM2B_NAME_Unmarshal(&target->qualifiedSigner, buffer, size);
   6434   if (result != TPM_RC_SUCCESS) {
   6435     return result;
   6436   }
   6437   result = TPM2B_DATA_Unmarshal(&target->extraData, buffer, size);
   6438   if (result != TPM_RC_SUCCESS) {
   6439     return result;
   6440   }
   6441   result = TPMS_CLOCK_INFO_Unmarshal(&target->clockInfo, buffer, size);
   6442   if (result != TPM_RC_SUCCESS) {
   6443     return result;
   6444   }
   6445   result = UINT64_Unmarshal(&target->firmwareVersion, buffer, size);
   6446   if (result != TPM_RC_SUCCESS) {
   6447     return result;
   6448   }
   6449   result = TPMU_ATTEST_Unmarshal(&target->attested, buffer, size, target->type);
   6450   if (result != TPM_RC_SUCCESS) {
   6451     return result;
   6452   }
   6453   return TPM_RC_SUCCESS;
   6454 }
   6455 
   6456 UINT16 TPMS_AUTH_COMMAND_Marshal(TPMS_AUTH_COMMAND* source,
   6457                                  BYTE** buffer,
   6458                                  INT32* size) {
   6459   UINT16 total_size = 0;
   6460   total_size +=
   6461       TPMI_SH_AUTH_SESSION_Marshal(&source->sessionHandle, buffer, size);
   6462   total_size += TPM2B_NONCE_Marshal(&source->nonce, buffer, size);
   6463   total_size += TPMA_SESSION_Marshal(&source->sessionAttributes, buffer, size);
   6464   total_size += TPM2B_AUTH_Marshal(&source->hmac, buffer, size);
   6465   return total_size;
   6466 }
   6467 
   6468 TPM_RC TPMS_AUTH_COMMAND_Unmarshal(TPMS_AUTH_COMMAND* target,
   6469                                    BYTE** buffer,
   6470                                    INT32* size) {
   6471   TPM_RC result;
   6472   result = TPMI_SH_AUTH_SESSION_Unmarshal(&target->sessionHandle, buffer, size,
   6473                                           TRUE);
   6474   if (result != TPM_RC_SUCCESS) {
   6475     return result;
   6476   }
   6477   result = TPM2B_NONCE_Unmarshal(&target->nonce, buffer, size);
   6478   if (result != TPM_RC_SUCCESS) {
   6479     return result;
   6480   }
   6481   result = TPMA_SESSION_Unmarshal(&target->sessionAttributes, buffer, size);
   6482   if (result != TPM_RC_SUCCESS) {
   6483     return result;
   6484   }
   6485   result = TPM2B_AUTH_Unmarshal(&target->hmac, buffer, size);
   6486   if (result != TPM_RC_SUCCESS) {
   6487     return result;
   6488   }
   6489   return TPM_RC_SUCCESS;
   6490 }
   6491 
   6492 UINT16 TPMS_AUTH_RESPONSE_Marshal(TPMS_AUTH_RESPONSE* source,
   6493                                   BYTE** buffer,
   6494                                   INT32* size) {
   6495   UINT16 total_size = 0;
   6496   total_size += TPM2B_NONCE_Marshal(&source->nonce, buffer, size);
   6497   total_size += TPMA_SESSION_Marshal(&source->sessionAttributes, buffer, size);
   6498   total_size += TPM2B_AUTH_Marshal(&source->hmac, buffer, size);
   6499   return total_size;
   6500 }
   6501 
   6502 TPM_RC TPMS_AUTH_RESPONSE_Unmarshal(TPMS_AUTH_RESPONSE* target,
   6503                                     BYTE** buffer,
   6504                                     INT32* size) {
   6505   TPM_RC result;
   6506   result = TPM2B_NONCE_Unmarshal(&target->nonce, buffer, size);
   6507   if (result != TPM_RC_SUCCESS) {
   6508     return result;
   6509   }
   6510   result = TPMA_SESSION_Unmarshal(&target->sessionAttributes, buffer, size);
   6511   if (result != TPM_RC_SUCCESS) {
   6512     return result;
   6513   }
   6514   result = TPM2B_AUTH_Unmarshal(&target->hmac, buffer, size);
   6515   if (result != TPM_RC_SUCCESS) {
   6516     return result;
   6517   }
   6518   return TPM_RC_SUCCESS;
   6519 }
   6520 
   6521 UINT16 TPM_CAP_Marshal(TPM_CAP* source, BYTE** buffer, INT32* size) {
   6522   return uint32_t_Marshal(source, buffer, size);
   6523 }
   6524 
   6525 TPM_RC TPM_CAP_Unmarshal(TPM_CAP* target, BYTE** buffer, INT32* size) {
   6526   TPM_RC result;
   6527   result = uint32_t_Unmarshal(target, buffer, size);
   6528   if (result != TPM_RC_SUCCESS) {
   6529     return result;
   6530   }
   6531   if (*target == TPM_CAP_FIRST) {
   6532     return TPM_RC_SUCCESS;
   6533   }
   6534   if (*target == TPM_CAP_ALGS) {
   6535     return TPM_RC_SUCCESS;
   6536   }
   6537   if (*target == TPM_CAP_HANDLES) {
   6538     return TPM_RC_SUCCESS;
   6539   }
   6540   if (*target == TPM_CAP_COMMANDS) {
   6541     return TPM_RC_SUCCESS;
   6542   }
   6543   if (*target == TPM_CAP_PP_COMMANDS) {
   6544     return TPM_RC_SUCCESS;
   6545   }
   6546   if (*target == TPM_CAP_AUDIT_COMMANDS) {
   6547     return TPM_RC_SUCCESS;
   6548   }
   6549   if (*target == TPM_CAP_PCRS) {
   6550     return TPM_RC_SUCCESS;
   6551   }
   6552   if (*target == TPM_CAP_TPM_PROPERTIES) {
   6553     return TPM_RC_SUCCESS;
   6554   }
   6555   if (*target == TPM_CAP_PCR_PROPERTIES) {
   6556     return TPM_RC_SUCCESS;
   6557   }
   6558   if (*target == TPM_CAP_ECC_CURVES) {
   6559     return TPM_RC_SUCCESS;
   6560   }
   6561   if (*target == TPM_CAP_LAST) {
   6562     return TPM_RC_SUCCESS;
   6563   }
   6564   if (*target == TPM_CAP_VENDOR_PROPERTY) {
   6565     return TPM_RC_SUCCESS;
   6566   }
   6567   return TPM_RC_VALUE;
   6568 }
   6569 
   6570 UINT16 TPMU_CAPABILITIES_Marshal(TPMU_CAPABILITIES* source,
   6571                                  BYTE** buffer,
   6572                                  INT32* size,
   6573                                  UINT32 selector) {
   6574   switch (selector) {
   6575     case TPM_CAP_ALGS:
   6576       return TPML_ALG_PROPERTY_Marshal((TPML_ALG_PROPERTY*)&source->algorithms,
   6577                                        buffer, size);
   6578     case TPM_CAP_HANDLES:
   6579       return TPML_HANDLE_Marshal((TPML_HANDLE*)&source->handles, buffer, size);
   6580     case TPM_CAP_COMMANDS:
   6581       return TPML_CCA_Marshal((TPML_CCA*)&source->command, buffer, size);
   6582     case TPM_CAP_PP_COMMANDS:
   6583       return TPML_CC_Marshal((TPML_CC*)&source->ppCommands, buffer, size);
   6584     case TPM_CAP_AUDIT_COMMANDS:
   6585       return TPML_CC_Marshal((TPML_CC*)&source->auditCommands, buffer, size);
   6586     case TPM_CAP_PCRS:
   6587       return TPML_PCR_SELECTION_Marshal(
   6588           (TPML_PCR_SELECTION*)&source->assignedPCR, buffer, size);
   6589     case TPM_CAP_TPM_PROPERTIES:
   6590       return TPML_TAGGED_TPM_PROPERTY_Marshal(
   6591           (TPML_TAGGED_TPM_PROPERTY*)&source->tpmProperties, buffer, size);
   6592     case TPM_CAP_PCR_PROPERTIES:
   6593       return TPML_TAGGED_PCR_PROPERTY_Marshal(
   6594           (TPML_TAGGED_PCR_PROPERTY*)&source->pcrProperties, buffer, size);
   6595     case TPM_CAP_ECC_CURVES:
   6596       return TPML_ECC_CURVE_Marshal((TPML_ECC_CURVE*)&source->eccCurves, buffer,
   6597                                     size);
   6598   }
   6599   return 0;
   6600 }
   6601 
   6602 TPM_RC TPMU_CAPABILITIES_Unmarshal(TPMU_CAPABILITIES* target,
   6603                                    BYTE** buffer,
   6604                                    INT32* size,
   6605                                    UINT32 selector) {
   6606   switch (selector) {
   6607     case TPM_CAP_ALGS:
   6608       return TPML_ALG_PROPERTY_Unmarshal(
   6609           (TPML_ALG_PROPERTY*)&target->algorithms, buffer, size);
   6610     case TPM_CAP_HANDLES:
   6611       return TPML_HANDLE_Unmarshal((TPML_HANDLE*)&target->handles, buffer,
   6612                                    size);
   6613     case TPM_CAP_COMMANDS:
   6614       return TPML_CCA_Unmarshal((TPML_CCA*)&target->command, buffer, size);
   6615     case TPM_CAP_PP_COMMANDS:
   6616       return TPML_CC_Unmarshal((TPML_CC*)&target->ppCommands, buffer, size);
   6617     case TPM_CAP_AUDIT_COMMANDS:
   6618       return TPML_CC_Unmarshal((TPML_CC*)&target->auditCommands, buffer, size);
   6619     case TPM_CAP_PCRS:
   6620       return TPML_PCR_SELECTION_Unmarshal(
   6621           (TPML_PCR_SELECTION*)&target->assignedPCR, buffer, size);
   6622     case TPM_CAP_TPM_PROPERTIES:
   6623       return TPML_TAGGED_TPM_PROPERTY_Unmarshal(
   6624           (TPML_TAGGED_TPM_PROPERTY*)&target->tpmProperties, buffer, size);
   6625     case TPM_CAP_PCR_PROPERTIES:
   6626       return TPML_TAGGED_PCR_PROPERTY_Unmarshal(
   6627           (TPML_TAGGED_PCR_PROPERTY*)&target->pcrProperties, buffer, size);
   6628     case TPM_CAP_ECC_CURVES:
   6629       return TPML_ECC_CURVE_Unmarshal((TPML_ECC_CURVE*)&target->eccCurves,
   6630                                       buffer, size);
   6631   }
   6632   return TPM_RC_SELECTOR;
   6633 }
   6634 
   6635 UINT16 TPMS_CAPABILITY_DATA_Marshal(TPMS_CAPABILITY_DATA* source,
   6636                                     BYTE** buffer,
   6637                                     INT32* size) {
   6638   UINT16 total_size = 0;
   6639   total_size += TPM_CAP_Marshal(&source->capability, buffer, size);
   6640   total_size += TPMU_CAPABILITIES_Marshal(&source->data, buffer, size,
   6641                                           source->capability);
   6642   return total_size;
   6643 }
   6644 
   6645 TPM_RC TPMS_CAPABILITY_DATA_Unmarshal(TPMS_CAPABILITY_DATA* target,
   6646                                       BYTE** buffer,
   6647                                       INT32* size) {
   6648   TPM_RC result;
   6649   result = TPM_CAP_Unmarshal(&target->capability, buffer, size);
   6650   if (result != TPM_RC_SUCCESS) {
   6651     return result;
   6652   }
   6653   result = TPMU_CAPABILITIES_Unmarshal(&target->data, buffer, size,
   6654                                        target->capability);
   6655   if (result != TPM_RC_SUCCESS) {
   6656     return result;
   6657   }
   6658   return TPM_RC_SUCCESS;
   6659 }
   6660 
   6661 UINT16 TPMS_CONTEXT_Marshal(TPMS_CONTEXT* source, BYTE** buffer, INT32* size) {
   6662   UINT16 total_size = 0;
   6663   total_size += UINT64_Marshal(&source->sequence, buffer, size);
   6664   total_size += TPMI_DH_CONTEXT_Marshal(&source->savedHandle, buffer, size);
   6665   total_size += TPMI_RH_HIERARCHY_Marshal(&source->hierarchy, buffer, size);
   6666   total_size += TPM2B_CONTEXT_DATA_Marshal(&source->contextBlob, buffer, size);
   6667   return total_size;
   6668 }
   6669 
   6670 TPM_RC TPMS_CONTEXT_Unmarshal(TPMS_CONTEXT* target,
   6671                               BYTE** buffer,
   6672                               INT32* size) {
   6673   TPM_RC result;
   6674   result = UINT64_Unmarshal(&target->sequence, buffer, size);
   6675   if (result != TPM_RC_SUCCESS) {
   6676     return result;
   6677   }
   6678   result = TPMI_DH_CONTEXT_Unmarshal(&target->savedHandle, buffer, size);
   6679   if (result != TPM_RC_SUCCESS) {
   6680     return result;
   6681   }
   6682   result = TPMI_RH_HIERARCHY_Unmarshal(&target->hierarchy, buffer, size, TRUE);
   6683   if (result != TPM_RC_SUCCESS) {
   6684     return result;
   6685   }
   6686   result = TPM2B_CONTEXT_DATA_Unmarshal(&target->contextBlob, buffer, size);
   6687   if (result != TPM_RC_SUCCESS) {
   6688     return result;
   6689   }
   6690   return TPM_RC_SUCCESS;
   6691 }
   6692 
   6693 UINT16 TPMS_CONTEXT_DATA_Marshal(TPMS_CONTEXT_DATA* source,
   6694                                  BYTE** buffer,
   6695                                  INT32* size) {
   6696   UINT16 total_size = 0;
   6697   total_size += TPM2B_DIGEST_Marshal(&source->integrity, buffer, size);
   6698   total_size +=
   6699       TPM2B_CONTEXT_SENSITIVE_Marshal(&source->encrypted, buffer, size);
   6700   return total_size;
   6701 }
   6702 
   6703 TPM_RC TPMS_CONTEXT_DATA_Unmarshal(TPMS_CONTEXT_DATA* target,
   6704                                    BYTE** buffer,
   6705                                    INT32* size) {
   6706   TPM_RC result;
   6707   result = TPM2B_DIGEST_Unmarshal(&target->integrity, buffer, size);
   6708   if (result != TPM_RC_SUCCESS) {
   6709     return result;
   6710   }
   6711   result = TPM2B_CONTEXT_SENSITIVE_Unmarshal(&target->encrypted, buffer, size);
   6712   if (result != TPM_RC_SUCCESS) {
   6713     return result;
   6714   }
   6715   return TPM_RC_SUCCESS;
   6716 }
   6717 
   6718 UINT16 TPMS_PCR_SELECT_Marshal(TPMS_PCR_SELECT* source,
   6719                                BYTE** buffer,
   6720                                INT32* size) {
   6721   UINT16 total_size = 0;
   6722   INT32 i;
   6723   total_size += UINT8_Marshal(&source->sizeofSelect, buffer, size);
   6724   for (i = 0; i < source->sizeofSelect; ++i) {
   6725     total_size += BYTE_Marshal(&source->pcrSelect[i], buffer, size);
   6726   }
   6727   return total_size;
   6728 }
   6729 
   6730 TPM_RC TPMS_PCR_SELECT_Unmarshal(TPMS_PCR_SELECT* target,
   6731                                  BYTE** buffer,
   6732                                  INT32* size) {
   6733   TPM_RC result;
   6734   INT32 i;
   6735   result = UINT8_Unmarshal(&target->sizeofSelect, buffer, size);
   6736   if (result != TPM_RC_SUCCESS) {
   6737     return result;
   6738   }
   6739   if (target->sizeofSelect > PCR_SELECT_MAX) {
   6740     return TPM_RC_VALUE;
   6741   }
   6742   if (target->sizeofSelect < PCR_SELECT_MIN) {
   6743     return TPM_RC_VALUE;
   6744   }
   6745   for (i = 0; i < target->sizeofSelect; ++i) {
   6746     result = BYTE_Unmarshal(&target->pcrSelect[i], buffer, size);
   6747     if (result != TPM_RC_SUCCESS) {
   6748       return result;
   6749     }
   6750   }
   6751   return TPM_RC_SUCCESS;
   6752 }
   6753 
   6754 UINT16 TPMS_SIGNATURE_ECC_Marshal(TPMS_SIGNATURE_ECC* source,
   6755                                   BYTE** buffer,
   6756                                   INT32* size) {
   6757   UINT16 total_size = 0;
   6758   total_size += TPMI_ALG_HASH_Marshal(&source->hash, buffer, size);
   6759   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->signatureR, buffer, size);
   6760   total_size += TPM2B_ECC_PARAMETER_Marshal(&source->signatureS, buffer, size);
   6761   return total_size;
   6762 }
   6763 
   6764 TPM_RC TPMS_SIGNATURE_ECC_Unmarshal(TPMS_SIGNATURE_ECC* target,
   6765                                     BYTE** buffer,
   6766                                     INT32* size) {
   6767   TPM_RC result;
   6768   result = TPMI_ALG_HASH_Unmarshal(&target->hash, buffer, size, FALSE);
   6769   if (result != TPM_RC_SUCCESS) {
   6770     return result;
   6771   }
   6772   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->signatureR, buffer, size);
   6773   if (result != TPM_RC_SUCCESS) {
   6774     return result;
   6775   }
   6776   result = TPM2B_ECC_PARAMETER_Unmarshal(&target->signatureS, buffer, size);
   6777   if (result != TPM_RC_SUCCESS) {
   6778     return result;
   6779   }
   6780   return TPM_RC_SUCCESS;
   6781 }
   6782 
   6783 UINT16 TPMS_SIGNATURE_ECDAA_Marshal(TPMS_SIGNATURE_ECDAA* source,
   6784                                     BYTE** buffer,
   6785                                     INT32* size) {
   6786   return TPMS_SIGNATURE_ECC_Marshal(source, buffer, size);
   6787 }
   6788 
   6789 TPM_RC TPMS_SIGNATURE_ECDAA_Unmarshal(TPMS_SIGNATURE_ECDAA* target,
   6790                                       BYTE** buffer,
   6791                                       INT32* size) {
   6792   return TPMS_SIGNATURE_ECC_Unmarshal(target, buffer, size);
   6793 }
   6794 
   6795 UINT16 TPMS_SIGNATURE_ECDSA_Marshal(TPMS_SIGNATURE_ECDSA* source,
   6796                                     BYTE** buffer,
   6797                                     INT32* size) {
   6798   return TPMS_SIGNATURE_ECC_Marshal(source, buffer, size);
   6799 }
   6800 
   6801 TPM_RC TPMS_SIGNATURE_ECDSA_Unmarshal(TPMS_SIGNATURE_ECDSA* target,
   6802                                       BYTE** buffer,
   6803                                       INT32* size) {
   6804   return TPMS_SIGNATURE_ECC_Unmarshal(target, buffer, size);
   6805 }
   6806 
   6807 UINT16 TPMS_SIGNATURE_ECSCHNORR_Marshal(TPMS_SIGNATURE_ECSCHNORR* source,
   6808                                         BYTE** buffer,
   6809                                         INT32* size) {
   6810   return TPMS_SIGNATURE_ECC_Marshal(source, buffer, size);
   6811 }
   6812 
   6813 TPM_RC TPMS_SIGNATURE_ECSCHNORR_Unmarshal(TPMS_SIGNATURE_ECSCHNORR* target,
   6814                                           BYTE** buffer,
   6815                                           INT32* size) {
   6816   return TPMS_SIGNATURE_ECC_Unmarshal(target, buffer, size);
   6817 }
   6818 
   6819 UINT16 TPMS_SIGNATURE_RSA_Marshal(TPMS_SIGNATURE_RSA* source,
   6820                                   BYTE** buffer,
   6821                                   INT32* size) {
   6822   UINT16 total_size = 0;
   6823   total_size += TPMI_ALG_HASH_Marshal(&source->hash, buffer, size);
   6824   total_size += TPM2B_PUBLIC_KEY_RSA_Marshal(&source->sig, buffer, size);
   6825   return total_size;
   6826 }
   6827 
   6828 TPM_RC TPMS_SIGNATURE_RSA_Unmarshal(TPMS_SIGNATURE_RSA* target,
   6829                                     BYTE** buffer,
   6830                                     INT32* size) {
   6831   TPM_RC result;
   6832   result = TPMI_ALG_HASH_Unmarshal(&target->hash, buffer, size, FALSE);
   6833   if (result != TPM_RC_SUCCESS) {
   6834     return result;
   6835   }
   6836   result = TPM2B_PUBLIC_KEY_RSA_Unmarshal(&target->sig, buffer, size);
   6837   if (result != TPM_RC_SUCCESS) {
   6838     return result;
   6839   }
   6840   return TPM_RC_SUCCESS;
   6841 }
   6842 
   6843 UINT16 TPMS_SIGNATURE_RSAPSS_Marshal(TPMS_SIGNATURE_RSAPSS* source,
   6844                                      BYTE** buffer,
   6845                                      INT32* size) {
   6846   return TPMS_SIGNATURE_RSA_Marshal(source, buffer, size);
   6847 }
   6848 
   6849 TPM_RC TPMS_SIGNATURE_RSAPSS_Unmarshal(TPMS_SIGNATURE_RSAPSS* target,
   6850                                        BYTE** buffer,
   6851                                        INT32* size) {
   6852   return TPMS_SIGNATURE_RSA_Unmarshal(target, buffer, size);
   6853 }
   6854 
   6855 UINT16 TPMS_SIGNATURE_RSASSA_Marshal(TPMS_SIGNATURE_RSASSA* source,
   6856                                      BYTE** buffer,
   6857                                      INT32* size) {
   6858   return TPMS_SIGNATURE_RSA_Marshal(source, buffer, size);
   6859 }
   6860 
   6861 TPM_RC TPMS_SIGNATURE_RSASSA_Unmarshal(TPMS_SIGNATURE_RSASSA* target,
   6862                                        BYTE** buffer,
   6863                                        INT32* size) {
   6864   return TPMS_SIGNATURE_RSA_Unmarshal(target, buffer, size);
   6865 }
   6866 
   6867 UINT16 TPMS_SIGNATURE_SM2_Marshal(TPMS_SIGNATURE_SM2* source,
   6868                                   BYTE** buffer,
   6869                                   INT32* size) {
   6870   return TPMS_SIGNATURE_ECC_Marshal(source, buffer, size);
   6871 }
   6872 
   6873 TPM_RC TPMS_SIGNATURE_SM2_Unmarshal(TPMS_SIGNATURE_SM2* target,
   6874                                     BYTE** buffer,
   6875                                     INT32* size) {
   6876   return TPMS_SIGNATURE_ECC_Unmarshal(target, buffer, size);
   6877 }
   6878 
   6879 UINT16 TPMT_PUBLIC_PARMS_Marshal(TPMT_PUBLIC_PARMS* source,
   6880                                  BYTE** buffer,
   6881                                  INT32* size) {
   6882   UINT16 total_size = 0;
   6883   total_size += TPMI_ALG_PUBLIC_Marshal(&source->type, buffer, size);
   6884   total_size += TPMU_PUBLIC_PARMS_Marshal(&source->parameters, buffer, size,
   6885                                           source->type);
   6886   return total_size;
   6887 }
   6888 
   6889 TPM_RC TPMT_PUBLIC_PARMS_Unmarshal(TPMT_PUBLIC_PARMS* target,
   6890                                    BYTE** buffer,
   6891                                    INT32* size) {
   6892   TPM_RC result;
   6893   result = TPMI_ALG_PUBLIC_Unmarshal(&target->type, buffer, size);
   6894   if (result != TPM_RC_SUCCESS) {
   6895     return result;
   6896   }
   6897   result = TPMU_PUBLIC_PARMS_Unmarshal(&target->parameters, buffer, size,
   6898                                        target->type);
   6899   if (result != TPM_RC_SUCCESS) {
   6900     return result;
   6901   }
   6902   return TPM_RC_SUCCESS;
   6903 }
   6904 
   6905 UINT16 TPMT_RSA_DECRYPT_Marshal(TPMT_RSA_DECRYPT* source,
   6906                                 BYTE** buffer,
   6907                                 INT32* size) {
   6908   UINT16 total_size = 0;
   6909   total_size += TPMI_ALG_RSA_DECRYPT_Marshal(&source->scheme, buffer, size);
   6910   total_size +=
   6911       TPMU_ASYM_SCHEME_Marshal(&source->details, buffer, size, source->scheme);
   6912   return total_size;
   6913 }
   6914 
   6915 TPM_RC TPMT_RSA_DECRYPT_Unmarshal(TPMT_RSA_DECRYPT* target,
   6916                                   BYTE** buffer,
   6917                                   INT32* size) {
   6918   TPM_RC result;
   6919   result = TPMI_ALG_RSA_DECRYPT_Unmarshal(&target->scheme, buffer, size, TRUE);
   6920   if (result != TPM_RC_SUCCESS) {
   6921     return result;
   6922   }
   6923   result = TPMU_ASYM_SCHEME_Unmarshal(&target->details, buffer, size,
   6924                                       target->scheme);
   6925   if (result != TPM_RC_SUCCESS) {
   6926     return result;
   6927   }
   6928   return TPM_RC_SUCCESS;
   6929 }
   6930 
   6931 UINT16 TPMU_SIGNATURE_Marshal(TPMU_SIGNATURE* source,
   6932                               BYTE** buffer,
   6933                               INT32* size,
   6934                               UINT32 selector) {
   6935   switch (selector) {
   6936 #ifdef TPM_ALG_RSASSA
   6937     case TPM_ALG_RSASSA:
   6938       return TPMS_SIGNATURE_RSASSA_Marshal(
   6939           (TPMS_SIGNATURE_RSASSA*)&source->rsassa, buffer, size);
   6940 #endif
   6941 #ifdef TPM_ALG_RSAPSS
   6942     case TPM_ALG_RSAPSS:
   6943       return TPMS_SIGNATURE_RSAPSS_Marshal(
   6944           (TPMS_SIGNATURE_RSAPSS*)&source->rsapss, buffer, size);
   6945 #endif
   6946 #ifdef TPM_ALG_ECDSA
   6947     case TPM_ALG_ECDSA:
   6948       return TPMS_SIGNATURE_ECDSA_Marshal((TPMS_SIGNATURE_ECDSA*)&source->ecdsa,
   6949                                           buffer, size);
   6950 #endif
   6951 #ifdef TPM_ALG_ECDAA
   6952     case TPM_ALG_ECDAA:
   6953       return TPMS_SIGNATURE_ECDAA_Marshal((TPMS_SIGNATURE_ECDAA*)&source->ecdaa,
   6954                                           buffer, size);
   6955 #endif
   6956 #ifdef TPM_ALG_SM2
   6957     case TPM_ALG_SM2:
   6958       return TPMS_SIGNATURE_SM2_Marshal((TPMS_SIGNATURE_SM2*)&source->sm2,
   6959                                         buffer, size);
   6960 #endif
   6961 #ifdef TPM_ALG_ECSCHNORR
   6962     case TPM_ALG_ECSCHNORR:
   6963       return TPMS_SIGNATURE_ECSCHNORR_Marshal(
   6964           (TPMS_SIGNATURE_ECSCHNORR*)&source->ecschnorr, buffer, size);
   6965 #endif
   6966 #ifdef TPM_ALG_HMAC
   6967     case TPM_ALG_HMAC:
   6968       return TPMT_HA_Marshal((TPMT_HA*)&source->hmac, buffer, size);
   6969 #endif
   6970 #ifdef TPM_ALG_NULL
   6971     case TPM_ALG_NULL:
   6972       return 0;
   6973 #endif
   6974   }
   6975   return 0;
   6976 }
   6977 
   6978 TPM_RC TPMU_SIGNATURE_Unmarshal(TPMU_SIGNATURE* target,
   6979                                 BYTE** buffer,
   6980                                 INT32* size,
   6981                                 UINT32 selector) {
   6982   switch (selector) {
   6983 #ifdef TPM_ALG_RSASSA
   6984     case TPM_ALG_RSASSA:
   6985       return TPMS_SIGNATURE_RSASSA_Unmarshal(
   6986           (TPMS_SIGNATURE_RSASSA*)&target->rsassa, buffer, size);
   6987 #endif
   6988 #ifdef TPM_ALG_RSAPSS
   6989     case TPM_ALG_RSAPSS:
   6990       return TPMS_SIGNATURE_RSAPSS_Unmarshal(
   6991           (TPMS_SIGNATURE_RSAPSS*)&target->rsapss, buffer, size);
   6992 #endif
   6993 #ifdef TPM_ALG_ECDSA
   6994     case TPM_ALG_ECDSA:
   6995       return TPMS_SIGNATURE_ECDSA_Unmarshal(
   6996           (TPMS_SIGNATURE_ECDSA*)&target->ecdsa, buffer, size);
   6997 #endif
   6998 #ifdef TPM_ALG_ECDAA
   6999     case TPM_ALG_ECDAA:
   7000       return TPMS_SIGNATURE_ECDAA_Unmarshal(
   7001           (TPMS_SIGNATURE_ECDAA*)&target->ecdaa, buffer, size);
   7002 #endif
   7003 #ifdef TPM_ALG_SM2
   7004     case TPM_ALG_SM2:
   7005       return TPMS_SIGNATURE_SM2_Unmarshal((TPMS_SIGNATURE_SM2*)&target->sm2,
   7006                                           buffer, size);
   7007 #endif
   7008 #ifdef TPM_ALG_ECSCHNORR
   7009     case TPM_ALG_ECSCHNORR:
   7010       return TPMS_SIGNATURE_ECSCHNORR_Unmarshal(
   7011           (TPMS_SIGNATURE_ECSCHNORR*)&target->ecschnorr, buffer, size);
   7012 #endif
   7013 #ifdef TPM_ALG_HMAC
   7014     case TPM_ALG_HMAC:
   7015       return TPMT_HA_Unmarshal((TPMT_HA*)&target->hmac, buffer, size);
   7016 #endif
   7017 #ifdef TPM_ALG_NULL
   7018     case TPM_ALG_NULL:
   7019       return TPM_RC_SUCCESS;
   7020 #endif
   7021   }
   7022   return TPM_RC_SELECTOR;
   7023 }
   7024 
   7025 UINT16 TPMT_SIGNATURE_Marshal(TPMT_SIGNATURE* source,
   7026                               BYTE** buffer,
   7027                               INT32* size) {
   7028   UINT16 total_size = 0;
   7029   total_size += TPMI_ALG_SIG_SCHEME_Marshal(&source->sigAlg, buffer, size);
   7030   total_size +=
   7031       TPMU_SIGNATURE_Marshal(&source->signature, buffer, size, source->sigAlg);
   7032   return total_size;
   7033 }
   7034 
   7035 TPM_RC TPMT_SIGNATURE_Unmarshal(TPMT_SIGNATURE* target,
   7036                                 BYTE** buffer,
   7037                                 INT32* size) {
   7038   TPM_RC result;
   7039   result = TPMI_ALG_SIG_SCHEME_Unmarshal(&target->sigAlg, buffer, size, TRUE);
   7040   if (result != TPM_RC_SUCCESS) {
   7041     return result;
   7042   }
   7043   result = TPMU_SIGNATURE_Unmarshal(&target->signature, buffer, size,
   7044                                     target->sigAlg);
   7045   if (result != TPM_RC_SUCCESS) {
   7046     return result;
   7047   }
   7048   return TPM_RC_SUCCESS;
   7049 }
   7050 
   7051 UINT16 TPMU_SIG_SCHEME_Marshal(TPMU_SIG_SCHEME* source,
   7052                                BYTE** buffer,
   7053                                INT32* size,
   7054                                UINT32 selector) {
   7055   switch (selector) {
   7056 #ifdef TPM_ALG_RSASSA
   7057     case TPM_ALG_RSASSA:
   7058       return TPMS_SIG_SCHEME_RSASSA_Marshal(
   7059           (TPMS_SIG_SCHEME_RSASSA*)&source->rsassa, buffer, size);
   7060 #endif
   7061 #ifdef TPM_ALG_RSAPSS
   7062     case TPM_ALG_RSAPSS:
   7063       return TPMS_SIG_SCHEME_RSAPSS_Marshal(
   7064           (TPMS_SIG_SCHEME_RSAPSS*)&source->rsapss, buffer, size);
   7065 #endif
   7066 #ifdef TPM_ALG_ECDSA
   7067     case TPM_ALG_ECDSA:
   7068       return TPMS_SIG_SCHEME_ECDSA_Marshal(
   7069           (TPMS_SIG_SCHEME_ECDSA*)&source->ecdsa, buffer, size);
   7070 #endif
   7071 #ifdef TPM_ALG_ECDAA
   7072     case TPM_ALG_ECDAA:
   7073       return TPMS_SIG_SCHEME_ECDAA_Marshal(
   7074           (TPMS_SIG_SCHEME_ECDAA*)&source->ecdaa, buffer, size);
   7075 #endif
   7076 #ifdef TPM_ALG_SM2
   7077     case TPM_ALG_SM2:
   7078       return TPMS_SIG_SCHEME_SM2_Marshal((TPMS_SIG_SCHEME_SM2*)&source->sm2,
   7079                                          buffer, size);
   7080 #endif
   7081 #ifdef TPM_ALG_ECSCHNORR
   7082     case TPM_ALG_ECSCHNORR:
   7083       return TPMS_SIG_SCHEME_ECSCHNORR_Marshal(
   7084           (TPMS_SIG_SCHEME_ECSCHNORR*)&source->ecschnorr, buffer, size);
   7085 #endif
   7086 #ifdef TPM_ALG_HMAC
   7087     case TPM_ALG_HMAC:
   7088       return TPMS_SCHEME_HMAC_Marshal((TPMS_SCHEME_HMAC*)&source->hmac, buffer,
   7089                                       size);
   7090 #endif
   7091 #ifdef TPM_ALG_NULL
   7092     case TPM_ALG_NULL:
   7093       return 0;
   7094 #endif
   7095   }
   7096   return 0;
   7097 }
   7098 
   7099 TPM_RC TPMU_SIG_SCHEME_Unmarshal(TPMU_SIG_SCHEME* target,
   7100                                  BYTE** buffer,
   7101                                  INT32* size,
   7102                                  UINT32 selector) {
   7103   switch (selector) {
   7104 #ifdef TPM_ALG_RSASSA
   7105     case TPM_ALG_RSASSA:
   7106       return TPMS_SIG_SCHEME_RSASSA_Unmarshal(
   7107           (TPMS_SIG_SCHEME_RSASSA*)&target->rsassa, buffer, size);
   7108 #endif
   7109 #ifdef TPM_ALG_RSAPSS
   7110     case TPM_ALG_RSAPSS:
   7111       return TPMS_SIG_SCHEME_RSAPSS_Unmarshal(
   7112           (TPMS_SIG_SCHEME_RSAPSS*)&target->rsapss, buffer, size);
   7113 #endif
   7114 #ifdef TPM_ALG_ECDSA
   7115     case TPM_ALG_ECDSA:
   7116       return TPMS_SIG_SCHEME_ECDSA_Unmarshal(
   7117           (TPMS_SIG_SCHEME_ECDSA*)&target->ecdsa, buffer, size);
   7118 #endif
   7119 #ifdef TPM_ALG_ECDAA
   7120     case TPM_ALG_ECDAA:
   7121       return TPMS_SIG_SCHEME_ECDAA_Unmarshal(
   7122           (TPMS_SIG_SCHEME_ECDAA*)&target->ecdaa, buffer, size);
   7123 #endif
   7124 #ifdef TPM_ALG_SM2
   7125     case TPM_ALG_SM2:
   7126       return TPMS_SIG_SCHEME_SM2_Unmarshal((TPMS_SIG_SCHEME_SM2*)&target->sm2,
   7127                                            buffer, size);
   7128 #endif
   7129 #ifdef TPM_ALG_ECSCHNORR
   7130     case TPM_ALG_ECSCHNORR:
   7131       return TPMS_SIG_SCHEME_ECSCHNORR_Unmarshal(
   7132           (TPMS_SIG_SCHEME_ECSCHNORR*)&target->ecschnorr, buffer, size);
   7133 #endif
   7134 #ifdef TPM_ALG_HMAC
   7135     case TPM_ALG_HMAC:
   7136       return TPMS_SCHEME_HMAC_Unmarshal((TPMS_SCHEME_HMAC*)&target->hmac,
   7137                                         buffer, size);
   7138 #endif
   7139 #ifdef TPM_ALG_NULL
   7140     case TPM_ALG_NULL:
   7141       return TPM_RC_SUCCESS;
   7142 #endif
   7143   }
   7144   return TPM_RC_SELECTOR;
   7145 }
   7146 
   7147 UINT16 TPMT_SIG_SCHEME_Marshal(TPMT_SIG_SCHEME* source,
   7148                                BYTE** buffer,
   7149                                INT32* size) {
   7150   UINT16 total_size = 0;
   7151   total_size += TPMI_ALG_SIG_SCHEME_Marshal(&source->scheme, buffer, size);
   7152   total_size +=
   7153       TPMU_SIG_SCHEME_Marshal(&source->details, buffer, size, source->scheme);
   7154   return total_size;
   7155 }
   7156 
   7157 TPM_RC TPMT_SIG_SCHEME_Unmarshal(TPMT_SIG_SCHEME* target,
   7158                                  BYTE** buffer,
   7159                                  INT32* size) {
   7160   TPM_RC result;
   7161   result = TPMI_ALG_SIG_SCHEME_Unmarshal(&target->scheme, buffer, size, TRUE);
   7162   if (result != TPM_RC_SUCCESS) {
   7163     return result;
   7164   }
   7165   result =
   7166       TPMU_SIG_SCHEME_Unmarshal(&target->details, buffer, size, target->scheme);
   7167   if (result != TPM_RC_SUCCESS) {
   7168     return result;
   7169   }
   7170   return TPM_RC_SUCCESS;
   7171 }
   7172 
   7173 UINT16 TPMT_SYM_DEF_Marshal(TPMT_SYM_DEF* source, BYTE** buffer, INT32* size) {
   7174   UINT16 total_size = 0;
   7175   total_size += TPMI_ALG_SYM_Marshal(&source->algorithm, buffer, size);
   7176   total_size += TPMU_SYM_KEY_BITS_Marshal(&source->keyBits, buffer, size,
   7177                                           source->algorithm);
   7178   total_size +=
   7179       TPMU_SYM_MODE_Marshal(&source->mode, buffer, size, source->algorithm);
   7180   return total_size;
   7181 }
   7182 
   7183 TPM_RC TPMT_SYM_DEF_Unmarshal(TPMT_SYM_DEF* target,
   7184                               BYTE** buffer,
   7185                               INT32* size) {
   7186   TPM_RC result;
   7187   result = TPMI_ALG_SYM_Unmarshal(&target->algorithm, buffer, size, TRUE);
   7188   if (result != TPM_RC_SUCCESS) {
   7189     return result;
   7190   }
   7191   result = TPMU_SYM_KEY_BITS_Unmarshal(&target->keyBits, buffer, size,
   7192                                        target->algorithm);
   7193   if (result != TPM_RC_SUCCESS) {
   7194     return result;
   7195   }
   7196   result =
   7197       TPMU_SYM_MODE_Unmarshal(&target->mode, buffer, size, target->algorithm);
   7198   if (result != TPM_RC_SUCCESS) {
   7199     return result;
   7200   }
   7201   return TPM_RC_SUCCESS;
   7202 }
   7203 
   7204 UINT16 TPM_ST_Marshal(TPM_ST* source, BYTE** buffer, INT32* size) {
   7205   return uint16_t_Marshal(source, buffer, size);
   7206 }
   7207 
   7208 TPM_RC TPM_ST_Unmarshal(TPM_ST* target, BYTE** buffer, INT32* size) {
   7209   TPM_RC result;
   7210   result = uint16_t_Unmarshal(target, buffer, size);
   7211   if (result != TPM_RC_SUCCESS) {
   7212     return result;
   7213   }
   7214   if (*target == TPM_ST_RSP_COMMAND) {
   7215     return TPM_RC_SUCCESS;
   7216   }
   7217   if (*target == TPM_ST_NULL) {
   7218     return TPM_RC_SUCCESS;
   7219   }
   7220   if (*target == TPM_ST_NO_SESSIONS) {
   7221     return TPM_RC_SUCCESS;
   7222   }
   7223   if (*target == TPM_ST_SESSIONS) {
   7224     return TPM_RC_SUCCESS;
   7225   }
   7226   if (*target == TPM_ST_ATTEST_NV) {
   7227     return TPM_RC_SUCCESS;
   7228   }
   7229   if (*target == TPM_ST_ATTEST_COMMAND_AUDIT) {
   7230     return TPM_RC_SUCCESS;
   7231   }
   7232   if (*target == TPM_ST_ATTEST_SESSION_AUDIT) {
   7233     return TPM_RC_SUCCESS;
   7234   }
   7235   if (*target == TPM_ST_ATTEST_CERTIFY) {
   7236     return TPM_RC_SUCCESS;
   7237   }
   7238   if (*target == TPM_ST_ATTEST_QUOTE) {
   7239     return TPM_RC_SUCCESS;
   7240   }
   7241   if (*target == TPM_ST_ATTEST_TIME) {
   7242     return TPM_RC_SUCCESS;
   7243   }
   7244   if (*target == TPM_ST_ATTEST_CREATION) {
   7245     return TPM_RC_SUCCESS;
   7246   }
   7247   if (*target == TPM_ST_CREATION) {
   7248     return TPM_RC_SUCCESS;
   7249   }
   7250   if (*target == TPM_ST_VERIFIED) {
   7251     return TPM_RC_SUCCESS;
   7252   }
   7253   if (*target == TPM_ST_AUTH_SECRET) {
   7254     return TPM_RC_SUCCESS;
   7255   }
   7256   if (*target == TPM_ST_HASHCHECK) {
   7257     return TPM_RC_SUCCESS;
   7258   }
   7259   if (*target == TPM_ST_AUTH_SIGNED) {
   7260     return TPM_RC_SUCCESS;
   7261   }
   7262   if (*target == TPM_ST_FU_MANIFEST) {
   7263     return TPM_RC_SUCCESS;
   7264   }
   7265   return TPM_RC_VALUE;
   7266 }
   7267 
   7268 UINT16 TPMT_TK_AUTH_Marshal(TPMT_TK_AUTH* source, BYTE** buffer, INT32* size) {
   7269   UINT16 total_size = 0;
   7270   total_size += TPM_ST_Marshal(&source->tag, buffer, size);
   7271   total_size += TPMI_RH_HIERARCHY_Marshal(&source->hierarchy, buffer, size);
   7272   total_size += TPM2B_DIGEST_Marshal(&source->digest, buffer, size);
   7273   return total_size;
   7274 }
   7275 
   7276 TPM_RC TPMT_TK_AUTH_Unmarshal(TPMT_TK_AUTH* target,
   7277                               BYTE** buffer,
   7278                               INT32* size) {
   7279   TPM_RC result;
   7280   result = TPM_ST_Unmarshal(&target->tag, buffer, size);
   7281   if (result != TPM_RC_SUCCESS) {
   7282     return result;
   7283   }
   7284   result = TPMI_RH_HIERARCHY_Unmarshal(&target->hierarchy, buffer, size, TRUE);
   7285   if (result != TPM_RC_SUCCESS) {
   7286     return result;
   7287   }
   7288   result = TPM2B_DIGEST_Unmarshal(&target->digest, buffer, size);
   7289   if (result != TPM_RC_SUCCESS) {
   7290     return result;
   7291   }
   7292   return TPM_RC_SUCCESS;
   7293 }
   7294 
   7295 UINT16 TPMT_TK_CREATION_Marshal(TPMT_TK_CREATION* source,
   7296                                 BYTE** buffer,
   7297                                 INT32* size) {
   7298   UINT16 total_size = 0;
   7299   total_size += TPM_ST_Marshal(&source->tag, buffer, size);
   7300   total_size += TPMI_RH_HIERARCHY_Marshal(&source->hierarchy, buffer, size);
   7301   total_size += TPM2B_DIGEST_Marshal(&source->digest, buffer, size);
   7302   return total_size;
   7303 }
   7304 
   7305 TPM_RC TPMT_TK_CREATION_Unmarshal(TPMT_TK_CREATION* target,
   7306                                   BYTE** buffer,
   7307                                   INT32* size) {
   7308   TPM_RC result;
   7309   result = TPM_ST_Unmarshal(&target->tag, buffer, size);
   7310   if (result != TPM_RC_SUCCESS) {
   7311     return result;
   7312   }
   7313   result = TPMI_RH_HIERARCHY_Unmarshal(&target->hierarchy, buffer, size, TRUE);
   7314   if (result != TPM_RC_SUCCESS) {
   7315     return result;
   7316   }
   7317   result = TPM2B_DIGEST_Unmarshal(&target->digest, buffer, size);
   7318   if (result != TPM_RC_SUCCESS) {
   7319     return result;
   7320   }
   7321   return TPM_RC_SUCCESS;
   7322 }
   7323 
   7324 UINT16 TPMT_TK_HASHCHECK_Marshal(TPMT_TK_HASHCHECK* source,
   7325                                  BYTE** buffer,
   7326                                  INT32* size) {
   7327   UINT16 total_size = 0;
   7328   total_size += TPM_ST_Marshal(&source->tag, buffer, size);
   7329   total_size += TPMI_RH_HIERARCHY_Marshal(&source->hierarchy, buffer, size);
   7330   total_size += TPM2B_DIGEST_Marshal(&source->digest, buffer, size);
   7331   return total_size;
   7332 }
   7333 
   7334 TPM_RC TPMT_TK_HASHCHECK_Unmarshal(TPMT_TK_HASHCHECK* target,
   7335                                    BYTE** buffer,
   7336                                    INT32* size) {
   7337   TPM_RC result;
   7338   result = TPM_ST_Unmarshal(&target->tag, buffer, size);
   7339   if (result != TPM_RC_SUCCESS) {
   7340     return result;
   7341   }
   7342   result = TPMI_RH_HIERARCHY_Unmarshal(&target->hierarchy, buffer, size, TRUE);
   7343   if (result != TPM_RC_SUCCESS) {
   7344     return result;
   7345   }
   7346   result = TPM2B_DIGEST_Unmarshal(&target->digest, buffer, size);
   7347   if (result != TPM_RC_SUCCESS) {
   7348     return result;
   7349   }
   7350   return TPM_RC_SUCCESS;
   7351 }
   7352 
   7353 UINT16 TPMT_TK_VERIFIED_Marshal(TPMT_TK_VERIFIED* source,
   7354                                 BYTE** buffer,
   7355                                 INT32* size) {
   7356   UINT16 total_size = 0;
   7357   total_size += TPM_ST_Marshal(&source->tag, buffer, size);
   7358   total_size += TPMI_RH_HIERARCHY_Marshal(&source->hierarchy, buffer, size);
   7359   total_size += TPM2B_DIGEST_Marshal(&source->digest, buffer, size);
   7360   return total_size;
   7361 }
   7362 
   7363 TPM_RC TPMT_TK_VERIFIED_Unmarshal(TPMT_TK_VERIFIED* target,
   7364                                   BYTE** buffer,
   7365                                   INT32* size) {
   7366   TPM_RC result;
   7367   result = TPM_ST_Unmarshal(&target->tag, buffer, size);
   7368   if (result != TPM_RC_SUCCESS) {
   7369     return result;
   7370   }
   7371   result = TPMI_RH_HIERARCHY_Unmarshal(&target->hierarchy, buffer, size, TRUE);
   7372   if (result != TPM_RC_SUCCESS) {
   7373     return result;
   7374   }
   7375   result = TPM2B_DIGEST_Unmarshal(&target->digest, buffer, size);
   7376   if (result != TPM_RC_SUCCESS) {
   7377     return result;
   7378   }
   7379   return TPM_RC_SUCCESS;
   7380 }
   7381 
   7382 UINT16 TPMU_SYM_DETAILS_Marshal(TPMU_SYM_DETAILS* source,
   7383                                 BYTE** buffer,
   7384                                 INT32* size,
   7385                                 UINT32 selector) {
   7386   switch (selector) {
   7387 #ifdef TPM_ALG_AES
   7388     case TPM_ALG_AES:
   7389       return 0;
   7390 #endif
   7391 #ifdef TPM_ALG_SM4
   7392     case TPM_ALG_SM4:
   7393       return 0;
   7394 #endif
   7395 #ifdef TPM_ALG_CAMELLIA
   7396     case TPM_ALG_CAMELLIA:
   7397       return 0;
   7398 #endif
   7399 #ifdef TPM_ALG_XOR
   7400     case TPM_ALG_XOR:
   7401       return 0;
   7402 #endif
   7403 #ifdef TPM_ALG_NULL
   7404     case TPM_ALG_NULL:
   7405       return 0;
   7406 #endif
   7407   }
   7408   return 0;
   7409 }
   7410 
   7411 TPM_RC TPMU_SYM_DETAILS_Unmarshal(TPMU_SYM_DETAILS* target,
   7412                                   BYTE** buffer,
   7413                                   INT32* size,
   7414                                   UINT32 selector) {
   7415   switch (selector) {
   7416 #ifdef TPM_ALG_AES
   7417     case TPM_ALG_AES:
   7418       return TPM_RC_SUCCESS;
   7419 #endif
   7420 #ifdef TPM_ALG_SM4
   7421     case TPM_ALG_SM4:
   7422       return TPM_RC_SUCCESS;
   7423 #endif
   7424 #ifdef TPM_ALG_CAMELLIA
   7425     case TPM_ALG_CAMELLIA:
   7426       return TPM_RC_SUCCESS;
   7427 #endif
   7428 #ifdef TPM_ALG_XOR
   7429     case TPM_ALG_XOR:
   7430       return TPM_RC_SUCCESS;
   7431 #endif
   7432 #ifdef TPM_ALG_NULL
   7433     case TPM_ALG_NULL:
   7434       return TPM_RC_SUCCESS;
   7435 #endif
   7436   }
   7437   return TPM_RC_SELECTOR;
   7438 }
   7439 
   7440 UINT16 TPM_ALGORITHM_ID_Marshal(TPM_ALGORITHM_ID* source,
   7441                                 BYTE** buffer,
   7442                                 INT32* size) {
   7443   return uint32_t_Marshal(source, buffer, size);
   7444 }
   7445 
   7446 TPM_RC TPM_ALGORITHM_ID_Unmarshal(TPM_ALGORITHM_ID* target,
   7447                                   BYTE** buffer,
   7448                                   INT32* size) {
   7449   return uint32_t_Unmarshal(target, buffer, size);
   7450 }
   7451 
   7452 UINT16 TPM_AUTHORIZATION_SIZE_Marshal(TPM_AUTHORIZATION_SIZE* source,
   7453                                       BYTE** buffer,
   7454                                       INT32* size) {
   7455   return uint32_t_Marshal(source, buffer, size);
   7456 }
   7457 
   7458 TPM_RC TPM_AUTHORIZATION_SIZE_Unmarshal(TPM_AUTHORIZATION_SIZE* target,
   7459                                         BYTE** buffer,
   7460                                         INT32* size) {
   7461   return uint32_t_Unmarshal(target, buffer, size);
   7462 }
   7463 
   7464 UINT16 TPM_CLOCK_ADJUST_Marshal(TPM_CLOCK_ADJUST* source,
   7465                                 BYTE** buffer,
   7466                                 INT32* size) {
   7467   return int8_t_Marshal(source, buffer, size);
   7468 }
   7469 
   7470 TPM_RC TPM_CLOCK_ADJUST_Unmarshal(TPM_CLOCK_ADJUST* target,
   7471                                   BYTE** buffer,
   7472                                   INT32* size) {
   7473   TPM_RC result;
   7474   result = int8_t_Unmarshal(target, buffer, size);
   7475   if (result != TPM_RC_SUCCESS) {
   7476     return result;
   7477   }
   7478   if (*target == TPM_CLOCK_COARSE_SLOWER) {
   7479     return TPM_RC_SUCCESS;
   7480   }
   7481   if (*target == TPM_CLOCK_MEDIUM_SLOWER) {
   7482     return TPM_RC_SUCCESS;
   7483   }
   7484   if (*target == TPM_CLOCK_FINE_SLOWER) {
   7485     return TPM_RC_SUCCESS;
   7486   }
   7487   if (*target == TPM_CLOCK_NO_CHANGE) {
   7488     return TPM_RC_SUCCESS;
   7489   }
   7490   if (*target == TPM_CLOCK_FINE_FASTER) {
   7491     return TPM_RC_SUCCESS;
   7492   }
   7493   if (*target == TPM_CLOCK_MEDIUM_FASTER) {
   7494     return TPM_RC_SUCCESS;
   7495   }
   7496   if (*target == TPM_CLOCK_COARSE_FASTER) {
   7497     return TPM_RC_SUCCESS;
   7498   }
   7499   return TPM_RC_VALUE;
   7500 }
   7501 
   7502 UINT16 TPM_EO_Marshal(TPM_EO* source, BYTE** buffer, INT32* size) {
   7503   return uint16_t_Marshal(source, buffer, size);
   7504 }
   7505 
   7506 TPM_RC TPM_EO_Unmarshal(TPM_EO* target, BYTE** buffer, INT32* size) {
   7507   TPM_RC result;
   7508   result = uint16_t_Unmarshal(target, buffer, size);
   7509   if (result != TPM_RC_SUCCESS) {
   7510     return result;
   7511   }
   7512   if (*target == TPM_EO_EQ) {
   7513     return TPM_RC_SUCCESS;
   7514   }
   7515   if (*target == TPM_EO_NEQ) {
   7516     return TPM_RC_SUCCESS;
   7517   }
   7518   if (*target == TPM_EO_SIGNED_GT) {
   7519     return TPM_RC_SUCCESS;
   7520   }
   7521   if (*target == TPM_EO_UNSIGNED_GT) {
   7522     return TPM_RC_SUCCESS;
   7523   }
   7524   if (*target == TPM_EO_SIGNED_LT) {
   7525     return TPM_RC_SUCCESS;
   7526   }
   7527   if (*target == TPM_EO_UNSIGNED_LT) {
   7528     return TPM_RC_SUCCESS;
   7529   }
   7530   if (*target == TPM_EO_SIGNED_GE) {
   7531     return TPM_RC_SUCCESS;
   7532   }
   7533   if (*target == TPM_EO_UNSIGNED_GE) {
   7534     return TPM_RC_SUCCESS;
   7535   }
   7536   if (*target == TPM_EO_SIGNED_LE) {
   7537     return TPM_RC_SUCCESS;
   7538   }
   7539   if (*target == TPM_EO_UNSIGNED_LE) {
   7540     return TPM_RC_SUCCESS;
   7541   }
   7542   if (*target == TPM_EO_BITSET) {
   7543     return TPM_RC_SUCCESS;
   7544   }
   7545   if (*target == TPM_EO_BITCLEAR) {
   7546     return TPM_RC_SUCCESS;
   7547   }
   7548   return TPM_RC_VALUE;
   7549 }
   7550 
   7551 UINT16 TPM_HC_Marshal(TPM_HC* source, BYTE** buffer, INT32* size) {
   7552   return uint32_t_Marshal(source, buffer, size);
   7553 }
   7554 
   7555 TPM_RC TPM_HC_Unmarshal(TPM_HC* target, BYTE** buffer, INT32* size) {
   7556   TPM_RC result;
   7557   result = uint32_t_Unmarshal(target, buffer, size);
   7558   if (result != TPM_RC_SUCCESS) {
   7559     return result;
   7560   }
   7561   if (*target == HR_HANDLE_MASK) {
   7562     return TPM_RC_SUCCESS;
   7563   }
   7564   if (*target == HR_RANGE_MASK) {
   7565     return TPM_RC_SUCCESS;
   7566   }
   7567   if (*target == HR_SHIFT) {
   7568     return TPM_RC_SUCCESS;
   7569   }
   7570   if (*target == HR_PCR) {
   7571     return TPM_RC_SUCCESS;
   7572   }
   7573   if (*target == HR_HMAC_SESSION) {
   7574     return TPM_RC_SUCCESS;
   7575   }
   7576   if (*target == HR_POLICY_SESSION) {
   7577     return TPM_RC_SUCCESS;
   7578   }
   7579   if (*target == HR_TRANSIENT) {
   7580     return TPM_RC_SUCCESS;
   7581   }
   7582   if (*target == HR_PERSISTENT) {
   7583     return TPM_RC_SUCCESS;
   7584   }
   7585   if (*target == HR_NV_INDEX) {
   7586     return TPM_RC_SUCCESS;
   7587   }
   7588   if (*target == HR_PERMANENT) {
   7589     return TPM_RC_SUCCESS;
   7590   }
   7591   if (*target == PCR_FIRST) {
   7592     return TPM_RC_SUCCESS;
   7593   }
   7594   if (*target == PCR_LAST) {
   7595     return TPM_RC_SUCCESS;
   7596   }
   7597   if (*target == HMAC_SESSION_FIRST) {
   7598     return TPM_RC_SUCCESS;
   7599   }
   7600   if (*target == HMAC_SESSION_LAST) {
   7601     return TPM_RC_SUCCESS;
   7602   }
   7603   if (*target == LOADED_SESSION_FIRST) {
   7604     return TPM_RC_SUCCESS;
   7605   }
   7606   if (*target == LOADED_SESSION_LAST) {
   7607     return TPM_RC_SUCCESS;
   7608   }
   7609   if (*target == POLICY_SESSION_FIRST) {
   7610     return TPM_RC_SUCCESS;
   7611   }
   7612   if (*target == POLICY_SESSION_LAST) {
   7613     return TPM_RC_SUCCESS;
   7614   }
   7615   if (*target == TRANSIENT_FIRST) {
   7616     return TPM_RC_SUCCESS;
   7617   }
   7618   if (*target == ACTIVE_SESSION_FIRST) {
   7619     return TPM_RC_SUCCESS;
   7620   }
   7621   if (*target == ACTIVE_SESSION_LAST) {
   7622     return TPM_RC_SUCCESS;
   7623   }
   7624   if (*target == TRANSIENT_LAST) {
   7625     return TPM_RC_SUCCESS;
   7626   }
   7627   if (*target == PERSISTENT_FIRST) {
   7628     return TPM_RC_SUCCESS;
   7629   }
   7630   if (*target == PERSISTENT_LAST) {
   7631     return TPM_RC_SUCCESS;
   7632   }
   7633   if (*target == PLATFORM_PERSISTENT) {
   7634     return TPM_RC_SUCCESS;
   7635   }
   7636   if (*target == NV_INDEX_FIRST) {
   7637     return TPM_RC_SUCCESS;
   7638   }
   7639   if (*target == NV_INDEX_LAST) {
   7640     return TPM_RC_SUCCESS;
   7641   }
   7642   if (*target == PERMANENT_FIRST) {
   7643     return TPM_RC_SUCCESS;
   7644   }
   7645   if (*target == PERMANENT_LAST) {
   7646     return TPM_RC_SUCCESS;
   7647   }
   7648   return TPM_RC_VALUE;
   7649 }
   7650 
   7651 UINT16 TPM_HT_Marshal(TPM_HT* source, BYTE** buffer, INT32* size) {
   7652   return uint8_t_Marshal(source, buffer, size);
   7653 }
   7654 
   7655 TPM_RC TPM_HT_Unmarshal(TPM_HT* target, BYTE** buffer, INT32* size) {
   7656   TPM_RC result;
   7657   result = uint8_t_Unmarshal(target, buffer, size);
   7658   if (result != TPM_RC_SUCCESS) {
   7659     return result;
   7660   }
   7661   if (*target == TPM_HT_PCR) {
   7662     return TPM_RC_SUCCESS;
   7663   }
   7664   if (*target == TPM_HT_NV_INDEX) {
   7665     return TPM_RC_SUCCESS;
   7666   }
   7667   if (*target == TPM_HT_HMAC_SESSION) {
   7668     return TPM_RC_SUCCESS;
   7669   }
   7670   if (*target == TPM_HT_LOADED_SESSION) {
   7671     return TPM_RC_SUCCESS;
   7672   }
   7673   if (*target == TPM_HT_POLICY_SESSION) {
   7674     return TPM_RC_SUCCESS;
   7675   }
   7676   if (*target == TPM_HT_ACTIVE_SESSION) {
   7677     return TPM_RC_SUCCESS;
   7678   }
   7679   if (*target == TPM_HT_PERMANENT) {
   7680     return TPM_RC_SUCCESS;
   7681   }
   7682   if (*target == TPM_HT_TRANSIENT) {
   7683     return TPM_RC_SUCCESS;
   7684   }
   7685   if (*target == TPM_HT_PERSISTENT) {
   7686     return TPM_RC_SUCCESS;
   7687   }
   7688   return TPM_RC_VALUE;
   7689 }
   7690 
   7691 UINT16 TPM_KEY_SIZE_Marshal(TPM_KEY_SIZE* source, BYTE** buffer, INT32* size) {
   7692   return uint16_t_Marshal(source, buffer, size);
   7693 }
   7694 
   7695 TPM_RC TPM_KEY_SIZE_Unmarshal(TPM_KEY_SIZE* target,
   7696                               BYTE** buffer,
   7697                               INT32* size) {
   7698   return uint16_t_Unmarshal(target, buffer, size);
   7699 }
   7700 
   7701 UINT16 TPM_MODIFIER_INDICATOR_Marshal(TPM_MODIFIER_INDICATOR* source,
   7702                                       BYTE** buffer,
   7703                                       INT32* size) {
   7704   return uint32_t_Marshal(source, buffer, size);
   7705 }
   7706 
   7707 TPM_RC TPM_MODIFIER_INDICATOR_Unmarshal(TPM_MODIFIER_INDICATOR* target,
   7708                                         BYTE** buffer,
   7709                                         INT32* size) {
   7710   return uint32_t_Unmarshal(target, buffer, size);
   7711 }
   7712 
   7713 UINT16 TPM_NV_INDEX_Marshal(TPM_NV_INDEX* source, BYTE** buffer, INT32* size) {
   7714   return uint32_t_Marshal((uint32_t*)source, buffer, size);
   7715 }
   7716 
   7717 TPM_RC TPM_NV_INDEX_Unmarshal(TPM_NV_INDEX* target,
   7718                               BYTE** buffer,
   7719                               INT32* size) {
   7720   TPM_RC result;
   7721   result = uint32_t_Unmarshal((uint32_t*)target, buffer, size);
   7722   if (result != TPM_RC_SUCCESS) {
   7723     return result;
   7724   }
   7725   return TPM_RC_SUCCESS;
   7726 }
   7727 
   7728 UINT16 TPM_PARAMETER_SIZE_Marshal(TPM_PARAMETER_SIZE* source,
   7729                                   BYTE** buffer,
   7730                                   INT32* size) {
   7731   return uint32_t_Marshal(source, buffer, size);
   7732 }
   7733 
   7734 TPM_RC TPM_PARAMETER_SIZE_Unmarshal(TPM_PARAMETER_SIZE* target,
   7735                                     BYTE** buffer,
   7736                                     INT32* size) {
   7737   return uint32_t_Unmarshal(target, buffer, size);
   7738 }
   7739 
   7740 UINT16 TPM_PS_Marshal(TPM_PS* source, BYTE** buffer, INT32* size) {
   7741   return uint32_t_Marshal(source, buffer, size);
   7742 }
   7743 
   7744 TPM_RC TPM_PS_Unmarshal(TPM_PS* target, BYTE** buffer, INT32* size) {
   7745   TPM_RC result;
   7746   result = uint32_t_Unmarshal(target, buffer, size);
   7747   if (result != TPM_RC_SUCCESS) {
   7748     return result;
   7749   }
   7750   if (*target == TPM_PS_MAIN) {
   7751     return TPM_RC_SUCCESS;
   7752   }
   7753   if (*target == TPM_PS_PC) {
   7754     return TPM_RC_SUCCESS;
   7755   }
   7756   if (*target == TPM_PS_PDA) {
   7757     return TPM_RC_SUCCESS;
   7758   }
   7759   if (*target == TPM_PS_CELL_PHONE) {
   7760     return TPM_RC_SUCCESS;
   7761   }
   7762   if (*target == TPM_PS_SERVER) {
   7763     return TPM_RC_SUCCESS;
   7764   }
   7765   if (*target == TPM_PS_PERIPHERAL) {
   7766     return TPM_RC_SUCCESS;
   7767   }
   7768   if (*target == TPM_PS_TSS) {
   7769     return TPM_RC_SUCCESS;
   7770   }
   7771   if (*target == TPM_PS_STORAGE) {
   7772     return TPM_RC_SUCCESS;
   7773   }
   7774   if (*target == TPM_PS_AUTHENTICATION) {
   7775     return TPM_RC_SUCCESS;
   7776   }
   7777   if (*target == TPM_PS_EMBEDDED) {
   7778     return TPM_RC_SUCCESS;
   7779   }
   7780   if (*target == TPM_PS_HARDCOPY) {
   7781     return TPM_RC_SUCCESS;
   7782   }
   7783   if (*target == TPM_PS_INFRASTRUCTURE) {
   7784     return TPM_RC_SUCCESS;
   7785   }
   7786   if (*target == TPM_PS_VIRTUALIZATION) {
   7787     return TPM_RC_SUCCESS;
   7788   }
   7789   if (*target == TPM_PS_TNC) {
   7790     return TPM_RC_SUCCESS;
   7791   }
   7792   if (*target == TPM_PS_MULTI_TENANT) {
   7793     return TPM_RC_SUCCESS;
   7794   }
   7795   if (*target == TPM_PS_TC) {
   7796     return TPM_RC_SUCCESS;
   7797   }
   7798   return TPM_RC_VALUE;
   7799 }
   7800 
   7801 UINT16 TPM_PT_PCR_Marshal(TPM_PT_PCR* source, BYTE** buffer, INT32* size) {
   7802   return uint32_t_Marshal(source, buffer, size);
   7803 }
   7804 
   7805 TPM_RC TPM_PT_PCR_Unmarshal(TPM_PT_PCR* target, BYTE** buffer, INT32* size) {
   7806   TPM_RC result;
   7807   result = uint32_t_Unmarshal(target, buffer, size);
   7808   if (result != TPM_RC_SUCCESS) {
   7809     return result;
   7810   }
   7811   if (*target == TPM_PT_PCR_FIRST) {
   7812     return TPM_RC_SUCCESS;
   7813   }
   7814   if (*target == TPM_PT_PCR_SAVE) {
   7815     return TPM_RC_SUCCESS;
   7816   }
   7817   if (*target == TPM_PT_PCR_EXTEND_L0) {
   7818     return TPM_RC_SUCCESS;
   7819   }
   7820   if (*target == TPM_PT_PCR_RESET_L0) {
   7821     return TPM_RC_SUCCESS;
   7822   }
   7823   if (*target == TPM_PT_PCR_EXTEND_L1) {
   7824     return TPM_RC_SUCCESS;
   7825   }
   7826   if (*target == TPM_PT_PCR_RESET_L1) {
   7827     return TPM_RC_SUCCESS;
   7828   }
   7829   if (*target == TPM_PT_PCR_EXTEND_L2) {
   7830     return TPM_RC_SUCCESS;
   7831   }
   7832   if (*target == TPM_PT_PCR_RESET_L2) {
   7833     return TPM_RC_SUCCESS;
   7834   }
   7835   if (*target == TPM_PT_PCR_EXTEND_L3) {
   7836     return TPM_RC_SUCCESS;
   7837   }
   7838   if (*target == TPM_PT_PCR_RESET_L3) {
   7839     return TPM_RC_SUCCESS;
   7840   }
   7841   if (*target == TPM_PT_PCR_EXTEND_L4) {
   7842     return TPM_RC_SUCCESS;
   7843   }
   7844   if (*target == TPM_PT_PCR_RESET_L4) {
   7845     return TPM_RC_SUCCESS;
   7846   }
   7847   if (*target == TPM_PT_PCR_NO_INCREMENT) {
   7848     return TPM_RC_SUCCESS;
   7849   }
   7850   if (*target == TPM_PT_PCR_DRTM_RESET) {
   7851     return TPM_RC_SUCCESS;
   7852   }
   7853   if (*target == TPM_PT_PCR_POLICY) {
   7854     return TPM_RC_SUCCESS;
   7855   }
   7856   if (*target == TPM_PT_PCR_AUTH) {
   7857     return TPM_RC_SUCCESS;
   7858   }
   7859   if (*target == TPM_PT_PCR_LAST) {
   7860     return TPM_RC_SUCCESS;
   7861   }
   7862   return TPM_RC_VALUE;
   7863 }
   7864 
   7865 UINT16 TPM_RC_Marshal(TPM_RC* source, BYTE** buffer, INT32* size) {
   7866   return uint32_t_Marshal(source, buffer, size);
   7867 }
   7868 
   7869 TPM_RC TPM_RC_Unmarshal(TPM_RC* target, BYTE** buffer, INT32* size) {
   7870   TPM_RC result;
   7871   result = uint32_t_Unmarshal(target, buffer, size);
   7872   if (result != TPM_RC_SUCCESS) {
   7873     return result;
   7874   }
   7875   if (*target == TPM_RC_SUCCESS) {
   7876     return TPM_RC_SUCCESS;
   7877   }
   7878   if (*target == TPM_RC_BAD_TAG) {
   7879     return TPM_RC_SUCCESS;
   7880   }
   7881   if (*target == RC_VER1) {
   7882     return TPM_RC_SUCCESS;
   7883   }
   7884   if (*target == TPM_RC_INITIALIZE) {
   7885     return TPM_RC_SUCCESS;
   7886   }
   7887   if (*target == TPM_RC_FAILURE) {
   7888     return TPM_RC_SUCCESS;
   7889   }
   7890   if (*target == TPM_RC_SEQUENCE) {
   7891     return TPM_RC_SUCCESS;
   7892   }
   7893   if (*target == TPM_RC_PRIVATE) {
   7894     return TPM_RC_SUCCESS;
   7895   }
   7896   if (*target == TPM_RC_HMAC) {
   7897     return TPM_RC_SUCCESS;
   7898   }
   7899   if (*target == TPM_RC_DISABLED) {
   7900     return TPM_RC_SUCCESS;
   7901   }
   7902   if (*target == TPM_RC_EXCLUSIVE) {
   7903     return TPM_RC_SUCCESS;
   7904   }
   7905   if (*target == TPM_RC_AUTH_TYPE) {
   7906     return TPM_RC_SUCCESS;
   7907   }
   7908   if (*target == TPM_RC_AUTH_MISSING) {
   7909     return TPM_RC_SUCCESS;
   7910   }
   7911   if (*target == TPM_RC_POLICY) {
   7912     return TPM_RC_SUCCESS;
   7913   }
   7914   if (*target == TPM_RC_PCR) {
   7915     return TPM_RC_SUCCESS;
   7916   }
   7917   if (*target == TPM_RC_PCR_CHANGED) {
   7918     return TPM_RC_SUCCESS;
   7919   }
   7920   if (*target == TPM_RC_UPGRADE) {
   7921     return TPM_RC_SUCCESS;
   7922   }
   7923   if (*target == TPM_RC_TOO_MANY_CONTEXTS) {
   7924     return TPM_RC_SUCCESS;
   7925   }
   7926   if (*target == TPM_RC_AUTH_UNAVAILABLE) {
   7927     return TPM_RC_SUCCESS;
   7928   }
   7929   if (*target == TPM_RC_REBOOT) {
   7930     return TPM_RC_SUCCESS;
   7931   }
   7932   if (*target == TPM_RC_UNBALANCED) {
   7933     return TPM_RC_SUCCESS;
   7934   }
   7935   if (*target == TPM_RC_COMMAND_SIZE) {
   7936     return TPM_RC_SUCCESS;
   7937   }
   7938   if (*target == TPM_RC_COMMAND_CODE) {
   7939     return TPM_RC_SUCCESS;
   7940   }
   7941   if (*target == TPM_RC_AUTHSIZE) {
   7942     return TPM_RC_SUCCESS;
   7943   }
   7944   if (*target == TPM_RC_AUTH_CONTEXT) {
   7945     return TPM_RC_SUCCESS;
   7946   }
   7947   if (*target == TPM_RC_NV_RANGE) {
   7948     return TPM_RC_SUCCESS;
   7949   }
   7950   if (*target == TPM_RC_NV_SIZE) {
   7951     return TPM_RC_SUCCESS;
   7952   }
   7953   if (*target == TPM_RC_NV_LOCKED) {
   7954     return TPM_RC_SUCCESS;
   7955   }
   7956   if (*target == TPM_RC_NV_AUTHORIZATION) {
   7957     return TPM_RC_SUCCESS;
   7958   }
   7959   if (*target == TPM_RC_NV_UNINITIALIZED) {
   7960     return TPM_RC_SUCCESS;
   7961   }
   7962   if (*target == TPM_RC_NV_SPACE) {
   7963     return TPM_RC_SUCCESS;
   7964   }
   7965   if (*target == TPM_RC_NV_DEFINED) {
   7966     return TPM_RC_SUCCESS;
   7967   }
   7968   if (*target == TPM_RC_BAD_CONTEXT) {
   7969     return TPM_RC_SUCCESS;
   7970   }
   7971   if (*target == TPM_RC_CPHASH) {
   7972     return TPM_RC_SUCCESS;
   7973   }
   7974   if (*target == TPM_RC_PARENT) {
   7975     return TPM_RC_SUCCESS;
   7976   }
   7977   if (*target == TPM_RC_NEEDS_TEST) {
   7978     return TPM_RC_SUCCESS;
   7979   }
   7980   if (*target == TPM_RC_NO_RESULT) {
   7981     return TPM_RC_SUCCESS;
   7982   }
   7983   if (*target == TPM_RC_SENSITIVE) {
   7984     return TPM_RC_SUCCESS;
   7985   }
   7986   if (*target == RC_MAX_FM0) {
   7987     return TPM_RC_SUCCESS;
   7988   }
   7989   if (*target == RC_FMT1) {
   7990     return TPM_RC_SUCCESS;
   7991   }
   7992   if (*target == TPM_RC_ASYMMETRIC) {
   7993     return TPM_RC_SUCCESS;
   7994   }
   7995   if (*target == TPM_RC_ATTRIBUTES) {
   7996     return TPM_RC_SUCCESS;
   7997   }
   7998   if (*target == TPM_RC_HASH) {
   7999     return TPM_RC_SUCCESS;
   8000   }
   8001   if (*target == TPM_RC_VALUE) {
   8002     return TPM_RC_SUCCESS;
   8003   }
   8004   if (*target == TPM_RC_HIERARCHY) {
   8005     return TPM_RC_SUCCESS;
   8006   }
   8007   if (*target == TPM_RC_KEY_SIZE) {
   8008     return TPM_RC_SUCCESS;
   8009   }
   8010   if (*target == TPM_RC_MGF) {
   8011     return TPM_RC_SUCCESS;
   8012   }
   8013   if (*target == TPM_RC_MODE) {
   8014     return TPM_RC_SUCCESS;
   8015   }
   8016   if (*target == TPM_RC_TYPE) {
   8017     return TPM_RC_SUCCESS;
   8018   }
   8019   if (*target == TPM_RC_HANDLE) {
   8020     return TPM_RC_SUCCESS;
   8021   }
   8022   if (*target == TPM_RC_KDF) {
   8023     return TPM_RC_SUCCESS;
   8024   }
   8025   if (*target == TPM_RC_RANGE) {
   8026     return TPM_RC_SUCCESS;
   8027   }
   8028   if (*target == TPM_RC_AUTH_FAIL) {
   8029     return TPM_RC_SUCCESS;
   8030   }
   8031   if (*target == TPM_RC_NONCE) {
   8032     return TPM_RC_SUCCESS;
   8033   }
   8034   if (*target == TPM_RC_PP) {
   8035     return TPM_RC_SUCCESS;
   8036   }
   8037   if (*target == TPM_RC_SCHEME) {
   8038     return TPM_RC_SUCCESS;
   8039   }
   8040   if (*target == TPM_RC_SIZE) {
   8041     return TPM_RC_SUCCESS;
   8042   }
   8043   if (*target == TPM_RC_SYMMETRIC) {
   8044     return TPM_RC_SUCCESS;
   8045   }
   8046   if (*target == TPM_RC_TAG) {
   8047     return TPM_RC_SUCCESS;
   8048   }
   8049   if (*target == TPM_RC_SELECTOR) {
   8050     return TPM_RC_SUCCESS;
   8051   }
   8052   if (*target == TPM_RC_INSUFFICIENT) {
   8053     return TPM_RC_SUCCESS;
   8054   }
   8055   if (*target == TPM_RC_SIGNATURE) {
   8056     return TPM_RC_SUCCESS;
   8057   }
   8058   if (*target == TPM_RC_KEY) {
   8059     return TPM_RC_SUCCESS;
   8060   }
   8061   if (*target == TPM_RC_POLICY_FAIL) {
   8062     return TPM_RC_SUCCESS;
   8063   }
   8064   if (*target == TPM_RC_INTEGRITY) {
   8065     return TPM_RC_SUCCESS;
   8066   }
   8067   if (*target == TPM_RC_TICKET) {
   8068     return TPM_RC_SUCCESS;
   8069   }
   8070   if (*target == TPM_RC_RESERVED_BITS) {
   8071     return TPM_RC_SUCCESS;
   8072   }
   8073   if (*target == TPM_RC_BAD_AUTH) {
   8074     return TPM_RC_SUCCESS;
   8075   }
   8076   if (*target == TPM_RC_EXPIRED) {
   8077     return TPM_RC_SUCCESS;
   8078   }
   8079   if (*target == TPM_RC_POLICY_CC) {
   8080     return TPM_RC_SUCCESS;
   8081   }
   8082   if (*target == TPM_RC_BINDING) {
   8083     return TPM_RC_SUCCESS;
   8084   }
   8085   if (*target == TPM_RC_CURVE) {
   8086     return TPM_RC_SUCCESS;
   8087   }
   8088   if (*target == TPM_RC_ECC_POINT) {
   8089     return TPM_RC_SUCCESS;
   8090   }
   8091   if (*target == RC_WARN) {
   8092     return TPM_RC_SUCCESS;
   8093   }
   8094   if (*target == TPM_RC_CONTEXT_GAP) {
   8095     return TPM_RC_SUCCESS;
   8096   }
   8097   if (*target == TPM_RC_OBJECT_MEMORY) {
   8098     return TPM_RC_SUCCESS;
   8099   }
   8100   if (*target == TPM_RC_SESSION_MEMORY) {
   8101     return TPM_RC_SUCCESS;
   8102   }
   8103   if (*target == TPM_RC_MEMORY) {
   8104     return TPM_RC_SUCCESS;
   8105   }
   8106   if (*target == TPM_RC_SESSION_HANDLES) {
   8107     return TPM_RC_SUCCESS;
   8108   }
   8109   if (*target == TPM_RC_OBJECT_HANDLES) {
   8110     return TPM_RC_SUCCESS;
   8111   }
   8112   if (*target == TPM_RC_LOCALITY) {
   8113     return TPM_RC_SUCCESS;
   8114   }
   8115   if (*target == TPM_RC_YIELDED) {
   8116     return TPM_RC_SUCCESS;
   8117   }
   8118   if (*target == TPM_RC_CANCELED) {
   8119     return TPM_RC_SUCCESS;
   8120   }
   8121   if (*target == TPM_RC_TESTING) {
   8122     return TPM_RC_SUCCESS;
   8123   }
   8124   if (*target == TPM_RC_REFERENCE_H0) {
   8125     return TPM_RC_SUCCESS;
   8126   }
   8127   if (*target == TPM_RC_REFERENCE_H1) {
   8128     return TPM_RC_SUCCESS;
   8129   }
   8130   if (*target == TPM_RC_REFERENCE_H2) {
   8131     return TPM_RC_SUCCESS;
   8132   }
   8133   if (*target == TPM_RC_REFERENCE_H3) {
   8134     return TPM_RC_SUCCESS;
   8135   }
   8136   if (*target == TPM_RC_REFERENCE_H4) {
   8137     return TPM_RC_SUCCESS;
   8138   }
   8139   if (*target == TPM_RC_REFERENCE_H5) {
   8140     return TPM_RC_SUCCESS;
   8141   }
   8142   if (*target == TPM_RC_REFERENCE_H6) {
   8143     return TPM_RC_SUCCESS;
   8144   }
   8145   if (*target == TPM_RC_REFERENCE_S0) {
   8146     return TPM_RC_SUCCESS;
   8147   }
   8148   if (*target == TPM_RC_REFERENCE_S1) {
   8149     return TPM_RC_SUCCESS;
   8150   }
   8151   if (*target == TPM_RC_REFERENCE_S2) {
   8152     return TPM_RC_SUCCESS;
   8153   }
   8154   if (*target == TPM_RC_REFERENCE_S3) {
   8155     return TPM_RC_SUCCESS;
   8156   }
   8157   if (*target == TPM_RC_REFERENCE_S4) {
   8158     return TPM_RC_SUCCESS;
   8159   }
   8160   if (*target == TPM_RC_REFERENCE_S5) {
   8161     return TPM_RC_SUCCESS;
   8162   }
   8163   if (*target == TPM_RC_REFERENCE_S6) {
   8164     return TPM_RC_SUCCESS;
   8165   }
   8166   if (*target == TPM_RC_NV_RATE) {
   8167     return TPM_RC_SUCCESS;
   8168   }
   8169   if (*target == TPM_RC_LOCKOUT) {
   8170     return TPM_RC_SUCCESS;
   8171   }
   8172   if (*target == TPM_RC_RETRY) {
   8173     return TPM_RC_SUCCESS;
   8174   }
   8175   if (*target == TPM_RC_NV_UNAVAILABLE) {
   8176     return TPM_RC_SUCCESS;
   8177   }
   8178   if (*target == TPM_RC_NOT_USED) {
   8179     return TPM_RC_SUCCESS;
   8180   }
   8181   if (*target == TPM_RC_H) {
   8182     return TPM_RC_SUCCESS;
   8183   }
   8184   if (*target == TPM_RC_P) {
   8185     return TPM_RC_SUCCESS;
   8186   }
   8187   if (*target == TPM_RC_S) {
   8188     return TPM_RC_SUCCESS;
   8189   }
   8190   if (*target == TPM_RC_1) {
   8191     return TPM_RC_SUCCESS;
   8192   }
   8193   if (*target == TPM_RC_2) {
   8194     return TPM_RC_SUCCESS;
   8195   }
   8196   if (*target == TPM_RC_3) {
   8197     return TPM_RC_SUCCESS;
   8198   }
   8199   if (*target == TPM_RC_4) {
   8200     return TPM_RC_SUCCESS;
   8201   }
   8202   if (*target == TPM_RC_5) {
   8203     return TPM_RC_SUCCESS;
   8204   }
   8205   if (*target == TPM_RC_6) {
   8206     return TPM_RC_SUCCESS;
   8207   }
   8208   if (*target == TPM_RC_7) {
   8209     return TPM_RC_SUCCESS;
   8210   }
   8211   if (*target == TPM_RC_8) {
   8212     return TPM_RC_SUCCESS;
   8213   }
   8214   if (*target == TPM_RC_9) {
   8215     return TPM_RC_SUCCESS;
   8216   }
   8217   if (*target == TPM_RC_A) {
   8218     return TPM_RC_SUCCESS;
   8219   }
   8220   if (*target == TPM_RC_B) {
   8221     return TPM_RC_SUCCESS;
   8222   }
   8223   if (*target == TPM_RC_C) {
   8224     return TPM_RC_SUCCESS;
   8225   }
   8226   if (*target == TPM_RC_D) {
   8227     return TPM_RC_SUCCESS;
   8228   }
   8229   if (*target == TPM_RC_E) {
   8230     return TPM_RC_SUCCESS;
   8231   }
   8232   if (*target == TPM_RC_F) {
   8233     return TPM_RC_SUCCESS;
   8234   }
   8235   if (*target == TPM_RC_N_MASK) {
   8236     return TPM_RC_SUCCESS;
   8237   }
   8238   return TPM_RC_VALUE;
   8239 }
   8240 
   8241 UINT16 TPM_RH_Marshal(TPM_RH* source, BYTE** buffer, INT32* size) {
   8242   return uint32_t_Marshal(source, buffer, size);
   8243 }
   8244 
   8245 TPM_RC TPM_RH_Unmarshal(TPM_RH* target, BYTE** buffer, INT32* size) {
   8246   TPM_RC result;
   8247   result = uint32_t_Unmarshal(target, buffer, size);
   8248   if (result != TPM_RC_SUCCESS) {
   8249     return result;
   8250   }
   8251   if (*target == TPM_RH_FIRST) {
   8252     return TPM_RC_SUCCESS;
   8253   }
   8254   if (*target == TPM_RH_SRK) {
   8255     return TPM_RC_SUCCESS;
   8256   }
   8257   if (*target == TPM_RH_OWNER) {
   8258     return TPM_RC_SUCCESS;
   8259   }
   8260   if (*target == TPM_RH_REVOKE) {
   8261     return TPM_RC_SUCCESS;
   8262   }
   8263   if (*target == TPM_RH_TRANSPORT) {
   8264     return TPM_RC_SUCCESS;
   8265   }
   8266   if (*target == TPM_RH_OPERATOR) {
   8267     return TPM_RC_SUCCESS;
   8268   }
   8269   if (*target == TPM_RH_ADMIN) {
   8270     return TPM_RC_SUCCESS;
   8271   }
   8272   if (*target == TPM_RH_EK) {
   8273     return TPM_RC_SUCCESS;
   8274   }
   8275   if (*target == TPM_RH_NULL) {
   8276     return TPM_RC_SUCCESS;
   8277   }
   8278   if (*target == TPM_RH_UNASSIGNED) {
   8279     return TPM_RC_SUCCESS;
   8280   }
   8281   if (*target == TPM_RS_PW) {
   8282     return TPM_RC_SUCCESS;
   8283   }
   8284   if (*target == TPM_RH_LOCKOUT) {
   8285     return TPM_RC_SUCCESS;
   8286   }
   8287   if (*target == TPM_RH_ENDORSEMENT) {
   8288     return TPM_RC_SUCCESS;
   8289   }
   8290   if (*target == TPM_RH_PLATFORM) {
   8291     return TPM_RC_SUCCESS;
   8292   }
   8293   if (*target == TPM_RH_PLATFORM_NV) {
   8294     return TPM_RC_SUCCESS;
   8295   }
   8296   if (*target == TPM_RH_AUTH_00) {
   8297     return TPM_RC_SUCCESS;
   8298   }
   8299   if (*target == TPM_RH_AUTH_FF) {
   8300     return TPM_RC_SUCCESS;
   8301   }
   8302   if (*target == TPM_RH_LAST) {
   8303     return TPM_RC_SUCCESS;
   8304   }
   8305   return TPM_RC_VALUE;
   8306 }
   8307 
   8308 UINT16 TPM_SE_Marshal(TPM_SE* source, BYTE** buffer, INT32* size) {
   8309   return uint8_t_Marshal(source, buffer, size);
   8310 }
   8311 
   8312 TPM_RC TPM_SE_Unmarshal(TPM_SE* target, BYTE** buffer, INT32* size) {
   8313   TPM_RC result;
   8314   result = uint8_t_Unmarshal(target, buffer, size);
   8315   if (result != TPM_RC_SUCCESS) {
   8316     return result;
   8317   }
   8318   if (*target == TPM_SE_HMAC) {
   8319     return TPM_RC_SUCCESS;
   8320   }
   8321   if (*target == TPM_SE_POLICY) {
   8322     return TPM_RC_SUCCESS;
   8323   }
   8324   if (*target == TPM_SE_TRIAL) {
   8325     return TPM_RC_SUCCESS;
   8326   }
   8327   return TPM_RC_VALUE;
   8328 }
   8329 
   8330 UINT16 TPM_SPEC_Marshal(TPM_SPEC* source, BYTE** buffer, INT32* size) {
   8331   return uint32_t_Marshal(source, buffer, size);
   8332 }
   8333 
   8334 TPM_RC TPM_SPEC_Unmarshal(TPM_SPEC* target, BYTE** buffer, INT32* size) {
   8335   TPM_RC result;
   8336   result = uint32_t_Unmarshal(target, buffer, size);
   8337   if (result != TPM_RC_SUCCESS) {
   8338     return result;
   8339   }
   8340   if (*target == TPM_SPEC_FAMILY) {
   8341     return TPM_RC_SUCCESS;
   8342   }
   8343   if (*target == TPM_SPEC_LEVEL) {
   8344     return TPM_RC_SUCCESS;
   8345   }
   8346   if (*target == TPM_SPEC_VERSION) {
   8347     return TPM_RC_SUCCESS;
   8348   }
   8349   if (*target == TPM_SPEC_YEAR) {
   8350     return TPM_RC_SUCCESS;
   8351   }
   8352   if (*target == TPM_SPEC_DAY_OF_YEAR) {
   8353     return TPM_RC_SUCCESS;
   8354   }
   8355   return TPM_RC_VALUE;
   8356 }
   8357 
   8358 UINT16 TPM_SU_Marshal(TPM_SU* source, BYTE** buffer, INT32* size) {
   8359   return uint16_t_Marshal(source, buffer, size);
   8360 }
   8361 
   8362 TPM_RC TPM_SU_Unmarshal(TPM_SU* target, BYTE** buffer, INT32* size) {
   8363   TPM_RC result;
   8364   result = uint16_t_Unmarshal(target, buffer, size);
   8365   if (result != TPM_RC_SUCCESS) {
   8366     return result;
   8367   }
   8368   if (*target == TPM_SU_CLEAR) {
   8369     return TPM_RC_SUCCESS;
   8370   }
   8371   if (*target == TPM_SU_STATE) {
   8372     return TPM_RC_SUCCESS;
   8373   }
   8374   return TPM_RC_VALUE;
   8375 }
   8376 
   8377 UINT16 _ID_OBJECT_Marshal(_ID_OBJECT* source, BYTE** buffer, INT32* size) {
   8378   UINT16 total_size = 0;
   8379   total_size += TPM2B_DIGEST_Marshal(&source->integrityHMAC, buffer, size);
   8380   total_size += TPM2B_DIGEST_Marshal(&source->encIdentity, buffer, size);
   8381   return total_size;
   8382 }
   8383 
   8384 TPM_RC _ID_OBJECT_Unmarshal(_ID_OBJECT* target, BYTE** buffer, INT32* size) {
   8385   TPM_RC result;
   8386   result = TPM2B_DIGEST_Unmarshal(&target->integrityHMAC, buffer, size);
   8387   if (result != TPM_RC_SUCCESS) {
   8388     return result;
   8389   }
   8390   result = TPM2B_DIGEST_Unmarshal(&target->encIdentity, buffer, size);
   8391   if (result != TPM_RC_SUCCESS) {
   8392     return result;
   8393   }
   8394   return TPM_RC_SUCCESS;
   8395 }
   8396 
   8397 UINT16 _PRIVATE_Marshal(_PRIVATE* source, BYTE** buffer, INT32* size) {
   8398   UINT16 total_size = 0;
   8399   total_size += TPM2B_DIGEST_Marshal(&source->integrityOuter, buffer, size);
   8400   total_size += TPM2B_DIGEST_Marshal(&source->integrityInner, buffer, size);
   8401   total_size += TPMT_SENSITIVE_Marshal(&source->sensitive, buffer, size);
   8402   return total_size;
   8403 }
   8404 
   8405 TPM_RC _PRIVATE_Unmarshal(_PRIVATE* target, BYTE** buffer, INT32* size) {
   8406   TPM_RC result;
   8407   result = TPM2B_DIGEST_Unmarshal(&target->integrityOuter, buffer, size);
   8408   if (result != TPM_RC_SUCCESS) {
   8409     return result;
   8410   }
   8411   result = TPM2B_DIGEST_Unmarshal(&target->integrityInner, buffer, size);
   8412   if (result != TPM_RC_SUCCESS) {
   8413     return result;
   8414   }
   8415   result = TPMT_SENSITIVE_Unmarshal(&target->sensitive, buffer, size);
   8416   if (result != TPM_RC_SUCCESS) {
   8417     return result;
   8418   }
   8419   return TPM_RC_SUCCESS;
   8420 }
   8421