Key wrapping implementation 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 <data-type.h>
26
27 namespace CKM {
28 namespace Crypto {
29 namespace TZ {
30
31 class Pwd {
32 public:
33         Pwd(Password pwd, RawBuffer iv, RawBuffer tag)
34                 : m_password(pwd.begin(), pwd.end())
35                 , m_iv(std::move(iv))
36                 , m_tag(std::move(tag))
37         {}
38
39         const RawBuffer& getPassword() const
40         {
41                 return m_password;
42         }
43
44         const RawBuffer& getIV() const
45         {
46                 return m_iv;
47         }
48
49         const RawBuffer& getTag() const
50         {
51                 return m_tag;
52         }
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         explicit BData(CryptoBackend backendId, RawBuffer buffer) :
64                 GObj(backendId), m_raw(std::move(buffer)) {}
65
66         RawBuffer getBinary() const override
67         {
68                 return m_raw;
69         }
70         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
71
72 protected:
73         RawBuffer m_raw;
74 };
75
76 class Key : public BData {
77 public:
78         Key(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
79                 BData(backendId, std::move(buffer)),
80                 m_scheme(scheme),
81                 m_password(std::move(pwd)),
82                 m_type(dataType) {}
83
84         virtual int getScheme() const
85         {
86                 return m_scheme;
87         }
88
89         virtual Pwd getPassword() const
90         {
91                 return m_password;
92         }
93
94         Token unwrap(const CryptoAlgorithm &params,
95                                  const Data &encryptedKey,
96                                  const Password &pass,
97                                  const RawBuffer &hash) override;
98
99         RawBuffer wrap(const CryptoAlgorithm &params,
100                                    const Token &keyToWrap,
101                                    const Password &keyToWrapPass) override;
102
103 protected:
104         int m_scheme;
105         Pwd m_password;
106         DataType m_type;
107 };
108
109 class SKey : public Key {
110 public:
111         SKey(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
112                 Key(backendId, scheme, std::move(buffer), std::move(pwd), dataType) {}
113
114         RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
115         RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
116 };
117
118 class AKey : public Key {
119 public:
120         AKey(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
121                 Key(backendId, scheme, std::move(buffer), std::move(pwd), dataType) {}
122
123         RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message) override;
124         int verify(const CryptoAlgorithm &alg, const RawBuffer &message,
125                            const RawBuffer &sign) override;
126         RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
127         RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
128         Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
129 };
130
131 class Cert : public AKey {
132 public:
133         Cert(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
134                 AKey(backendId, scheme, std::move(buffer), std::move(pwd), dataType) {}
135
136         Token unwrap(const CryptoAlgorithm &,
137                                  const Data &,
138                                  const Password &,
139                                  const RawBuffer &) override;
140
141         RawBuffer wrap(const CryptoAlgorithm &params,
142                                    const Token &keyToWrap,
143                                    const Password &keyToWrapPass) override;
144 };
145
146 } // namespace TZ
147 } // namespace Crypto
148 } // namespace CKM
149