2 * Copyright (c) 2019-2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file tz-serializer.h
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
29 #include <km_serialization.h>
31 #include <tz-backend/tz-memory.h>
32 #include <generic-backend/exception.h>
33 #include <dpl/raw-buffer.h>
40 class TZSerializable {
43 virtual ~TZSerializable() {}
44 TZSerializable(const TZSerializable&) = delete;
45 TZSerializable& operator=(const TZSerializable&) = delete;
47 virtual uint32_t GetSize() const = 0;
48 virtual int Serialize(void **buffer, uint32_t *size_guard) const = 0;
49 virtual int Deserialize(void **buffer, uint32_t *size_guard) = 0;
50 virtual void Pull(RawBuffer &buffer) const;
51 virtual void Pull(uint32_t &flag) const;
55 class TZSerializableBinary : public TZSerializable {
57 explicit TZSerializableBinary(uint32_t data_size, bool is_size_fixed = true);
58 explicit TZSerializableBinary(const RawBuffer &data);
59 uint32_t GetSize() const override;
60 int Serialize(void **buffer, uint32_t *size_guard) const override;
61 int Deserialize(void **buffer, uint32_t *size_guard) override;
62 void Pull(RawBuffer &buffer) const override;
66 uint32_t m_expectedSize;
70 class TZSerializablePwdData : public TZSerializable {
72 TZSerializablePwdData(const RawBuffer &pwd,
75 const RawBuffer &tag = RawBuffer());
76 uint32_t GetSize() const override;
77 int Serialize(void **buffer, uint32_t *size_guard) const override;
78 int Deserialize(void **buffer, uint32_t *size_guard) override;
84 class TZSerializableFlag : public TZSerializable {
86 TZSerializableFlag() : m_flag(0) {}
87 explicit TZSerializableFlag(uint32_t flag) : m_flag(flag) {}
88 uint32_t GetSize() const override;
89 int Serialize(void **buffer, uint32_t *size_guard) const override;
90 int Deserialize(void **buffer, uint32_t *size_guard) override;
91 void Pull(uint32_t &flag) const override;
99 TZSerializer() : m_memorySize(0) {}
101 TZSerializer(const TZSerializer&) = delete;
102 TZSerializer(TZSerializer&&) = default;
103 TZSerializer& operator=(const TZSerializer&) = delete;
104 TZSerializer& operator=(TZSerializer&&) = default;
106 void Push(TZSerializable *serializable);
108 template <typename T>
109 void Pull(T &buffer);
110 uint32_t GetSize() const { return m_memorySize; }
111 void Serialize(TrustZoneMemory &memory) const;
112 void Deserialize(const TrustZoneMemory &memory);
115 std::list<std::unique_ptr<TZSerializable>> m_serializables;
116 uint32_t m_memorySize;
119 template <typename T>
120 void TZSerializer::Pull(T &data)
122 if (m_serializables.empty()) {
123 ThrowErr(Exc::Crypto::InternalError, "No more serializables to extract");
126 m_serializables.front()->Pull(data);
127 m_serializables.pop_front();
130 } // namespace Internals
132 } // namespace Crypto