• 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_ECC_PKEY_H
17 #define CRYPT_ECC_PKEY_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_ECC
21 
22 #include "crypt_bn.h"
23 #include "crypt_ecc.h"
24 #include "crypt_algid.h"
25 #include "bsl_params.h"
26 #include "sal_atomic.h"
27 #include "bsl_params.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #ifndef CRYPT_ECC_TRY_MAX_CNT
34 #define CRYPT_ECC_TRY_MAX_CNT 100 // Maximum number of attempts to generate keys and signatures
35 #endif
36 
37 /* ECC key context */
38 typedef struct ECC_PkeyCtx {
39     BN_BigNum *prvkey;      // Private key
40     ECC_Point *pubkey;      // Public key
41     ECC_Para *para;         // Key parameter
42     CRYPT_PKEY_PointFormat pointFormat;   // Public key point format
43     uint32_t useCofactorMode;   // Indicates whether to use the cofactor mode. 1 indicates yes, and 0 indicates no.
44     BSL_SAL_RefCount references;
45     void *libCtx;
46 } ECC_Pkey;
47 
48 /**
49  * @ingroup ecc
50  * @brief After the copied ECC context is used up, call the ECC_FreeCtx to release the memory.
51  *
52  * @param ctx [IN] Source ECC context
53  *
54  * @return ECC_Pkey ECC context pointer
55  * If the operation fails, null is returned.
56  */
57 ECC_Pkey *ECC_DupCtx(ECC_Pkey *ctx);
58 
59 /**
60  * @ingroup ecc
61  * @brief ecc Release the key context structure
62  *
63  * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker.
64  */
65 void ECC_FreeCtx(ECC_Pkey *ctx);
66 
67 /**
68  * @ingroup ecc
69  * @brief Obtain the valid length of the key, which is used before obtaining the private key.
70  *
71  * @param ctx [IN] Structure from which the key length is expected to be obtained
72  *
73  * @retval 0        The input is incorrect or the corresponding key structure does not have a valid key length.
74  * @retval uint32_t Valid key length greater than 0
75  */
76 uint32_t ECC_PkeyGetBits(const ECC_Pkey *ctx);
77 
78 /**
79  * @ingroup ecc
80  * @brief Obtain curve parameters.
81  *
82  * @param pkey [IN] Curve parameter information
83  * @param eccPara [OUT] Curve parameter information
84  *
85  * @retval CRYPT_SUCCESS
86  * @retval Other            failure
87  */
88 int32_t ECC_GetPara(const ECC_Pkey *pkey, BSL_Param *eccPara);
89 
90 /**
91  * @ingroup ecc
92  * @brief Generate a public key from the public key.
93  *
94  * @param ctx [IN] ECC key context structure
95  *
96  * @retval CRYPT_NULL_INPUT         Error null pointer input
97  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure
98  * @retval ECC error code.          Internal ECC calculation error
99  * @retval BN error code.           An error occurred in the internal BigNum calculation.
100  * @retval CRYPT_SUCCESS            The public key is successfully generated.
101  */
102 int32_t ECC_GenPublicKey(ECC_Pkey *ctx);
103 
104 /**
105  * @ingroup ecc
106  * @brief Generate the ECC key pair.
107  *
108  * @param ctx [IN] dh Context structure
109  *
110  * @retval CRYPT_NULL_INPUT         Invalid null pointer input
111  * @retval CRYPT_MEM_ALLOC_FAIL     Memory allocation failure
112  * @retval ECC error code.          Internal ECC calculation error
113  * @retval BN error code.           An error occurred in the internal BigNum calculation.
114  * @retval CRYPT_SUCCESS            The key pair is successfully generated.
115  */
116 int32_t ECC_PkeyGen(ECC_Pkey *ctx);
117 
118 /**
119  * @ingroup ecc
120  * @brief ECC Set the private key data.
121  *
122  * @param ctx [OUT] ECC context structure
123  * @param para [IN] Private key data
124  *
125  * @retval CRYPT_NULL_INPUT     Error null pointer input
126  * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure
127  * @retval BN error.            An error occurs in the internal BigNum operation.
128  * @retval CRYPT_SUCCESS        Set successfully.
129  */
130 int32_t ECC_PkeySetPrvKey(ECC_Pkey *ctx, const BSL_Param *para);
131 
132 /**
133  * @ingroup ecc
134  * @brief ECC Set the public key data.
135  *
136  * @param ctx [OUT] ECC context structure
137  * @param para [IN] Public key data
138  *
139  * @retval CRYPT_NULL_INPUT     Error null pointer input
140  * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure
141  * @retval BN error.            An error occurs in the internal BigNum operation.
142  * @retval CRYPT_SUCCESS        Set successfully.
143  */
144 int32_t ECC_PkeySetPubKey(ECC_Pkey *ctx, const BSL_Param *para);
145 /**
146  * @ingroup ecc
147  * @brief ECC Obtain the private key data.
148  *
149  * @param ctx [IN] ECC context structure
150  * @param para [OUT] Private key data
151  *
152  * @retval CRYPT_NULL_INPUT         Invalid null pointer input
153  * @retval ECC_Pkey_KEYINFO_ERROR   The key information is incorrect.
154  * @retval BN error.                An error occurred in the internal BigNum calculation.
155  * @retval CRYPT_SUCCESS            Obtained successfully.
156  */
157 int32_t ECC_PkeyGetPrvKey(const ECC_Pkey *ctx, BSL_Param *para);
158 
159 /**
160  * @ingroup ecc
161  * @brief ECC Obtain the public key data.
162  *
163  * @param ctx [IN] ECC context structure
164  * @param para [OUT] Public key data
165  *
166  * @retval CRYPT_NULL_INPUT             Invalid null pointer input
167  * @retval ECC_Pkey_BUFF_LEN_NOT_ENOUGH The buffer length is insufficient.
168  * @retval ECC_Pkey_KEYINFO_ERROR       The key information is incorrect.
169  * @retval BN error.                    An error occurs in the internal BigNum operation.
170  * @retval CRYPT_SUCCESS                Obtained successfully.
171  */
172 int32_t ECC_PkeyGetPubKey(const ECC_Pkey *ctx, BSL_Param *para);
173 
174 /**
175  * @ingroup ecc
176  * @brief ECC control interface
177  *
178  * @param ctx [IN/OUT] ECC context structure
179  * @param opt [IN] Operation mode. For details, see ECC_CtrlType.
180  * @param val [IN] Input parameter
181  * @param len [IN] val Length
182  *
183  * @retval CRYPT_SUCCESS                         Set successfully.
184  * @retval CRYPT_NULL_INPUT                      If any input parameter is empty
185  * @retval ECC_Pkey_ERR_UNSUPPORTED_CTRL_OPTION  opt mode not supported
186  */
187 int32_t ECC_PkeyCtrl(ECC_Pkey *ctx, int32_t opt, void *val, uint32_t len);
188 
189 /**
190  * @ingroup ecc
191  * @brief ecc Create a context.
192  *
193  * @param id [IN] elliptic curve ID
194  * @return ECC_Pkey ECC context pointer
195  * If the operation fails, null is returned.
196  */
197 ECC_Pkey *ECC_PkeyNewCtx(CRYPT_PKEY_ParaId id);
198 
199 /**
200  * @ingroup ecc
201  * @brief ecc Compare public keys and parameters
202  *
203  * @param a [IN] ECC Context structure
204  * @param b [IN] ECC context structure
205  *
206  * @retval CRYPT_SUCCESS                    is the same
207  * @retval CRYPT_NULL_INPUT                 Invalid null pointer input
208  * @retval CRYPT_ECC_KEY_PUBKEY_NOT_EQUAL   Public keys are not equal
209  * @retval CRYPT_ECC_POINT_ERR_CURVE_ID     Parameter curve IDs are not equal.
210  * @retval CRYPT_ECC_ERR_POINT_FORMAT       Point compression formats are not equal
211  * @retval For other error codes, see crypt_errno.h.
212  */
213 int32_t ECC_PkeyCmp(const ECC_Pkey *a, const ECC_Pkey *b);
214 
215 /**
216  * @ingroup ecc
217  * @brief Set the parameter of the ECC context
218  *
219  * @param ctx [IN] ECC context
220  * @param para [IN] ECC parameter
221  *
222  * @retval CRYPT_SUCCESS succeeded.
223  * @retval For details about other errors, see crypt_errno.h.
224  */
225 int32_t ECC_SetPara(ECC_Pkey *ctx, ECC_Para *para);
226 
227 #ifdef __cplusplus
228 }
229 #endif
230 
231 #endif // HITLS_CRYPTO_ECC
232 
233 #endif // CRYPT_ECC_PKEY_H
234