• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Generating a Key (C/C++)
2
3
4This topic walks you through on how to randomly generate an ECC key. For details about the scenarios and supported algorithms, see [Supported Algorithms](huks-key-generation-overview.md#supported-algorithms).
5
6
7## How to Develop
8
91. Set the alias (**keyAlias**) of the key to generate.
10   - The key alias cannot exceed 64 bytes.
11   - For the keys generated for different services, HUKS isolates the storage paths based on the service identity information to prevent conflicts caused by the same key alias.
12
132. Initialize the key property set. 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).
14   ** 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).
15
163. Use [OH_Huks_GenerateKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_generatekeyitem) to generate a key based on the key alias and key properties specified.
17
18> **NOTE**<br>
19> If the service uses the same key alias to call the HUKS API to generate a key again, HUKS will generate a new key and overwrite the historical key file.
20
21```c++
22/* Generate an ECC key. */
23#include "huks/native_huks_api.h"
24#include "huks/native_huks_param.h"
25#include <string.h>
26OH_Huks_Result InitParamSet(
27    struct OH_Huks_ParamSet **paramSet,
28    const struct OH_Huks_Param *params,
29    uint32_t paramCount)
30{
31    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
32    if (ret.errorCode != OH_HUKS_SUCCESS) {
33        return ret;
34    }
35    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
36    if (ret.errorCode != OH_HUKS_SUCCESS) {
37        OH_Huks_FreeParamSet(paramSet);
38        return ret;
39    }
40    ret = OH_Huks_BuildParamSet(paramSet);
41    if (ret.errorCode != OH_HUKS_SUCCESS) {
42        OH_Huks_FreeParamSet(paramSet);
43        return ret;
44    }
45    return ret;
46}
47struct OH_Huks_Param g_testGenerateKeyParam[] = {
48    {
49        .tag = OH_HUKS_TAG_ALGORITHM,
50        .uint32Param = OH_HUKS_ALG_ECC
51    }, {
52        .tag = OH_HUKS_TAG_PURPOSE,
53        .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE
54    }, {
55        .tag = OH_HUKS_TAG_KEY_SIZE,
56        .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
57    }, {
58        .tag = OH_HUKS_TAG_DIGEST,
59        .uint32Param = OH_HUKS_DIGEST_NONE
60    }
61};
62static napi_value GenerateKey(napi_env env, napi_callback_info info)
63{
64    /* 1. Set the key alias. */
65    const char *alias = "test_generate";
66    struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias };
67    struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr;
68    struct OH_Huks_Result ohResult;
69    do {
70        /* 2. Initialize the key property set. */
71        ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam,
72            sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
73        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
74            break;
75        }
76        /* 3. Generate a key. */
77        ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr);
78    } while (0);
79    OH_Huks_FreeParamSet(&testGenerateKeyParamSet);
80    napi_value ret;
81    napi_create_int32(env, ohResult.errorCode, &ret);
82    return ret;
83}
84```
85