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