// // Copyright (C) 2020 The Android Open Source Project // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "tpm_serialize.h" #include #include #include "tss2/tss2_mu.h" #include "tss2/tss2_rc.h" template int MarshalFn = 0; // Break code without an explicit specialization. template int UnmarshalFn = 0; // Break code without an explicit specialization. template<> auto MarshalFn = Tss2_MU_TPM2B_PRIVATE_Marshal; template<> auto UnmarshalFn = Tss2_MU_TPM2B_PRIVATE_Unmarshal; template<> auto MarshalFn = Tss2_MU_TPM2B_PUBLIC_Marshal; template<> auto UnmarshalFn = Tss2_MU_TPM2B_PUBLIC_Unmarshal; template TpmSerializable::TpmSerializable(T* instance) : instance_(instance) {} template size_t TpmSerializable::SerializedSize() const { std::size_t size = 0; auto rc = MarshalFn(instance_, nullptr, sizeof(T), &size); if (rc != TPM2_RC_SUCCESS) { LOG(ERROR) << "tss2 marshalling failed: " << Tss2_RC_Decode(rc) << "(" << rc << ")"; return -1; } return size; } template uint8_t* TpmSerializable::Serialize(uint8_t* buf, const uint8_t* end) const { std::size_t offset = 0; auto rc = MarshalFn(instance_, buf, end - buf, &offset); if (rc != TPM2_RC_SUCCESS) { LOG(ERROR) << "tss2 marshalling failed: " << Tss2_RC_Decode(rc) << "(" << rc << ")"; return buf; } return buf + offset; } template bool TpmSerializable::Deserialize( const uint8_t** buf_ptr, const uint8_t* end) { std::size_t offset = 0; auto rc = UnmarshalFn(*buf_ptr, end - *buf_ptr, &offset, instance_); if (rc != TPM2_RC_SUCCESS) { LOG(ERROR) << "tss2 unmarshalling failed: " << Tss2_RC_Decode(rc) << "(" << rc << ")"; return false; } *buf_ptr += offset; return true; } template class TpmSerializable; template class TpmSerializable;