• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CM_TYPE_H
17 #define CM_TYPE_H
18 
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 #ifndef CM_API_PUBLIC
27     #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(__ICCARM__) /* __ICCARM__ for iar */
28         #define CM_API_EXPORT
29     #else
30         #define CM_API_EXPORT __attribute__ ((visibility("default")))
31     #endif
32 #else
33     #define CM_API_EXPORT __attribute__ ((visibility("default")))
34 #endif
35 
36 #define MAX_LEN_CERTIFICATE    8196
37 
38 #define MAX_LEN_CERTIFICATE_CHAIN    (3 * MAX_LEN_CERTIFICATE)
39 
40 #define MAX_SUFFIX_LEN           16
41 #define MAX_COUNT_CERTIFICATE    256
42 #define MAX_LEN_URI              256
43 #define MAX_AUTH_LEN_URI         256
44 #define MAX_LEN_CERT_ALIAS       129     /* include 1 byte: the terminator('\0') */
45 #define MAX_LEN_SUBJECT_NAME     1025
46 #define MAX_LEN_PACKGE_NAME      64
47 #define MAX_UINT32_LEN           16
48 
49 #define CM_ARRAY_SIZE(arr) ((sizeof(arr)) / (sizeof((arr)[0])))
50 
51 /*
52  * Align to 4-tuple
53  * Before calling this function, ensure that the size does not overflow after 3 is added.
54  */
55 #define ALIGN_SIZE(size) ((((uint32_t)(size) + 3) >> 2) << 2)
56 
57 #define CM_BITS_PER_BYTE 8
58 
59 #define CM_CREDENTIAL_STORE             0
60 #define CM_SYSTEM_TRUSTED_STORE         1
61 #define CM_USER_TRUSTED_STORE           2
62 #define CM_PRI_CREDENTIAL_STORE         3
63 #define CM_SYS_CREDENTIAL_STORE         4
64 
65 enum CmKeyDigest {
66     CM_DIGEST_NONE = 0,
67     CM_DIGEST_MD5 = 1,
68     CM_DIGEST_SHA1 = 10,
69     CM_DIGEST_SHA224 = 11,
70     CM_DIGEST_SHA256 = 12,
71     CM_DIGEST_SHA384 = 13,
72     CM_DIGEST_SHA512 = 14,
73 };
74 
75 enum CmKeyPurpose {
76     CM_KEY_PURPOSE_ENCRYPT = 1,                   /* Usable with RSA, EC, AES, and SM4 keys. */
77     CM_KEY_PURPOSE_DECRYPT = 2,                   /* Usable with RSA, EC, AES, and SM4 keys. */
78     CM_KEY_PURPOSE_SIGN = 4,                      /* Usable with RSA, EC keys. */
79     CM_KEY_PURPOSE_VERIFY = 8,                    /* Usable with RSA, EC keys. */
80     CM_KEY_PURPOSE_DERIVE = 16,                   /* Usable with EC keys. */
81     CM_KEY_PURPOSE_WRAP = 32,                     /* Usable with wrap key. */
82     CM_KEY_PURPOSE_UNWRAP = 64,                   /* Usable with unwrap key. */
83     CM_KEY_PURPOSE_MAC = 128,                     /* Usable with mac. */
84     CM_KEY_PURPOSE_AGREE = 256,                   /* Usable with agree. */
85 };
86 
87 enum CmKeyPadding {
88     CM_PADDING_NONE = 0,
89     CM_PADDING_OAEP = 1,
90     CM_PADDING_PSS = 2,
91     CM_PADDING_PKCS1_V1_5 = 3,
92     CM_PADDING_PKCS5 = 4,
93     CM_PADDING_PKCS7 = 5,
94 };
95 
96 enum CmErrorCode {
97     CM_SUCCESS = 0,
98     CM_FAILURE = -1,
99 
100     CMR_ERROR_NOT_PERMITTED = -2,
101     CMR_ERROR_NOT_SUPPORTED = -3,
102     CMR_ERROR_STORAGE = -4,
103     CMR_ERROR_NOT_FOUND = -5,
104     CMR_ERROR_NULL_POINTER = -6,
105     CMR_ERROR_INVALID_ARGUMENT = -7,
106     CMR_ERROR_MAKE_DIR_FAIL = -8,
107     CMR_ERROR_INVALID_OPERATION = -9,
108     CMR_ERROR_OPEN_FILE_FAIL = -10,
109     CMR_ERROR_READ_FILE_ERROR = -11,
110     CMR_ERROR_WRITE_FILE_FAIL = -12,
111     CMR_ERROR_REMOVE_FILE_FAIL = -13,
112     CMR_ERROR_CLOSE_FILE_FAIL = -14,
113     CMR_ERROR_MALLOC_FAIL = -15,
114     CMR_ERROR_NOT_EXIST = -16,
115     CMR_ERROR_ALREADY_EXISTS = -17,
116     CMR_ERROR_INSUFFICIENT_DATA = -18,
117     CMR_ERROR_BUFFER_TOO_SMALL = -19,
118     CMR_ERROR_INVALID_CERT_FORMAT = -20,
119     CMR_ERROR_PARAM_NOT_EXIST = -21,
120     CMR_ERROR_SESSION_REACHED_LIMIT = -22,
121     CMR_ERROR_PERMISSION_DENIED = -23,
122     CMR_ERROR_AUTH_CHECK_FAILED = -24,
123     CMR_ERROR_KEY_OPERATION_FAILED = -25,
124     CMR_ERROR_NOT_SYSTEMP_APP = -26,
125     CMR_ERROR_CERT_NUM_REACHED_LIMIT = -27,
126     CMR_ERROR_ALIAS_LENGTH_REACHED_LIMIT = -28,
127 };
128 
129 enum CMErrorCode { /* temp use */
130     CMR_OK = 0,
131     CMR_ERROR = -1,
132 };
133 
134 enum CmTagType {
135     CM_TAG_TYPE_INVALID = 0 << 28,
136     CM_TAG_TYPE_INT = 1 << 28,
137     CM_TAG_TYPE_UINT = 2 << 28,
138     CM_TAG_TYPE_ULONG = 3 << 28,
139     CM_TAG_TYPE_BOOL = 4 << 28,
140     CM_TAG_TYPE_BYTES = 5 << 28,
141 };
142 
143 enum CmTag {
144     /* Inner-use TAGS used for ipc serialization */
145     CM_TAG_PARAM0_BUFFER = CM_TAG_TYPE_BYTES | 30001,
146     CM_TAG_PARAM1_BUFFER = CM_TAG_TYPE_BYTES | 30002,
147     CM_TAG_PARAM2_BUFFER = CM_TAG_TYPE_BYTES | 30003,
148     CM_TAG_PARAM3_BUFFER = CM_TAG_TYPE_BYTES | 30004,
149     CM_TAG_PARAM4_BUFFER = CM_TAG_TYPE_BYTES | 30005,
150     CM_TAG_PARAM0_UINT32 = CM_TAG_TYPE_UINT | 30006,
151     CM_TAG_PARAM1_UINT32 = CM_TAG_TYPE_UINT | 30007,
152     CM_TAG_PARAM2_UINT32 = CM_TAG_TYPE_UINT | 30008,
153     CM_TAG_PARAM3_UINT32 = CM_TAG_TYPE_UINT | 30009,
154     CM_TAG_PARAM4_UINT32 = CM_TAG_TYPE_UINT | 30010,
155     CM_TAG_PARAM0_BOOL = CM_TAG_TYPE_BOOL | 30011,
156     CM_TAG_PARAM1_BOOL = CM_TAG_TYPE_BOOL | 30012,
157     CM_TAG_PARAM2_BOOL = CM_TAG_TYPE_BOOL | 30013,
158     CM_TAG_PARAM3_BOOL = CM_TAG_TYPE_BOOL | 30014,
159     CM_TAG_PARAM4_BOOL = CM_TAG_TYPE_BOOL | 30015,
160     CM_TAG_PARAM0_NULL = CM_TAG_TYPE_BYTES | 30016,
161     CM_TAG_PARAM1_NULL = CM_TAG_TYPE_BYTES | 30017,
162     CM_TAG_PARAM2_NULL = CM_TAG_TYPE_BYTES | 30018,
163     CM_TAG_PARAM3_NULL = CM_TAG_TYPE_BYTES | 30019,
164     CM_TAG_PARAM4_NULL = CM_TAG_TYPE_BYTES | 30020,
165 };
166 
167 #define CM_PARAM_BUFFER_NULL_INTERVAL ((CM_TAG_PARAM0_NULL) - (CM_TAG_PARAM0_BUFFER))
168 
169 enum CmSendType {
170     CM_SEND_TYPE_ASYNC = 0,
171     CM_SEND_TYPE_SYNC,
172 };
173 
174 struct CmBlob {
175     uint32_t size;
176     uint8_t *data;
177 };
178 
179 struct Credential {
180     uint32_t isExist;
181     char type[MAX_LEN_SUBJECT_NAME];
182     char alias[MAX_LEN_CERT_ALIAS];
183     char keyUri[MAX_LEN_URI];
184     uint32_t certNum;
185     uint32_t keyNum;
186     struct CmBlob credData;
187 };
188 
189 struct CmParam {
190     uint32_t tag;
191     union {
192         bool boolParam;
193         int32_t int32Param;
194         uint32_t uint32Param;
195         uint64_t uint64Param;
196         struct CmBlob blob;
197     };
198 };
199 
200 struct CmParamSet {
201     uint32_t paramSetSize;
202     uint32_t paramsCnt;
203     struct CmParam params[];
204 };
205 
206 struct CmSignatureSpec {
207     uint32_t purpose;
208     uint32_t padding;
209     uint32_t digest;
210 };
211 
CmIsAdditionOverflow(uint32_t a,uint32_t b)212 static inline bool CmIsAdditionOverflow(uint32_t a, uint32_t b)
213 {
214     return (UINT32_MAX - a) < b;
215 }
216 
CmCheckBlob(const struct CmBlob * blob)217 static inline int32_t CmCheckBlob(const struct CmBlob *blob)
218 {
219     if ((blob == NULL) || (blob->data == NULL) || (blob->size == 0)) {
220         return CMR_ERROR_INVALID_ARGUMENT;
221     }
222     return CM_SUCCESS;
223 }
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif /* CM_TYPE_H */
230