32c6444f7b38c486fbf503633eb23cd25e06e068
[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(RawBuffer buffer) : m_raw(std::move(buffer)) {}
63
64         virtual RawBuffer getBinary() const override
65         {
66                 return m_raw;
67         }
68
69 protected:
70         RawBuffer m_raw;
71 };
72
73 class Key : public BData {
74 public:
75         Key(int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
76                 BData(std::move(buffer)),
77                 m_scheme(scheme),
78                 m_password(std::move(pwd)),
79                 m_type(dataType) {}
80
81         virtual int getScheme() const
82         {
83                 return m_scheme;
84         }
85
86         virtual Pwd getPassword() const
87         {
88                 return m_password;
89         }
90
91 protected:
92         int m_scheme;
93         Pwd m_password;
94         DataType m_type;
95 };
96
97 class SKey : public Key {
98 public:
99         SKey(int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
100                 Key(scheme, std::move(buffer), std::move(pwd), dataType) {}
101
102         virtual RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &);
103         virtual RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &);
104 };
105
106 class AKey : public Key {
107 public:
108         AKey(int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
109                 Key(scheme, std::move(buffer), std::move(pwd), dataType) {}
110
111         virtual RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message);
112         virtual int verify(const CryptoAlgorithm &alg, const RawBuffer &message,
113                                            const RawBuffer &sign);
114         virtual RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &);
115         virtual RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &);
116         virtual ~AKey() {}
117 };
118
119 class Cert : public AKey {
120 public:
121         Cert(int scheme, RawBuffer buffer, Pwd pwd, DataType dataType) :
122                 AKey(scheme, std::move(buffer), std::move(pwd), dataType) {}
123
124         virtual ~Cert() {}
125 };
126
127 } // namespace TZ
128 } // namespace Crypto
129 } // namespace CKM
130