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_RSA_H 17 #define CRYPT_RSA_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_RSA 21 22 #include <stdlib.h> 23 #include <stdint.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 RSA_MIN_MODULUS_BITS 1024 32 #define RSA_MAX_MODULUS_BITS 16384 33 #define RSA_SMALL_MODULUS_BYTES (3072 / 8) 34 #define RSA_MAX_PUBEXP_BYTES (64 / 8) 35 #define RSA_MIN_MODULUS_LEN (RSA_MIN_MODULUS_BITS / 8) 36 #define RSA_MAX_MODULUS_LEN (RSA_MAX_MODULUS_BITS / 8) 37 38 /* RSA */ 39 typedef struct RSA_Ctx CRYPT_RSA_Ctx; 40 typedef struct RSA_Para CRYPT_RSA_Para; 41 42 43 /* RSA method */ 44 45 /** 46 * @ingroup rsa 47 * @brief Allocate rsa context memory space. 48 * 49 * @retval (CRYPT_RSA_Ctx *) Pointer to the memory space of the allocated context 50 * @retval NULL Invalid null pointer. 51 */ 52 CRYPT_RSA_Ctx *CRYPT_RSA_NewCtx(void); // create key structure 53 54 /** 55 * @ingroup rsa 56 * @brief Allocate rsa context memory space. 57 * 58 * @param libCtx [IN] Library context 59 * 60 * @retval (CRYPT_RSA_Ctx *) Pointer to the memory space of the allocated context 61 * @retval NULL Invalid null pointer. 62 */ 63 CRYPT_RSA_Ctx *CRYPT_RSA_NewCtxEx(void *libCtx); 64 65 /** 66 * @ingroup rsa 67 * @brief Copy the RSA context. After the duplication is complete, call the CRYPT_RSA_FreeCtx to release the memory. 68 * 69 * @param ctx [IN] RSA context 70 * 71 * @return CRYPT_RSA_Ctx Rsa context pointer 72 * If the operation fails, a null value is returned. 73 */ 74 CRYPT_RSA_Ctx *CRYPT_RSA_DupCtx(CRYPT_RSA_Ctx *keyCtx); 75 76 /** 77 * @ingroup rsa 78 * @brief Create rsa key parameter structure 79 * 80 * @param para [IN] RSA External parameter 81 * 82 * @retval (CRYPT_RSA_Para *) Pointer to the allocated memory space of the structure 83 * @retval NULL Invalid null pointer. 84 */ 85 CRYPT_RSA_Para *CRYPT_RSA_NewPara(const BSL_Param *para); 86 87 /** 88 * @ingroup rsa 89 * @brief Release rsa key parameter structure 90 * 91 * @param para [IN] Storage pointer in the parameter structure to be released. The parameter is set NULL by the invoker. 92 */ 93 void CRYPT_RSA_FreePara(CRYPT_RSA_Para *para); 94 95 /** 96 * @ingroup rsa 97 * @brief release rsa key context structure 98 * 99 * @param ctx [IN] Pointer to the context structure to be released. The ctx is set NULL by the invoker. 100 */ 101 void CRYPT_RSA_FreeCtx(CRYPT_RSA_Ctx *ctx); 102 103 /** 104 * @ingroup rsa 105 * @brief Set the data of the key parameter structure to the key structure. 106 * 107 * @param ctx [OUT] RSA context structure for which related parameters need to be set 108 * @param para [IN] Key parameter structure 109 * 110 * @retval CRYPT_NULL_INPUT Invalid null pointer input. 111 * @retval CRYPT_RSA_ERR_KEY_BITS The expected key length does not meet the requirements. 112 * @retval CRYPT_RSA_ERR_E_VALUE The expected value of e does not meet the requirements. 113 * @retval CRYPT_MEM_ALLOC_FAIL internal memory allocation error 114 * @retval CRYPT_SUCCESS set successfully. 115 */ 116 int32_t CRYPT_RSA_SetPara(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); 117 118 /** 119 * @ingroup rsa 120 * @brief Obtain the valid length of the key. 121 * 122 * @param ctx [IN] Structure from which the key length is expected to be obtained 123 * 124 * @retval 0: The input is incorrect or the corresponding key structure does not have a valid key length. 125 * @retval uint32_t: Valid key length 126 */ 127 uint32_t CRYPT_RSA_GetBits(const CRYPT_RSA_Ctx *ctx); 128 129 #ifdef HITLS_CRYPTO_RSA_GEN 130 /** 131 * @ingroup rsa 132 * @brief Generate the RSA key pair. 133 * 134 * @param ctx [IN/OUT] rsa context structure 135 * 136 * @retval CRYPT_NULL_INPUT Error null pointer input 137 * @retval CRYPT_RSA_ERR_KEY_BITS The value of e in the context structure does not meet the requirements. 138 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 139 * @retval BN error An error occurs in the internal BigNum operation. 140 * @retval CRYPT_SUCCESS The key pair is successfully generated. 141 */ 142 int32_t CRYPT_RSA_Gen(CRYPT_RSA_Ctx *ctx); 143 #endif 144 145 #if defined(HITLS_CRYPTO_RSA_ENCRYPT) || defined(HITLS_CRYPTO_RSA_VERIFY) || defined(HITLS_CRYPTO_RSA_SIGN) 146 /** 147 * @ingroup rsa 148 * @brief RSA public key encryption 149 * 150 * @param ctx [IN] RSA context structure 151 * @param input [IN] Information to be encrypted 152 * @param inputLen [IN] Length of the information to be encrypted 153 * @param out [OUT] Pointer to the encrypted information output. 154 * @param outLen [IN/OUT] Pointer to the length of the encrypted information. 155 * Before being transferred, the value must be set to the maximum length of the array. 156 * 157 * @retval CRYPT_NULL_INPUT Invalid null pointer input 158 * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. 159 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 160 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 161 * @retval CRYPT_SECUREC_FAIL A security function error occurs. 162 * @retval BN error An error occurs in the internal BigNum operation. 163 * @retval CRYPT_SUCCESS encryption succeeded. 164 */ 165 int32_t CRYPT_RSA_PubEnc(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, 166 uint8_t *out, uint32_t *outLen); 167 #endif 168 169 /** 170 * @ingroup rsa 171 * @brief RSA private key decryption 172 * 173 * @param ctx [IN] RSA context structure 174 * @param input [IN] Information to be decrypted 175 * @param inputLen [IN] Length of the information to be decrypted 176 * @param out [OUT] Pointer to the decrypted information output. 177 * @param outLen [IN/OUT] Pointer to the length of the decrypted information. 178 * Before being transferred, the value must be set to the maximum length of the array. 179 * 180 * @retval CRYPT_NULL_INPUT Invalid null pointer input 181 * @retval CRYPT_RSA_ERR_DEC_BITS Incorrect length of the encrypted private key. 182 * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. 183 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 184 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 185 * @retval CRYPT_SECUREC_FAIL A security function error occurs. 186 * @retval BN error. An error occurs in the internal BigNum operation. 187 * @retval CRYPT_SUCCESS Decrypted Successfully 188 */ 189 int32_t CRYPT_RSA_PrvDec(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, 190 uint8_t *out, uint32_t *outLen); 191 192 /** 193 * @ingroup rsa 194 * @brief RSA Set the private key information. 195 * 196 * @param ctx [OUT] rsa context structure 197 * @param para [IN] Private key data 198 * 199 * @retval CRYPT_NULL_INPUT Error null pointer input 200 * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. 201 * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. 202 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 203 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 204 * @retval BN error An error occurs in the internal BigNum operation. 205 * @retval CRYPT_SUCCESS The private key is successfully set. 206 */ 207 int32_t CRYPT_RSA_SetPrvKey(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); 208 209 /** 210 * @ingroup rsa 211 * @brief RSA Set the public key information. 212 * 213 * @param ctx [OUT] RSA context structure 214 * @param para [IN] Public key data 215 * 216 * @retval CRYPT_NULL_INPUT Error null pointer input 217 * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. 218 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 219 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 220 * @retval BN error An error occurs in the internal BigNum operation. 221 * @retval CRYPT_SUCCESS The public key is successfully set. 222 */ 223 int32_t CRYPT_RSA_SetPubKey(CRYPT_RSA_Ctx *ctx, const BSL_Param *para); 224 225 /** 226 * @ingroup rsa 227 * @brief RSA Obtain the private key information. 228 * 229 * @param ctx [IN] RSA context structure 230 * @param para [OUT] Private key data 231 * 232 * @retval CRYPT_NULL_INPUT Invalid null pointer input 233 * @retval BN error An error occurs in the internal BigNum operation. 234 * @retval CRYPT_SUCCESS The private key is obtained successfully. 235 */ 236 int32_t CRYPT_RSA_GetPrvKey(const CRYPT_RSA_Ctx *ctx, BSL_Param *para); 237 238 /** 239 * @ingroup rsa 240 * @brief RSA Obtain the public key information. 241 * 242 * @param ctx [IN] RSA context structure 243 * @param para [OUT] Public key data 244 * 245 * @retval CRYPT_NULL_INPUT Invalid null pointer input 246 * @retval BN error An error occurs in the internal BigNum operation. 247 * @retval CRYPT_SUCCESS The public key is obtained successfully. 248 */ 249 int32_t CRYPT_RSA_GetPubKey(const CRYPT_RSA_Ctx *ctx, BSL_Param *para); 250 251 int32_t CRYPT_RSA_Ctrl(CRYPT_RSA_Ctx *ctx, int32_t opt, void *val, uint32_t len); 252 253 #ifdef HITLS_CRYPTO_RSA_BSSA 254 255 #ifdef HITLS_CRYPTO_RSA_SIGN 256 /** 257 * @ingroup RSA 258 * @brief RSA blind operation for blind signature 259 * 260 * @param ctx [IN] RSA Context structure 261 * @param algId [IN] hash Id for input 262 * @param input [IN] Message to be blinded 263 * @param inputLen [IN] Length of input message 264 * @param out [OUT] Blinded message 265 * @param outLen [OUT] Length of blinded message 266 * 267 * @retval CRYPT_SUCCESS on success 268 * For other error codes, see crypt_errno.h. 269 */ 270 int32_t CRYPT_RSA_Blind(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *input, uint32_t inputLen, 271 uint8_t *out, uint32_t *outLen); 272 #endif 273 274 #ifdef HITLS_CRYPTO_RSA_VERIFY 275 /** 276 * @ingroup RSA 277 * @brief RSA unblind operation for blind signature 278 * 279 * @param ctx [IN] RSA Context structure 280 * @param input [IN] Blind signature to be unblinded 281 * @param inputLen [IN] Length of blind signature 282 * @param out [OUT] Final unblinded signature 283 * @param outLen [OUT] Length of unblinded signature 284 * 285 * @retval CRYPT_SUCCESS on success 286 * For other error codes, see crypt_errno.h. 287 */ 288 int32_t CRYPT_RSA_UnBlind(const CRYPT_RSA_Ctx *ctx, const uint8_t *input, uint32_t inputLen, 289 uint8_t *out, uint32_t *outLen); 290 #endif 291 292 #endif 293 294 #ifdef HITLS_CRYPTO_RSA_EMSA_PSS 295 #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_BSSA) 296 /** 297 * @ingroup rsa 298 * @brief Set the PSS for the original data. 299 * 300 * @param hashMethod [IN] pss Required Hash Method 301 * @param mgfMethod [IN] pss Internal hash method required by the mgf. 302 * @param keyBits [IN] pss Key length 303 * @param salt [IN] Input salt value 304 * @param saltLen [IN] Length of the input salt. 305 * @param data [IN] Original data 306 * @param dataLen [IN] Length of the original data 307 * @param pad [OUT] pss Output buffer 308 * @param padLen [OUT] Maximum length of the array output by the PSS. 309 * 310 * @retval CRYPT_NULL_INPUT Error null pointer input 311 * @retval CRYPT_RSA_ERR_PSS_SALT_DATA The salt value does not meet the requirements. 312 * @retval CRYPT_RSA_ERR_KEY_BITS The key length does not meet the requirements. 313 * @retval CRYPT_RSA_ERR_PSS_SALT_LEN The salt length does not meet the requirements. 314 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length of the reserved buffer is insufficient. 315 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 316 * @retval CRYPT_SUCCESS Succeeded in setting the PSS. 317 */ 318 int32_t CRYPT_RSA_SetPss(const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t keyBits, 319 const uint8_t *salt, uint32_t saltLen, const uint8_t *data, uint32_t dataLen, uint8_t *pad, uint32_t padLen); 320 #endif // HITLS_CRYPTO_RSA_SIGN || HITLS_CRYPTO_RSA_BSSA 321 322 #ifdef HITLS_CRYPTO_RSA_VERIFY 323 /** 324 * @ingroup rsa 325 * @brief Compare the original data from the PSS. 326 * 327 * @param hashMethod [IN] pss Required the hash method 328 * @param mgfMethod [IN] pss Internal hash method required by the mgf. 329 * @param keyBits [IN] pss Key length 330 * @param saltLen [IN] Salt value length 331 * @param data [IN] Original data 332 * @param dataLen [IN] Length of the original data 333 * @param pad [IN] Data after PSS is set. 334 * @param padLen [IN] Data length after PSS is set. 335 * 336 * @retval CRYPT_NULL_INPUT Invalid null pointer input 337 * @retval CRYPT_RSA_ERR_PSS_SALT_DATA The salt value does not meet the requirements. 338 * @retval CRYPT_RSA_ERR_PSS_SALT_LEN The salt length does not meet the requirements. 339 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required for padding does not match the input parameter. 340 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 341 * @retval CRYPT_SUCCESS pss comparison succeeded. 342 */ 343 int32_t CRYPT_RSA_VerifyPss(const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, uint32_t keyBits, 344 uint32_t saltLen, const uint8_t *data, uint32_t dataLen, const uint8_t *pad, uint32_t padLen); 345 #endif // HITLS_CRYPTO_RSA_VERIFY 346 #endif // HITLS_CRYPTO_RSA_EMSA_PSS 347 348 #ifdef HITLS_CRYPTO_RSA_EMSA_PKCSV15 349 /** 350 * @ingroup rsa 351 * @brief Set pkcsv1.5 padding. 352 * 353 * @param hashId [IN] the hash method required by pkcsv1.5 setting. 354 * @param data [IN] Original data 355 * @param dataLen [IN] Length of the original data 356 * @param pad [OUT] Pointer to the array for receiving the padding. 357 * @param padLen [IN] Array length for receiving padding. 358 * 359 * @retval CRYPT_NULL_INPUT Invalid null pointer input 360 * @retval CRYPT_RSA_NO_KEY_INFO The key information is insufficient. 361 * @retval CRYPT_SECUREC_FAIL The security function fails. 362 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required by the padding does not match the input parameter. 363 * @retval CRYPT_RSA_ERR_INPUT_VALUE The hash algorithm ID is not supported. 364 * @retval CRYPT_SUCCESS The pkcsv1.5 padding is successfully set. 365 */ 366 int32_t CRYPT_RSA_SetPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *data, uint32_t dataLen, 367 uint8_t *pad, uint32_t padLen); 368 369 #ifdef HITLS_CRYPTO_RSA_VERIFY 370 /** 371 * @ingroup rsa 372 * @brief Verify pkcsv1.5 padding. 373 * 374 * @param hashId [IN] the hash method corresponding to pkcsv1.5 verification. 375 * @param pad [IN] Data after padding 376 * @param padLen [IN] Data length after padding 377 * @param data [IN] Original data 378 * @param dataLen [IN] Length of the original data 379 * 380 * @retval CRYPT_NULL_INPUT Invalid null pointer input 381 * @retval CRYPT_RSA_ERR_PKCSV15_SALT_DATA Incorrect padding value. 382 * @retval CRYPT_SECUREC_FAIL Security Function Failure 383 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The length required for padding does not match the input parameter. 384 * @retval CRYPT_RSA_ERR_INPUT_VALUE The hash algorithm ID is not supported. 385 * @retval CRYPT_SUCCESS Verify pkcsv1.5 is padded successfully. 386 */ 387 int32_t CRYPT_RSA_VerifyPkcsV15Type1(CRYPT_MD_AlgId hashId, const uint8_t *pad, uint32_t padLen, 388 const uint8_t *data, uint32_t dataLen); 389 #endif // HITLS_CRYPTO_RSA_VERIFY 390 #endif // HITLS_CRYPTO_RSA_EMSA_PKCSV15 391 392 #if defined(HITLS_CRYPTO_RSA_SIGN) || defined(HITLS_CRYPTO_RSA_VERIFY) 393 /** 394 * @ingroup rsa 395 * @brief Obtain the maximum length of RSA signature data. 396 * 397 * @param ctx [IN] Maximum length of the RSA signature data that is expected to be obtained 398 * 399 * @retval 0 The input is incorrect or the corresponding key structure does not contain valid key information. 400 * @retval uint32_t Maximum length of the signature data 401 */ 402 uint32_t CRYPT_RSA_GetSignLen(const CRYPT_RSA_Ctx *ctx); 403 #endif 404 405 #ifdef HITLS_CRYPTO_RSA_VERIFY 406 int32_t CRYPT_RSA_VerifyData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, 407 const uint8_t *sign, uint32_t signLen); 408 409 int32_t CRYPT_RSA_Verify(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, 410 const uint8_t *sign, uint32_t signLen); 411 #endif 412 413 #ifdef HITLS_CRYPTO_RSA_SIGN 414 int32_t CRYPT_RSA_SignData(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, 415 uint8_t *sign, uint32_t *signLen); 416 417 int32_t CRYPT_RSA_Sign(CRYPT_RSA_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, 418 uint8_t *sign, uint32_t *signLen); 419 #endif 420 421 #ifdef HITLS_CRYPTO_RSA_ENCRYPT 422 /** 423 * @ingroup rsa 424 * @brief RSA public key encryption 425 * 426 * @param ctx [IN] RSA context structure 427 * @param data [IN] Information to be encrypted 428 * @param dataLen [IN] Length of the information to be encrypted 429 * @param out [OUT] Pointer to the encrypted information output. 430 * @param outLen [OUT] Pointer to the length of the encrypted information 431 * 432 * @retval CRYPT_NULL_INPUT Invalid null pointer input 433 * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. 434 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 435 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient 436 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 437 * @retval CRYPT_SECUREC_FAIL A safe function error occurs. 438 * @retval BN error. An error occurs in the internal BigNum operation. 439 * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the encryption method. 440 * @retval CRYPT_SUCCESS encryption succeeded. 441 */ 442 int32_t CRYPT_RSA_Encrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, 443 uint8_t *out, uint32_t *outLen); 444 #endif 445 446 #ifdef HITLS_CRYPTO_RSA_DECRYPT 447 /** 448 * @ingroup rsa 449 * @brief RSA private key decryption 450 * 451 * @param ctx [IN] RSA context structure 452 * @param data [IN] Information to be decrypted 453 * @param dataLen [IN] Length of the information to be decrypted 454 * @param out [OUT] Pointer to the output information after decryption. 455 * @param outLen [OUT] Pointer to the length of the decrypted information 456 * 457 * @retval CRYPT_NULL_INPUT Error null pointer input 458 * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. 459 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 460 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient 461 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 462 * @retval CRYPT_SECUREC_FAIL A security function error occurs. 463 * @retval CRYPT_EAL_ALG_NOT_SUPPORT does not register the decryption method. 464 * @retval BN error. An error occurs in the internal BigNum operation. 465 * @retval CRYPT_SUCCESS Decryption succeeded. 466 */ 467 int32_t CRYPT_RSA_Decrypt(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, 468 uint8_t *out, uint32_t *outLen); 469 #endif 470 471 #ifdef HITLS_CRYPTO_RSA_VERIFY 472 /** 473 * @ingroup rsa 474 * @brief RSA public key decryption 475 * 476 * @param ctx [IN] RSA context structure 477 * @param data [IN] Information to be decrypted 478 * @param dataLen [IN] Length of the information to be decrypted 479 * @param out [OUT] Pointer to the output information after decryption. 480 * @param outLen [IN/OUT] Pointer to the length of the decrypted information. 481 * Before being transferred, the value must be set to the maximum length of the array. 482 * 483 * @retval CRYPT_NULL_INPUT Invalid null pointer input 484 * @retval CRYPT_RSA_NO_KEY_INFO does not contain the key information. 485 * @retval CRYPT_RSA_PAD_NO_SET_ERROR The padding type is not set. 486 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH The space is insufficient after decryption. 487 * @retval CRYPT_RSA_ERR_INPUT_VALUE The input parameter does not meet the requirements. 488 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 489 * @retval Other error codes, for example, the CRYPT_RSA_UnPackPkcsV15Type1 de-padding function. 490 * @retval CRYPT_SUCCESS Decrypted Successfully 491 */ 492 int32_t CRYPT_RSA_Recover(CRYPT_RSA_Ctx *ctx, const uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen); 493 #endif 494 495 /** 496 * @ingroup rsa 497 * @brief RSA compare the public key 498 * 499 * @param a [IN] RSA context structure 500 * @param b [IN] RSA context structure 501 * 502 * @retval CRYPT_SUCCESS is the same 503 * @retval CRYPT_NULL_INPUT Invalid null pointer input 504 * @retval CRYPT_RSA_NO_KEY_INFO No public key 505 * @retval CRYPT_RSA_PUBKEY_NOT_EQUAL Public Keys are not equal 506 */ 507 int32_t CRYPT_RSA_Cmp(const CRYPT_RSA_Ctx *a, const CRYPT_RSA_Ctx *b); 508 509 #ifdef HITLS_CRYPTO_RSAES_OAEP 510 #ifdef HITLS_CRYPTO_RSA_ENCRYPT 511 /** 512 * @ingroup rsa 513 * @brief oaep padding 514 * 515 * @param hashMethod [IN] Hash method. Only sha1, sha244, sha256, sha384, and sha512 are supported. 516 * @param mgfMethod [IN] Hash method required by mgf 517 * @param in [IN] Original data 518 * @param inLen [IN] Original data length 519 * @param param [IN] oaep parameter, which can be null 520 * @param paramLen [IN] oaep Parameter length 521 * @param pad [IN] Data after padding 522 * @param padLen [IN] Data length after padding 523 * 524 * @retval CRYPT_NULL_INPUT Error null pointer input 525 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 526 * @retval CRYPT_SECUREC_FAIL A security function error occurs. 527 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 528 * @retval CRYPT_RSA_BUFF_LEN_NOT_ENOUGH Outbuf Insufficient 529 * */ 530 int32_t CRYPT_RSA_SetPkcs1Oaep(CRYPT_RSA_Ctx *ctx, const uint8_t *in, uint32_t inLen, uint8_t *pad, uint32_t padLen); 531 #endif // HITLS_CRYPTO_RSA_ENCRYPT 532 533 #ifdef HITLS_CRYPTO_RSA_DECRYPT 534 /** 535 * @ingroup rsa 536 * @brief Verify the oaep padding. 537 * 538 * @param hashMethod [IN] Hash method, which supports sha1, sha244, sha256, sha384, and sha512. 539 * @param mgfMethod [IN] Hash method required by mgf 540 * @param in [IN] Data after padding 541 * @param inLen [IN] Data length after padding 542 * @param param [IN] oaep parameter, which can be null 543 * @param paramLen [IN] oaep Parameter length 544 * @param msg [IN] Data after the de-padding 545 * @param msgLen [IN/OUT] The input parameter is the length of the msg buffer, 546 * and the output parameter is the length of the msg after the de-padding. 547 * 548 * @retval CRYPT_NULL_INPUT Error null pointer input 549 * @retval CRYPT_RSA_ERR_INPUT_VALUE The entered value does not meet the calculation conditions. 550 * @retval CRYPT_SECUREC_FAIL A security function error occurs. 551 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 552 * */ 553 int32_t CRYPT_RSA_VerifyPkcs1Oaep(const EAL_MdMethod *hashMethod, const EAL_MdMethod *mgfMethod, const uint8_t *in, 554 uint32_t inLen, const uint8_t *param, uint32_t paramLen, uint8_t *msg, uint32_t *msgLen); 555 #endif // HITLS_CRYPTO_RSA_DECRYPT 556 #endif // HITLS_CRYPTO_RSAES_OAEP 557 558 #if defined(HITLS_CRYPTO_RSA_ENCRYPT) && \ 559 (defined(HITLS_CRYPTO_RSAES_PKCSV15_TLS) || defined(HITLS_CRYPTO_RSAES_PKCSV15)) 560 int32_t CRYPT_RSA_SetPkcsV15Type2(void *libCtx, const uint8_t *in, uint32_t inLen, 561 uint8_t *out, uint32_t outLen); 562 #endif 563 564 #ifdef HITLS_CRYPTO_RSA_DECRYPT 565 #ifdef HITLS_CRYPTO_RSAES_PKCSV15 566 int32_t CRYPT_RSA_VerifyPkcsV15Type2(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 567 #endif 568 569 #ifdef HITLS_CRYPTO_RSAES_PKCSV15_TLS 570 int32_t CRYPT_RSA_VerifyPkcsV15Type2TLS(const uint8_t *in, uint32_t inLen, uint8_t *out, uint32_t *outLen); 571 #endif 572 #endif // HITLS_CRYPTO_RSA_DECRYPT 573 574 /** 575 * @ingroup rsa 576 * @brief rsa get security bits 577 * 578 * @param ctx [IN] rsa Context structure 579 * 580 * @retval security bits 581 */ 582 int32_t CRYPT_RSA_GetSecBits(const CRYPT_RSA_Ctx *ctx); 583 584 #ifdef HITLS_CRYPTO_PROVIDER 585 /** 586 * @ingroup RSA 587 * @brief RSA import key 588 * 589 * @param ctx [IN/OUT] RSA context structure 590 * @param params [IN] parameters 591 */ 592 int32_t CRYPT_RSA_Import(CRYPT_RSA_Ctx *ctx, const BSL_Param *params); 593 594 /** 595 * @ingroup RSA 596 * @brief RSA export key 597 * 598 * @param ctx [IN] RSA context structure 599 * @param params [IN/OUT] key parameters 600 */ 601 int32_t CRYPT_RSA_Export(const CRYPT_RSA_Ctx *ctx, BSL_Param *params); 602 #endif // HITLS_CRYPTO_PROVIDER 603 604 #ifdef __cplusplus 605 } 606 #endif 607 608 #endif // HITLS_CRYPTO_RSA 609 610 #endif // CRYPT_RSA_H 611