Implement public key extraction in TZ backend
[platform/core/security/key-manager.git] / src / manager / crypto / tz-backend / obj.h
1 /*
2  *  Copyright (c) 2015 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       obj.h
18  * @author     Lukasz Kostyra (l.kostyra@samsung.com)
19  * @version    1.0
20  */
21 #pragma once
22
23 #include <generic-backend/gobj.h>
24 #include <generic-backend/gstore.h>
25 #include <generic-backend/gctx.h>
26 #include <data-type.h>
27
28 namespace CKM {
29 namespace Crypto {
30 namespace TZ {
31
32 class Pwd {
33 public:
34         Pwd(Password pwd, RawBuffer iv, RawBuffer tag)
35                 : m_password(pwd.begin(), pwd.end())
36                 , m_iv(std::move(iv))
37                 , m_tag(std::move(tag))
38         {}
39
40         const RawBuffer& getPassword() const
41         {
42                 return m_password;
43         }
44
45         const RawBuffer& getIV() const
46         {
47                 return m_iv;
48         }
49
50         const RawBuffer& getTag() const
51         {
52                 return m_tag;
53         }
54
55 private:
56         RawBuffer m_password;
57         RawBuffer m_iv;
58         RawBuffer m_tag;
59 };
60
61 class BData : public GObj {
62 public:
63         BData(CryptoBackend backendId,
64                   int scheme,
65                   RawBuffer id,
66                   Pwd pwd,
67                   RawBuffer buffer = RawBuffer()) :
68                 GObj(backendId),
69                 m_scheme(scheme),
70                 m_password(std::move(pwd)),
71                 m_id(std::move(id)),
72                 m_raw(std::move(buffer)) {}
73
74         RawBuffer getBinary() const override
75         {
76                 return m_raw;
77         }
78         virtual const RawBuffer& getId() const
79         {
80                 return m_id;
81         }
82         virtual int getScheme() const
83         {
84                 return m_scheme;
85         }
86         virtual const Pwd& getPassword() const
87         {
88                 return m_password;
89         }
90         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
91
92 protected:
93         int m_scheme;
94         Pwd m_password;
95         RawBuffer m_id;
96         RawBuffer m_raw;
97 };
98
99 class Key : public BData {
100 public:
101         Key(CryptoBackend backendId, int scheme, RawBuffer id, Pwd pwd) :
102                 BData(backendId, scheme, std::move(id), std::move(pwd)) {}
103
104         Token unwrap(const CryptoAlgorithm &params,
105                                  const Data &encryptedKey,
106                                  const Password &pass,
107                                  const RawBuffer &hash) override;
108
109         RawBuffer wrap(const CryptoAlgorithm &params,
110                                    const Token &keyToWrap,
111                                    const Password &keyToWrapPass) override;
112 };
113
114 class SKey : public Key {
115 public:
116         SKey(CryptoBackend backendId, int scheme, RawBuffer id, Pwd pwd) :
117                 Key(backendId, scheme, std::move(id), std::move(pwd)) {}
118
119         RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
120         RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
121         GCtxShPtr initContext(const CryptoAlgorithm &, bool) override;
122 };
123
124 class AKey : public Key {
125 public:
126         AKey(CryptoBackend backendId,
127                  int scheme,
128                  RawBuffer id,
129                  Pwd pwd,
130                  DataType dataType,
131                  RawBuffer raw = RawBuffer()) :
132                 Key(backendId, scheme, std::move(id), std::move(pwd)), m_type(dataType)
133         {
134                 m_raw = std::move(raw);
135         }
136
137         RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message) override;
138         int verify(const CryptoAlgorithm &alg, const RawBuffer &message,
139                            const RawBuffer &sign) override;
140         RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
141         RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
142         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
143         GCtxShPtr initContext(const CryptoAlgorithm &, bool) override;
144
145 protected:
146         DataType m_type;
147 };
148
149 class Cert : public AKey {
150 public:
151         Cert(CryptoBackend backendId, int scheme, RawBuffer id, Pwd pwd, DataType dataType) :
152                 AKey(backendId, scheme, std::move(id), std::move(pwd), dataType) {}
153
154         Token unwrap(const CryptoAlgorithm &,
155                                  const Data &,
156                                  const Password &,
157                                  const RawBuffer &) override;
158
159         RawBuffer wrap(const CryptoAlgorithm &params,
160                                    const Token &keyToWrap,
161                                    const Password &keyToWrapPass) override;
162 };
163
164 } // namespace TZ
165 } // namespace Crypto
166 } // namespace CKM
167