1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 // [Start import_keys_in_plaintext]
17 /* 以下以明文导入AES密钥为例 */
18 #include "huks/native_huks_api.h"
19 #include "huks/native_huks_param.h"
20 #include "napi/native_api.h"
21 #include <cstring>
22
23 #define OH_HUKS_AES_KEY_SIZE_32 32
24
InitParamSet(struct OH_Huks_ParamSet ** paramSet,const struct OH_Huks_Param * params,uint32_t paramCount)25 OH_Huks_Result InitParamSet(struct OH_Huks_ParamSet **paramSet, const struct OH_Huks_Param *params,
26 uint32_t paramCount)
27 {
28 OH_Huks_Result ret = OH_Huks_InitParamSet(paramSet);
29 if (ret.errorCode != OH_HUKS_SUCCESS) {
30 return ret;
31 }
32 ret = OH_Huks_AddParams(*paramSet, params, paramCount);
33 if (ret.errorCode != OH_HUKS_SUCCESS) {
34 OH_Huks_FreeParamSet(paramSet);
35 return ret;
36 }
37 ret = OH_Huks_BuildParamSet(paramSet);
38 if (ret.errorCode != OH_HUKS_SUCCESS) {
39 OH_Huks_FreeParamSet(paramSet);
40 return ret;
41 }
42 return ret;
43 }
44 struct OH_Huks_Param g_testImportKeyParam[] = {{.tag = OH_HUKS_TAG_ALGORITHM, .uint32Param = OH_HUKS_ALG_AES},
45 {.tag = OH_HUKS_TAG_PURPOSE, .uint32Param = OH_HUKS_KEY_PURPOSE_ENCRYPT},
46 {.tag = OH_HUKS_TAG_KEY_SIZE, .uint32Param = OH_HUKS_AES_KEY_SIZE_256},
47 {.tag = OH_HUKS_TAG_DIGEST, .uint32Param = OH_HUKS_DIGEST_NONE}};
48
ImportKey(napi_env env,napi_callback_info info)49 static napi_value ImportKey(napi_env env, napi_callback_info info)
50 {
51 const char *alias = "test_import";
52 struct OH_Huks_Blob aliasBlob = {.size = (uint32_t)strlen(alias), .data = (uint8_t *)alias};
53 /* DER格式的公钥,用于后续导入密钥 */
54 uint8_t pubKey[OH_HUKS_AES_KEY_SIZE_32] = {0xfb, 0x8b, 0x9f, 0x12, 0xa0, 0x83, 0x19, 0xbe, 0x6a, 0x6f, 0x63,
55 0x2a, 0x7c, 0x86, 0xba, 0xca, 0x64, 0x0b, 0x88, 0x96, 0xe2, 0xfa,
56 0x77, 0xbc, 0x71, 0xe3, 0x0f, 0x0f, 0x9e, 0x3c, 0xe5, 0xf9};
57 struct OH_Huks_Blob publicKey = {OH_HUKS_AES_KEY_SIZE_32, pubKey};
58 struct OH_Huks_ParamSet *testImportKeyParamSet = nullptr;
59 struct OH_Huks_Result ohResult;
60 do {
61 ohResult = InitParamSet(&testImportKeyParamSet, g_testImportKeyParam,
62 sizeof(g_testImportKeyParam) / sizeof(OH_Huks_Param));
63 if (ohResult.errorCode != OH_HUKS_SUCCESS) {
64 break;
65 }
66 if (ohResult.errorCode != OH_HUKS_SUCCESS) {
67 break;
68 }
69 /* 4. Import Key */
70 char newKey[] = "test_import";
71 struct OH_Huks_Blob newKeyAlias = {.size = (uint32_t)strlen(newKey), .data = (uint8_t *)newKey};
72 ohResult = OH_Huks_ImportKeyItem(&newKeyAlias, testImportKeyParamSet, &publicKey);
73 } while (0);
74 OH_Huks_FreeParamSet(&testImportKeyParamSet);
75 napi_value ret;
76 napi_create_int32(env, ohResult.errorCode, &ret);
77 return ret;
78 }
79
80 // [End import_keys_in_plaintext]
81 EXTERN_C_START
Init(napi_env env,napi_value exports)82 static napi_value Init(napi_env env, napi_value exports)
83 {
84 napi_property_descriptor desc[] = {
85 {"importKey", nullptr, ImportKey, nullptr, nullptr, nullptr, napi_default, nullptr}};
86 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
87 return exports;
88 }
89 EXTERN_C_END
90
91 static napi_module demoModule = {
92 .nm_version = 1,
93 .nm_flags = 0,
94 .nm_filename = nullptr,
95 .nm_register_func = Init,
96 .nm_modname = "entry",
97 .nm_priv = ((void *)0),
98 .reserved = {0},
99 };
100
RegisterEntryModule(void)101 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
102