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_SM2_H 17 #define CRYPT_SM2_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_SM2 21 22 #include <stdint.h> 23 #include "crypt_types.h" 24 #include "crypt_ecc.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif /* __cpluscplus */ 29 30 typedef struct SM2_Ctx CRYPT_SM2_Ctx; 31 /* SM2 parameter structure */ 32 typedef struct EccPara CRYPT_Sm2Para; 33 34 /** 35 * @ingroup sm2 36 * @brief sm2 Allocate the context memory space. 37 * 38 * @retval (CRYPT_SM2_Ctx *) Pointer to the memory space of the allocated context 39 * @retval NULL Invalid null pointer. 40 */ 41 CRYPT_SM2_Ctx *CRYPT_SM2_NewCtx(void); 42 43 /** 44 * @ingroup sm2 45 * @brief sm2 Allocate the context memory space. 46 * 47 * @param libCtx [IN] Library context 48 * 49 * @retval (CRYPT_SM2_Ctx *) Pointer to the memory space of the allocated context 50 * @retval NULL Invalid null pointer. 51 */ 52 CRYPT_SM2_Ctx *CRYPT_SM2_NewCtxEx(void *libCtx); 53 54 /** 55 * @ingroup sm2 56 * @brief Copy the sm2 context. After the duplication is complete, invoke the CRYPT_SM2_FreeCtx to release the memory. 57 * 58 * @param ctx [IN] Source SM2 context 59 * 60 * @return CRYPT_SM2_Ctx SM2 context pointer= 61 */ 62 CRYPT_SM2_Ctx *CRYPT_SM2_DupCtx(CRYPT_SM2_Ctx *ctx); 63 64 /** 65 * @ingroup sm2 66 * @brief release sm2 key context structure 67 * 68 * @param ctx [IN] Context structure to be released. 69 */ 70 void CRYPT_SM2_FreeCtx(CRYPT_SM2_Ctx *ctx); 71 72 /** 73 * @ingroup sm2 74 * @brief sm2 Obtain the key length. 75 * 76 * @param ctx [IN] sm2 context structure 77 * 78 * @retval 0 The input is incorrect or the corresponding key structure does not contain valid key length. 79 * @retval uint32_t Valid key length 80 */ 81 uint32_t CRYPT_SM2_GetBits(const CRYPT_SM2_Ctx *ctx); 82 83 /** 84 * @ingroup sm2 85 * @brief Generate the SM2 key pair. 86 * 87 * @param ctx [IN/OUT] sm2 context structure 88 * 89 * @retval CRYPT_NULL_INPUT Invalid null pointer input 90 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 91 * @retval ECC error code. An error occurred in the internal ECC calculation. 92 * @retval CRYPT_SUCCESS The key pair is successfully generated. 93 */ 94 int32_t CRYPT_SM2_Gen(CRYPT_SM2_Ctx *ctx); 95 96 #ifdef HITLS_CRYPTO_SM2_SIGN 97 /** 98 * @ingroup sm2 99 * @brief sm2 obtain the length of the signature data, in bytes. 100 * 101 * @param ctx [IN] sm2 context structure 102 * 103 * @retval 0 The input is incorrect or the corresponding key structure does not contain valid parameter data. 104 * @retval uint32_t Length required for valid signature data 105 */ 106 uint32_t CRYPT_SM2_GetSignLen(const CRYPT_SM2_Ctx *ctx); 107 108 /** 109 * @ingroup sm2 110 * @brief SM2 Signature 111 * 112 * @param ctx [IN] sm2 context structure 113 * @param algId [IN] md algId 114 * @param data [IN] Data to be signed 115 * @param dataLen [IN] Length of the data to be signed 116 * @param sign [OUT] Signature data 117 * @param signLen [IN/OUT] The input parameter is the space length of the sign, 118 * and the output parameter is the valid length of the sign. 119 * The required space can be obtained by calling CRYPT_SM2_GetSignLen. 120 * 121 * @retval CRYPT_NULL_INPUT Error null pointer input 122 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 123 * @retval CRYPT_SM2_ERR_EMPTY_KEY The key cannot be empty. 124 * @retval CRYPT_SM2_BUFF_LEN_NOT_ENOUGH The buffer length is insufficient. 125 * @retval BN error. An error occurs in the internal BigNum operation. 126 * @retval ECC error. An error occurred in the internal ECC calculation. 127 * @retval CRYPT_SUCCESS Signed successfully. 128 */ 129 int32_t CRYPT_SM2_Sign(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, 130 uint8_t *sign, uint32_t *signLen); 131 132 /** 133 * @ingroup sm2 134 * @brief SM2 Verify the signature. 135 * 136 * @param ctx [IN] sm2 context structure 137 * @param algId [IN] md algId 138 * @param data [IN] Data to be signed 139 * @param dataLen [IN] Length of the data to be signed 140 * @param sign [IN] Signature data 141 * @param signLen [IN] Valid length of the sign 142 * 143 * @retval CRYPT_NULL_INPUT Invalid null pointer input 144 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 145 * @retval CRYPT_SM2_VERIFY_FAIL Failed to verify the signature. 146 * @retval BN error. An error occurs in the internal BigNum operation. 147 * @retval ECC error. An error occurred in the internal ECC calculation. 148 * @retval DSA error. An error occurs in the DSA encoding and decoding part. 149 * @retval CRYPT_SUCCESS The signature verification is successful. 150 */ 151 int32_t CRYPT_SM2_Verify(const CRYPT_SM2_Ctx *ctx, int32_t algId, const uint8_t *data, uint32_t dataLen, 152 const uint8_t *sign, uint32_t signLen); 153 #endif 154 155 /** 156 * @ingroup sm2 157 * @brief SM2 Set the private key data. 158 * 159 * @param ctx [OUT] sm2 context structure 160 * @param para [IN] External private key data 161 * 162 * @retval CRYPT_NULL_INPUT Error null pointer input 163 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 164 * @retval ECC error. An error occurred in the internal ECC calculation. 165 * @retval CRYPT_SUCCESS set successfully. 166 */ 167 int32_t CRYPT_SM2_SetPrvKey(CRYPT_SM2_Ctx *ctx, const BSL_Param *para); 168 169 /** 170 * @ingroup sm2 171 * @brief SM2 Set the public key data. 172 * 173 * @param ctx [OUT] sm2 context structure 174 * @param para [IN] External public key data 175 * 176 * @retval CRYPT_NULL_INPUT Invalid null pointer input 177 * @retval CRYPT_MEM_ALLOC_FAIL Memory allocation failure 178 * @retval ECC error. An error occurred in the internal ECC calculation. 179 * @retval CRYPT_SUCCESS set successfully. 180 */ 181 int32_t CRYPT_SM2_SetPubKey(CRYPT_SM2_Ctx *ctx, const BSL_Param *para); 182 183 /** 184 * @ingroup sm2 185 * @brief SM2 Obtain the private key data. 186 * 187 * @param ctx [IN] sm2 context structure 188 * @param para [OUT] External private key data 189 * 190 * @retval CRYPT_NULL_INPUT Error null pointer input 191 * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty. 192 * @retval ECC error. An error occurred in the internal ECC calculation. 193 * @retval CRYPT_SUCCESS obtained successfully. 194 */ 195 int32_t CRYPT_SM2_GetPrvKey(const CRYPT_SM2_Ctx *ctx, BSL_Param *para); 196 197 /** 198 * @ingroup sm2 199 * @brief SM2 Obtain the public key data. 200 * 201 * @param ctx [IN] sm2 context structure 202 * @param para [OUT] External public key data 203 * 204 * @retval CRYPT_NULL_INPUT Invalid null pointer input 205 * @retval CRYPT_ECC_PKEY_ERR_EMPTY_KEY The key is empty. 206 * @retval ECC error. An error occurred in the internal ECC calculation. 207 * @retval CRYPT_SUCCESS Obtained successfully. 208 */ 209 int32_t CRYPT_SM2_GetPubKey(const CRYPT_SM2_Ctx *ctx, BSL_Param *para); 210 211 /** 212 * @ingroup sm2 213 * @brief sm2 control interface 214 * 215 * @param ctx [IN/OUT] sm2 context structure 216 * @param opt [IN] Operation mode. For details, see ECC_CtrlType. 217 * @param val [IN] Input parameter 218 * @param len [IN] val Length 219 * 220 * @retval CRYPT_SUCCESS set successfully. 221 * @retval CRYPT_NULL_INPUT If any input parameter is empty 222 * @retval For other error codes, see crypt_errno.h. 223 */ 224 int32_t CRYPT_SM2_Ctrl(CRYPT_SM2_Ctx *ctx, int32_t opt, void *val, uint32_t len); 225 226 #ifdef HITLS_CRYPTO_SM2_EXCH 227 /** 228 * @ingroup sm2 229 * @brief sm2 Generate the shared key. 230 * 231 * @param selfCtx [IN] Local context structure 232 * @param peerCtx [IN] Peer context structure 233 * @param out [OUT] Generated shared key 234 * @param outlen [IN/OUT] Length of the generated shared key 235 * 236 * @retval CRYPT_SUCCESS secceeded. 237 * @retval CRYPT_NULL_INPUT If any input parameter is empty 238 * @retval For other error codes, see crypt_errno.h. 239 */ 240 int32_t CRYPT_SM2_KapComputeKey(const CRYPT_SM2_Ctx *selfCtx, const CRYPT_SM2_Ctx *peerCtx, uint8_t *out, 241 uint32_t *outlen); 242 #endif 243 244 #ifdef HITLS_CRYPTO_SM2_CRYPT 245 /** 246 * @ingroup sm2 247 * @brief sm2 Encryption 248 * @param ctx [IN] Context structure 249 * @param data [IN] Plaintext 250 * @param datalen [IN] Plaintext length 251 * @param out [OUT] Output ciphertext 252 * @param outlen [OUT] Ciphertext length 253 * 254 * @retval CRYPT_SUCCESS secceeded. 255 * @retval CRYPT_NULL_INPUT If any input parameter is empty 256 * @retval For other error codes, see crypt_errno.h. 257 */ 258 int32_t CRYPT_SM2_Encrypt(CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, uint8_t *out, uint32_t *outlen); 259 260 /** 261 * @ingroup sm2 262 * @brief sm2 Decryption 263 * @param ctx [IN] Context structure 264 * @param data [IN] Received ciphertext 265 * @param datalen [IN] Ciphertext length 266 * @param out [OUT] Output plaintext after decryption 267 * @param outlen [OUT] Length of the decrypted plaintext 268 * 269 * @retval CRYPT_SUCCESS secceeded. 270 * @retval CRYPT_NULL_INPUT If any input parameter is empty 271 * @retval For other error codes, see crypt_errno.h. 272 */ 273 int32_t CRYPT_SM2_Decrypt(CRYPT_SM2_Ctx *ctx, const uint8_t *data, uint32_t datalen, uint8_t *out, uint32_t *outlen); 274 #endif 275 /** 276 * @ingroup sm2 277 * @brief sm2 Compare the public key and parameters. 278 * 279 * @param a [IN] sm2 context structure 280 * @param b [IN] sm2 context structure 281 * 282 * @retval CRYPT_SUCCESS is the same 283 * For other error codes, see crypt_errno.h. 284 */ 285 int32_t CRYPT_SM2_Cmp(const CRYPT_SM2_Ctx *a, const CRYPT_SM2_Ctx *b); 286 287 /** 288 * @ingroup sm2 289 * @brief sm2 get security bits 290 * 291 * @param ctx [IN] sm2 Context structure 292 * 293 * @retval security bits 294 */ 295 int32_t CRYPT_SM2_GetSecBits(const CRYPT_SM2_Ctx *ctx); 296 297 /** 298 * @ingroup sm2 299 * @brief sm2 import key 300 * 301 * @param ctx [IN/OUT] sm2 context structure 302 * @param params [IN] key parameters 303 */ 304 int32_t CRYPT_SM2_Import(CRYPT_SM2_Ctx *ctx, const BSL_Param *params); 305 306 /** 307 * @ingroup sm2 308 * @brief sm2 export key 309 * 310 * @param ctx [IN] sm2 context structure 311 * @param params [IN/OUT] key parameters 312 */ 313 int32_t CRYPT_SM2_Export(const CRYPT_SM2_Ctx *ctx, BSL_Param *params); 314 315 #ifdef __cplusplus 316 } 317 #endif 318 319 #endif // HITLS_CRYPTO_SM2 320 321 #endif // CRYPT_SM2_H 322