1# 明文导入密钥(C/C++) 2 3<!--Kit: Universal Keystore Kit--> 4<!--Subsystem: Security--> 5<!--Owner: @wutiantian-gitee--> 6<!--Designer: @HighLowWorld--> 7<!--Tester: @wxy1234564846--> 8<!--Adviser: @zengyawen--> 9 10以明文导入ECC密钥为例。具体的场景介绍及支持的算法规格,请参考[密钥导入支持的算法](huks-key-import-overview.md#支持的算法)。 11 12## 在CMake脚本中链接相关动态库 13```txt 14target_link_libraries(entry PUBLIC libhuks_ndk.z.so) 15``` 16## 开发步骤 17 181. 指定密钥别名,密钥别名命名规范参考[密钥生成介绍及算法规格](huks-key-generation-overview.md)。 19 202. 封装密钥属性集和密钥材料。通过[OH_Huks_InitParamSet](../../reference/apis-universal-keystore-kit/capi-native-huks-param-h.md#oh_huks_initparamset)、[OH_Huks_AddParams](../../reference/apis-universal-keystore-kit/capi-native-huks-param-h.md#oh_huks_addparams)、[OH_Huks_BuildParamSet](../../reference/apis-universal-keystore-kit/capi-native-huks-param-h.md#oh_huks_buildparamset)构造密钥属性集paramSet。 21 - 密钥属性集中必须包含[OH_Huks_KeyAlg](../../reference/apis-universal-keystore-kit/capi-native-huks-type-h.md#oh_huks_keyalg)、[OH_Huks_KeySize](../../reference/apis-universal-keystore-kit/capi-native-huks-type-h.md#oh_huks_keysize)、[OH_Huks_KeyPurpose](../../reference/apis-universal-keystore-kit/capi-native-huks-type-h.md#oh_huks_keypurpose)属性。 22 - 密钥材料须符合[HUKS密钥材料格式](huks-concepts.md#密钥材料格式)。 23 243. 调用[OH_Huks_ImportKeyItem](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_importkeyitem),传入密钥别名和密钥属性集,导入密钥。 25 26```c++ 27/* 以下以明文导入AES密钥为例 */ 28#include "huks/native_huks_api.h" 29#include "huks/native_huks_param.h" 30#include "napi/native_api.h" 31#include <cstring> 32 33#define OH_HUKS_AES_KEY_SIZE_32 32 34 35OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params, 36 uint32_t paramCount) 37{ 38 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet); 39 if (ret.errorCode != OH_HUKS_SUCCESS) { 40 return ret; 41 } 42 ret = OH_Huks_AddParams(*paramSet, params, paramCount); 43 if (ret.errorCode != OH_HUKS_SUCCESS) { 44 OH_Huks_FreeParamSet(paramSet); 45 return ret; 46 } 47 ret = OH_Huks_BuildParamSet(paramSet); 48 if (ret.errorCode != OH_HUKS_SUCCESS) { 49 OH_Huks_FreeParamSet(paramSet); 50 return ret; 51 } 52 return ret; 53} 54struct OH_Huks_Param g_testImportKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES}, 55 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT}, 56 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256}, 57 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}}; 58 59static napi_value ImportKey(napi_env env, napi_callback_info info) { 60 const char *alias = "test_import"; 61 struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias}; 62 /* DER格式的密钥,用于后续导入密钥 */ 63 uint8_t pubKey[OH_HUKS_AES_KEY_SIZE_32] = {0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63, 64 0x2a, 0x7c, 0x86, 0xba, 0xca, 0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa, 65 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f, 0x9e, 0x3c, 0xe5, 0xf9}; 66 struct OH_Huks_Blob publicKey = {OH_HUKS_AES_KEY_SIZE_32, pubKey}; 67 struct OH_Huks_ParamSet *testImportKeyParamSet = nullptr; 68 struct OH_Huks_Result ohResult; 69 do { 70 ohResult = InitParamSet(&testImportKeyParamSet, g_testImportKeyParam, 71 sizeof(g_testImportKeyParam) / sizeof(OH_Huks_Param)); 72 if (ohResult.errorCode != OH_HUKS_SUCCESS) { 73 break; 74 } 75 /* 4. Import Key */ 76 char newKey[] = "test_import"; 77 struct OH_Huks_Blob newKeyAlias = {.size = (uint32_t)strlen(newKey), .data = (uint8_t *)newKey}; 78 ohResult = OH_Huks_ImportKeyItem(&newKeyAlias, testImportKeyParamSet, &publicKey); 79 } while (0); 80 OH_Huks_FreeParamSet(&testImportKeyParamSet); 81 napi_value ret; 82 napi_create_int32(env, ohResult.errorCode, &ret); 83 return ret; 84} 85``` 86