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 <keymaster/serializable.h>
19 
20 #include "host/commands/secure_env/tpm_resource_manager.h"
21 
22 /**
23  * A keymaster::Serializable that wraps another keymaster::Serializable,
24  * protecting it from tampering while it is stored elsewhere. This stores
25  * the serialized data of the other type together with a signature over that
26  * serialized data. When deserializing, it will attempt to make the same
27  * signature over the data. If the signature or data has been tampered with,
28  * the signatures won't match and it won't attempt to deserialize the wrapped
29  * type.
30  *
31  * The serialization format is:
32  * [uint32_t: wrapped_size] [wrapped_data]
33  * [uint32_t: signature_size] [signature_data]
34  *
35  * While this class currently assumes all signatures will use the same key
36  * and algorithm and therefore be the same size, the serialization format is
37  * future-proof to accommodate signature changes.
38  */
39 class HmacSerializable : public keymaster::Serializable {
40 public:
41   HmacSerializable(TpmResourceManager*,
42                    std::function<TpmObjectSlot(TpmResourceManager*)>,
43                    uint32_t digest_size,
44                    Serializable*);
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   TpmResourceManager* resource_manager_;
51   std::function<TpmObjectSlot(TpmResourceManager*)> signing_key_fn_;
52   uint32_t digest_size_;
53   keymaster::Serializable* wrapped_;
54 };
55