• 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_DSA_H
17 #define CRYPT_DSA_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_DSA
21 
22 #include <stdint.h>
23 #include "crypt_bn.h"
24 #include "crypt_types.h"
25 #include "bsl_params.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif /* __cpluscplus */
30 
31 #ifndef CRYPT_DSA_TRY_MAX_CNT
32 #define CRYPT_DSA_TRY_MAX_CNT 100 // Maximum number of attempts to generate keys and signatures
33 #endif
34 /* DSA key parameters */
35 typedef struct DSA_Para CRYPT_DSA_Para;
36 
37 /* DSA key context */
38 typedef struct DSA_Ctx CRYPT_DSA_Ctx;
39 
40 /**
41  * @ingroup dsa
42  * @brief dsa Allocates context memory space.
43  *
44  * @retval (CRYPT_DSA_Ctx *) Pointer to the memory space of the allocated context
45  * @retval NULL              Invalid null pointer
46  */
47 CRYPT_DSA_Ctx *CRYPT_DSA_NewCtx(void);
48 
49 /**
50  * @ingroup dsa
51  * @brief dsa Allocates context memory space.
52  *
53  * @param libCtx [IN] Library context
54  *
55  * @retval (CRYPT_DSA_Ctx *) Pointer to the memory space of the allocated context
56  * @retval NULL              Invalid null pointer
57  */
58 CRYPT_DSA_Ctx *CRYPT_DSA_NewCtxEx(void *libCtx);
59 
60 /**
61  * @ingroup dsa
62  * @brief Copy the DSA context. After the duplication is complete, invoke the CRYPT_DSA_FreeCtx to release the memory.
63  *
64  * @param ctx [IN] Source DSA context
65  *
66  * @return CRYPT_DSA_Ctx Dsa context pointer
67  * If the operation fails, null is returned.
68  */
69 CRYPT_DSA_Ctx *CRYPT_DSA_DupCtx(CRYPT_DSA_Ctx *dsaCtx);
70 
71 /**
72  * @ingroup dsa
73  * @brief dsa Release the key context structure
74  *
75  * @param ctx [IN] Indicates the pointer to the context structure to be released. The ctx is set NULL by the invoker.
76  */
77 void CRYPT_DSA_FreeCtx(CRYPT_DSA_Ctx *ctx);
78 
79 /**
80  * @ingroup dsa
81  * @brief dsa generate key parameter structure
82  *
83  * @param para [IN] dsa external parameter
84  *
85  * @retval (CRYPT_DSA_Para *) Pointer to the memory space of the allocated context
86  * @retval NULL               Invalid null pointer
87  */
88 CRYPT_DSA_Para *CRYPT_DSA_NewPara(const BSL_Param *para);
89 
90 /**
91  * @ingroup dsa
92  * @brief Release the key parameter structure of DSA.
93  *
94  * @param para [IN] Pointer to the key parameter structure to be released. para is set NULL by the invoker.
95  */
96 void CRYPT_DSA_FreePara(CRYPT_DSA_Para *para);
97 
98 /**
99  * @ingroup dsa
100  * @brief Set the data of the key parameter structure to the key structure.
101  *
102  * @param ctx [IN] Key structure for setting related parameters. The key specification is 1024-3072 bits.
103  * @param para [IN] Key parameters
104  *
105  * @retval CRYPT_NULL_INPUT          Invalid null pointer input.
106  * @retval CRYPT_DSA_ERR_KEY_PARA    The key parameter data is incorrect.
107  * @retval CRYPT_MEM_ALLOC_FAIL      internal memory allocation error
108  * @retval BN error code.            An error occurred in the internal BigNum calculation.
109  * @retval CRYPT_SUCCESS             Set successfully.
110  */
111 int32_t CRYPT_DSA_SetPara(CRYPT_DSA_Ctx *ctx, const BSL_Param *param);
112 
113 /**
114  * @ingroup dsa
115  * @brief Set the parameter data in the key structure to the key parameter structure.
116  *
117  * @param ctx [IN] Key structure for setting related parameters. The key specification is 1024-3072 bits.
118  * @param para [OUT] Key parameters
119  *
120  * @retval CRYPT_NULL_INPUT          Invalid null pointer input.
121  * @retval CRYPT_DSA_PARA_ERROR      The key parameter data is incorrect.
122  * @retval BN error code.            An error occurred in the internal BigNum calculation.
123  * @retval CRYPT_SUCCESS             Get successfully.
124  */
125 int32_t CRYPT_DSA_GetPara(const CRYPT_DSA_Ctx *ctx, BSL_Param *para);
126 
127 /**
128  * @ingroup dsa
129  * @brief dsa Obtain the key length.
130  *
131  * @param ctx [IN] DSA context structure
132  *
133  * @retval 0        The input is incorrect or the corresponding key structure does not contain valid key length.
134  * @retval uint32_t Valid key length
135  */
136 uint32_t CRYPT_DSA_GetBits(const CRYPT_DSA_Ctx *ctx);
137 
138 /**
139  * @ingroup dsa
140  * @brief dsa Obtain the required length of the signature.
141  *
142  * @param ctx [IN] DSA context structure
143  *
144  * @retval 0        The input is incorrect or the corresponding key structure does not contain valid parameter data.
145  * @retval uint32_t Length required for valid signature data
146  */
147 uint32_t CRYPT_DSA_GetSignLen(const CRYPT_DSA_Ctx *ctx);
148 /**
149  * @ingroup dsa
150  * @brief Generate a DSA key pair.
151  *
152  * @param ctx [IN/OUT] DSA context structure
153  *
154  * @retval CRYPT_NULL_INPUT         Error null pointer input.
155  * @retval CRYPT_DSA_ERR_KEY_PARA   The key parameter data is incorrect.
156  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure.
157  * @retval CRYPT_DSA_ERR_TRY_CNT    Unable to generate results within the specified number of attempts.
158  * @retval BN error code.           An error occurred in the internal BigNum calculation.
159  * @retval CRYPT_SUCCESS            The key pair is successfully generated.
160  */
161 int32_t CRYPT_DSA_Gen(CRYPT_DSA_Ctx *ctx);
162 
163 /**
164  * @ingroup dsa
165  * @brief DSA Signature
166  *
167  * @param ctx [IN] DSA context structure
168  * @param algId [IN] md algId
169  * @param data [IN] Data to be signed
170  * @param dataLen [IN] Length of the data to be signed
171  * @param sign [OUT] Signature data
172  * @param signLen [IN/OUT] The input parameter is the space length of the sign,
173  *                         and the output parameter is the valid length of the sign.
174  *                         The required space can be obtained by calling CRYPT_DSA_GetSignLen.
175  *
176  * @retval CRYPT_NULL_INPUT                 Invalid null pointer input.
177  * @retval CRYPT_DSA_BUFF_LEN_NOT_ENOUGH    The buffer length is insufficient.
178  * @retval CRYPT_DSA_ERR_KEY_INFO           The key information is incorrect.
179  * @retval CRYPT_DSA_ERR_TRY_CNT            Unable to generate results within the specified number of attempts.
180  * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure.
181  * @retval BN error                         An error occurred in the internal BigNum operation.
182  * @retval CRYPT_SUCCESS                    Signed successfully.
183  */
184 int32_t CRYPT_DSA_Sign(const CRYPT_DSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen,
185     uint8_t *sign, uint32_t *signLen);
186 
187 /**
188  * @ingroup dsa
189  * @brief DSA Signature
190  *
191  * @param ctx [IN] DSA context structure
192  * @param data [IN] Data to be signed
193  * @param dataLen [IN] Length of the data to be signed
194  * @param sign [OUT] Signature data
195  * @param signLen [IN/OUT] The input parameter is the space length of the sign,
196  *                         and the output parameter is the valid length of the sign.
197  *                         The required space can be obtained by calling CRYPT_DSA_GetSignLen.
198  *
199  * @retval CRYPT_NULL_INPUT                 Invalid null pointer input.
200  * @retval CRYPT_DSA_BUFF_LEN_NOT_ENOUGH    The buffer length is insufficient.
201  * @retval CRYPT_DSA_ERR_KEY_INFO           The key information is incorrect.
202  * @retval CRYPT_DSA_ERR_TRY_CNT            Unable to generate results within the specified number of attempts.
203  * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure.
204  * @retval BN error                         An error occurred in the internal BigNum operation.
205  * @retval CRYPT_SUCCESS                    Signed successfully.
206  */
207 int32_t CRYPT_DSA_SignData(const CRYPT_DSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen,
208     uint8_t *sign, uint32_t *signLen);
209 
210 /**
211  * @ingroup dsa
212  * @brief DSA verification
213  *
214  * @param ctx [IN] DSA context structure
215  * @param data [IN] Data to be signed
216  * @param dataLen [IN] Length of the data to be signed
217  * @param sign [IN] Signature data
218  * @param signLen [IN] Valid length of the sign
219  *
220  * @retval CRYPT_NULL_INPUT         Error null pointer input.
221  * @retval CRYPT_DSA_ERR_KEY_INFO   The key information is incorrect.
222  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure.
223  * @retval CRYPT_DSA_DECODE_FAIL    Signature Data Decoding Failure.
224  * @retval CRYPT_DSA_VERIFY_FAIL    Failed to verify the signature.
225  * @retval BN error.                An error occurs in the internal BigNum operation.
226  * @retval CRYPT_SUCCESS            The signature is verified successfully.
227  */
228 int32_t CRYPT_DSA_VerifyData(const CRYPT_DSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen,
229     const uint8_t *sign, uint32_t signLen);
230 
231 /**
232  * @ingroup dsa
233  * @brief DSA verification
234  *
235  * @param ctx [IN] DSA context structure
236  * @param algId [IN] md algId
237  * @param data [IN] Data to be signed
238  * @param dataLen [IN] Length of the data to be signed
239  * @param sign [IN] Signature data
240  * @param signLen [IN] Valid length of the sign
241  *
242  * @retval CRYPT_NULL_INPUT         Error null pointer input.
243  * @retval CRYPT_DSA_ERR_KEY_INFO   The key information is incorrect.
244  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure.
245  * @retval CRYPT_DSA_DECODE_FAIL    Signature Data Decoding Failure.
246  * @retval CRYPT_DSA_VERIFY_FAIL    Failed to verify the signature.
247  * @retval BN error.                An error occurs in the internal BigNum operation.
248  * @retval CRYPT_SUCCESS            The signature is verified successfully.
249  */
250 int32_t CRYPT_DSA_Verify(const CRYPT_DSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen,
251     const uint8_t *sign, uint32_t signLen);
252 
253 /**
254  * @ingroup dsa
255  * @brief Set the private key data for the DSA.
256  *
257  * @param ctx [IN] DSA context structure
258  * @param para [IN] External private key data
259  *
260  * @retval CRYPT_NULL_INPUT          Invalid null pointer input.
261  * @retval CRYPT_DSA_ERR_KEY_PARA    The key parameter data is incorrect.
262  * @retval CRYPT_DSA_ERR_KEY_INFO    The key information is incorrect.
263  * @retval CRYPT_MEM_ALLOC_FAIL      Memory allocation failure.
264  * @retval BN error.                 An error occurs in the internal BigNum operation.
265  * @retval CRYPT_SUCCESS             Set successfully.
266  */
267 int32_t CRYPT_DSA_SetPrvKey(CRYPT_DSA_Ctx *ctx, const BSL_Param *para);
268 
269 /**
270  * @ingroup dsa
271  * @brief Set the public key data for the DSA.
272  *
273  * @param ctx [IN] DSA context structure
274  * @param para [IN] External public key data
275  *
276  * @retval CRYPT_NULL_INPUT         Error null pointer input.
277  * @retval CRYPT_DSA_ERR_KEY_PARA   The key parameter data is incorrect.
278  * @retval CRYPT_DSA_ERR_KEY_INFO   The key information is incorrect.
279  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure.
280  * @retval BN error.                An error occurs in the internal BigNum operation.
281  * @retval CRYPT_SUCCESS            Set successfully.
282  */
283 int32_t CRYPT_DSA_SetPubKey(CRYPT_DSA_Ctx *ctx, const BSL_Param *para);
284 
285 /**
286  * @ingroup dsa
287  * @brief Obtain the private key data of the DSA.
288  *
289  * @param ctx [IN] DSA context structure
290  * @param para [OUT] External private key data
291  *
292  * @retval CRYPT_NULL_INPUT                 Invalid null pointer input.
293  * @retval CRYPT_DSA_BUFF_LEN_NOT_ENOUGH    The buffer length is insufficient.
294  * @retval CRYPT_DSA_ERR_KEY_INFO           The key information is incorrect.
295  * @retval BN error.                        An error occurs in the internal BigNum calculation.
296  * @retval CRYPT_SUCCESS                    Obtained successfully.
297  */
298 int32_t CRYPT_DSA_GetPrvKey(const CRYPT_DSA_Ctx *ctx, BSL_Param *para);
299 
300 /**
301  * @ingroup dsa
302  * @brief Obtain the public key data of the DSA.
303  *
304  * @param ctx [IN] DSA context structure
305  * @param para [OUT] External public key data
306  *
307  * @retval CRYPT_NULL_INPUT                 Invalid null pointer input.
308  * @retval CRYPT_DSA_BUFF_LEN_NOT_ENOUGH    The buffer length is insufficient.
309  * @retval CRYPT_DSA_ERR_KEY_INFO           The key information is incorrect.
310  * @retval BN error.                        An error occurred in the internal BigNum calculation.
311  * @retval CRYPT_SUCCESS                    Obtained successfully.
312  */
313 int32_t CRYPT_DSA_GetPubKey(const CRYPT_DSA_Ctx *ctx, BSL_Param *para);
314 
315 /**
316  * @ingroup dsa
317  * @brief dsa Compare public keys and parameters
318  *
319  * @param a [IN] DSA context structure
320  * @param b [IN] DSA context structure
321  *
322  * @retval CRYPT_SUCCESS                is the same
323  * @retval CRYPT_NULL_INPUT             Invalid null pointer input.
324  * @retval CRYPT_DSA_ERR_KEY_INFO       The key information is incorrect.
325  * @retval CRYPT_DSA_PUBKEY_NOT_EQUAL   Public keys are not equal.
326  * @retval CRYPT_DSA_PARA_ERROR         The parameter information is incorrect.
327  * @retval CRYPT_DSA_PARA_NOT_EQUAL     The parameters are not equal.
328  */
329 int32_t CRYPT_DSA_Cmp(const CRYPT_DSA_Ctx *a, const CRYPT_DSA_Ctx *b);
330 
331 /**
332  * @ingroup dsa
333  * @brief DSA control interface
334  *
335  * @param ctx [IN] DSA context structure
336  * @param opt [IN] Operation mode
337  * @param val [IN] Parameter
338  * @param len [IN] val length
339  *
340  * @retval CRYPT_NULL_INPUT Invalid null pointer input
341  * @retval CRYPT_SUCCESS    obtained successfully.
342  */
343 int32_t CRYPT_DSA_Ctrl(CRYPT_DSA_Ctx *ctx, int32_t opt, void *val, uint32_t len);
344 
345 
346 /**
347  * @ingroup DSA
348  * @brief DSA get security bits
349  *
350  * @param ctx [IN] DSA Context structure
351  *
352  * @retval security bits
353  */
354 int32_t CRYPT_DSA_GetSecBits(const CRYPT_DSA_Ctx *ctx);
355 
356 #ifdef __cplusplus
357 }
358 #endif
359 
360 #endif // HITLS_CRYPTO_DSA
361 
362 #endif // CRYPT_DSA_H
363