1 //
2 // Copyright (C) 2020 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 #include "tpm_serialize.h"
17 
18 #include <cstring>
19 
20 #include <android-base/logging.h>
21 #include "tss2/tss2_mu.h"
22 #include "tss2/tss2_rc.h"
23 
24 namespace cuttlefish {
25 
26 template<typename T>
27 int MarshalFn = 0; // Break code without an explicit specialization.
28 
29 template<typename T>
30 int UnmarshalFn = 0; // Break code without an explicit specialization.
31 
32 template<>
33 auto MarshalFn<TPM2B_PRIVATE> = Tss2_MU_TPM2B_PRIVATE_Marshal;
34 
35 template<>
36 auto UnmarshalFn<TPM2B_PRIVATE> = Tss2_MU_TPM2B_PRIVATE_Unmarshal;
37 
38 template<>
39 auto MarshalFn<TPM2B_PUBLIC> = Tss2_MU_TPM2B_PUBLIC_Marshal;
40 
41 template<>
42 auto UnmarshalFn<TPM2B_PUBLIC> = Tss2_MU_TPM2B_PUBLIC_Unmarshal;
43 
44 template<typename T>
TpmSerializable(T * instance)45 TpmSerializable<T>::TpmSerializable(T* instance) : instance_(instance) {}
46 
47 template<typename T>
SerializedSize() const48 size_t TpmSerializable<T>::SerializedSize() const {
49   std::size_t size = 0;
50   auto rc = MarshalFn<T>(instance_, nullptr, sizeof(T), &size);
51   if (rc != TPM2_RC_SUCCESS) {
52     LOG(ERROR) << "tss2 marshalling failed: " << Tss2_RC_Decode(rc)
53                 << "(" << rc << ")";
54     return -1;
55   }
56   return size;
57 }
58 
59 template<typename T>
Serialize(uint8_t * buf,const uint8_t * end) const60 uint8_t* TpmSerializable<T>::Serialize(uint8_t* buf, const uint8_t* end) const {
61   std::size_t offset = 0;
62   auto rc = MarshalFn<T>(instance_, buf, end - buf, &offset);
63   if (rc != TPM2_RC_SUCCESS) {
64     LOG(ERROR) << "tss2 marshalling failed: " << Tss2_RC_Decode(rc)
65                 << "(" << rc << ")";
66     return buf;
67   }
68   return buf + offset;
69 }
70 
71 template<typename T>
Deserialize(const uint8_t ** buf_ptr,const uint8_t * end)72 bool TpmSerializable<T>::Deserialize(
73     const uint8_t** buf_ptr, const uint8_t* end) {
74   std::size_t offset = 0;
75   auto rc = UnmarshalFn<T>(*buf_ptr, end - *buf_ptr, &offset, instance_);
76   if (rc != TPM2_RC_SUCCESS) {
77     LOG(ERROR) << "tss2 unmarshalling failed: " << Tss2_RC_Decode(rc)
78                 << "(" << rc << ")";
79     return false;
80   }
81   *buf_ptr += offset;
82   return true;
83 }
84 
85 template class TpmSerializable<TPM2B_PRIVATE>;
86 template class TpmSerializable<TPM2B_PUBLIC>;
87 
88 }  // namespace cuttlefish
89