95233d8e2df558f5104d31950f4aafa478bdc5c0
[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     // encryption & decryption
115     ED_IV = 1,
116     ED_CTR,
117     ED_CTR_LEN,
118     ED_AAD,
119     ED_TAG_LEN,
120     ED_LABEL,
121
122     // key generation
123     GEN_KEY_LEN = 101,
124     GEN_EC,             // elliptic curve (ElipticCurve)
125
126     // sign & verify
127     SV_HASH_ALGO = 201, // hash algorithm (HashAlgorithm)
128     SV_RSA_PADDING,     // RSA padding (RSAPaddingAlgorithm)
129 };
130
131 // algorithm types
132 enum class AlgoType : int {
133     AES_CTR = 1,
134     AES_CBC,
135     AES_GCM,
136     AES_CFB,
137     RSA_OAEP,
138     RSA,
139     DSA,
140     ECDSA,
141 };
142
143 class KEY_MANAGER_API BaseParam {
144 public:
145     virtual int getBuffer(RawBuffer&) const;
146     virtual int getInt(uint64_t&) const;
147     virtual ~BaseParam() {}
148
149 protected:
150     BaseParam() {}
151 };
152 typedef std::unique_ptr<BaseParam> BaseParamPtr;
153
154 class KEY_MANAGER_API BufferParam : public BaseParam {
155 public:
156     int getBuffer(RawBuffer& buffer) const;
157     static BaseParamPtr create(const RawBuffer& buffer);
158 private:
159     explicit BufferParam(const RawBuffer& value) : m_buffer(value) {}
160
161     RawBuffer m_buffer;
162 };
163
164 class KEY_MANAGER_API IntParam : public BaseParam {
165 public:
166     static BaseParamPtr create(uint64_t value);
167     int getInt(uint64_t& value) const;
168 private:
169     explicit IntParam(uint64_t value) : m_int(value) {}
170
171     uint64_t m_int;
172 };
173
174 // cryptographic algorithm description
175 struct CryptoAlgorithm {
176     AlgoType m_type;
177     std::map<ParamName, BaseParamPtr> m_params;
178 };
179
180 } // namespace CKM
181