• 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_ELGAMAL_H
17 #define CRYPT_ELGAMAL_H
18 
19 #include "hitls_build.h"
20 #ifdef HITLS_CRYPTO_ELGAMAL
21 
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include "crypt_bn.h"
25 #include "crypt_local_types.h"
26 #include "bsl_params.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cpluscplus */
31 
32 #define ELGAMAL_MAX_MODULUS_BITS 16384
33 
34 /* ElGamal*/
35 typedef struct ELGAMAL_Ctx CRYPT_ELGAMAL_Ctx;
36 typedef struct ELGAMAL_Para CRYPT_ELGAMAL_Para;
37 
38 /* ElGamal method*/
39 /**
40    * @ingroup elgamal
41    * @brief Allocate elgamal context memory space.
42    *
43    * @retval (CRYPT_ELGAMAL_Ctx *)   Pointer to the memory space of the allocated context
44    * @retval NULL                     Invalid null pointer.
45    */
46 CRYPT_ELGAMAL_Ctx *CRYPT_ELGAMAL_NewCtx(void);
47 
48 CRYPT_ELGAMAL_Ctx *CRYPT_ELGAMAL_NewCtxEx(void *libCtx);
49 
50 /**
51    * @ingroup elgamal
52    * @brief Copy the ElGamal context. After the duplication is complete, call the CRYPT_ELGAMAL_FreeCtx to release the memory.
53    *
54    * @param ctx [IN] ELGAMAL context
55    *
56    * @return CRYPT_ELGAMAL_Ctx    ELGAMAL context pointer
57    *         If the operation fails, a null value is returned.
58    */
59 CRYPT_ELGAMAL_Ctx *CRYPT_ELGAMAL_DupCtx(CRYPT_ELGAMAL_Ctx *keyCtx);
60 
61 /**
62    * @ingroup elgamal
63    * @brief Create elgamal key parameter structure
64    *
65    * @param para [IN] ELGAMAL External parameter
66    *
67    * @retval (CRYPT_ELGAMAL_Para *)  Pointer to the allocated memory space of the structure
68    * @retval NULL                     Invalid null pointer.
69    */
70 CRYPT_ELGAMAL_Para *CRYPT_ELGAMAL_NewPara(const BSL_Param *para);
71 
72 /**
73    * @ingroup elgamal
74    * @brief release ElGamal key context structure
75    *
76    * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker.
77    */
78 void CRYPT_ELGAMAL_FreeCtx(CRYPT_ELGAMAL_Ctx *ctx);
79 
80 /**
81    * @ingroup elgamal
82    * @brief Release ElGamal key parameter structure
83    *
84    * @param para [IN] Storage pointer in the parameter structure to be released. The parameter is set NULL by the invoker.
85    */
86 void CRYPT_ELGAMAL_FreePara(CRYPT_ELGAMAL_Para *para);
87 
88 /**
89    * @ingroup elgamal
90    * @brief Set the data of the key parameter structure to the key structure.
91    *
92    * @param ctx [OUT] ElGamal context structure for which related parameters need to be set
93    * @param param [IN] Key parameter structure
94    *
95    * @retval CRYPT_NULL_INPUT             Invalid null pointer input.
96    * @retval CRYPT_ELGAMAL_ERR_KEY_BITS  The expected key length does not meet the requirements.
97    * @retval CRYPT_ELGAMAL_ERR_E_VALUE   The expected value of e does not meet the requirements.
98    * @retval CRYPT_MEM_ALLOC_FAIL         internal memory allocation error
99    * @retval CRYPT_SUCCESS                set successfully.
100    */
101 int32_t CRYPT_ELGAMAL_SetPara(CRYPT_ELGAMAL_Ctx *ctx, const BSL_Param *param);
102 
103 /**
104    * @ingroup elgamal
105    * @brief Obtain the valid length of the key.
106    *
107    * @param ctx [IN] Structure from which the key length is expected to be obtained
108    *
109    * @retval 0: The input is incorrect or the corresponding key structure does not have a valid key length.
110    * @retval uint32_t: Valid key length
111    */
112 uint32_t CRYPT_ELGAMAL_GetBits(const CRYPT_ELGAMAL_Ctx *ctx);
113 
114 /**
115    * @ingroup elgamal
116    * @brief Obtain the valid length of the k.
117    *
118    * @param ctx [IN] Structure from which the key length is expected to be obtained
119    *
120    * @retval 0: The input is incorrect or the corresponding key structure does not have a valid key length.
121    * @retval uint32_t: Valid key length
122    */
123 uint32_t CRYPT_ELGAMAL_GetKBits(const CRYPT_ELGAMAL_Ctx *ctx);
124 
125 /**
126    * @ingroup elgamal
127    * @brief Generate the ElGamal key pair.
128    *
129    * @param ctx [IN/OUT] elgamal context structure
130    *
131    * @retval CRYPT_NULL_INPUT             Error null pointer input
132    * @retval CRYPT_ELGAMAL_ERR_KEY_BITS  The value of e in the context structure does not meet the requirements.
133    * @retval CRYPT_MEM_ALLOC_FAIL         Memory allocation failure
134    * @retval BN error                     An error occurs in the internal BigNum operation.
135    * @retval CRYPT_SUCCESS                The key pair is successfully generated.
136    */
137 int32_t CRYPT_ELGAMAL_Gen(CRYPT_ELGAMAL_Ctx *ctx);
138 
139 /**
140    * @ingroup elgamal
141    * @brief ElGamal public key encryption
142    *
143    * @param ctx [IN] ElGamal context structure
144    * @param input [IN] Information to be encrypted
145    * @param inputLen [IN] Length of the information to be encrypted
146    * @param out1 [OUT] Pointer to the encrypted information output.(c1)
147    * @param out1Len [IN/OUT] Pointer to the length of the encrypted information.
148    *                        Before being transferred, the value must be set to the maximum length of the array.
149    * @param out2 [OUT] Pointer to the encrypted information output.(c2)
150    * @param out2Len [IN/OUT] Pointer to the length of the encrypted information.
151    *                        Before being transferred, the value must be set to the maximum length of the array.
152    *
153    * @retval CRYPT_NULL_INPUT                 Invalid null pointer input
154    * @retval CRYPT_ELGAMAL_NO_KEY_INFO       does not contain the key information.
155    * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE   The entered value does not meet the calculation conditions.
156    * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure
157    * @retval CRYPT_SECUREC_FAIL               A security function error occurs.
158    * @retval BN error                         An error occurs in the internal BigNum operation.
159    * @retval CRYPT_SUCCESS                    encryption succeeded.
160    */
161 int32_t CRYPT_ELGAMAL_PubEnc(const CRYPT_ELGAMAL_Ctx *ctx, const uint8_t *input, uint32_t inputLen, uint8_t *out1,
162                              uint32_t *out1Len, uint8_t *out2, uint32_t *out2Len);
163 
164 /**
165    * @ingroup elgamal
166    * @brief ElGamal private key decryption
167    *
168    * @param ctx [IN] ElGamal context structure
169    * @param c1 [IN] Information to be decrypted
170    * @param c2 [IN] Information to be decrypted
171    * @param bits [IN] Length of the information to be decrypted
172    * @param out [OUT] Pointer to the decrypted information output.
173    * @param outLen [IN/OUT] Pointer to the length of the decrypted information.
174    *                        Before being transferred, the value must be set to the maximum length of the array.
175    *
176    * @retval CRYPT_NULL_INPUT                 Invalid null pointer input
177    * @retval CRYPT_ELGAMAL_ERR_DEC_BITS      Incorrect length of the encrypted private key.
178    * @retval CRYPT_ELGAMAL_NO_KEY_INFO       does not contain the key information.
179    * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE   The entered value does not meet the calculation conditions.
180    * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure
181    * @retval CRYPT_SECUREC_FAIL               A security function error occurs.
182    * @retval BN error.                        An error occurs in the internal BigNum operation.
183    * @retval CRYPT_SUCCESS                    Decrypted Successfully
184    */
185 int32_t CRYPT_ELGAMAL_PrvDec(const CRYPT_ELGAMAL_Ctx *ctx, const BN_BigNum *c1, const BN_BigNum *c2, uint32_t bits,
186                              uint8_t *out, uint32_t *outLen);
187 
188 /**
189    * @ingroup elgamal
190    * @brief ElGamal Set the private key information.
191    *
192    * @param ctx [OUT] ElGamal context structure
193    * @param para [IN] Private key data
194    *
195    * @retval CRYPT_NULL_INPUT                 Error null pointer input
196    * @retval CRYPT_ELGAMAL_ERR_KEY_BITS      The key length does not meet the requirements.
197    * @retval CRYPT_ELGAMAL_NO_KEY_INFO       does not contain the key information.
198    * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE   The entered value does not meet the calculation conditions.
199    * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure
200    * @retval BN error                         An error occurs in the internal BigNum operation.
201    * @retval CRYPT_SUCCESS                    The private key is successfully set.
202    */
203 int32_t CRYPT_ELGAMAL_SetPrvKey(CRYPT_ELGAMAL_Ctx *ctx, const BSL_Param *para);
204 
205 /**
206    * @ingroup elgamal
207    * @brief ElGamal Set the public key information.
208    *
209    * @param ctx [OUT] ElGamal context structure
210    * @param para [IN] Public key data
211    *
212    * @retval CRYPT_NULL_INPUT                 Error null pointer input
213    * @retval CRYPT_ELGAMAL_ERR_KEY_BITS      The key length does not meet the requirements.
214    * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE   The entered value does not meet the calculation conditions.
215    * @retval CRYPT_MEM_ALLOC_FAIL             Memory allocation failure
216    * @retval BN error                         An error occurs in the internal BigNum operation.
217    * @retval CRYPT_SUCCESS                    The public key is successfully set.
218    */
219 int32_t CRYPT_ELGAMAL_SetPubKey(CRYPT_ELGAMAL_Ctx *ctx, const BSL_Param *para);
220 
221 /**
222    * @ingroup elgamal
223    * @brief ElGamal Obtain the private key information.
224    *
225    * @param ctx [IN] ElGamal context structure
226    * @param para [OUT] Private key data
227    *
228    * @retval CRYPT_NULL_INPUT Invalid null pointer input
229    * @retval BN error         An error occurs in the internal BigNum operation.
230    * @retval CRYPT_SUCCESS    The private key is obtained successfully.
231    */
232 int32_t CRYPT_ELGAMAL_GetPrvKey(const CRYPT_ELGAMAL_Ctx *ctx, BSL_Param *para);
233 
234 /**
235    * @ingroup elgamal
236    * @brief ElGamal Obtain the public key information.
237    *
238    * @param ctx [IN] ElGamal context structure
239    * @param para [OUT] Public key data
240    *
241    * @retval CRYPT_NULL_INPUT Invalid null pointer input
242    * @retval BN error         An error occurs in the internal BigNum operation.
243    * @retval CRYPT_SUCCESS    The public key is obtained successfully.
244    */
245 int32_t CRYPT_ELGAMAL_GetPubKey(const CRYPT_ELGAMAL_Ctx *ctx, BSL_Param *para);
246 
247 /**
248    * @ingroup elgamal
249    * @brief ElGamal public key encryption
250    *
251    * @param ctx [IN] ELGAMAL context structure
252    * @param data [IN] Information to be encrypted
253    * @param dataLen [IN] Length of the information to be encrypted
254    * @param out [OUT] Pointer to the encrypted information output.
255    * @param outLen [OUT] Pointer to the length of the encrypted information
256    *
257    * @retval CRYPT_NULL_INPUT                     Invalid null pointer input
258    * @retval CRYPT_ELGAMAL_NO_KEY_INFO           does not contain the key information.
259    * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE       The entered value does not meet the calculation conditions.
260    * @retval CRYPT_ELGAMAL_BUFF_LEN_NOT_ENOUGH   Outbuf Insufficient
261    * @retval CRYPT_MEM_ALLOC_FAIL                 Memory allocation failure
262    * @retval CRYPT_SECUREC_FAIL                   A safe function error occurs.
263    * @retval BN error.                            An error occurs in the internal BigNum operation.
264    * @retval CRYPT_EAL_ALG_NOT_SUPPORT            does not register the encryption method.
265    * @retval CRYPT_SUCCESS                        encryption succeeded.
266    */
267 int32_t CRYPT_ELGAMAL_Encrypt(CRYPT_ELGAMAL_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out,
268                               uint32_t *outLen);
269 
270 /**
271    * @ingroup elgamal
272    * @brief ElGamal private key decryption
273    *
274    * @param ctx [IN] ELGAMAL context structure
275    * @param data [IN] Information to be decrypted
276    * @param dataLen [IN] Length of the information to be decrypted
277    * @param out [OUT] Pointer to the output information after decryption.
278    * @param outLen [OUT] Pointer to the length of the decrypted information
279    *
280    * @retval CRYPT_NULL_INPUT                     Error null pointer input
281    * @retval CRYPT_ELGAMAL_NO_KEY_INFO           does not contain the key information.
282    * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE       The entered value does not meet the calculation conditions.
283    * @retval CRYPT_ELGAMAL_BUFF_LEN_NOT_ENOUGH   Outbuf Insufficient
284    * @retval CRYPT_MEM_ALLOC_FAIL                 Memory allocation failure
285    * @retval CRYPT_SECUREC_FAIL                   A security function error occurs.
286    * @retval CRYPT_EAL_ALG_NOT_SUPPORT            does not register the decryption method.
287    * @retval BN error.                            An error occurs in the internal BigNum operation.
288    * @retval CRYPT_SUCCESS                        Decryption succeeded.
289    */
290 int32_t CRYPT_ELGAMAL_Decrypt(CRYPT_ELGAMAL_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out,
291                               uint32_t *outLen);
292 
293 /**
294    * @ingroup elgamal
295    * @brief ELGAMAL get security bits
296    *
297    * @param ctx [IN] ELGAMAL Context structure
298    *
299    * @retval security bits
300    */
301 int32_t CRYPT_ELGAMAL_GetSecBits(const CRYPT_ELGAMAL_Ctx *ctx);
302 
303 /**
304  * @ingroup elgamal
305  * @brief ELGAMAL control function for various operations
306  *
307  * @param ctx [IN/OUT] ELGAMAL context structure
308  * @param opt [IN] Control operation type
309  * @param val [IN/OUT] Parameter value for the operation
310  * @param len [IN] Length of the parameter value
311  *
312  * @retval CRYPT_NULL_INPUT                     Invalid null pointer input
313  * @retval CRYPT_ELGAMAL_ERR_INPUT_VALUE       The entered value does not meet the calculation conditions
314  * @retval CRYPT_ELGAMAL_NO_KEY_INFO           Does not contain the key information
315  * @retval CRYPT_MEM_ALLOC_FAIL                 Memory allocation failure
316  * @retval CRYPT_EAL_ALG_NOT_SUPPORT            Operation not supported
317  * @retval CRYPT_SUCCESS                        Operation succeeded
318  */
319 int32_t CRYPT_ELGAMAL_Ctrl(CRYPT_ELGAMAL_Ctx *ctx, int32_t opt, void *val, uint32_t len);
320 
321 #ifdef HITLS_CRYPTO_ELGAMAL
322 /**
323  * @ingroup elgamal
324  * @brief BigNum Calculate the original root
325  *
326  * @param g    [OUT] Safety prime
327  * @param p    [IN] Big prime
328  * @param q    [IN] Big prime
329  * @param opt   [IN] Optimizer
330  *
331  * @retval CRYPT_SUCCESS
332  * @retval CRYPT_NULL_INPUT             Invalid null pointer
333  * @retval CRYPT_MEM_ALLOC_FAIL         Memory allocation failure
334  */
335 int32_t OriginalRoot(void *libCtx, BN_BigNum *g, const BN_BigNum *p, const BN_BigNum *q, uint32_t bits);
336 #endif
337 
338 #ifdef _cplusplus
339 }
340 #endif
341 
342 #endif // HITLS_CRYPTO_ELGAMAL
343 #endif // CRYPT_ELGAMAL_H
344