1e41f4b71Sopenharmony_ci# Importing a Key in Plaintext (C/C++) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciThis topic walks you through on how to import an ECC key. For details about the scenarios and supported algorithm specifications, see [Supported Algorithms](huks-key-import-overview.md#supported-algorithms). 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci## Add the dynamic library in the CMake script. 7e41f4b71Sopenharmony_ci```txt 8e41f4b71Sopenharmony_ci target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 9e41f4b71Sopenharmony_ci``` 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## How to Develop 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci1. Set the alias **keyAlias** of the key to import. 14e41f4b71Sopenharmony_ci The key alias cannot exceed 128 bytes. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci2. Encapsulate the key property set and key material. Construct the key property set **paramSet** using [OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_initparamset), [OH_Huks_AddParams](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_addparams), and [OH_Huks_BuildParamSet](../../reference/apis-universal-keystore-kit/_huks_param_set_api.md#oh_huks_buildparamset). 17e41f4b71Sopenharmony_ci - ** paramSet** must contain [OH_Huks_KeyAlg](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_keyalg), [OH_Huks_KeySize](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_keysize), and [OH_Huks_KeyPurpose](../../reference/apis-universal-keystore-kit/_huks_type_api.md#oh_huks_keypurpose). 18e41f4b71Sopenharmony_ci - The key material must comply with the [HUKS key material format](huks-concepts.md#key-material-format). 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci3. Use [OH_Huks_ImportKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_importkeyitem) to import the key. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci```c++ 23e41f4b71Sopenharmony_ci/* Import an ECC key in plaintext. */ 24e41f4b71Sopenharmony_ci#include "huks/native_huks_api.h" 25e41f4b71Sopenharmony_ci#include "huks/native_huks_param.h" 26e41f4b71Sopenharmony_ci#include <string.h> 27e41f4b71Sopenharmony_ciOH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, 28e41f4b71Sopenharmony_ci uint32_t paramCount) { 29e41f4b71Sopenharmony_ci OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 30e41f4b71Sopenharmony_ci if (ret.errorCode != OH_HUKS_SUCCESS) { 31e41f4b71Sopenharmony_ci return ret; 32e41f4b71Sopenharmony_ci } 33e41f4b71Sopenharmony_ci ret = OH_Huks_AddParams(*paramSet, params, paramCount); 34e41f4b71Sopenharmony_ci if (ret.errorCode != OH_HUKS_SUCCESS) { 35e41f4b71Sopenharmony_ci OH_Huks_FreeParamSet(paramSet); 36e41f4b71Sopenharmony_ci return ret; 37e41f4b71Sopenharmony_ci } 38e41f4b71Sopenharmony_ci ret = OH_Huks_BuildParamSet(paramSet); 39e41f4b71Sopenharmony_ci if (ret.errorCode != OH_HUKS_SUCCESS) { 40e41f4b71Sopenharmony_ci OH_Huks_FreeParamSet(paramSet); 41e41f4b71Sopenharmony_ci return ret; 42e41f4b71Sopenharmony_ci } 43e41f4b71Sopenharmony_ci return ret; 44e41f4b71Sopenharmony_ci} 45e41f4b71Sopenharmony_cistruct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC}, 46e41f4b71Sopenharmony_ci {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE}, 47e41f4b71Sopenharmony_ci {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256}, 48e41f4b71Sopenharmony_ci {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}}; 49e41f4b71Sopenharmony_cistatic napi_value GenerateKey(napi_env env, napi_callback_info info) { 50e41f4b71Sopenharmony_ci const char *alias = "test_generate"; 51e41f4b71Sopenharmony_ci struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; 52e41f4b71Sopenharmony_ci struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr; 53e41f4b71Sopenharmony_ci struct OH_Huks_Result ohResult; 54e41f4b71Sopenharmony_ci do { 55e41f4b71Sopenharmony_ci ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam, 56e41f4b71Sopenharmony_ci sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); 57e41f4b71Sopenharmony_ci if (ohResult.errorCode != OH_HUKS_SUCCESS) { 58e41f4b71Sopenharmony_ci break; 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr); 61e41f4b71Sopenharmony_ci } while (0); 62e41f4b71Sopenharmony_ci OH_Huks_FreeParamSet(&testGenerateKeyParamSet); 63e41f4b71Sopenharmony_ci napi_value ret; 64e41f4b71Sopenharmony_ci napi_create_int32(env, ohResult.errorCode, &ret); 65e41f4b71Sopenharmony_ci return ret; 66e41f4b71Sopenharmony_ci} 67e41f4b71Sopenharmony_cistatic napi_value ImportKey(napi_env env, napi_callback_info info) { 68e41f4b71Sopenharmony_ci (void)GenerateKey(env, info); 69e41f4b71Sopenharmony_ci const char *alias = "test_generate"; 70e41f4b71Sopenharmony_ci struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; 71e41f4b71Sopenharmony_ci uint8_t pubKey[OH_HUKS_ECC_KEY_SIZE_256] = {0}; 72e41f4b71Sopenharmony_ci struct OH_Huks_Blob publicKey = {OH_HUKS_ECC_KEY_SIZE_256, pubKey}; 73e41f4b71Sopenharmony_ci struct OH_Huks_ParamSet *testImportKeyParamSet = nullptr; 74e41f4b71Sopenharmony_ci struct OH_Huks_Result ohResult; 75e41f4b71Sopenharmony_ci do { 76e41f4b71Sopenharmony_ci ohResult = InitParamSet(&testImportKeyParamSet, g_testGenerateKeyParam, 77e41f4b71Sopenharmony_ci sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param)); 78e41f4b71Sopenharmony_ci if (ohResult.errorCode != OH_HUKS_SUCCESS) { 79e41f4b71Sopenharmony_ci break; 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci ohResult = OH_Huks_ExportPublicKeyItem(&aliasBlob, testImportKeyParamSet, &publicKey); 82e41f4b71Sopenharmony_ci if (ohResult.errorCode != OH_HUKS_SUCCESS) { 83e41f4b71Sopenharmony_ci break; 84e41f4b71Sopenharmony_ci } 85e41f4b71Sopenharmony_ci /* 4. Import Key */ 86e41f4b71Sopenharmony_ci char newKey[] = "test_import"; 87e41f4b71Sopenharmony_ci struct OH_Huks_Blob newKeyAlias = {.size = (uint32_t)strlen(newKey), .data = (uint8_t *)newKey}; 88e41f4b71Sopenharmony_ci ohResult = OH_Huks_ImportKeyItem(&newKeyAlias, testImportKeyParamSet, &publicKey); 89e41f4b71Sopenharmony_ci } while (0); 90e41f4b71Sopenharmony_ci OH_Huks_FreeParamSet(&testImportKeyParamSet); 91e41f4b71Sopenharmony_ci napi_value ret; 92e41f4b71Sopenharmony_ci napi_create_int32(env, ohResult.errorCode, &ret); 93e41f4b71Sopenharmony_ci return ret; 94e41f4b71Sopenharmony_ci} 95e41f4b71Sopenharmony_ci``` 96