• 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> **NOTE**<br>
7> Key aliases must not contain sensitive information, such as personal data.
8
9## Adding the Dynamic Library in the CMake Script
10```txt
11   target_link_libraries(entry PUBLIC libhuks_ndk.z.so)
12```
13
14## How to Develop
15
161. Set the alias (**keyAlias**) of the key to generate.
17   - The key alias cannot exceed 128 bytes.
18   - 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.
19
202. 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).
21   **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).
22
23   > **NOTE**<br>A key can have only one purpose (**HUKS_TAG_PURPOSE**), and the purpose must be the same in the lifecycle of the key. For details, see [key usage](huks-key-generation-overview.md).
24
253. 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.
26
27   > **NOTE**
28   >
29   > 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.
30   >
31
32   ```c++
33   /* Generate an ECC key. */
34   #include "huks/native_huks_api.h"
35   #include "huks/native_huks_param.h"
36   #include <string.h>
37   OH_Huks_Result InitParamSet(
38       struct OH_Huks_ParamSet **paramSet,
39       const struct OH_Huks_Param *params,
40       uint32_t paramCount)
41   {
42       OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
43       if (ret.errorCode != OH_HUKS_SUCCESS) {
44           return ret;
45       }
46       ret = OH_Huks_AddParams(*paramSet, params, paramCount);
47       if (ret.errorCode != OH_HUKS_SUCCESS) {
48           OH_Huks_FreeParamSet(paramSet);
49           return ret;
50       }
51       ret = OH_Huks_BuildParamSet(paramSet);
52       if (ret.errorCode != OH_HUKS_SUCCESS) {
53           OH_Huks_FreeParamSet(paramSet);
54           return ret;
55       }
56       return ret;
57   }
58   struct OH_Huks_Param g_testGenerateKeyParam[] = {
59       {
60           .tag = OH_HUKS_TAG_ALGORITHM,
61           .uint32Param = OH_HUKS_ALG_ECC
62       }, {
63           .tag = OH_HUKS_TAG_PURPOSE,
64           .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE
65       }, {
66           .tag = OH_HUKS_TAG_KEY_SIZE,
67           .uint32Param = OH_HUKS_ECC_KEY_SIZE_256
68       }, {
69           .tag = OH_HUKS_TAG_DIGEST,
70           .uint32Param = OH_HUKS_DIGEST_NONE
71       }
72   };
73   static napi_value GenerateKey(napi_env env, napi_callback_info info)
74   {
75       /* 1. Set the key alias. */
76       const char *alias = "test_generate";
77       struct OH_Huks_Blob aliasBlob = { .size = (uint32_t)strlen(alias), .data = (uint8_t *)alias };
78       struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr;
79       struct OH_Huks_Result ohResult;
80       do {
81           /* 2. Initialize the key property set. */
82           ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam,
83               sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
84           if (ohResult.errorCode != OH_HUKS_SUCCESS) {
85               break;
86           }
87           /* 3. Generate a key. */
88           ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr);
89       } while (0);
90       OH_Huks_FreeParamSet(&testGenerateKeyParamSet);
91       napi_value ret;
92       napi_create_int32(env, ohResult.errorCode, &ret);
93       return ret;
94   }
95   ```