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 #pragma once
17 
18 #include <cstddef>
19 
20 #include "keymaster/serializable.h"
21 #include "tss2/tss2_mu.h"
22 #include "tss2/tss2_rc.h"
23 #include "tss2/tss2_tpm2_types.h"
24 
25 #include <android-base/logging.h>
26 
27 namespace cuttlefish {
28 
29 /**
30  * An implementation of a keymaster::Serializable type that refers to a TPM type
31  * by an unmanaged pointer. When the TpmSerializable serializes or deserializes
32  * data, it loads it from and saves it to the pointed at instance.
33  *
34  * The serialization format is the same as the one used in the command protocol
35  * for TPM messages.
36  *
37  * This is a template class, specialized in the corresponding implementation
38  * file for the TPM types necessary to serialize as part of larger Keymaster
39  * serializable types.
40  */
41 template<typename Type>
42 class TpmSerializable : public keymaster::Serializable {
43 public:
44   TpmSerializable(Type*);
45 
46   size_t SerializedSize() const override;
47   uint8_t* Serialize(uint8_t* buf, const uint8_t* end) const override;
48   bool Deserialize(const uint8_t** buf_ptr, const uint8_t* end) override;
49 private:
50   Type* instance_;
51 };
52 
53 using SerializeTpmKeyPrivate = TpmSerializable<TPM2B_PRIVATE>;
54 using SerializeTpmKeyPublic = TpmSerializable<TPM2B_PUBLIC>;
55 
56 }  // namespace cuttlefish
57