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, buffer);
   8436     if (result) {
   8437       return result;
   8438     }
   8439   }
   8440 
   8441   if (selector == TPM_ALG_ECC) {
   8442     result = Serialize_TPM2B_ECC_PARAMETER(value.ecc, buffer);
   8443     if (result) {
   8444       return result;
   8445     }
   8446   }
   8447   return result;
   8448 }
   8449 
   8450 TPM_RC Parse_TPMU_SENSITIVE_COMPOSITE(
   8451     std::string* buffer,
   8452     TPMI_ALG_PUBLIC selector,
   8453     TPMU_SENSITIVE_COMPOSITE* value,
   8454     std::string* value_bytes) {
   8455   TPM_RC result = TPM_RC_SUCCESS;
   8456   VLOG(3) << __func__;
   8457 
   8458   if (selector == TPM_ALG_KEYEDHASH) {
   8459     result = Parse_TPM2B_SENSITIVE_DATA(
   8460         buffer,
   8461         &value->bits,
   8462         value_bytes);
   8463     if (result) {
   8464       return result;
   8465     }
   8466   }
   8467 
   8468   if (selector == TPM_ALG_RSA) {
   8469     result = Parse_TPM2B_PRIVATE_KEY_RSA(
   8470         buffer,
   8471         &value->rsa,
   8472         value_bytes);
   8473     if (result) {
   8474       return result;
   8475     }
   8476   }
   8477 
   8478   if (selector == TPM_ALG_SYMCIPHER) {
   8479     result = Parse_TPM2B_SYM_KEY(
   8480         buffer,
   8481         &value->sym,
   8482         value_bytes);
   8483     if (result) {
   8484       return result;
   8485     }
   8486   }
   8487 
   8488   if (selector == TPM_ALG_ECC) {
   8489     result = Parse_TPM2B_ECC_PARAMETER(
   8490         buffer,
   8491         &value->ecc,
   8492         value_bytes);
   8493     if (result) {
   8494       return result;
   8495     }
   8496   }
   8497   return result;
   8498 }
   8499 
   8500 TPM_RC Serialize_TPMT_SENSITIVE(
   8501     const TPMT_SENSITIVE& value,
   8502     std::string* buffer) {
   8503   TPM_RC result = TPM_RC_SUCCESS;
   8504   VLOG(3) << __func__;
   8505 
   8506   result = Serialize_TPMI_ALG_PUBLIC(value.sensitive_type, buffer);
   8507   if (result) {
   8508     return result;
   8509   }
   8510 
   8511   result = Serialize_TPM2B_AUTH(value.auth_value, buffer);
   8512   if (result) {
   8513     return result;
   8514   }
   8515 
   8516   result = Serialize_TPM2B_DIGEST(value.seed_value, buffer);
   8517   if (result) {
   8518     return result;
   8519   }
   8520 
   8521   result = Serialize_TPMU_SENSITIVE_COMPOSITE(
   8522       value.sensitive,
   8523       value.sensitive_type,
   8524       buffer);
   8525   if (result) {
   8526     return result;
   8527   }
   8528   return result;
   8529 }
   8530 
   8531 TPM_RC Parse_TPMT_SENSITIVE(
   8532     std::string* buffer,
   8533     TPMT_SENSITIVE* value,
   8534     std::string* value_bytes) {
   8535   TPM_RC result = TPM_RC_SUCCESS;
   8536   VLOG(3) << __func__;
   8537 
   8538   result = Parse_TPMI_ALG_PUBLIC(
   8539       buffer,
   8540       &value->sensitive_type,
   8541       value_bytes);
   8542   if (result) {
   8543     return result;
   8544   }
   8545 
   8546   result = Parse_TPM2B_AUTH(
   8547       buffer,
   8548       &value->auth_value,
   8549       value_bytes);
   8550   if (result) {
   8551     return result;
   8552   }
   8553 
   8554   result = Parse_TPM2B_DIGEST(
   8555       buffer,
   8556       &value->seed_value,
   8557       value_bytes);
   8558   if (result) {
   8559     return result;
   8560   }
   8561 
   8562   result = Parse_TPMU_SENSITIVE_COMPOSITE(
   8563       buffer,
   8564       value->sensitive_type,
   8565       &value->sensitive,
   8566       value_bytes);
   8567   if (result) {
   8568     return result;
   8569   }
   8570   return result;
   8571 }
   8572 
   8573 TPM_RC Serialize_TPM2B_SENSITIVE(
   8574     const TPM2B_SENSITIVE& value,
   8575     std::string* buffer) {
   8576   TPM_RC result = TPM_RC_SUCCESS;
   8577   VLOG(3) << __func__;
   8578 
   8579   std::string field_bytes;
   8580   result = Serialize_TPMT_SENSITIVE(value.sensitive_area, &field_bytes);
   8581   if (result) {
   8582     return result;
   8583   }
   8584   std::string size_bytes;
   8585   result = Serialize_UINT16(field_bytes.size(), &size_bytes);
   8586   if (result) {
   8587     return result;
   8588   }
   8589   buffer->append(size_bytes + field_bytes);
   8590   return result;
   8591 }
   8592 
   8593 TPM_RC Parse_TPM2B_SENSITIVE(
   8594     std::string* buffer,
   8595     TPM2B_SENSITIVE* value,
   8596     std::string* value_bytes) {
   8597   TPM_RC result = TPM_RC_SUCCESS;
   8598   VLOG(3) << __func__;
   8599 
   8600   result = Parse_UINT16(
   8601       buffer,
   8602       &value->size,
   8603       value_bytes);
   8604   if (result) {
   8605     return result;
   8606   }
   8607 
   8608   result = Parse_TPMT_SENSITIVE(
   8609       buffer,
   8610       &value->sensitive_area,
   8611       value_bytes);
   8612   if (result) {
   8613     return result;
   8614   }
   8615   return result;
   8616 }
   8617 
   8618 TPM2B_SENSITIVE Make_TPM2B_SENSITIVE(
   8619     const TPMT_SENSITIVE& inner) {
   8620   TPM2B_SENSITIVE tpm2b;
   8621   tpm2b.size = sizeof(TPMT_SENSITIVE);
   8622   tpm2b.sensitive_area = inner;
   8623   return tpm2b;
   8624 }
   8625 
   8626 TPM_RC Serialize__PRIVATE(
   8627     const _PRIVATE& value,
   8628     std::string* buffer) {
   8629   TPM_RC result = TPM_RC_SUCCESS;
   8630   VLOG(3) << __func__;
   8631 
   8632   result = Serialize_TPM2B_DIGEST(value.integrity_outer, buffer);
   8633   if (result) {
   8634     return result;
   8635   }
   8636 
   8637   result = Serialize_TPM2B_DIGEST(value.integrity_inner, buffer);
   8638   if (result) {
   8639     return result;
   8640   }
   8641 
   8642   result = Serialize_TPMT_SENSITIVE(value.sensitive, buffer);
   8643   if (result) {
   8644     return result;
   8645   }
   8646   return result;
   8647 }
   8648 
   8649 TPM_RC Parse__PRIVATE(
   8650     std::string* buffer,
   8651     _PRIVATE* value,
   8652     std::string* value_bytes) {
   8653   TPM_RC result = TPM_RC_SUCCESS;
   8654   VLOG(3) << __func__;
   8655 
   8656   result = Parse_TPM2B_DIGEST(
   8657       buffer,
   8658       &value->integrity_outer,
   8659       value_bytes);
   8660   if (result) {
   8661     return result;
   8662   }
   8663 
   8664   result = Parse_TPM2B_DIGEST(
   8665       buffer,
   8666       &value->integrity_inner,
   8667       value_bytes);
   8668   if (result) {
   8669     return result;
   8670   }
   8671 
   8672   result = Parse_TPMT_SENSITIVE(
   8673       buffer,
   8674       &value->sensitive,
   8675       value_bytes);
   8676   if (result) {
   8677     return result;
   8678   }
   8679   return result;
   8680 }
   8681 
   8682 TPM_RC Serialize_TPM2B_PRIVATE(
   8683     const TPM2B_PRIVATE& value,
   8684     std::string* buffer) {
   8685   TPM_RC result = TPM_RC_SUCCESS;
   8686   VLOG(3) << __func__;
   8687 
   8688   result = Serialize_UINT16(value.size, buffer);
   8689   if (result) {
   8690     return result;
   8691   }
   8692 
   8693   if (arraysize(value.buffer) < value.size) {
   8694     return TPM_RC_INSUFFICIENT;
   8695   }
   8696   for (uint32_t i = 0; i < value.size; ++i) {
   8697     result = Serialize_BYTE(value.buffer[i], buffer);
   8698     if (result) {
   8699       return result;
   8700     }
   8701   }
   8702   return result;
   8703 }
   8704 
   8705 TPM_RC Parse_TPM2B_PRIVATE(
   8706     std::string* buffer,
   8707     TPM2B_PRIVATE* value,
   8708     std::string* value_bytes) {
   8709   TPM_RC result = TPM_RC_SUCCESS;
   8710   VLOG(3) << __func__;
   8711 
   8712   result = Parse_UINT16(
   8713       buffer,
   8714       &value->size,
   8715       value_bytes);
   8716   if (result) {
   8717     return result;
   8718   }
   8719 
   8720   if (arraysize(value->buffer) < value->size) {
   8721     return TPM_RC_INSUFFICIENT;
   8722   }
   8723   for (uint32_t i = 0; i < value->size; ++i) {
   8724     result = Parse_BYTE(
   8725         buffer,
   8726         &value->buffer[i],
   8727         value_bytes);
   8728     if (result) {
   8729       return result;
   8730     }
   8731   }
   8732   return result;
   8733 }
   8734 
   8735 TPM2B_PRIVATE Make_TPM2B_PRIVATE(
   8736     const std::string& bytes) {
   8737   TPM2B_PRIVATE tpm2b;
   8738   CHECK(bytes.size() <= sizeof(tpm2b.buffer));
   8739   memset(&tpm2b, 0, sizeof(TPM2B_PRIVATE));
   8740   tpm2b.size = bytes.size();
   8741   memcpy(tpm2b.buffer, bytes.data(), bytes.size());
   8742   return tpm2b;
   8743 }
   8744 
   8745 std::string StringFrom_TPM2B_PRIVATE(
   8746     const TPM2B_PRIVATE& tpm2b) {
   8747   const char* char_buffer = reinterpret_cast<const char*>(
   8748       tpm2b.buffer);
   8749   return std::string(char_buffer, tpm2b.size);
   8750 }
   8751 
   8752 TPM_RC Serialize__ID_OBJECT(
   8753     const _ID_OBJECT& value,
   8754     std::string* buffer) {
   8755   TPM_RC result = TPM_RC_SUCCESS;
   8756   VLOG(3) << __func__;
   8757 
   8758   result = Serialize_TPM2B_DIGEST(value.integrity_hmac, buffer);
   8759   if (result) {
   8760     return result;
   8761   }
   8762 
   8763   result = Serialize_TPM2B_DIGEST(value.enc_identity, buffer);
   8764   if (result) {
   8765     return result;
   8766   }
   8767   return result;
   8768 }
   8769 
   8770 TPM_RC Parse__ID_OBJECT(
   8771     std::string* buffer,
   8772     _ID_OBJECT* value,
   8773     std::string* value_bytes) {
   8774   TPM_RC result = TPM_RC_SUCCESS;
   8775   VLOG(3) << __func__;
   8776 
   8777   result = Parse_TPM2B_DIGEST(
   8778       buffer,
   8779       &value->integrity_hmac,
   8780       value_bytes);
   8781   if (result) {
   8782     return result;
   8783   }
   8784 
   8785   result = Parse_TPM2B_DIGEST(
   8786       buffer,
   8787       &value->enc_identity,
   8788       value_bytes);
   8789   if (result) {
   8790     return result;
   8791   }
   8792   return result;
   8793 }
   8794 
   8795 TPM_RC Serialize_TPM2B_ID_OBJECT(
   8796     const TPM2B_ID_OBJECT& value,
   8797     std::string* buffer) {
   8798   TPM_RC result = TPM_RC_SUCCESS;
   8799   VLOG(3) << __func__;
   8800 
   8801   result = Serialize_UINT16(value.size, buffer);
   8802   if (result) {
   8803     return result;
   8804   }
   8805 
   8806   if (arraysize(value.credential) < value.size) {
   8807     return TPM_RC_INSUFFICIENT;
   8808   }
   8809   for (uint32_t i = 0; i < value.size; ++i) {
   8810     result = Serialize_BYTE(value.credential[i], buffer);
   8811     if (result) {
   8812       return result;
   8813     }
   8814   }
   8815   return result;
   8816 }
   8817 
   8818 TPM_RC Parse_TPM2B_ID_OBJECT(
   8819     std::string* buffer,
   8820     TPM2B_ID_OBJECT* value,
   8821     std::string* value_bytes) {
   8822   TPM_RC result = TPM_RC_SUCCESS;
   8823   VLOG(3) << __func__;
   8824 
   8825   result = Parse_UINT16(
   8826       buffer,
   8827       &value->size,
   8828       value_bytes);
   8829   if (result) {
   8830     return result;
   8831   }
   8832 
   8833   if (arraysize(value->credential) < value->size) {
   8834     return TPM_RC_INSUFFICIENT;
   8835   }
   8836   for (uint32_t i = 0; i < value->size; ++i) {
   8837     result = Parse_BYTE(
   8838         buffer,
   8839         &value->credential[i],
   8840         value_bytes);
   8841     if (result) {
   8842       return result;
   8843     }
   8844   }
   8845   return result;
   8846 }
   8847 
   8848 TPM2B_ID_OBJECT Make_TPM2B_ID_OBJECT(
   8849     const std::string& bytes) {
   8850   TPM2B_ID_OBJECT tpm2b;
   8851   CHECK(bytes.size() <= sizeof(tpm2b.credential));
   8852   memset(&tpm2b, 0, sizeof(TPM2B_ID_OBJECT));
   8853   tpm2b.size = bytes.size();
   8854   memcpy(tpm2b.credential, bytes.data(), bytes.size());
   8855   return tpm2b;
   8856 }
   8857 
   8858 std::string StringFrom_TPM2B_ID_OBJECT(
   8859     const TPM2B_ID_OBJECT& tpm2b) {
   8860   const char* char_buffer = reinterpret_cast<const char*>(
   8861       tpm2b.credential);
   8862   return std::string(char_buffer, tpm2b.size);
   8863 }
   8864 
   8865 TPM_RC Serialize_TPMS_NV_PUBLIC(
   8866     const TPMS_NV_PUBLIC& value,
   8867     std::string* buffer) {
   8868   TPM_RC result = TPM_RC_SUCCESS;
   8869   VLOG(3) << __func__;
   8870 
   8871   result = Serialize_TPMI_RH_NV_INDEX(value.nv_index, buffer);
   8872   if (result) {
   8873     return result;
   8874   }
   8875 
   8876   result = Serialize_TPMI_ALG_HASH(value.name_alg, buffer);
   8877   if (result) {
   8878     return result;
   8879   }
   8880 
   8881   result = Serialize_TPMA_NV(value.attributes, buffer);
   8882   if (result) {
   8883     return result;
   8884   }
   8885 
   8886   result = Serialize_TPM2B_DIGEST(value.auth_policy, buffer);
   8887   if (result) {
   8888     return result;
   8889   }
   8890 
   8891   result = Serialize_UINT16(value.data_size, buffer);
   8892   if (result) {
   8893     return result;
   8894   }
   8895   return result;
   8896 }
   8897 
   8898 TPM_RC Parse_TPMS_NV_PUBLIC(
   8899     std::string* buffer,
   8900     TPMS_NV_PUBLIC* value,
   8901     std::string* value_bytes) {
   8902   TPM_RC result = TPM_RC_SUCCESS;
   8903   VLOG(3) << __func__;
   8904 
   8905   result = Parse_TPMI_RH_NV_INDEX(
   8906       buffer,
   8907       &value->nv_index,
   8908       value_bytes);
   8909   if (result) {
   8910     return result;
   8911   }
   8912 
   8913   result = Parse_TPMI_ALG_HASH(
   8914       buffer,
   8915       &value->name_alg,
   8916       value_bytes);
   8917   if (result) {
   8918     return result;
   8919   }
   8920 
   8921   result = Parse_TPMA_NV(
   8922       buffer,
   8923       &value->attributes,
   8924       value_bytes);
   8925   if (result) {
   8926     return result;
   8927   }
   8928 
   8929   result = Parse_TPM2B_DIGEST(
   8930       buffer,
   8931       &value->auth_policy,
   8932       value_bytes);
   8933   if (result) {
   8934     return result;
   8935   }
   8936 
   8937   result = Parse_UINT16(
   8938       buffer,
   8939       &value->data_size,
   8940       value_bytes);
   8941   if (result) {
   8942     return result;
   8943   }
   8944   return result;
   8945 }
   8946 
   8947 TPM_RC Serialize_TPM2B_NV_PUBLIC(
   8948     const TPM2B_NV_PUBLIC& value,
   8949     std::string* buffer) {
   8950   TPM_RC result = TPM_RC_SUCCESS;
   8951   VLOG(3) << __func__;
   8952 
   8953   std::string field_bytes;
   8954   result = Serialize_TPMS_NV_PUBLIC(value.nv_public, &field_bytes);
   8955   if (result) {
   8956     return result;
   8957   }
   8958   std::string size_bytes;
   8959   result = Serialize_UINT16(field_bytes.size(), &size_bytes);
   8960   if (result) {
   8961     return result;
   8962   }
   8963   buffer->append(size_bytes + field_bytes);
   8964   return result;
   8965 }
   8966 
   8967 TPM_RC Parse_TPM2B_NV_PUBLIC(
   8968     std::string* buffer,
   8969     TPM2B_NV_PUBLIC* value,
   8970     std::string* value_bytes) {
   8971   TPM_RC result = TPM_RC_SUCCESS;
   8972   VLOG(3) << __func__;
   8973 
   8974   result = Parse_UINT16(
   8975       buffer,
   8976       &value->size,
   8977       value_bytes);
   8978   if (result) {
   8979     return result;
   8980   }
   8981 
   8982   result = Parse_TPMS_NV_PUBLIC(
   8983       buffer,
   8984       &value->nv_public,
   8985       value_bytes);
   8986   if (result) {
   8987     return result;
   8988   }
   8989   return result;
   8990 }
   8991 
   8992 TPM2B_NV_PUBLIC Make_TPM2B_NV_PUBLIC(
   8993     const TPMS_NV_PUBLIC& inner) {
   8994   TPM2B_NV_PUBLIC tpm2b;
   8995   tpm2b.size = sizeof(TPMS_NV_PUBLIC);
   8996   tpm2b.nv_public = inner;
   8997   return tpm2b;
   8998 }
   8999 
   9000 TPM_RC Serialize_TPM2B_CONTEXT_SENSITIVE(
   9001     const TPM2B_CONTEXT_SENSITIVE& value,
   9002     std::string* buffer) {
   9003   TPM_RC result = TPM_RC_SUCCESS;
   9004   VLOG(3) << __func__;
   9005 
   9006   result = Serialize_UINT16(value.size, buffer);
   9007   if (result) {
   9008     return result;
   9009   }
   9010 
   9011   if (arraysize(value.buffer) < value.size) {
   9012     return TPM_RC_INSUFFICIENT;
   9013   }
   9014   for (uint32_t i = 0; i < value.size; ++i) {
   9015     result = Serialize_BYTE(value.buffer[i], buffer);
   9016     if (result) {
   9017       return result;
   9018     }
   9019   }
   9020   return result;
   9021 }
   9022 
   9023 TPM_RC Parse_TPM2B_CONTEXT_SENSITIVE(
   9024     std::string* buffer,
   9025     TPM2B_CONTEXT_SENSITIVE* value,
   9026     std::string* value_bytes) {
   9027   TPM_RC result = TPM_RC_SUCCESS;
   9028   VLOG(3) << __func__;
   9029 
   9030   result = Parse_UINT16(
   9031       buffer,
   9032       &value->size,
   9033       value_bytes);
   9034   if (result) {
   9035     return result;
   9036   }
   9037 
   9038   if (arraysize(value->buffer) < value->size) {
   9039     return TPM_RC_INSUFFICIENT;
   9040   }
   9041   for (uint32_t i = 0; i < value->size; ++i) {
   9042     result = Parse_BYTE(
   9043         buffer,
   9044         &value->buffer[i],
   9045         value_bytes);
   9046     if (result) {
   9047       return result;
   9048     }
   9049   }
   9050   return result;
   9051 }
   9052 
   9053 TPM2B_CONTEXT_SENSITIVE Make_TPM2B_CONTEXT_SENSITIVE(
   9054     const std::string& bytes) {
   9055   TPM2B_CONTEXT_SENSITIVE tpm2b;
   9056   CHECK(bytes.size() <= sizeof(tpm2b.buffer));
   9057   memset(&tpm2b, 0, sizeof(TPM2B_CONTEXT_SENSITIVE));
   9058   tpm2b.size = bytes.size();
   9059   memcpy(tpm2b.buffer, bytes.data(), bytes.size());
   9060   return tpm2b;
   9061 }
   9062 
   9063 std::string StringFrom_TPM2B_CONTEXT_SENSITIVE(
   9064     const TPM2B_CONTEXT_SENSITIVE& tpm2b) {
   9065   const char* char_buffer = reinterpret_cast<const char*>(
   9066       tpm2b.buffer);
   9067   return std::string(char_buffer, tpm2b.size);
   9068 }
   9069 
   9070 TPM_RC Serialize_TPMS_CONTEXT_DATA(
   9071     const TPMS_CONTEXT_DATA& value,
   9072     std::string* buffer) {
   9073   TPM_RC result = TPM_RC_SUCCESS;
   9074   VLOG(3) << __func__;
   9075 
   9076   result = Serialize_TPM2B_DIGEST(value.integrity, buffer);
   9077   if (result) {
   9078     return result;
   9079   }
   9080 
   9081   result = Serialize_TPM2B_CONTEXT_SENSITIVE(value.encrypted, buffer);
   9082   if (result) {
   9083     return result;
   9084   }
   9085   return result;
   9086 }
   9087 
   9088 TPM_RC Parse_TPMS_CONTEXT_DATA(
   9089     std::string* buffer,
   9090     TPMS_CONTEXT_DATA* value,
   9091     std::string* value_bytes) {
   9092   TPM_RC result = TPM_RC_SUCCESS;
   9093   VLOG(3) << __func__;
   9094 
   9095   result = Parse_TPM2B_DIGEST(
   9096       buffer,
   9097       &value->integrity,
   9098       value_bytes);
   9099   if (result) {
   9100     return result;
   9101   }
   9102 
   9103   result = Parse_TPM2B_CONTEXT_SENSITIVE(
   9104       buffer,
   9105       &value->encrypted,
   9106       value_bytes);
   9107   if (result) {
   9108     return result;
   9109   }
   9110   return result;
   9111 }
   9112 
   9113 TPM_RC Serialize_TPM2B_CONTEXT_DATA(
   9114     const TPM2B_CONTEXT_DATA& value,
   9115     std::string* buffer) {
   9116   TPM_RC result = TPM_RC_SUCCESS;
   9117   VLOG(3) << __func__;
   9118 
   9119   result = Serialize_UINT16(value.size, buffer);
   9120   if (result) {
   9121     return result;
   9122   }
   9123 
   9124   if (arraysize(value.buffer) < value.size) {
   9125     return TPM_RC_INSUFFICIENT;
   9126   }
   9127   for (uint32_t i = 0; i < value.size; ++i) {
   9128     result = Serialize_BYTE(value.buffer[i], buffer);
   9129     if (result) {
   9130       return result;
   9131     }
   9132   }
   9133   return result;
   9134 }
   9135 
   9136 TPM_RC Parse_TPM2B_CONTEXT_DATA(
   9137     std::string* buffer,
   9138     TPM2B_CONTEXT_DATA* value,
   9139     std::string* value_bytes) {
   9140   TPM_RC result = TPM_RC_SUCCESS;
   9141   VLOG(3) << __func__;
   9142 
   9143   result = Parse_UINT16(
   9144       buffer,
   9145       &value->size,
   9146       value_bytes);
   9147   if (result) {
   9148     return result;
   9149   }
   9150 
   9151   if (arraysize(value->buffer) < value->size) {
   9152     return TPM_RC_INSUFFICIENT;
   9153   }
   9154   for (uint32_t i = 0; i < value->size; ++i) {
   9155     result = Parse_BYTE(
   9156         buffer,
   9157         &value->buffer[i],
   9158         value_bytes);
   9159     if (result) {
   9160       return result;
   9161     }
   9162   }
   9163   return result;
   9164 }
   9165 
   9166 TPM2B_CONTEXT_DATA Make_TPM2B_CONTEXT_DATA(
   9167     const std::string& bytes) {
   9168   TPM2B_CONTEXT_DATA tpm2b;
   9169   CHECK(bytes.size() <= sizeof(tpm2b.buffer));
   9170   memset(&tpm2b, 0, sizeof(TPM2B_CONTEXT_DATA));
   9171   tpm2b.size = bytes.size();
   9172   memcpy(tpm2b.buffer, bytes.data(), bytes.size());
   9173   return tpm2b;
   9174 }
   9175 
   9176 std::string StringFrom_TPM2B_CONTEXT_DATA(
   9177     const TPM2B_CONTEXT_DATA& tpm2b) {
   9178   const char* char_buffer = reinterpret_cast<const char*>(
   9179       tpm2b.buffer);
   9180   return std::string(char_buffer, tpm2b.size);
   9181 }
   9182 
   9183 TPM_RC Serialize_TPMS_CONTEXT(
   9184     const TPMS_CONTEXT& value,
   9185     std::string* buffer) {
   9186   TPM_RC result = TPM_RC_SUCCESS;
   9187   VLOG(3) << __func__;
   9188 
   9189   result = Serialize_UINT64(value.sequence, buffer);
   9190   if (result) {
   9191     return result;
   9192   }
   9193 
   9194   result = Serialize_TPMI_DH_CONTEXT(value.saved_handle, buffer);
   9195   if (result) {
   9196     return result;
   9197   }
   9198 
   9199   result = Serialize_TPMI_RH_HIERARCHY(value.hierarchy, buffer);
   9200   if (result) {
   9201     return result;
   9202   }
   9203 
   9204   result = Serialize_TPM2B_CONTEXT_DATA(value.context_blob, buffer);
   9205   if (result) {
   9206     return result;
   9207   }
   9208   return result;
   9209 }
   9210 
   9211 TPM_RC Parse_TPMS_CONTEXT(
   9212     std::string* buffer,
   9213     TPMS_CONTEXT* value,
   9214     std::string* value_bytes) {
   9215   TPM_RC result = TPM_RC_SUCCESS;
   9216   VLOG(3) << __func__;
   9217 
   9218   result = Parse_UINT64(
   9219       buffer,
   9220       &value->sequence,
   9221       value_bytes);
   9222   if (result) {
   9223     return result;
   9224   }
   9225 
   9226   result = Parse_TPMI_DH_CONTEXT(
   9227       buffer,
   9228       &value->saved_handle,
   9229       value_bytes);
   9230   if (result) {
   9231     return result;
   9232   }
   9233 
   9234   result = Parse_TPMI_RH_HIERARCHY(
   9235       buffer,
   9236       &value->hierarchy,
   9237       value_bytes);
   9238   if (result) {
   9239     return result;
   9240   }
   9241 
   9242   result = Parse_TPM2B_CONTEXT_DATA(
   9243       buffer,
   9244       &value->context_blob,
   9245       value_bytes);
   9246   if (result) {
   9247     return result;
   9248   }
   9249   return result;
   9250 }
   9251 
   9252 TPM_RC Serialize_TPMS_CREATION_DATA(
   9253     const TPMS_CREATION_DATA& value,
   9254     std::string* buffer) {
   9255   TPM_RC result = TPM_RC_SUCCESS;
   9256   VLOG(3) << __func__;
   9257 
   9258   result = Serialize_TPML_PCR_SELECTION(value.pcr_select, buffer);
   9259   if (result) {
   9260     return result;
   9261   }
   9262 
   9263   result = Serialize_TPM2B_DIGEST(value.pcr_digest, buffer);
   9264   if (result) {
   9265     return result;
   9266   }
   9267 
   9268   result = Serialize_TPMA_LOCALITY(value.locality, buffer);
   9269   if (result) {
   9270     return result;
   9271   }
   9272 
   9273   result = Serialize_TPM_ALG_ID(value.parent_name_alg, buffer);
   9274   if (result) {
   9275     return result;
   9276   }
   9277 
   9278   result = Serialize_TPM2B_NAME(value.parent_name, buffer);
   9279   if (result) {
   9280     return result;
   9281   }
   9282 
   9283   result = Serialize_TPM2B_NAME(value.parent_qualified_name, buffer);
   9284   if (result) {
   9285     return result;
   9286   }
   9287 
   9288   result = Serialize_TPM2B_DATA(value.outside_info, buffer);
   9289   if (result) {
   9290     return result;
   9291   }
   9292   return result;
   9293 }
   9294 
   9295 TPM_RC Parse_TPMS_CREATION_DATA(
   9296     std::string* buffer,
   9297     TPMS_CREATION_DATA* value,
   9298     std::string* value_bytes) {
   9299   TPM_RC result = TPM_RC_SUCCESS;
   9300   VLOG(3) << __func__;
   9301 
   9302   result = Parse_TPML_PCR_SELECTION(
   9303       buffer,
   9304       &value->pcr_select,
   9305       value_bytes);
   9306   if (result) {
   9307     return result;
   9308   }
   9309 
   9310   result = Parse_TPM2B_DIGEST(
   9311       buffer,
   9312       &value->pcr_digest,
   9313       value_bytes);
   9314   if (result) {
   9315     return result;
   9316   }
   9317 
   9318   result = Parse_TPMA_LOCALITY(
   9319       buffer,
   9320       &value->locality,
   9321       value_bytes);
   9322   if (result) {
   9323     return result;
   9324   }
   9325 
   9326   result = Parse_TPM_ALG_ID(
   9327       buffer,
   9328       &value->parent_name_alg,
   9329       value_bytes);
   9330   if (result) {
   9331     return result;
   9332   }
   9333 
   9334   result = Parse_TPM2B_NAME(
   9335       buffer,
   9336       &value->parent_name,
   9337       value_bytes);
   9338   if (result) {
   9339     return result;
   9340   }
   9341 
   9342   result = Parse_TPM2B_NAME(
   9343       buffer,
   9344       &value->parent_qualified_name,
   9345       value_bytes);
   9346   if (result) {
   9347     return result;
   9348   }
   9349 
   9350   result = Parse_TPM2B_DATA(
   9351       buffer,
   9352       &value->outside_info,
   9353       value_bytes);
   9354   if (result) {
   9355     return result;
   9356   }
   9357   return result;
   9358 }
   9359 
   9360 TPM_RC Serialize_TPM2B_CREATION_DATA(
   9361     const TPM2B_CREATION_DATA& value,
   9362     std::string* buffer) {
   9363   TPM_RC result = TPM_RC_SUCCESS;
   9364   VLOG(3) << __func__;
   9365 
   9366   std::string field_bytes;
   9367   result = Serialize_TPMS_CREATION_DATA(value.creation_data, &field_bytes);
   9368   if (result) {
   9369     return result;
   9370   }
   9371   std::string size_bytes;
   9372   result = Serialize_UINT16(field_bytes.size(), &size_bytes);
   9373   if (result) {
   9374     return result;
   9375   }
   9376   buffer->append(size_bytes + field_bytes);
   9377   return result;
   9378 }
   9379 
   9380 TPM_RC Parse_TPM2B_CREATION_DATA(
   9381     std::string* buffer,
   9382     TPM2B_CREATION_DATA* value,
   9383     std::string* value_bytes) {
   9384   TPM_RC result = TPM_RC_SUCCESS;
   9385   VLOG(3) << __func__;
   9386 
   9387   result = Parse_UINT16(
   9388       buffer,
   9389       &value->size,
   9390       value_bytes);
   9391   if (result) {
   9392     return result;
   9393   }
   9394 
   9395   result = Parse_TPMS_CREATION_DATA(
   9396       buffer,
   9397       &value->creation_data,
   9398       value_bytes);
   9399   if (result) {
   9400     return result;
   9401   }
   9402   return result;
   9403 }
   9404 
   9405 TPM2B_CREATION_DATA Make_TPM2B_CREATION_DATA(
   9406     const TPMS_CREATION_DATA& inner) {
   9407   TPM2B_CREATION_DATA tpm2b;
   9408   tpm2b.size = sizeof(TPMS_CREATION_DATA);
   9409   tpm2b.creation_data = inner;
   9410   return tpm2b;
   9411 }
   9412 
   9413 TPM_RC Tpm::SerializeCommand_Startup(
   9414       const TPM_SU& startup_type,
   9415       std::string* serialized_command,
   9416       AuthorizationDelegate* authorization_delegate) {
   9417   VLOG(3) << __func__;
   9418   TPM_RC rc = TPM_RC_SUCCESS;
   9419   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   9420   UINT32 command_size = 10;  // Header size.
   9421   std::string handle_section_bytes;
   9422   std::string parameter_section_bytes;
   9423   TPM_CC command_code = TPM_CC_Startup;
   9424   bool is_command_parameter_encryption_possible = false;
   9425   bool is_response_parameter_encryption_possible = false;
   9426   std::string command_code_bytes;
   9427   rc = Serialize_TPM_CC(
   9428       command_code,
   9429       &command_code_bytes);
   9430   if (rc != TPM_RC_SUCCESS) {
   9431     return rc;
   9432   }
   9433   std::string startup_type_bytes;
   9434   rc = Serialize_TPM_SU(
   9435       startup_type,
   9436       &startup_type_bytes);
   9437   if (rc != TPM_RC_SUCCESS) {
   9438     return rc;
   9439   }
   9440   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   9441       crypto::SecureHash::SHA256));
   9442   hash->Update(command_code_bytes.data(),
   9443                command_code_bytes.size());
   9444   hash->Update(startup_type_bytes.data(),
   9445                startup_type_bytes.size());
   9446   parameter_section_bytes += startup_type_bytes;
   9447   command_size += startup_type_bytes.size();
   9448   std::string command_hash(32, 0);
   9449   hash->Finish(string_as_array(&command_hash), command_hash.size());
   9450   std::string authorization_section_bytes;
   9451   std::string authorization_size_bytes;
   9452   if (authorization_delegate) {
   9453     if (!authorization_delegate->GetCommandAuthorization(
   9454         command_hash,
   9455         is_command_parameter_encryption_possible,
   9456         is_response_parameter_encryption_possible,
   9457         &authorization_section_bytes)) {
   9458       return TRUNKS_RC_AUTHORIZATION_FAILED;
   9459     }
   9460     if (!authorization_section_bytes.empty()) {
   9461       tag = TPM_ST_SESSIONS;
   9462       std::string tmp;
   9463       rc = Serialize_UINT32(authorization_section_bytes.size(),
   9464                             &authorization_size_bytes);
   9465       if (rc != TPM_RC_SUCCESS) {
   9466         return rc;
   9467       }
   9468       command_size += authorization_size_bytes.size() +
   9469                       authorization_section_bytes.size();
   9470     }
   9471   }
   9472   std::string tag_bytes;
   9473   rc = Serialize_TPMI_ST_COMMAND_TAG(
   9474       tag,
   9475       &tag_bytes);
   9476   if (rc != TPM_RC_SUCCESS) {
   9477     return rc;
   9478   }
   9479   std::string command_size_bytes;
   9480   rc = Serialize_UINT32(
   9481       command_size,
   9482       &command_size_bytes);
   9483   if (rc != TPM_RC_SUCCESS) {
   9484     return rc;
   9485   }
   9486   *serialized_command = tag_bytes +
   9487                         command_size_bytes +
   9488                         command_code_bytes +
   9489                         handle_section_bytes +
   9490                         authorization_size_bytes +
   9491                         authorization_section_bytes +
   9492                         parameter_section_bytes;
   9493   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   9494   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   9495                                             serialized_command->size());
   9496   return TPM_RC_SUCCESS;
   9497 }
   9498 
   9499 TPM_RC Tpm::ParseResponse_Startup(
   9500       const std::string& response,
   9501       AuthorizationDelegate* authorization_delegate) {
   9502   VLOG(3) << __func__;
   9503   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   9504   TPM_RC rc = TPM_RC_SUCCESS;
   9505   std::string buffer(response);
   9506   TPM_ST tag;
   9507   std::string tag_bytes;
   9508   rc = Parse_TPM_ST(
   9509       &buffer,
   9510       &tag,
   9511       &tag_bytes);
   9512   if (rc != TPM_RC_SUCCESS) {
   9513     return rc;
   9514   }
   9515   UINT32 response_size;
   9516   std::string response_size_bytes;
   9517   rc = Parse_UINT32(
   9518       &buffer,
   9519       &response_size,
   9520       &response_size_bytes);
   9521   if (rc != TPM_RC_SUCCESS) {
   9522     return rc;
   9523   }
   9524   TPM_RC response_code;
   9525   std::string response_code_bytes;
   9526   rc = Parse_TPM_RC(
   9527       &buffer,
   9528       &response_code,
   9529       &response_code_bytes);
   9530   if (rc != TPM_RC_SUCCESS) {
   9531     return rc;
   9532   }
   9533   if (response_size != response.size()) {
   9534     return TPM_RC_SIZE;
   9535   }
   9536   if (response_code != TPM_RC_SUCCESS) {
   9537     return response_code;
   9538   }
   9539   TPM_CC command_code = TPM_CC_Startup;
   9540   std::string command_code_bytes;
   9541   rc = Serialize_TPM_CC(
   9542       command_code,
   9543       &command_code_bytes);
   9544   if (rc != TPM_RC_SUCCESS) {
   9545     return rc;
   9546   }
   9547   std::string authorization_section_bytes;
   9548   if (tag == TPM_ST_SESSIONS) {
   9549     UINT32 parameter_section_size = buffer.size();
   9550     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   9551     if (rc != TPM_RC_SUCCESS) {
   9552       return rc;
   9553     }
   9554     if (parameter_section_size > buffer.size()) {
   9555       return TPM_RC_INSUFFICIENT;
   9556     }
   9557     authorization_section_bytes = buffer.substr(parameter_section_size);
   9558     // Keep the parameter section in |buffer|.
   9559     buffer.erase(parameter_section_size);
   9560   }
   9561   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   9562       crypto::SecureHash::SHA256));
   9563   hash->Update(response_code_bytes.data(),
   9564                response_code_bytes.size());
   9565   hash->Update(command_code_bytes.data(),
   9566                command_code_bytes.size());
   9567   hash->Update(buffer.data(),
   9568                buffer.size());
   9569   std::string response_hash(32, 0);
   9570   hash->Finish(string_as_array(&response_hash), response_hash.size());
   9571   if (tag == TPM_ST_SESSIONS) {
   9572     CHECK(authorization_delegate) << "Authorization delegate missing!";
   9573     if (!authorization_delegate->CheckResponseAuthorization(
   9574         response_hash,
   9575         authorization_section_bytes)) {
   9576       return TRUNKS_RC_AUTHORIZATION_FAILED;
   9577     }
   9578   }
   9579   return TPM_RC_SUCCESS;
   9580 }
   9581 
   9582 void StartupErrorCallback(
   9583     const Tpm::StartupResponse& callback,
   9584     TPM_RC response_code) {
   9585   VLOG(1) << __func__;
   9586   callback.Run(response_code);
   9587 }
   9588 
   9589 void StartupResponseParser(
   9590     const Tpm::StartupResponse& callback,
   9591     AuthorizationDelegate* authorization_delegate,
   9592     const std::string& response) {
   9593   VLOG(1) << __func__;
   9594   base::Callback<void(TPM_RC)> error_reporter =
   9595       base::Bind(StartupErrorCallback, callback);
   9596   TPM_RC rc = Tpm::ParseResponse_Startup(
   9597       response,
   9598       authorization_delegate);
   9599   if (rc != TPM_RC_SUCCESS) {
   9600     error_reporter.Run(rc);
   9601     return;
   9602   }
   9603   callback.Run(
   9604       rc);
   9605 }
   9606 
   9607 void Tpm::Startup(
   9608       const TPM_SU& startup_type,
   9609       AuthorizationDelegate* authorization_delegate,
   9610       const StartupResponse& callback) {
   9611   VLOG(1) << __func__;
   9612   base::Callback<void(TPM_RC)> error_reporter =
   9613       base::Bind(StartupErrorCallback, callback);
   9614   base::Callback<void(const std::string&)> parser =
   9615       base::Bind(StartupResponseParser,
   9616                  callback,
   9617                  authorization_delegate);
   9618   std::string command;
   9619   TPM_RC rc = SerializeCommand_Startup(
   9620       startup_type,
   9621       &command,
   9622       authorization_delegate);
   9623   if (rc != TPM_RC_SUCCESS) {
   9624     error_reporter.Run(rc);
   9625     return;
   9626   }
   9627   transceiver_->SendCommand(command, parser);
   9628 }
   9629 
   9630 TPM_RC Tpm::StartupSync(
   9631       const TPM_SU& startup_type,
   9632       AuthorizationDelegate* authorization_delegate) {
   9633   VLOG(1) << __func__;
   9634   std::string command;
   9635   TPM_RC rc = SerializeCommand_Startup(
   9636       startup_type,
   9637       &command,
   9638       authorization_delegate);
   9639   if (rc != TPM_RC_SUCCESS) {
   9640     return rc;
   9641   }
   9642   std::string response = transceiver_->SendCommandAndWait(command);
   9643   rc = ParseResponse_Startup(
   9644       response,
   9645       authorization_delegate);
   9646   return rc;
   9647 }
   9648 
   9649 TPM_RC Tpm::SerializeCommand_Shutdown(
   9650       const TPM_SU& shutdown_type,
   9651       std::string* serialized_command,
   9652       AuthorizationDelegate* authorization_delegate) {
   9653   VLOG(3) << __func__;
   9654   TPM_RC rc = TPM_RC_SUCCESS;
   9655   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   9656   UINT32 command_size = 10;  // Header size.
   9657   std::string handle_section_bytes;
   9658   std::string parameter_section_bytes;
   9659   TPM_CC command_code = TPM_CC_Shutdown;
   9660   bool is_command_parameter_encryption_possible = false;
   9661   bool is_response_parameter_encryption_possible = false;
   9662   std::string command_code_bytes;
   9663   rc = Serialize_TPM_CC(
   9664       command_code,
   9665       &command_code_bytes);
   9666   if (rc != TPM_RC_SUCCESS) {
   9667     return rc;
   9668   }
   9669   std::string shutdown_type_bytes;
   9670   rc = Serialize_TPM_SU(
   9671       shutdown_type,
   9672       &shutdown_type_bytes);
   9673   if (rc != TPM_RC_SUCCESS) {
   9674     return rc;
   9675   }
   9676   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   9677       crypto::SecureHash::SHA256));
   9678   hash->Update(command_code_bytes.data(),
   9679                command_code_bytes.size());
   9680   hash->Update(shutdown_type_bytes.data(),
   9681                shutdown_type_bytes.size());
   9682   parameter_section_bytes += shutdown_type_bytes;
   9683   command_size += shutdown_type_bytes.size();
   9684   std::string command_hash(32, 0);
   9685   hash->Finish(string_as_array(&command_hash), command_hash.size());
   9686   std::string authorization_section_bytes;
   9687   std::string authorization_size_bytes;
   9688   if (authorization_delegate) {
   9689     if (!authorization_delegate->GetCommandAuthorization(
   9690         command_hash,
   9691         is_command_parameter_encryption_possible,
   9692         is_response_parameter_encryption_possible,
   9693         &authorization_section_bytes)) {
   9694       return TRUNKS_RC_AUTHORIZATION_FAILED;
   9695     }
   9696     if (!authorization_section_bytes.empty()) {
   9697       tag = TPM_ST_SESSIONS;
   9698       std::string tmp;
   9699       rc = Serialize_UINT32(authorization_section_bytes.size(),
   9700                             &authorization_size_bytes);
   9701       if (rc != TPM_RC_SUCCESS) {
   9702         return rc;
   9703       }
   9704       command_size += authorization_size_bytes.size() +
   9705                       authorization_section_bytes.size();
   9706     }
   9707   }
   9708   std::string tag_bytes;
   9709   rc = Serialize_TPMI_ST_COMMAND_TAG(
   9710       tag,
   9711       &tag_bytes);
   9712   if (rc != TPM_RC_SUCCESS) {
   9713     return rc;
   9714   }
   9715   std::string command_size_bytes;
   9716   rc = Serialize_UINT32(
   9717       command_size,
   9718       &command_size_bytes);
   9719   if (rc != TPM_RC_SUCCESS) {
   9720     return rc;
   9721   }
   9722   *serialized_command = tag_bytes +
   9723                         command_size_bytes +
   9724                         command_code_bytes +
   9725                         handle_section_bytes +
   9726                         authorization_size_bytes +
   9727                         authorization_section_bytes +
   9728                         parameter_section_bytes;
   9729   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   9730   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   9731                                             serialized_command->size());
   9732   return TPM_RC_SUCCESS;
   9733 }
   9734 
   9735 TPM_RC Tpm::ParseResponse_Shutdown(
   9736       const std::string& response,
   9737       AuthorizationDelegate* authorization_delegate) {
   9738   VLOG(3) << __func__;
   9739   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   9740   TPM_RC rc = TPM_RC_SUCCESS;
   9741   std::string buffer(response);
   9742   TPM_ST tag;
   9743   std::string tag_bytes;
   9744   rc = Parse_TPM_ST(
   9745       &buffer,
   9746       &tag,
   9747       &tag_bytes);
   9748   if (rc != TPM_RC_SUCCESS) {
   9749     return rc;
   9750   }
   9751   UINT32 response_size;
   9752   std::string response_size_bytes;
   9753   rc = Parse_UINT32(
   9754       &buffer,
   9755       &response_size,
   9756       &response_size_bytes);
   9757   if (rc != TPM_RC_SUCCESS) {
   9758     return rc;
   9759   }
   9760   TPM_RC response_code;
   9761   std::string response_code_bytes;
   9762   rc = Parse_TPM_RC(
   9763       &buffer,
   9764       &response_code,
   9765       &response_code_bytes);
   9766   if (rc != TPM_RC_SUCCESS) {
   9767     return rc;
   9768   }
   9769   if (response_size != response.size()) {
   9770     return TPM_RC_SIZE;
   9771   }
   9772   if (response_code != TPM_RC_SUCCESS) {
   9773     return response_code;
   9774   }
   9775   TPM_CC command_code = TPM_CC_Shutdown;
   9776   std::string command_code_bytes;
   9777   rc = Serialize_TPM_CC(
   9778       command_code,
   9779       &command_code_bytes);
   9780   if (rc != TPM_RC_SUCCESS) {
   9781     return rc;
   9782   }
   9783   std::string authorization_section_bytes;
   9784   if (tag == TPM_ST_SESSIONS) {
   9785     UINT32 parameter_section_size = buffer.size();
   9786     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   9787     if (rc != TPM_RC_SUCCESS) {
   9788       return rc;
   9789     }
   9790     if (parameter_section_size > buffer.size()) {
   9791       return TPM_RC_INSUFFICIENT;
   9792     }
   9793     authorization_section_bytes = buffer.substr(parameter_section_size);
   9794     // Keep the parameter section in |buffer|.
   9795     buffer.erase(parameter_section_size);
   9796   }
   9797   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   9798       crypto::SecureHash::SHA256));
   9799   hash->Update(response_code_bytes.data(),
   9800                response_code_bytes.size());
   9801   hash->Update(command_code_bytes.data(),
   9802                command_code_bytes.size());
   9803   hash->Update(buffer.data(),
   9804                buffer.size());
   9805   std::string response_hash(32, 0);
   9806   hash->Finish(string_as_array(&response_hash), response_hash.size());
   9807   if (tag == TPM_ST_SESSIONS) {
   9808     CHECK(authorization_delegate) << "Authorization delegate missing!";
   9809     if (!authorization_delegate->CheckResponseAuthorization(
   9810         response_hash,
   9811         authorization_section_bytes)) {
   9812       return TRUNKS_RC_AUTHORIZATION_FAILED;
   9813     }
   9814   }
   9815   return TPM_RC_SUCCESS;
   9816 }
   9817 
   9818 void ShutdownErrorCallback(
   9819     const Tpm::ShutdownResponse& callback,
   9820     TPM_RC response_code) {
   9821   VLOG(1) << __func__;
   9822   callback.Run(response_code);
   9823 }
   9824 
   9825 void ShutdownResponseParser(
   9826     const Tpm::ShutdownResponse& callback,
   9827     AuthorizationDelegate* authorization_delegate,
   9828     const std::string& response) {
   9829   VLOG(1) << __func__;
   9830   base::Callback<void(TPM_RC)> error_reporter =
   9831       base::Bind(ShutdownErrorCallback, callback);
   9832   TPM_RC rc = Tpm::ParseResponse_Shutdown(
   9833       response,
   9834       authorization_delegate);
   9835   if (rc != TPM_RC_SUCCESS) {
   9836     error_reporter.Run(rc);
   9837     return;
   9838   }
   9839   callback.Run(
   9840       rc);
   9841 }
   9842 
   9843 void Tpm::Shutdown(
   9844       const TPM_SU& shutdown_type,
   9845       AuthorizationDelegate* authorization_delegate,
   9846       const ShutdownResponse& callback) {
   9847   VLOG(1) << __func__;
   9848   base::Callback<void(TPM_RC)> error_reporter =
   9849       base::Bind(ShutdownErrorCallback, callback);
   9850   base::Callback<void(const std::string&)> parser =
   9851       base::Bind(ShutdownResponseParser,
   9852                  callback,
   9853                  authorization_delegate);
   9854   std::string command;
   9855   TPM_RC rc = SerializeCommand_Shutdown(
   9856       shutdown_type,
   9857       &command,
   9858       authorization_delegate);
   9859   if (rc != TPM_RC_SUCCESS) {
   9860     error_reporter.Run(rc);
   9861     return;
   9862   }
   9863   transceiver_->SendCommand(command, parser);
   9864 }
   9865 
   9866 TPM_RC Tpm::ShutdownSync(
   9867       const TPM_SU& shutdown_type,
   9868       AuthorizationDelegate* authorization_delegate) {
   9869   VLOG(1) << __func__;
   9870   std::string command;
   9871   TPM_RC rc = SerializeCommand_Shutdown(
   9872       shutdown_type,
   9873       &command,
   9874       authorization_delegate);
   9875   if (rc != TPM_RC_SUCCESS) {
   9876     return rc;
   9877   }
   9878   std::string response = transceiver_->SendCommandAndWait(command);
   9879   rc = ParseResponse_Shutdown(
   9880       response,
   9881       authorization_delegate);
   9882   return rc;
   9883 }
   9884 
   9885 TPM_RC Tpm::SerializeCommand_SelfTest(
   9886       const TPMI_YES_NO& full_test,
   9887       std::string* serialized_command,
   9888       AuthorizationDelegate* authorization_delegate) {
   9889   VLOG(3) << __func__;
   9890   TPM_RC rc = TPM_RC_SUCCESS;
   9891   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   9892   UINT32 command_size = 10;  // Header size.
   9893   std::string handle_section_bytes;
   9894   std::string parameter_section_bytes;
   9895   TPM_CC command_code = TPM_CC_SelfTest;
   9896   bool is_command_parameter_encryption_possible = false;
   9897   bool is_response_parameter_encryption_possible = false;
   9898   std::string command_code_bytes;
   9899   rc = Serialize_TPM_CC(
   9900       command_code,
   9901       &command_code_bytes);
   9902   if (rc != TPM_RC_SUCCESS) {
   9903     return rc;
   9904   }
   9905   std::string full_test_bytes;
   9906   rc = Serialize_TPMI_YES_NO(
   9907       full_test,
   9908       &full_test_bytes);
   9909   if (rc != TPM_RC_SUCCESS) {
   9910     return rc;
   9911   }
   9912   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   9913       crypto::SecureHash::SHA256));
   9914   hash->Update(command_code_bytes.data(),
   9915                command_code_bytes.size());
   9916   hash->Update(full_test_bytes.data(),
   9917                full_test_bytes.size());
   9918   parameter_section_bytes += full_test_bytes;
   9919   command_size += full_test_bytes.size();
   9920   std::string command_hash(32, 0);
   9921   hash->Finish(string_as_array(&command_hash), command_hash.size());
   9922   std::string authorization_section_bytes;
   9923   std::string authorization_size_bytes;
   9924   if (authorization_delegate) {
   9925     if (!authorization_delegate->GetCommandAuthorization(
   9926         command_hash,
   9927         is_command_parameter_encryption_possible,
   9928         is_response_parameter_encryption_possible,
   9929         &authorization_section_bytes)) {
   9930       return TRUNKS_RC_AUTHORIZATION_FAILED;
   9931     }
   9932     if (!authorization_section_bytes.empty()) {
   9933       tag = TPM_ST_SESSIONS;
   9934       std::string tmp;
   9935       rc = Serialize_UINT32(authorization_section_bytes.size(),
   9936                             &authorization_size_bytes);
   9937       if (rc != TPM_RC_SUCCESS) {
   9938         return rc;
   9939       }
   9940       command_size += authorization_size_bytes.size() +
   9941                       authorization_section_bytes.size();
   9942     }
   9943   }
   9944   std::string tag_bytes;
   9945   rc = Serialize_TPMI_ST_COMMAND_TAG(
   9946       tag,
   9947       &tag_bytes);
   9948   if (rc != TPM_RC_SUCCESS) {
   9949     return rc;
   9950   }
   9951   std::string command_size_bytes;
   9952   rc = Serialize_UINT32(
   9953       command_size,
   9954       &command_size_bytes);
   9955   if (rc != TPM_RC_SUCCESS) {
   9956     return rc;
   9957   }
   9958   *serialized_command = tag_bytes +
   9959                         command_size_bytes +
   9960                         command_code_bytes +
   9961                         handle_section_bytes +
   9962                         authorization_size_bytes +
   9963                         authorization_section_bytes +
   9964                         parameter_section_bytes;
   9965   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   9966   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   9967                                             serialized_command->size());
   9968   return TPM_RC_SUCCESS;
   9969 }
   9970 
   9971 TPM_RC Tpm::ParseResponse_SelfTest(
   9972       const std::string& response,
   9973       AuthorizationDelegate* authorization_delegate) {
   9974   VLOG(3) << __func__;
   9975   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   9976   TPM_RC rc = TPM_RC_SUCCESS;
   9977   std::string buffer(response);
   9978   TPM_ST tag;
   9979   std::string tag_bytes;
   9980   rc = Parse_TPM_ST(
   9981       &buffer,
   9982       &tag,
   9983       &tag_bytes);
   9984   if (rc != TPM_RC_SUCCESS) {
   9985     return rc;
   9986   }
   9987   UINT32 response_size;
   9988   std::string response_size_bytes;
   9989   rc = Parse_UINT32(
   9990       &buffer,
   9991       &response_size,
   9992       &response_size_bytes);
   9993   if (rc != TPM_RC_SUCCESS) {
   9994     return rc;
   9995   }
   9996   TPM_RC response_code;
   9997   std::string response_code_bytes;
   9998   rc = Parse_TPM_RC(
   9999       &buffer,
   10000       &response_code,
   10001       &response_code_bytes);
   10002   if (rc != TPM_RC_SUCCESS) {
   10003     return rc;
   10004   }
   10005   if (response_size != response.size()) {
   10006     return TPM_RC_SIZE;
   10007   }
   10008   if (response_code != TPM_RC_SUCCESS) {
   10009     return response_code;
   10010   }
   10011   TPM_CC command_code = TPM_CC_SelfTest;
   10012   std::string command_code_bytes;
   10013   rc = Serialize_TPM_CC(
   10014       command_code,
   10015       &command_code_bytes);
   10016   if (rc != TPM_RC_SUCCESS) {
   10017     return rc;
   10018   }
   10019   std::string authorization_section_bytes;
   10020   if (tag == TPM_ST_SESSIONS) {
   10021     UINT32 parameter_section_size = buffer.size();
   10022     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   10023     if (rc != TPM_RC_SUCCESS) {
   10024       return rc;
   10025     }
   10026     if (parameter_section_size > buffer.size()) {
   10027       return TPM_RC_INSUFFICIENT;
   10028     }
   10029     authorization_section_bytes = buffer.substr(parameter_section_size);
   10030     // Keep the parameter section in |buffer|.
   10031     buffer.erase(parameter_section_size);
   10032   }
   10033   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10034       crypto::SecureHash::SHA256));
   10035   hash->Update(response_code_bytes.data(),
   10036                response_code_bytes.size());
   10037   hash->Update(command_code_bytes.data(),
   10038                command_code_bytes.size());
   10039   hash->Update(buffer.data(),
   10040                buffer.size());
   10041   std::string response_hash(32, 0);
   10042   hash->Finish(string_as_array(&response_hash), response_hash.size());
   10043   if (tag == TPM_ST_SESSIONS) {
   10044     CHECK(authorization_delegate) << "Authorization delegate missing!";
   10045     if (!authorization_delegate->CheckResponseAuthorization(
   10046         response_hash,
   10047         authorization_section_bytes)) {
   10048       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10049     }
   10050   }
   10051   return TPM_RC_SUCCESS;
   10052 }
   10053 
   10054 void SelfTestErrorCallback(
   10055     const Tpm::SelfTestResponse& callback,
   10056     TPM_RC response_code) {
   10057   VLOG(1) << __func__;
   10058   callback.Run(response_code);
   10059 }
   10060 
   10061 void SelfTestResponseParser(
   10062     const Tpm::SelfTestResponse& callback,
   10063     AuthorizationDelegate* authorization_delegate,
   10064     const std::string& response) {
   10065   VLOG(1) << __func__;
   10066   base::Callback<void(TPM_RC)> error_reporter =
   10067       base::Bind(SelfTestErrorCallback, callback);
   10068   TPM_RC rc = Tpm::ParseResponse_SelfTest(
   10069       response,
   10070       authorization_delegate);
   10071   if (rc != TPM_RC_SUCCESS) {
   10072     error_reporter.Run(rc);
   10073     return;
   10074   }
   10075   callback.Run(
   10076       rc);
   10077 }
   10078 
   10079 void Tpm::SelfTest(
   10080       const TPMI_YES_NO& full_test,
   10081       AuthorizationDelegate* authorization_delegate,
   10082       const SelfTestResponse& callback) {
   10083   VLOG(1) << __func__;
   10084   base::Callback<void(TPM_RC)> error_reporter =
   10085       base::Bind(SelfTestErrorCallback, callback);
   10086   base::Callback<void(const std::string&)> parser =
   10087       base::Bind(SelfTestResponseParser,
   10088                  callback,
   10089                  authorization_delegate);
   10090   std::string command;
   10091   TPM_RC rc = SerializeCommand_SelfTest(
   10092       full_test,
   10093       &command,
   10094       authorization_delegate);
   10095   if (rc != TPM_RC_SUCCESS) {
   10096     error_reporter.Run(rc);
   10097     return;
   10098   }
   10099   transceiver_->SendCommand(command, parser);
   10100 }
   10101 
   10102 TPM_RC Tpm::SelfTestSync(
   10103       const TPMI_YES_NO& full_test,
   10104       AuthorizationDelegate* authorization_delegate) {
   10105   VLOG(1) << __func__;
   10106   std::string command;
   10107   TPM_RC rc = SerializeCommand_SelfTest(
   10108       full_test,
   10109       &command,
   10110       authorization_delegate);
   10111   if (rc != TPM_RC_SUCCESS) {
   10112     return rc;
   10113   }
   10114   std::string response = transceiver_->SendCommandAndWait(command);
   10115   rc = ParseResponse_SelfTest(
   10116       response,
   10117       authorization_delegate);
   10118   return rc;
   10119 }
   10120 
   10121 TPM_RC Tpm::SerializeCommand_IncrementalSelfTest(
   10122       const TPML_ALG& to_test,
   10123       std::string* serialized_command,
   10124       AuthorizationDelegate* authorization_delegate) {
   10125   VLOG(3) << __func__;
   10126   TPM_RC rc = TPM_RC_SUCCESS;
   10127   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   10128   UINT32 command_size = 10;  // Header size.
   10129   std::string handle_section_bytes;
   10130   std::string parameter_section_bytes;
   10131   TPM_CC command_code = TPM_CC_IncrementalSelfTest;
   10132   bool is_command_parameter_encryption_possible = false;
   10133   bool is_response_parameter_encryption_possible = false;
   10134   std::string command_code_bytes;
   10135   rc = Serialize_TPM_CC(
   10136       command_code,
   10137       &command_code_bytes);
   10138   if (rc != TPM_RC_SUCCESS) {
   10139     return rc;
   10140   }
   10141   std::string to_test_bytes;
   10142   rc = Serialize_TPML_ALG(
   10143       to_test,
   10144       &to_test_bytes);
   10145   if (rc != TPM_RC_SUCCESS) {
   10146     return rc;
   10147   }
   10148   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10149       crypto::SecureHash::SHA256));
   10150   hash->Update(command_code_bytes.data(),
   10151                command_code_bytes.size());
   10152   hash->Update(to_test_bytes.data(),
   10153                to_test_bytes.size());
   10154   parameter_section_bytes += to_test_bytes;
   10155   command_size += to_test_bytes.size();
   10156   std::string command_hash(32, 0);
   10157   hash->Finish(string_as_array(&command_hash), command_hash.size());
   10158   std::string authorization_section_bytes;
   10159   std::string authorization_size_bytes;
   10160   if (authorization_delegate) {
   10161     if (!authorization_delegate->GetCommandAuthorization(
   10162         command_hash,
   10163         is_command_parameter_encryption_possible,
   10164         is_response_parameter_encryption_possible,
   10165         &authorization_section_bytes)) {
   10166       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10167     }
   10168     if (!authorization_section_bytes.empty()) {
   10169       tag = TPM_ST_SESSIONS;
   10170       std::string tmp;
   10171       rc = Serialize_UINT32(authorization_section_bytes.size(),
   10172                             &authorization_size_bytes);
   10173       if (rc != TPM_RC_SUCCESS) {
   10174         return rc;
   10175       }
   10176       command_size += authorization_size_bytes.size() +
   10177                       authorization_section_bytes.size();
   10178     }
   10179   }
   10180   std::string tag_bytes;
   10181   rc = Serialize_TPMI_ST_COMMAND_TAG(
   10182       tag,
   10183       &tag_bytes);
   10184   if (rc != TPM_RC_SUCCESS) {
   10185     return rc;
   10186   }
   10187   std::string command_size_bytes;
   10188   rc = Serialize_UINT32(
   10189       command_size,
   10190       &command_size_bytes);
   10191   if (rc != TPM_RC_SUCCESS) {
   10192     return rc;
   10193   }
   10194   *serialized_command = tag_bytes +
   10195                         command_size_bytes +
   10196                         command_code_bytes +
   10197                         handle_section_bytes +
   10198                         authorization_size_bytes +
   10199                         authorization_section_bytes +
   10200                         parameter_section_bytes;
   10201   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   10202   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   10203                                             serialized_command->size());
   10204   return TPM_RC_SUCCESS;
   10205 }
   10206 
   10207 TPM_RC Tpm::ParseResponse_IncrementalSelfTest(
   10208       const std::string& response,
   10209       TPML_ALG* to_do_list,
   10210       AuthorizationDelegate* authorization_delegate) {
   10211   VLOG(3) << __func__;
   10212   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   10213   TPM_RC rc = TPM_RC_SUCCESS;
   10214   std::string buffer(response);
   10215   TPM_ST tag;
   10216   std::string tag_bytes;
   10217   rc = Parse_TPM_ST(
   10218       &buffer,
   10219       &tag,
   10220       &tag_bytes);
   10221   if (rc != TPM_RC_SUCCESS) {
   10222     return rc;
   10223   }
   10224   UINT32 response_size;
   10225   std::string response_size_bytes;
   10226   rc = Parse_UINT32(
   10227       &buffer,
   10228       &response_size,
   10229       &response_size_bytes);
   10230   if (rc != TPM_RC_SUCCESS) {
   10231     return rc;
   10232   }
   10233   TPM_RC response_code;
   10234   std::string response_code_bytes;
   10235   rc = Parse_TPM_RC(
   10236       &buffer,
   10237       &response_code,
   10238       &response_code_bytes);
   10239   if (rc != TPM_RC_SUCCESS) {
   10240     return rc;
   10241   }
   10242   if (response_size != response.size()) {
   10243     return TPM_RC_SIZE;
   10244   }
   10245   if (response_code != TPM_RC_SUCCESS) {
   10246     return response_code;
   10247   }
   10248   TPM_CC command_code = TPM_CC_IncrementalSelfTest;
   10249   std::string command_code_bytes;
   10250   rc = Serialize_TPM_CC(
   10251       command_code,
   10252       &command_code_bytes);
   10253   if (rc != TPM_RC_SUCCESS) {
   10254     return rc;
   10255   }
   10256   std::string authorization_section_bytes;
   10257   if (tag == TPM_ST_SESSIONS) {
   10258     UINT32 parameter_section_size = buffer.size();
   10259     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   10260     if (rc != TPM_RC_SUCCESS) {
   10261       return rc;
   10262     }
   10263     if (parameter_section_size > buffer.size()) {
   10264       return TPM_RC_INSUFFICIENT;
   10265     }
   10266     authorization_section_bytes = buffer.substr(parameter_section_size);
   10267     // Keep the parameter section in |buffer|.
   10268     buffer.erase(parameter_section_size);
   10269   }
   10270   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10271       crypto::SecureHash::SHA256));
   10272   hash->Update(response_code_bytes.data(),
   10273                response_code_bytes.size());
   10274   hash->Update(command_code_bytes.data(),
   10275                command_code_bytes.size());
   10276   hash->Update(buffer.data(),
   10277                buffer.size());
   10278   std::string response_hash(32, 0);
   10279   hash->Finish(string_as_array(&response_hash), response_hash.size());
   10280   if (tag == TPM_ST_SESSIONS) {
   10281     CHECK(authorization_delegate) << "Authorization delegate missing!";
   10282     if (!authorization_delegate->CheckResponseAuthorization(
   10283         response_hash,
   10284         authorization_section_bytes)) {
   10285       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10286     }
   10287   }
   10288   std::string to_do_list_bytes;
   10289   rc = Parse_TPML_ALG(
   10290       &buffer,
   10291       to_do_list,
   10292       &to_do_list_bytes);
   10293   if (rc != TPM_RC_SUCCESS) {
   10294     return rc;
   10295   }
   10296   return TPM_RC_SUCCESS;
   10297 }
   10298 
   10299 void IncrementalSelfTestErrorCallback(
   10300     const Tpm::IncrementalSelfTestResponse& callback,
   10301     TPM_RC response_code) {
   10302   VLOG(1) << __func__;
   10303   callback.Run(response_code,
   10304                TPML_ALG());
   10305 }
   10306 
   10307 void IncrementalSelfTestResponseParser(
   10308     const Tpm::IncrementalSelfTestResponse& callback,
   10309     AuthorizationDelegate* authorization_delegate,
   10310     const std::string& response) {
   10311   VLOG(1) << __func__;
   10312   base::Callback<void(TPM_RC)> error_reporter =
   10313       base::Bind(IncrementalSelfTestErrorCallback, callback);
   10314   TPML_ALG to_do_list;
   10315   TPM_RC rc = Tpm::ParseResponse_IncrementalSelfTest(
   10316       response,
   10317       &to_do_list,
   10318       authorization_delegate);
   10319   if (rc != TPM_RC_SUCCESS) {
   10320     error_reporter.Run(rc);
   10321     return;
   10322   }
   10323   callback.Run(
   10324       rc,
   10325       to_do_list);
   10326 }
   10327 
   10328 void Tpm::IncrementalSelfTest(
   10329       const TPML_ALG& to_test,
   10330       AuthorizationDelegate* authorization_delegate,
   10331       const IncrementalSelfTestResponse& callback) {
   10332   VLOG(1) << __func__;
   10333   base::Callback<void(TPM_RC)> error_reporter =
   10334       base::Bind(IncrementalSelfTestErrorCallback, callback);
   10335   base::Callback<void(const std::string&)> parser =
   10336       base::Bind(IncrementalSelfTestResponseParser,
   10337                  callback,
   10338                  authorization_delegate);
   10339   std::string command;
   10340   TPM_RC rc = SerializeCommand_IncrementalSelfTest(
   10341       to_test,
   10342       &command,
   10343       authorization_delegate);
   10344   if (rc != TPM_RC_SUCCESS) {
   10345     error_reporter.Run(rc);
   10346     return;
   10347   }
   10348   transceiver_->SendCommand(command, parser);
   10349 }
   10350 
   10351 TPM_RC Tpm::IncrementalSelfTestSync(
   10352       const TPML_ALG& to_test,
   10353       TPML_ALG* to_do_list,
   10354       AuthorizationDelegate* authorization_delegate) {
   10355   VLOG(1) << __func__;
   10356   std::string command;
   10357   TPM_RC rc = SerializeCommand_IncrementalSelfTest(
   10358       to_test,
   10359       &command,
   10360       authorization_delegate);
   10361   if (rc != TPM_RC_SUCCESS) {
   10362     return rc;
   10363   }
   10364   std::string response = transceiver_->SendCommandAndWait(command);
   10365   rc = ParseResponse_IncrementalSelfTest(
   10366       response,
   10367       to_do_list,
   10368       authorization_delegate);
   10369   return rc;
   10370 }
   10371 
   10372 TPM_RC Tpm::SerializeCommand_GetTestResult(
   10373       std::string* serialized_command,
   10374       AuthorizationDelegate* authorization_delegate) {
   10375   VLOG(3) << __func__;
   10376   TPM_RC rc = TPM_RC_SUCCESS;
   10377   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   10378   UINT32 command_size = 10;  // Header size.
   10379   std::string handle_section_bytes;
   10380   std::string parameter_section_bytes;
   10381   TPM_CC command_code = TPM_CC_GetTestResult;
   10382   bool is_command_parameter_encryption_possible = false;
   10383   bool is_response_parameter_encryption_possible = true;
   10384   std::string command_code_bytes;
   10385   rc = Serialize_TPM_CC(
   10386       command_code,
   10387       &command_code_bytes);
   10388   if (rc != TPM_RC_SUCCESS) {
   10389     return rc;
   10390   }
   10391   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10392       crypto::SecureHash::SHA256));
   10393   hash->Update(command_code_bytes.data(),
   10394                command_code_bytes.size());
   10395   std::string command_hash(32, 0);
   10396   hash->Finish(string_as_array(&command_hash), command_hash.size());
   10397   std::string authorization_section_bytes;
   10398   std::string authorization_size_bytes;
   10399   if (authorization_delegate) {
   10400     if (!authorization_delegate->GetCommandAuthorization(
   10401         command_hash,
   10402         is_command_parameter_encryption_possible,
   10403         is_response_parameter_encryption_possible,
   10404         &authorization_section_bytes)) {
   10405       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10406     }
   10407     if (!authorization_section_bytes.empty()) {
   10408       tag = TPM_ST_SESSIONS;
   10409       std::string tmp;
   10410       rc = Serialize_UINT32(authorization_section_bytes.size(),
   10411                             &authorization_size_bytes);
   10412       if (rc != TPM_RC_SUCCESS) {
   10413         return rc;
   10414       }
   10415       command_size += authorization_size_bytes.size() +
   10416                       authorization_section_bytes.size();
   10417     }
   10418   }
   10419   std::string tag_bytes;
   10420   rc = Serialize_TPMI_ST_COMMAND_TAG(
   10421       tag,
   10422       &tag_bytes);
   10423   if (rc != TPM_RC_SUCCESS) {
   10424     return rc;
   10425   }
   10426   std::string command_size_bytes;
   10427   rc = Serialize_UINT32(
   10428       command_size,
   10429       &command_size_bytes);
   10430   if (rc != TPM_RC_SUCCESS) {
   10431     return rc;
   10432   }
   10433   *serialized_command = tag_bytes +
   10434                         command_size_bytes +
   10435                         command_code_bytes +
   10436                         handle_section_bytes +
   10437                         authorization_size_bytes +
   10438                         authorization_section_bytes +
   10439                         parameter_section_bytes;
   10440   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   10441   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   10442                                             serialized_command->size());
   10443   return TPM_RC_SUCCESS;
   10444 }
   10445 
   10446 TPM_RC Tpm::ParseResponse_GetTestResult(
   10447       const std::string& response,
   10448       TPM2B_MAX_BUFFER* out_data,
   10449       TPM_RC* test_result,
   10450       AuthorizationDelegate* authorization_delegate) {
   10451   VLOG(3) << __func__;
   10452   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   10453   TPM_RC rc = TPM_RC_SUCCESS;
   10454   std::string buffer(response);
   10455   TPM_ST tag;
   10456   std::string tag_bytes;
   10457   rc = Parse_TPM_ST(
   10458       &buffer,
   10459       &tag,
   10460       &tag_bytes);
   10461   if (rc != TPM_RC_SUCCESS) {
   10462     return rc;
   10463   }
   10464   UINT32 response_size;
   10465   std::string response_size_bytes;
   10466   rc = Parse_UINT32(
   10467       &buffer,
   10468       &response_size,
   10469       &response_size_bytes);
   10470   if (rc != TPM_RC_SUCCESS) {
   10471     return rc;
   10472   }
   10473   TPM_RC response_code;
   10474   std::string response_code_bytes;
   10475   rc = Parse_TPM_RC(
   10476       &buffer,
   10477       &response_code,
   10478       &response_code_bytes);
   10479   if (rc != TPM_RC_SUCCESS) {
   10480     return rc;
   10481   }
   10482   if (response_size != response.size()) {
   10483     return TPM_RC_SIZE;
   10484   }
   10485   if (response_code != TPM_RC_SUCCESS) {
   10486     return response_code;
   10487   }
   10488   TPM_CC command_code = TPM_CC_GetTestResult;
   10489   std::string command_code_bytes;
   10490   rc = Serialize_TPM_CC(
   10491       command_code,
   10492       &command_code_bytes);
   10493   if (rc != TPM_RC_SUCCESS) {
   10494     return rc;
   10495   }
   10496   std::string authorization_section_bytes;
   10497   if (tag == TPM_ST_SESSIONS) {
   10498     UINT32 parameter_section_size = buffer.size();
   10499     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   10500     if (rc != TPM_RC_SUCCESS) {
   10501       return rc;
   10502     }
   10503     if (parameter_section_size > buffer.size()) {
   10504       return TPM_RC_INSUFFICIENT;
   10505     }
   10506     authorization_section_bytes = buffer.substr(parameter_section_size);
   10507     // Keep the parameter section in |buffer|.
   10508     buffer.erase(parameter_section_size);
   10509   }
   10510   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10511       crypto::SecureHash::SHA256));
   10512   hash->Update(response_code_bytes.data(),
   10513                response_code_bytes.size());
   10514   hash->Update(command_code_bytes.data(),
   10515                command_code_bytes.size());
   10516   hash->Update(buffer.data(),
   10517                buffer.size());
   10518   std::string response_hash(32, 0);
   10519   hash->Finish(string_as_array(&response_hash), response_hash.size());
   10520   if (tag == TPM_ST_SESSIONS) {
   10521     CHECK(authorization_delegate) << "Authorization delegate missing!";
   10522     if (!authorization_delegate->CheckResponseAuthorization(
   10523         response_hash,
   10524         authorization_section_bytes)) {
   10525       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10526     }
   10527   }
   10528   std::string out_data_bytes;
   10529   rc = Parse_TPM2B_MAX_BUFFER(
   10530       &buffer,
   10531       out_data,
   10532       &out_data_bytes);
   10533   if (rc != TPM_RC_SUCCESS) {
   10534     return rc;
   10535   }
   10536   std::string test_result_bytes;
   10537   rc = Parse_TPM_RC(
   10538       &buffer,
   10539       test_result,
   10540       &test_result_bytes);
   10541   if (rc != TPM_RC_SUCCESS) {
   10542     return rc;
   10543   }
   10544   if (tag == TPM_ST_SESSIONS) {
   10545     CHECK(authorization_delegate) << "Authorization delegate missing!";
   10546     // Decrypt just the parameter data, not the size.
   10547     std::string tmp = out_data_bytes.substr(2);
   10548     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   10549       return TRUNKS_RC_ENCRYPTION_FAILED;
   10550     }
   10551     out_data_bytes.replace(2, std::string::npos, tmp);
   10552     rc = Parse_TPM2B_MAX_BUFFER(
   10553         &out_data_bytes,
   10554         out_data,
   10555         nullptr);
   10556     if (rc != TPM_RC_SUCCESS) {
   10557       return rc;
   10558     }
   10559   }
   10560   return TPM_RC_SUCCESS;
   10561 }
   10562 
   10563 void GetTestResultErrorCallback(
   10564     const Tpm::GetTestResultResponse& callback,
   10565     TPM_RC response_code) {
   10566   VLOG(1) << __func__;
   10567   callback.Run(response_code,
   10568                TPM2B_MAX_BUFFER(),
   10569                TPM_RC());
   10570 }
   10571 
   10572 void GetTestResultResponseParser(
   10573     const Tpm::GetTestResultResponse& callback,
   10574     AuthorizationDelegate* authorization_delegate,
   10575     const std::string& response) {
   10576   VLOG(1) << __func__;
   10577   base::Callback<void(TPM_RC)> error_reporter =
   10578       base::Bind(GetTestResultErrorCallback, callback);
   10579   TPM2B_MAX_BUFFER out_data;
   10580   TPM_RC test_result;
   10581   TPM_RC rc = Tpm::ParseResponse_GetTestResult(
   10582       response,
   10583       &out_data,
   10584       &test_result,
   10585       authorization_delegate);
   10586   if (rc != TPM_RC_SUCCESS) {
   10587     error_reporter.Run(rc);
   10588     return;
   10589   }
   10590   callback.Run(
   10591       rc,
   10592       out_data,
   10593       test_result);
   10594 }
   10595 
   10596 void Tpm::GetTestResult(
   10597       AuthorizationDelegate* authorization_delegate,
   10598       const GetTestResultResponse& callback) {
   10599   VLOG(1) << __func__;
   10600   base::Callback<void(TPM_RC)> error_reporter =
   10601       base::Bind(GetTestResultErrorCallback, callback);
   10602   base::Callback<void(const std::string&)> parser =
   10603       base::Bind(GetTestResultResponseParser,
   10604                  callback,
   10605                  authorization_delegate);
   10606   std::string command;
   10607   TPM_RC rc = SerializeCommand_GetTestResult(
   10608       &command,
   10609       authorization_delegate);
   10610   if (rc != TPM_RC_SUCCESS) {
   10611     error_reporter.Run(rc);
   10612     return;
   10613   }
   10614   transceiver_->SendCommand(command, parser);
   10615 }
   10616 
   10617 TPM_RC Tpm::GetTestResultSync(
   10618       TPM2B_MAX_BUFFER* out_data,
   10619       TPM_RC* test_result,
   10620       AuthorizationDelegate* authorization_delegate) {
   10621   VLOG(1) << __func__;
   10622   std::string command;
   10623   TPM_RC rc = SerializeCommand_GetTestResult(
   10624       &command,
   10625       authorization_delegate);
   10626   if (rc != TPM_RC_SUCCESS) {
   10627     return rc;
   10628   }
   10629   std::string response = transceiver_->SendCommandAndWait(command);
   10630   rc = ParseResponse_GetTestResult(
   10631       response,
   10632       out_data,
   10633       test_result,
   10634       authorization_delegate);
   10635   return rc;
   10636 }
   10637 
   10638 TPM_RC Tpm::SerializeCommand_StartAuthSession(
   10639       const TPMI_DH_OBJECT& tpm_key,
   10640       const std::string& tpm_key_name,
   10641       const TPMI_DH_ENTITY& bind,
   10642       const std::string& bind_name,
   10643       const TPM2B_NONCE& nonce_caller,
   10644       const TPM2B_ENCRYPTED_SECRET& encrypted_salt,
   10645       const TPM_SE& session_type,
   10646       const TPMT_SYM_DEF& symmetric,
   10647       const TPMI_ALG_HASH& auth_hash,
   10648       std::string* serialized_command,
   10649       AuthorizationDelegate* authorization_delegate) {
   10650   VLOG(3) << __func__;
   10651   TPM_RC rc = TPM_RC_SUCCESS;
   10652   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   10653   UINT32 command_size = 10;  // Header size.
   10654   std::string handle_section_bytes;
   10655   std::string parameter_section_bytes;
   10656   TPM_CC command_code = TPM_CC_StartAuthSession;
   10657   bool is_command_parameter_encryption_possible = true;
   10658   bool is_response_parameter_encryption_possible = true;
   10659   std::string command_code_bytes;
   10660   rc = Serialize_TPM_CC(
   10661       command_code,
   10662       &command_code_bytes);
   10663   if (rc != TPM_RC_SUCCESS) {
   10664     return rc;
   10665   }
   10666   std::string tpm_key_bytes;
   10667   rc = Serialize_TPMI_DH_OBJECT(
   10668       tpm_key,
   10669       &tpm_key_bytes);
   10670   if (rc != TPM_RC_SUCCESS) {
   10671     return rc;
   10672   }
   10673   std::string bind_bytes;
   10674   rc = Serialize_TPMI_DH_ENTITY(
   10675       bind,
   10676       &bind_bytes);
   10677   if (rc != TPM_RC_SUCCESS) {
   10678     return rc;
   10679   }
   10680   std::string nonce_caller_bytes;
   10681   rc = Serialize_TPM2B_NONCE(
   10682       nonce_caller,
   10683       &nonce_caller_bytes);
   10684   if (rc != TPM_RC_SUCCESS) {
   10685     return rc;
   10686   }
   10687   std::string encrypted_salt_bytes;
   10688   rc = Serialize_TPM2B_ENCRYPTED_SECRET(
   10689       encrypted_salt,
   10690       &encrypted_salt_bytes);
   10691   if (rc != TPM_RC_SUCCESS) {
   10692     return rc;
   10693   }
   10694   std::string session_type_bytes;
   10695   rc = Serialize_TPM_SE(
   10696       session_type,
   10697       &session_type_bytes);
   10698   if (rc != TPM_RC_SUCCESS) {
   10699     return rc;
   10700   }
   10701   std::string symmetric_bytes;
   10702   rc = Serialize_TPMT_SYM_DEF(
   10703       symmetric,
   10704       &symmetric_bytes);
   10705   if (rc != TPM_RC_SUCCESS) {
   10706     return rc;
   10707   }
   10708   std::string auth_hash_bytes;
   10709   rc = Serialize_TPMI_ALG_HASH(
   10710       auth_hash,
   10711       &auth_hash_bytes);
   10712   if (rc != TPM_RC_SUCCESS) {
   10713     return rc;
   10714   }
   10715   if (authorization_delegate) {
   10716     // Encrypt just the parameter data, not the size.
   10717     std::string tmp = nonce_caller_bytes.substr(2);
   10718     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   10719       return TRUNKS_RC_ENCRYPTION_FAILED;
   10720     }
   10721     nonce_caller_bytes.replace(2, std::string::npos, tmp);
   10722   }
   10723   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10724       crypto::SecureHash::SHA256));
   10725   hash->Update(command_code_bytes.data(),
   10726                command_code_bytes.size());
   10727   hash->Update(tpm_key_name.data(),
   10728                tpm_key_name.size());
   10729   handle_section_bytes += tpm_key_bytes;
   10730   command_size += tpm_key_bytes.size();
   10731   hash->Update(bind_name.data(),
   10732                bind_name.size());
   10733   handle_section_bytes += bind_bytes;
   10734   command_size += bind_bytes.size();
   10735   hash->Update(nonce_caller_bytes.data(),
   10736                nonce_caller_bytes.size());
   10737   parameter_section_bytes += nonce_caller_bytes;
   10738   command_size += nonce_caller_bytes.size();
   10739   hash->Update(encrypted_salt_bytes.data(),
   10740                encrypted_salt_bytes.size());
   10741   parameter_section_bytes += encrypted_salt_bytes;
   10742   command_size += encrypted_salt_bytes.size();
   10743   hash->Update(session_type_bytes.data(),
   10744                session_type_bytes.size());
   10745   parameter_section_bytes += session_type_bytes;
   10746   command_size += session_type_bytes.size();
   10747   hash->Update(symmetric_bytes.data(),
   10748                symmetric_bytes.size());
   10749   parameter_section_bytes += symmetric_bytes;
   10750   command_size += symmetric_bytes.size();
   10751   hash->Update(auth_hash_bytes.data(),
   10752                auth_hash_bytes.size());
   10753   parameter_section_bytes += auth_hash_bytes;
   10754   command_size += auth_hash_bytes.size();
   10755   std::string command_hash(32, 0);
   10756   hash->Finish(string_as_array(&command_hash), command_hash.size());
   10757   std::string authorization_section_bytes;
   10758   std::string authorization_size_bytes;
   10759   if (authorization_delegate) {
   10760     if (!authorization_delegate->GetCommandAuthorization(
   10761         command_hash,
   10762         is_command_parameter_encryption_possible,
   10763         is_response_parameter_encryption_possible,
   10764         &authorization_section_bytes)) {
   10765       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10766     }
   10767     if (!authorization_section_bytes.empty()) {
   10768       tag = TPM_ST_SESSIONS;
   10769       std::string tmp;
   10770       rc = Serialize_UINT32(authorization_section_bytes.size(),
   10771                             &authorization_size_bytes);
   10772       if (rc != TPM_RC_SUCCESS) {
   10773         return rc;
   10774       }
   10775       command_size += authorization_size_bytes.size() +
   10776                       authorization_section_bytes.size();
   10777     }
   10778   }
   10779   std::string tag_bytes;
   10780   rc = Serialize_TPMI_ST_COMMAND_TAG(
   10781       tag,
   10782       &tag_bytes);
   10783   if (rc != TPM_RC_SUCCESS) {
   10784     return rc;
   10785   }
   10786   std::string command_size_bytes;
   10787   rc = Serialize_UINT32(
   10788       command_size,
   10789       &command_size_bytes);
   10790   if (rc != TPM_RC_SUCCESS) {
   10791     return rc;
   10792   }
   10793   *serialized_command = tag_bytes +
   10794                         command_size_bytes +
   10795                         command_code_bytes +
   10796                         handle_section_bytes +
   10797                         authorization_size_bytes +
   10798                         authorization_section_bytes +
   10799                         parameter_section_bytes;
   10800   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   10801   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   10802                                             serialized_command->size());
   10803   return TPM_RC_SUCCESS;
   10804 }
   10805 
   10806 TPM_RC Tpm::ParseResponse_StartAuthSession(
   10807       const std::string& response,
   10808       TPMI_SH_AUTH_SESSION* session_handle,
   10809       TPM2B_NONCE* nonce_tpm,
   10810       AuthorizationDelegate* authorization_delegate) {
   10811   VLOG(3) << __func__;
   10812   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   10813   TPM_RC rc = TPM_RC_SUCCESS;
   10814   std::string buffer(response);
   10815   TPM_ST tag;
   10816   std::string tag_bytes;
   10817   rc = Parse_TPM_ST(
   10818       &buffer,
   10819       &tag,
   10820       &tag_bytes);
   10821   if (rc != TPM_RC_SUCCESS) {
   10822     return rc;
   10823   }
   10824   UINT32 response_size;
   10825   std::string response_size_bytes;
   10826   rc = Parse_UINT32(
   10827       &buffer,
   10828       &response_size,
   10829       &response_size_bytes);
   10830   if (rc != TPM_RC_SUCCESS) {
   10831     return rc;
   10832   }
   10833   TPM_RC response_code;
   10834   std::string response_code_bytes;
   10835   rc = Parse_TPM_RC(
   10836       &buffer,
   10837       &response_code,
   10838       &response_code_bytes);
   10839   if (rc != TPM_RC_SUCCESS) {
   10840     return rc;
   10841   }
   10842   if (response_size != response.size()) {
   10843     return TPM_RC_SIZE;
   10844   }
   10845   if (response_code != TPM_RC_SUCCESS) {
   10846     return response_code;
   10847   }
   10848   std::string session_handle_bytes;
   10849   rc = Parse_TPMI_SH_AUTH_SESSION(
   10850       &buffer,
   10851       session_handle,
   10852       &session_handle_bytes);
   10853   if (rc != TPM_RC_SUCCESS) {
   10854     return rc;
   10855   }
   10856   TPM_CC command_code = TPM_CC_StartAuthSession;
   10857   std::string command_code_bytes;
   10858   rc = Serialize_TPM_CC(
   10859       command_code,
   10860       &command_code_bytes);
   10861   if (rc != TPM_RC_SUCCESS) {
   10862     return rc;
   10863   }
   10864   std::string authorization_section_bytes;
   10865   if (tag == TPM_ST_SESSIONS) {
   10866     UINT32 parameter_section_size = buffer.size();
   10867     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   10868     if (rc != TPM_RC_SUCCESS) {
   10869       return rc;
   10870     }
   10871     if (parameter_section_size > buffer.size()) {
   10872       return TPM_RC_INSUFFICIENT;
   10873     }
   10874     authorization_section_bytes = buffer.substr(parameter_section_size);
   10875     // Keep the parameter section in |buffer|.
   10876     buffer.erase(parameter_section_size);
   10877   }
   10878   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   10879       crypto::SecureHash::SHA256));
   10880   hash->Update(response_code_bytes.data(),
   10881                response_code_bytes.size());
   10882   hash->Update(command_code_bytes.data(),
   10883                command_code_bytes.size());
   10884   hash->Update(buffer.data(),
   10885                buffer.size());
   10886   std::string response_hash(32, 0);
   10887   hash->Finish(string_as_array(&response_hash), response_hash.size());
   10888   if (tag == TPM_ST_SESSIONS) {
   10889     CHECK(authorization_delegate) << "Authorization delegate missing!";
   10890     if (!authorization_delegate->CheckResponseAuthorization(
   10891         response_hash,
   10892         authorization_section_bytes)) {
   10893       return TRUNKS_RC_AUTHORIZATION_FAILED;
   10894     }
   10895   }
   10896   std::string nonce_tpm_bytes;
   10897   rc = Parse_TPM2B_NONCE(
   10898       &buffer,
   10899       nonce_tpm,
   10900       &nonce_tpm_bytes);
   10901   if (rc != TPM_RC_SUCCESS) {
   10902     return rc;
   10903   }
   10904   if (tag == TPM_ST_SESSIONS) {
   10905     CHECK(authorization_delegate) << "Authorization delegate missing!";
   10906     // Decrypt just the parameter data, not the size.
   10907     std::string tmp = nonce_tpm_bytes.substr(2);
   10908     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   10909       return TRUNKS_RC_ENCRYPTION_FAILED;
   10910     }
   10911     nonce_tpm_bytes.replace(2, std::string::npos, tmp);
   10912     rc = Parse_TPM2B_NONCE(
   10913         &nonce_tpm_bytes,
   10914         nonce_tpm,
   10915         nullptr);
   10916     if (rc != TPM_RC_SUCCESS) {
   10917       return rc;
   10918     }
   10919   }
   10920   return TPM_RC_SUCCESS;
   10921 }
   10922 
   10923 void StartAuthSessionErrorCallback(
   10924     const Tpm::StartAuthSessionResponse& callback,
   10925     TPM_RC response_code) {
   10926   VLOG(1) << __func__;
   10927   callback.Run(response_code,
   10928                TPMI_SH_AUTH_SESSION(),
   10929                TPM2B_NONCE());
   10930 }
   10931 
   10932 void StartAuthSessionResponseParser(
   10933     const Tpm::StartAuthSessionResponse& callback,
   10934     AuthorizationDelegate* authorization_delegate,
   10935     const std::string& response) {
   10936   VLOG(1) << __func__;
   10937   base::Callback<void(TPM_RC)> error_reporter =
   10938       base::Bind(StartAuthSessionErrorCallback, callback);
   10939   TPMI_SH_AUTH_SESSION session_handle;
   10940   TPM2B_NONCE nonce_tpm;
   10941   TPM_RC rc = Tpm::ParseResponse_StartAuthSession(
   10942       response,
   10943       &session_handle,
   10944       &nonce_tpm,
   10945       authorization_delegate);
   10946   if (rc != TPM_RC_SUCCESS) {
   10947     error_reporter.Run(rc);
   10948     return;
   10949   }
   10950   callback.Run(
   10951       rc,
   10952       session_handle,
   10953       nonce_tpm);
   10954 }
   10955 
   10956 void Tpm::StartAuthSession(
   10957       const TPMI_DH_OBJECT& tpm_key,
   10958       const std::string& tpm_key_name,
   10959       const TPMI_DH_ENTITY& bind,
   10960       const std::string& bind_name,
   10961       const TPM2B_NONCE& nonce_caller,
   10962       const TPM2B_ENCRYPTED_SECRET& encrypted_salt,
   10963       const TPM_SE& session_type,
   10964       const TPMT_SYM_DEF& symmetric,
   10965       const TPMI_ALG_HASH& auth_hash,
   10966       AuthorizationDelegate* authorization_delegate,
   10967       const StartAuthSessionResponse& callback) {
   10968   VLOG(1) << __func__;
   10969   base::Callback<void(TPM_RC)> error_reporter =
   10970       base::Bind(StartAuthSessionErrorCallback, callback);
   10971   base::Callback<void(const std::string&)> parser =
   10972       base::Bind(StartAuthSessionResponseParser,
   10973                  callback,
   10974                  authorization_delegate);
   10975   std::string command;
   10976   TPM_RC rc = SerializeCommand_StartAuthSession(
   10977       tpm_key,
   10978       tpm_key_name,
   10979       bind,
   10980       bind_name,
   10981       nonce_caller,
   10982       encrypted_salt,
   10983       session_type,
   10984       symmetric,
   10985       auth_hash,
   10986       &command,
   10987       authorization_delegate);
   10988   if (rc != TPM_RC_SUCCESS) {
   10989     error_reporter.Run(rc);
   10990     return;
   10991   }
   10992   transceiver_->SendCommand(command, parser);
   10993 }
   10994 
   10995 TPM_RC Tpm::StartAuthSessionSync(
   10996       const TPMI_DH_OBJECT& tpm_key,
   10997       const std::string& tpm_key_name,
   10998       const TPMI_DH_ENTITY& bind,
   10999       const std::string& bind_name,
   11000       const TPM2B_NONCE& nonce_caller,
   11001       const TPM2B_ENCRYPTED_SECRET& encrypted_salt,
   11002       const TPM_SE& session_type,
   11003       const TPMT_SYM_DEF& symmetric,
   11004       const TPMI_ALG_HASH& auth_hash,
   11005       TPMI_SH_AUTH_SESSION* session_handle,
   11006       TPM2B_NONCE* nonce_tpm,
   11007       AuthorizationDelegate* authorization_delegate) {
   11008   VLOG(1) << __func__;
   11009   std::string command;
   11010   TPM_RC rc = SerializeCommand_StartAuthSession(
   11011       tpm_key,
   11012       tpm_key_name,
   11013       bind,
   11014       bind_name,
   11015       nonce_caller,
   11016       encrypted_salt,
   11017       session_type,
   11018       symmetric,
   11019       auth_hash,
   11020       &command,
   11021       authorization_delegate);
   11022   if (rc != TPM_RC_SUCCESS) {
   11023     return rc;
   11024   }
   11025   std::string response = transceiver_->SendCommandAndWait(command);
   11026   rc = ParseResponse_StartAuthSession(
   11027       response,
   11028       session_handle,
   11029       nonce_tpm,
   11030       authorization_delegate);
   11031   return rc;
   11032 }
   11033 
   11034 TPM_RC Tpm::SerializeCommand_PolicyRestart(
   11035       const TPMI_SH_POLICY& session_handle,
   11036       const std::string& session_handle_name,
   11037       std::string* serialized_command,
   11038       AuthorizationDelegate* authorization_delegate) {
   11039   VLOG(3) << __func__;
   11040   TPM_RC rc = TPM_RC_SUCCESS;
   11041   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   11042   UINT32 command_size = 10;  // Header size.
   11043   std::string handle_section_bytes;
   11044   std::string parameter_section_bytes;
   11045   TPM_CC command_code = TPM_CC_PolicyRestart;
   11046   bool is_command_parameter_encryption_possible = false;
   11047   bool is_response_parameter_encryption_possible = false;
   11048   std::string command_code_bytes;
   11049   rc = Serialize_TPM_CC(
   11050       command_code,
   11051       &command_code_bytes);
   11052   if (rc != TPM_RC_SUCCESS) {
   11053     return rc;
   11054   }
   11055   std::string session_handle_bytes;
   11056   rc = Serialize_TPMI_SH_POLICY(
   11057       session_handle,
   11058       &session_handle_bytes);
   11059   if (rc != TPM_RC_SUCCESS) {
   11060     return rc;
   11061   }
   11062   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   11063       crypto::SecureHash::SHA256));
   11064   hash->Update(command_code_bytes.data(),
   11065                command_code_bytes.size());
   11066   hash->Update(session_handle_name.data(),
   11067                session_handle_name.size());
   11068   handle_section_bytes += session_handle_bytes;
   11069   command_size += session_handle_bytes.size();
   11070   std::string command_hash(32, 0);
   11071   hash->Finish(string_as_array(&command_hash), command_hash.size());
   11072   std::string authorization_section_bytes;
   11073   std::string authorization_size_bytes;
   11074   if (authorization_delegate) {
   11075     if (!authorization_delegate->GetCommandAuthorization(
   11076         command_hash,
   11077         is_command_parameter_encryption_possible,
   11078         is_response_parameter_encryption_possible,
   11079         &authorization_section_bytes)) {
   11080       return TRUNKS_RC_AUTHORIZATION_FAILED;
   11081     }
   11082     if (!authorization_section_bytes.empty()) {
   11083       tag = TPM_ST_SESSIONS;
   11084       std::string tmp;
   11085       rc = Serialize_UINT32(authorization_section_bytes.size(),
   11086                             &authorization_size_bytes);
   11087       if (rc != TPM_RC_SUCCESS) {
   11088         return rc;
   11089       }
   11090       command_size += authorization_size_bytes.size() +
   11091                       authorization_section_bytes.size();
   11092     }
   11093   }
   11094   std::string tag_bytes;
   11095   rc = Serialize_TPMI_ST_COMMAND_TAG(
   11096       tag,
   11097       &tag_bytes);
   11098   if (rc != TPM_RC_SUCCESS) {
   11099     return rc;
   11100   }
   11101   std::string command_size_bytes;
   11102   rc = Serialize_UINT32(
   11103       command_size,
   11104       &command_size_bytes);
   11105   if (rc != TPM_RC_SUCCESS) {
   11106     return rc;
   11107   }
   11108   *serialized_command = tag_bytes +
   11109                         command_size_bytes +
   11110                         command_code_bytes +
   11111                         handle_section_bytes +
   11112                         authorization_size_bytes +
   11113                         authorization_section_bytes +
   11114                         parameter_section_bytes;
   11115   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   11116   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   11117                                             serialized_command->size());
   11118   return TPM_RC_SUCCESS;
   11119 }
   11120 
   11121 TPM_RC Tpm::ParseResponse_PolicyRestart(
   11122       const std::string& response,
   11123       AuthorizationDelegate* authorization_delegate) {
   11124   VLOG(3) << __func__;
   11125   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   11126   TPM_RC rc = TPM_RC_SUCCESS;
   11127   std::string buffer(response);
   11128   TPM_ST tag;
   11129   std::string tag_bytes;
   11130   rc = Parse_TPM_ST(
   11131       &buffer,
   11132       &tag,
   11133       &tag_bytes);
   11134   if (rc != TPM_RC_SUCCESS) {
   11135     return rc;
   11136   }
   11137   UINT32 response_size;
   11138   std::string response_size_bytes;
   11139   rc = Parse_UINT32(
   11140       &buffer,
   11141       &response_size,
   11142       &response_size_bytes);
   11143   if (rc != TPM_RC_SUCCESS) {
   11144     return rc;
   11145   }
   11146   TPM_RC response_code;
   11147   std::string response_code_bytes;
   11148   rc = Parse_TPM_RC(
   11149       &buffer,
   11150       &response_code,
   11151       &response_code_bytes);
   11152   if (rc != TPM_RC_SUCCESS) {
   11153     return rc;
   11154   }
   11155   if (response_size != response.size()) {
   11156     return TPM_RC_SIZE;
   11157   }
   11158   if (response_code != TPM_RC_SUCCESS) {
   11159     return response_code;
   11160   }
   11161   TPM_CC command_code = TPM_CC_PolicyRestart;
   11162   std::string command_code_bytes;
   11163   rc = Serialize_TPM_CC(
   11164       command_code,
   11165       &command_code_bytes);
   11166   if (rc != TPM_RC_SUCCESS) {
   11167     return rc;
   11168   }
   11169   std::string authorization_section_bytes;
   11170   if (tag == TPM_ST_SESSIONS) {
   11171     UINT32 parameter_section_size = buffer.size();
   11172     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   11173     if (rc != TPM_RC_SUCCESS) {
   11174       return rc;
   11175     }
   11176     if (parameter_section_size > buffer.size()) {
   11177       return TPM_RC_INSUFFICIENT;
   11178     }
   11179     authorization_section_bytes = buffer.substr(parameter_section_size);
   11180     // Keep the parameter section in |buffer|.
   11181     buffer.erase(parameter_section_size);
   11182   }
   11183   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   11184       crypto::SecureHash::SHA256));
   11185   hash->Update(response_code_bytes.data(),
   11186                response_code_bytes.size());
   11187   hash->Update(command_code_bytes.data(),
   11188                command_code_bytes.size());
   11189   hash->Update(buffer.data(),
   11190                buffer.size());
   11191   std::string response_hash(32, 0);
   11192   hash->Finish(string_as_array(&response_hash), response_hash.size());
   11193   if (tag == TPM_ST_SESSIONS) {
   11194     CHECK(authorization_delegate) << "Authorization delegate missing!";
   11195     if (!authorization_delegate->CheckResponseAuthorization(
   11196         response_hash,
   11197         authorization_section_bytes)) {
   11198       return TRUNKS_RC_AUTHORIZATION_FAILED;
   11199     }
   11200   }
   11201   return TPM_RC_SUCCESS;
   11202 }
   11203 
   11204 void PolicyRestartErrorCallback(
   11205     const Tpm::PolicyRestartResponse& callback,
   11206     TPM_RC response_code) {
   11207   VLOG(1) << __func__;
   11208   callback.Run(response_code);
   11209 }
   11210 
   11211 void PolicyRestartResponseParser(
   11212     const Tpm::PolicyRestartResponse& callback,
   11213     AuthorizationDelegate* authorization_delegate,
   11214     const std::string& response) {
   11215   VLOG(1) << __func__;
   11216   base::Callback<void(TPM_RC)> error_reporter =
   11217       base::Bind(PolicyRestartErrorCallback, callback);
   11218   TPM_RC rc = Tpm::ParseResponse_PolicyRestart(
   11219       response,
   11220       authorization_delegate);
   11221   if (rc != TPM_RC_SUCCESS) {
   11222     error_reporter.Run(rc);
   11223     return;
   11224   }
   11225   callback.Run(
   11226       rc);
   11227 }
   11228 
   11229 void Tpm::PolicyRestart(
   11230       const TPMI_SH_POLICY& session_handle,
   11231       const std::string& session_handle_name,
   11232       AuthorizationDelegate* authorization_delegate,
   11233       const PolicyRestartResponse& callback) {
   11234   VLOG(1) << __func__;
   11235   base::Callback<void(TPM_RC)> error_reporter =
   11236       base::Bind(PolicyRestartErrorCallback, callback);
   11237   base::Callback<void(const std::string&)> parser =
   11238       base::Bind(PolicyRestartResponseParser,
   11239                  callback,
   11240                  authorization_delegate);
   11241   std::string command;
   11242   TPM_RC rc = SerializeCommand_PolicyRestart(
   11243       session_handle,
   11244       session_handle_name,
   11245       &command,
   11246       authorization_delegate);
   11247   if (rc != TPM_RC_SUCCESS) {
   11248     error_reporter.Run(rc);
   11249     return;
   11250   }
   11251   transceiver_->SendCommand(command, parser);
   11252 }
   11253 
   11254 TPM_RC Tpm::PolicyRestartSync(
   11255       const TPMI_SH_POLICY& session_handle,
   11256       const std::string& session_handle_name,
   11257       AuthorizationDelegate* authorization_delegate) {
   11258   VLOG(1) << __func__;
   11259   std::string command;
   11260   TPM_RC rc = SerializeCommand_PolicyRestart(
   11261       session_handle,
   11262       session_handle_name,
   11263       &command,
   11264       authorization_delegate);
   11265   if (rc != TPM_RC_SUCCESS) {
   11266     return rc;
   11267   }
   11268   std::string response = transceiver_->SendCommandAndWait(command);
   11269   rc = ParseResponse_PolicyRestart(
   11270       response,
   11271       authorization_delegate);
   11272   return rc;
   11273 }
   11274 
   11275 TPM_RC Tpm::SerializeCommand_Create(
   11276       const TPMI_DH_OBJECT& parent_handle,
   11277       const std::string& parent_handle_name,
   11278       const TPM2B_SENSITIVE_CREATE& in_sensitive,
   11279       const TPM2B_PUBLIC& in_public,
   11280       const TPM2B_DATA& outside_info,
   11281       const TPML_PCR_SELECTION& creation_pcr,
   11282       std::string* serialized_command,
   11283       AuthorizationDelegate* authorization_delegate) {
   11284   VLOG(3) << __func__;
   11285   TPM_RC rc = TPM_RC_SUCCESS;
   11286   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   11287   UINT32 command_size = 10;  // Header size.
   11288   std::string handle_section_bytes;
   11289   std::string parameter_section_bytes;
   11290   TPM_CC command_code = TPM_CC_Create;
   11291   bool is_command_parameter_encryption_possible = true;
   11292   bool is_response_parameter_encryption_possible = true;
   11293   std::string command_code_bytes;
   11294   rc = Serialize_TPM_CC(
   11295       command_code,
   11296       &command_code_bytes);
   11297   if (rc != TPM_RC_SUCCESS) {
   11298     return rc;
   11299   }
   11300   std::string parent_handle_bytes;
   11301   rc = Serialize_TPMI_DH_OBJECT(
   11302       parent_handle,
   11303       &parent_handle_bytes);
   11304   if (rc != TPM_RC_SUCCESS) {
   11305     return rc;
   11306   }
   11307   std::string in_sensitive_bytes;
   11308   rc = Serialize_TPM2B_SENSITIVE_CREATE(
   11309       in_sensitive,
   11310       &in_sensitive_bytes);
   11311   if (rc != TPM_RC_SUCCESS) {
   11312     return rc;
   11313   }
   11314   std::string in_public_bytes;
   11315   rc = Serialize_TPM2B_PUBLIC(
   11316       in_public,
   11317       &in_public_bytes);
   11318   if (rc != TPM_RC_SUCCESS) {
   11319     return rc;
   11320   }
   11321   std::string outside_info_bytes;
   11322   rc = Serialize_TPM2B_DATA(
   11323       outside_info,
   11324       &outside_info_bytes);
   11325   if (rc != TPM_RC_SUCCESS) {
   11326     return rc;
   11327   }
   11328   std::string creation_pcr_bytes;
   11329   rc = Serialize_TPML_PCR_SELECTION(
   11330       creation_pcr,
   11331       &creation_pcr_bytes);
   11332   if (rc != TPM_RC_SUCCESS) {
   11333     return rc;
   11334   }
   11335   if (authorization_delegate) {
   11336     // Encrypt just the parameter data, not the size.
   11337     std::string tmp = in_sensitive_bytes.substr(2);
   11338     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   11339       return TRUNKS_RC_ENCRYPTION_FAILED;
   11340     }
   11341     in_sensitive_bytes.replace(2, std::string::npos, tmp);
   11342   }
   11343   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   11344       crypto::SecureHash::SHA256));
   11345   hash->Update(command_code_bytes.data(),
   11346                command_code_bytes.size());
   11347   hash->Update(parent_handle_name.data(),
   11348                parent_handle_name.size());
   11349   handle_section_bytes += parent_handle_bytes;
   11350   command_size += parent_handle_bytes.size();
   11351   hash->Update(in_sensitive_bytes.data(),
   11352                in_sensitive_bytes.size());
   11353   parameter_section_bytes += in_sensitive_bytes;
   11354   command_size += in_sensitive_bytes.size();
   11355   hash->Update(in_public_bytes.data(),
   11356                in_public_bytes.size());
   11357   parameter_section_bytes += in_public_bytes;
   11358   command_size += in_public_bytes.size();
   11359   hash->Update(outside_info_bytes.data(),
   11360                outside_info_bytes.size());
   11361   parameter_section_bytes += outside_info_bytes;
   11362   command_size += outside_info_bytes.size();
   11363   hash->Update(creation_pcr_bytes.data(),
   11364                creation_pcr_bytes.size());
   11365   parameter_section_bytes += creation_pcr_bytes;
   11366   command_size += creation_pcr_bytes.size();
   11367   std::string command_hash(32, 0);
   11368   hash->Finish(string_as_array(&command_hash), command_hash.size());
   11369   std::string authorization_section_bytes;
   11370   std::string authorization_size_bytes;
   11371   if (authorization_delegate) {
   11372     if (!authorization_delegate->GetCommandAuthorization(
   11373         command_hash,
   11374         is_command_parameter_encryption_possible,
   11375         is_response_parameter_encryption_possible,
   11376         &authorization_section_bytes)) {
   11377       return TRUNKS_RC_AUTHORIZATION_FAILED;
   11378     }
   11379     if (!authorization_section_bytes.empty()) {
   11380       tag = TPM_ST_SESSIONS;
   11381       std::string tmp;
   11382       rc = Serialize_UINT32(authorization_section_bytes.size(),
   11383                             &authorization_size_bytes);
   11384       if (rc != TPM_RC_SUCCESS) {
   11385         return rc;
   11386       }
   11387       command_size += authorization_size_bytes.size() +
   11388                       authorization_section_bytes.size();
   11389     }
   11390   }
   11391   std::string tag_bytes;
   11392   rc = Serialize_TPMI_ST_COMMAND_TAG(
   11393       tag,
   11394       &tag_bytes);
   11395   if (rc != TPM_RC_SUCCESS) {
   11396     return rc;
   11397   }
   11398   std::string command_size_bytes;
   11399   rc = Serialize_UINT32(
   11400       command_size,
   11401       &command_size_bytes);
   11402   if (rc != TPM_RC_SUCCESS) {
   11403     return rc;
   11404   }
   11405   *serialized_command = tag_bytes +
   11406                         command_size_bytes +
   11407                         command_code_bytes +
   11408                         handle_section_bytes +
   11409                         authorization_size_bytes +
   11410                         authorization_section_bytes +
   11411                         parameter_section_bytes;
   11412   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   11413   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   11414                                             serialized_command->size());
   11415   return TPM_RC_SUCCESS;
   11416 }
   11417 
   11418 TPM_RC Tpm::ParseResponse_Create(
   11419       const std::string& response,
   11420       TPM2B_PRIVATE* out_private,
   11421       TPM2B_PUBLIC* out_public,
   11422       TPM2B_CREATION_DATA* creation_data,
   11423       TPM2B_DIGEST* creation_hash,
   11424       TPMT_TK_CREATION* creation_ticket,
   11425       AuthorizationDelegate* authorization_delegate) {
   11426   VLOG(3) << __func__;
   11427   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   11428   TPM_RC rc = TPM_RC_SUCCESS;
   11429   std::string buffer(response);
   11430   TPM_ST tag;
   11431   std::string tag_bytes;
   11432   rc = Parse_TPM_ST(
   11433       &buffer,
   11434       &tag,
   11435       &tag_bytes);
   11436   if (rc != TPM_RC_SUCCESS) {
   11437     return rc;
   11438   }
   11439   UINT32 response_size;
   11440   std::string response_size_bytes;
   11441   rc = Parse_UINT32(
   11442       &buffer,
   11443       &response_size,
   11444       &response_size_bytes);
   11445   if (rc != TPM_RC_SUCCESS) {
   11446     return rc;
   11447   }
   11448   TPM_RC response_code;
   11449   std::string response_code_bytes;
   11450   rc = Parse_TPM_RC(
   11451       &buffer,
   11452       &response_code,
   11453       &response_code_bytes);
   11454   if (rc != TPM_RC_SUCCESS) {
   11455     return rc;
   11456   }
   11457   if (response_size != response.size()) {
   11458     return TPM_RC_SIZE;
   11459   }
   11460   if (response_code != TPM_RC_SUCCESS) {
   11461     return response_code;
   11462   }
   11463   TPM_CC command_code = TPM_CC_Create;
   11464   std::string command_code_bytes;
   11465   rc = Serialize_TPM_CC(
   11466       command_code,
   11467       &command_code_bytes);
   11468   if (rc != TPM_RC_SUCCESS) {
   11469     return rc;
   11470   }
   11471   std::string authorization_section_bytes;
   11472   if (tag == TPM_ST_SESSIONS) {
   11473     UINT32 parameter_section_size = buffer.size();
   11474     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   11475     if (rc != TPM_RC_SUCCESS) {
   11476       return rc;
   11477     }
   11478     if (parameter_section_size > buffer.size()) {
   11479       return TPM_RC_INSUFFICIENT;
   11480     }
   11481     authorization_section_bytes = buffer.substr(parameter_section_size);
   11482     // Keep the parameter section in |buffer|.
   11483     buffer.erase(parameter_section_size);
   11484   }
   11485   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   11486       crypto::SecureHash::SHA256));
   11487   hash->Update(response_code_bytes.data(),
   11488                response_code_bytes.size());
   11489   hash->Update(command_code_bytes.data(),
   11490                command_code_bytes.size());
   11491   hash->Update(buffer.data(),
   11492                buffer.size());
   11493   std::string response_hash(32, 0);
   11494   hash->Finish(string_as_array(&response_hash), response_hash.size());
   11495   if (tag == TPM_ST_SESSIONS) {
   11496     CHECK(authorization_delegate) << "Authorization delegate missing!";
   11497     if (!authorization_delegate->CheckResponseAuthorization(
   11498         response_hash,
   11499         authorization_section_bytes)) {
   11500       return TRUNKS_RC_AUTHORIZATION_FAILED;
   11501     }
   11502   }
   11503   std::string out_private_bytes;
   11504   rc = Parse_TPM2B_PRIVATE(
   11505       &buffer,
   11506       out_private,
   11507       &out_private_bytes);
   11508   if (rc != TPM_RC_SUCCESS) {
   11509     return rc;
   11510   }
   11511   std::string out_public_bytes;
   11512   rc = Parse_TPM2B_PUBLIC(
   11513       &buffer,
   11514       out_public,
   11515       &out_public_bytes);
   11516   if (rc != TPM_RC_SUCCESS) {
   11517     return rc;
   11518   }
   11519   std::string creation_data_bytes;
   11520   rc = Parse_TPM2B_CREATION_DATA(
   11521       &buffer,
   11522       creation_data,
   11523       &creation_data_bytes);
   11524   if (rc != TPM_RC_SUCCESS) {
   11525     return rc;
   11526   }
   11527   std::string creation_hash_bytes;
   11528   rc = Parse_TPM2B_DIGEST(
   11529       &buffer,
   11530       creation_hash,
   11531       &creation_hash_bytes);
   11532   if (rc != TPM_RC_SUCCESS) {
   11533     return rc;
   11534   }
   11535   std::string creation_ticket_bytes;
   11536   rc = Parse_TPMT_TK_CREATION(
   11537       &buffer,
   11538       creation_ticket,
   11539       &creation_ticket_bytes);
   11540   if (rc != TPM_RC_SUCCESS) {
   11541     return rc;
   11542   }
   11543   if (tag == TPM_ST_SESSIONS) {
   11544     CHECK(authorization_delegate) << "Authorization delegate missing!";
   11545     // Decrypt just the parameter data, not the size.
   11546     std::string tmp = out_private_bytes.substr(2);
   11547     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   11548       return TRUNKS_RC_ENCRYPTION_FAILED;
   11549     }
   11550     out_private_bytes.replace(2, std::string::npos, tmp);
   11551     rc = Parse_TPM2B_PRIVATE(
   11552         &out_private_bytes,
   11553         out_private,
   11554         nullptr);
   11555     if (rc != TPM_RC_SUCCESS) {
   11556       return rc;
   11557     }
   11558   }
   11559   return TPM_RC_SUCCESS;
   11560 }
   11561 
   11562 void CreateErrorCallback(
   11563     const Tpm::CreateResponse& callback,
   11564     TPM_RC response_code) {
   11565   VLOG(1) << __func__;
   11566   callback.Run(response_code,
   11567                TPM2B_PRIVATE(),
   11568                TPM2B_PUBLIC(),
   11569                TPM2B_CREATION_DATA(),
   11570                TPM2B_DIGEST(),
   11571                TPMT_TK_CREATION());
   11572 }
   11573 
   11574 void CreateResponseParser(
   11575     const Tpm::CreateResponse& callback,
   11576     AuthorizationDelegate* authorization_delegate,
   11577     const std::string& response) {
   11578   VLOG(1) << __func__;
   11579   base::Callback<void(TPM_RC)> error_reporter =
   11580       base::Bind(CreateErrorCallback, callback);
   11581   TPM2B_PRIVATE out_private;
   11582   TPM2B_PUBLIC out_public;
   11583   TPM2B_CREATION_DATA creation_data;
   11584   TPM2B_DIGEST creation_hash;
   11585   TPMT_TK_CREATION creation_ticket;
   11586   TPM_RC rc = Tpm::ParseResponse_Create(
   11587       response,
   11588       &out_private,
   11589       &out_public,
   11590       &creation_data,
   11591       &creation_hash,
   11592       &creation_ticket,
   11593       authorization_delegate);
   11594   if (rc != TPM_RC_SUCCESS) {
   11595     error_reporter.Run(rc);
   11596     return;
   11597   }
   11598   callback.Run(
   11599       rc,
   11600       out_private,
   11601       out_public,
   11602       creation_data,
   11603       creation_hash,
   11604       creation_ticket);
   11605 }
   11606 
   11607 void Tpm::Create(
   11608       const TPMI_DH_OBJECT& parent_handle,
   11609       const std::string& parent_handle_name,
   11610       const TPM2B_SENSITIVE_CREATE& in_sensitive,
   11611       const TPM2B_PUBLIC& in_public,
   11612       const TPM2B_DATA& outside_info,
   11613       const TPML_PCR_SELECTION& creation_pcr,
   11614       AuthorizationDelegate* authorization_delegate,
   11615       const CreateResponse& callback) {
   11616   VLOG(1) << __func__;
   11617   base::Callback<void(TPM_RC)> error_reporter =
   11618       base::Bind(CreateErrorCallback, callback);
   11619   base::Callback<void(const std::string&)> parser =
   11620       base::Bind(CreateResponseParser,
   11621                  callback,
   11622                  authorization_delegate);
   11623   std::string command;
   11624   TPM_RC rc = SerializeCommand_Create(
   11625       parent_handle,
   11626       parent_handle_name,
   11627       in_sensitive,
   11628       in_public,
   11629       outside_info,
   11630       creation_pcr,
   11631       &command,
   11632       authorization_delegate);
   11633   if (rc != TPM_RC_SUCCESS) {
   11634     error_reporter.Run(rc);
   11635     return;
   11636   }
   11637   transceiver_->SendCommand(command, parser);
   11638 }
   11639 
   11640 TPM_RC Tpm::CreateSync(
   11641       const TPMI_DH_OBJECT& parent_handle,
   11642       const std::string& parent_handle_name,
   11643       const TPM2B_SENSITIVE_CREATE& in_sensitive,
   11644       const TPM2B_PUBLIC& in_public,
   11645       const TPM2B_DATA& outside_info,
   11646       const TPML_PCR_SELECTION& creation_pcr,
   11647       TPM2B_PRIVATE* out_private,
   11648       TPM2B_PUBLIC* out_public,
   11649       TPM2B_CREATION_DATA* creation_data,
   11650       TPM2B_DIGEST* creation_hash,
   11651       TPMT_TK_CREATION* creation_ticket,
   11652       AuthorizationDelegate* authorization_delegate) {
   11653   VLOG(1) << __func__;
   11654   std::string command;
   11655   TPM_RC rc = SerializeCommand_Create(
   11656       parent_handle,
   11657       parent_handle_name,
   11658       in_sensitive,
   11659       in_public,
   11660       outside_info,
   11661       creation_pcr,
   11662       &command,
   11663       authorization_delegate);
   11664   if (rc != TPM_RC_SUCCESS) {
   11665     return rc;
   11666   }
   11667   std::string response = transceiver_->SendCommandAndWait(command);
   11668   rc = ParseResponse_Create(
   11669       response,
   11670       out_private,
   11671       out_public,
   11672       creation_data,
   11673       creation_hash,
   11674       creation_ticket,
   11675       authorization_delegate);
   11676   return rc;
   11677 }
   11678 
   11679 TPM_RC Tpm::SerializeCommand_Load(
   11680       const TPMI_DH_OBJECT& parent_handle,
   11681       const std::string& parent_handle_name,
   11682       const TPM2B_PRIVATE& in_private,
   11683       const TPM2B_PUBLIC& in_public,
   11684       std::string* serialized_command,
   11685       AuthorizationDelegate* authorization_delegate) {
   11686   VLOG(3) << __func__;
   11687   TPM_RC rc = TPM_RC_SUCCESS;
   11688   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   11689   UINT32 command_size = 10;  // Header size.
   11690   std::string handle_section_bytes;
   11691   std::string parameter_section_bytes;
   11692   TPM_CC command_code = TPM_CC_Load;
   11693   bool is_command_parameter_encryption_possible = true;
   11694   bool is_response_parameter_encryption_possible = true;
   11695   std::string command_code_bytes;
   11696   rc = Serialize_TPM_CC(
   11697       command_code,
   11698       &command_code_bytes);
   11699   if (rc != TPM_RC_SUCCESS) {
   11700     return rc;
   11701   }
   11702   std::string parent_handle_bytes;
   11703   rc = Serialize_TPMI_DH_OBJECT(
   11704       parent_handle,
   11705       &parent_handle_bytes);
   11706   if (rc != TPM_RC_SUCCESS) {
   11707     return rc;
   11708   }
   11709   std::string in_private_bytes;
   11710   rc = Serialize_TPM2B_PRIVATE(
   11711       in_private,
   11712       &in_private_bytes);
   11713   if (rc != TPM_RC_SUCCESS) {
   11714     return rc;
   11715   }
   11716   std::string in_public_bytes;
   11717   rc = Serialize_TPM2B_PUBLIC(
   11718       in_public,
   11719       &in_public_bytes);
   11720   if (rc != TPM_RC_SUCCESS) {
   11721     return rc;
   11722   }
   11723   if (authorization_delegate) {
   11724     // Encrypt just the parameter data, not the size.
   11725     std::string tmp = in_private_bytes.substr(2);
   11726     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   11727       return TRUNKS_RC_ENCRYPTION_FAILED;
   11728     }
   11729     in_private_bytes.replace(2, std::string::npos, tmp);
   11730   }
   11731   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   11732       crypto::SecureHash::SHA256));
   11733   hash->Update(command_code_bytes.data(),
   11734                command_code_bytes.size());
   11735   hash->Update(parent_handle_name.data(),
   11736                parent_handle_name.size());
   11737   handle_section_bytes += parent_handle_bytes;
   11738   command_size += parent_handle_bytes.size();
   11739   hash->Update(in_private_bytes.data(),
   11740                in_private_bytes.size());
   11741   parameter_section_bytes += in_private_bytes;
   11742   command_size += in_private_bytes.size();
   11743   hash->Update(in_public_bytes.data(),
   11744                in_public_bytes.size());
   11745   parameter_section_bytes += in_public_bytes;
   11746   command_size += in_public_bytes.size();
   11747   std::string command_hash(32, 0);
   11748   hash->Finish(string_as_array(&command_hash), command_hash.size());
   11749   std::string authorization_section_bytes;
   11750   std::string authorization_size_bytes;
   11751   if (authorization_delegate) {
   11752     if (!authorization_delegate->GetCommandAuthorization(
   11753         command_hash,
   11754         is_command_parameter_encryption_possible,
   11755         is_response_parameter_encryption_possible,
   11756         &authorization_section_bytes)) {
   11757       return TRUNKS_RC_AUTHORIZATION_FAILED;
   11758     }
   11759     if (!authorization_section_bytes.empty()) {
   11760       tag = TPM_ST_SESSIONS;
   11761       std::string tmp;
   11762       rc = Serialize_UINT32(authorization_section_bytes.size(),
   11763                             &authorization_size_bytes);
   11764       if (rc != TPM_RC_SUCCESS) {
   11765         return rc;
   11766       }
   11767       command_size += authorization_size_bytes.size() +
   11768                       authorization_section_bytes.size();
   11769     }
   11770   }
   11771   std::string tag_bytes;
   11772   rc = Serialize_TPMI_ST_COMMAND_TAG(
   11773       tag,
   11774       &tag_bytes);
   11775   if (rc != TPM_RC_SUCCESS) {
   11776     return rc;
   11777   }
   11778   std::string command_size_bytes;
   11779   rc = Serialize_UINT32(
   11780       command_size,
   11781       &command_size_bytes);
   11782   if (rc != TPM_RC_SUCCESS) {
   11783     return rc;
   11784   }
   11785   *serialized_command = tag_bytes +
   11786                         command_size_bytes +
   11787                         command_code_bytes +
   11788                         handle_section_bytes +
   11789                         authorization_size_bytes +
   11790                         authorization_section_bytes +
   11791                         parameter_section_bytes;
   11792   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   11793   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   11794                                             serialized_command->size());
   11795   return TPM_RC_SUCCESS;
   11796 }
   11797 
   11798 TPM_RC Tpm::ParseResponse_Load(
   11799       const std::string& response,
   11800       TPM_HANDLE* object_handle,
   11801       TPM2B_NAME* name,
   11802       AuthorizationDelegate* authorization_delegate) {
   11803   VLOG(3) << __func__;
   11804   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   11805   TPM_RC rc = TPM_RC_SUCCESS;
   11806   std::string buffer(response);
   11807   TPM_ST tag;
   11808   std::string tag_bytes;
   11809   rc = Parse_TPM_ST(
   11810       &buffer,
   11811       &tag,
   11812       &tag_bytes);
   11813   if (rc != TPM_RC_SUCCESS) {
   11814     return rc;
   11815   }
   11816   UINT32 response_size;
   11817   std::string response_size_bytes;
   11818   rc = Parse_UINT32(
   11819       &buffer,
   11820       &response_size,
   11821       &response_size_bytes);
   11822   if (rc != TPM_RC_SUCCESS) {
   11823     return rc;
   11824   }
   11825   TPM_RC response_code;
   11826   std::string response_code_bytes;
   11827   rc = Parse_TPM_RC(
   11828       &buffer,
   11829       &response_code,
   11830       &response_code_bytes);
   11831   if (rc != TPM_RC_SUCCESS) {
   11832     return rc;
   11833   }
   11834   if (response_size != response.size()) {
   11835     return TPM_RC_SIZE;
   11836   }
   11837   if (response_code != TPM_RC_SUCCESS) {
   11838     return response_code;
   11839   }
   11840   std::string object_handle_bytes;
   11841   rc = Parse_TPM_HANDLE(
   11842       &buffer,
   11843       object_handle,
   11844       &object_handle_bytes);
   11845   if (rc != TPM_RC_SUCCESS) {
   11846     return rc;
   11847   }
   11848   TPM_CC command_code = TPM_CC_Load;
   11849   std::string command_code_bytes;
   11850   rc = Serialize_TPM_CC(
   11851       command_code,
   11852       &command_code_bytes);
   11853   if (rc != TPM_RC_SUCCESS) {
   11854     return rc;
   11855   }
   11856   std::string authorization_section_bytes;
   11857   if (tag == TPM_ST_SESSIONS) {
   11858     UINT32 parameter_section_size = buffer.size();
   11859     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   11860     if (rc != TPM_RC_SUCCESS) {
   11861       return rc;
   11862     }
   11863     if (parameter_section_size > buffer.size()) {
   11864       return TPM_RC_INSUFFICIENT;
   11865     }
   11866     authorization_section_bytes = buffer.substr(parameter_section_size);
   11867     // Keep the parameter section in |buffer|.
   11868     buffer.erase(parameter_section_size);
   11869   }
   11870   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   11871       crypto::SecureHash::SHA256));
   11872   hash->Update(response_code_bytes.data(),
   11873                response_code_bytes.size());
   11874   hash->Update(command_code_bytes.data(),
   11875                command_code_bytes.size());
   11876   hash->Update(buffer.data(),
   11877                buffer.size());
   11878   std::string response_hash(32, 0);
   11879   hash->Finish(string_as_array(&response_hash), response_hash.size());
   11880   if (tag == TPM_ST_SESSIONS) {
   11881     CHECK(authorization_delegate) << "Authorization delegate missing!";
   11882     if (!authorization_delegate->CheckResponseAuthorization(
   11883         response_hash,
   11884         authorization_section_bytes)) {
   11885       return TRUNKS_RC_AUTHORIZATION_FAILED;
   11886     }
   11887   }
   11888   std::string name_bytes;
   11889   rc = Parse_TPM2B_NAME(
   11890       &buffer,
   11891       name,
   11892       &name_bytes);
   11893   if (rc != TPM_RC_SUCCESS) {
   11894     return rc;
   11895   }
   11896   if (tag == TPM_ST_SESSIONS) {
   11897     CHECK(authorization_delegate) << "Authorization delegate missing!";
   11898     // Decrypt just the parameter data, not the size.
   11899     std::string tmp = name_bytes.substr(2);
   11900     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   11901       return TRUNKS_RC_ENCRYPTION_FAILED;
   11902     }
   11903     name_bytes.replace(2, std::string::npos, tmp);
   11904     rc = Parse_TPM2B_NAME(
   11905         &name_bytes,
   11906         name,
   11907         nullptr);
   11908     if (rc != TPM_RC_SUCCESS) {
   11909       return rc;
   11910     }
   11911   }
   11912   return TPM_RC_SUCCESS;
   11913 }
   11914 
   11915 void LoadErrorCallback(
   11916     const Tpm::LoadResponse& callback,
   11917     TPM_RC response_code) {
   11918   VLOG(1) << __func__;
   11919   callback.Run(response_code,
   11920                TPM_HANDLE(),
   11921                TPM2B_NAME());
   11922 }
   11923 
   11924 void LoadResponseParser(
   11925     const Tpm::LoadResponse& callback,
   11926     AuthorizationDelegate* authorization_delegate,
   11927     const std::string& response) {
   11928   VLOG(1) << __func__;
   11929   base::Callback<void(TPM_RC)> error_reporter =
   11930       base::Bind(LoadErrorCallback, callback);
   11931   TPM_HANDLE object_handle;
   11932   TPM2B_NAME name;
   11933   TPM_RC rc = Tpm::ParseResponse_Load(
   11934       response,
   11935       &object_handle,
   11936       &name,
   11937       authorization_delegate);
   11938   if (rc != TPM_RC_SUCCESS) {
   11939     error_reporter.Run(rc);
   11940     return;
   11941   }
   11942   callback.Run(
   11943       rc,
   11944       object_handle,
   11945       name);
   11946 }
   11947 
   11948 void Tpm::Load(
   11949       const TPMI_DH_OBJECT& parent_handle,
   11950       const std::string& parent_handle_name,
   11951       const TPM2B_PRIVATE& in_private,
   11952       const TPM2B_PUBLIC& in_public,
   11953       AuthorizationDelegate* authorization_delegate,
   11954       const LoadResponse& callback) {
   11955   VLOG(1) << __func__;
   11956   base::Callback<void(TPM_RC)> error_reporter =
   11957       base::Bind(LoadErrorCallback, callback);
   11958   base::Callback<void(const std::string&)> parser =
   11959       base::Bind(LoadResponseParser,
   11960                  callback,
   11961                  authorization_delegate);
   11962   std::string command;
   11963   TPM_RC rc = SerializeCommand_Load(
   11964       parent_handle,
   11965       parent_handle_name,
   11966       in_private,
   11967       in_public,
   11968       &command,
   11969       authorization_delegate);
   11970   if (rc != TPM_RC_SUCCESS) {
   11971     error_reporter.Run(rc);
   11972     return;
   11973   }
   11974   transceiver_->SendCommand(command, parser);
   11975 }
   11976 
   11977 TPM_RC Tpm::LoadSync(
   11978       const TPMI_DH_OBJECT& parent_handle,
   11979       const std::string& parent_handle_name,
   11980       const TPM2B_PRIVATE& in_private,
   11981       const TPM2B_PUBLIC& in_public,
   11982       TPM_HANDLE* object_handle,
   11983       TPM2B_NAME* name,
   11984       AuthorizationDelegate* authorization_delegate) {
   11985   VLOG(1) << __func__;
   11986   std::string command;
   11987   TPM_RC rc = SerializeCommand_Load(
   11988       parent_handle,
   11989       parent_handle_name,
   11990       in_private,
   11991       in_public,
   11992       &command,
   11993       authorization_delegate);
   11994   if (rc != TPM_RC_SUCCESS) {
   11995     return rc;
   11996   }
   11997   std::string response = transceiver_->SendCommandAndWait(command);
   11998   rc = ParseResponse_Load(
   11999       response,
   12000       object_handle,
   12001       name,
   12002       authorization_delegate);
   12003   return rc;
   12004 }
   12005 
   12006 TPM_RC Tpm::SerializeCommand_LoadExternal(
   12007       const TPM2B_SENSITIVE& in_private,
   12008       const TPM2B_PUBLIC& in_public,
   12009       const TPMI_RH_HIERARCHY& hierarchy,
   12010       std::string* serialized_command,
   12011       AuthorizationDelegate* authorization_delegate) {
   12012   VLOG(3) << __func__;
   12013   TPM_RC rc = TPM_RC_SUCCESS;
   12014   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   12015   UINT32 command_size = 10;  // Header size.
   12016   std::string handle_section_bytes;
   12017   std::string parameter_section_bytes;
   12018   TPM_CC command_code = TPM_CC_LoadExternal;
   12019   bool is_command_parameter_encryption_possible = true;
   12020   bool is_response_parameter_encryption_possible = true;
   12021   std::string command_code_bytes;
   12022   rc = Serialize_TPM_CC(
   12023       command_code,
   12024       &command_code_bytes);
   12025   if (rc != TPM_RC_SUCCESS) {
   12026     return rc;
   12027   }
   12028   std::string in_private_bytes;
   12029   rc = Serialize_TPM2B_SENSITIVE(
   12030       in_private,
   12031       &in_private_bytes);
   12032   if (rc != TPM_RC_SUCCESS) {
   12033     return rc;
   12034   }
   12035   std::string in_public_bytes;
   12036   rc = Serialize_TPM2B_PUBLIC(
   12037       in_public,
   12038       &in_public_bytes);
   12039   if (rc != TPM_RC_SUCCESS) {
   12040     return rc;
   12041   }
   12042   std::string hierarchy_bytes;
   12043   rc = Serialize_TPMI_RH_HIERARCHY(
   12044       hierarchy,
   12045       &hierarchy_bytes);
   12046   if (rc != TPM_RC_SUCCESS) {
   12047     return rc;
   12048   }
   12049   if (authorization_delegate) {
   12050     // Encrypt just the parameter data, not the size.
   12051     std::string tmp = in_private_bytes.substr(2);
   12052     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   12053       return TRUNKS_RC_ENCRYPTION_FAILED;
   12054     }
   12055     in_private_bytes.replace(2, std::string::npos, tmp);
   12056   }
   12057   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   12058       crypto::SecureHash::SHA256));
   12059   hash->Update(command_code_bytes.data(),
   12060                command_code_bytes.size());
   12061   hash->Update(in_private_bytes.data(),
   12062                in_private_bytes.size());
   12063   parameter_section_bytes += in_private_bytes;
   12064   command_size += in_private_bytes.size();
   12065   hash->Update(in_public_bytes.data(),
   12066                in_public_bytes.size());
   12067   parameter_section_bytes += in_public_bytes;
   12068   command_size += in_public_bytes.size();
   12069   hash->Update(hierarchy_bytes.data(),
   12070                hierarchy_bytes.size());
   12071   parameter_section_bytes += hierarchy_bytes;
   12072   command_size += hierarchy_bytes.size();
   12073   std::string command_hash(32, 0);
   12074   hash->Finish(string_as_array(&command_hash), command_hash.size());
   12075   std::string authorization_section_bytes;
   12076   std::string authorization_size_bytes;
   12077   if (authorization_delegate) {
   12078     if (!authorization_delegate->GetCommandAuthorization(
   12079         command_hash,
   12080         is_command_parameter_encryption_possible,
   12081         is_response_parameter_encryption_possible,
   12082         &authorization_section_bytes)) {
   12083       return TRUNKS_RC_AUTHORIZATION_FAILED;
   12084     }
   12085     if (!authorization_section_bytes.empty()) {
   12086       tag = TPM_ST_SESSIONS;
   12087       std::string tmp;
   12088       rc = Serialize_UINT32(authorization_section_bytes.size(),
   12089                             &authorization_size_bytes);
   12090       if (rc != TPM_RC_SUCCESS) {
   12091         return rc;
   12092       }
   12093       command_size += authorization_size_bytes.size() +
   12094                       authorization_section_bytes.size();
   12095     }
   12096   }
   12097   std::string tag_bytes;
   12098   rc = Serialize_TPMI_ST_COMMAND_TAG(
   12099       tag,
   12100       &tag_bytes);
   12101   if (rc != TPM_RC_SUCCESS) {
   12102     return rc;
   12103   }
   12104   std::string command_size_bytes;
   12105   rc = Serialize_UINT32(
   12106       command_size,
   12107       &command_size_bytes);
   12108   if (rc != TPM_RC_SUCCESS) {
   12109     return rc;
   12110   }
   12111   *serialized_command = tag_bytes +
   12112                         command_size_bytes +
   12113                         command_code_bytes +
   12114                         handle_section_bytes +
   12115                         authorization_size_bytes +
   12116                         authorization_section_bytes +
   12117                         parameter_section_bytes;
   12118   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   12119   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   12120                                             serialized_command->size());
   12121   return TPM_RC_SUCCESS;
   12122 }
   12123 
   12124 TPM_RC Tpm::ParseResponse_LoadExternal(
   12125       const std::string& response,
   12126       TPM_HANDLE* object_handle,
   12127       TPM2B_NAME* name,
   12128       AuthorizationDelegate* authorization_delegate) {
   12129   VLOG(3) << __func__;
   12130   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   12131   TPM_RC rc = TPM_RC_SUCCESS;
   12132   std::string buffer(response);
   12133   TPM_ST tag;
   12134   std::string tag_bytes;
   12135   rc = Parse_TPM_ST(
   12136       &buffer,
   12137       &tag,
   12138       &tag_bytes);
   12139   if (rc != TPM_RC_SUCCESS) {
   12140     return rc;
   12141   }
   12142   UINT32 response_size;
   12143   std::string response_size_bytes;
   12144   rc = Parse_UINT32(
   12145       &buffer,
   12146       &response_size,
   12147       &response_size_bytes);
   12148   if (rc != TPM_RC_SUCCESS) {
   12149     return rc;
   12150   }
   12151   TPM_RC response_code;
   12152   std::string response_code_bytes;
   12153   rc = Parse_TPM_RC(
   12154       &buffer,
   12155       &response_code,
   12156       &response_code_bytes);
   12157   if (rc != TPM_RC_SUCCESS) {
   12158     return rc;
   12159   }
   12160   if (response_size != response.size()) {
   12161     return TPM_RC_SIZE;
   12162   }
   12163   if (response_code != TPM_RC_SUCCESS) {
   12164     return response_code;
   12165   }
   12166   std::string object_handle_bytes;
   12167   rc = Parse_TPM_HANDLE(
   12168       &buffer,
   12169       object_handle,
   12170       &object_handle_bytes);
   12171   if (rc != TPM_RC_SUCCESS) {
   12172     return rc;
   12173   }
   12174   TPM_CC command_code = TPM_CC_LoadExternal;
   12175   std::string command_code_bytes;
   12176   rc = Serialize_TPM_CC(
   12177       command_code,
   12178       &command_code_bytes);
   12179   if (rc != TPM_RC_SUCCESS) {
   12180     return rc;
   12181   }
   12182   std::string authorization_section_bytes;
   12183   if (tag == TPM_ST_SESSIONS) {
   12184     UINT32 parameter_section_size = buffer.size();
   12185     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   12186     if (rc != TPM_RC_SUCCESS) {
   12187       return rc;
   12188     }
   12189     if (parameter_section_size > buffer.size()) {
   12190       return TPM_RC_INSUFFICIENT;
   12191     }
   12192     authorization_section_bytes = buffer.substr(parameter_section_size);
   12193     // Keep the parameter section in |buffer|.
   12194     buffer.erase(parameter_section_size);
   12195   }
   12196   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   12197       crypto::SecureHash::SHA256));
   12198   hash->Update(response_code_bytes.data(),
   12199                response_code_bytes.size());
   12200   hash->Update(command_code_bytes.data(),
   12201                command_code_bytes.size());
   12202   hash->Update(buffer.data(),
   12203                buffer.size());
   12204   std::string response_hash(32, 0);
   12205   hash->Finish(string_as_array(&response_hash), response_hash.size());
   12206   if (tag == TPM_ST_SESSIONS) {
   12207     CHECK(authorization_delegate) << "Authorization delegate missing!";
   12208     if (!authorization_delegate->CheckResponseAuthorization(
   12209         response_hash,
   12210         authorization_section_bytes)) {
   12211       return TRUNKS_RC_AUTHORIZATION_FAILED;
   12212     }
   12213   }
   12214   std::string name_bytes;
   12215   rc = Parse_TPM2B_NAME(
   12216       &buffer,
   12217       name,
   12218       &name_bytes);
   12219   if (rc != TPM_RC_SUCCESS) {
   12220     return rc;
   12221   }
   12222   if (tag == TPM_ST_SESSIONS) {
   12223     CHECK(authorization_delegate) << "Authorization delegate missing!";
   12224     // Decrypt just the parameter data, not the size.
   12225     std::string tmp = name_bytes.substr(2);
   12226     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   12227       return TRUNKS_RC_ENCRYPTION_FAILED;
   12228     }
   12229     name_bytes.replace(2, std::string::npos, tmp);
   12230     rc = Parse_TPM2B_NAME(
   12231         &name_bytes,
   12232         name,
   12233         nullptr);
   12234     if (rc != TPM_RC_SUCCESS) {
   12235       return rc;
   12236     }
   12237   }
   12238   return TPM_RC_SUCCESS;
   12239 }
   12240 
   12241 void LoadExternalErrorCallback(
   12242     const Tpm::LoadExternalResponse& callback,
   12243     TPM_RC response_code) {
   12244   VLOG(1) << __func__;
   12245   callback.Run(response_code,
   12246                TPM_HANDLE(),
   12247                TPM2B_NAME());
   12248 }
   12249 
   12250 void LoadExternalResponseParser(
   12251     const Tpm::LoadExternalResponse& callback,
   12252     AuthorizationDelegate* authorization_delegate,
   12253     const std::string& response) {
   12254   VLOG(1) << __func__;
   12255   base::Callback<void(TPM_RC)> error_reporter =
   12256       base::Bind(LoadExternalErrorCallback, callback);
   12257   TPM_HANDLE object_handle;
   12258   TPM2B_NAME name;
   12259   TPM_RC rc = Tpm::ParseResponse_LoadExternal(
   12260       response,
   12261       &object_handle,
   12262       &name,
   12263       authorization_delegate);
   12264   if (rc != TPM_RC_SUCCESS) {
   12265     error_reporter.Run(rc);
   12266     return;
   12267   }
   12268   callback.Run(
   12269       rc,
   12270       object_handle,
   12271       name);
   12272 }
   12273 
   12274 void Tpm::LoadExternal(
   12275       const TPM2B_SENSITIVE& in_private,
   12276       const TPM2B_PUBLIC& in_public,
   12277       const TPMI_RH_HIERARCHY& hierarchy,
   12278       AuthorizationDelegate* authorization_delegate,
   12279       const LoadExternalResponse& callback) {
   12280   VLOG(1) << __func__;
   12281   base::Callback<void(TPM_RC)> error_reporter =
   12282       base::Bind(LoadExternalErrorCallback, callback);
   12283   base::Callback<void(const std::string&)> parser =
   12284       base::Bind(LoadExternalResponseParser,
   12285                  callback,
   12286                  authorization_delegate);
   12287   std::string command;
   12288   TPM_RC rc = SerializeCommand_LoadExternal(
   12289       in_private,
   12290       in_public,
   12291       hierarchy,
   12292       &command,
   12293       authorization_delegate);
   12294   if (rc != TPM_RC_SUCCESS) {
   12295     error_reporter.Run(rc);
   12296     return;
   12297   }
   12298   transceiver_->SendCommand(command, parser);
   12299 }
   12300 
   12301 TPM_RC Tpm::LoadExternalSync(
   12302       const TPM2B_SENSITIVE& in_private,
   12303       const TPM2B_PUBLIC& in_public,
   12304       const TPMI_RH_HIERARCHY& hierarchy,
   12305       TPM_HANDLE* object_handle,
   12306       TPM2B_NAME* name,
   12307       AuthorizationDelegate* authorization_delegate) {
   12308   VLOG(1) << __func__;
   12309   std::string command;
   12310   TPM_RC rc = SerializeCommand_LoadExternal(
   12311       in_private,
   12312       in_public,
   12313       hierarchy,
   12314       &command,
   12315       authorization_delegate);
   12316   if (rc != TPM_RC_SUCCESS) {
   12317     return rc;
   12318   }
   12319   std::string response = transceiver_->SendCommandAndWait(command);
   12320   rc = ParseResponse_LoadExternal(
   12321       response,
   12322       object_handle,
   12323       name,
   12324       authorization_delegate);
   12325   return rc;
   12326 }
   12327 
   12328 TPM_RC Tpm::SerializeCommand_ReadPublic(
   12329       const TPMI_DH_OBJECT& object_handle,
   12330       const std::string& object_handle_name,
   12331       std::string* serialized_command,
   12332       AuthorizationDelegate* authorization_delegate) {
   12333   VLOG(3) << __func__;
   12334   TPM_RC rc = TPM_RC_SUCCESS;
   12335   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   12336   UINT32 command_size = 10;  // Header size.
   12337   std::string handle_section_bytes;
   12338   std::string parameter_section_bytes;
   12339   TPM_CC command_code = TPM_CC_ReadPublic;
   12340   bool is_command_parameter_encryption_possible = false;
   12341   bool is_response_parameter_encryption_possible = true;
   12342   std::string command_code_bytes;
   12343   rc = Serialize_TPM_CC(
   12344       command_code,
   12345       &command_code_bytes);
   12346   if (rc != TPM_RC_SUCCESS) {
   12347     return rc;
   12348   }
   12349   std::string object_handle_bytes;
   12350   rc = Serialize_TPMI_DH_OBJECT(
   12351       object_handle,
   12352       &object_handle_bytes);
   12353   if (rc != TPM_RC_SUCCESS) {
   12354     return rc;
   12355   }
   12356   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   12357       crypto::SecureHash::SHA256));
   12358   hash->Update(command_code_bytes.data(),
   12359                command_code_bytes.size());
   12360   hash->Update(object_handle_name.data(),
   12361                object_handle_name.size());
   12362   handle_section_bytes += object_handle_bytes;
   12363   command_size += object_handle_bytes.size();
   12364   std::string command_hash(32, 0);
   12365   hash->Finish(string_as_array(&command_hash), command_hash.size());
   12366   std::string authorization_section_bytes;
   12367   std::string authorization_size_bytes;
   12368   if (authorization_delegate) {
   12369     if (!authorization_delegate->GetCommandAuthorization(
   12370         command_hash,
   12371         is_command_parameter_encryption_possible,
   12372         is_response_parameter_encryption_possible,
   12373         &authorization_section_bytes)) {
   12374       return TRUNKS_RC_AUTHORIZATION_FAILED;
   12375     }
   12376     if (!authorization_section_bytes.empty()) {
   12377       tag = TPM_ST_SESSIONS;
   12378       std::string tmp;
   12379       rc = Serialize_UINT32(authorization_section_bytes.size(),
   12380                             &authorization_size_bytes);
   12381       if (rc != TPM_RC_SUCCESS) {
   12382         return rc;
   12383       }
   12384       command_size += authorization_size_bytes.size() +
   12385                       authorization_section_bytes.size();
   12386     }
   12387   }
   12388   std::string tag_bytes;
   12389   rc = Serialize_TPMI_ST_COMMAND_TAG(
   12390       tag,
   12391       &tag_bytes);
   12392   if (rc != TPM_RC_SUCCESS) {
   12393     return rc;
   12394   }
   12395   std::string command_size_bytes;
   12396   rc = Serialize_UINT32(
   12397       command_size,
   12398       &command_size_bytes);
   12399   if (rc != TPM_RC_SUCCESS) {
   12400     return rc;
   12401   }
   12402   *serialized_command = tag_bytes +
   12403                         command_size_bytes +
   12404                         command_code_bytes +
   12405                         handle_section_bytes +
   12406                         authorization_size_bytes +
   12407                         authorization_section_bytes +
   12408                         parameter_section_bytes;
   12409   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   12410   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   12411                                             serialized_command->size());
   12412   return TPM_RC_SUCCESS;
   12413 }
   12414 
   12415 TPM_RC Tpm::ParseResponse_ReadPublic(
   12416       const std::string& response,
   12417       TPM2B_PUBLIC* out_public,
   12418       TPM2B_NAME* name,
   12419       TPM2B_NAME* qualified_name,
   12420       AuthorizationDelegate* authorization_delegate) {
   12421   VLOG(3) << __func__;
   12422   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   12423   TPM_RC rc = TPM_RC_SUCCESS;
   12424   std::string buffer(response);
   12425   TPM_ST tag;
   12426   std::string tag_bytes;
   12427   rc = Parse_TPM_ST(
   12428       &buffer,
   12429       &tag,
   12430       &tag_bytes);
   12431   if (rc != TPM_RC_SUCCESS) {
   12432     return rc;
   12433   }
   12434   UINT32 response_size;
   12435   std::string response_size_bytes;
   12436   rc = Parse_UINT32(
   12437       &buffer,
   12438       &response_size,
   12439       &response_size_bytes);
   12440   if (rc != TPM_RC_SUCCESS) {
   12441     return rc;
   12442   }
   12443   TPM_RC response_code;
   12444   std::string response_code_bytes;
   12445   rc = Parse_TPM_RC(
   12446       &buffer,
   12447       &response_code,
   12448       &response_code_bytes);
   12449   if (rc != TPM_RC_SUCCESS) {
   12450     return rc;
   12451   }
   12452   if (response_size != response.size()) {
   12453     return TPM_RC_SIZE;
   12454   }
   12455   if (response_code != TPM_RC_SUCCESS) {
   12456     return response_code;
   12457   }
   12458   TPM_CC command_code = TPM_CC_ReadPublic;
   12459   std::string command_code_bytes;
   12460   rc = Serialize_TPM_CC(
   12461       command_code,
   12462       &command_code_bytes);
   12463   if (rc != TPM_RC_SUCCESS) {
   12464     return rc;
   12465   }
   12466   std::string authorization_section_bytes;
   12467   if (tag == TPM_ST_SESSIONS) {
   12468     UINT32 parameter_section_size = buffer.size();
   12469     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   12470     if (rc != TPM_RC_SUCCESS) {
   12471       return rc;
   12472     }
   12473     if (parameter_section_size > buffer.size()) {
   12474       return TPM_RC_INSUFFICIENT;
   12475     }
   12476     authorization_section_bytes = buffer.substr(parameter_section_size);
   12477     // Keep the parameter section in |buffer|.
   12478     buffer.erase(parameter_section_size);
   12479   }
   12480   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   12481       crypto::SecureHash::SHA256));
   12482   hash->Update(response_code_bytes.data(),
   12483                response_code_bytes.size());
   12484   hash->Update(command_code_bytes.data(),
   12485                command_code_bytes.size());
   12486   hash->Update(buffer.data(),
   12487                buffer.size());
   12488   std::string response_hash(32, 0);
   12489   hash->Finish(string_as_array(&response_hash), response_hash.size());
   12490   if (tag == TPM_ST_SESSIONS) {
   12491     CHECK(authorization_delegate) << "Authorization delegate missing!";
   12492     if (!authorization_delegate->CheckResponseAuthorization(
   12493         response_hash,
   12494         authorization_section_bytes)) {
   12495       return TRUNKS_RC_AUTHORIZATION_FAILED;
   12496     }
   12497   }
   12498   std::string out_public_bytes;
   12499   rc = Parse_TPM2B_PUBLIC(
   12500       &buffer,
   12501       out_public,
   12502       &out_public_bytes);
   12503   if (rc != TPM_RC_SUCCESS) {
   12504     return rc;
   12505   }
   12506   std::string name_bytes;
   12507   rc = Parse_TPM2B_NAME(
   12508       &buffer,
   12509       name,
   12510       &name_bytes);
   12511   if (rc != TPM_RC_SUCCESS) {
   12512     return rc;
   12513   }
   12514   std::string qualified_name_bytes;
   12515   rc = Parse_TPM2B_NAME(
   12516       &buffer,
   12517       qualified_name,
   12518       &qualified_name_bytes);
   12519   if (rc != TPM_RC_SUCCESS) {
   12520     return rc;
   12521   }
   12522   if (tag == TPM_ST_SESSIONS) {
   12523     CHECK(authorization_delegate) << "Authorization delegate missing!";
   12524     // Decrypt just the parameter data, not the size.
   12525     std::string tmp = out_public_bytes.substr(2);
   12526     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   12527       return TRUNKS_RC_ENCRYPTION_FAILED;
   12528     }
   12529     out_public_bytes.replace(2, std::string::npos, tmp);
   12530     rc = Parse_TPM2B_PUBLIC(
   12531         &out_public_bytes,
   12532         out_public,
   12533         nullptr);
   12534     if (rc != TPM_RC_SUCCESS) {
   12535       return rc;
   12536     }
   12537   }
   12538   return TPM_RC_SUCCESS;
   12539 }
   12540 
   12541 void ReadPublicErrorCallback(
   12542     const Tpm::ReadPublicResponse& callback,
   12543     TPM_RC response_code) {
   12544   VLOG(1) << __func__;
   12545   callback.Run(response_code,
   12546                TPM2B_PUBLIC(),
   12547                TPM2B_NAME(),
   12548                TPM2B_NAME());
   12549 }
   12550 
   12551 void ReadPublicResponseParser(
   12552     const Tpm::ReadPublicResponse& callback,
   12553     AuthorizationDelegate* authorization_delegate,
   12554     const std::string& response) {
   12555   VLOG(1) << __func__;
   12556   base::Callback<void(TPM_RC)> error_reporter =
   12557       base::Bind(ReadPublicErrorCallback, callback);
   12558   TPM2B_PUBLIC out_public;
   12559   TPM2B_NAME name;
   12560   TPM2B_NAME qualified_name;
   12561   TPM_RC rc = Tpm::ParseResponse_ReadPublic(
   12562       response,
   12563       &out_public,
   12564       &name,
   12565       &qualified_name,
   12566       authorization_delegate);
   12567   if (rc != TPM_RC_SUCCESS) {
   12568     error_reporter.Run(rc);
   12569     return;
   12570   }
   12571   callback.Run(
   12572       rc,
   12573       out_public,
   12574       name,
   12575       qualified_name);
   12576 }
   12577 
   12578 void Tpm::ReadPublic(
   12579       const TPMI_DH_OBJECT& object_handle,
   12580       const std::string& object_handle_name,
   12581       AuthorizationDelegate* authorization_delegate,
   12582       const ReadPublicResponse& callback) {
   12583   VLOG(1) << __func__;
   12584   base::Callback<void(TPM_RC)> error_reporter =
   12585       base::Bind(ReadPublicErrorCallback, callback);
   12586   base::Callback<void(const std::string&)> parser =
   12587       base::Bind(ReadPublicResponseParser,
   12588                  callback,
   12589                  authorization_delegate);
   12590   std::string command;
   12591   TPM_RC rc = SerializeCommand_ReadPublic(
   12592       object_handle,
   12593       object_handle_name,
   12594       &command,
   12595       authorization_delegate);
   12596   if (rc != TPM_RC_SUCCESS) {
   12597     error_reporter.Run(rc);
   12598     return;
   12599   }
   12600   transceiver_->SendCommand(command, parser);
   12601 }
   12602 
   12603 TPM_RC Tpm::ReadPublicSync(
   12604       const TPMI_DH_OBJECT& object_handle,
   12605       const std::string& object_handle_name,
   12606       TPM2B_PUBLIC* out_public,
   12607       TPM2B_NAME* name,
   12608       TPM2B_NAME* qualified_name,
   12609       AuthorizationDelegate* authorization_delegate) {
   12610   VLOG(1) << __func__;
   12611   std::string command;
   12612   TPM_RC rc = SerializeCommand_ReadPublic(
   12613       object_handle,
   12614       object_handle_name,
   12615       &command,
   12616       authorization_delegate);
   12617   if (rc != TPM_RC_SUCCESS) {
   12618     return rc;
   12619   }
   12620   std::string response = transceiver_->SendCommandAndWait(command);
   12621   rc = ParseResponse_ReadPublic(
   12622       response,
   12623       out_public,
   12624       name,
   12625       qualified_name,
   12626       authorization_delegate);
   12627   return rc;
   12628 }
   12629 
   12630 TPM_RC Tpm::SerializeCommand_ActivateCredential(
   12631       const TPMI_DH_OBJECT& activate_handle,
   12632       const std::string& activate_handle_name,
   12633       const TPMI_DH_OBJECT& key_handle,
   12634       const std::string& key_handle_name,
   12635       const TPM2B_ID_OBJECT& credential_blob,
   12636       const TPM2B_ENCRYPTED_SECRET& secret,
   12637       std::string* serialized_command,
   12638       AuthorizationDelegate* authorization_delegate) {
   12639   VLOG(3) << __func__;
   12640   TPM_RC rc = TPM_RC_SUCCESS;
   12641   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   12642   UINT32 command_size = 10;  // Header size.
   12643   std::string handle_section_bytes;
   12644   std::string parameter_section_bytes;
   12645   TPM_CC command_code = TPM_CC_ActivateCredential;
   12646   bool is_command_parameter_encryption_possible = true;
   12647   bool is_response_parameter_encryption_possible = true;
   12648   std::string command_code_bytes;
   12649   rc = Serialize_TPM_CC(
   12650       command_code,
   12651       &command_code_bytes);
   12652   if (rc != TPM_RC_SUCCESS) {
   12653     return rc;
   12654   }
   12655   std::string activate_handle_bytes;
   12656   rc = Serialize_TPMI_DH_OBJECT(
   12657       activate_handle,
   12658       &activate_handle_bytes);
   12659   if (rc != TPM_RC_SUCCESS) {
   12660     return rc;
   12661   }
   12662   std::string key_handle_bytes;
   12663   rc = Serialize_TPMI_DH_OBJECT(
   12664       key_handle,
   12665       &key_handle_bytes);
   12666   if (rc != TPM_RC_SUCCESS) {
   12667     return rc;
   12668   }
   12669   std::string credential_blob_bytes;
   12670   rc = Serialize_TPM2B_ID_OBJECT(
   12671       credential_blob,
   12672       &credential_blob_bytes);
   12673   if (rc != TPM_RC_SUCCESS) {
   12674     return rc;
   12675   }
   12676   std::string secret_bytes;
   12677   rc = Serialize_TPM2B_ENCRYPTED_SECRET(
   12678       secret,
   12679       &secret_bytes);
   12680   if (rc != TPM_RC_SUCCESS) {
   12681     return rc;
   12682   }
   12683   if (authorization_delegate) {
   12684     // Encrypt just the parameter data, not the size.
   12685     std::string tmp = credential_blob_bytes.substr(2);
   12686     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   12687       return TRUNKS_RC_ENCRYPTION_FAILED;
   12688     }
   12689     credential_blob_bytes.replace(2, std::string::npos, tmp);
   12690   }
   12691   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   12692       crypto::SecureHash::SHA256));
   12693   hash->Update(command_code_bytes.data(),
   12694                command_code_bytes.size());
   12695   hash->Update(activate_handle_name.data(),
   12696                activate_handle_name.size());
   12697   handle_section_bytes += activate_handle_bytes;
   12698   command_size += activate_handle_bytes.size();
   12699   hash->Update(key_handle_name.data(),
   12700                key_handle_name.size());
   12701   handle_section_bytes += key_handle_bytes;
   12702   command_size += key_handle_bytes.size();
   12703   hash->Update(credential_blob_bytes.data(),
   12704                credential_blob_bytes.size());
   12705   parameter_section_bytes += credential_blob_bytes;
   12706   command_size += credential_blob_bytes.size();
   12707   hash->Update(secret_bytes.data(),
   12708                secret_bytes.size());
   12709   parameter_section_bytes += secret_bytes;
   12710   command_size += secret_bytes.size();
   12711   std::string command_hash(32, 0);
   12712   hash->Finish(string_as_array(&command_hash), command_hash.size());
   12713   std::string authorization_section_bytes;
   12714   std::string authorization_size_bytes;
   12715   if (authorization_delegate) {
   12716     if (!authorization_delegate->GetCommandAuthorization(
   12717         command_hash,
   12718         is_command_parameter_encryption_possible,
   12719         is_response_parameter_encryption_possible,
   12720         &authorization_section_bytes)) {
   12721       return TRUNKS_RC_AUTHORIZATION_FAILED;
   12722     }
   12723     if (!authorization_section_bytes.empty()) {
   12724       tag = TPM_ST_SESSIONS;
   12725       std::string tmp;
   12726       rc = Serialize_UINT32(authorization_section_bytes.size(),
   12727                             &authorization_size_bytes);
   12728       if (rc != TPM_RC_SUCCESS) {
   12729         return rc;
   12730       }
   12731       command_size += authorization_size_bytes.size() +
   12732                       authorization_section_bytes.size();
   12733     }
   12734   }
   12735   std::string tag_bytes;
   12736   rc = Serialize_TPMI_ST_COMMAND_TAG(
   12737       tag,
   12738       &tag_bytes);
   12739   if (rc != TPM_RC_SUCCESS) {
   12740     return rc;
   12741   }
   12742   std::string command_size_bytes;
   12743   rc = Serialize_UINT32(
   12744       command_size,
   12745       &command_size_bytes);
   12746   if (rc != TPM_RC_SUCCESS) {
   12747     return rc;
   12748   }
   12749   *serialized_command = tag_bytes +
   12750                         command_size_bytes +
   12751                         command_code_bytes +
   12752                         handle_section_bytes +
   12753                         authorization_size_bytes +
   12754                         authorization_section_bytes +
   12755                         parameter_section_bytes;
   12756   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   12757   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   12758                                             serialized_command->size());
   12759   return TPM_RC_SUCCESS;
   12760 }
   12761 
   12762 TPM_RC Tpm::ParseResponse_ActivateCredential(
   12763       const std::string& response,
   12764       TPM2B_DIGEST* cert_info,
   12765       AuthorizationDelegate* authorization_delegate) {
   12766   VLOG(3) << __func__;
   12767   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   12768   TPM_RC rc = TPM_RC_SUCCESS;
   12769   std::string buffer(response);
   12770   TPM_ST tag;
   12771   std::string tag_bytes;
   12772   rc = Parse_TPM_ST(
   12773       &buffer,
   12774       &tag,
   12775       &tag_bytes);
   12776   if (rc != TPM_RC_SUCCESS) {
   12777     return rc;
   12778   }
   12779   UINT32 response_size;
   12780   std::string response_size_bytes;
   12781   rc = Parse_UINT32(
   12782       &buffer,
   12783       &response_size,
   12784       &response_size_bytes);
   12785   if (rc != TPM_RC_SUCCESS) {
   12786     return rc;
   12787   }
   12788   TPM_RC response_code;
   12789   std::string response_code_bytes;
   12790   rc = Parse_TPM_RC(
   12791       &buffer,
   12792       &response_code,
   12793       &response_code_bytes);
   12794   if (rc != TPM_RC_SUCCESS) {
   12795     return rc;
   12796   }
   12797   if (response_size != response.size()) {
   12798     return TPM_RC_SIZE;
   12799   }
   12800   if (response_code != TPM_RC_SUCCESS) {
   12801     return response_code;
   12802   }
   12803   TPM_CC command_code = TPM_CC_ActivateCredential;
   12804   std::string command_code_bytes;
   12805   rc = Serialize_TPM_CC(
   12806       command_code,
   12807       &command_code_bytes);
   12808   if (rc != TPM_RC_SUCCESS) {
   12809     return rc;
   12810   }
   12811   std::string authorization_section_bytes;
   12812   if (tag == TPM_ST_SESSIONS) {
   12813     UINT32 parameter_section_size = buffer.size();
   12814     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   12815     if (rc != TPM_RC_SUCCESS) {
   12816       return rc;
   12817     }
   12818     if (parameter_section_size > buffer.size()) {
   12819       return TPM_RC_INSUFFICIENT;
   12820     }
   12821     authorization_section_bytes = buffer.substr(parameter_section_size);
   12822     // Keep the parameter section in |buffer|.
   12823     buffer.erase(parameter_section_size);
   12824   }
   12825   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   12826       crypto::SecureHash::SHA256));
   12827   hash->Update(response_code_bytes.data(),
   12828                response_code_bytes.size());
   12829   hash->Update(command_code_bytes.data(),
   12830                command_code_bytes.size());
   12831   hash->Update(buffer.data(),
   12832                buffer.size());
   12833   std::string response_hash(32, 0);
   12834   hash->Finish(string_as_array(&response_hash), response_hash.size());
   12835   if (tag == TPM_ST_SESSIONS) {
   12836     CHECK(authorization_delegate) << "Authorization delegate missing!";
   12837     if (!authorization_delegate->CheckResponseAuthorization(
   12838         response_hash,
   12839         authorization_section_bytes)) {
   12840       return TRUNKS_RC_AUTHORIZATION_FAILED;
   12841     }
   12842   }
   12843   std::string cert_info_bytes;
   12844   rc = Parse_TPM2B_DIGEST(
   12845       &buffer,
   12846       cert_info,
   12847       &cert_info_bytes);
   12848   if (rc != TPM_RC_SUCCESS) {
   12849     return rc;
   12850   }
   12851   if (tag == TPM_ST_SESSIONS) {
   12852     CHECK(authorization_delegate) << "Authorization delegate missing!";
   12853     // Decrypt just the parameter data, not the size.
   12854     std::string tmp = cert_info_bytes.substr(2);
   12855     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   12856       return TRUNKS_RC_ENCRYPTION_FAILED;
   12857     }
   12858     cert_info_bytes.replace(2, std::string::npos, tmp);
   12859     rc = Parse_TPM2B_DIGEST(
   12860         &cert_info_bytes,
   12861         cert_info,
   12862         nullptr);
   12863     if (rc != TPM_RC_SUCCESS) {
   12864       return rc;
   12865     }
   12866   }
   12867   return TPM_RC_SUCCESS;
   12868 }
   12869 
   12870 void ActivateCredentialErrorCallback(
   12871     const Tpm::ActivateCredentialResponse& callback,
   12872     TPM_RC response_code) {
   12873   VLOG(1) << __func__;
   12874   callback.Run(response_code,
   12875                TPM2B_DIGEST());
   12876 }
   12877 
   12878 void ActivateCredentialResponseParser(
   12879     const Tpm::ActivateCredentialResponse& callback,
   12880     AuthorizationDelegate* authorization_delegate,
   12881     const std::string& response) {
   12882   VLOG(1) << __func__;
   12883   base::Callback<void(TPM_RC)> error_reporter =
   12884       base::Bind(ActivateCredentialErrorCallback, callback);
   12885   TPM2B_DIGEST cert_info;
   12886   TPM_RC rc = Tpm::ParseResponse_ActivateCredential(
   12887       response,
   12888       &cert_info,
   12889       authorization_delegate);
   12890   if (rc != TPM_RC_SUCCESS) {
   12891     error_reporter.Run(rc);
   12892     return;
   12893   }
   12894   callback.Run(
   12895       rc,
   12896       cert_info);
   12897 }
   12898 
   12899 void Tpm::ActivateCredential(
   12900       const TPMI_DH_OBJECT& activate_handle,
   12901       const std::string& activate_handle_name,
   12902       const TPMI_DH_OBJECT& key_handle,
   12903       const std::string& key_handle_name,
   12904       const TPM2B_ID_OBJECT& credential_blob,
   12905       const TPM2B_ENCRYPTED_SECRET& secret,
   12906       AuthorizationDelegate* authorization_delegate,
   12907       const ActivateCredentialResponse& callback) {
   12908   VLOG(1) << __func__;
   12909   base::Callback<void(TPM_RC)> error_reporter =
   12910       base::Bind(ActivateCredentialErrorCallback, callback);
   12911   base::Callback<void(const std::string&)> parser =
   12912       base::Bind(ActivateCredentialResponseParser,
   12913                  callback,
   12914                  authorization_delegate);
   12915   std::string command;
   12916   TPM_RC rc = SerializeCommand_ActivateCredential(
   12917       activate_handle,
   12918       activate_handle_name,
   12919       key_handle,
   12920       key_handle_name,
   12921       credential_blob,
   12922       secret,
   12923       &command,
   12924       authorization_delegate);
   12925   if (rc != TPM_RC_SUCCESS) {
   12926     error_reporter.Run(rc);
   12927     return;
   12928   }
   12929   transceiver_->SendCommand(command, parser);
   12930 }
   12931 
   12932 TPM_RC Tpm::ActivateCredentialSync(
   12933       const TPMI_DH_OBJECT& activate_handle,
   12934       const std::string& activate_handle_name,
   12935       const TPMI_DH_OBJECT& key_handle,
   12936       const std::string& key_handle_name,
   12937       const TPM2B_ID_OBJECT& credential_blob,
   12938       const TPM2B_ENCRYPTED_SECRET& secret,
   12939       TPM2B_DIGEST* cert_info,
   12940       AuthorizationDelegate* authorization_delegate) {
   12941   VLOG(1) << __func__;
   12942   std::string command;
   12943   TPM_RC rc = SerializeCommand_ActivateCredential(
   12944       activate_handle,
   12945       activate_handle_name,
   12946       key_handle,
   12947       key_handle_name,
   12948       credential_blob,
   12949       secret,
   12950       &command,
   12951       authorization_delegate);
   12952   if (rc != TPM_RC_SUCCESS) {
   12953     return rc;
   12954   }
   12955   std::string response = transceiver_->SendCommandAndWait(command);
   12956   rc = ParseResponse_ActivateCredential(
   12957       response,
   12958       cert_info,
   12959       authorization_delegate);
   12960   return rc;
   12961 }
   12962 
   12963 TPM_RC Tpm::SerializeCommand_MakeCredential(
   12964       const TPMI_DH_OBJECT& handle,
   12965       const std::string& handle_name,
   12966       const TPM2B_DIGEST& credential,
   12967       const TPM2B_NAME& object_name,
   12968       std::string* serialized_command,
   12969       AuthorizationDelegate* authorization_delegate) {
   12970   VLOG(3) << __func__;
   12971   TPM_RC rc = TPM_RC_SUCCESS;
   12972   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   12973   UINT32 command_size = 10;  // Header size.
   12974   std::string handle_section_bytes;
   12975   std::string parameter_section_bytes;
   12976   TPM_CC command_code = TPM_CC_MakeCredential;
   12977   bool is_command_parameter_encryption_possible = true;
   12978   bool is_response_parameter_encryption_possible = true;
   12979   std::string command_code_bytes;
   12980   rc = Serialize_TPM_CC(
   12981       command_code,
   12982       &command_code_bytes);
   12983   if (rc != TPM_RC_SUCCESS) {
   12984     return rc;
   12985   }
   12986   std::string handle_bytes;
   12987   rc = Serialize_TPMI_DH_OBJECT(
   12988       handle,
   12989       &handle_bytes);
   12990   if (rc != TPM_RC_SUCCESS) {
   12991     return rc;
   12992   }
   12993   std::string credential_bytes;
   12994   rc = Serialize_TPM2B_DIGEST(
   12995       credential,
   12996       &credential_bytes);
   12997   if (rc != TPM_RC_SUCCESS) {
   12998     return rc;
   12999   }
   13000   std::string object_name_bytes;
   13001   rc = Serialize_TPM2B_NAME(
   13002       object_name,
   13003       &object_name_bytes);
   13004   if (rc != TPM_RC_SUCCESS) {
   13005     return rc;
   13006   }
   13007   if (authorization_delegate) {
   13008     // Encrypt just the parameter data, not the size.
   13009     std::string tmp = credential_bytes.substr(2);
   13010     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   13011       return TRUNKS_RC_ENCRYPTION_FAILED;
   13012     }
   13013     credential_bytes.replace(2, std::string::npos, tmp);
   13014   }
   13015   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13016       crypto::SecureHash::SHA256));
   13017   hash->Update(command_code_bytes.data(),
   13018                command_code_bytes.size());
   13019   hash->Update(handle_name.data(),
   13020                handle_name.size());
   13021   handle_section_bytes += handle_bytes;
   13022   command_size += handle_bytes.size();
   13023   hash->Update(credential_bytes.data(),
   13024                credential_bytes.size());
   13025   parameter_section_bytes += credential_bytes;
   13026   command_size += credential_bytes.size();
   13027   hash->Update(object_name_bytes.data(),
   13028                object_name_bytes.size());
   13029   parameter_section_bytes += object_name_bytes;
   13030   command_size += object_name_bytes.size();
   13031   std::string command_hash(32, 0);
   13032   hash->Finish(string_as_array(&command_hash), command_hash.size());
   13033   std::string authorization_section_bytes;
   13034   std::string authorization_size_bytes;
   13035   if (authorization_delegate) {
   13036     if (!authorization_delegate->GetCommandAuthorization(
   13037         command_hash,
   13038         is_command_parameter_encryption_possible,
   13039         is_response_parameter_encryption_possible,
   13040         &authorization_section_bytes)) {
   13041       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13042     }
   13043     if (!authorization_section_bytes.empty()) {
   13044       tag = TPM_ST_SESSIONS;
   13045       std::string tmp;
   13046       rc = Serialize_UINT32(authorization_section_bytes.size(),
   13047                             &authorization_size_bytes);
   13048       if (rc != TPM_RC_SUCCESS) {
   13049         return rc;
   13050       }
   13051       command_size += authorization_size_bytes.size() +
   13052                       authorization_section_bytes.size();
   13053     }
   13054   }
   13055   std::string tag_bytes;
   13056   rc = Serialize_TPMI_ST_COMMAND_TAG(
   13057       tag,
   13058       &tag_bytes);
   13059   if (rc != TPM_RC_SUCCESS) {
   13060     return rc;
   13061   }
   13062   std::string command_size_bytes;
   13063   rc = Serialize_UINT32(
   13064       command_size,
   13065       &command_size_bytes);
   13066   if (rc != TPM_RC_SUCCESS) {
   13067     return rc;
   13068   }
   13069   *serialized_command = tag_bytes +
   13070                         command_size_bytes +
   13071                         command_code_bytes +
   13072                         handle_section_bytes +
   13073                         authorization_size_bytes +
   13074                         authorization_section_bytes +
   13075                         parameter_section_bytes;
   13076   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   13077   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   13078                                             serialized_command->size());
   13079   return TPM_RC_SUCCESS;
   13080 }
   13081 
   13082 TPM_RC Tpm::ParseResponse_MakeCredential(
   13083       const std::string& response,
   13084       TPM2B_ID_OBJECT* credential_blob,
   13085       TPM2B_ENCRYPTED_SECRET* secret,
   13086       AuthorizationDelegate* authorization_delegate) {
   13087   VLOG(3) << __func__;
   13088   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   13089   TPM_RC rc = TPM_RC_SUCCESS;
   13090   std::string buffer(response);
   13091   TPM_ST tag;
   13092   std::string tag_bytes;
   13093   rc = Parse_TPM_ST(
   13094       &buffer,
   13095       &tag,
   13096       &tag_bytes);
   13097   if (rc != TPM_RC_SUCCESS) {
   13098     return rc;
   13099   }
   13100   UINT32 response_size;
   13101   std::string response_size_bytes;
   13102   rc = Parse_UINT32(
   13103       &buffer,
   13104       &response_size,
   13105       &response_size_bytes);
   13106   if (rc != TPM_RC_SUCCESS) {
   13107     return rc;
   13108   }
   13109   TPM_RC response_code;
   13110   std::string response_code_bytes;
   13111   rc = Parse_TPM_RC(
   13112       &buffer,
   13113       &response_code,
   13114       &response_code_bytes);
   13115   if (rc != TPM_RC_SUCCESS) {
   13116     return rc;
   13117   }
   13118   if (response_size != response.size()) {
   13119     return TPM_RC_SIZE;
   13120   }
   13121   if (response_code != TPM_RC_SUCCESS) {
   13122     return response_code;
   13123   }
   13124   TPM_CC command_code = TPM_CC_MakeCredential;
   13125   std::string command_code_bytes;
   13126   rc = Serialize_TPM_CC(
   13127       command_code,
   13128       &command_code_bytes);
   13129   if (rc != TPM_RC_SUCCESS) {
   13130     return rc;
   13131   }
   13132   std::string authorization_section_bytes;
   13133   if (tag == TPM_ST_SESSIONS) {
   13134     UINT32 parameter_section_size = buffer.size();
   13135     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   13136     if (rc != TPM_RC_SUCCESS) {
   13137       return rc;
   13138     }
   13139     if (parameter_section_size > buffer.size()) {
   13140       return TPM_RC_INSUFFICIENT;
   13141     }
   13142     authorization_section_bytes = buffer.substr(parameter_section_size);
   13143     // Keep the parameter section in |buffer|.
   13144     buffer.erase(parameter_section_size);
   13145   }
   13146   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13147       crypto::SecureHash::SHA256));
   13148   hash->Update(response_code_bytes.data(),
   13149                response_code_bytes.size());
   13150   hash->Update(command_code_bytes.data(),
   13151                command_code_bytes.size());
   13152   hash->Update(buffer.data(),
   13153                buffer.size());
   13154   std::string response_hash(32, 0);
   13155   hash->Finish(string_as_array(&response_hash), response_hash.size());
   13156   if (tag == TPM_ST_SESSIONS) {
   13157     CHECK(authorization_delegate) << "Authorization delegate missing!";
   13158     if (!authorization_delegate->CheckResponseAuthorization(
   13159         response_hash,
   13160         authorization_section_bytes)) {
   13161       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13162     }
   13163   }
   13164   std::string credential_blob_bytes;
   13165   rc = Parse_TPM2B_ID_OBJECT(
   13166       &buffer,
   13167       credential_blob,
   13168       &credential_blob_bytes);
   13169   if (rc != TPM_RC_SUCCESS) {
   13170     return rc;
   13171   }
   13172   std::string secret_bytes;
   13173   rc = Parse_TPM2B_ENCRYPTED_SECRET(
   13174       &buffer,
   13175       secret,
   13176       &secret_bytes);
   13177   if (rc != TPM_RC_SUCCESS) {
   13178     return rc;
   13179   }
   13180   if (tag == TPM_ST_SESSIONS) {
   13181     CHECK(authorization_delegate) << "Authorization delegate missing!";
   13182     // Decrypt just the parameter data, not the size.
   13183     std::string tmp = credential_blob_bytes.substr(2);
   13184     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   13185       return TRUNKS_RC_ENCRYPTION_FAILED;
   13186     }
   13187     credential_blob_bytes.replace(2, std::string::npos, tmp);
   13188     rc = Parse_TPM2B_ID_OBJECT(
   13189         &credential_blob_bytes,
   13190         credential_blob,
   13191         nullptr);
   13192     if (rc != TPM_RC_SUCCESS) {
   13193       return rc;
   13194     }
   13195   }
   13196   return TPM_RC_SUCCESS;
   13197 }
   13198 
   13199 void MakeCredentialErrorCallback(
   13200     const Tpm::MakeCredentialResponse& callback,
   13201     TPM_RC response_code) {
   13202   VLOG(1) << __func__;
   13203   callback.Run(response_code,
   13204                TPM2B_ID_OBJECT(),
   13205                TPM2B_ENCRYPTED_SECRET());
   13206 }
   13207 
   13208 void MakeCredentialResponseParser(
   13209     const Tpm::MakeCredentialResponse& callback,
   13210     AuthorizationDelegate* authorization_delegate,
   13211     const std::string& response) {
   13212   VLOG(1) << __func__;
   13213   base::Callback<void(TPM_RC)> error_reporter =
   13214       base::Bind(MakeCredentialErrorCallback, callback);
   13215   TPM2B_ID_OBJECT credential_blob;
   13216   TPM2B_ENCRYPTED_SECRET secret;
   13217   TPM_RC rc = Tpm::ParseResponse_MakeCredential(
   13218       response,
   13219       &credential_blob,
   13220       &secret,
   13221       authorization_delegate);
   13222   if (rc != TPM_RC_SUCCESS) {
   13223     error_reporter.Run(rc);
   13224     return;
   13225   }
   13226   callback.Run(
   13227       rc,
   13228       credential_blob,
   13229       secret);
   13230 }
   13231 
   13232 void Tpm::MakeCredential(
   13233       const TPMI_DH_OBJECT& handle,
   13234       const std::string& handle_name,
   13235       const TPM2B_DIGEST& credential,
   13236       const TPM2B_NAME& object_name,
   13237       AuthorizationDelegate* authorization_delegate,
   13238       const MakeCredentialResponse& callback) {
   13239   VLOG(1) << __func__;
   13240   base::Callback<void(TPM_RC)> error_reporter =
   13241       base::Bind(MakeCredentialErrorCallback, callback);
   13242   base::Callback<void(const std::string&)> parser =
   13243       base::Bind(MakeCredentialResponseParser,
   13244                  callback,
   13245                  authorization_delegate);
   13246   std::string command;
   13247   TPM_RC rc = SerializeCommand_MakeCredential(
   13248       handle,
   13249       handle_name,
   13250       credential,
   13251       object_name,
   13252       &command,
   13253       authorization_delegate);
   13254   if (rc != TPM_RC_SUCCESS) {
   13255     error_reporter.Run(rc);
   13256     return;
   13257   }
   13258   transceiver_->SendCommand(command, parser);
   13259 }
   13260 
   13261 TPM_RC Tpm::MakeCredentialSync(
   13262       const TPMI_DH_OBJECT& handle,
   13263       const std::string& handle_name,
   13264       const TPM2B_DIGEST& credential,
   13265       const TPM2B_NAME& object_name,
   13266       TPM2B_ID_OBJECT* credential_blob,
   13267       TPM2B_ENCRYPTED_SECRET* secret,
   13268       AuthorizationDelegate* authorization_delegate) {
   13269   VLOG(1) << __func__;
   13270   std::string command;
   13271   TPM_RC rc = SerializeCommand_MakeCredential(
   13272       handle,
   13273       handle_name,
   13274       credential,
   13275       object_name,
   13276       &command,
   13277       authorization_delegate);
   13278   if (rc != TPM_RC_SUCCESS) {
   13279     return rc;
   13280   }
   13281   std::string response = transceiver_->SendCommandAndWait(command);
   13282   rc = ParseResponse_MakeCredential(
   13283       response,
   13284       credential_blob,
   13285       secret,
   13286       authorization_delegate);
   13287   return rc;
   13288 }
   13289 
   13290 TPM_RC Tpm::SerializeCommand_Unseal(
   13291       const TPMI_DH_OBJECT& item_handle,
   13292       const std::string& item_handle_name,
   13293       std::string* serialized_command,
   13294       AuthorizationDelegate* authorization_delegate) {
   13295   VLOG(3) << __func__;
   13296   TPM_RC rc = TPM_RC_SUCCESS;
   13297   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   13298   UINT32 command_size = 10;  // Header size.
   13299   std::string handle_section_bytes;
   13300   std::string parameter_section_bytes;
   13301   TPM_CC command_code = TPM_CC_Unseal;
   13302   bool is_command_parameter_encryption_possible = false;
   13303   bool is_response_parameter_encryption_possible = true;
   13304   std::string command_code_bytes;
   13305   rc = Serialize_TPM_CC(
   13306       command_code,
   13307       &command_code_bytes);
   13308   if (rc != TPM_RC_SUCCESS) {
   13309     return rc;
   13310   }
   13311   std::string item_handle_bytes;
   13312   rc = Serialize_TPMI_DH_OBJECT(
   13313       item_handle,
   13314       &item_handle_bytes);
   13315   if (rc != TPM_RC_SUCCESS) {
   13316     return rc;
   13317   }
   13318   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13319       crypto::SecureHash::SHA256));
   13320   hash->Update(command_code_bytes.data(),
   13321                command_code_bytes.size());
   13322   hash->Update(item_handle_name.data(),
   13323                item_handle_name.size());
   13324   handle_section_bytes += item_handle_bytes;
   13325   command_size += item_handle_bytes.size();
   13326   std::string command_hash(32, 0);
   13327   hash->Finish(string_as_array(&command_hash), command_hash.size());
   13328   std::string authorization_section_bytes;
   13329   std::string authorization_size_bytes;
   13330   if (authorization_delegate) {
   13331     if (!authorization_delegate->GetCommandAuthorization(
   13332         command_hash,
   13333         is_command_parameter_encryption_possible,
   13334         is_response_parameter_encryption_possible,
   13335         &authorization_section_bytes)) {
   13336       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13337     }
   13338     if (!authorization_section_bytes.empty()) {
   13339       tag = TPM_ST_SESSIONS;
   13340       std::string tmp;
   13341       rc = Serialize_UINT32(authorization_section_bytes.size(),
   13342                             &authorization_size_bytes);
   13343       if (rc != TPM_RC_SUCCESS) {
   13344         return rc;
   13345       }
   13346       command_size += authorization_size_bytes.size() +
   13347                       authorization_section_bytes.size();
   13348     }
   13349   }
   13350   std::string tag_bytes;
   13351   rc = Serialize_TPMI_ST_COMMAND_TAG(
   13352       tag,
   13353       &tag_bytes);
   13354   if (rc != TPM_RC_SUCCESS) {
   13355     return rc;
   13356   }
   13357   std::string command_size_bytes;
   13358   rc = Serialize_UINT32(
   13359       command_size,
   13360       &command_size_bytes);
   13361   if (rc != TPM_RC_SUCCESS) {
   13362     return rc;
   13363   }
   13364   *serialized_command = tag_bytes +
   13365                         command_size_bytes +
   13366                         command_code_bytes +
   13367                         handle_section_bytes +
   13368                         authorization_size_bytes +
   13369                         authorization_section_bytes +
   13370                         parameter_section_bytes;
   13371   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   13372   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   13373                                             serialized_command->size());
   13374   return TPM_RC_SUCCESS;
   13375 }
   13376 
   13377 TPM_RC Tpm::ParseResponse_Unseal(
   13378       const std::string& response,
   13379       TPM2B_SENSITIVE_DATA* out_data,
   13380       AuthorizationDelegate* authorization_delegate) {
   13381   VLOG(3) << __func__;
   13382   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   13383   TPM_RC rc = TPM_RC_SUCCESS;
   13384   std::string buffer(response);
   13385   TPM_ST tag;
   13386   std::string tag_bytes;
   13387   rc = Parse_TPM_ST(
   13388       &buffer,
   13389       &tag,
   13390       &tag_bytes);
   13391   if (rc != TPM_RC_SUCCESS) {
   13392     return rc;
   13393   }
   13394   UINT32 response_size;
   13395   std::string response_size_bytes;
   13396   rc = Parse_UINT32(
   13397       &buffer,
   13398       &response_size,
   13399       &response_size_bytes);
   13400   if (rc != TPM_RC_SUCCESS) {
   13401     return rc;
   13402   }
   13403   TPM_RC response_code;
   13404   std::string response_code_bytes;
   13405   rc = Parse_TPM_RC(
   13406       &buffer,
   13407       &response_code,
   13408       &response_code_bytes);
   13409   if (rc != TPM_RC_SUCCESS) {
   13410     return rc;
   13411   }
   13412   if (response_size != response.size()) {
   13413     return TPM_RC_SIZE;
   13414   }
   13415   if (response_code != TPM_RC_SUCCESS) {
   13416     return response_code;
   13417   }
   13418   TPM_CC command_code = TPM_CC_Unseal;
   13419   std::string command_code_bytes;
   13420   rc = Serialize_TPM_CC(
   13421       command_code,
   13422       &command_code_bytes);
   13423   if (rc != TPM_RC_SUCCESS) {
   13424     return rc;
   13425   }
   13426   std::string authorization_section_bytes;
   13427   if (tag == TPM_ST_SESSIONS) {
   13428     UINT32 parameter_section_size = buffer.size();
   13429     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   13430     if (rc != TPM_RC_SUCCESS) {
   13431       return rc;
   13432     }
   13433     if (parameter_section_size > buffer.size()) {
   13434       return TPM_RC_INSUFFICIENT;
   13435     }
   13436     authorization_section_bytes = buffer.substr(parameter_section_size);
   13437     // Keep the parameter section in |buffer|.
   13438     buffer.erase(parameter_section_size);
   13439   }
   13440   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13441       crypto::SecureHash::SHA256));
   13442   hash->Update(response_code_bytes.data(),
   13443                response_code_bytes.size());
   13444   hash->Update(command_code_bytes.data(),
   13445                command_code_bytes.size());
   13446   hash->Update(buffer.data(),
   13447                buffer.size());
   13448   std::string response_hash(32, 0);
   13449   hash->Finish(string_as_array(&response_hash), response_hash.size());
   13450   if (tag == TPM_ST_SESSIONS) {
   13451     CHECK(authorization_delegate) << "Authorization delegate missing!";
   13452     if (!authorization_delegate->CheckResponseAuthorization(
   13453         response_hash,
   13454         authorization_section_bytes)) {
   13455       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13456     }
   13457   }
   13458   std::string out_data_bytes;
   13459   rc = Parse_TPM2B_SENSITIVE_DATA(
   13460       &buffer,
   13461       out_data,
   13462       &out_data_bytes);
   13463   if (rc != TPM_RC_SUCCESS) {
   13464     return rc;
   13465   }
   13466   if (tag == TPM_ST_SESSIONS) {
   13467     CHECK(authorization_delegate) << "Authorization delegate missing!";
   13468     // Decrypt just the parameter data, not the size.
   13469     std::string tmp = out_data_bytes.substr(2);
   13470     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   13471       return TRUNKS_RC_ENCRYPTION_FAILED;
   13472     }
   13473     out_data_bytes.replace(2, std::string::npos, tmp);
   13474     rc = Parse_TPM2B_SENSITIVE_DATA(
   13475         &out_data_bytes,
   13476         out_data,
   13477         nullptr);
   13478     if (rc != TPM_RC_SUCCESS) {
   13479       return rc;
   13480     }
   13481   }
   13482   return TPM_RC_SUCCESS;
   13483 }
   13484 
   13485 void UnsealErrorCallback(
   13486     const Tpm::UnsealResponse& callback,
   13487     TPM_RC response_code) {
   13488   VLOG(1) << __func__;
   13489   callback.Run(response_code,
   13490                TPM2B_SENSITIVE_DATA());
   13491 }
   13492 
   13493 void UnsealResponseParser(
   13494     const Tpm::UnsealResponse& callback,
   13495     AuthorizationDelegate* authorization_delegate,
   13496     const std::string& response) {
   13497   VLOG(1) << __func__;
   13498   base::Callback<void(TPM_RC)> error_reporter =
   13499       base::Bind(UnsealErrorCallback, callback);
   13500   TPM2B_SENSITIVE_DATA out_data;
   13501   TPM_RC rc = Tpm::ParseResponse_Unseal(
   13502       response,
   13503       &out_data,
   13504       authorization_delegate);
   13505   if (rc != TPM_RC_SUCCESS) {
   13506     error_reporter.Run(rc);
   13507     return;
   13508   }
   13509   callback.Run(
   13510       rc,
   13511       out_data);
   13512 }
   13513 
   13514 void Tpm::Unseal(
   13515       const TPMI_DH_OBJECT& item_handle,
   13516       const std::string& item_handle_name,
   13517       AuthorizationDelegate* authorization_delegate,
   13518       const UnsealResponse& callback) {
   13519   VLOG(1) << __func__;
   13520   base::Callback<void(TPM_RC)> error_reporter =
   13521       base::Bind(UnsealErrorCallback, callback);
   13522   base::Callback<void(const std::string&)> parser =
   13523       base::Bind(UnsealResponseParser,
   13524                  callback,
   13525                  authorization_delegate);
   13526   std::string command;
   13527   TPM_RC rc = SerializeCommand_Unseal(
   13528       item_handle,
   13529       item_handle_name,
   13530       &command,
   13531       authorization_delegate);
   13532   if (rc != TPM_RC_SUCCESS) {
   13533     error_reporter.Run(rc);
   13534     return;
   13535   }
   13536   transceiver_->SendCommand(command, parser);
   13537 }
   13538 
   13539 TPM_RC Tpm::UnsealSync(
   13540       const TPMI_DH_OBJECT& item_handle,
   13541       const std::string& item_handle_name,
   13542       TPM2B_SENSITIVE_DATA* out_data,
   13543       AuthorizationDelegate* authorization_delegate) {
   13544   VLOG(1) << __func__;
   13545   std::string command;
   13546   TPM_RC rc = SerializeCommand_Unseal(
   13547       item_handle,
   13548       item_handle_name,
   13549       &command,
   13550       authorization_delegate);
   13551   if (rc != TPM_RC_SUCCESS) {
   13552     return rc;
   13553   }
   13554   std::string response = transceiver_->SendCommandAndWait(command);
   13555   rc = ParseResponse_Unseal(
   13556       response,
   13557       out_data,
   13558       authorization_delegate);
   13559   return rc;
   13560 }
   13561 
   13562 TPM_RC Tpm::SerializeCommand_ObjectChangeAuth(
   13563       const TPMI_DH_OBJECT& object_handle,
   13564       const std::string& object_handle_name,
   13565       const TPMI_DH_OBJECT& parent_handle,
   13566       const std::string& parent_handle_name,
   13567       const TPM2B_AUTH& new_auth,
   13568       std::string* serialized_command,
   13569       AuthorizationDelegate* authorization_delegate) {
   13570   VLOG(3) << __func__;
   13571   TPM_RC rc = TPM_RC_SUCCESS;
   13572   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   13573   UINT32 command_size = 10;  // Header size.
   13574   std::string handle_section_bytes;
   13575   std::string parameter_section_bytes;
   13576   TPM_CC command_code = TPM_CC_ObjectChangeAuth;
   13577   bool is_command_parameter_encryption_possible = true;
   13578   bool is_response_parameter_encryption_possible = true;
   13579   std::string command_code_bytes;
   13580   rc = Serialize_TPM_CC(
   13581       command_code,
   13582       &command_code_bytes);
   13583   if (rc != TPM_RC_SUCCESS) {
   13584     return rc;
   13585   }
   13586   std::string object_handle_bytes;
   13587   rc = Serialize_TPMI_DH_OBJECT(
   13588       object_handle,
   13589       &object_handle_bytes);
   13590   if (rc != TPM_RC_SUCCESS) {
   13591     return rc;
   13592   }
   13593   std::string parent_handle_bytes;
   13594   rc = Serialize_TPMI_DH_OBJECT(
   13595       parent_handle,
   13596       &parent_handle_bytes);
   13597   if (rc != TPM_RC_SUCCESS) {
   13598     return rc;
   13599   }
   13600   std::string new_auth_bytes;
   13601   rc = Serialize_TPM2B_AUTH(
   13602       new_auth,
   13603       &new_auth_bytes);
   13604   if (rc != TPM_RC_SUCCESS) {
   13605     return rc;
   13606   }
   13607   if (authorization_delegate) {
   13608     // Encrypt just the parameter data, not the size.
   13609     std::string tmp = new_auth_bytes.substr(2);
   13610     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   13611       return TRUNKS_RC_ENCRYPTION_FAILED;
   13612     }
   13613     new_auth_bytes.replace(2, std::string::npos, tmp);
   13614   }
   13615   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13616       crypto::SecureHash::SHA256));
   13617   hash->Update(command_code_bytes.data(),
   13618                command_code_bytes.size());
   13619   hash->Update(object_handle_name.data(),
   13620                object_handle_name.size());
   13621   handle_section_bytes += object_handle_bytes;
   13622   command_size += object_handle_bytes.size();
   13623   hash->Update(parent_handle_name.data(),
   13624                parent_handle_name.size());
   13625   handle_section_bytes += parent_handle_bytes;
   13626   command_size += parent_handle_bytes.size();
   13627   hash->Update(new_auth_bytes.data(),
   13628                new_auth_bytes.size());
   13629   parameter_section_bytes += new_auth_bytes;
   13630   command_size += new_auth_bytes.size();
   13631   std::string command_hash(32, 0);
   13632   hash->Finish(string_as_array(&command_hash), command_hash.size());
   13633   std::string authorization_section_bytes;
   13634   std::string authorization_size_bytes;
   13635   if (authorization_delegate) {
   13636     if (!authorization_delegate->GetCommandAuthorization(
   13637         command_hash,
   13638         is_command_parameter_encryption_possible,
   13639         is_response_parameter_encryption_possible,
   13640         &authorization_section_bytes)) {
   13641       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13642     }
   13643     if (!authorization_section_bytes.empty()) {
   13644       tag = TPM_ST_SESSIONS;
   13645       std::string tmp;
   13646       rc = Serialize_UINT32(authorization_section_bytes.size(),
   13647                             &authorization_size_bytes);
   13648       if (rc != TPM_RC_SUCCESS) {
   13649         return rc;
   13650       }
   13651       command_size += authorization_size_bytes.size() +
   13652                       authorization_section_bytes.size();
   13653     }
   13654   }
   13655   std::string tag_bytes;
   13656   rc = Serialize_TPMI_ST_COMMAND_TAG(
   13657       tag,
   13658       &tag_bytes);
   13659   if (rc != TPM_RC_SUCCESS) {
   13660     return rc;
   13661   }
   13662   std::string command_size_bytes;
   13663   rc = Serialize_UINT32(
   13664       command_size,
   13665       &command_size_bytes);
   13666   if (rc != TPM_RC_SUCCESS) {
   13667     return rc;
   13668   }
   13669   *serialized_command = tag_bytes +
   13670                         command_size_bytes +
   13671                         command_code_bytes +
   13672                         handle_section_bytes +
   13673                         authorization_size_bytes +
   13674                         authorization_section_bytes +
   13675                         parameter_section_bytes;
   13676   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   13677   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   13678                                             serialized_command->size());
   13679   return TPM_RC_SUCCESS;
   13680 }
   13681 
   13682 TPM_RC Tpm::ParseResponse_ObjectChangeAuth(
   13683       const std::string& response,
   13684       TPM2B_PRIVATE* out_private,
   13685       AuthorizationDelegate* authorization_delegate) {
   13686   VLOG(3) << __func__;
   13687   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   13688   TPM_RC rc = TPM_RC_SUCCESS;
   13689   std::string buffer(response);
   13690   TPM_ST tag;
   13691   std::string tag_bytes;
   13692   rc = Parse_TPM_ST(
   13693       &buffer,
   13694       &tag,
   13695       &tag_bytes);
   13696   if (rc != TPM_RC_SUCCESS) {
   13697     return rc;
   13698   }
   13699   UINT32 response_size;
   13700   std::string response_size_bytes;
   13701   rc = Parse_UINT32(
   13702       &buffer,
   13703       &response_size,
   13704       &response_size_bytes);
   13705   if (rc != TPM_RC_SUCCESS) {
   13706     return rc;
   13707   }
   13708   TPM_RC response_code;
   13709   std::string response_code_bytes;
   13710   rc = Parse_TPM_RC(
   13711       &buffer,
   13712       &response_code,
   13713       &response_code_bytes);
   13714   if (rc != TPM_RC_SUCCESS) {
   13715     return rc;
   13716   }
   13717   if (response_size != response.size()) {
   13718     return TPM_RC_SIZE;
   13719   }
   13720   if (response_code != TPM_RC_SUCCESS) {
   13721     return response_code;
   13722   }
   13723   TPM_CC command_code = TPM_CC_ObjectChangeAuth;
   13724   std::string command_code_bytes;
   13725   rc = Serialize_TPM_CC(
   13726       command_code,
   13727       &command_code_bytes);
   13728   if (rc != TPM_RC_SUCCESS) {
   13729     return rc;
   13730   }
   13731   std::string authorization_section_bytes;
   13732   if (tag == TPM_ST_SESSIONS) {
   13733     UINT32 parameter_section_size = buffer.size();
   13734     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   13735     if (rc != TPM_RC_SUCCESS) {
   13736       return rc;
   13737     }
   13738     if (parameter_section_size > buffer.size()) {
   13739       return TPM_RC_INSUFFICIENT;
   13740     }
   13741     authorization_section_bytes = buffer.substr(parameter_section_size);
   13742     // Keep the parameter section in |buffer|.
   13743     buffer.erase(parameter_section_size);
   13744   }
   13745   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13746       crypto::SecureHash::SHA256));
   13747   hash->Update(response_code_bytes.data(),
   13748                response_code_bytes.size());
   13749   hash->Update(command_code_bytes.data(),
   13750                command_code_bytes.size());
   13751   hash->Update(buffer.data(),
   13752                buffer.size());
   13753   std::string response_hash(32, 0);
   13754   hash->Finish(string_as_array(&response_hash), response_hash.size());
   13755   if (tag == TPM_ST_SESSIONS) {
   13756     CHECK(authorization_delegate) << "Authorization delegate missing!";
   13757     if (!authorization_delegate->CheckResponseAuthorization(
   13758         response_hash,
   13759         authorization_section_bytes)) {
   13760       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13761     }
   13762   }
   13763   std::string out_private_bytes;
   13764   rc = Parse_TPM2B_PRIVATE(
   13765       &buffer,
   13766       out_private,
   13767       &out_private_bytes);
   13768   if (rc != TPM_RC_SUCCESS) {
   13769     return rc;
   13770   }
   13771   if (tag == TPM_ST_SESSIONS) {
   13772     CHECK(authorization_delegate) << "Authorization delegate missing!";
   13773     // Decrypt just the parameter data, not the size.
   13774     std::string tmp = out_private_bytes.substr(2);
   13775     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   13776       return TRUNKS_RC_ENCRYPTION_FAILED;
   13777     }
   13778     out_private_bytes.replace(2, std::string::npos, tmp);
   13779     rc = Parse_TPM2B_PRIVATE(
   13780         &out_private_bytes,
   13781         out_private,
   13782         nullptr);
   13783     if (rc != TPM_RC_SUCCESS) {
   13784       return rc;
   13785     }
   13786   }
   13787   return TPM_RC_SUCCESS;
   13788 }
   13789 
   13790 void ObjectChangeAuthErrorCallback(
   13791     const Tpm::ObjectChangeAuthResponse& callback,
   13792     TPM_RC response_code) {
   13793   VLOG(1) << __func__;
   13794   callback.Run(response_code,
   13795                TPM2B_PRIVATE());
   13796 }
   13797 
   13798 void ObjectChangeAuthResponseParser(
   13799     const Tpm::ObjectChangeAuthResponse& callback,
   13800     AuthorizationDelegate* authorization_delegate,
   13801     const std::string& response) {
   13802   VLOG(1) << __func__;
   13803   base::Callback<void(TPM_RC)> error_reporter =
   13804       base::Bind(ObjectChangeAuthErrorCallback, callback);
   13805   TPM2B_PRIVATE out_private;
   13806   TPM_RC rc = Tpm::ParseResponse_ObjectChangeAuth(
   13807       response,
   13808       &out_private,
   13809       authorization_delegate);
   13810   if (rc != TPM_RC_SUCCESS) {
   13811     error_reporter.Run(rc);
   13812     return;
   13813   }
   13814   callback.Run(
   13815       rc,
   13816       out_private);
   13817 }
   13818 
   13819 void Tpm::ObjectChangeAuth(
   13820       const TPMI_DH_OBJECT& object_handle,
   13821       const std::string& object_handle_name,
   13822       const TPMI_DH_OBJECT& parent_handle,
   13823       const std::string& parent_handle_name,
   13824       const TPM2B_AUTH& new_auth,
   13825       AuthorizationDelegate* authorization_delegate,
   13826       const ObjectChangeAuthResponse& callback) {
   13827   VLOG(1) << __func__;
   13828   base::Callback<void(TPM_RC)> error_reporter =
   13829       base::Bind(ObjectChangeAuthErrorCallback, callback);
   13830   base::Callback<void(const std::string&)> parser =
   13831       base::Bind(ObjectChangeAuthResponseParser,
   13832                  callback,
   13833                  authorization_delegate);
   13834   std::string command;
   13835   TPM_RC rc = SerializeCommand_ObjectChangeAuth(
   13836       object_handle,
   13837       object_handle_name,
   13838       parent_handle,
   13839       parent_handle_name,
   13840       new_auth,
   13841       &command,
   13842       authorization_delegate);
   13843   if (rc != TPM_RC_SUCCESS) {
   13844     error_reporter.Run(rc);
   13845     return;
   13846   }
   13847   transceiver_->SendCommand(command, parser);
   13848 }
   13849 
   13850 TPM_RC Tpm::ObjectChangeAuthSync(
   13851       const TPMI_DH_OBJECT& object_handle,
   13852       const std::string& object_handle_name,
   13853       const TPMI_DH_OBJECT& parent_handle,
   13854       const std::string& parent_handle_name,
   13855       const TPM2B_AUTH& new_auth,
   13856       TPM2B_PRIVATE* out_private,
   13857       AuthorizationDelegate* authorization_delegate) {
   13858   VLOG(1) << __func__;
   13859   std::string command;
   13860   TPM_RC rc = SerializeCommand_ObjectChangeAuth(
   13861       object_handle,
   13862       object_handle_name,
   13863       parent_handle,
   13864       parent_handle_name,
   13865       new_auth,
   13866       &command,
   13867       authorization_delegate);
   13868   if (rc != TPM_RC_SUCCESS) {
   13869     return rc;
   13870   }
   13871   std::string response = transceiver_->SendCommandAndWait(command);
   13872   rc = ParseResponse_ObjectChangeAuth(
   13873       response,
   13874       out_private,
   13875       authorization_delegate);
   13876   return rc;
   13877 }
   13878 
   13879 TPM_RC Tpm::SerializeCommand_Duplicate(
   13880       const TPMI_DH_OBJECT& object_handle,
   13881       const std::string& object_handle_name,
   13882       const TPMI_DH_OBJECT& new_parent_handle,
   13883       const std::string& new_parent_handle_name,
   13884       const TPM2B_DATA& encryption_key_in,
   13885       const TPMT_SYM_DEF_OBJECT& symmetric_alg,
   13886       std::string* serialized_command,
   13887       AuthorizationDelegate* authorization_delegate) {
   13888   VLOG(3) << __func__;
   13889   TPM_RC rc = TPM_RC_SUCCESS;
   13890   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   13891   UINT32 command_size = 10;  // Header size.
   13892   std::string handle_section_bytes;
   13893   std::string parameter_section_bytes;
   13894   TPM_CC command_code = TPM_CC_Duplicate;
   13895   bool is_command_parameter_encryption_possible = true;
   13896   bool is_response_parameter_encryption_possible = true;
   13897   std::string command_code_bytes;
   13898   rc = Serialize_TPM_CC(
   13899       command_code,
   13900       &command_code_bytes);
   13901   if (rc != TPM_RC_SUCCESS) {
   13902     return rc;
   13903   }
   13904   std::string object_handle_bytes;
   13905   rc = Serialize_TPMI_DH_OBJECT(
   13906       object_handle,
   13907       &object_handle_bytes);
   13908   if (rc != TPM_RC_SUCCESS) {
   13909     return rc;
   13910   }
   13911   std::string new_parent_handle_bytes;
   13912   rc = Serialize_TPMI_DH_OBJECT(
   13913       new_parent_handle,
   13914       &new_parent_handle_bytes);
   13915   if (rc != TPM_RC_SUCCESS) {
   13916     return rc;
   13917   }
   13918   std::string encryption_key_in_bytes;
   13919   rc = Serialize_TPM2B_DATA(
   13920       encryption_key_in,
   13921       &encryption_key_in_bytes);
   13922   if (rc != TPM_RC_SUCCESS) {
   13923     return rc;
   13924   }
   13925   std::string symmetric_alg_bytes;
   13926   rc = Serialize_TPMT_SYM_DEF_OBJECT(
   13927       symmetric_alg,
   13928       &symmetric_alg_bytes);
   13929   if (rc != TPM_RC_SUCCESS) {
   13930     return rc;
   13931   }
   13932   if (authorization_delegate) {
   13933     // Encrypt just the parameter data, not the size.
   13934     std::string tmp = encryption_key_in_bytes.substr(2);
   13935     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   13936       return TRUNKS_RC_ENCRYPTION_FAILED;
   13937     }
   13938     encryption_key_in_bytes.replace(2, std::string::npos, tmp);
   13939   }
   13940   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   13941       crypto::SecureHash::SHA256));
   13942   hash->Update(command_code_bytes.data(),
   13943                command_code_bytes.size());
   13944   hash->Update(object_handle_name.data(),
   13945                object_handle_name.size());
   13946   handle_section_bytes += object_handle_bytes;
   13947   command_size += object_handle_bytes.size();
   13948   hash->Update(new_parent_handle_name.data(),
   13949                new_parent_handle_name.size());
   13950   handle_section_bytes += new_parent_handle_bytes;
   13951   command_size += new_parent_handle_bytes.size();
   13952   hash->Update(encryption_key_in_bytes.data(),
   13953                encryption_key_in_bytes.size());
   13954   parameter_section_bytes += encryption_key_in_bytes;
   13955   command_size += encryption_key_in_bytes.size();
   13956   hash->Update(symmetric_alg_bytes.data(),
   13957                symmetric_alg_bytes.size());
   13958   parameter_section_bytes += symmetric_alg_bytes;
   13959   command_size += symmetric_alg_bytes.size();
   13960   std::string command_hash(32, 0);
   13961   hash->Finish(string_as_array(&command_hash), command_hash.size());
   13962   std::string authorization_section_bytes;
   13963   std::string authorization_size_bytes;
   13964   if (authorization_delegate) {
   13965     if (!authorization_delegate->GetCommandAuthorization(
   13966         command_hash,
   13967         is_command_parameter_encryption_possible,
   13968         is_response_parameter_encryption_possible,
   13969         &authorization_section_bytes)) {
   13970       return TRUNKS_RC_AUTHORIZATION_FAILED;
   13971     }
   13972     if (!authorization_section_bytes.empty()) {
   13973       tag = TPM_ST_SESSIONS;
   13974       std::string tmp;
   13975       rc = Serialize_UINT32(authorization_section_bytes.size(),
   13976                             &authorization_size_bytes);
   13977       if (rc != TPM_RC_SUCCESS) {
   13978         return rc;
   13979       }
   13980       command_size += authorization_size_bytes.size() +
   13981                       authorization_section_bytes.size();
   13982     }
   13983   }
   13984   std::string tag_bytes;
   13985   rc = Serialize_TPMI_ST_COMMAND_TAG(
   13986       tag,
   13987       &tag_bytes);
   13988   if (rc != TPM_RC_SUCCESS) {
   13989     return rc;
   13990   }
   13991   std::string command_size_bytes;
   13992   rc = Serialize_UINT32(
   13993       command_size,
   13994       &command_size_bytes);
   13995   if (rc != TPM_RC_SUCCESS) {
   13996     return rc;
   13997   }
   13998   *serialized_command = tag_bytes +
   13999                         command_size_bytes +
   14000                         command_code_bytes +
   14001                         handle_section_bytes +
   14002                         authorization_size_bytes +
   14003                         authorization_section_bytes +
   14004                         parameter_section_bytes;
   14005   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   14006   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   14007                                             serialized_command->size());
   14008   return TPM_RC_SUCCESS;
   14009 }
   14010 
   14011 TPM_RC Tpm::ParseResponse_Duplicate(
   14012       const std::string& response,
   14013       TPM2B_DATA* encryption_key_out,
   14014       TPM2B_PRIVATE* duplicate,
   14015       TPM2B_ENCRYPTED_SECRET* out_sym_seed,
   14016       AuthorizationDelegate* authorization_delegate) {
   14017   VLOG(3) << __func__;
   14018   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   14019   TPM_RC rc = TPM_RC_SUCCESS;
   14020   std::string buffer(response);
   14021   TPM_ST tag;
   14022   std::string tag_bytes;
   14023   rc = Parse_TPM_ST(
   14024       &buffer,
   14025       &tag,
   14026       &tag_bytes);
   14027   if (rc != TPM_RC_SUCCESS) {
   14028     return rc;
   14029   }
   14030   UINT32 response_size;
   14031   std::string response_size_bytes;
   14032   rc = Parse_UINT32(
   14033       &buffer,
   14034       &response_size,
   14035       &response_size_bytes);
   14036   if (rc != TPM_RC_SUCCESS) {
   14037     return rc;
   14038   }
   14039   TPM_RC response_code;
   14040   std::string response_code_bytes;
   14041   rc = Parse_TPM_RC(
   14042       &buffer,
   14043       &response_code,
   14044       &response_code_bytes);
   14045   if (rc != TPM_RC_SUCCESS) {
   14046     return rc;
   14047   }
   14048   if (response_size != response.size()) {
   14049     return TPM_RC_SIZE;
   14050   }
   14051   if (response_code != TPM_RC_SUCCESS) {
   14052     return response_code;
   14053   }
   14054   TPM_CC command_code = TPM_CC_Duplicate;
   14055   std::string command_code_bytes;
   14056   rc = Serialize_TPM_CC(
   14057       command_code,
   14058       &command_code_bytes);
   14059   if (rc != TPM_RC_SUCCESS) {
   14060     return rc;
   14061   }
   14062   std::string authorization_section_bytes;
   14063   if (tag == TPM_ST_SESSIONS) {
   14064     UINT32 parameter_section_size = buffer.size();
   14065     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   14066     if (rc != TPM_RC_SUCCESS) {
   14067       return rc;
   14068     }
   14069     if (parameter_section_size > buffer.size()) {
   14070       return TPM_RC_INSUFFICIENT;
   14071     }
   14072     authorization_section_bytes = buffer.substr(parameter_section_size);
   14073     // Keep the parameter section in |buffer|.
   14074     buffer.erase(parameter_section_size);
   14075   }
   14076   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   14077       crypto::SecureHash::SHA256));
   14078   hash->Update(response_code_bytes.data(),
   14079                response_code_bytes.size());
   14080   hash->Update(command_code_bytes.data(),
   14081                command_code_bytes.size());
   14082   hash->Update(buffer.data(),
   14083                buffer.size());
   14084   std::string response_hash(32, 0);
   14085   hash->Finish(string_as_array(&response_hash), response_hash.size());
   14086   if (tag == TPM_ST_SESSIONS) {
   14087     CHECK(authorization_delegate) << "Authorization delegate missing!";
   14088     if (!authorization_delegate->CheckResponseAuthorization(
   14089         response_hash,
   14090         authorization_section_bytes)) {
   14091       return TRUNKS_RC_AUTHORIZATION_FAILED;
   14092     }
   14093   }
   14094   std::string encryption_key_out_bytes;
   14095   rc = Parse_TPM2B_DATA(
   14096       &buffer,
   14097       encryption_key_out,
   14098       &encryption_key_out_bytes);
   14099   if (rc != TPM_RC_SUCCESS) {
   14100     return rc;
   14101   }
   14102   std::string duplicate_bytes;
   14103   rc = Parse_TPM2B_PRIVATE(
   14104       &buffer,
   14105       duplicate,
   14106       &duplicate_bytes);
   14107   if (rc != TPM_RC_SUCCESS) {
   14108     return rc;
   14109   }
   14110   std::string out_sym_seed_bytes;
   14111   rc = Parse_TPM2B_ENCRYPTED_SECRET(
   14112       &buffer,
   14113       out_sym_seed,
   14114       &out_sym_seed_bytes);
   14115   if (rc != TPM_RC_SUCCESS) {
   14116     return rc;
   14117   }
   14118   if (tag == TPM_ST_SESSIONS) {
   14119     CHECK(authorization_delegate) << "Authorization delegate missing!";
   14120     // Decrypt just the parameter data, not the size.
   14121     std::string tmp = encryption_key_out_bytes.substr(2);
   14122     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   14123       return TRUNKS_RC_ENCRYPTION_FAILED;
   14124     }
   14125     encryption_key_out_bytes.replace(2, std::string::npos, tmp);
   14126     rc = Parse_TPM2B_DATA(
   14127         &encryption_key_out_bytes,
   14128         encryption_key_out,
   14129         nullptr);
   14130     if (rc != TPM_RC_SUCCESS) {
   14131       return rc;
   14132     }
   14133   }
   14134   return TPM_RC_SUCCESS;
   14135 }
   14136 
   14137 void DuplicateErrorCallback(
   14138     const Tpm::DuplicateResponse& callback,
   14139     TPM_RC response_code) {
   14140   VLOG(1) << __func__;
   14141   callback.Run(response_code,
   14142                TPM2B_DATA(),
   14143                TPM2B_PRIVATE(),
   14144                TPM2B_ENCRYPTED_SECRET());
   14145 }
   14146 
   14147 void DuplicateResponseParser(
   14148     const Tpm::DuplicateResponse& callback,
   14149     AuthorizationDelegate* authorization_delegate,
   14150     const std::string& response) {
   14151   VLOG(1) << __func__;
   14152   base::Callback<void(TPM_RC)> error_reporter =
   14153       base::Bind(DuplicateErrorCallback, callback);
   14154   TPM2B_DATA encryption_key_out;
   14155   TPM2B_PRIVATE duplicate;
   14156   TPM2B_ENCRYPTED_SECRET out_sym_seed;
   14157   TPM_RC rc = Tpm::ParseResponse_Duplicate(
   14158       response,
   14159       &encryption_key_out,
   14160       &duplicate,
   14161       &out_sym_seed,
   14162       authorization_delegate);
   14163   if (rc != TPM_RC_SUCCESS) {
   14164     error_reporter.Run(rc);
   14165     return;
   14166   }
   14167   callback.Run(
   14168       rc,
   14169       encryption_key_out,
   14170       duplicate,
   14171       out_sym_seed);
   14172 }
   14173 
   14174 void Tpm::Duplicate(
   14175       const TPMI_DH_OBJECT& object_handle,
   14176       const std::string& object_handle_name,
   14177       const TPMI_DH_OBJECT& new_parent_handle,
   14178       const std::string& new_parent_handle_name,
   14179       const TPM2B_DATA& encryption_key_in,
   14180       const TPMT_SYM_DEF_OBJECT& symmetric_alg,
   14181       AuthorizationDelegate* authorization_delegate,
   14182       const DuplicateResponse& callback) {
   14183   VLOG(1) << __func__;
   14184   base::Callback<void(TPM_RC)> error_reporter =
   14185       base::Bind(DuplicateErrorCallback, callback);
   14186   base::Callback<void(const std::string&)> parser =
   14187       base::Bind(DuplicateResponseParser,
   14188                  callback,
   14189                  authorization_delegate);
   14190   std::string command;
   14191   TPM_RC rc = SerializeCommand_Duplicate(
   14192       object_handle,
   14193       object_handle_name,
   14194       new_parent_handle,
   14195       new_parent_handle_name,
   14196       encryption_key_in,
   14197       symmetric_alg,
   14198       &command,
   14199       authorization_delegate);
   14200   if (rc != TPM_RC_SUCCESS) {
   14201     error_reporter.Run(rc);
   14202     return;
   14203   }
   14204   transceiver_->SendCommand(command, parser);
   14205 }
   14206 
   14207 TPM_RC Tpm::DuplicateSync(
   14208       const TPMI_DH_OBJECT& object_handle,
   14209       const std::string& object_handle_name,
   14210       const TPMI_DH_OBJECT& new_parent_handle,
   14211       const std::string& new_parent_handle_name,
   14212       const TPM2B_DATA& encryption_key_in,
   14213       const TPMT_SYM_DEF_OBJECT& symmetric_alg,
   14214       TPM2B_DATA* encryption_key_out,
   14215       TPM2B_PRIVATE* duplicate,
   14216       TPM2B_ENCRYPTED_SECRET* out_sym_seed,
   14217       AuthorizationDelegate* authorization_delegate) {
   14218   VLOG(1) << __func__;
   14219   std::string command;
   14220   TPM_RC rc = SerializeCommand_Duplicate(
   14221       object_handle,
   14222       object_handle_name,
   14223       new_parent_handle,
   14224       new_parent_handle_name,
   14225       encryption_key_in,
   14226       symmetric_alg,
   14227       &command,
   14228       authorization_delegate);
   14229   if (rc != TPM_RC_SUCCESS) {
   14230     return rc;
   14231   }
   14232   std::string response = transceiver_->SendCommandAndWait(command);
   14233   rc = ParseResponse_Duplicate(
   14234       response,
   14235       encryption_key_out,
   14236       duplicate,
   14237       out_sym_seed,
   14238       authorization_delegate);
   14239   return rc;
   14240 }
   14241 
   14242 TPM_RC Tpm::SerializeCommand_Rewrap(
   14243       const TPMI_DH_OBJECT& old_parent,
   14244       const std::string& old_parent_name,
   14245       const TPMI_DH_OBJECT& new_parent,
   14246       const std::string& new_parent_name,
   14247       const TPM2B_PRIVATE& in_duplicate,
   14248       const TPM2B_NAME& name,
   14249       const TPM2B_ENCRYPTED_SECRET& in_sym_seed,
   14250       std::string* serialized_command,
   14251       AuthorizationDelegate* authorization_delegate) {
   14252   VLOG(3) << __func__;
   14253   TPM_RC rc = TPM_RC_SUCCESS;
   14254   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   14255   UINT32 command_size = 10;  // Header size.
   14256   std::string handle_section_bytes;
   14257   std::string parameter_section_bytes;
   14258   TPM_CC command_code = TPM_CC_Rewrap;
   14259   bool is_command_parameter_encryption_possible = true;
   14260   bool is_response_parameter_encryption_possible = true;
   14261   std::string command_code_bytes;
   14262   rc = Serialize_TPM_CC(
   14263       command_code,
   14264       &command_code_bytes);
   14265   if (rc != TPM_RC_SUCCESS) {
   14266     return rc;
   14267   }
   14268   std::string old_parent_bytes;
   14269   rc = Serialize_TPMI_DH_OBJECT(
   14270       old_parent,
   14271       &old_parent_bytes);
   14272   if (rc != TPM_RC_SUCCESS) {
   14273     return rc;
   14274   }
   14275   std::string new_parent_bytes;
   14276   rc = Serialize_TPMI_DH_OBJECT(
   14277       new_parent,
   14278       &new_parent_bytes);
   14279   if (rc != TPM_RC_SUCCESS) {
   14280     return rc;
   14281   }
   14282   std::string in_duplicate_bytes;
   14283   rc = Serialize_TPM2B_PRIVATE(
   14284       in_duplicate,
   14285       &in_duplicate_bytes);
   14286   if (rc != TPM_RC_SUCCESS) {
   14287     return rc;
   14288   }
   14289   std::string name_bytes;
   14290   rc = Serialize_TPM2B_NAME(
   14291       name,
   14292       &name_bytes);
   14293   if (rc != TPM_RC_SUCCESS) {
   14294     return rc;
   14295   }
   14296   std::string in_sym_seed_bytes;
   14297   rc = Serialize_TPM2B_ENCRYPTED_SECRET(
   14298       in_sym_seed,
   14299       &in_sym_seed_bytes);
   14300   if (rc != TPM_RC_SUCCESS) {
   14301     return rc;
   14302   }
   14303   if (authorization_delegate) {
   14304     // Encrypt just the parameter data, not the size.
   14305     std::string tmp = in_duplicate_bytes.substr(2);
   14306     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   14307       return TRUNKS_RC_ENCRYPTION_FAILED;
   14308     }
   14309     in_duplicate_bytes.replace(2, std::string::npos, tmp);
   14310   }
   14311   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   14312       crypto::SecureHash::SHA256));
   14313   hash->Update(command_code_bytes.data(),
   14314                command_code_bytes.size());
   14315   hash->Update(old_parent_name.data(),
   14316                old_parent_name.size());
   14317   handle_section_bytes += old_parent_bytes;
   14318   command_size += old_parent_bytes.size();
   14319   hash->Update(new_parent_name.data(),
   14320                new_parent_name.size());
   14321   handle_section_bytes += new_parent_bytes;
   14322   command_size += new_parent_bytes.size();
   14323   hash->Update(in_duplicate_bytes.data(),
   14324                in_duplicate_bytes.size());
   14325   parameter_section_bytes += in_duplicate_bytes;
   14326   command_size += in_duplicate_bytes.size();
   14327   hash->Update(name_bytes.data(),
   14328                name_bytes.size());
   14329   parameter_section_bytes += name_bytes;
   14330   command_size += name_bytes.size();
   14331   hash->Update(in_sym_seed_bytes.data(),
   14332                in_sym_seed_bytes.size());
   14333   parameter_section_bytes += in_sym_seed_bytes;
   14334   command_size += in_sym_seed_bytes.size();
   14335   std::string command_hash(32, 0);
   14336   hash->Finish(string_as_array(&command_hash), command_hash.size());
   14337   std::string authorization_section_bytes;
   14338   std::string authorization_size_bytes;
   14339   if (authorization_delegate) {
   14340     if (!authorization_delegate->GetCommandAuthorization(
   14341         command_hash,
   14342         is_command_parameter_encryption_possible,
   14343         is_response_parameter_encryption_possible,
   14344         &authorization_section_bytes)) {
   14345       return TRUNKS_RC_AUTHORIZATION_FAILED;
   14346     }
   14347     if (!authorization_section_bytes.empty()) {
   14348       tag = TPM_ST_SESSIONS;
   14349       std::string tmp;
   14350       rc = Serialize_UINT32(authorization_section_bytes.size(),
   14351                             &authorization_size_bytes);
   14352       if (rc != TPM_RC_SUCCESS) {
   14353         return rc;
   14354       }
   14355       command_size += authorization_size_bytes.size() +
   14356                       authorization_section_bytes.size();
   14357     }
   14358   }
   14359   std::string tag_bytes;
   14360   rc = Serialize_TPMI_ST_COMMAND_TAG(
   14361       tag,
   14362       &tag_bytes);
   14363   if (rc != TPM_RC_SUCCESS) {
   14364     return rc;
   14365   }
   14366   std::string command_size_bytes;
   14367   rc = Serialize_UINT32(
   14368       command_size,
   14369       &command_size_bytes);
   14370   if (rc != TPM_RC_SUCCESS) {
   14371     return rc;
   14372   }
   14373   *serialized_command = tag_bytes +
   14374                         command_size_bytes +
   14375                         command_code_bytes +
   14376                         handle_section_bytes +
   14377                         authorization_size_bytes +
   14378                         authorization_section_bytes +
   14379                         parameter_section_bytes;
   14380   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   14381   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   14382                                             serialized_command->size());
   14383   return TPM_RC_SUCCESS;
   14384 }
   14385 
   14386 TPM_RC Tpm::ParseResponse_Rewrap(
   14387       const std::string& response,
   14388       TPM2B_PRIVATE* out_duplicate,
   14389       TPM2B_ENCRYPTED_SECRET* out_sym_seed,
   14390       AuthorizationDelegate* authorization_delegate) {
   14391   VLOG(3) << __func__;
   14392   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   14393   TPM_RC rc = TPM_RC_SUCCESS;
   14394   std::string buffer(response);
   14395   TPM_ST tag;
   14396   std::string tag_bytes;
   14397   rc = Parse_TPM_ST(
   14398       &buffer,
   14399       &tag,
   14400       &tag_bytes);
   14401   if (rc != TPM_RC_SUCCESS) {
   14402     return rc;
   14403   }
   14404   UINT32 response_size;
   14405   std::string response_size_bytes;
   14406   rc = Parse_UINT32(
   14407       &buffer,
   14408       &response_size,
   14409       &response_size_bytes);
   14410   if (rc != TPM_RC_SUCCESS) {
   14411     return rc;
   14412   }
   14413   TPM_RC response_code;
   14414   std::string response_code_bytes;
   14415   rc = Parse_TPM_RC(
   14416       &buffer,
   14417       &response_code,
   14418       &response_code_bytes);
   14419   if (rc != TPM_RC_SUCCESS) {
   14420     return rc;
   14421   }
   14422   if (response_size != response.size()) {
   14423     return TPM_RC_SIZE;
   14424   }
   14425   if (response_code != TPM_RC_SUCCESS) {
   14426     return response_code;
   14427   }
   14428   TPM_CC command_code = TPM_CC_Rewrap;
   14429   std::string command_code_bytes;
   14430   rc = Serialize_TPM_CC(
   14431       command_code,
   14432       &command_code_bytes);
   14433   if (rc != TPM_RC_SUCCESS) {
   14434     return rc;
   14435   }
   14436   std::string authorization_section_bytes;
   14437   if (tag == TPM_ST_SESSIONS) {
   14438     UINT32 parameter_section_size = buffer.size();
   14439     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   14440     if (rc != TPM_RC_SUCCESS) {
   14441       return rc;
   14442     }
   14443     if (parameter_section_size > buffer.size()) {
   14444       return TPM_RC_INSUFFICIENT;
   14445     }
   14446     authorization_section_bytes = buffer.substr(parameter_section_size);
   14447     // Keep the parameter section in |buffer|.
   14448     buffer.erase(parameter_section_size);
   14449   }
   14450   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   14451       crypto::SecureHash::SHA256));
   14452   hash->Update(response_code_bytes.data(),
   14453                response_code_bytes.size());
   14454   hash->Update(command_code_bytes.data(),
   14455                command_code_bytes.size());
   14456   hash->Update(buffer.data(),
   14457                buffer.size());
   14458   std::string response_hash(32, 0);
   14459   hash->Finish(string_as_array(&response_hash), response_hash.size());
   14460   if (tag == TPM_ST_SESSIONS) {
   14461     CHECK(authorization_delegate) << "Authorization delegate missing!";
   14462     if (!authorization_delegate->CheckResponseAuthorization(
   14463         response_hash,
   14464         authorization_section_bytes)) {
   14465       return TRUNKS_RC_AUTHORIZATION_FAILED;
   14466     }
   14467   }
   14468   std::string out_duplicate_bytes;
   14469   rc = Parse_TPM2B_PRIVATE(
   14470       &buffer,
   14471       out_duplicate,
   14472       &out_duplicate_bytes);
   14473   if (rc != TPM_RC_SUCCESS) {
   14474     return rc;
   14475   }
   14476   std::string out_sym_seed_bytes;
   14477   rc = Parse_TPM2B_ENCRYPTED_SECRET(
   14478       &buffer,
   14479       out_sym_seed,
   14480       &out_sym_seed_bytes);
   14481   if (rc != TPM_RC_SUCCESS) {
   14482     return rc;
   14483   }
   14484   if (tag == TPM_ST_SESSIONS) {
   14485     CHECK(authorization_delegate) << "Authorization delegate missing!";
   14486     // Decrypt just the parameter data, not the size.
   14487     std::string tmp = out_duplicate_bytes.substr(2);
   14488     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   14489       return TRUNKS_RC_ENCRYPTION_FAILED;
   14490     }
   14491     out_duplicate_bytes.replace(2, std::string::npos, tmp);
   14492     rc = Parse_TPM2B_PRIVATE(
   14493         &out_duplicate_bytes,
   14494         out_duplicate,
   14495         nullptr);
   14496     if (rc != TPM_RC_SUCCESS) {
   14497       return rc;
   14498     }
   14499   }
   14500   return TPM_RC_SUCCESS;
   14501 }
   14502 
   14503 void RewrapErrorCallback(
   14504     const Tpm::RewrapResponse& callback,
   14505     TPM_RC response_code) {
   14506   VLOG(1) << __func__;
   14507   callback.Run(response_code,
   14508                TPM2B_PRIVATE(),
   14509                TPM2B_ENCRYPTED_SECRET());
   14510 }
   14511 
   14512 void RewrapResponseParser(
   14513     const Tpm::RewrapResponse& callback,
   14514     AuthorizationDelegate* authorization_delegate,
   14515     const std::string& response) {
   14516   VLOG(1) << __func__;
   14517   base::Callback<void(TPM_RC)> error_reporter =
   14518       base::Bind(RewrapErrorCallback, callback);
   14519   TPM2B_PRIVATE out_duplicate;
   14520   TPM2B_ENCRYPTED_SECRET out_sym_seed;
   14521   TPM_RC rc = Tpm::ParseResponse_Rewrap(
   14522       response,
   14523       &out_duplicate,
   14524       &out_sym_seed,
   14525       authorization_delegate);
   14526   if (rc != TPM_RC_SUCCESS) {
   14527     error_reporter.Run(rc);
   14528     return;
   14529   }
   14530   callback.Run(
   14531       rc,
   14532       out_duplicate,
   14533       out_sym_seed);
   14534 }
   14535 
   14536 void Tpm::Rewrap(
   14537       const TPMI_DH_OBJECT& old_parent,
   14538       const std::string& old_parent_name,
   14539       const TPMI_DH_OBJECT& new_parent,
   14540       const std::string& new_parent_name,
   14541       const TPM2B_PRIVATE& in_duplicate,
   14542       const TPM2B_NAME& name,
   14543       const TPM2B_ENCRYPTED_SECRET& in_sym_seed,
   14544       AuthorizationDelegate* authorization_delegate,
   14545       const RewrapResponse& callback) {
   14546   VLOG(1) << __func__;
   14547   base::Callback<void(TPM_RC)> error_reporter =
   14548       base::Bind(RewrapErrorCallback, callback);
   14549   base::Callback<void(const std::string&)> parser =
   14550       base::Bind(RewrapResponseParser,
   14551                  callback,
   14552                  authorization_delegate);
   14553   std::string command;
   14554   TPM_RC rc = SerializeCommand_Rewrap(
   14555       old_parent,
   14556       old_parent_name,
   14557       new_parent,
   14558       new_parent_name,
   14559       in_duplicate,
   14560       name,
   14561       in_sym_seed,
   14562       &command,
   14563       authorization_delegate);
   14564   if (rc != TPM_RC_SUCCESS) {
   14565     error_reporter.Run(rc);
   14566     return;
   14567   }
   14568   transceiver_->SendCommand(command, parser);
   14569 }
   14570 
   14571 TPM_RC Tpm::RewrapSync(
   14572       const TPMI_DH_OBJECT& old_parent,
   14573       const std::string& old_parent_name,
   14574       const TPMI_DH_OBJECT& new_parent,
   14575       const std::string& new_parent_name,
   14576       const TPM2B_PRIVATE& in_duplicate,
   14577       const TPM2B_NAME& name,
   14578       const TPM2B_ENCRYPTED_SECRET& in_sym_seed,
   14579       TPM2B_PRIVATE* out_duplicate,
   14580       TPM2B_ENCRYPTED_SECRET* out_sym_seed,
   14581       AuthorizationDelegate* authorization_delegate) {
   14582   VLOG(1) << __func__;
   14583   std::string command;
   14584   TPM_RC rc = SerializeCommand_Rewrap(
   14585       old_parent,
   14586       old_parent_name,
   14587       new_parent,
   14588       new_parent_name,
   14589       in_duplicate,
   14590       name,
   14591       in_sym_seed,
   14592       &command,
   14593       authorization_delegate);
   14594   if (rc != TPM_RC_SUCCESS) {
   14595     return rc;
   14596   }
   14597   std::string response = transceiver_->SendCommandAndWait(command);
   14598   rc = ParseResponse_Rewrap(
   14599       response,
   14600       out_duplicate,
   14601       out_sym_seed,
   14602       authorization_delegate);
   14603   return rc;
   14604 }
   14605 
   14606 TPM_RC Tpm::SerializeCommand_Import(
   14607       const TPMI_DH_OBJECT& parent_handle,
   14608       const std::string& parent_handle_name,
   14609       const TPM2B_DATA& encryption_key,
   14610       const TPM2B_PUBLIC& object_public,
   14611       const TPM2B_PRIVATE& duplicate,
   14612       const TPM2B_ENCRYPTED_SECRET& in_sym_seed,
   14613       const TPMT_SYM_DEF_OBJECT& symmetric_alg,
   14614       std::string* serialized_command,
   14615       AuthorizationDelegate* authorization_delegate) {
   14616   VLOG(3) << __func__;
   14617   TPM_RC rc = TPM_RC_SUCCESS;
   14618   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   14619   UINT32 command_size = 10;  // Header size.
   14620   std::string handle_section_bytes;
   14621   std::string parameter_section_bytes;
   14622   TPM_CC command_code = TPM_CC_Import;
   14623   bool is_command_parameter_encryption_possible = true;
   14624   bool is_response_parameter_encryption_possible = true;
   14625   std::string command_code_bytes;
   14626   rc = Serialize_TPM_CC(
   14627       command_code,
   14628       &command_code_bytes);
   14629   if (rc != TPM_RC_SUCCESS) {
   14630     return rc;
   14631   }
   14632   std::string parent_handle_bytes;
   14633   rc = Serialize_TPMI_DH_OBJECT(
   14634       parent_handle,
   14635       &parent_handle_bytes);
   14636   if (rc != TPM_RC_SUCCESS) {
   14637     return rc;
   14638   }
   14639   std::string encryption_key_bytes;
   14640   rc = Serialize_TPM2B_DATA(
   14641       encryption_key,
   14642       &encryption_key_bytes);
   14643   if (rc != TPM_RC_SUCCESS) {
   14644     return rc;
   14645   }
   14646   std::string object_public_bytes;
   14647   rc = Serialize_TPM2B_PUBLIC(
   14648       object_public,
   14649       &object_public_bytes);
   14650   if (rc != TPM_RC_SUCCESS) {
   14651     return rc;
   14652   }
   14653   std::string duplicate_bytes;
   14654   rc = Serialize_TPM2B_PRIVATE(
   14655       duplicate,
   14656       &duplicate_bytes);
   14657   if (rc != TPM_RC_SUCCESS) {
   14658     return rc;
   14659   }
   14660   std::string in_sym_seed_bytes;
   14661   rc = Serialize_TPM2B_ENCRYPTED_SECRET(
   14662       in_sym_seed,
   14663       &in_sym_seed_bytes);
   14664   if (rc != TPM_RC_SUCCESS) {
   14665     return rc;
   14666   }
   14667   std::string symmetric_alg_bytes;
   14668   rc = Serialize_TPMT_SYM_DEF_OBJECT(
   14669       symmetric_alg,
   14670       &symmetric_alg_bytes);
   14671   if (rc != TPM_RC_SUCCESS) {
   14672     return rc;
   14673   }
   14674   if (authorization_delegate) {
   14675     // Encrypt just the parameter data, not the size.
   14676     std::string tmp = encryption_key_bytes.substr(2);
   14677     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   14678       return TRUNKS_RC_ENCRYPTION_FAILED;
   14679     }
   14680     encryption_key_bytes.replace(2, std::string::npos, tmp);
   14681   }
   14682   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   14683       crypto::SecureHash::SHA256));
   14684   hash->Update(command_code_bytes.data(),
   14685                command_code_bytes.size());
   14686   hash->Update(parent_handle_name.data(),
   14687                parent_handle_name.size());
   14688   handle_section_bytes += parent_handle_bytes;
   14689   command_size += parent_handle_bytes.size();
   14690   hash->Update(encryption_key_bytes.data(),
   14691                encryption_key_bytes.size());
   14692   parameter_section_bytes += encryption_key_bytes;
   14693   command_size += encryption_key_bytes.size();
   14694   hash->Update(object_public_bytes.data(),
   14695                object_public_bytes.size());
   14696   parameter_section_bytes += object_public_bytes;
   14697   command_size += object_public_bytes.size();
   14698   hash->Update(duplicate_bytes.data(),
   14699                duplicate_bytes.size());
   14700   parameter_section_bytes += duplicate_bytes;
   14701   command_size += duplicate_bytes.size();
   14702   hash->Update(in_sym_seed_bytes.data(),
   14703                in_sym_seed_bytes.size());
   14704   parameter_section_bytes += in_sym_seed_bytes;
   14705   command_size += in_sym_seed_bytes.size();
   14706   hash->Update(symmetric_alg_bytes.data(),
   14707                symmetric_alg_bytes.size());
   14708   parameter_section_bytes += symmetric_alg_bytes;
   14709   command_size += symmetric_alg_bytes.size();
   14710   std::string command_hash(32, 0);
   14711   hash->Finish(string_as_array(&command_hash), command_hash.size());
   14712   std::string authorization_section_bytes;
   14713   std::string authorization_size_bytes;
   14714   if (authorization_delegate) {
   14715     if (!authorization_delegate->GetCommandAuthorization(
   14716         command_hash,
   14717         is_command_parameter_encryption_possible,
   14718         is_response_parameter_encryption_possible,
   14719         &authorization_section_bytes)) {
   14720       return TRUNKS_RC_AUTHORIZATION_FAILED;
   14721     }
   14722     if (!authorization_section_bytes.empty()) {
   14723       tag = TPM_ST_SESSIONS;
   14724       std::string tmp;
   14725       rc = Serialize_UINT32(authorization_section_bytes.size(),
   14726                             &authorization_size_bytes);
   14727       if (rc != TPM_RC_SUCCESS) {
   14728         return rc;
   14729       }
   14730       command_size += authorization_size_bytes.size() +
   14731                       authorization_section_bytes.size();
   14732     }
   14733   }
   14734   std::string tag_bytes;
   14735   rc = Serialize_TPMI_ST_COMMAND_TAG(
   14736       tag,
   14737       &tag_bytes);
   14738   if (rc != TPM_RC_SUCCESS) {
   14739     return rc;
   14740   }
   14741   std::string command_size_bytes;
   14742   rc = Serialize_UINT32(
   14743       command_size,
   14744       &command_size_bytes);
   14745   if (rc != TPM_RC_SUCCESS) {
   14746     return rc;
   14747   }
   14748   *serialized_command = tag_bytes +
   14749                         command_size_bytes +
   14750                         command_code_bytes +
   14751                         handle_section_bytes +
   14752                         authorization_size_bytes +
   14753                         authorization_section_bytes +
   14754                         parameter_section_bytes;
   14755   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   14756   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   14757                                             serialized_command->size());
   14758   return TPM_RC_SUCCESS;
   14759 }
   14760 
   14761 TPM_RC Tpm::ParseResponse_Import(
   14762       const std::string& response,
   14763       TPM2B_PRIVATE* out_private,
   14764       AuthorizationDelegate* authorization_delegate) {
   14765   VLOG(3) << __func__;
   14766   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   14767   TPM_RC rc = TPM_RC_SUCCESS;
   14768   std::string buffer(response);
   14769   TPM_ST tag;
   14770   std::string tag_bytes;
   14771   rc = Parse_TPM_ST(
   14772       &buffer,
   14773       &tag,
   14774       &tag_bytes);
   14775   if (rc != TPM_RC_SUCCESS) {
   14776     return rc;
   14777   }
   14778   UINT32 response_size;
   14779   std::string response_size_bytes;
   14780   rc = Parse_UINT32(
   14781       &buffer,
   14782       &response_size,
   14783       &response_size_bytes);
   14784   if (rc != TPM_RC_SUCCESS) {
   14785     return rc;
   14786   }
   14787   TPM_RC response_code;
   14788   std::string response_code_bytes;
   14789   rc = Parse_TPM_RC(
   14790       &buffer,
   14791       &response_code,
   14792       &response_code_bytes);
   14793   if (rc != TPM_RC_SUCCESS) {
   14794     return rc;
   14795   }
   14796   if (response_size != response.size()) {
   14797     return TPM_RC_SIZE;
   14798   }
   14799   if (response_code != TPM_RC_SUCCESS) {
   14800     return response_code;
   14801   }
   14802   TPM_CC command_code = TPM_CC_Import;
   14803   std::string command_code_bytes;
   14804   rc = Serialize_TPM_CC(
   14805       command_code,
   14806       &command_code_bytes);
   14807   if (rc != TPM_RC_SUCCESS) {
   14808     return rc;
   14809   }
   14810   std::string authorization_section_bytes;
   14811   if (tag == TPM_ST_SESSIONS) {
   14812     UINT32 parameter_section_size = buffer.size();
   14813     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   14814     if (rc != TPM_RC_SUCCESS) {
   14815       return rc;
   14816     }
   14817     if (parameter_section_size > buffer.size()) {
   14818       return TPM_RC_INSUFFICIENT;
   14819     }
   14820     authorization_section_bytes = buffer.substr(parameter_section_size);
   14821     // Keep the parameter section in |buffer|.
   14822     buffer.erase(parameter_section_size);
   14823   }
   14824   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   14825       crypto::SecureHash::SHA256));
   14826   hash->Update(response_code_bytes.data(),
   14827                response_code_bytes.size());
   14828   hash->Update(command_code_bytes.data(),
   14829                command_code_bytes.size());
   14830   hash->Update(buffer.data(),
   14831                buffer.size());
   14832   std::string response_hash(32, 0);
   14833   hash->Finish(string_as_array(&response_hash), response_hash.size());
   14834   if (tag == TPM_ST_SESSIONS) {
   14835     CHECK(authorization_delegate) << "Authorization delegate missing!";
   14836     if (!authorization_delegate->CheckResponseAuthorization(
   14837         response_hash,
   14838         authorization_section_bytes)) {
   14839       return TRUNKS_RC_AUTHORIZATION_FAILED;
   14840     }
   14841   }
   14842   std::string out_private_bytes;
   14843   rc = Parse_TPM2B_PRIVATE(
   14844       &buffer,
   14845       out_private,
   14846       &out_private_bytes);
   14847   if (rc != TPM_RC_SUCCESS) {
   14848     return rc;
   14849   }
   14850   if (tag == TPM_ST_SESSIONS) {
   14851     CHECK(authorization_delegate) << "Authorization delegate missing!";
   14852     // Decrypt just the parameter data, not the size.
   14853     std::string tmp = out_private_bytes.substr(2);
   14854     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   14855       return TRUNKS_RC_ENCRYPTION_FAILED;
   14856     }
   14857     out_private_bytes.replace(2, std::string::npos, tmp);
   14858     rc = Parse_TPM2B_PRIVATE(
   14859         &out_private_bytes,
   14860         out_private,
   14861         nullptr);
   14862     if (rc != TPM_RC_SUCCESS) {
   14863       return rc;
   14864     }
   14865   }
   14866   return TPM_RC_SUCCESS;
   14867 }
   14868 
   14869 void ImportErrorCallback(
   14870     const Tpm::ImportResponse& callback,
   14871     TPM_RC response_code) {
   14872   VLOG(1) << __func__;
   14873   callback.Run(response_code,
   14874                TPM2B_PRIVATE());
   14875 }
   14876 
   14877 void ImportResponseParser(
   14878     const Tpm::ImportResponse& callback,
   14879     AuthorizationDelegate* authorization_delegate,
   14880     const std::string& response) {
   14881   VLOG(1) << __func__;
   14882   base::Callback<void(TPM_RC)> error_reporter =
   14883       base::Bind(ImportErrorCallback, callback);
   14884   TPM2B_PRIVATE out_private;
   14885   TPM_RC rc = Tpm::ParseResponse_Import(
   14886       response,
   14887       &out_private,
   14888       authorization_delegate);
   14889   if (rc != TPM_RC_SUCCESS) {
   14890     error_reporter.Run(rc);
   14891     return;
   14892   }
   14893   callback.Run(
   14894       rc,
   14895       out_private);
   14896 }
   14897 
   14898 void Tpm::Import(
   14899       const TPMI_DH_OBJECT& parent_handle,
   14900       const std::string& parent_handle_name,
   14901       const TPM2B_DATA& encryption_key,
   14902       const TPM2B_PUBLIC& object_public,
   14903       const TPM2B_PRIVATE& duplicate,
   14904       const TPM2B_ENCRYPTED_SECRET& in_sym_seed,
   14905       const TPMT_SYM_DEF_OBJECT& symmetric_alg,
   14906       AuthorizationDelegate* authorization_delegate,
   14907       const ImportResponse& callback) {
   14908   VLOG(1) << __func__;
   14909   base::Callback<void(TPM_RC)> error_reporter =
   14910       base::Bind(ImportErrorCallback, callback);
   14911   base::Callback<void(const std::string&)> parser =
   14912       base::Bind(ImportResponseParser,
   14913                  callback,
   14914                  authorization_delegate);
   14915   std::string command;
   14916   TPM_RC rc = SerializeCommand_Import(
   14917       parent_handle,
   14918       parent_handle_name,
   14919       encryption_key,
   14920       object_public,
   14921       duplicate,
   14922       in_sym_seed,
   14923       symmetric_alg,
   14924       &command,
   14925       authorization_delegate);
   14926   if (rc != TPM_RC_SUCCESS) {
   14927     error_reporter.Run(rc);
   14928     return;
   14929   }
   14930   transceiver_->SendCommand(command, parser);
   14931 }
   14932 
   14933 TPM_RC Tpm::ImportSync(
   14934       const TPMI_DH_OBJECT& parent_handle,
   14935       const std::string& parent_handle_name,
   14936       const TPM2B_DATA& encryption_key,
   14937       const TPM2B_PUBLIC& object_public,
   14938       const TPM2B_PRIVATE& duplicate,
   14939       const TPM2B_ENCRYPTED_SECRET& in_sym_seed,
   14940       const TPMT_SYM_DEF_OBJECT& symmetric_alg,
   14941       TPM2B_PRIVATE* out_private,
   14942       AuthorizationDelegate* authorization_delegate) {
   14943   VLOG(1) << __func__;
   14944   std::string command;
   14945   TPM_RC rc = SerializeCommand_Import(
   14946       parent_handle,
   14947       parent_handle_name,
   14948       encryption_key,
   14949       object_public,
   14950       duplicate,
   14951       in_sym_seed,
   14952       symmetric_alg,
   14953       &command,
   14954       authorization_delegate);
   14955   if (rc != TPM_RC_SUCCESS) {
   14956     return rc;
   14957   }
   14958   std::string response = transceiver_->SendCommandAndWait(command);
   14959   rc = ParseResponse_Import(
   14960       response,
   14961       out_private,
   14962       authorization_delegate);
   14963   return rc;
   14964 }
   14965 
   14966 TPM_RC Tpm::SerializeCommand_RSA_Encrypt(
   14967       const TPMI_DH_OBJECT& key_handle,
   14968       const std::string& key_handle_name,
   14969       const TPM2B_PUBLIC_KEY_RSA& message,
   14970       const TPMT_RSA_DECRYPT& in_scheme,
   14971       const TPM2B_DATA& label,
   14972       std::string* serialized_command,
   14973       AuthorizationDelegate* authorization_delegate) {
   14974   VLOG(3) << __func__;
   14975   TPM_RC rc = TPM_RC_SUCCESS;
   14976   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   14977   UINT32 command_size = 10;  // Header size.
   14978   std::string handle_section_bytes;
   14979   std::string parameter_section_bytes;
   14980   TPM_CC command_code = TPM_CC_RSA_Encrypt;
   14981   bool is_command_parameter_encryption_possible = true;
   14982   bool is_response_parameter_encryption_possible = true;
   14983   std::string command_code_bytes;
   14984   rc = Serialize_TPM_CC(
   14985       command_code,
   14986       &command_code_bytes);
   14987   if (rc != TPM_RC_SUCCESS) {
   14988     return rc;
   14989   }
   14990   std::string key_handle_bytes;
   14991   rc = Serialize_TPMI_DH_OBJECT(
   14992       key_handle,
   14993       &key_handle_bytes);
   14994   if (rc != TPM_RC_SUCCESS) {
   14995     return rc;
   14996   }
   14997   std::string message_bytes;
   14998   rc = Serialize_TPM2B_PUBLIC_KEY_RSA(
   14999       message,
   15000       &message_bytes);
   15001   if (rc != TPM_RC_SUCCESS) {
   15002     return rc;
   15003   }
   15004   std::string in_scheme_bytes;
   15005   rc = Serialize_TPMT_RSA_DECRYPT(
   15006       in_scheme,
   15007       &in_scheme_bytes);
   15008   if (rc != TPM_RC_SUCCESS) {
   15009     return rc;
   15010   }
   15011   std::string label_bytes;
   15012   rc = Serialize_TPM2B_DATA(
   15013       label,
   15014       &label_bytes);
   15015   if (rc != TPM_RC_SUCCESS) {
   15016     return rc;
   15017   }
   15018   if (authorization_delegate) {
   15019     // Encrypt just the parameter data, not the size.
   15020     std::string tmp = message_bytes.substr(2);
   15021     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   15022       return TRUNKS_RC_ENCRYPTION_FAILED;
   15023     }
   15024     message_bytes.replace(2, std::string::npos, tmp);
   15025   }
   15026   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15027       crypto::SecureHash::SHA256));
   15028   hash->Update(command_code_bytes.data(),
   15029                command_code_bytes.size());
   15030   hash->Update(key_handle_name.data(),
   15031                key_handle_name.size());
   15032   handle_section_bytes += key_handle_bytes;
   15033   command_size += key_handle_bytes.size();
   15034   hash->Update(message_bytes.data(),
   15035                message_bytes.size());
   15036   parameter_section_bytes += message_bytes;
   15037   command_size += message_bytes.size();
   15038   hash->Update(in_scheme_bytes.data(),
   15039                in_scheme_bytes.size());
   15040   parameter_section_bytes += in_scheme_bytes;
   15041   command_size += in_scheme_bytes.size();
   15042   hash->Update(label_bytes.data(),
   15043                label_bytes.size());
   15044   parameter_section_bytes += label_bytes;
   15045   command_size += label_bytes.size();
   15046   std::string command_hash(32, 0);
   15047   hash->Finish(string_as_array(&command_hash), command_hash.size());
   15048   std::string authorization_section_bytes;
   15049   std::string authorization_size_bytes;
   15050   if (authorization_delegate) {
   15051     if (!authorization_delegate->GetCommandAuthorization(
   15052         command_hash,
   15053         is_command_parameter_encryption_possible,
   15054         is_response_parameter_encryption_possible,
   15055         &authorization_section_bytes)) {
   15056       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15057     }
   15058     if (!authorization_section_bytes.empty()) {
   15059       tag = TPM_ST_SESSIONS;
   15060       std::string tmp;
   15061       rc = Serialize_UINT32(authorization_section_bytes.size(),
   15062                             &authorization_size_bytes);
   15063       if (rc != TPM_RC_SUCCESS) {
   15064         return rc;
   15065       }
   15066       command_size += authorization_size_bytes.size() +
   15067                       authorization_section_bytes.size();
   15068     }
   15069   }
   15070   std::string tag_bytes;
   15071   rc = Serialize_TPMI_ST_COMMAND_TAG(
   15072       tag,
   15073       &tag_bytes);
   15074   if (rc != TPM_RC_SUCCESS) {
   15075     return rc;
   15076   }
   15077   std::string command_size_bytes;
   15078   rc = Serialize_UINT32(
   15079       command_size,
   15080       &command_size_bytes);
   15081   if (rc != TPM_RC_SUCCESS) {
   15082     return rc;
   15083   }
   15084   *serialized_command = tag_bytes +
   15085                         command_size_bytes +
   15086                         command_code_bytes +
   15087                         handle_section_bytes +
   15088                         authorization_size_bytes +
   15089                         authorization_section_bytes +
   15090                         parameter_section_bytes;
   15091   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   15092   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   15093                                             serialized_command->size());
   15094   return TPM_RC_SUCCESS;
   15095 }
   15096 
   15097 TPM_RC Tpm::ParseResponse_RSA_Encrypt(
   15098       const std::string& response,
   15099       TPM2B_PUBLIC_KEY_RSA* out_data,
   15100       AuthorizationDelegate* authorization_delegate) {
   15101   VLOG(3) << __func__;
   15102   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   15103   TPM_RC rc = TPM_RC_SUCCESS;
   15104   std::string buffer(response);
   15105   TPM_ST tag;
   15106   std::string tag_bytes;
   15107   rc = Parse_TPM_ST(
   15108       &buffer,
   15109       &tag,
   15110       &tag_bytes);
   15111   if (rc != TPM_RC_SUCCESS) {
   15112     return rc;
   15113   }
   15114   UINT32 response_size;
   15115   std::string response_size_bytes;
   15116   rc = Parse_UINT32(
   15117       &buffer,
   15118       &response_size,
   15119       &response_size_bytes);
   15120   if (rc != TPM_RC_SUCCESS) {
   15121     return rc;
   15122   }
   15123   TPM_RC response_code;
   15124   std::string response_code_bytes;
   15125   rc = Parse_TPM_RC(
   15126       &buffer,
   15127       &response_code,
   15128       &response_code_bytes);
   15129   if (rc != TPM_RC_SUCCESS) {
   15130     return rc;
   15131   }
   15132   if (response_size != response.size()) {
   15133     return TPM_RC_SIZE;
   15134   }
   15135   if (response_code != TPM_RC_SUCCESS) {
   15136     return response_code;
   15137   }
   15138   TPM_CC command_code = TPM_CC_RSA_Encrypt;
   15139   std::string command_code_bytes;
   15140   rc = Serialize_TPM_CC(
   15141       command_code,
   15142       &command_code_bytes);
   15143   if (rc != TPM_RC_SUCCESS) {
   15144     return rc;
   15145   }
   15146   std::string authorization_section_bytes;
   15147   if (tag == TPM_ST_SESSIONS) {
   15148     UINT32 parameter_section_size = buffer.size();
   15149     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   15150     if (rc != TPM_RC_SUCCESS) {
   15151       return rc;
   15152     }
   15153     if (parameter_section_size > buffer.size()) {
   15154       return TPM_RC_INSUFFICIENT;
   15155     }
   15156     authorization_section_bytes = buffer.substr(parameter_section_size);
   15157     // Keep the parameter section in |buffer|.
   15158     buffer.erase(parameter_section_size);
   15159   }
   15160   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15161       crypto::SecureHash::SHA256));
   15162   hash->Update(response_code_bytes.data(),
   15163                response_code_bytes.size());
   15164   hash->Update(command_code_bytes.data(),
   15165                command_code_bytes.size());
   15166   hash->Update(buffer.data(),
   15167                buffer.size());
   15168   std::string response_hash(32, 0);
   15169   hash->Finish(string_as_array(&response_hash), response_hash.size());
   15170   if (tag == TPM_ST_SESSIONS) {
   15171     CHECK(authorization_delegate) << "Authorization delegate missing!";
   15172     if (!authorization_delegate->CheckResponseAuthorization(
   15173         response_hash,
   15174         authorization_section_bytes)) {
   15175       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15176     }
   15177   }
   15178   std::string out_data_bytes;
   15179   rc = Parse_TPM2B_PUBLIC_KEY_RSA(
   15180       &buffer,
   15181       out_data,
   15182       &out_data_bytes);
   15183   if (rc != TPM_RC_SUCCESS) {
   15184     return rc;
   15185   }
   15186   if (tag == TPM_ST_SESSIONS) {
   15187     CHECK(authorization_delegate) << "Authorization delegate missing!";
   15188     // Decrypt just the parameter data, not the size.
   15189     std::string tmp = out_data_bytes.substr(2);
   15190     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   15191       return TRUNKS_RC_ENCRYPTION_FAILED;
   15192     }
   15193     out_data_bytes.replace(2, std::string::npos, tmp);
   15194     rc = Parse_TPM2B_PUBLIC_KEY_RSA(
   15195         &out_data_bytes,
   15196         out_data,
   15197         nullptr);
   15198     if (rc != TPM_RC_SUCCESS) {
   15199       return rc;
   15200     }
   15201   }
   15202   return TPM_RC_SUCCESS;
   15203 }
   15204 
   15205 void RSA_EncryptErrorCallback(
   15206     const Tpm::RSA_EncryptResponse& callback,
   15207     TPM_RC response_code) {
   15208   VLOG(1) << __func__;
   15209   callback.Run(response_code,
   15210                TPM2B_PUBLIC_KEY_RSA());
   15211 }
   15212 
   15213 void RSA_EncryptResponseParser(
   15214     const Tpm::RSA_EncryptResponse& callback,
   15215     AuthorizationDelegate* authorization_delegate,
   15216     const std::string& response) {
   15217   VLOG(1) << __func__;
   15218   base::Callback<void(TPM_RC)> error_reporter =
   15219       base::Bind(RSA_EncryptErrorCallback, callback);
   15220   TPM2B_PUBLIC_KEY_RSA out_data;
   15221   TPM_RC rc = Tpm::ParseResponse_RSA_Encrypt(
   15222       response,
   15223       &out_data,
   15224       authorization_delegate);
   15225   if (rc != TPM_RC_SUCCESS) {
   15226     error_reporter.Run(rc);
   15227     return;
   15228   }
   15229   callback.Run(
   15230       rc,
   15231       out_data);
   15232 }
   15233 
   15234 void Tpm::RSA_Encrypt(
   15235       const TPMI_DH_OBJECT& key_handle,
   15236       const std::string& key_handle_name,
   15237       const TPM2B_PUBLIC_KEY_RSA& message,
   15238       const TPMT_RSA_DECRYPT& in_scheme,
   15239       const TPM2B_DATA& label,
   15240       AuthorizationDelegate* authorization_delegate,
   15241       const RSA_EncryptResponse& callback) {
   15242   VLOG(1) << __func__;
   15243   base::Callback<void(TPM_RC)> error_reporter =
   15244       base::Bind(RSA_EncryptErrorCallback, callback);
   15245   base::Callback<void(const std::string&)> parser =
   15246       base::Bind(RSA_EncryptResponseParser,
   15247                  callback,
   15248                  authorization_delegate);
   15249   std::string command;
   15250   TPM_RC rc = SerializeCommand_RSA_Encrypt(
   15251       key_handle,
   15252       key_handle_name,
   15253       message,
   15254       in_scheme,
   15255       label,
   15256       &command,
   15257       authorization_delegate);
   15258   if (rc != TPM_RC_SUCCESS) {
   15259     error_reporter.Run(rc);
   15260     return;
   15261   }
   15262   transceiver_->SendCommand(command, parser);
   15263 }
   15264 
   15265 TPM_RC Tpm::RSA_EncryptSync(
   15266       const TPMI_DH_OBJECT& key_handle,
   15267       const std::string& key_handle_name,
   15268       const TPM2B_PUBLIC_KEY_RSA& message,
   15269       const TPMT_RSA_DECRYPT& in_scheme,
   15270       const TPM2B_DATA& label,
   15271       TPM2B_PUBLIC_KEY_RSA* out_data,
   15272       AuthorizationDelegate* authorization_delegate) {
   15273   VLOG(1) << __func__;
   15274   std::string command;
   15275   TPM_RC rc = SerializeCommand_RSA_Encrypt(
   15276       key_handle,
   15277       key_handle_name,
   15278       message,
   15279       in_scheme,
   15280       label,
   15281       &command,
   15282       authorization_delegate);
   15283   if (rc != TPM_RC_SUCCESS) {
   15284     return rc;
   15285   }
   15286   std::string response = transceiver_->SendCommandAndWait(command);
   15287   rc = ParseResponse_RSA_Encrypt(
   15288       response,
   15289       out_data,
   15290       authorization_delegate);
   15291   return rc;
   15292 }
   15293 
   15294 TPM_RC Tpm::SerializeCommand_RSA_Decrypt(
   15295       const TPMI_DH_OBJECT& key_handle,
   15296       const std::string& key_handle_name,
   15297       const TPM2B_PUBLIC_KEY_RSA& cipher_text,
   15298       const TPMT_RSA_DECRYPT& in_scheme,
   15299       const TPM2B_DATA& label,
   15300       std::string* serialized_command,
   15301       AuthorizationDelegate* authorization_delegate) {
   15302   VLOG(3) << __func__;
   15303   TPM_RC rc = TPM_RC_SUCCESS;
   15304   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   15305   UINT32 command_size = 10;  // Header size.
   15306   std::string handle_section_bytes;
   15307   std::string parameter_section_bytes;
   15308   TPM_CC command_code = TPM_CC_RSA_Decrypt;
   15309   bool is_command_parameter_encryption_possible = true;
   15310   bool is_response_parameter_encryption_possible = true;
   15311   std::string command_code_bytes;
   15312   rc = Serialize_TPM_CC(
   15313       command_code,
   15314       &command_code_bytes);
   15315   if (rc != TPM_RC_SUCCESS) {
   15316     return rc;
   15317   }
   15318   std::string key_handle_bytes;
   15319   rc = Serialize_TPMI_DH_OBJECT(
   15320       key_handle,
   15321       &key_handle_bytes);
   15322   if (rc != TPM_RC_SUCCESS) {
   15323     return rc;
   15324   }
   15325   std::string cipher_text_bytes;
   15326   rc = Serialize_TPM2B_PUBLIC_KEY_RSA(
   15327       cipher_text,
   15328       &cipher_text_bytes);
   15329   if (rc != TPM_RC_SUCCESS) {
   15330     return rc;
   15331   }
   15332   std::string in_scheme_bytes;
   15333   rc = Serialize_TPMT_RSA_DECRYPT(
   15334       in_scheme,
   15335       &in_scheme_bytes);
   15336   if (rc != TPM_RC_SUCCESS) {
   15337     return rc;
   15338   }
   15339   std::string label_bytes;
   15340   rc = Serialize_TPM2B_DATA(
   15341       label,
   15342       &label_bytes);
   15343   if (rc != TPM_RC_SUCCESS) {
   15344     return rc;
   15345   }
   15346   if (authorization_delegate) {
   15347     // Encrypt just the parameter data, not the size.
   15348     std::string tmp = cipher_text_bytes.substr(2);
   15349     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   15350       return TRUNKS_RC_ENCRYPTION_FAILED;
   15351     }
   15352     cipher_text_bytes.replace(2, std::string::npos, tmp);
   15353   }
   15354   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15355       crypto::SecureHash::SHA256));
   15356   hash->Update(command_code_bytes.data(),
   15357                command_code_bytes.size());
   15358   hash->Update(key_handle_name.data(),
   15359                key_handle_name.size());
   15360   handle_section_bytes += key_handle_bytes;
   15361   command_size += key_handle_bytes.size();
   15362   hash->Update(cipher_text_bytes.data(),
   15363                cipher_text_bytes.size());
   15364   parameter_section_bytes += cipher_text_bytes;
   15365   command_size += cipher_text_bytes.size();
   15366   hash->Update(in_scheme_bytes.data(),
   15367                in_scheme_bytes.size());
   15368   parameter_section_bytes += in_scheme_bytes;
   15369   command_size += in_scheme_bytes.size();
   15370   hash->Update(label_bytes.data(),
   15371                label_bytes.size());
   15372   parameter_section_bytes += label_bytes;
   15373   command_size += label_bytes.size();
   15374   std::string command_hash(32, 0);
   15375   hash->Finish(string_as_array(&command_hash), command_hash.size());
   15376   std::string authorization_section_bytes;
   15377   std::string authorization_size_bytes;
   15378   if (authorization_delegate) {
   15379     if (!authorization_delegate->GetCommandAuthorization(
   15380         command_hash,
   15381         is_command_parameter_encryption_possible,
   15382         is_response_parameter_encryption_possible,
   15383         &authorization_section_bytes)) {
   15384       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15385     }
   15386     if (!authorization_section_bytes.empty()) {
   15387       tag = TPM_ST_SESSIONS;
   15388       std::string tmp;
   15389       rc = Serialize_UINT32(authorization_section_bytes.size(),
   15390                             &authorization_size_bytes);
   15391       if (rc != TPM_RC_SUCCESS) {
   15392         return rc;
   15393       }
   15394       command_size += authorization_size_bytes.size() +
   15395                       authorization_section_bytes.size();
   15396     }
   15397   }
   15398   std::string tag_bytes;
   15399   rc = Serialize_TPMI_ST_COMMAND_TAG(
   15400       tag,
   15401       &tag_bytes);
   15402   if (rc != TPM_RC_SUCCESS) {
   15403     return rc;
   15404   }
   15405   std::string command_size_bytes;
   15406   rc = Serialize_UINT32(
   15407       command_size,
   15408       &command_size_bytes);
   15409   if (rc != TPM_RC_SUCCESS) {
   15410     return rc;
   15411   }
   15412   *serialized_command = tag_bytes +
   15413                         command_size_bytes +
   15414                         command_code_bytes +
   15415                         handle_section_bytes +
   15416                         authorization_size_bytes +
   15417                         authorization_section_bytes +
   15418                         parameter_section_bytes;
   15419   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   15420   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   15421                                             serialized_command->size());
   15422   return TPM_RC_SUCCESS;
   15423 }
   15424 
   15425 TPM_RC Tpm::ParseResponse_RSA_Decrypt(
   15426       const std::string& response,
   15427       TPM2B_PUBLIC_KEY_RSA* message,
   15428       AuthorizationDelegate* authorization_delegate) {
   15429   VLOG(3) << __func__;
   15430   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   15431   TPM_RC rc = TPM_RC_SUCCESS;
   15432   std::string buffer(response);
   15433   TPM_ST tag;
   15434   std::string tag_bytes;
   15435   rc = Parse_TPM_ST(
   15436       &buffer,
   15437       &tag,
   15438       &tag_bytes);
   15439   if (rc != TPM_RC_SUCCESS) {
   15440     return rc;
   15441   }
   15442   UINT32 response_size;
   15443   std::string response_size_bytes;
   15444   rc = Parse_UINT32(
   15445       &buffer,
   15446       &response_size,
   15447       &response_size_bytes);
   15448   if (rc != TPM_RC_SUCCESS) {
   15449     return rc;
   15450   }
   15451   TPM_RC response_code;
   15452   std::string response_code_bytes;
   15453   rc = Parse_TPM_RC(
   15454       &buffer,
   15455       &response_code,
   15456       &response_code_bytes);
   15457   if (rc != TPM_RC_SUCCESS) {
   15458     return rc;
   15459   }
   15460   if (response_size != response.size()) {
   15461     return TPM_RC_SIZE;
   15462   }
   15463   if (response_code != TPM_RC_SUCCESS) {
   15464     return response_code;
   15465   }
   15466   TPM_CC command_code = TPM_CC_RSA_Decrypt;
   15467   std::string command_code_bytes;
   15468   rc = Serialize_TPM_CC(
   15469       command_code,
   15470       &command_code_bytes);
   15471   if (rc != TPM_RC_SUCCESS) {
   15472     return rc;
   15473   }
   15474   std::string authorization_section_bytes;
   15475   if (tag == TPM_ST_SESSIONS) {
   15476     UINT32 parameter_section_size = buffer.size();
   15477     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   15478     if (rc != TPM_RC_SUCCESS) {
   15479       return rc;
   15480     }
   15481     if (parameter_section_size > buffer.size()) {
   15482       return TPM_RC_INSUFFICIENT;
   15483     }
   15484     authorization_section_bytes = buffer.substr(parameter_section_size);
   15485     // Keep the parameter section in |buffer|.
   15486     buffer.erase(parameter_section_size);
   15487   }
   15488   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15489       crypto::SecureHash::SHA256));
   15490   hash->Update(response_code_bytes.data(),
   15491                response_code_bytes.size());
   15492   hash->Update(command_code_bytes.data(),
   15493                command_code_bytes.size());
   15494   hash->Update(buffer.data(),
   15495                buffer.size());
   15496   std::string response_hash(32, 0);
   15497   hash->Finish(string_as_array(&response_hash), response_hash.size());
   15498   if (tag == TPM_ST_SESSIONS) {
   15499     CHECK(authorization_delegate) << "Authorization delegate missing!";
   15500     if (!authorization_delegate->CheckResponseAuthorization(
   15501         response_hash,
   15502         authorization_section_bytes)) {
   15503       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15504     }
   15505   }
   15506   std::string message_bytes;
   15507   rc = Parse_TPM2B_PUBLIC_KEY_RSA(
   15508       &buffer,
   15509       message,
   15510       &message_bytes);
   15511   if (rc != TPM_RC_SUCCESS) {
   15512     return rc;
   15513   }
   15514   if (tag == TPM_ST_SESSIONS) {
   15515     CHECK(authorization_delegate) << "Authorization delegate missing!";
   15516     // Decrypt just the parameter data, not the size.
   15517     std::string tmp = message_bytes.substr(2);
   15518     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   15519       return TRUNKS_RC_ENCRYPTION_FAILED;
   15520     }
   15521     message_bytes.replace(2, std::string::npos, tmp);
   15522     rc = Parse_TPM2B_PUBLIC_KEY_RSA(
   15523         &message_bytes,
   15524         message,
   15525         nullptr);
   15526     if (rc != TPM_RC_SUCCESS) {
   15527       return rc;
   15528     }
   15529   }
   15530   return TPM_RC_SUCCESS;
   15531 }
   15532 
   15533 void RSA_DecryptErrorCallback(
   15534     const Tpm::RSA_DecryptResponse& callback,
   15535     TPM_RC response_code) {
   15536   VLOG(1) << __func__;
   15537   callback.Run(response_code,
   15538                TPM2B_PUBLIC_KEY_RSA());
   15539 }
   15540 
   15541 void RSA_DecryptResponseParser(
   15542     const Tpm::RSA_DecryptResponse& callback,
   15543     AuthorizationDelegate* authorization_delegate,
   15544     const std::string& response) {
   15545   VLOG(1) << __func__;
   15546   base::Callback<void(TPM_RC)> error_reporter =
   15547       base::Bind(RSA_DecryptErrorCallback, callback);
   15548   TPM2B_PUBLIC_KEY_RSA message;
   15549   TPM_RC rc = Tpm::ParseResponse_RSA_Decrypt(
   15550       response,
   15551       &message,
   15552       authorization_delegate);
   15553   if (rc != TPM_RC_SUCCESS) {
   15554     error_reporter.Run(rc);
   15555     return;
   15556   }
   15557   callback.Run(
   15558       rc,
   15559       message);
   15560 }
   15561 
   15562 void Tpm::RSA_Decrypt(
   15563       const TPMI_DH_OBJECT& key_handle,
   15564       const std::string& key_handle_name,
   15565       const TPM2B_PUBLIC_KEY_RSA& cipher_text,
   15566       const TPMT_RSA_DECRYPT& in_scheme,
   15567       const TPM2B_DATA& label,
   15568       AuthorizationDelegate* authorization_delegate,
   15569       const RSA_DecryptResponse& callback) {
   15570   VLOG(1) << __func__;
   15571   base::Callback<void(TPM_RC)> error_reporter =
   15572       base::Bind(RSA_DecryptErrorCallback, callback);
   15573   base::Callback<void(const std::string&)> parser =
   15574       base::Bind(RSA_DecryptResponseParser,
   15575                  callback,
   15576                  authorization_delegate);
   15577   std::string command;
   15578   TPM_RC rc = SerializeCommand_RSA_Decrypt(
   15579       key_handle,
   15580       key_handle_name,
   15581       cipher_text,
   15582       in_scheme,
   15583       label,
   15584       &command,
   15585       authorization_delegate);
   15586   if (rc != TPM_RC_SUCCESS) {
   15587     error_reporter.Run(rc);
   15588     return;
   15589   }
   15590   transceiver_->SendCommand(command, parser);
   15591 }
   15592 
   15593 TPM_RC Tpm::RSA_DecryptSync(
   15594       const TPMI_DH_OBJECT& key_handle,
   15595       const std::string& key_handle_name,
   15596       const TPM2B_PUBLIC_KEY_RSA& cipher_text,
   15597       const TPMT_RSA_DECRYPT& in_scheme,
   15598       const TPM2B_DATA& label,
   15599       TPM2B_PUBLIC_KEY_RSA* message,
   15600       AuthorizationDelegate* authorization_delegate) {
   15601   VLOG(1) << __func__;
   15602   std::string command;
   15603   TPM_RC rc = SerializeCommand_RSA_Decrypt(
   15604       key_handle,
   15605       key_handle_name,
   15606       cipher_text,
   15607       in_scheme,
   15608       label,
   15609       &command,
   15610       authorization_delegate);
   15611   if (rc != TPM_RC_SUCCESS) {
   15612     return rc;
   15613   }
   15614   std::string response = transceiver_->SendCommandAndWait(command);
   15615   rc = ParseResponse_RSA_Decrypt(
   15616       response,
   15617       message,
   15618       authorization_delegate);
   15619   return rc;
   15620 }
   15621 
   15622 TPM_RC Tpm::SerializeCommand_ECDH_KeyGen(
   15623       const TPMI_DH_OBJECT& key_handle,
   15624       const std::string& key_handle_name,
   15625       std::string* serialized_command,
   15626       AuthorizationDelegate* authorization_delegate) {
   15627   VLOG(3) << __func__;
   15628   TPM_RC rc = TPM_RC_SUCCESS;
   15629   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   15630   UINT32 command_size = 10;  // Header size.
   15631   std::string handle_section_bytes;
   15632   std::string parameter_section_bytes;
   15633   TPM_CC command_code = TPM_CC_ECDH_KeyGen;
   15634   bool is_command_parameter_encryption_possible = false;
   15635   bool is_response_parameter_encryption_possible = true;
   15636   std::string command_code_bytes;
   15637   rc = Serialize_TPM_CC(
   15638       command_code,
   15639       &command_code_bytes);
   15640   if (rc != TPM_RC_SUCCESS) {
   15641     return rc;
   15642   }
   15643   std::string key_handle_bytes;
   15644   rc = Serialize_TPMI_DH_OBJECT(
   15645       key_handle,
   15646       &key_handle_bytes);
   15647   if (rc != TPM_RC_SUCCESS) {
   15648     return rc;
   15649   }
   15650   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15651       crypto::SecureHash::SHA256));
   15652   hash->Update(command_code_bytes.data(),
   15653                command_code_bytes.size());
   15654   hash->Update(key_handle_name.data(),
   15655                key_handle_name.size());
   15656   handle_section_bytes += key_handle_bytes;
   15657   command_size += key_handle_bytes.size();
   15658   std::string command_hash(32, 0);
   15659   hash->Finish(string_as_array(&command_hash), command_hash.size());
   15660   std::string authorization_section_bytes;
   15661   std::string authorization_size_bytes;
   15662   if (authorization_delegate) {
   15663     if (!authorization_delegate->GetCommandAuthorization(
   15664         command_hash,
   15665         is_command_parameter_encryption_possible,
   15666         is_response_parameter_encryption_possible,
   15667         &authorization_section_bytes)) {
   15668       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15669     }
   15670     if (!authorization_section_bytes.empty()) {
   15671       tag = TPM_ST_SESSIONS;
   15672       std::string tmp;
   15673       rc = Serialize_UINT32(authorization_section_bytes.size(),
   15674                             &authorization_size_bytes);
   15675       if (rc != TPM_RC_SUCCESS) {
   15676         return rc;
   15677       }
   15678       command_size += authorization_size_bytes.size() +
   15679                       authorization_section_bytes.size();
   15680     }
   15681   }
   15682   std::string tag_bytes;
   15683   rc = Serialize_TPMI_ST_COMMAND_TAG(
   15684       tag,
   15685       &tag_bytes);
   15686   if (rc != TPM_RC_SUCCESS) {
   15687     return rc;
   15688   }
   15689   std::string command_size_bytes;
   15690   rc = Serialize_UINT32(
   15691       command_size,
   15692       &command_size_bytes);
   15693   if (rc != TPM_RC_SUCCESS) {
   15694     return rc;
   15695   }
   15696   *serialized_command = tag_bytes +
   15697                         command_size_bytes +
   15698                         command_code_bytes +
   15699                         handle_section_bytes +
   15700                         authorization_size_bytes +
   15701                         authorization_section_bytes +
   15702                         parameter_section_bytes;
   15703   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   15704   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   15705                                             serialized_command->size());
   15706   return TPM_RC_SUCCESS;
   15707 }
   15708 
   15709 TPM_RC Tpm::ParseResponse_ECDH_KeyGen(
   15710       const std::string& response,
   15711       TPM2B_ECC_POINT* z_point,
   15712       TPM2B_ECC_POINT* pub_point,
   15713       AuthorizationDelegate* authorization_delegate) {
   15714   VLOG(3) << __func__;
   15715   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   15716   TPM_RC rc = TPM_RC_SUCCESS;
   15717   std::string buffer(response);
   15718   TPM_ST tag;
   15719   std::string tag_bytes;
   15720   rc = Parse_TPM_ST(
   15721       &buffer,
   15722       &tag,
   15723       &tag_bytes);
   15724   if (rc != TPM_RC_SUCCESS) {
   15725     return rc;
   15726   }
   15727   UINT32 response_size;
   15728   std::string response_size_bytes;
   15729   rc = Parse_UINT32(
   15730       &buffer,
   15731       &response_size,
   15732       &response_size_bytes);
   15733   if (rc != TPM_RC_SUCCESS) {
   15734     return rc;
   15735   }
   15736   TPM_RC response_code;
   15737   std::string response_code_bytes;
   15738   rc = Parse_TPM_RC(
   15739       &buffer,
   15740       &response_code,
   15741       &response_code_bytes);
   15742   if (rc != TPM_RC_SUCCESS) {
   15743     return rc;
   15744   }
   15745   if (response_size != response.size()) {
   15746     return TPM_RC_SIZE;
   15747   }
   15748   if (response_code != TPM_RC_SUCCESS) {
   15749     return response_code;
   15750   }
   15751   TPM_CC command_code = TPM_CC_ECDH_KeyGen;
   15752   std::string command_code_bytes;
   15753   rc = Serialize_TPM_CC(
   15754       command_code,
   15755       &command_code_bytes);
   15756   if (rc != TPM_RC_SUCCESS) {
   15757     return rc;
   15758   }
   15759   std::string authorization_section_bytes;
   15760   if (tag == TPM_ST_SESSIONS) {
   15761     UINT32 parameter_section_size = buffer.size();
   15762     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   15763     if (rc != TPM_RC_SUCCESS) {
   15764       return rc;
   15765     }
   15766     if (parameter_section_size > buffer.size()) {
   15767       return TPM_RC_INSUFFICIENT;
   15768     }
   15769     authorization_section_bytes = buffer.substr(parameter_section_size);
   15770     // Keep the parameter section in |buffer|.
   15771     buffer.erase(parameter_section_size);
   15772   }
   15773   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15774       crypto::SecureHash::SHA256));
   15775   hash->Update(response_code_bytes.data(),
   15776                response_code_bytes.size());
   15777   hash->Update(command_code_bytes.data(),
   15778                command_code_bytes.size());
   15779   hash->Update(buffer.data(),
   15780                buffer.size());
   15781   std::string response_hash(32, 0);
   15782   hash->Finish(string_as_array(&response_hash), response_hash.size());
   15783   if (tag == TPM_ST_SESSIONS) {
   15784     CHECK(authorization_delegate) << "Authorization delegate missing!";
   15785     if (!authorization_delegate->CheckResponseAuthorization(
   15786         response_hash,
   15787         authorization_section_bytes)) {
   15788       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15789     }
   15790   }
   15791   std::string z_point_bytes;
   15792   rc = Parse_TPM2B_ECC_POINT(
   15793       &buffer,
   15794       z_point,
   15795       &z_point_bytes);
   15796   if (rc != TPM_RC_SUCCESS) {
   15797     return rc;
   15798   }
   15799   std::string pub_point_bytes;
   15800   rc = Parse_TPM2B_ECC_POINT(
   15801       &buffer,
   15802       pub_point,
   15803       &pub_point_bytes);
   15804   if (rc != TPM_RC_SUCCESS) {
   15805     return rc;
   15806   }
   15807   if (tag == TPM_ST_SESSIONS) {
   15808     CHECK(authorization_delegate) << "Authorization delegate missing!";
   15809     // Decrypt just the parameter data, not the size.
   15810     std::string tmp = z_point_bytes.substr(2);
   15811     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   15812       return TRUNKS_RC_ENCRYPTION_FAILED;
   15813     }
   15814     z_point_bytes.replace(2, std::string::npos, tmp);
   15815     rc = Parse_TPM2B_ECC_POINT(
   15816         &z_point_bytes,
   15817         z_point,
   15818         nullptr);
   15819     if (rc != TPM_RC_SUCCESS) {
   15820       return rc;
   15821     }
   15822   }
   15823   return TPM_RC_SUCCESS;
   15824 }
   15825 
   15826 void ECDH_KeyGenErrorCallback(
   15827     const Tpm::ECDH_KeyGenResponse& callback,
   15828     TPM_RC response_code) {
   15829   VLOG(1) << __func__;
   15830   callback.Run(response_code,
   15831                TPM2B_ECC_POINT(),
   15832                TPM2B_ECC_POINT());
   15833 }
   15834 
   15835 void ECDH_KeyGenResponseParser(
   15836     const Tpm::ECDH_KeyGenResponse& callback,
   15837     AuthorizationDelegate* authorization_delegate,
   15838     const std::string& response) {
   15839   VLOG(1) << __func__;
   15840   base::Callback<void(TPM_RC)> error_reporter =
   15841       base::Bind(ECDH_KeyGenErrorCallback, callback);
   15842   TPM2B_ECC_POINT z_point;
   15843   TPM2B_ECC_POINT pub_point;
   15844   TPM_RC rc = Tpm::ParseResponse_ECDH_KeyGen(
   15845       response,
   15846       &z_point,
   15847       &pub_point,
   15848       authorization_delegate);
   15849   if (rc != TPM_RC_SUCCESS) {
   15850     error_reporter.Run(rc);
   15851     return;
   15852   }
   15853   callback.Run(
   15854       rc,
   15855       z_point,
   15856       pub_point);
   15857 }
   15858 
   15859 void Tpm::ECDH_KeyGen(
   15860       const TPMI_DH_OBJECT& key_handle,
   15861       const std::string& key_handle_name,
   15862       AuthorizationDelegate* authorization_delegate,
   15863       const ECDH_KeyGenResponse& callback) {
   15864   VLOG(1) << __func__;
   15865   base::Callback<void(TPM_RC)> error_reporter =
   15866       base::Bind(ECDH_KeyGenErrorCallback, callback);
   15867   base::Callback<void(const std::string&)> parser =
   15868       base::Bind(ECDH_KeyGenResponseParser,
   15869                  callback,
   15870                  authorization_delegate);
   15871   std::string command;
   15872   TPM_RC rc = SerializeCommand_ECDH_KeyGen(
   15873       key_handle,
   15874       key_handle_name,
   15875       &command,
   15876       authorization_delegate);
   15877   if (rc != TPM_RC_SUCCESS) {
   15878     error_reporter.Run(rc);
   15879     return;
   15880   }
   15881   transceiver_->SendCommand(command, parser);
   15882 }
   15883 
   15884 TPM_RC Tpm::ECDH_KeyGenSync(
   15885       const TPMI_DH_OBJECT& key_handle,
   15886       const std::string& key_handle_name,
   15887       TPM2B_ECC_POINT* z_point,
   15888       TPM2B_ECC_POINT* pub_point,
   15889       AuthorizationDelegate* authorization_delegate) {
   15890   VLOG(1) << __func__;
   15891   std::string command;
   15892   TPM_RC rc = SerializeCommand_ECDH_KeyGen(
   15893       key_handle,
   15894       key_handle_name,
   15895       &command,
   15896       authorization_delegate);
   15897   if (rc != TPM_RC_SUCCESS) {
   15898     return rc;
   15899   }
   15900   std::string response = transceiver_->SendCommandAndWait(command);
   15901   rc = ParseResponse_ECDH_KeyGen(
   15902       response,
   15903       z_point,
   15904       pub_point,
   15905       authorization_delegate);
   15906   return rc;
   15907 }
   15908 
   15909 TPM_RC Tpm::SerializeCommand_ECDH_ZGen(
   15910       const TPMI_DH_OBJECT& key_handle,
   15911       const std::string& key_handle_name,
   15912       const TPM2B_ECC_POINT& in_point,
   15913       std::string* serialized_command,
   15914       AuthorizationDelegate* authorization_delegate) {
   15915   VLOG(3) << __func__;
   15916   TPM_RC rc = TPM_RC_SUCCESS;
   15917   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   15918   UINT32 command_size = 10;  // Header size.
   15919   std::string handle_section_bytes;
   15920   std::string parameter_section_bytes;
   15921   TPM_CC command_code = TPM_CC_ECDH_ZGen;
   15922   bool is_command_parameter_encryption_possible = true;
   15923   bool is_response_parameter_encryption_possible = true;
   15924   std::string command_code_bytes;
   15925   rc = Serialize_TPM_CC(
   15926       command_code,
   15927       &command_code_bytes);
   15928   if (rc != TPM_RC_SUCCESS) {
   15929     return rc;
   15930   }
   15931   std::string key_handle_bytes;
   15932   rc = Serialize_TPMI_DH_OBJECT(
   15933       key_handle,
   15934       &key_handle_bytes);
   15935   if (rc != TPM_RC_SUCCESS) {
   15936     return rc;
   15937   }
   15938   std::string in_point_bytes;
   15939   rc = Serialize_TPM2B_ECC_POINT(
   15940       in_point,
   15941       &in_point_bytes);
   15942   if (rc != TPM_RC_SUCCESS) {
   15943     return rc;
   15944   }
   15945   if (authorization_delegate) {
   15946     // Encrypt just the parameter data, not the size.
   15947     std::string tmp = in_point_bytes.substr(2);
   15948     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   15949       return TRUNKS_RC_ENCRYPTION_FAILED;
   15950     }
   15951     in_point_bytes.replace(2, std::string::npos, tmp);
   15952   }
   15953   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   15954       crypto::SecureHash::SHA256));
   15955   hash->Update(command_code_bytes.data(),
   15956                command_code_bytes.size());
   15957   hash->Update(key_handle_name.data(),
   15958                key_handle_name.size());
   15959   handle_section_bytes += key_handle_bytes;
   15960   command_size += key_handle_bytes.size();
   15961   hash->Update(in_point_bytes.data(),
   15962                in_point_bytes.size());
   15963   parameter_section_bytes += in_point_bytes;
   15964   command_size += in_point_bytes.size();
   15965   std::string command_hash(32, 0);
   15966   hash->Finish(string_as_array(&command_hash), command_hash.size());
   15967   std::string authorization_section_bytes;
   15968   std::string authorization_size_bytes;
   15969   if (authorization_delegate) {
   15970     if (!authorization_delegate->GetCommandAuthorization(
   15971         command_hash,
   15972         is_command_parameter_encryption_possible,
   15973         is_response_parameter_encryption_possible,
   15974         &authorization_section_bytes)) {
   15975       return TRUNKS_RC_AUTHORIZATION_FAILED;
   15976     }
   15977     if (!authorization_section_bytes.empty()) {
   15978       tag = TPM_ST_SESSIONS;
   15979       std::string tmp;
   15980       rc = Serialize_UINT32(authorization_section_bytes.size(),
   15981                             &authorization_size_bytes);
   15982       if (rc != TPM_RC_SUCCESS) {
   15983         return rc;
   15984       }
   15985       command_size += authorization_size_bytes.size() +
   15986                       authorization_section_bytes.size();
   15987     }
   15988   }
   15989   std::string tag_bytes;
   15990   rc = Serialize_TPMI_ST_COMMAND_TAG(
   15991       tag,
   15992       &tag_bytes);
   15993   if (rc != TPM_RC_SUCCESS) {
   15994     return rc;
   15995   }
   15996   std::string command_size_bytes;
   15997   rc = Serialize_UINT32(
   15998       command_size,
   15999       &command_size_bytes);
   16000   if (rc != TPM_RC_SUCCESS) {
   16001     return rc;
   16002   }
   16003   *serialized_command = tag_bytes +
   16004                         command_size_bytes +
   16005                         command_code_bytes +
   16006                         handle_section_bytes +
   16007                         authorization_size_bytes +
   16008                         authorization_section_bytes +
   16009                         parameter_section_bytes;
   16010   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   16011   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   16012                                             serialized_command->size());
   16013   return TPM_RC_SUCCESS;
   16014 }
   16015 
   16016 TPM_RC Tpm::ParseResponse_ECDH_ZGen(
   16017       const std::string& response,
   16018       TPM2B_ECC_POINT* out_point,
   16019       AuthorizationDelegate* authorization_delegate) {
   16020   VLOG(3) << __func__;
   16021   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   16022   TPM_RC rc = TPM_RC_SUCCESS;
   16023   std::string buffer(response);
   16024   TPM_ST tag;
   16025   std::string tag_bytes;
   16026   rc = Parse_TPM_ST(
   16027       &buffer,
   16028       &tag,
   16029       &tag_bytes);
   16030   if (rc != TPM_RC_SUCCESS) {
   16031     return rc;
   16032   }
   16033   UINT32 response_size;
   16034   std::string response_size_bytes;
   16035   rc = Parse_UINT32(
   16036       &buffer,
   16037       &response_size,
   16038       &response_size_bytes);
   16039   if (rc != TPM_RC_SUCCESS) {
   16040     return rc;
   16041   }
   16042   TPM_RC response_code;
   16043   std::string response_code_bytes;
   16044   rc = Parse_TPM_RC(
   16045       &buffer,
   16046       &response_code,
   16047       &response_code_bytes);
   16048   if (rc != TPM_RC_SUCCESS) {
   16049     return rc;
   16050   }
   16051   if (response_size != response.size()) {
   16052     return TPM_RC_SIZE;
   16053   }
   16054   if (response_code != TPM_RC_SUCCESS) {
   16055     return response_code;
   16056   }
   16057   TPM_CC command_code = TPM_CC_ECDH_ZGen;
   16058   std::string command_code_bytes;
   16059   rc = Serialize_TPM_CC(
   16060       command_code,
   16061       &command_code_bytes);
   16062   if (rc != TPM_RC_SUCCESS) {
   16063     return rc;
   16064   }
   16065   std::string authorization_section_bytes;
   16066   if (tag == TPM_ST_SESSIONS) {
   16067     UINT32 parameter_section_size = buffer.size();
   16068     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   16069     if (rc != TPM_RC_SUCCESS) {
   16070       return rc;
   16071     }
   16072     if (parameter_section_size > buffer.size()) {
   16073       return TPM_RC_INSUFFICIENT;
   16074     }
   16075     authorization_section_bytes = buffer.substr(parameter_section_size);
   16076     // Keep the parameter section in |buffer|.
   16077     buffer.erase(parameter_section_size);
   16078   }
   16079   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   16080       crypto::SecureHash::SHA256));
   16081   hash->Update(response_code_bytes.data(),
   16082                response_code_bytes.size());
   16083   hash->Update(command_code_bytes.data(),
   16084                command_code_bytes.size());
   16085   hash->Update(buffer.data(),
   16086                buffer.size());
   16087   std::string response_hash(32, 0);
   16088   hash->Finish(string_as_array(&response_hash), response_hash.size());
   16089   if (tag == TPM_ST_SESSIONS) {
   16090     CHECK(authorization_delegate) << "Authorization delegate missing!";
   16091     if (!authorization_delegate->CheckResponseAuthorization(
   16092         response_hash,
   16093         authorization_section_bytes)) {
   16094       return TRUNKS_RC_AUTHORIZATION_FAILED;
   16095     }
   16096   }
   16097   std::string out_point_bytes;
   16098   rc = Parse_TPM2B_ECC_POINT(
   16099       &buffer,
   16100       out_point,
   16101       &out_point_bytes);
   16102   if (rc != TPM_RC_SUCCESS) {
   16103     return rc;
   16104   }
   16105   if (tag == TPM_ST_SESSIONS) {
   16106     CHECK(authorization_delegate) << "Authorization delegate missing!";
   16107     // Decrypt just the parameter data, not the size.
   16108     std::string tmp = out_point_bytes.substr(2);
   16109     if (!authorization_delegate->DecryptResponseParameter(&tmp)) {
   16110       return TRUNKS_RC_ENCRYPTION_FAILED;
   16111     }
   16112     out_point_bytes.replace(2, std::string::npos, tmp);
   16113     rc = Parse_TPM2B_ECC_POINT(
   16114         &out_point_bytes,
   16115         out_point,
   16116         nullptr);
   16117     if (rc != TPM_RC_SUCCESS) {
   16118       return rc;
   16119     }
   16120   }
   16121   return TPM_RC_SUCCESS;
   16122 }
   16123 
   16124 void ECDH_ZGenErrorCallback(
   16125     const Tpm::ECDH_ZGenResponse& callback,
   16126     TPM_RC response_code) {
   16127   VLOG(1) << __func__;
   16128   callback.Run(response_code,
   16129                TPM2B_ECC_POINT());
   16130 }
   16131 
   16132 void ECDH_ZGenResponseParser(
   16133     const Tpm::ECDH_ZGenResponse& callback,
   16134     AuthorizationDelegate* authorization_delegate,
   16135     const std::string& response) {
   16136   VLOG(1) << __func__;
   16137   base::Callback<void(TPM_RC)> error_reporter =
   16138       base::Bind(ECDH_ZGenErrorCallback, callback);
   16139   TPM2B_ECC_POINT out_point;
   16140   TPM_RC rc = Tpm::ParseResponse_ECDH_ZGen(
   16141       response,
   16142       &out_point,
   16143       authorization_delegate);
   16144   if (rc != TPM_RC_SUCCESS) {
   16145     error_reporter.Run(rc);
   16146     return;
   16147   }
   16148   callback.Run(
   16149       rc,
   16150       out_point);
   16151 }
   16152 
   16153 void Tpm::ECDH_ZGen(
   16154       const TPMI_DH_OBJECT& key_handle,
   16155       const std::string& key_handle_name,
   16156       const TPM2B_ECC_POINT& in_point,
   16157       AuthorizationDelegate* authorization_delegate,
   16158       const ECDH_ZGenResponse& callback) {
   16159   VLOG(1) << __func__;
   16160   base::Callback<void(TPM_RC)> error_reporter =
   16161       base::Bind(ECDH_ZGenErrorCallback, callback);
   16162   base::Callback<void(const std::string&)> parser =
   16163       base::Bind(ECDH_ZGenResponseParser,
   16164                  callback,
   16165                  authorization_delegate);
   16166   std::string command;
   16167   TPM_RC rc = SerializeCommand_ECDH_ZGen(
   16168       key_handle,
   16169       key_handle_name,
   16170       in_point,
   16171       &command,
   16172       authorization_delegate);
   16173   if (rc != TPM_RC_SUCCESS) {
   16174     error_reporter.Run(rc);
   16175     return;
   16176   }
   16177   transceiver_->SendCommand(command, parser);
   16178 }
   16179 
   16180 TPM_RC Tpm::ECDH_ZGenSync(
   16181       const TPMI_DH_OBJECT& key_handle,
   16182       const std::string& key_handle_name,
   16183       const TPM2B_ECC_POINT& in_point,
   16184       TPM2B_ECC_POINT* out_point,
   16185       AuthorizationDelegate* authorization_delegate) {
   16186   VLOG(1) << __func__;
   16187   std::string command;
   16188   TPM_RC rc = SerializeCommand_ECDH_ZGen(
   16189       key_handle,
   16190       key_handle_name,
   16191       in_point,
   16192       &command,
   16193       authorization_delegate);
   16194   if (rc != TPM_RC_SUCCESS) {
   16195     return rc;
   16196   }
   16197   std::string response = transceiver_->SendCommandAndWait(command);
   16198   rc = ParseResponse_ECDH_ZGen(
   16199       response,
   16200       out_point,
   16201       authorization_delegate);
   16202   return rc;
   16203 }
   16204 
   16205 TPM_RC Tpm::SerializeCommand_ECC_Parameters(
   16206       const TPMI_ECC_CURVE& curve_id,
   16207       std::string* serialized_command,
   16208       AuthorizationDelegate* authorization_delegate) {
   16209   VLOG(3) << __func__;
   16210   TPM_RC rc = TPM_RC_SUCCESS;
   16211   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   16212   UINT32 command_size = 10;  // Header size.
   16213   std::string handle_section_bytes;
   16214   std::string parameter_section_bytes;
   16215   TPM_CC command_code = TPM_CC_ECC_Parameters;
   16216   bool is_command_parameter_encryption_possible = false;
   16217   bool is_response_parameter_encryption_possible = false;
   16218   std::string command_code_bytes;
   16219   rc = Serialize_TPM_CC(
   16220       command_code,
   16221       &command_code_bytes);
   16222   if (rc != TPM_RC_SUCCESS) {
   16223     return rc;
   16224   }
   16225   std::string curve_id_bytes;
   16226   rc = Serialize_TPMI_ECC_CURVE(
   16227       curve_id,
   16228       &curve_id_bytes);
   16229   if (rc != TPM_RC_SUCCESS) {
   16230     return rc;
   16231   }
   16232   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   16233       crypto::SecureHash::SHA256));
   16234   hash->Update(command_code_bytes.data(),
   16235                command_code_bytes.size());
   16236   hash->Update(curve_id_bytes.data(),
   16237                curve_id_bytes.size());
   16238   parameter_section_bytes += curve_id_bytes;
   16239   command_size += curve_id_bytes.size();
   16240   std::string command_hash(32, 0);
   16241   hash->Finish(string_as_array(&command_hash), command_hash.size());
   16242   std::string authorization_section_bytes;
   16243   std::string authorization_size_bytes;
   16244   if (authorization_delegate) {
   16245     if (!authorization_delegate->GetCommandAuthorization(
   16246         command_hash,
   16247         is_command_parameter_encryption_possible,
   16248         is_response_parameter_encryption_possible,
   16249         &authorization_section_bytes)) {
   16250       return TRUNKS_RC_AUTHORIZATION_FAILED;
   16251     }
   16252     if (!authorization_section_bytes.empty()) {
   16253       tag = TPM_ST_SESSIONS;
   16254       std::string tmp;
   16255       rc = Serialize_UINT32(authorization_section_bytes.size(),
   16256                             &authorization_size_bytes);
   16257       if (rc != TPM_RC_SUCCESS) {
   16258         return rc;
   16259       }
   16260       command_size += authorization_size_bytes.size() +
   16261                       authorization_section_bytes.size();
   16262     }
   16263   }
   16264   std::string tag_bytes;
   16265   rc = Serialize_TPMI_ST_COMMAND_TAG(
   16266       tag,
   16267       &tag_bytes);
   16268   if (rc != TPM_RC_SUCCESS) {
   16269     return rc;
   16270   }
   16271   std::string command_size_bytes;
   16272   rc = Serialize_UINT32(
   16273       command_size,
   16274       &command_size_bytes);
   16275   if (rc != TPM_RC_SUCCESS) {
   16276     return rc;
   16277   }
   16278   *serialized_command = tag_bytes +
   16279                         command_size_bytes +
   16280                         command_code_bytes +
   16281                         handle_section_bytes +
   16282                         authorization_size_bytes +
   16283                         authorization_section_bytes +
   16284                         parameter_section_bytes;
   16285   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   16286   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   16287                                             serialized_command->size());
   16288   return TPM_RC_SUCCESS;
   16289 }
   16290 
   16291 TPM_RC Tpm::ParseResponse_ECC_Parameters(
   16292       const std::string& response,
   16293       TPMS_ALGORITHM_DETAIL_ECC* parameters,
   16294       AuthorizationDelegate* authorization_delegate) {
   16295   VLOG(3) << __func__;
   16296   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   16297   TPM_RC rc = TPM_RC_SUCCESS;
   16298   std::string buffer(response);
   16299   TPM_ST tag;
   16300   std::string tag_bytes;
   16301   rc = Parse_TPM_ST(
   16302       &buffer,
   16303       &tag,
   16304       &tag_bytes);
   16305   if (rc != TPM_RC_SUCCESS) {
   16306     return rc;
   16307   }
   16308   UINT32 response_size;
   16309   std::string response_size_bytes;
   16310   rc = Parse_UINT32(
   16311       &buffer,
   16312       &response_size,
   16313       &response_size_bytes);
   16314   if (rc != TPM_RC_SUCCESS) {
   16315     return rc;
   16316   }
   16317   TPM_RC response_code;
   16318   std::string response_code_bytes;
   16319   rc = Parse_TPM_RC(
   16320       &buffer,
   16321       &response_code,
   16322       &response_code_bytes);
   16323   if (rc != TPM_RC_SUCCESS) {
   16324     return rc;
   16325   }
   16326   if (response_size != response.size()) {
   16327     return TPM_RC_SIZE;
   16328   }
   16329   if (response_code != TPM_RC_SUCCESS) {
   16330     return response_code;
   16331   }
   16332   TPM_CC command_code = TPM_CC_ECC_Parameters;
   16333   std::string command_code_bytes;
   16334   rc = Serialize_TPM_CC(
   16335       command_code,
   16336       &command_code_bytes);
   16337   if (rc != TPM_RC_SUCCESS) {
   16338     return rc;
   16339   }
   16340   std::string authorization_section_bytes;
   16341   if (tag == TPM_ST_SESSIONS) {
   16342     UINT32 parameter_section_size = buffer.size();
   16343     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   16344     if (rc != TPM_RC_SUCCESS) {
   16345       return rc;
   16346     }
   16347     if (parameter_section_size > buffer.size()) {
   16348       return TPM_RC_INSUFFICIENT;
   16349     }
   16350     authorization_section_bytes = buffer.substr(parameter_section_size);
   16351     // Keep the parameter section in |buffer|.
   16352     buffer.erase(parameter_section_size);
   16353   }
   16354   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   16355       crypto::SecureHash::SHA256));
   16356   hash->Update(response_code_bytes.data(),
   16357                response_code_bytes.size());
   16358   hash->Update(command_code_bytes.data(),
   16359                command_code_bytes.size());
   16360   hash->Update(buffer.data(),
   16361                buffer.size());
   16362   std::string response_hash(32, 0);
   16363   hash->Finish(string_as_array(&response_hash), response_hash.size());
   16364   if (tag == TPM_ST_SESSIONS) {
   16365     CHECK(authorization_delegate) << "Authorization delegate missing!";
   16366     if (!authorization_delegate->CheckResponseAuthorization(
   16367         response_hash,
   16368         authorization_section_bytes)) {
   16369       return TRUNKS_RC_AUTHORIZATION_FAILED;
   16370     }
   16371   }
   16372   std::string parameters_bytes;
   16373   rc = Parse_TPMS_ALGORITHM_DETAIL_ECC(
   16374       &buffer,
   16375       parameters,
   16376       &parameters_bytes);
   16377   if (rc != TPM_RC_SUCCESS) {
   16378     return rc;
   16379   }
   16380   return TPM_RC_SUCCESS;
   16381 }
   16382 
   16383 void ECC_ParametersErrorCallback(
   16384     const Tpm::ECC_ParametersResponse& callback,
   16385     TPM_RC response_code) {
   16386   VLOG(1) << __func__;
   16387   callback.Run(response_code,
   16388                TPMS_ALGORITHM_DETAIL_ECC());
   16389 }
   16390 
   16391 void ECC_ParametersResponseParser(
   16392     const Tpm::ECC_ParametersResponse& callback,
   16393     AuthorizationDelegate* authorization_delegate,
   16394     const std::string& response) {
   16395   VLOG(1) << __func__;
   16396   base::Callback<void(TPM_RC)> error_reporter =
   16397       base::Bind(ECC_ParametersErrorCallback, callback);
   16398   TPMS_ALGORITHM_DETAIL_ECC parameters;
   16399   TPM_RC rc = Tpm::ParseResponse_ECC_Parameters(
   16400       response,
   16401       &parameters,
   16402       authorization_delegate);
   16403   if (rc != TPM_RC_SUCCESS) {
   16404     error_reporter.Run(rc);
   16405     return;
   16406   }
   16407   callback.Run(
   16408       rc,
   16409       parameters);
   16410 }
   16411 
   16412 void Tpm::ECC_Parameters(
   16413       const TPMI_ECC_CURVE& curve_id,
   16414       AuthorizationDelegate* authorization_delegate,
   16415       const ECC_ParametersResponse& callback) {
   16416   VLOG(1) << __func__;
   16417   base::Callback<void(TPM_RC)> error_reporter =
   16418       base::Bind(ECC_ParametersErrorCallback, callback);
   16419   base::Callback<void(const std::string&)> parser =
   16420       base::Bind(ECC_ParametersResponseParser,
   16421                  callback,
   16422                  authorization_delegate);
   16423   std::string command;
   16424   TPM_RC rc = SerializeCommand_ECC_Parameters(
   16425       curve_id,
   16426       &command,
   16427       authorization_delegate);
   16428   if (rc != TPM_RC_SUCCESS) {
   16429     error_reporter.Run(rc);
   16430     return;
   16431   }
   16432   transceiver_->SendCommand(command, parser);
   16433 }
   16434 
   16435 TPM_RC Tpm::ECC_ParametersSync(
   16436       const TPMI_ECC_CURVE& curve_id,
   16437       TPMS_ALGORITHM_DETAIL_ECC* parameters,
   16438       AuthorizationDelegate* authorization_delegate) {
   16439   VLOG(1) << __func__;
   16440   std::string command;
   16441   TPM_RC rc = SerializeCommand_ECC_Parameters(
   16442       curve_id,
   16443       &command,
   16444       authorization_delegate);
   16445   if (rc != TPM_RC_SUCCESS) {
   16446     return rc;
   16447   }
   16448   std::string response = transceiver_->SendCommandAndWait(command);
   16449   rc = ParseResponse_ECC_Parameters(
   16450       response,
   16451       parameters,
   16452       authorization_delegate);
   16453   return rc;
   16454 }
   16455 
   16456 TPM_RC Tpm::SerializeCommand_ZGen_2Phase(
   16457       const TPMI_DH_OBJECT& key_a,
   16458       const std::string& key_a_name,
   16459       const TPM2B_ECC_POINT& in_qs_b,
   16460       const TPM2B_ECC_POINT& in_qe_b,
   16461       const TPMI_ECC_KEY_EXCHANGE& in_scheme,
   16462       const UINT16& counter,
   16463       std::string* serialized_command,
   16464       AuthorizationDelegate* authorization_delegate) {
   16465   VLOG(3) << __func__;
   16466   TPM_RC rc = TPM_RC_SUCCESS;
   16467   TPMI_ST_COMMAND_TAG tag = TPM_ST_NO_SESSIONS;
   16468   UINT32 command_size = 10;  // Header size.
   16469   std::string handle_section_bytes;
   16470   std::string parameter_section_bytes;
   16471   TPM_CC command_code = TPM_CC_ZGen_2Phase;
   16472   bool is_command_parameter_encryption_possible = true;
   16473   bool is_response_parameter_encryption_possible = true;
   16474   std::string command_code_bytes;
   16475   rc = Serialize_TPM_CC(
   16476       command_code,
   16477       &command_code_bytes);
   16478   if (rc != TPM_RC_SUCCESS) {
   16479     return rc;
   16480   }
   16481   std::string key_a_bytes;
   16482   rc = Serialize_TPMI_DH_OBJECT(
   16483       key_a,
   16484       &key_a_bytes);
   16485   if (rc != TPM_RC_SUCCESS) {
   16486     return rc;
   16487   }
   16488   std::string in_qs_b_bytes;
   16489   rc = Serialize_TPM2B_ECC_POINT(
   16490       in_qs_b,
   16491       &in_qs_b_bytes);
   16492   if (rc != TPM_RC_SUCCESS) {
   16493     return rc;
   16494   }
   16495   std::string in_qe_b_bytes;
   16496   rc = Serialize_TPM2B_ECC_POINT(
   16497       in_qe_b,
   16498       &in_qe_b_bytes);
   16499   if (rc != TPM_RC_SUCCESS) {
   16500     return rc;
   16501   }
   16502   std::string in_scheme_bytes;
   16503   rc = Serialize_TPMI_ECC_KEY_EXCHANGE(
   16504       in_scheme,
   16505       &in_scheme_bytes);
   16506   if (rc != TPM_RC_SUCCESS) {
   16507     return rc;
   16508   }
   16509   std::string counter_bytes;
   16510   rc = Serialize_UINT16(
   16511       counter,
   16512       &counter_bytes);
   16513   if (rc != TPM_RC_SUCCESS) {
   16514     return rc;
   16515   }
   16516   if (authorization_delegate) {
   16517     // Encrypt just the parameter data, not the size.
   16518     std::string tmp = in_qs_b_bytes.substr(2);
   16519     if (!authorization_delegate->EncryptCommandParameter(&tmp)) {
   16520       return TRUNKS_RC_ENCRYPTION_FAILED;
   16521     }
   16522     in_qs_b_bytes.replace(2, std::string::npos, tmp);
   16523   }
   16524   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   16525       crypto::SecureHash::SHA256));
   16526   hash->Update(command_code_bytes.data(),
   16527                command_code_bytes.size());
   16528   hash->Update(key_a_name.data(),
   16529                key_a_name.size());
   16530   handle_section_bytes += key_a_bytes;
   16531   command_size += key_a_bytes.size();
   16532   hash->Update(in_qs_b_bytes.data(),
   16533                in_qs_b_bytes.size());
   16534   parameter_section_bytes += in_qs_b_bytes;
   16535   command_size += in_qs_b_bytes.size();
   16536   hash->Update(in_qe_b_bytes.data(),
   16537                in_qe_b_bytes.size());
   16538   parameter_section_bytes += in_qe_b_bytes;
   16539   command_size += in_qe_b_bytes.size();
   16540   hash->Update(in_scheme_bytes.data(),
   16541                in_scheme_bytes.size());
   16542   parameter_section_bytes += in_scheme_bytes;
   16543   command_size += in_scheme_bytes.size();
   16544   hash->Update(counter_bytes.data(),
   16545                counter_bytes.size());
   16546   parameter_section_bytes += counter_bytes;
   16547   command_size += counter_bytes.size();
   16548   std::string command_hash(32, 0);
   16549   hash->Finish(string_as_array(&command_hash), command_hash.size());
   16550   std::string authorization_section_bytes;
   16551   std::string authorization_size_bytes;
   16552   if (authorization_delegate) {
   16553     if (!authorization_delegate->GetCommandAuthorization(
   16554         command_hash,
   16555         is_command_parameter_encryption_possible,
   16556         is_response_parameter_encryption_possible,
   16557         &authorization_section_bytes)) {
   16558       return TRUNKS_RC_AUTHORIZATION_FAILED;
   16559     }
   16560     if (!authorization_section_bytes.empty()) {
   16561       tag = TPM_ST_SESSIONS;
   16562       std::string tmp;
   16563       rc = Serialize_UINT32(authorization_section_bytes.size(),
   16564                             &authorization_size_bytes);
   16565       if (rc != TPM_RC_SUCCESS) {
   16566         return rc;
   16567       }
   16568       command_size += authorization_size_bytes.size() +
   16569                       authorization_section_bytes.size();
   16570     }
   16571   }
   16572   std::string tag_bytes;
   16573   rc = Serialize_TPMI_ST_COMMAND_TAG(
   16574       tag,
   16575       &tag_bytes);
   16576   if (rc != TPM_RC_SUCCESS) {
   16577     return rc;
   16578   }
   16579   std::string command_size_bytes;
   16580   rc = Serialize_UINT32(
   16581       command_size,
   16582       &command_size_bytes);
   16583   if (rc != TPM_RC_SUCCESS) {
   16584     return rc;
   16585   }
   16586   *serialized_command = tag_bytes +
   16587                         command_size_bytes +
   16588                         command_code_bytes +
   16589                         handle_section_bytes +
   16590                         authorization_size_bytes +
   16591                         authorization_section_bytes +
   16592                         parameter_section_bytes;
   16593   CHECK(serialized_command->size() == command_size) << "Command size mismatch!";
   16594   VLOG(2) << "Command: " << base::HexEncode(serialized_command->data(),
   16595                                             serialized_command->size());
   16596   return TPM_RC_SUCCESS;
   16597 }
   16598 
   16599 TPM_RC Tpm::ParseResponse_ZGen_2Phase(
   16600       const std::string& response,
   16601       TPM2B_ECC_POINT* out_z1,
   16602       TPM2B_ECC_POINT* out_z2,
   16603       AuthorizationDelegate* authorization_delegate) {
   16604   VLOG(3) << __func__;
   16605   VLOG(2) << "Response: " << base::HexEncode(response.data(), response.size());
   16606   TPM_RC rc = TPM_RC_SUCCESS;
   16607   std::string buffer(response);
   16608   TPM_ST tag;
   16609   std::string tag_bytes;
   16610   rc = Parse_TPM_ST(
   16611       &buffer,
   16612       &tag,
   16613       &tag_bytes);
   16614   if (rc != TPM_RC_SUCCESS) {
   16615     return rc;
   16616   }
   16617   UINT32 response_size;
   16618   std::string response_size_bytes;
   16619   rc = Parse_UINT32(
   16620       &buffer,
   16621       &response_size,
   16622       &response_size_bytes);
   16623   if (rc != TPM_RC_SUCCESS) {
   16624     return rc;
   16625   }
   16626   TPM_RC response_code;
   16627   std::string response_code_bytes;
   16628   rc = Parse_TPM_RC(
   16629       &buffer,
   16630       &response_code,
   16631       &response_code_bytes);
   16632   if (rc != TPM_RC_SUCCESS) {
   16633     return rc;
   16634   }
   16635   if (response_size != response.size()) {
   16636     return TPM_RC_SIZE;
   16637   }
   16638   if (response_code != TPM_RC_SUCCESS) {
   16639     return response_code;
   16640   }
   16641   TPM_CC command_code = TPM_CC_ZGen_2Phase;
   16642   std::string command_code_bytes;
   16643   rc = Serialize_TPM_CC(
   16644       command_code,
   16645       &command_code_bytes);
   16646   if (rc != TPM_RC_SUCCESS) {
   16647     return rc;
   16648   }
   16649   std::string authorization_section_bytes;
   16650   if (tag == TPM_ST_SESSIONS) {
   16651     UINT32 parameter_section_size = buffer.size();
   16652     rc = Parse_UINT32(&buffer, &parameter_section_size, nullptr);
   16653     if (rc != TPM_RC_SUCCESS) {
   16654       return rc;
   16655     }
   16656     if (parameter_section_size > buffer.size()) {
   16657       return TPM_RC_INSUFFICIENT;
   16658     }
   16659     authorization_section_bytes = buffer.substr(parameter_section_size);
   16660     // Keep the parameter section in |buffer|.
   16661     buffer.erase(parameter_section_size);
   16662   }
   16663   scoped_ptr<crypto::SecureHash> hash(crypto::SecureHash::Create(
   16664       crypto::SecureHash::SHA256));
   16665   hash->Update(response_code_bytes.data(),
   16666                response_code_bytes.size());
   16667   hash->Update(command_code_bytes.data(),
   16668                command_code_bytes.size());
   16669   hash->Update(buffer.data(),
   16670                buffer.size());
   16671   std::string response_hash(32, 0);
   16672   hash->Finish(string_as_array(&response_hash), response_hash.size());
   16673   if (tag == TPM_ST_SESSIONS) {
   16674     CHECK(authorization_delegate) << "Authorization delegate missing!";
   16675     if (!authorization_delegate->CheckResponseAuthorization(
   16676         response_hash,
   16677         authorization_section_bytes)) {
   16678       return TRUNKS_RC_AUTHORIZATION_FAILED;
   16679     }
   16680   }
   16681   std::string out_z1_bytes;
   16682   rc = Parse_TPM2B_ECC_POINT(
   16683       &buffer,
   16684       out_z1,
   16685       &out_z1_bytes);
   16686   if (rc != TPM_RC_SUCCESS) {
   16687     return rc;
   16688   }
   16689   std::string out_z2_bytes;
   16690   rc = Parse_TPM2B_ECC_POINT(
   16691       &buffer,
   16692       out_z2,
   16693       &out_z2_bytes);
   16694   if (rc != TPM_RC_SUCCESS) {
   16695     return rc;
   16696   }
   16697   if (tag == TPM_ST_SE