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