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_CURVE25519_H 17 #define CRYPT_CURVE25519_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_CURVE25519 21 22 #include <stdint.h> 23 #include "crypt_local_types.h" 24 #include "bsl_params.h" 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #define CRYPT_CURVE25519_KEYLEN 32 31 #define CRYPT_CURVE25519_SIGNLEN 64 32 33 typedef struct CryptCurve25519Ctx CRYPT_CURVE25519_Ctx; 34 35 #ifdef HITLS_CRYPTO_X25519 36 /** 37 * @ingroup curve25519 38 * @brief curve25519 Create a key pair structure and allocate memory space. 39 * 40 * @retval (CRYPT_CURVE25519_Ctx *) Pointer to the key pair structure 41 * @retval NULL Invalid null pointer 42 */ 43 CRYPT_CURVE25519_Ctx *CRYPT_X25519_NewCtx(void); 44 45 /** 46 * @ingroup curve25519 47 * @brief curve25519 Create a key pair structure and allocate memory space. 48 * 49 * @param libCtx [IN] Library context 50 * 51 * @retval (CRYPT_CURVE25519_Ctx *) Pointer to the key pair structure 52 * @retval NULL Invalid null pointer 53 */ 54 CRYPT_CURVE25519_Ctx *CRYPT_X25519_NewCtxEx(void *libCtx); 55 #endif 56 57 #ifdef HITLS_CRYPTO_ED25519 58 /** 59 * @ingroup ed25519 60 * @brief curve25519 Create a key pair structure for ED25519 algorithm and allocate memory space. 61 * 62 * @retval (CRYPT_CURVE25519_Ctx *) Pointer to the key pair structure 63 * @retval NULL Invalid null pointer 64 */ 65 CRYPT_CURVE25519_Ctx *CRYPT_ED25519_NewCtx(void); 66 67 /** 68 * @ingroup ed25519 69 * @brief curve25519 Create a key pair structure for ED25519 algorithm and allocate memory space. 70 * 71 * @param libCtx [IN] Library context 72 * 73 * @retval (CRYPT_CURVE25519_Ctx *) Pointer to the key pair structure 74 * @retval NULL Invalid null pointer 75 */ 76 CRYPT_CURVE25519_Ctx *CRYPT_ED25519_NewCtxEx(void *libCtx); 77 #endif 78 79 /** 80 * @ingroup curve25519 81 * @brief Copy the curve25519 context. The memory management of the return value is handed over to the caller. 82 * 83 * @param ctx [IN] Source curve25519 context. The CTX is set NULL by the invoker. 84 * 85 * @return CRYPT_CURVE25519_Ctx curve25519 Context pointer 86 * If the operation fails, null is returned. 87 */ 88 CRYPT_CURVE25519_Ctx *CRYPT_CURVE25519_DupCtx(CRYPT_CURVE25519_Ctx *ctx); 89 90 /** 91 * @ingroup curve25519 92 * @brief Clear the curve25519 key pair data and releases memory. 93 * 94 * @param pkey [IN] curve25519 Key pair structure. The pkey is set NULL by the invoker. 95 */ 96 void CRYPT_CURVE25519_FreeCtx(CRYPT_CURVE25519_Ctx *pkey); 97 98 /** 99 * @ingroup curve25519 100 * @brief curve25519 Control interface 101 * 102 * @param pkey [IN/OUT] curve25519 Key pair structure 103 * @param val [IN] Hash method, which must be SHA512. 104 * @param opt [IN] Operation mode 105 * @param len [IN] val length 106 * 107 * @retval CRYPT_SUCCESS set successfully. 108 * @retval CRYPT_NULL_INPUT If any input parameter is empty 109 * @retval CRYPT_CURVE25519_UNSUPPORTED_CTRL_OPTION The opt mode is not supported. 110 * @retval CRYPT_CURVE25519_HASH_METH_ERROR The hash method is not SHA512 111 */ 112 int32_t CRYPT_CURVE25519_Ctrl(CRYPT_CURVE25519_Ctx *pkey, int32_t opt, void *val, uint32_t len); 113 114 /** 115 * @ingroup curve25519 116 * @brief curve25519 Set the public key. 117 * 118 * @param pkey [IN] curve25519 Key pair structure 119 * @param para [IN] Public key 120 * 121 * @retval CRYPT_SUCCESS set successfully. 122 * @retval CRYPT_NULL_INPUT If any input parameter is empty 123 * @retval CRYPT_CURVE25519_KEYLEN_ERROR pubKeyLen is not equal to curve25519 public key length 124 */ 125 int32_t CRYPT_CURVE25519_SetPubKey(CRYPT_CURVE25519_Ctx *pkey, const BSL_Param *para); 126 127 /** 128 * @ingroup curve25519 129 * @brief curve25519 Obtain the public key. 130 * 131 * @param pkey [IN] curve25519 Key pair structure 132 * @param para [OUT] Public key 133 * 134 * @retval CRYPT_SUCCESS set successfully. 135 * @retval CRYPT_NULL_INPUT If any input parameter is empty 136 * @retval CRYPT_CURVE25519_NO_PUBKEY The key pair has no public key. 137 * @retval CRYPT_CURVE25519_KEYLEN_ERROR pubKeyLen is less than curve25519 public key length. 138 */ 139 int32_t CRYPT_CURVE25519_GetPubKey(const CRYPT_CURVE25519_Ctx *pkey, BSL_Param *para); 140 141 /** 142 * @ingroup curve25519 143 * @brief curve25519 Set the private key. 144 * 145 * @param pkey [IN] curve25519 Key pair structure 146 * @param para [IN] Private key 147 * 148 * @retval CRYPT_SUCCESS set successfully. 149 * @retval CRYPT_NULL_INPUT If any input parameter is empty 150 * @retval CRYPT_CURVE25519_KEYLEN_ERROR prvKeyLen is not equal to curve25519 private key length 151 */ 152 int32_t CRYPT_CURVE25519_SetPrvKey(CRYPT_CURVE25519_Ctx *pkey, const BSL_Param *para); 153 154 /** 155 * @ingroup curve25519 156 * @brief curve25519 Obtain the private key. 157 * 158 * @param pkey [IN] curve25519 Key pair structure 159 * @param para [OUT] private key 160 * 161 * @retval CRYPT_SUCCESS successfully set. 162 * @retval CRYPT_NULL_INPUT Any input parameter is empty. 163 * @retval CRYPT_CURVE25519_NO_PRVKEY The key pair has no private key. 164 * @retval CRYPT_CURVE25519_KEYLEN_ERROR prvKeyLen is less than the private key length of curve25519. 165 */ 166 int32_t CRYPT_CURVE25519_GetPrvKey(const CRYPT_CURVE25519_Ctx *pkey, BSL_Param *para); 167 168 /** 169 * @ingroup curve25519 170 * @brief curve25519 Obtain the key length, in bits. 171 * 172 * @param pkey [IN] curve25519 Key pair structure 173 * 174 * @retval Key length 175 */ 176 int32_t CRYPT_CURVE25519_GetBits(const CRYPT_CURVE25519_Ctx *pkey); 177 178 #ifdef HITLS_CRYPTO_ED25519 179 /** 180 * @ingroup curve25519 181 * @brief curve25519 Sign 182 * 183 * @param pkey [IN/OUT] curve25519 Key pair structure. A private key is required for signature. 184 * After signature, a public key is generated. 185 * @param algid [IN] md algid 186 * @param msg [IN] Data to be signed 187 * @param msgLen [IN] Data length: 0 <= msgLen <= (2^125 - 64) bytes 188 * @param hashMethod [IN] SHA512 method 189 * @param sign [OUT] Signature 190 * @param signLen [IN/OUT] Length of the signature buffer (must be greater than 64 bytes)/Length of the signature 191 * 192 * @retval CRYPT_SUCCESS generated successfully. 193 * @retval CRYPT_CURVE25519_NO_PRVKEY The key pair has no private key. 194 * @retval CRYPT_NULL_INPUT If any input parameter is empty 195 * @retval Error code of the hash module. An error occurs in the sha512 operation. 196 * @retval CRYPT_CURVE25519_NO_HASH_METHOD No hash method is set. 197 * @retval CRYPT_CURVE25519_SIGNLEN_ERROR signLen is less than the signature length of curve25519. 198 */ 199 int32_t CRYPT_CURVE25519_Sign(CRYPT_CURVE25519_Ctx *pkey, int32_t algId, const uint8_t *msg, 200 uint32_t msgLen, uint8_t *sign, uint32_t *signLen); 201 202 /** 203 * @ingroup curve25519 204 * @brief curve25519 Obtain the signature length, in bytes. 205 * 206 * @param pkey [IN] curve25519 Key pair structure 207 * 208 * @retval Signature length 209 */ 210 int32_t CRYPT_CURVE25519_GetSignLen(const CRYPT_CURVE25519_Ctx *pkey); 211 212 /** 213 * @ingroup curve25519 214 * @brief curve25519 Verification 215 * 216 * @param pkey [IN] curve25519 Key pair structure. A public key is required for signature verification. 217 * @param algid [IN] md algid 218 * @param msg [IN] Data 219 * @param msgLen [IN] Data length: 0 <= msgLen <= (2^125 - 64) bytes 220 * @param sign [IN] Signature 221 * @param signLen [IN] Signature length, which must be 64 bytes 222 * 223 * @retval CRYPT_SUCCESS The signature verification is successful. 224 * @retval CRYPT_CURVE25519_NO_PUBKEY The key pair has no public key. 225 * @retval CRYPT_NULL_INPUT If any input parameter is empty 226 * @retval Error code of the hash module. An error occurs in the sha512 operation. 227 * @retval CRYPT_CURVE25519_VERIFY_FAIL Failed to verify the signature. 228 * @retval CRYPT_CURVE25519_INVALID_PUBKEY Invalid public key. 229 * @retval CRYPT_CURVE25519_SIGNLEN_ERROR signLen is not equal to curve25519 signature length 230 * @retval CRYPT_CURVE25519_NO_HASH_METHOD No hash method is set. 231 */ 232 int32_t CRYPT_CURVE25519_Verify(const CRYPT_CURVE25519_Ctx *pkey, int32_t algId, const uint8_t *msg, 233 uint32_t msgLen, const uint8_t *sign, uint32_t signLen); 234 235 /** 236 * @ingroup curve25519 237 * @brief ed25519 Generate a key pair (public and private keys). 238 * 239 * @param pkey [IN/OUT] curve25519 Key pair structure/Key pair structure containing public and private keys 240 * 241 * @retval CRYPT_SUCCESS generated successfully. 242 * @retval CRYPT_NO_REGIST_RAND Unregistered random number 243 * @retval Error code of the hash module. An error occurs during the SHA512 operation. 244 * @retval Error code of the registered random number module. Failed to obtain the random number. 245 * @retval CRYPT_CURVE25519_NO_HASH_METHOD No hash method is set. 246 * @retval CRYPT_NULL_INPUT The input parameter is empty. 247 */ 248 int32_t CRYPT_ED25519_GenKey(CRYPT_CURVE25519_Ctx *pkey); 249 #endif /* HITLS_CRYPTO_ED25519 */ 250 251 #ifdef HITLS_CRYPTO_X25519 252 /** 253 * @ingroup curve25519 254 * @brief x25519 Calculate the shared key based on the private key of the local end and the public key of the peer end. 255 * 256 * @param prvKey [IN] curve25519 Key pair structure, local private key 257 * @param pubKey [IN] curve25519 Key pair structure, peer public key 258 * @param sharedKey [OUT] Shared key 259 * @param shareKeyLen [IN/OUT] Shared key length 260 * 261 * @retval CRYPT_SUCCESS generated successfully. 262 * @retval CRYPT_CURVE25519_KEY_COMPUTE_FAILED Failed to generate the shared key. 263 */ 264 int32_t CRYPT_CURVE25519_ComputeSharedKey(CRYPT_CURVE25519_Ctx *prvKey, CRYPT_CURVE25519_Ctx *pubKey, 265 uint8_t *sharedKey, uint32_t *shareKeyLen); 266 267 /** 268 * @ingroup curve25519 269 * @brief x25519 Generate a key pair (public and private keys). 270 * 271 * @param pkey [IN/OUT] curve25519 Key pair structure/Key pair structure containing public and private keys 272 * 273 * @retval CRYPT_SUCCESS generated successfully. 274 * @retval CRYPT_NO_REGIST_RAND Unregistered random number callback 275 * @retval Error code of the registered random number module. Failed to obtain the random number. 276 * @retval CRYPT_NULL_INPUT The input parameter is empty. 277 */ 278 int32_t CRYPT_X25519_GenKey(CRYPT_CURVE25519_Ctx *pkey); 279 #endif /* HITLS_CRYPTO_X25519 */ 280 281 /** 282 * @ingroup curve25519 283 * @brief curve25519 Public key comparison 284 * 285 * @param a [IN] curve25519 Context structure 286 * @param b [IN] curve25519 Context structure 287 * 288 * @retval CRYPT_SUCCESS is the same 289 * @retval CRYPT_NULL_INPUT Invalid null pointer input 290 * @retval CRYPT_CURVE25519_PUBKEY_NOT_EQUAL Public Keys are not equal 291 */ 292 int32_t CRYPT_CURVE25519_Cmp(const CRYPT_CURVE25519_Ctx *a, const CRYPT_CURVE25519_Ctx *b); 293 294 /** 295 * @ingroup curve25519 296 * @brief curve25519 get security bits 297 * 298 * @param ctx [IN] curve25519 Context structure 299 * 300 * @retval security bits 301 */ 302 int32_t CRYPT_CURVE25519_GetSecBits(const CRYPT_CURVE25519_Ctx *ctx); 303 304 #ifdef HITLS_CRYPTO_PROVIDER 305 /** 306 * @ingroup curve25519 307 * @brief curve25519 import key 308 * 309 * @param ctx [IN/OUT] curve25519 context structure 310 * @param params [IN] parameters 311 */ 312 int32_t CRYPT_CURVE25519_Import(CRYPT_CURVE25519_Ctx *ctx, const BSL_Param *params); 313 314 /** 315 * @ingroup curve25519 316 * @brief curve25519 export key 317 * 318 * @param ctx [IN] curve25519 context structure 319 * @param params [IN/OUT] key parameters 320 */ 321 int32_t CRYPT_CURVE25519_Export(const CRYPT_CURVE25519_Ctx *ctx, BSL_Param *params); 322 #endif // HITLS_CRYPTO_PROVIDER 323 324 #ifdef __cplusplus 325 } 326 #endif 327 328 #endif // HITLS_CRYPTO_CURVE25519 329 330 #endif // CRYPT_CURVE25519_H 331