• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Checking a Key (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
10Check whether a key exists.
11
12## Add the dynamic library in the CMake script.
13```txt
14target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
15```
16## How to Develop
17
181. Construct the parameters.
19   - Specify the key alias. For details about the naming rules, see [Key Generation Overview and Algorithm Specifications](huks-key-generation-overview.md).
20   - Set the [property tags](../../reference/apis-universal-keystore-kit/capi-native-huks-type-h.md#oh_huks_tag) required for querying a key. By default, this parameter is left empty.
21
222. Call [OH_Huks_IsKeyItemExist](../../reference/apis-universal-keystore-kit/capi-native-huks-api-h.md#oh_huks_iskeyitemexist) to check whether the key exists.
23
24```c++
25#include "huks/native_huks_api.h"
26#include "huks/native_huks_param.h"
27#include "napi/native_api.h"
28#include <cstring>
29
30/* Generate an ECC key. */
31OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params,
32                            uint32_t paramCount)
33{
34    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
35    if (ret.errorCode != OH_HUKS_SUCCESS) {
36        return ret;
37    }
38    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
39    if (ret.errorCode != OH_HUKS_SUCCESS) {
40        OH_Huks_FreeParamSet(paramSet);
41        return ret;
42    }
43    ret = OH_Huks_BuildParamSet(paramSet);
44    if (ret.errorCode != OH_HUKS_SUCCESS) {
45        OH_Huks_FreeParamSet(paramSet);
46        return ret;
47    }
48    return ret;
49}
50
51struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC},
52                                                 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
53                                                 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
54                                                 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}};
55
56static OH_Huks_Result GenerateKeyHelper(const char *alias)
57{
58    struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias};
59    struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr;
60    struct OH_Huks_Result ohResult;
61    do {
62        /* 1. Initialize the key property set. */
63        ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam,
64                                sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
65        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
66            break;
67        }
68        /* 2. Generate a key. */
69        ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr);
70    } while (0);
71    OH_Huks_FreeParamSet(&testGenerateKeyParamSet);
72    return ohResult;
73}
74
75static napi_value IsKeyExist(napi_env env, napi_callback_info info)
76{
77    /* 1. Obtain the key alias. */
78    const char *alias = "test_key";
79    struct OH_Huks_Blob keyAlias = {
80        (uint32_t)strlen(alias),
81        (uint8_t *)alias
82    };
83
84    /* Generate a key. */
85    OH_Huks_Result genResult = GenerateKeyHelper(alias);
86    if (genResult.errorCode != OH_HUKS_SUCCESS) {
87        napi_value ret;
88        napi_create_int32(env, genResult.errorCode, &ret);
89        return ret;
90    }
91
92    /* 2. Call OH_Huks_IsKeyItemExist to check whether the key exists. */
93    struct OH_Huks_Result ohResult = OH_Huks_IsKeyItemExist(&keyAlias, nullptr);
94
95    napi_value ret;
96    napi_create_int32(env, ohResult.errorCode, &ret);
97    return ret;
98}
99```
100