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