Algorithm types and param names updated
[platform/core/security/key-manager.git] / src / include / ckm / ckm-type.h
1 /*
2  *  Copyright (c) 2000 - 2013 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        ckm-type.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       Sample service implementation.
21  */
22 #pragma once
23
24 #include <stdint.h>
25 #include <cassert>
26
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include <memory>
31
32 #include <ckm/ckm-raw-buffer.h>
33 #include <ckm/ckm-password.h>
34
35 #define KEY_MANAGER_API __attribute__((visibility("default")))
36
37 namespace CKM {
38
39 // used to pass password and raw key data
40 typedef std::vector<RawBuffer> RawBufferVector;
41 typedef std::string Alias;
42 typedef std::string Label;
43 typedef std::vector<Alias> AliasVector;
44
45 enum class KeyType : int {
46     KEY_NONE = 0,
47     KEY_RSA_PUBLIC,
48     KEY_RSA_PRIVATE,
49     KEY_ECDSA_PUBLIC,
50     KEY_ECDSA_PRIVATE,
51     KEY_DSA_PUBLIC,
52     KEY_DSA_PRIVATE,
53     KEY_AES
54 };
55
56 enum class DataFormat : int {
57     FORM_DER_BASE64 = 0,
58     FORM_DER,
59     FORM_PEM
60 };
61
62 enum class ElipticCurve : int {
63     prime192v1 = 0,
64     prime256v1,
65     secp384r1
66 };
67
68 enum class CertificateFieldId : int {
69     ISSUER = 0,
70     SUBJECT
71 };
72
73 struct Policy {
74     Policy(const Password &pass = Password(), bool extract = true)
75       : password(pass)
76       , extractable(extract)
77     {}
78     virtual ~Policy(){}
79     Password password;  // byte array used to encrypt data inside CKM
80     bool extractable;   // if true key may be extracted from storage
81 };
82
83 enum class HashAlgorithm : int {
84     NONE = 0,
85     SHA1,
86     SHA256,
87     SHA384,
88     SHA512
89 };
90
91 enum class RSAPaddingAlgorithm : int {
92     NONE = 0,
93     PKCS1,
94     X931
95 };
96
97 enum class DBCMAlgType : int {
98     NONE = 0,
99     AES_GCM_256,
100     COUNT
101 };
102
103 typedef int PermissionMask;
104 enum Permission: int {
105     NONE            = 0x00,
106     READ            = 0x01,
107     REMOVE          = 0x02
108     // keep in sync with ckmc_permission_e !
109 };
110
111 const char * ErrorToString(int error);
112
113 // algorithm parameters
114 enum class ParamName : int {
115     ALGO_TYPE = 1,      // If there's no such param, the service will try to deduce the algorithm
116                         // type from the key.
117
118     // encryption & decryption
119     ED_IV = 101,
120     ED_CTR_LEN,
121     ED_AAD,
122     ED_TAG_LEN,
123     ED_LABEL,
124
125     // key generation
126     GEN_KEY_LEN = 201,
127     GEN_EC,             // elliptic curve (ElipticCurve)
128
129     // sign & verify
130     SV_HASH_ALGO = 301, // hash algorithm (HashAlgorithm)
131     SV_RSA_PADDING,     // RSA padding (RSAPaddingAlgorithm)
132 };
133
134 // algorithm types (ALGO_TYPE param)
135 enum class AlgoType : int {
136     AES_CTR = 1,
137     AES_CBC,
138     AES_GCM,
139     AES_CFB,
140     RSA_OAEP,
141     RSA_SV,
142     DSA_SV,
143     ECDSA_SV,
144     RSA_GEN,
145     DSA_GEN,
146     ECDSA_GEN,
147 };
148
149 // cryptographic algorithm description
150 class KEY_MANAGER_API CryptoAlgorithm {
151 public:
152     template <typename T>
153     bool getParam(ParamName name, T& value) const;
154
155     // returns false if param 'name' already exists
156     template <typename T>
157     bool addParam(ParamName name, const T& value);
158
159 protected:
160     class BaseParam {
161     public:
162         virtual bool getBuffer(RawBuffer&) const { return false; }
163         virtual bool getInt(uint64_t&) const { return false; }
164         virtual ~BaseParam() {}
165
166     protected:
167         BaseParam() {}
168     };
169     typedef std::shared_ptr<BaseParam> BaseParamPtr;
170
171     class BufferParam : public BaseParam {
172     public:
173         bool getBuffer(RawBuffer& buffer) const;
174         static BaseParamPtr create(const RawBuffer& buffer);
175     private:
176         explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
177
178         RawBuffer m_buffer;
179     };
180
181     class IntParam : public BaseParam {
182     public:
183         static BaseParamPtr create(uint64_t value);
184         bool getInt(uint64_t& value) const;
185     private:
186         explicit IntParam(uint64_t value) : m_int(value) {}
187
188         uint64_t m_int;
189     };
190
191     std::map<ParamName, BaseParamPtr> m_params;
192 };
193
194 template <typename T>
195 bool CryptoAlgorithm::getParam(ParamName name, T& value) const {
196     auto param = m_params.find(name);
197     if (param == m_params.end())
198         return false;
199
200     assert(param->second);
201
202     uint64_t valueTmp;
203     if (param->second->getInt(valueTmp)) {
204         value = static_cast<T>(valueTmp);
205         return true;
206     }
207     return false;
208 }
209
210 template <>
211 bool CryptoAlgorithm::getParam(ParamName name, RawBuffer& value) const;
212
213 template <typename T>
214 bool CryptoAlgorithm::addParam(ParamName name, const T& value) {
215     return m_params.emplace(name, IntParam::create(static_cast<uint64_t>(value))).second;
216 }
217
218 template <>
219 bool CryptoAlgorithm::addParam(ParamName name, const RawBuffer& value);
220
221 } // namespace CKM
222