• 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 #ifndef CRYPT_ECDSA_H
17 #define CRYPT_ECDSA_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_ECDSA
21 
22 #include <stdint.h>
23 #include "crypt_types.h"
24 #include "crypt_local_types.h"
25 #include "crypt_ecc_pkey.h"
26 #include "crypt_ecc.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cpluscplus */
31 
32 /* ECDSA key context */
33 typedef struct ECC_PkeyCtx CRYPT_ECDSA_Ctx;
34 
35 /* ECDSA parameter structure */
36 typedef struct EccPara CRYPT_EcdsaPara;
37 
38 /**
39  * @ingroup ecdsa
40  * @brief ecdsa Allocate context memory space.
41  *
42  * @retval (CRYPT_ECDSA_Ctx *) Pointer to the memory space of the allocated context
43  * @retval NULL                Invalid null pointer
44  */
45 CRYPT_ECDSA_Ctx *CRYPT_ECDSA_NewCtx(void);
46 
47 /**
48  * @ingroup ecdsa
49  * @brief ecdsa Allocate context memory space.
50  *
51  * @param libCtx [IN] Library context
52  *
53  * @retval (CRYPT_ECDSA_Ctx *) Pointer to the memory space of the allocated context
54  * @retval NULL                Invalid null pointer
55  */
56 CRYPT_ECDSA_Ctx *CRYPT_ECDSA_NewCtxEx(void *libCtx);
57 
58 /**
59  * @ingroup ecdsa
60  * @brief Copy the ECDSA context. After the duplication is complete, call the CRYPT_ECDSA_FreeCtx to release the memory.
61  *
62  * @param ctx [IN] Source ECDSA context
63  *
64  * @return CRYPT_ECDSA_Ctx ECDSA context pointer
65  * If it fails, null is returned.
66  */
67 CRYPT_ECDSA_Ctx *CRYPT_ECDSA_DupCtx(CRYPT_ECDSA_Ctx *ctx);
68 
69 /**
70  * @ingroup ecdsa
71  * @brief ecdsa Releasing the key context structure
72  *
73  * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker.
74  */
75 void CRYPT_ECDSA_FreeCtx(CRYPT_ECDSA_Ctx *ctx);
76 
77 /**
78  * @ingroup ecdsa
79  * @brief ecdsa Generate the key parameter structure
80  *
81  * @param id [IN] Curve ID Parameter ID, which can be selected CRYPT_ECC_NISTP224 to CRYPT_ECC_NISTP521 only
82  *                from CRYPT_PKEY_ParaId.
83  *
84  * @retval (CRYPT_EcdsaPara *) Pointer to the memory space of the allocated context
85  * @retval NULL                Invalid null pointer
86  */
87 CRYPT_EcdsaPara *CRYPT_ECDSA_NewParaById(int32_t id);
88 
89 /**
90  * @ingroup ecdsa
91  * @brief ecdsa Generate the key parameter structure
92  *
93  * @param eccPara [IN] Curve parameter information, which can be selected CRYPT_ECC_NISTP224 to CRYPT_ECC_NISTP521 only
94  *                     from CRYPT_PKEY_ParaId.
95  *
96  * @retval (CRYPT_EcdsaPara *) Pointer to the memory space of the allocated context
97  * @retval NULL Invalid null pointer
98  */
99 CRYPT_EcdsaPara *CRYPT_ECDSA_NewPara(const BSL_Param *eccPara);
100 
101 /**
102  * @ingroup ecdsa
103  * @brief Obtain the parameter ID.
104  *
105  * @param ctx [IN] ECDSA context
106  *
107  * @retval ID. If the context is invalid, CRYPT_PKEY_PARAID_MAX is returned.
108  */
109 CRYPT_PKEY_ParaId CRYPT_ECDSA_GetParaId(const CRYPT_ECDSA_Ctx *ctx);
110 
111 /**
112  * @ingroup ecdsa
113  * @brief ecdsa Release the key parameter structure
114  *
115  * @param para [IN] Pointer to the key parameter structure to be released. The parameter is set NULL by the invoker.
116  */
117 void CRYPT_ECDSA_FreePara(CRYPT_EcdsaPara *para);
118 
119 /**
120  * @ingroup ecdsa
121  * @brief Set the data of the key parameter structure to the key structure.
122  *
123  * @param ctx [OUT] Key structure for which related parameters need to be set
124  * @param para [IN] Key parameters
125  *
126  * @retval CRYPT_NULL_INPUT     Invalid null pointer input
127  * @retval CRYPT_MEM_ALLOC_FAIL Internal memory allocation error
128  * @retval CRYPT_SUCCESS        Set successfully.
129  */
130 int32_t CRYPT_ECDSA_SetPara(CRYPT_ECDSA_Ctx *ctx, const BSL_Param *param);
131 
132 /**
133  * @ingroup ecdsa
134  * @brief Obtain the key parameter structure.
135  *
136  * @param ctx [IN] Key structure for which related parameters need to be get
137  * @param para [OUT] Key parameters
138  *
139  * @retval CRYPT_NULL_INPUT     Invalid null pointer input
140  * @retval CRYPT_MEM_ALLOC_FAIL Internal memory allocation error
141  * @retval CRYPT_SUCCESS        Get parameters successfully.
142  */
143 int32_t CRYPT_ECDSA_GetPara(const CRYPT_ECDSA_Ctx *ctx, BSL_Param *param);
144 
145 /**
146  * @ingroup ecdsa
147  * @brief ecdsa Obtain the key length.
148  *
149  * @param ctx [IN] ecdsa context structure
150  *
151  * @retval 0        The input is incorrect or the corresponding key structure does not contain valid key length.
152  * @retval uint32_t Valid key length
153  */
154 uint32_t CRYPT_ECDSA_GetBits(const CRYPT_ECDSA_Ctx *ctx);
155 
156 /**
157  * @ingroup ecdsa
158  * @brief ecdsa Obtains the length required for signing.
159  *
160  * @param ctx [IN] ecdsa context structure
161  *
162  * @retval 0        The input is incorrect or the corresponding key structure does not contain valid parameter data.
163  * @retval uint32_t Length required for valid signature data
164  */
165 uint32_t CRYPT_ECDSA_GetSignLen(const CRYPT_ECDSA_Ctx *ctx);
166 
167 /**
168  * @ingroup ecdsa
169  * @brief Generate the ECDSA key pair.
170  *
171  * @param ctx [IN/OUT] ecdsa context structure
172  *
173  * @retval CRYPT_NULL_INPUT     Error null pointer input
174  * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure
175  * @retval ECC error code.      An error occurred in the internal ECC calculation.
176  * @retval CRYPT_SUCCESS        The key pair is successfully generated.
177  */
178 int32_t CRYPT_ECDSA_Gen(CRYPT_ECDSA_Ctx *ctx);
179 
180 /**
181  * @ingroup ecdsa
182  * @brief ECDSA Signature
183  *
184  * @param ctx [IN] ecdsa context structure
185  * @param algId [IN] md algId
186  * @param data [IN] Data to be signed
187  * @param dataLen [IN] Length of the data to be signed
188  * @param sign [OUT] Signature data
189  * @param signLen [IN/OUT] The input parameter is the space length of the sign,
190  *                         and the output parameter is the valid length of the sign.
191  *                         The required space can be obtained by calling CRYPT_ECDSA_GetSignLen.
192  *
193  * @retval CRYPT_NULL_INPUT                 Error null pointer input
194  * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure
195  * @retval CRYPT_ECDSA_ERR_EMPTY_KEY        The key cannot be empty.
196  * @retval CRYPT_ECDSA_BUFF_LEN_NOT_ENOUGH  The buffer length is insufficient.
197  * @retval BN error.                        An error occurs in the internal BigNum operation.
198  * @retval ECC error.                       An error occurred in the internal ECC calculation.
199  * @retval CRYPT_SUCCESS                    Signed successfully.
200  */
201 int32_t CRYPT_ECDSA_Sign(const CRYPT_ECDSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen,
202     uint8_t *sign, uint32_t *signLen);
203 
204 /**
205  * @ingroup ecdsa
206  * @brief ECDSA Signature
207  *
208  * @param ctx [IN] ecdsa context structure
209  * @param data [IN] Data to be signed
210  * @param dataLen [IN] Length of the data to be signed
211  * @param sign [OUT] Signature data
212  * @param signLen [IN/OUT] The input parameter is the space length of the sign,
213  *                         and the output parameter is the valid length of the sign.
214  *                         The required space can be obtained by calling CRYPT_ECDSA_GetSignLen.
215  *
216  * @retval CRYPT_NULL_INPUT                 Error null pointer input
217  * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure
218  * @retval CRYPT_ECDSA_ERR_EMPTY_KEY        The key cannot be empty.
219  * @retval CRYPT_ECDSA_BUFF_LEN_NOT_ENOUGH  The buffer length is insufficient.
220  * @retval BN error.                        An error occurs in the internal BigNum operation.
221  * @retval ECC error.                       An error occurred in the internal ECC calculation.
222  * @retval CRYPT_SUCCESS                    Signed successfully.
223  */
224 int32_t CRYPT_ECDSA_SignData(const CRYPT_ECDSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen,
225     uint8_t *sign, uint32_t *signLen);
226 
227 /**
228  * @ingroup ecdsa
229  * @brief ECDSA Verification
230  *
231  * @param ctx [IN] ecdsa context structure
232  * @param algId [IN] md algId
233  * @param data [IN] Data to be signed
234  * @param dataLen [IN] Length of the data to be signed
235  * @param sign [IN] Signature data
236  * @param signLen [IN] Valid length of the sign
237  *
238  * @retval CRYPT_NULL_INPUT         Error null pointer input
239  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure
240  * @retval CRYPT_ECDSA_VERIFY_FAIL  Failed to verify the signature.
241  * @retval BN error.                An error occurs in the internal BigNum operation.
242  * @retval ECC error.               An error occurred in the internal ECC calculation.
243  * @retval DSA error.               An error occurs in the DSA encoding and decoding part.
244  * @retval CRYPT_SUCCESS            The signature is verified successfully.
245  */
246 int32_t CRYPT_ECDSA_Verify(const CRYPT_ECDSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen,
247     const uint8_t *sign, uint32_t signLen);
248 
249 /**
250  * @ingroup ecdsa
251  * @brief ECDSA Verification
252  *
253  * @param ctx [IN] ecdsa context structure
254  * @param data [IN] Data to be signed
255  * @param dataLen [IN] Length of the data to be signed
256  * @param sign [IN] Signature data
257  * @param signLen [IN] Valid length of the sign
258  *
259  * @retval CRYPT_NULL_INPUT         Error null pointer input
260  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure
261  * @retval CRYPT_ECDSA_VERIFY_FAIL  Failed to verify the signature.
262  * @retval BN error.                An error occurs in the internal BigNum operation.
263  * @retval ECC error.               An error occurred in the internal ECC calculation.
264  * @retval DSA error.               An error occurs in the DSA encoding and decoding part.
265  * @retval CRYPT_SUCCESS            The signature is verified successfully.
266  */
267 int32_t CRYPT_ECDSA_VerifyData(const CRYPT_ECDSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen,
268     const uint8_t *sign, uint32_t signLen);
269 
270 /**
271  * @ingroup ecdsa
272  * @brief ECDSA Set the private key data.
273  *
274  * @param ctx [OUT] ecdsa context structure
275  * @param para [IN] External private key data
276  *
277  * @retval CRYPT_NULL_INPUT     Error null pointer input
278  * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure
279  * @retval ECC error.           An error occurred in the internal ECC calculation.
280  * @retval CRYPT_SUCCESS        Set successfully.
281  */
282 int32_t CRYPT_ECDSA_SetPrvKey(CRYPT_ECDSA_Ctx *ctx, const BSL_Param *para);
283 
284 /**
285  * @ingroup ecdsa
286  * @brief ECDSA Set the public key data.
287  *
288  * @param ctx [OUT] ecdsa context structure
289  * @param para [IN] External public key data
290  *
291  * @retval CRYPT_NULL_INPUT     Error null pointer input
292  * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure
293  * @retval ECC error.           An error occurred in the internal ECC calculation.
294  * @retval CRYPT_SUCCESS        Set successfully.
295  */
296 int32_t CRYPT_ECDSA_SetPubKey(CRYPT_ECDSA_Ctx *ctx, const BSL_Param *para);
297 
298 /**
299  * @ingroup ecdsa
300  * @brief ECDSA Obtain the private key data.
301  *
302  * @param ctx [IN] ecdsa context structure
303  * @param para [OUT] External private key data
304  *
305  * @retval CRYPT_NULL_INPUT             Invalid null pointer input
306  * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty.
307  * @retval ECC error.                   An error occurred in the internal ECC calculation.
308  * @retval CRYPT_SUCCESS                Obtained successfully.
309  */
310 int32_t CRYPT_ECDSA_GetPrvKey(const CRYPT_ECDSA_Ctx *ctx, BSL_Param *para);
311 
312 /**
313  * @ingroup ecdsa
314  * @brief ECDSA Obtain the public key data.
315  *
316  * @param ctx [IN] ecdsa context structure
317  * @param para [OUT] External public key data
318  *
319  * @retval CRYPT_NULL_INPUT             Invalid null pointer input
320  * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty.
321  * @retval ECC error.                   An error occurred in the internal ECC calculation.
322  * @retval CRYPT_SUCCESS                Obtained successfully.
323  */
324 int32_t CRYPT_ECDSA_GetPubKey(const CRYPT_ECDSA_Ctx *ctx, BSL_Param *para);
325 
326 /**
327  * @ingroup ecdsa
328  * @brief ecdsa control interface
329  *
330  * @param ctx [IN/OUT] ecdsa context structure
331  * @param opt [IN] Operation mode. For details, see ECC_CtrlType.
332  * @param val [IN] Input parameter
333  * @param len [IN] val length
334  *
335  * @retval CRYPT_SUCCESS                            Set successfully.
336  * @retval CRYPT_NULL_INPUT                         If any input parameter is empty
337  * @retval CRYPT_ECC_PKEY_ERR_INVALID_POINT_FORMAT  Invalid point format
338  * @retval CRYPT_ECC_PKEY_ERR_CTRL_LEN              The length of len is incorrect.
339  * @retval CRYPT_ECDSA_ERR_UNSUPPORTED_CTRL_OPTION  The opt mode is not supported.
340  */
341 int32_t CRYPT_ECDSA_Ctrl(CRYPT_ECDSA_Ctx *ctx, int32_t opt, void *val, uint32_t len);
342 
343 /**
344  * @ingroup ecdsa
345  * @brief ecdsa Compare public keys and parameters
346  *
347  * @param a [IN] ecdsa Context structure
348  * @param b [IN] ecdsa context structure
349  *
350  * @retval CRYPT_SUCCESS is the same
351  * Others. For details, see error code in errno.
352  */
353 int32_t CRYPT_ECDSA_Cmp(const CRYPT_ECDSA_Ctx *a, const CRYPT_ECDSA_Ctx *b);
354 
355 /**
356  * @ingroup ecdsa
357  * @brief ecdsa get security bits
358  *
359  * @param ctx [IN] ecdsa Context structure
360  *
361  * @retval security bits
362  */
363 int32_t CRYPT_ECDSA_GetSecBits(const CRYPT_ECDSA_Ctx *ctx);
364 
365 #ifdef HITLS_CRYPTO_PROVIDER
366 /**
367  * @ingroup ecdsa
368  * @brief ecdsa import key
369  *
370  * @param ctx [IN/OUT] ecdsa context structure
371  * @param params [IN] parameters
372  */
373 int32_t CRYPT_ECDSA_Import(CRYPT_ECDSA_Ctx *ctx, const BSL_Param *params);
374 
375 /**
376  * @ingroup ecdsa
377  * @brief ecdsa export key
378  *
379  * @param ctx [IN] ecdsa context structure
380  * @param params [IN/OUT] key parameters
381  */
382 int32_t CRYPT_ECDSA_Export(const CRYPT_ECDSA_Ctx *ctx, BSL_Param *params);
383 
384 #endif // HITLS_CRYPTO_PROVIDER
385 #ifdef __cplusplus
386 }
387 #endif
388 
389 #endif // HITLS_CRYPTO_ECDSA
390 
391 #endif // CRYPT_ECDSA_H
392