9d93421f38ef0c1a1da75dfc32b49cdfed97ca9f
[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
26 #include <string>
27 #include <vector>
28 #include <map>
29 #include <memory>
30
31 #include <ckm/ckm-raw-buffer.h>
32 #include <ckm/ckm-password.h>
33
34 #define KEY_MANAGER_API __attribute__((visibility("default")))
35
36 namespace CKM {
37
38 // used to pass password and raw key data
39 typedef std::vector<RawBuffer> RawBufferVector;
40 typedef std::string Alias;
41 typedef std::string Label;
42 typedef std::vector<Alias> AliasVector;
43
44 enum class KeyType : int {
45     KEY_NONE = 0,
46     KEY_RSA_PUBLIC,
47     KEY_RSA_PRIVATE,
48     KEY_ECDSA_PUBLIC,
49     KEY_ECDSA_PRIVATE,
50     KEY_DSA_PUBLIC,
51     KEY_DSA_PRIVATE,
52     KEY_AES
53 };
54
55 enum class DataFormat : int {
56     FORM_DER_BASE64 = 0,
57     FORM_DER,
58     FORM_PEM
59 };
60
61 enum class ElipticCurve : int {
62     prime192v1 = 0,
63     prime256v1,
64     secp384r1
65 };
66
67 enum class CertificateFieldId : int {
68     ISSUER = 0,
69     SUBJECT
70 };
71
72 struct Policy {
73     Policy(const Password &pass = Password(), bool extract = true)
74       : password(pass)
75       , extractable(extract)
76     {}
77     virtual ~Policy(){}
78     Password password;  // byte array used to encrypt data inside CKM
79     bool extractable;   // if true key may be extracted from storage
80 };
81
82 enum class HashAlgorithm : int {
83     NONE = 0,
84     SHA1,
85     SHA256,
86     SHA384,
87     SHA512
88 };
89
90 enum class RSAPaddingAlgorithm : int {
91     NONE = 0,
92     PKCS1,
93     X931
94 };
95
96 enum class DBCMAlgType : int {
97     NONE = 0,
98     AES_GCM_256,
99     COUNT
100 };
101
102 typedef int PermissionMask;
103 enum Permission: int {
104     NONE            = 0x00,
105     READ            = 0x01,
106     REMOVE          = 0x02
107     // keep in sync with ckmc_permission_e !
108 };
109
110 const char * ErrorToString(int error);
111
112 // algorithm parameters
113 enum class ParamName : int {
114     ALGO_TYPE = 1,      // If there's no such param, the service will try to deduce the algorithm
115                         // type from the key.
116
117     // encryption & decryption
118     ED_IV = 101,
119     ED_CTR,
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,
142     DSA,
143     ECDSA,
144 };
145
146 // cryptographic algorithm description
147 class KEY_MANAGER_API CryptoAlgorithm {
148 public:
149     template <typename T>
150     bool getParam(ParamName name, T& value) const;
151
152     // returns false if param 'name' already exists
153     template <typename T>
154     bool addParam(ParamName name, const T& value);
155
156 protected:
157     class BaseParam {
158     public:
159         virtual bool getBuffer(RawBuffer&) const { return false; }
160         virtual bool getInt(uint64_t&) const { return false; }
161         virtual ~BaseParam() {}
162
163     protected:
164         BaseParam() {}
165     };
166     typedef std::unique_ptr<BaseParam> BaseParamPtr;
167
168     class BufferParam : public BaseParam {
169     public:
170         bool getBuffer(RawBuffer& buffer) const;
171         static BaseParamPtr create(const RawBuffer& buffer);
172     private:
173         explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
174
175         RawBuffer m_buffer;
176     };
177
178     class IntParam : public BaseParam {
179     public:
180         static BaseParamPtr create(uint64_t value);
181         bool getInt(uint64_t& value) const;
182     private:
183         explicit IntParam(uint64_t value) : m_int(value) {}
184
185         uint64_t m_int;
186     };
187
188     std::map<ParamName, BaseParamPtr> m_params;
189 };
190
191
192
193 } // namespace CKM
194