• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Importing a Key in Plaintext (C/C++)
2
3
4This topic walks you through on how to import an ECC key. For details about the scenarios and supported algorithm specifications, see [Supported Algorithms](huks-key-import-overview.md#supported-algorithms).
5
6
7## How to Develop
8
91. Set the alias **keyAlias** of the key to import.
10   The key alias cannot exceed 64 bytes.
11
122. Encapsulate the key property set and key material. 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).
13   - ** 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).
14   - The key material must comply with the [HUKS key material format](huks-concepts.md#key-material-format).
15
163. Use [OH_Huks_ImportKeyItem](../../reference/apis-universal-keystore-kit/_huks_key_api.md#oh_huks_importkeyitem) to import the key.
17
18```c++
19/* Import an ECC key in plaintext. */
20#include "huks/native_huks_api.h"
21#include "huks/native_huks_param.h"
22#include <string.h>
23OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params,
24                            uint32_t paramCount) {
25    OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
26    if (ret.errorCode != OH_HUKS_SUCCESS) {
27        return ret;
28    }
29    ret = OH_Huks_AddParams(*paramSet, params, paramCount);
30    if (ret.errorCode != OH_HUKS_SUCCESS) {
31        OH_Huks_FreeParamSet(paramSet);
32        return ret;
33    }
34    ret = OH_Huks_BuildParamSet(paramSet);
35    if (ret.errorCode != OH_HUKS_SUCCESS) {
36        OH_Huks_FreeParamSet(paramSet);
37        return ret;
38    }
39    return ret;
40}
41struct OH_Huks_Param g_testGenerateKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_ECC},
42                                                 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_AGREE},
43                                                 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_ECC_KEY_SIZE_256},
44                                                 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}};
45static napi_value GenerateKey(napi_env env, napi_callback_info info) {
46    const char *alias = "test_generate";
47    struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias};
48    struct OH_Huks_ParamSet *testGenerateKeyParamSet = nullptr;
49    struct OH_Huks_Result ohResult;
50    do {
51        ohResult = InitParamSet(&testGenerateKeyParamSet, g_testGenerateKeyParam,
52                                sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
53        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
54            break;
55        }
56        ohResult = OH_Huks_GenerateKeyItem(&aliasBlob, testGenerateKeyParamSet, nullptr);
57    } while (0);
58    OH_Huks_FreeParamSet(&testGenerateKeyParamSet);
59    napi_value ret;
60    napi_create_int32(env, ohResult.errorCode, &ret);
61    return ret;
62}
63static napi_value ImportKey(napi_env env, napi_callback_info info) {
64    (void)GenerateKey(env, info);
65    const char *alias = "test_generate";
66    struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias};
67    uint8_t pubKey[OH_HUKS_ECC_KEY_SIZE_256] = {0};
68    struct OH_Huks_Blob publicKey = {OH_HUKS_ECC_KEY_SIZE_256, pubKey};
69    struct OH_Huks_ParamSet *testImportKeyParamSet = nullptr;
70    struct OH_Huks_Result ohResult;
71    do {
72        ohResult = InitParamSet(&testImportKeyParamSet, g_testGenerateKeyParam,
73                                sizeof(g_testGenerateKeyParam) / sizeof(OH_Huks_Param));
74        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
75            break;
76        }
77        ohResult = OH_Huks_ExportPublicKeyItem(&aliasBlob, testImportKeyParamSet, &publicKey);
78        if (ohResult.errorCode != OH_HUKS_SUCCESS) {
79            break;
80        }
81        /* 4. Import Key */
82        char newKey[] = "test_import";
83        struct OH_Huks_Blob newKeyAlias = {.size = (uint32_t)strlen(newKey), .data = (uint8_t *)newKey};
84        ohResult = OH_Huks_ImportKeyItem(&newKeyAlias, testImportKeyParamSet, &publicKey);
85    } while (0);
86    OH_Huks_FreeParamSet(&testImportKeyParamSet);
87    napi_value ret;
88    napi_create_int32(env, ohResult.errorCode, &ret);
89    return ret;
90}
91```
92