1# CryptoAsymKeyApi 2 3 4## Overview 5 6Provides APIs for asymmetric keys. 7 8**Since**: 12 9 10 11## Summary 12 13 14### Files 15 16| Name | Description | 17| -------- | -------- | 18| [crypto_asym_key.h](crypto__asym__key_8h.md) | Defines APIs for asymmetric keys. | 19 20 21### Types 22 23| Name | Description | 24| -------- | -------- | 25| typedef struct [OH_CryptoKeyPair](#oh_cryptokeypair) [OH_CryptoKeyPair](#oh_cryptokeypair) | Defines a struct for an asymmetric key pair. | 26| typedef struct [OH_CryptoPubKey](#oh_cryptopubkey) [OH_CryptoPubKey](#oh_cryptopubkey) | Defines a struct for the public key in an asymmetric key pair. | 27| typedef struct [OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) [OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) | Defines a struct for an asymmetric key generator. | 28 29 30### Enums 31 32| Name | Description | 33| -------- | -------- | 34| [CryptoAsymKey_ParamType](#cryptoasymkey_paramtype) {<br>CRYPTO_DSA_P_DATABLOB = 101, CRYPTO_DSA_Q_DATABLOB = 102,<br>CRYPTO_DSA_G_DATABLOB = 103, CRYPTO_DSA_SK_DATABLOB = 104,<br>CRYPTO_DSA_PK_DATABLOB = 105, CRYPTO_ECC_FP_P_DATABLOB = 201,<br>CRYPTO_ECC_A_DATABLOB = 202, CRYPTO_ECC_B_DATABLOB = 203,<br>CRYPTO_ECC_G_X_DATABLOB = 204, CRYPTO_ECC_G_Y_DATABLOB = 205,<br>CRYPTO_ECC_N_DATABLOB = 206, CRYPTO_ECC_H_INT = 207,<br>CRYPTO_ECC_SK_DATABLOB = 208, CRYPTO_ECC_PK_X_DATABLOB = 209,<br>CRYPTO_ECC_PK_Y_DATABLOB = 210, CRYPTO_ECC_FIELD_TYPE_STR = 211,<br>CRYPTO_ECC_FIELD_SIZE_INT = 212, CRYPTO_ECC_CURVE_NAME_STR = 213,<br>CRYPTO_RSA_N_DATABLOB = 301, CRYPTO_RSA_D_DATABLOB = 302,<br>CRYPTO_RSA_E_DATABLOB = 303, CRYPTO_DH_P_DATABLOB = 401,<br>CRYPTO_DH_G_DATABLOB = 402, CRYPTO_DH_L_NUM = 403,<br>CRYPTO_DH_SK_DATABLOB = 404, CRYPTO_DH_PK_DATABLOB = 405,<br>CRYPTO_ED25519_SK_DATABLOB = 501, CRYPTO_ED25519_PK_DATABLOB = 502,<br>CRYPTO_X25519_SK_DATABLOB = 601, CRYPTO_X25519_PK_DATABLOB = 602<br>} | Enumerates the types of the asymmetric key parameters. | 35| [Crypto_EncodingType](#crypto_encodingtype) { CRYPTO_PEM = 0, CRYPTO_DER = 1 } | Enumerates the encoding types. | 36 37 38### Functions 39 40| Name | Description | 41| -------- | -------- | 42| [OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode) [OH_CryptoAsymKeyGenerator_Create](#oh_cryptoasymkeygenerator_create) (const char \*algoName, [OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) \*\*ctx) | Creates an asymmetric key generator instance based on the specified algorithm. | 43| [OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode) [OH_CryptoAsymKeyGenerator_Generate](#oh_cryptoasymkeygenerator_generate) ([OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) \*ctx, [OH_CryptoKeyPair](#oh_cryptokeypair) \*\*keyCtx) | Randomly generates an asymmetric key pair. | 44| [OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode) [OH_CryptoAsymKeyGenerator_Convert](#oh_cryptoasymkeygenerator_convert) ([OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) \*ctx, [Crypto_EncodingType](#crypto_encodingtype) type, [Crypto_DataBlob](_crypto___data_blob.md) \*pubKeyData, [Crypto_DataBlob](_crypto___data_blob.md) \*priKeyData, [OH_CryptoKeyPair](#oh_cryptokeypair) \*\*keyCtx) | Converts asymmetric key data into a key pair. | 45| const char \* [OH_CryptoAsymKeyGenerator_GetAlgoName](#oh_cryptoasymkeygenerator_getalgoname) ([OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) \*ctx) | Obtains the asymmetric key algorithm. | 46| void [OH_CryptoAsymKeyGenerator_Destroy](#oh_cryptoasymkeygenerator_destroy) ([OH_CryptoAsymKeyGenerator](#oh_cryptoasymkeygenerator) \*ctx) | Destroys an asymmetric key generator instance. | 47| void [OH_CryptoKeyPair_Destroy](#oh_cryptokeypair_destroy) ([OH_CryptoKeyPair](#oh_cryptokeypair) \*keyCtx) | Destroys an asymmetric key pair instance. | 48| [OH_CryptoPubKey](#oh_cryptopubkey) \* [OH_CryptoKeyPair_GetPubKey](#oh_cryptokeypair_getpubkey) ([OH_CryptoKeyPair](#oh_cryptokeypair) \*keyCtx) | Obtains the public key instance from a key pair. | 49| [OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode) [OH_CryptoPubKey_Encode](#oh_cryptopubkey_encode) ([OH_CryptoPubKey](#oh_cryptopubkey) \*key, [Crypto_EncodingType](#crypto_encodingtype) type, const char \*encodingStandard, [Crypto_DataBlob](_crypto___data_blob.md) \*out) | Encodes the public key data in the specified format. | 50| [OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode) [OH_CryptoPubKey_GetParam](#oh_cryptopubkey_getparam) ([OH_CryptoPubKey](#oh_cryptopubkey) \*key, [CryptoAsymKey_ParamType](#cryptoasymkey_paramtype) item, [Crypto_DataBlob](_crypto___data_blob.md) \*value) | Obtains the specified parameter from a public key instance. | 51 52 53## Type Description 54 55 56### OH_CryptoAsymKeyGenerator 57 58``` 59typedef struct OH_CryptoAsymKeyGenerator OH_CryptoAsymKeyGenerator 60``` 61 62**Description** 63 64Defines a struct for an asymmetric key generator. 65 66**Since**: 12 67 68 69### OH_CryptoKeyPair 70 71``` 72typedef struct OH_CryptoKeyPair OH_CryptoKeyPair 73``` 74 75**Description** 76 77Defines a struct for an asymmetric key pair. 78 79**Since**: 12 80 81 82### OH_CryptoPubKey 83 84``` 85typedef struct OH_CryptoPubKey OH_CryptoPubKey 86``` 87 88**Description** 89 90Defines a struct for the public key in an asymmetric key pair. 91 92**Since**: 12 93 94 95## Enum Description 96 97 98### Crypto_EncodingType 99 100``` 101enum Crypto_EncodingType 102``` 103 104**Description** 105 106Enumerates the encoding types. 107 108**Since**: 12 109 110| Enum | Description | 111| -------- | -------- | 112| CRYPTO_PEM | PEM. | 113| CRYPTO_DER | DER. | 114 115 116### CryptoAsymKey_ParamType 117 118``` 119enum CryptoAsymKey_ParamType 120``` 121 122**Description** 123 124Enumerates the types of the asymmetric key parameters. 125 126**Since**: 12 127 128| Enum | Description | 129| -------- | -------- | 130| CRYPTO_DSA_P_DATABLOB | Prime modulus **p** in the DSA algorithm. | 131| CRYPTO_DSA_Q_DATABLOB | Parameter **q**, prime factor of (p – 1) in the DSA algorithm. | 132| CRYPTO_DSA_G_DATABLOB | Parameter **g** in the DSA algorithm. | 133| CRYPTO_DSA_SK_DATABLOB | Private key **sk** in the DSA algorithm. | 134| CRYPTO_DSA_PK_DATABLOB | Public key **pk** in the DSA algorithm. | 135| CRYPTO_ECC_FP_P_DATABLOB | Prime number **p** in the **Fp** field of the elliptic curve in the ECC algorithm. | 136| CRYPTO_ECC_A_DATABLOB | First coefficient **a** of the elliptic curve in the ECC algorithm. | 137| CRYPTO_ECC_B_DATABLOB | Second coefficient **b** of the elliptic curve in the ECC algorithm. | 138| CRYPTO_ECC_G_X_DATABLOB | X coordinate of the base point **g** in the ECC algorithm. | 139| CRYPTO_ECC_G_Y_DATABLOB | Y coordinate of the base point **g** in the ECC algorithm. | 140| CRYPTO_ECC_N_DATABLOB | Order **n** of the base point **g** in the ECC algorithm. | 141| CRYPTO_ECC_H_INT | Cofactor **h** in the ECC algorithm. | 142| CRYPTO_ECC_SK_DATABLOB | Private key **sk** in the ECC algorithm. | 143| CRYPTO_ECC_PK_X_DATABLOB | X coordinate of the public key **pk** (a point on the elliptic curve) in the ECC algorithm. | 144| CRYPTO_ECC_PK_Y_DATABLOB | Y coordinate of the public key **pk** (a point on the elliptic curve) in the ECC algorithm. | 145| CRYPTO_ECC_FIELD_TYPE_STR | Elliptic curve field type in the ECC algorithm. Currently, only the **Fp** field is supported. | 146| CRYPTO_ECC_FIELD_SIZE_INT | Size of the field in the ECC algorithm, in bits.<br>**NOTE**: The size of the **Fp** field is the length of the prime **p**, in bits. | 147| CRYPTO_ECC_CURVE_NAME_STR | Standards for Efficient Cryptography Group (SECG) curve name in the ECC algorithm. | 148| CRYPTO_RSA_N_DATABLOB | Modulus **n** in the RSA algorithm. | 149| CRYPTO_RSA_D_DATABLOB | Private key **sk** (private key exponent **d**) in the RSA algorithm. | 150| CRYPTO_RSA_E_DATABLOB | Public key **pk** (public key exponent **e**) in the RSA algorithm. | 151| CRYPTO_DH_P_DATABLOB | Prime **p** in the DH algorithm. | 152| CRYPTO_DH_G_DATABLOB | Parameter **g** in the DH algorithm. | 153| CRYPTO_DH_L_NUM | Length of the private key in the DH algorithm, in bits. | 154| CRYPTO_DH_SK_DATABLOB | Private key **sk** in the DH algorithm. | 155| CRYPTO_DH_PK_DATABLOB | Public key **pk** in the DH algorithm. | 156| CRYPTO_ED25519_SK_DATABLOB | Private key **sk** in the Ed25519 algorithm. | 157| CRYPTO_ED25519_PK_DATABLOB | Public key **pk** in the Ed25519 algorithm. | 158| CRYPTO_X25519_SK_DATABLOB | Private key **sk** in the X25519 algorithm. | 159| CRYPTO_X25519_PK_DATABLOB | Public key **pk** in the X25519 algorithm. | 160 161 162## Function Description 163 164 165### OH_CryptoAsymKeyGenerator_Convert() 166 167``` 168OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Convert (OH_CryptoAsymKeyGenerator *ctx, Crypto_EncodingType type, Crypto_DataBlob *pubKeyData, Crypto_DataBlob *priKeyData, OH_CryptoKeyPair **keyCtx ) 169``` 170 171**Description** 172 173Converts binary data into an asymmetric key pair. 174 175**Since**: 12 176 177**Parameters** 178 179| Name | Description | 180| -------- | -------- | 181| ctx | Pointer to the asymmetric key generator instance. | 182| type | Encoding type. | 183| pubKeyData | Pointer to the public key data to convert. | 184| priKeyData | Pointer to the private key data to convert. | 185| keyCtx | Pointer to the asymmetric key pair instance. | 186 187**Returns** 188 189[OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode): 190 191**0**: The operation is successful. 192 193**401**: Invalid parameters are detected. 194 195**801**: The operation is not supported. 196 197**17620001**: A memory error occurred. 198 199**17630001**: Failed to call an API of a third-party algorithm library. 200 201 202### OH_CryptoAsymKeyGenerator_Create() 203 204``` 205OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Create (const char *algoName, OH_CryptoAsymKeyGenerator **ctx ) 206``` 207 208**Description** 209 210Creates an asymmetric key generator instance based on the specified algorithm. 211 212**Since**: 12 213 214**Parameters** 215 216| Name | Description | 217| -------- | -------- | 218| algoName | Pointer to the algorithm used to create the asymmetric key generator instance. For example, **'RSA1024\|PRIMES_2'**. | 219| ctx | Pointer to the asymmetric key generator instance created. | 220 221**Returns** 222 223[OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode): 224 225**0**: The operation is successful. 226 227**401**: Invalid parameters are detected. 228 229**801**: The operation is not supported. 230 231**17620001**: A memory error occurred. 232 233**17630001**: Failed to call an API of a third-party algorithm library. 234 235 236### OH_CryptoAsymKeyGenerator_Destroy() 237 238``` 239void OH_CryptoAsymKeyGenerator_Destroy (OH_CryptoAsymKeyGenerator *ctx) 240``` 241 242**Description** 243 244Destroys an asymmetric key generator instance. 245 246**Since**: 12 247 248**Parameters** 249 250| Name | Description | 251| -------- | -------- | 252| ctx | Pointer to the asymmetric key generator instance to destroy. | 253 254 255### OH_CryptoAsymKeyGenerator_Generate() 256 257``` 258OH_Crypto_ErrCode OH_CryptoAsymKeyGenerator_Generate (OH_CryptoAsymKeyGenerator *ctx, OH_CryptoKeyPair **keyCtx ) 259``` 260 261**Description** 262 263Randomly generates an asymmetric key pair. 264 265**Since**: 12 266 267**Parameters** 268 269| Name | Description | 270| -------- | -------- | 271| ctx | Pointer to the asymmetric key generator instance. | 272| keyCtx | Pointer to the asymmetric key pair instance created. | 273 274**Returns** 275 276[OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode): 277 278**0**: The operation is successful. 279 280**401**: Invalid parameters are detected. 281 282**801**: The operation is not supported. 283 284**17620001**: A memory error occurred. 285 286**17630001**: Failed to call an API of a third-party algorithm library. 287 288 289### OH_CryptoAsymKeyGenerator_GetAlgoName() 290 291``` 292const char* OH_CryptoAsymKeyGenerator_GetAlgoName (OH_CryptoAsymKeyGenerator *ctx) 293``` 294 295**Description** 296 297Obtains the asymmetric key algorithm. 298 299**Since**: 12 300 301**Parameters** 302 303| Name | Description | 304| -------- | -------- | 305| ctx | Pointer to the asymmetric key generator instance. | 306 307**Returns** 308 309Name of the asymmetric key algorithm obtained. 310 311 312### OH_CryptoKeyPair_Destroy() 313 314``` 315void OH_CryptoKeyPair_Destroy (OH_CryptoKeyPair *keyCtx) 316``` 317 318**Description** 319 320Destroys an asymmetric key pair instance. 321 322**Since**: 12 323 324**Parameters** 325 326| Name | Description | 327| -------- | -------- | 328| keyCtx | Pointer to the asymmetric key pair instance to destroy. | 329 330 331### OH_CryptoKeyPair_GetPubKey() 332 333``` 334OH_CryptoPubKey* OH_CryptoKeyPair_GetPubKey (OH_CryptoKeyPair *keyCtx) 335``` 336 337**Description** 338 339Obtains the public key from a key pair. 340 341**Since**: 12 342 343**Parameters** 344 345| Name | Description | 346| -------- | -------- | 347| keyCtx | Pointer to the key pair instance. | 348 349**Returns** 350 351Public key obtained. 352 353 354### OH_CryptoPubKey_Encode() 355 356``` 357OH_Crypto_ErrCode OH_CryptoPubKey_Encode (OH_CryptoPubKey *key, Crypto_EncodingType type, const char *encodingStandard, Crypto_DataBlob *out ) 358``` 359 360**Description** 361 362Encodes the public key data in the specified format. 363 364**Since**: 12 365 366**Parameters** 367 368| Name | Description | 369| -------- | -------- | 370| key | Ponter to the public key data. | 371| type | Encoding type. | 372| encodingStandard | Encoding format. | 373| out | Pointer to the public key encoded in the specified format. | 374 375**Returns** 376 377[OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode): 378 379**0**: The operation is successful. 380 381**401**: Invalid parameters are detected. 382 383**801**: The operation is not supported. 384 385**17620001**: A memory error occurred. 386 387**17630001**: Failed to call an API of a third-party algorithm library. 388 389 390### OH_CryptoPubKey_GetParam() 391 392``` 393OH_Crypto_ErrCode OH_CryptoPubKey_GetParam (OH_CryptoPubKey *key, CryptoAsymKey_ParamType item, Crypto_DataBlob *value ) 394``` 395 396**Description** 397 398Obtains the specified parameter from a public key instance. 399 400**Since**: 12 401 402**Parameters** 403 404| Name | Description | 405| -------- | -------- | 406| key | Ponter to the public key data. | 407| item | Type of the asymmetric key parameter to obtain. | 408| value | Pointer to the parameter value obtained. | 409 410**Returns** 411 412[OH_Crypto_ErrCode](_crypto_common_api.md#oh_crypto_errcode): 413 414**0**: The operation is successful. 415 416**401**: Invalid parameters are detected. 417 418**801**: The operation is not supported. 419 420**17620001**: A memory error occurred. 421 422**17630001**: Failed to call an API of a third-party algorithm library. 423