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 SM2_LOCAL_H 17 #define SM2_LOCAL_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_SM2 21 22 #include <stdint.h> 23 #include "crypt_sm2.h" 24 #include "crypt_local_types.h" 25 #include "crypt_ecc_pkey.h" 26 #include "sal_atomic.h" 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif /* __cpluscplus */ 31 32 #define SM2_MAX_ID_BITS 65535 33 #define SM2_MAX_ID_LENGTH (SM2_MAX_ID_BITS / 8) 34 #define SM2_MAX_PUBKEY_DATA_LENGTH 256 35 #define MAX_MD_SIZE 64 36 #define SM3_MD_SIZE 32 37 #define SM2_POINT_SINGLE_COORDINATE_LEN 32 38 #define SM2_POINT_COORDINATE_LEN 65 39 #define SM2_TWO_POINT_COORDINATE_LEN 128 40 #define SM2_X_LEN 32 41 42 /* SM2 key context */ 43 struct SM2_Ctx { 44 ECC_Pkey *pkey; 45 uint32_t pkgImpl; 46 ECC_Point *pointR; // Local R 47 const EAL_MdMethod *hashMethod; 48 BN_BigNum *r; // Local r 49 uint8_t *userId; // User ID 50 uint32_t userIdLen; // the length of User ID 51 int32_t server; // 1: the initiator, 0: the receiver, and the default value is 1. 52 uint8_t sumCheck[SM3_MD_SIZE]; // Hash value used as a check 53 uint8_t sumSend[SM3_MD_SIZE]; // Hash value sent to the peer end 54 uint8_t isSumValid; // Indicates whether the checksum is valid. 1: valid; 0: invalid. 55 BSL_SAL_RefCount references; 56 }; 57 58 /** 59 * @ingroup sm2 60 * @brief The sm2 invokes the SM3 to calculate the hash value. 61 * 62 * @param ctx [IN] sm2 context structure 63 * @param out [IN/OUT] Hash value 64 * @param outLen [IN/OUT] Length of the hash value 65 * 66 * @retval CRYPT_SUCCESS calculated successfully. 67 * @retval Other: The calculation fails. For details about the return value type, see crypt_errno.h. 68 */ 69 int32_t Sm2ComputeZDigest(const CRYPT_SM2_Ctx *ctx, uint8_t *out, uint32_t *outLen); 70 71 #if defined(HITLS_CRYPTO_SM2_EXCH) || defined(HITLS_CRYPTO_SM2_CRYPT) 72 /** 73 * @ingroup sm2 74 * @brief sm2 kdf function 75 * 76 * @param out [IN/OUT] Calculation result 77 * @param outlen [IN/OUT] Output data length 78 * @param z [IN] Input data 79 * @param zlen [IN] Length of the input data 80 * @param hashMethod [IN] hash method 81 * 82 * @retval CRYPT_SUCCESS calculated successfully. 83 * @retval Other: The calculation fails. For details about the return value type, see crypt_errno.h. 84 */ 85 int32_t KdfGmt0032012(uint8_t *out, const uint32_t *outlen, const uint8_t *z, uint32_t zlen, 86 const EAL_MdMethod *hashMethod); 87 #endif 88 89 #ifdef __cplusplus 90 } 91 #endif 92 93 #endif // HITLS_CRYPTO_SM2 94 95 #endif // SM2_LOCAL_H 96