• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-2023 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 #include "napi_asy_key_generator.h"
17 
18 #include "securec.h"
19 #include "log.h"
20 #include "memory.h"
21 
22 #include "napi_crypto_framework_defines.h"
23 #include "napi_utils.h"
24 #include "napi_key_pair.h"
25 #include "napi_pri_key.h"
26 #include "napi_pub_key.h"
27 
28 namespace OHOS {
29 namespace CryptoFramework {
30 struct GenKeyPairCtx {
31     napi_env env = nullptr;
32 
33     AsyncType asyncType = ASYNC_CALLBACK;
34     napi_ref callback = nullptr;
35     napi_deferred deferred = nullptr;
36     napi_value promise = nullptr;
37     napi_async_work asyncWork = nullptr;
38 
39     HcfAsyKeyGenerator *generator = nullptr;
40     HcfParamsSpec *params = nullptr;
41 
42     HcfResult errCode = HCF_SUCCESS;
43     const char *errMsg = nullptr;
44     HcfKeyPair *returnKeyPair = nullptr;
45 };
46 
47 struct ConvertKeyCtx {
48     napi_env env = nullptr;
49 
50     AsyncType asyncType = ASYNC_CALLBACK;
51     napi_ref callback = nullptr;
52     napi_deferred deferred = nullptr;
53     napi_value promise = nullptr;
54     napi_async_work asyncWork = nullptr;
55 
56     HcfAsyKeyGenerator *generator = nullptr;
57     HcfParamsSpec *params = nullptr;
58     HcfBlob *pubKey = nullptr;
59     HcfBlob *priKey = nullptr;
60 
61     HcfResult errCode = HCF_SUCCESS;
62     const char *errMsg = nullptr;
63     HcfKeyPair *returnKeyPair = nullptr;
64 };
65 
66 thread_local napi_ref NapiAsyKeyGenerator::classRef_ = nullptr;
67 
FreeGenKeyPairCtx(napi_env env,GenKeyPairCtx * ctx)68 static void FreeGenKeyPairCtx(napi_env env, GenKeyPairCtx *ctx)
69 {
70     if (ctx == nullptr) {
71         return;
72     }
73 
74     if (ctx->asyncWork != nullptr) {
75         napi_delete_async_work(env, ctx->asyncWork);
76         ctx->asyncWork = nullptr;
77     }
78 
79     if (ctx->callback != nullptr) {
80         napi_delete_reference(env, ctx->callback);
81         ctx->callback = nullptr;
82     }
83 
84     HcfFree(ctx);
85 }
86 
FreeConvertKeyCtx(napi_env env,ConvertKeyCtx * ctx)87 static void FreeConvertKeyCtx(napi_env env, ConvertKeyCtx *ctx)
88 {
89     if (ctx == nullptr) {
90         return;
91     }
92 
93     if (ctx->asyncWork != nullptr) {
94         napi_delete_async_work(env, ctx->asyncWork);
95         ctx->asyncWork = nullptr;
96     }
97 
98     if (ctx->callback != nullptr) {
99         napi_delete_reference(env, ctx->callback);
100         ctx->callback = nullptr;
101     }
102 
103     HcfBlobDataFree(ctx->pubKey);
104     HcfFree(ctx->pubKey);
105     HcfBlobDataFree(ctx->priKey);
106     HcfFree(ctx->priKey);
107     HcfFree(ctx);
108 }
109 
BuildGenKeyPairCtx(napi_env env,napi_callback_info info,GenKeyPairCtx * ctx)110 static bool BuildGenKeyPairCtx(napi_env env, napi_callback_info info, GenKeyPairCtx *ctx)
111 {
112     napi_value thisVar = nullptr;
113     size_t expectedArgc = PARAMS_NUM_ONE;
114     size_t argc = expectedArgc;
115     napi_value argv[PARAMS_NUM_ONE] = { nullptr };
116     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
117     if (argc != expectedArgc && argc != expectedArgc - 1) {
118         LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
119         return false;
120     }
121     ctx->asyncType = isCallback(env, argv[0], argc, expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
122 
123     NapiAsyKeyGenerator *napiGenerator;
124     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
125     if (status != napi_ok || napiGenerator == nullptr) {
126         LOGE("failed to unwrap napi asyKeyGenerator obj.");
127         return false;
128     }
129 
130     ctx->generator = napiGenerator->GetAsyKeyGenerator();
131     ctx->params = nullptr;
132 
133     if (ctx->asyncType == ASYNC_PROMISE) {
134         napi_create_promise(env, &ctx->deferred, &ctx->promise);
135         return true;
136     } else {
137         return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
138     }
139 }
140 
GetPkAndSkBlobFromNapiValueIfInput(napi_env env,napi_value pkValue,napi_value skValue,HcfBlob ** returnPubKey,HcfBlob ** returnPriKey)141 static bool GetPkAndSkBlobFromNapiValueIfInput(napi_env env, napi_value pkValue, napi_value skValue,
142     HcfBlob **returnPubKey, HcfBlob **returnPriKey)
143 {
144     napi_valuetype valueType;
145     napi_typeof(env, pkValue, &valueType);
146     HcfBlob *pubKey = nullptr;
147     if (valueType != napi_null) {
148         pubKey = GetBlobFromNapiDataBlob(env, pkValue);
149         if (pubKey == nullptr) {
150             LOGE("failed to get pubKey.");
151             return false;
152         }
153     }
154 
155     napi_typeof(env, skValue, &valueType);
156     HcfBlob *priKey = nullptr;
157     if (valueType != napi_null) {
158         priKey = GetBlobFromNapiDataBlob(env, skValue);
159         if (priKey == nullptr) {
160             // if the prikey get func fails, the return pointer will not take the ownership of pubkey and not free it.
161             HcfBlobDataFree(pubKey);
162             HcfFree(pubKey);
163             LOGE("failed to get priKey.");
164             return false;
165         }
166     }
167 
168     *returnPubKey = pubKey;
169     *returnPriKey = priKey;
170     return true;
171 }
172 
BuildConvertKeyCtx(napi_env env,napi_callback_info info,ConvertKeyCtx * ctx)173 static bool BuildConvertKeyCtx(napi_env env, napi_callback_info info, ConvertKeyCtx *ctx)
174 {
175     napi_value thisVar = nullptr;
176     size_t expectedArgc = PARAMS_NUM_THREE;
177     size_t argc = expectedArgc;
178     napi_value argv[PARAMS_NUM_THREE] = { nullptr };
179     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
180     if (argc != expectedArgc && argc != expectedArgc - 1) {
181         LOGE("wrong argument num. require %zu or %zu arguments. [Argc]: %zu!", expectedArgc - 1, expectedArgc, argc);
182         return false;
183     }
184     ctx->asyncType = isCallback(env, argv[expectedArgc - 1], argc, expectedArgc) ? ASYNC_CALLBACK : ASYNC_PROMISE;
185 
186     NapiAsyKeyGenerator *napiGenerator;
187     napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&napiGenerator));
188     if (status != napi_ok || napiGenerator == nullptr) {
189         LOGE("failed to unwrap napi asyKeyGenerator obj.");
190         return false;
191     }
192 
193     HcfBlob *pubKey = nullptr;
194     HcfBlob *priKey = nullptr;
195     if (!GetPkAndSkBlobFromNapiValueIfInput(env, argv[PARAM0], argv[PARAM1], &pubKey, &priKey)) {
196         return false;
197     }
198 
199     ctx->generator = napiGenerator->GetAsyKeyGenerator();
200     ctx->params = nullptr;
201     ctx->pubKey = pubKey;
202     ctx->priKey = priKey;
203 
204     if (ctx->asyncType == ASYNC_PROMISE) {
205         napi_create_promise(env, &ctx->deferred, &ctx->promise);
206         return true;
207     } else {
208         return GetCallbackFromJSParams(env, argv[expectedArgc - 1], &ctx->callback);
209     }
210 }
211 
ReturnGenKeyPairCallbackResult(napi_env env,GenKeyPairCtx * ctx,napi_value result)212 static void ReturnGenKeyPairCallbackResult(napi_env env, GenKeyPairCtx *ctx, napi_value result)
213 {
214     napi_value businessError = nullptr;
215     if (ctx->errCode != HCF_SUCCESS) {
216         businessError = GenerateBusinessError(env, ctx->errCode, ctx->errMsg);
217     }
218 
219     napi_value params[ARGS_SIZE_TWO] = { businessError, result };
220 
221     napi_value func = nullptr;
222     napi_get_reference_value(env, ctx->callback, &func);
223 
224     napi_value recv = nullptr;
225     napi_value callFuncRet = nullptr;
226     napi_get_undefined(env, &recv);
227     napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
228 }
229 
ReturnGenKeyPairPromiseResult(napi_env env,GenKeyPairCtx * ctx,napi_value result)230 static void ReturnGenKeyPairPromiseResult(napi_env env, GenKeyPairCtx *ctx, napi_value result)
231 {
232     if (ctx->errCode == HCF_SUCCESS) {
233         napi_resolve_deferred(env, ctx->deferred, result);
234     } else {
235         napi_reject_deferred(env, ctx->deferred,
236             GenerateBusinessError(env, ctx->errCode, ctx->errMsg));
237     }
238 }
239 
ReturnConvertKeyCallbackResult(napi_env env,ConvertKeyCtx * ctx,napi_value result)240 static void ReturnConvertKeyCallbackResult(napi_env env, ConvertKeyCtx *ctx, napi_value result)
241 {
242     napi_value businessError = nullptr;
243     if (ctx->errCode != HCF_SUCCESS) {
244         businessError = GenerateBusinessError(env, ctx->errCode, ctx->errMsg);
245     }
246 
247     napi_value params[ARGS_SIZE_TWO] = { businessError, result };
248 
249     napi_value func = nullptr;
250     napi_get_reference_value(env, ctx->callback, &func);
251 
252     napi_value recv = nullptr;
253     napi_value callFuncRet = nullptr;
254     napi_get_undefined(env, &recv);
255     napi_call_function(env, recv, func, ARGS_SIZE_TWO, params, &callFuncRet);
256 }
257 
ReturnConvertKeyPromiseResult(napi_env env,ConvertKeyCtx * ctx,napi_value result)258 static void ReturnConvertKeyPromiseResult(napi_env env, ConvertKeyCtx *ctx, napi_value result)
259 {
260     if (ctx->errCode == HCF_SUCCESS) {
261         napi_resolve_deferred(env, ctx->deferred, result);
262     } else {
263         napi_reject_deferred(env, ctx->deferred,
264             GenerateBusinessError(env, ctx->errCode, ctx->errMsg));
265     }
266 }
267 
GenKeyPairAsyncWorkProcess(napi_env env,void * data)268 static void GenKeyPairAsyncWorkProcess(napi_env env, void *data)
269 {
270     GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(data);
271 
272     ctx->errCode = ctx->generator->generateKeyPair(ctx->generator, ctx->params, &(ctx->returnKeyPair));
273     if (ctx->errCode != HCF_SUCCESS) {
274         LOGD("[error] generate key pair fail.");
275         ctx->errMsg = "generate key pair fail.";
276     }
277 }
278 
GenKeyPairAsyncWorkReturn(napi_env env,napi_status status,void * data)279 static void GenKeyPairAsyncWorkReturn(napi_env env, napi_status status, void *data)
280 {
281     GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(data);
282 
283     napi_value instance = nullptr;
284     if (ctx->errCode == HCF_SUCCESS) {
285         NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(ctx->returnKeyPair);
286         if (napiKeyPair == nullptr) {
287             napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!"));
288             LOGE("new napi key pair failed");
289             FreeGenKeyPairCtx(env, ctx);
290             return;
291         }
292         instance = napiKeyPair->ConvertToJsKeyPair(env);
293 
294         napi_status ret = napi_wrap(
295             env, instance, napiKeyPair,
296             [](napi_env env, void *data, void *hint) {
297                 NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
298                 delete keyPair;
299                 return;
300             }, nullptr, nullptr);
301         if (ret != napi_ok) {
302             LOGE("failed to wrap napiKeyPair obj!");
303             ctx->errCode = HCF_INVALID_PARAMS;
304             ctx->errMsg = "failed to wrap napiKeyPair obj!";
305             delete napiKeyPair;
306         }
307     }
308 
309     if (ctx->asyncType == ASYNC_CALLBACK) {
310         ReturnGenKeyPairCallbackResult(env, ctx, instance);
311     } else {
312         ReturnGenKeyPairPromiseResult(env, ctx, instance);
313     }
314     FreeGenKeyPairCtx(env, ctx);
315 }
316 
ConvertKeyAsyncWorkProcess(napi_env env,void * data)317 static void ConvertKeyAsyncWorkProcess(napi_env env, void *data)
318 {
319     ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(data);
320 
321     ctx->errCode = ctx->generator->convertKey(ctx->generator, ctx->params,
322         ctx->pubKey, ctx->priKey, &(ctx->returnKeyPair));
323     if (ctx->errCode != HCF_SUCCESS) {
324         LOGD("[error] convert key fail.");
325         ctx->errMsg = "convert key fail.";
326     }
327 }
328 
ConvertKeyAsyncWorkReturn(napi_env env,napi_status status,void * data)329 static void ConvertKeyAsyncWorkReturn(napi_env env, napi_status status, void *data)
330 {
331     ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(data);
332 
333     napi_value instance = nullptr;
334     if (ctx->errCode == HCF_SUCCESS) {
335         NapiKeyPair *napiKeyPair = new (std::nothrow) NapiKeyPair(ctx->returnKeyPair);
336         if (napiKeyPair == nullptr) {
337             napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi key pair failed!"));
338             LOGE("new napi key pair failed");
339             FreeConvertKeyCtx(env, ctx);
340             return;
341         }
342         instance = napiKeyPair->ConvertToJsKeyPair(env);
343 
344         napi_status ret = napi_wrap(
345             env, instance, napiKeyPair,
346             [](napi_env env, void *data, void *hint) {
347                 NapiKeyPair *keyPair = static_cast<NapiKeyPair *>(data);
348                 delete keyPair;
349                 return;
350             }, nullptr, nullptr);
351         if (ret != napi_ok) {
352             LOGE("failed to wrap napiKeyPair obj!");
353             ctx->errCode = HCF_INVALID_PARAMS;
354             ctx->errMsg = "failed to wrap napiKeyPair obj!";
355             delete napiKeyPair;
356         }
357     }
358 
359     if (ctx->asyncType == ASYNC_CALLBACK) {
360         ReturnConvertKeyCallbackResult(env, ctx, instance);
361     } else {
362         ReturnConvertKeyPromiseResult(env, ctx, instance);
363     }
364     FreeConvertKeyCtx(env, ctx);
365 }
366 
NewGenKeyPairAsyncWork(napi_env env,GenKeyPairCtx * ctx)367 static napi_value NewGenKeyPairAsyncWork(napi_env env, GenKeyPairCtx *ctx)
368 {
369     napi_value resourceName = nullptr;
370     napi_create_string_utf8(env, "generatorKeyPair", NAPI_AUTO_LENGTH, &resourceName);
371 
372     napi_create_async_work(
373         env, nullptr, resourceName,
374         [](napi_env env, void *data) {
375             GenKeyPairAsyncWorkProcess(env, data);
376             return;
377         },
378         [](napi_env env, napi_status status, void *data) {
379             GenKeyPairAsyncWorkReturn(env, status, data);
380             return;
381         },
382         static_cast<void *>(ctx),
383         &ctx->asyncWork);
384 
385     napi_queue_async_work(env, ctx->asyncWork);
386     if (ctx->asyncType == ASYNC_PROMISE) {
387         return ctx->promise;
388     } else {
389         return NapiGetNull(env);
390     }
391 }
392 
NewConvertKeyAsyncWork(napi_env env,ConvertKeyCtx * ctx)393 static napi_value NewConvertKeyAsyncWork(napi_env env, ConvertKeyCtx *ctx)
394 {
395     napi_value resourceName = nullptr;
396     napi_create_string_utf8(env, "convertKey", NAPI_AUTO_LENGTH, &resourceName);
397 
398     napi_create_async_work(
399         env, nullptr, resourceName,
400         [](napi_env env, void *data) {
401             ConvertKeyAsyncWorkProcess(env, data);
402             return;
403         },
404         [](napi_env env, napi_status status, void *data) {
405             ConvertKeyAsyncWorkReturn(env, status, data);
406             return;
407         },
408         static_cast<void *>(ctx),
409         &ctx->asyncWork);
410 
411     napi_queue_async_work(env, ctx->asyncWork);
412     if (ctx->asyncType == ASYNC_PROMISE) {
413         return ctx->promise;
414     } else {
415         return NapiGetNull(env);
416     }
417 }
418 
NapiAsyKeyGenerator(HcfAsyKeyGenerator * generator)419 NapiAsyKeyGenerator::NapiAsyKeyGenerator(HcfAsyKeyGenerator *generator)
420 {
421     this->generator_ = generator;
422 }
423 
~NapiAsyKeyGenerator()424 NapiAsyKeyGenerator::~NapiAsyKeyGenerator()
425 {
426     HcfObjDestroy(this->generator_);
427 }
428 
GetAsyKeyGenerator()429 HcfAsyKeyGenerator *NapiAsyKeyGenerator::GetAsyKeyGenerator()
430 {
431     return this->generator_;
432 }
433 
JsGenerateKeyPair(napi_env env,napi_callback_info info)434 napi_value NapiAsyKeyGenerator::JsGenerateKeyPair(napi_env env, napi_callback_info info)
435 {
436     GenKeyPairCtx *ctx = static_cast<GenKeyPairCtx *>(HcfMalloc(sizeof(GenKeyPairCtx), 0));
437     if (ctx == nullptr) {
438         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc ctx fail."));
439         LOGE("create context fail.");
440         return nullptr;
441     }
442 
443     if (!BuildGenKeyPairCtx(env, info, ctx)) {
444         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail."));
445         LOGE("build context fail.");
446         FreeGenKeyPairCtx(env, ctx);
447         return nullptr;
448     }
449 
450     return NewGenKeyPairAsyncWork(env, ctx);
451 }
452 
JsConvertKey(napi_env env,napi_callback_info info)453 napi_value NapiAsyKeyGenerator::JsConvertKey(napi_env env, napi_callback_info info)
454 {
455     ConvertKeyCtx *ctx = static_cast<ConvertKeyCtx *>(HcfMalloc(sizeof(ConvertKeyCtx), 0));
456     if (ctx == nullptr) {
457         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "create context fail!"));
458         LOGE("create context fail.");
459         return nullptr;
460     }
461 
462     if (!BuildConvertKeyCtx(env, info, ctx)) {
463         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build context fail."));
464         LOGE("build context fail.");
465         FreeConvertKeyCtx(env, ctx);
466         return nullptr;
467     }
468 
469     return NewConvertKeyAsyncWork(env, ctx);
470 }
471 
AsyKeyGeneratorConstructor(napi_env env,napi_callback_info info)472 napi_value NapiAsyKeyGenerator::AsyKeyGeneratorConstructor(napi_env env, napi_callback_info info)
473 {
474     napi_value thisVar = nullptr;
475     napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
476     return thisVar;
477 }
478 
NapiWrapAsyKeyGen(napi_env env,napi_value instance,NapiAsyKeyGenerator * napiAsyKeyGenerator)479 static napi_value NapiWrapAsyKeyGen(napi_env env, napi_value instance, NapiAsyKeyGenerator *napiAsyKeyGenerator)
480 {
481     napi_status status = napi_wrap(
482         env, instance, napiAsyKeyGenerator,
483         [](napi_env env, void *data, void *hint) {
484             NapiAsyKeyGenerator *napiAsyKeyGenerator = static_cast<NapiAsyKeyGenerator *>(data);
485             delete napiAsyKeyGenerator;
486             return;
487         }, nullptr, nullptr);
488     if (status != napi_ok) {
489         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to wrap napiAsyKeyGenerator obj!"));
490         delete napiAsyKeyGenerator;
491         napiAsyKeyGenerator = nullptr;
492         LOGE("failed to wrap napiAsyKeyGenerator obj!");
493         return nullptr;
494     }
495     return instance;
496 }
497 
CreateJsAsyKeyGenerator(napi_env env,napi_callback_info info)498 napi_value NapiAsyKeyGenerator::CreateJsAsyKeyGenerator(napi_env env, napi_callback_info info)
499 {
500     LOGD("Enter CreateJsAsyKeyGenerator...");
501     size_t expectedArgc = PARAMS_NUM_ONE;
502     size_t argc = expectedArgc;
503     napi_value argv[PARAMS_NUM_ONE] = { nullptr };
504     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
505     if (argc != expectedArgc) {
506         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
507         LOGE("The input args num is invalid.");
508         return NapiGetNull(env);
509     }
510 
511     napi_value instance;
512     napi_value constructor = nullptr;
513     napi_get_reference_value(env, classRef_, &constructor);
514     napi_new_instance(env, constructor, argc, argv, &instance);
515 
516     std::string algName;
517     if (!GetStringFromJSParams(env, argv[0], algName)) {
518         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get algoName."));
519         LOGE("failed to get algoName.");
520         return NapiGetNull(env);
521     }
522 
523     HcfAsyKeyGenerator *generator = nullptr;
524     HcfResult res = HcfAsyKeyGeneratorCreate(algName.c_str(), &generator);
525     if (res != HCF_SUCCESS) {
526         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create c generator fail."));
527         LOGE("create c generator fail.");
528         return NapiGetNull(env);
529     }
530 
531     NapiAsyKeyGenerator *napiAsyKeyGenerator = new (std::nothrow) NapiAsyKeyGenerator(generator);
532     if (napiAsyKeyGenerator == nullptr) {
533         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "new napi asy key napi generator failed!"));
534         LOGE("new napi asy key napi generator failed");
535         HcfObjDestroy(generator);
536         return NapiGetNull(env);
537     }
538 
539     napi_value napiAlgName = nullptr;
540     napi_create_string_utf8(env, algName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName);
541     napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName);
542 
543     return NapiWrapAsyKeyGen(env, instance, napiAsyKeyGenerator);
544 }
545 
DefineAsyKeyGeneratorJSClass(napi_env env,napi_value exports)546 void NapiAsyKeyGenerator::DefineAsyKeyGeneratorJSClass(napi_env env, napi_value exports)
547 {
548     napi_property_descriptor desc[] = {
549         DECLARE_NAPI_FUNCTION("createAsyKeyGenerator", NapiAsyKeyGenerator::CreateJsAsyKeyGenerator),
550     };
551     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
552 
553     napi_property_descriptor classDesc[] = {
554         DECLARE_NAPI_FUNCTION("generateKeyPair", NapiAsyKeyGenerator::JsGenerateKeyPair),
555         DECLARE_NAPI_FUNCTION("convertKey", NapiAsyKeyGenerator::JsConvertKey),
556     };
557     napi_value constructor = nullptr;
558     napi_define_class(env, "AsyKeyGenerator", NAPI_AUTO_LENGTH, NapiAsyKeyGenerator::AsyKeyGeneratorConstructor,
559         nullptr, sizeof(classDesc) / sizeof(classDesc[0]), classDesc, &constructor);
560     napi_create_reference(env, constructor, 1, &classRef_);
561 }
562 } // CryptoFramework
563 } // OHOS
564