TZ backend helpers
[platform/core/security/key-manager.git] / src / manager / crypto / tz-backend / tz-serializer.h
1 /*
2  *  Copyright (c) 2019-2021 Samsung Electronics Co., Ltd All Rights Reserved
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 /*
17  * @file       tz-serializer.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  * @brief
21  */
22
23 #pragma once
24
25 #include <cstdint>
26 #include <list>
27 #include <memory>
28
29 #include <km_serialization.h>
30
31 #include <tz-backend/tz-memory.h>
32 #include <generic-backend/exception.h>
33 #include <dpl/raw-buffer.h>
34
35 namespace CKM {
36 namespace Crypto {
37 namespace TZ {
38 namespace Internals {
39
40 class TZSerializable {
41 public:
42         TZSerializable() {}
43         virtual ~TZSerializable() {}
44         TZSerializable(const TZSerializable&) = delete;
45         TZSerializable& operator=(const TZSerializable&) = delete;
46
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;
52 };
53
54
55 class TZSerializableBinary : public TZSerializable {
56 public:
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;
63 private:
64         KM_BinaryData m_data;
65         bool m_isSizeFixed;
66         uint32_t m_expectedSize;
67 };
68
69
70 class TZSerializablePwdData : public TZSerializable {
71 public:
72         TZSerializablePwdData(const RawBuffer &pwd,
73                               const RawBuffer &iv,
74                               uint32_t tagSizeBits,
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;
79 private:
80         KM_PwdData m_data;
81 };
82
83
84 class TZSerializableFlag : public TZSerializable {
85 public:
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;
92 private:
93         uint32_t m_flag;
94 };
95
96
97 class TZSerializer {
98 public:
99         TZSerializer() : m_memorySize(0) {}
100         ~TZSerializer() {}
101         TZSerializer(const TZSerializer&) = delete;
102         TZSerializer(TZSerializer&&) = default;
103         TZSerializer& operator=(const TZSerializer&) = delete;
104         TZSerializer& operator=(TZSerializer&&) = default;
105
106         void Push(TZSerializable *serializable);
107
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);
113
114 private:
115         std::list<std::unique_ptr<TZSerializable>> m_serializables;
116         uint32_t m_memorySize;
117 };
118
119 template <typename T>
120 void TZSerializer::Pull(T &data)
121 {
122         if (m_serializables.empty()) {
123                 ThrowErr(Exc::Crypto::InternalError, "No more serializables to extract");
124         }
125
126         m_serializables.front()->Pull(data);
127         m_serializables.pop_front();
128 }
129
130 } // namespace Internals
131 } // namespace TZ
132 } // namespace Crypto
133 } // namespace CKM