跳到主要内容

密钥删除(C/C++)

为保证数据安全性,当不需要使用该密钥时,应该删除密钥。

从API 23开始支持群组密钥特性。

在CMake脚本中链接相关动态库

target_link_libraries(entry PUBLIC libhuks_ndk.z.so)

开发步骤

以删除ECC密钥为例。

  1. 指定密钥别名,密钥别名命名规范参考密钥生成介绍及算法规格
  2. 初始化密钥属性集。用于删除时指定密钥的属性,删除单个密钥或者非群组密钥,可传空。
  3. 调用接口OH_Huks_DeleteKeyItem,删除密钥。
#include "huks/native_huks_api.h"
#include "huks/native_huks_param.h"
#include "napi/native_api.h"
#include <cstring>

OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params,
uint32_t paramCount)
{
OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
if (ret.errorCode != OH_HUKS_SUCCESS) {
return ret;
}
ret = OH_Huks_AddParams(*paramSet, params, paramCount);
if (ret.errorCode != OH_HUKS_SUCCESS) {
OH_Huks_FreeParamSet(paramSet);
return ret;
}
ret = OH_Huks_BuildParamSet(paramSet);
if (ret.errorCode != OH_HUKS_SUCCESS) {
OH_Huks_FreeParamSet(paramSet);
return ret;
}
return ret;
}

struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC},
{.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
{.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
{.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}};

/* 1.生成密钥 */
static OH_Huks_Result GenerateKeyHelper(const char *alias)
{
struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias};
struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr;
struct OH_Huks_Result ohResult;

do {
ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam,
sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
if (ohResult.errorCode != OH_HUKS_SUCCESS) {
break;
}

ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr);
} while (0);

OH_Huks_FreeParamSet(&testGenerateKeyParamSet);
return ohResult;
}

static napi_value DeleteKey(napi_env env, napi_callback_info info)
{
const char *alias = "test_key";
struct OH_Huks_Blob keyAlias = {
(uint32_t)strlen("test_key"),
(uint8_t *)"test_key"
};

/* 1.生成密钥 */
OH_Huks_Result genResult = GenerateKeyHelper(alias);
if (genResult.errorCode != OH_HUKS_SUCCESS) {
napi_value ret;
napi_create_int32(env, genResult.errorCode, &ret);
return ret;
}

/* 2.删除密钥 */
struct OH_Huks_Result ohResult = OH_Huks_DeleteKeyItem(&keyAlias, nullptr);

napi_value ret;
napi_create_int32(env, ohResult.errorCode, &ret);
return ret;
}