• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 
16 /**
17  * @defgroup crypt_eal_encode
18  * @ingroup crypt
19  * @brief pubkey encode/decode of crypto module
20  */
21 
22 #ifndef CRYPT_EAL_ENCODE_H
23 #define CRYPT_EAL_ENCODE_H
24 
25 #include <stdint.h>
26 
27 #include "bsl_params.h"
28 #include "bsl_types.h"
29 #include "bsl_list.h"
30 #include "crypt_eal_pkey.h"
31 #include "crypt_eal_provider.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif // __cplusplus
36 
37 typedef struct CRYPT_DecoderCtx CRYPT_DECODER_Ctx;
38 
39 /**
40  * @brief Create a decoder context for the specified format and type
41  *
42  * @param libCtx EAL library context
43  * @param keyType Decoding target type (e.g., CRYPT_ALG_ID_RSA, CRYPT_ALG_ID_EC)
44  * @param attrName Attribute name for specific type decoding (can be NULL)
45  * @return CRYPT_DECODER_Ctx* Decoder context, returns NULL on failure
46  */
47 CRYPT_DECODER_Ctx *CRYPT_DECODE_ProviderNewCtx(CRYPT_EAL_LibCtx *libCtx, int32_t keyType, const char *attrName);
48 
49 /**
50  * @brief Free the decoder context
51  *
52  * @param ctx Decoder context
53  */
54 void CRYPT_DECODE_Free(CRYPT_DECODER_Ctx *ctx);
55 
56 /**
57  * @brief Set decoder parameters
58  *
59  * @param ctx Decoder context
60  * @param param Parameter
61  * @return int32_t CRYPT_SUCCESS on success, error code on failure
62  */
63 int32_t CRYPT_DECODE_SetParam(CRYPT_DECODER_Ctx *ctx, const BSL_Param *param);
64 
65 /**
66  * @brief Get decoder parameters
67  *
68  * @param ctx Decoder context
69  * @param param Parameter (output)
70  * @return int32_t CRYPT_SUCCESS on success, error code on failure
71  */
72 int32_t CRYPT_DECODE_GetParam(CRYPT_DECODER_Ctx *ctx, BSL_Param *param);
73 
74 /**
75  * @brief Perform decoding operation
76  *
77  * @param ctx Decoder context
78  * @param input Input data
79  * @param inParam Input parameter
80  * @param out Output object to store decoding results
81  * @return int32_t CRYPT_SUCCESS on success, error code on failure
82  */
83 int32_t CRYPT_DECODE_Decode(CRYPT_DECODER_Ctx *ctx, const BSL_Param *inParam, BSL_Param **outParam);
84 
85 /**
86  * @brief Free the output data
87  *
88  * @param ctx Decoder context
89  * @param data Output data
90  */
91 void CRYPT_DECODE_FreeOutData(CRYPT_DECODER_Ctx *ctx, BSL_Param *outData);
92 
93 typedef struct CRYPT_DECODER_PoolCtx CRYPT_DECODER_PoolCtx;
94 
95 /**
96  * @brief Command codes for CRYPT_DECODE_PoolCtrl function
97  */
98 typedef enum {
99     /** Set the target format */
100     CRYPT_DECODE_POOL_CMD_SET_TARGET_FORMAT,
101     /** Set the target type */
102     CRYPT_DECODE_POOL_CMD_SET_TARGET_TYPE,
103     /** Set the not free out data */
104     CRYPT_DECODE_POOL_CMD_SET_FLAG_FREE_OUT_DATA,
105 } CRYPT_DECODE_POOL_CMD;
106 
107 /**
108  * @brief Create a decoder pool context
109  *
110  * @param libCtx EAL library context
111  * @param attrName Provider attribute name, can be NULL
112  * @param format Input data format (e.g., BSL_FORMAT_PEM, BSL_FORMAT_DER)
113  * @param type Decoding target type (e.g., CRYPT_ALG_ID_RSA, CRYPT_ALG_ID_EC)
114  * @return CRYPT_DECODER_PoolCtx* Decoder pool context on success, NULL on failure
115  */
116 CRYPT_DECODER_PoolCtx *CRYPT_DECODE_PoolNewCtx(CRYPT_EAL_LibCtx *libCtx, const char *attrName,
117     int32_t keyType, const char *format, const char *type);
118 /**
119  * @brief Free a decoder pool context
120  *
121  * @param poolCtx Decoder pool context
122  */
123 void CRYPT_DECODE_PoolFreeCtx(CRYPT_DECODER_PoolCtx *poolCtx);
124 
125 /**
126  * @brief Decode the input data with the decoder chain
127  *
128  * @param poolCtx Decoder pool context
129  * @param inParam Input data
130  * @param outParam Output Data
131  * @return int32_t CRYPT_SUCCESS on success, error code on failure
132  */
133 int32_t CRYPT_DECODE_PoolDecode(CRYPT_DECODER_PoolCtx *poolCtx, const BSL_Param *inParam, BSL_Param **outParam);
134 
135 /**
136  * @brief Control operation for decoder pool
137  *
138  * @param poolCtx Decoder pool context
139  * @param cmd Control command
140  * @param val The value of the control command
141  * @param valLen The length of the value
142  * @return int32_t CRYPT_SUCCESS on success, error code on failure
143  */
144 int32_t CRYPT_DECODE_PoolCtrl(CRYPT_DECODER_PoolCtx *poolCtx, int32_t cmd, void *val, int32_t valLen);
145 
146 /**
147  * @ingroup crypt_eal_encode
148  * @brief   Decode formatted buffer of pkey
149  *
150  * @param   format [IN] the buffer format.
151  * @param   type [IN] the type of pkey.
152  * @param   encode [IN] the encoded asn1 buffer.
153  * @param   pwd [IN] the password, maybe NULL for unencrypted private key / public key.
154  * @param   pwdlen [IN] the length of password.
155  * @param   ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the ans1 buffer.
156  *
157  * @retval #CRYPT_SUCCESS, if success.
158  *         Other error codes see the crypt_errno.h
159  */
160 int32_t CRYPT_EAL_DecodeBuffKey(int32_t format, int32_t type,
161     BSL_Buffer *encode, const uint8_t *pwd, uint32_t pwdlen, CRYPT_EAL_PkeyCtx **ealPKey);
162 
163 /**
164  * @ingroup crypt_eal_encode
165  * @brief   Decode formatted buffer of pkey with provider
166  *
167  * @param   libCtx [IN] the library context of provider.
168  * @param   attrName [IN] provider attribute name, maybe NULL.
169  * @param   keyType [IN] the type of pkey.
170  * @param   format [IN] the buffer format.
171  * @param   type [IN] the type of pkey.
172  * @param   encode [IN] the encoded asn1 buffer.
173  * @param   pwd [IN] the password buffer, maybe NULL for unencrypted private key / public key.
174  * @param   ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the ans1 buffer.
175  *
176  * @retval #CRYPT_SUCCESS, if success.
177  *         Other error codes see the crypt_errno.h
178  */
179 int32_t CRYPT_EAL_ProviderDecodeBuffKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType,
180     const char *format, const char *type, BSL_Buffer *encode, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey);
181 
182 /**
183  * @ingroup crypt_eal_encode
184  * @brief   Decode formatted file of pkey
185  *
186  * @param   format [IN] the file format.
187  * @param   type [IN] the type of pkey.
188  * @param   path [IN] the encoded file path.
189  * @param   pwd [IN] the password, maybe NULL for unencrypted private key / public key.
190  * @param   pwdlen [IN] the length of password.
191  * @param   ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the path.
192  *
193  * @retval #CRYPT_SUCCESS, if success.
194  *         Other error codes see the crypt_errno.h
195  */
196 int32_t CRYPT_EAL_DecodeFileKey(int32_t format, int32_t type, const char *path,
197     uint8_t *pwd, uint32_t pwdlen, CRYPT_EAL_PkeyCtx **ealPKey);
198 
199 /**
200  * @ingroup crypt_eal_encode
201  * @brief   Decode formatted file of pkey with extended parameters
202  *
203  * @param   libCtx [IN] the library context of provider.
204  * @param   attrName [IN] provider attribute name, maybe NULL.
205  * @param   format [IN] the file format.
206  * @param   type [IN] the type of pkey.
207  * @param   path [IN] the encoded file path.
208  * @param   pwd [IN] the password buffer, maybe NULL for unencrypted private key / public key.
209  * @param   ealPKey [OUT] created CRYPT_EAL_PkeyCtx which parsed from the path.
210  *
211  * @retval #CRYPT_SUCCESS, if success.
212  *         Other error codes see the crypt_errno.h
213  */
214 int32_t CRYPT_EAL_ProviderDecodeFileKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, int32_t keyType,
215     const char *format, const char *type, const char *path, const BSL_Buffer *pwd, CRYPT_EAL_PkeyCtx **ealPKey);
216 
217 /**
218  * @ingroup crypt_eal_encode
219  * @brief   Encode formatted buffer of pkey
220  *
221  * @param   ealPKey [IN] CRYPT_EAL_PkeyCtx to encode.
222  * @param   encodeParam [IN] pkcs8 encode params.
223  * @param   format [IN] the buffer format.
224  * @param   type [IN] the type of pkey.
225  * @param   encode [OUT] the encoded asn1 buffer.
226  *
227  * @retval #CRYPT_SUCCESS, if success.
228  *         Other error codes see the crypt_errno.h
229  */
230 int32_t CRYPT_EAL_EncodeBuffKey(CRYPT_EAL_PkeyCtx *ealPKey, const CRYPT_EncodeParam *encodeParam,
231     int32_t format, int32_t type, BSL_Buffer *encode);
232 
233 /**
234  * @ingroup crypt_eal_encode
235  * @brief   Encode formatted buffer of pkey with provider
236  *
237  * @param   libCtx [IN] the library context of provider.
238  * @param   attrName [IN] provider attribute name, maybe NULL.
239  * @param   ealPKey [IN] CRYPT_EAL_PkeyCtx to encode.
240  * @param   encodeParam [IN] pkcs8 encode params.
241  * @param   format [IN] the buffer format.
242  * @param   type [IN] the type of pkey.
243  * @param   encode [OUT] the encoded asn1 buffer.
244  *
245  * @retval #CRYPT_SUCCESS, if success.
246  *         Other error codes see the crypt_errno.h
247  */
248 int32_t CRYPT_EAL_ProviderEncodeBuffKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, CRYPT_EAL_PkeyCtx *ealPKey,
249     const CRYPT_EncodeParam *encodeParam, const char *format, const char *type, BSL_Buffer *encode);
250 
251 /**
252  * @ingroup crypt_eal_encode
253  * @brief   Encode formatted file of pkey
254  *
255  * @param   ealPKey [IN] CRYPT_EAL_PkeyCtx to encode.
256  * @param   encodeParam [IN] pkcs8 encode params.
257  * @param   format [IN] the file format.
258  * @param   type [IN] the type of pkey.
259  * @param   path [IN] the encoded file path.
260  *
261  * @retval #CRYPT_SUCCESS, if success.
262  *         Other error codes see the crypt_errno.h
263  */
264 int32_t CRYPT_EAL_EncodeFileKey(CRYPT_EAL_PkeyCtx *ealPKey, const CRYPT_EncodeParam *encodeParam,
265     int32_t format, int32_t type, const char *path);
266 
267 /**
268  * @ingroup crypt_eal_encode
269  * @brief   Encode formatted file of pkey with provider
270  *
271  * @param   libCtx [IN] the library context of provider.
272  * @param   attrName [IN] provider attribute name, maybe NULL.
273  * @param   ealPKey [IN] CRYPT_EAL_PkeyCtx to encode.
274  * @param   encodeParam [IN] pkcs8 encode params.
275  * @param   format [IN] the file format.
276  * @param   type [IN] the type of pkey.
277  * @param   path [IN] the encoded file path.
278  *
279  * @retval #CRYPT_SUCCESS, if success.
280  *         Other error codes see the crypt_errno.h
281  */
282 int32_t CRYPT_EAL_ProviderEncodeFileKey(CRYPT_EAL_LibCtx *libCtx, const char *attrName, CRYPT_EAL_PkeyCtx *ealPKey,
283     const CRYPT_EncodeParam *encodeParam, const char *format, const char *type, const char *path);
284 
285 #ifdef __cplusplus
286 }
287 #endif // __cplusplus
288 
289 #endif // CRYPT_EAL_ENCODE_H