2d9bec82cce89706fc55f3453f1316608a3e968c
[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 <data-type.h>
25
26 namespace CKM {
27 namespace Crypto {
28 namespace TZ {
29
30 class Pwd {
31 public:
32         Pwd(Password pwd, RawBuffer iv, RawBuffer tag)
33                 : m_password(pwd.begin(), pwd.end())
34                 , m_iv(std::move(iv))
35                 , m_tag(std::move(tag))
36         {}
37
38         const RawBuffer& getPassword() const
39         {
40                 return m_password;
41         }
42
43         const RawBuffer& getIV() const
44         {
45                 return m_iv;
46         }
47
48         const RawBuffer& getTag() const
49         {
50                 return m_tag;
51         }
52
53
54 private:
55         RawBuffer m_password;
56         RawBuffer m_iv;
57         RawBuffer m_tag;
58 };
59
60 class BData : public GObj {
61 public:
62         explicit BData(CryptoBackend backendId, RawBuffer buffer) :
63                 GObj(backendId), m_raw(std::move(buffer)) {}
64
65         RawBuffer getBinary() const override
66         {
67                 return m_raw;
68         }
69
70 protected:
71         RawBuffer m_raw;
72 };
73
74 class Key : public BData {
75 public:
76         Key(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
77                 BData(backendId, std::move(buffer)),
78                 m_scheme(scheme),
79                 m_password(std::move(pwd)),
80                 m_type(dataType) {}
81
82         virtual int getScheme() const
83         {
84                 return m_scheme;
85         }
86
87         virtual Pwd getPassword() const
88         {
89                 return m_password;
90         }
91
92 protected:
93         int m_scheme;
94         Pwd m_password;
95         DataType m_type;
96 };
97
98 class SKey : public Key {
99 public:
100         SKey(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
101                 Key(backendId, scheme, std::move(buffer), std::move(pwd), dataType) {}
102
103         virtual RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &);
104         virtual RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &);
105 };
106
107 class AKey : public Key {
108 public:
109         AKey(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
110                 Key(backendId, scheme, std::move(buffer), std::move(pwd), dataType) {}
111
112         virtual RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message);
113         virtual int verify(const CryptoAlgorithm &alg, const RawBuffer &message,
114                                            const RawBuffer &sign);
115         virtual RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &);
116         virtual RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &);
117         virtual ~AKey() {}
118 };
119
120 class Cert : public AKey {
121 public:
122         Cert(CryptoBackend backendId, int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
123                 AKey(backendId, scheme, std::move(buffer), std::move(pwd), dataType) {}
124
125         virtual ~Cert() {}
126 };
127
128 } // namespace TZ
129 } // namespace Crypto
130 } // namespace CKM
131