• 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_utils.h"
17 #include "params_parser.h"
18 #include "log.h"
19 #include "memory.h"
20 #include "securec.h"
21 #include "napi_crypto_framework_defines.h"
22 #include "detailed_iv_params.h"
23 #include "detailed_gcm_params.h"
24 #include "detailed_ccm_params.h"
25 #include "detailed_dsa_key_params.h"
26 #include "detailed_ecc_key_params.h"
27 #include "detailed_rsa_key_params.h"
28 #include "detailed_alg_25519_key_params.h"
29 #include "detailed_dh_key_params.h"
30 #include "utils.h"
31 
32 namespace OHOS {
33 namespace CryptoFramework {
34 using namespace std;
35 
36 struct AsyKeySpecItemRelationT {
37     AsyKeySpecItem item;
38     int32_t itemType;
39 };
40 using AsyKeySpecItemRelation = AsyKeySpecItemRelationT;
41 
42 static const AsyKeySpecItemRelation ASY_KEY_SPEC_RELATION_SET[] = {
43     { DSA_P_BN, SPEC_ITEM_TYPE_BIG_INT },
44     { DSA_Q_BN, SPEC_ITEM_TYPE_BIG_INT },
45     { DSA_G_BN, SPEC_ITEM_TYPE_BIG_INT },
46     { DSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
47     { DSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
48 
49     { ECC_FP_P_BN, SPEC_ITEM_TYPE_BIG_INT },
50     { ECC_A_BN, SPEC_ITEM_TYPE_BIG_INT },
51     { ECC_B_BN, SPEC_ITEM_TYPE_BIG_INT },
52     { ECC_G_X_BN, SPEC_ITEM_TYPE_BIG_INT },
53     { ECC_G_Y_BN, SPEC_ITEM_TYPE_BIG_INT },
54     { ECC_N_BN, SPEC_ITEM_TYPE_BIG_INT },
55     { ECC_H_INT, SPEC_ITEM_TYPE_NUM },  // warning: ECC_H_NUM in JS
56     { ECC_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
57     { ECC_PK_X_BN, SPEC_ITEM_TYPE_BIG_INT },
58     { ECC_PK_Y_BN, SPEC_ITEM_TYPE_BIG_INT },
59     { ECC_FIELD_TYPE_STR, SPEC_ITEM_TYPE_STR },
60     { ECC_FIELD_SIZE_INT, SPEC_ITEM_TYPE_NUM },  // warning: ECC_FIELD_SIZE_NUM in JS
61     { ECC_CURVE_NAME_STR, SPEC_ITEM_TYPE_STR },
62 
63     { RSA_N_BN, SPEC_ITEM_TYPE_BIG_INT },
64     { RSA_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
65     { RSA_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
66     { DH_P_BN, SPEC_ITEM_TYPE_BIG_INT },
67     { DH_G_BN, SPEC_ITEM_TYPE_BIG_INT },
68     { DH_L_NUM, SPEC_ITEM_TYPE_NUM },
69     { DH_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
70     { DH_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
71     { ED25519_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
72     { ED25519_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
73     { X25519_SK_BN, SPEC_ITEM_TYPE_BIG_INT },
74     { X25519_PK_BN, SPEC_ITEM_TYPE_BIG_INT },
75 };
76 
GetAsyKeySpecType(AsyKeySpecItem targetItemType)77 int32_t GetAsyKeySpecType(AsyKeySpecItem targetItemType)
78 {
79     for (uint32_t i = 0; i < sizeof(ASY_KEY_SPEC_RELATION_SET) / sizeof(AsyKeySpecItemRelation); i++) {
80         if (ASY_KEY_SPEC_RELATION_SET[i].item == targetItemType) {
81             return ASY_KEY_SPEC_RELATION_SET[i].itemType;
82         }
83     }
84     LOGE("AsyKeySpecItem not support! ItemType: %d", targetItemType);
85     return -1;
86 }
87 
GetSignSpecType(SignSpecItem targetItemType)88 int32_t GetSignSpecType(SignSpecItem targetItemType)
89 {
90     if (targetItemType == PSS_MD_NAME_STR || targetItemType == PSS_MGF_NAME_STR ||
91         targetItemType == PSS_MGF1_MD_STR) {
92         return SPEC_ITEM_TYPE_STR;
93     }
94     if (targetItemType == SM2_USER_ID_UINT8ARR) {
95         return SPEC_ITEM_TYPE_UINT8ARR;
96     }
97     if (targetItemType == PSS_SALT_LEN_INT || targetItemType == PSS_TRAILER_FIELD_INT) {
98         return SPEC_ITEM_TYPE_NUM;
99     }
100     LOGE("SignSpecItem not support! ItemType: %d", targetItemType);
101     return -1;
102 }
103 
GetCipherSpecType(CipherSpecItem targetItemType)104 int32_t GetCipherSpecType(CipherSpecItem targetItemType)
105 {
106     if (targetItemType == OAEP_MD_NAME_STR || targetItemType == OAEP_MGF_NAME_STR ||
107         targetItemType == OAEP_MGF1_MD_STR || targetItemType == SM2_MD_NAME_STR) {
108         return SPEC_ITEM_TYPE_STR;
109     }
110     if (targetItemType == OAEP_MGF1_PSRC_UINT8ARR) {
111         return SPEC_ITEM_TYPE_UINT8ARR;
112     }
113     LOGE("CipherSpecItem not support! ItemType: %d", targetItemType);
114     return -1;
115 }
116 
NapiGetNull(napi_env env)117 napi_value NapiGetNull(napi_env env)
118 {
119     napi_value result = nullptr;
120     napi_get_null(env, &result);
121     return result;
122 }
123 
GetUint8ArrFromNapiDataBlob(napi_env env,napi_value arg)124 static napi_value GetUint8ArrFromNapiDataBlob(napi_env env, napi_value arg)
125 {
126     if ((env == nullptr) || (arg == nullptr)) {
127         LOGE("Invalid params!");
128         return nullptr;
129     }
130     napi_value data = nullptr;
131     napi_valuetype valueType = napi_undefined;
132     napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_DATA.c_str(), &data);
133     napi_typeof(env, data, &valueType);
134     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
135         LOGE("failed to get valid data property!");
136         return nullptr;
137     }
138     return data;
139 }
140 
GetBlobFromNapiUint8Arr(napi_env env,napi_value data)141 HcfBlob *GetBlobFromNapiUint8Arr(napi_env env, napi_value data)
142 {
143     size_t length = 0;
144     size_t offset = 0;
145     void *rawData = nullptr;
146     napi_value arrayBuffer = nullptr;
147     napi_typedarray_type arrayType;
148     // Warning: Do not release the rawData returned by this interface because the rawData is managed by VM.
149     napi_status status = napi_get_typedarray_info(env, data, &arrayType, &length,
150         reinterpret_cast<void **>(&rawData), &arrayBuffer, &offset);
151     if ((status != napi_ok)) {
152         LOGE("failed to get valid rawData.");
153         return nullptr;
154     }
155     if (arrayType != napi_uint8_array) {
156         LOGE("input data is not uint8 array.");
157         return nullptr;
158     }
159 
160     HcfBlob *newBlob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
161     if (newBlob == nullptr) {
162         LOGE("Failed to allocate newBlob memory!");
163         return nullptr;
164     }
165 
166     // input empty uint8Arr, ex: new Uint8Arr(), the length is 0 and rawData is nullptr;
167     if ((length == 0) || (rawData == nullptr)) {
168         newBlob->len = 0;
169         newBlob->data = nullptr;
170         LOGD("napi Uint8Arr is null");
171         return newBlob;
172     }
173     newBlob->len = length;
174     newBlob->data = static_cast<uint8_t *>(HcfMalloc(length, 0));
175     if (newBlob->data == nullptr) {
176         LOGE("malloc blob data failed!");
177         HcfFree(newBlob);
178         return nullptr;
179     }
180     (void)memcpy_s(newBlob->data, length, rawData, length);
181     return newBlob;
182 }
183 
GetBlobFromNapiDataBlob(napi_env env,napi_value arg)184 HcfBlob *GetBlobFromNapiDataBlob(napi_env env, napi_value arg)
185 {
186     napi_value data = GetUint8ArrFromNapiDataBlob(env, arg);
187     if (data == nullptr) {
188         LOGE("failed to get data in DataBlob");
189         return nullptr;
190     }
191     return GetBlobFromNapiUint8Arr(env, data);
192 }
193 
GetAadFromParamsSpec(napi_env env,napi_value arg)194 static HcfBlob *GetAadFromParamsSpec(napi_env env, napi_value arg)
195 {
196     napi_value data = nullptr;
197     HcfBlob *blob = nullptr;
198     napi_valuetype valueType = napi_undefined;
199 
200     napi_status status = napi_get_named_property(env, arg, AAD_PARAMS.c_str(), &data);
201     napi_typeof(env, data, &valueType);
202     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
203         LOGE("failed to get valid param property!");
204         return nullptr;
205     }
206     if (valueType == napi_null) {
207         blob = reinterpret_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
208         if (blob == nullptr) {
209             LOGE("Failed to allocate newBlob memory!");
210             return nullptr;
211         }
212         blob->data = nullptr;
213         blob->len = 0;
214         LOGD("Input GCM Aad is Null");
215         return blob;
216     }
217     blob = GetBlobFromNapiDataBlob(env, data);
218     if (blob == nullptr) {
219         LOGE("GetBlobFromNapiDataBlob failed!");
220         return nullptr;
221     }
222     return blob;
223 }
224 
GetBigIntFromNapiValue(napi_env env,napi_value arg,HcfBigInteger * bigInt)225 bool GetBigIntFromNapiValue(napi_env env, napi_value arg, HcfBigInteger *bigInt)
226 {
227     if ((env == nullptr) || (arg == nullptr)) {
228         LOGE("Invalid params!");
229         return false;
230     }
231 
232     int signBit;
233     size_t wordCount;
234 
235     napi_get_value_bigint_words(env, arg, nullptr, &wordCount, nullptr);
236     if ((wordCount == 0) || (wordCount > (INT_MAX / sizeof(uint64_t)))) {
237         LOGE("Get big int failed.");
238         return false;
239     }
240     int length = wordCount * sizeof(uint64_t);
241     uint8_t *retArr = reinterpret_cast<uint8_t *>(HcfMalloc(length, 0));
242     if (retArr == nullptr) {
243         LOGE("malloc blob data failed!");
244         return false;
245     }
246     if (napi_get_value_bigint_words(env, arg, &signBit, &wordCount, reinterpret_cast<uint64_t *>(retArr)) != napi_ok) {
247         HcfFree(retArr);
248         LOGE("failed to get valid rawData.");
249         return false;
250     }
251     if (signBit != 0) {
252         HcfFree(retArr);
253         LOGE("failed to get gegative rawData.");
254         return false;
255     }
256     bigInt->data = retArr;
257     bigInt->len = length;
258     return true;
259 }
260 
GetPointFromNapiValue(napi_env env,napi_value arg,HcfPoint * point)261 static bool GetPointFromNapiValue(napi_env env, napi_value arg, HcfPoint *point)
262 {
263     if ((env == nullptr) || (arg == nullptr)) {
264         LOGE("Invalid params!");
265         return false;
266     }
267     napi_value dataX = nullptr;
268     napi_value dataY = nullptr;
269     napi_valuetype valueType = napi_undefined;
270     napi_status status = napi_get_named_property(env, arg, "x", &dataX);
271     napi_typeof(env, dataX, &valueType);
272     if ((status != napi_ok) || (dataX == nullptr) || (valueType == napi_undefined)) {
273         LOGE("failed to get valid algo name!");
274         return false;
275     }
276     status = napi_get_named_property(env, arg, "y", &dataY);
277     napi_typeof(env, dataY, &valueType);
278     if ((status != napi_ok) || (dataY == nullptr) || (valueType == napi_undefined)) {
279         LOGE("failed to get valid algo name!");
280         return false;
281     }
282 
283     bool ret = GetBigIntFromNapiValue(env, dataX, &point->x);
284     if (!ret) {
285         LOGE("get point x failed!");
286         return false;
287     }
288     ret = GetBigIntFromNapiValue(env, dataY, &point->y);
289     if (!ret) {
290         LOGE("get point y failed!");
291         HcfFree((point->x).data);
292         (point->x).data = nullptr;
293         return false;
294     }
295     return true;
296 }
297 
GetIvParamsSpecType()298 static const char *GetIvParamsSpecType()
299 {
300     return IV_PARAMS_SPEC.c_str();
301 }
302 
GetGcmParamsSpecType()303 static const char *GetGcmParamsSpecType()
304 {
305     return GCM_PARAMS_SPEC.c_str();
306 }
307 
GetCcmParamsSpecType()308 static const char *GetCcmParamsSpecType()
309 {
310     return CCM_PARAMS_SPEC.c_str();
311 }
312 
GetBlobFromParamsSpec(napi_env env,napi_value arg,const string & type)313 static HcfBlob *GetBlobFromParamsSpec(napi_env env, napi_value arg, const string &type)
314 {
315     napi_value data = nullptr;
316     HcfBlob *blob = nullptr;
317     napi_valuetype valueType = napi_undefined;
318 
319     napi_status status = napi_get_named_property(env, arg, type.c_str(), &data);
320     napi_typeof(env, data, &valueType);
321     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
322         LOGE("failed to get valid param property!");
323         return nullptr;
324     }
325     blob = GetBlobFromNapiDataBlob(env, data);
326     if (blob == nullptr) {
327         LOGE("GetBlobFromNapiDataBlob failed!");
328         return nullptr;
329     }
330     return blob;
331 }
332 
GetIvParamsSpec(napi_env env,napi_value arg,HcfParamsSpec ** paramsSpec)333 static bool GetIvParamsSpec(napi_env env, napi_value arg, HcfParamsSpec **paramsSpec)
334 {
335     HcfIvParamsSpec *ivParamsSpec = reinterpret_cast<HcfIvParamsSpec *>(HcfMalloc(sizeof(HcfIvParamsSpec), 0));
336     if (ivParamsSpec == nullptr) {
337         LOGE("ivParamsSpec malloc failed!");
338         return false;
339     }
340 
341     HcfBlob *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
342     if (iv == nullptr) {
343         LOGE("GetBlobFromNapiDataBlob failed!");
344         HcfFree(ivParamsSpec);
345         return false;
346     }
347     ivParamsSpec->base.getType = GetIvParamsSpecType;
348     ivParamsSpec->iv = *iv;
349     *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ivParamsSpec);
350     HcfFree(iv);
351     return true;
352 }
353 
GetIvAndAadBlob(napi_env env,napi_value arg,HcfBlob ** iv,HcfBlob ** aad)354 static bool GetIvAndAadBlob(napi_env env, napi_value arg, HcfBlob **iv, HcfBlob **aad)
355 {
356     *iv = GetBlobFromParamsSpec(env, arg, IV_PARAMS);
357     if (*iv == nullptr) {
358         LOGE("get iv failed!");
359         return false;
360     }
361 
362     *aad = GetAadFromParamsSpec(env, arg);
363     // error case free is in get paramspec func.
364     if (*aad == nullptr) {
365         LOGE("get aad failed!");
366         return false;
367     }
368     return true;
369 }
370 
GetGcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)371 static bool GetGcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
372 {
373     HcfBlob *iv = nullptr;
374     HcfBlob *aad = nullptr;
375     HcfBlob *tag = nullptr;
376     HcfBlob authTag = {};
377     bool ret = false;
378 
379     HcfGcmParamsSpec *gcmParamsSpec = reinterpret_cast<HcfGcmParamsSpec *>(HcfMalloc(sizeof(HcfGcmParamsSpec), 0));
380     if (gcmParamsSpec == nullptr) {
381         LOGE("gcmParamsSpec malloc failed!");
382         return false;
383     }
384 
385     ret = GetIvAndAadBlob(env, arg, &iv, &aad);
386     if (!ret) {
387         LOGE("GetIvAndAadBlob failed!");
388         goto clearup;
389     }
390 
391     if (opMode == DECRYPT_MODE) {
392         tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
393         if (tag == nullptr) {
394             LOGE("get tag failed!");
395             goto clearup;
396         }
397     } else if (opMode == ENCRYPT_MODE) {
398         authTag.data = static_cast<uint8_t *>(HcfMalloc(GCM_AUTH_TAG_LEN, 0));
399         if (authTag.data == nullptr) {
400             LOGE("get tag failed!");
401             goto clearup;
402         }
403         authTag.len = GCM_AUTH_TAG_LEN;
404     } else {
405         goto clearup;
406     }
407 
408     gcmParamsSpec->base.getType = GetGcmParamsSpecType;
409     gcmParamsSpec->iv = *iv;
410     gcmParamsSpec->aad = *aad;
411     gcmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
412     *paramsSpec = reinterpret_cast<HcfParamsSpec *>(gcmParamsSpec);
413     ret = true;
414 clearup:
415    if (!ret) {
416         HcfBlobDataFree(iv);
417         HcfBlobDataFree(aad);
418         HcfBlobDataFree(tag);
419         HcfFree(gcmParamsSpec);
420     }
421     HcfFree(iv);
422     HcfFree(aad);
423     HcfFree(tag);
424     return ret;
425 }
426 
GetCcmParamsSpec(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)427 static bool GetCcmParamsSpec(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
428 {
429     HcfBlob *iv = nullptr;
430     HcfBlob *aad = nullptr;
431     HcfBlob *tag = nullptr;
432     HcfBlob authTag = {};
433     bool ret = false;
434 
435     HcfCcmParamsSpec *ccmParamsSpec = reinterpret_cast<HcfCcmParamsSpec *>(HcfMalloc(sizeof(HcfCcmParamsSpec), 0));
436     if (ccmParamsSpec == nullptr) {
437         LOGE("ccmParamsSpec malloc failed!");
438         return ret;
439     }
440     ret = GetIvAndAadBlob(env, arg, &iv, &aad);
441     if (!ret) {
442         LOGE("GetIvAndAadBlob failed!");
443         goto clearup;
444     }
445 
446     if (opMode == DECRYPT_MODE) {
447         tag = GetBlobFromParamsSpec(env, arg, AUTHTAG_PARAMS);
448         if (tag == nullptr) {
449             LOGE("get tag failed!");
450             goto clearup;
451         }
452     } else if (opMode == ENCRYPT_MODE) {
453         authTag.data = static_cast<uint8_t *>(HcfMalloc(CCM_AUTH_TAG_LEN, 0));
454         if (authTag.data == nullptr) {
455             LOGE("get tag failed!");
456             goto clearup;
457         }
458         authTag.len = CCM_AUTH_TAG_LEN;
459     } else {
460         goto clearup;
461     }
462     ccmParamsSpec->base.getType = GetCcmParamsSpecType;
463     ccmParamsSpec->iv = *iv;
464     ccmParamsSpec->aad = *aad;
465     ccmParamsSpec->tag = opMode == DECRYPT_MODE ? *tag : authTag;
466     *paramsSpec = reinterpret_cast<HcfParamsSpec *>(ccmParamsSpec);
467     ret = true;
468 clearup:
469     if (!ret) {
470         HcfBlobDataFree(iv);
471         HcfBlobDataFree(aad);
472         HcfBlobDataFree(tag);
473         HcfFree(ccmParamsSpec);
474     }
475     HcfFree(iv);
476     HcfFree(aad);
477     HcfFree(tag);
478     return ret;
479 }
480 
GetParamsSpecFromNapiValue(napi_env env,napi_value arg,HcfCryptoMode opMode,HcfParamsSpec ** paramsSpec)481 bool GetParamsSpecFromNapiValue(napi_env env, napi_value arg, HcfCryptoMode opMode, HcfParamsSpec **paramsSpec)
482 {
483     napi_value data = nullptr;
484     napi_valuetype valueType = napi_undefined;
485     if ((env == nullptr) || (arg == nullptr) || (paramsSpec == nullptr)) {
486         LOGE("Invalid params!");
487         return false;
488     }
489 
490     napi_status status = napi_get_named_property(env, arg, ALGO_PARAMS.c_str(), &data);
491     napi_typeof(env, data, &valueType);
492     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
493         status = napi_get_named_property(env, arg, ALGO_PARAMS_OLD.c_str(), &data);
494         napi_typeof(env, data, &valueType);
495         if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
496             LOGE("failed to get valid algo name!");
497             return false;
498         }
499     }
500     string algoName;
501     if (!GetStringFromJSParams(env, data, algoName)) {
502         LOGE("GetStringFromJSParams failed!");
503         return false;
504     }
505     if (algoName.compare(IV_PARAMS_SPEC) == 0) {
506         return GetIvParamsSpec(env, arg, paramsSpec);
507     } else if (algoName.compare(GCM_PARAMS_SPEC) == 0) {
508         return GetGcmParamsSpec(env, arg, opMode, paramsSpec);
509     } else if (algoName.compare(CCM_PARAMS_SPEC) == 0) {
510         return GetCcmParamsSpec(env, arg, opMode, paramsSpec);
511     } else {
512         return false;
513     }
514 }
515 
GetDetailAsyKeySpecValue(napi_env env,napi_value arg,string argName)516 static napi_value GetDetailAsyKeySpecValue(napi_env env, napi_value arg, string argName)
517 {
518     napi_value data = nullptr;
519     napi_valuetype valueType = napi_undefined;
520     napi_status status = napi_get_named_property(env, arg, argName.c_str(), &data);
521     napi_typeof(env, data, &valueType);
522     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
523         LOGE("failed to get valid algo name!");
524         return nullptr;
525     }
526     return data;
527 }
528 
GetCommSpecNapiValue(napi_env env,napi_value arg)529 static napi_value GetCommSpecNapiValue(napi_env env, napi_value arg)
530 {
531     napi_value data = nullptr;
532     napi_valuetype valueType = napi_undefined;
533 
534     napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_COMM_PARAMS.c_str(), &data);
535     napi_typeof(env, data, &valueType);
536 
537     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
538         LOGE("failed to get valid algo name!");
539         return nullptr;
540     }
541     return data;
542 }
543 
InitDsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfDsaCommParamsSpec * spec)544 static bool InitDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfDsaCommParamsSpec *spec)
545 {
546     size_t algNameLen = DSA_ASY_KEY_SPEC.length();
547     spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
548     if (spec->base.algName == nullptr) {
549         LOGE("malloc DSA algName failed!");
550         return false;
551     }
552     (void)memcpy_s(spec->base.algName, algNameLen+ 1, DSA_ASY_KEY_SPEC.c_str(), algNameLen);
553     spec->base.specType = HCF_COMMON_PARAMS_SPEC;
554 
555     napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
556     napi_value q = GetDetailAsyKeySpecValue(env, arg, "q");
557     napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
558     bool ret = GetBigIntFromNapiValue(env, p, &spec->p);
559     if (!ret) {
560         HcfFree(spec->base.algName);
561         spec->base.algName = nullptr;
562         return false;
563     }
564     ret = GetBigIntFromNapiValue(env, q, &spec->q);
565     if (!ret) {
566         FreeDsaCommParamsSpec(spec);
567         return false;
568     }
569     ret = GetBigIntFromNapiValue(env, g, &spec->g);
570     if (!ret) {
571         FreeDsaCommParamsSpec(spec);
572         return false;
573     }
574     return true;
575 }
576 
GetDsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)577 static bool GetDsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
578 {
579     HcfDsaCommParamsSpec *spec = reinterpret_cast<HcfDsaCommParamsSpec *>(HcfMalloc(sizeof(HcfDsaCommParamsSpec), 0));
580     if (spec == nullptr) {
581         LOGE("malloc failed!");
582         return false;
583     }
584     if (!InitDsaCommonAsyKeySpec(env, arg, spec)) {
585         LOGE("InitDsaCommonAsyKeySpec failed!");
586         HcfFree(spec);
587         return false;
588     }
589     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
590     return true;
591 }
592 
GetDsaPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)593 static bool GetDsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
594 {
595     HcfDsaPubKeyParamsSpec *spec = reinterpret_cast<HcfDsaPubKeyParamsSpec *>(
596         HcfMalloc(sizeof(HcfDsaPubKeyParamsSpec), 0));
597     if (spec == nullptr) {
598         LOGE("malloc failed!");
599         return false;
600     }
601 
602     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
603     if (commSpecValue == nullptr) {
604         LOGE("Get comm spec napi value failed.");
605         HcfFree(spec);
606         return false;
607     }
608     if (!InitDsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDsaCommParamsSpec *>(spec))) {
609         LOGE("InitDsaCommonAsyKeySpec failed.");
610         HcfFree(spec);
611         return false;
612     }
613     spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
614 
615     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
616     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
617     if (!ret) {
618         DestroyDsaPubKeySpec(spec);
619         return false;
620     }
621     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
622     return true;
623 }
624 
GetDsaKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)625 static bool GetDsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
626 {
627     HcfDsaKeyPairParamsSpec *spec = reinterpret_cast<HcfDsaKeyPairParamsSpec *>(
628         HcfMalloc(sizeof(HcfDsaKeyPairParamsSpec), 0));
629     if (spec == nullptr) {
630         LOGE("malloc failed!");
631         return false;
632     }
633 
634     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
635     if (commSpecValue == nullptr) {
636         LOGE("Get comm spec napi value failed.");
637         HcfFree(spec);
638         return false;
639     }
640     if (!InitDsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDsaCommParamsSpec *>(spec))) {
641         LOGE("InitDsaCommonAsyKeySpec failed!");
642         HcfFree(spec);
643         return false;
644     }
645     spec->base.base.specType = HCF_KEY_PAIR_SPEC;
646 
647     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
648     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
649     if (!ret) {
650         FreeDsaCommParamsSpec(reinterpret_cast<HcfDsaCommParamsSpec *>(spec));
651         HcfFree(spec);
652         return false;
653     }
654     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
655     ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
656     if (!ret) {
657         FreeDsaCommParamsSpec(reinterpret_cast<HcfDsaCommParamsSpec *>(spec));
658         HcfFree(spec->pk.data);
659         HcfFree(spec);
660         return false;
661     }
662     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
663     return true;
664 }
665 
GetDsaAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)666 static bool GetDsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
667 {
668     napi_value data = nullptr;
669     napi_valuetype valueType = napi_undefined;
670 
671     napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
672     napi_typeof(env, data, &valueType);
673     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
674         LOGE("failed to get valid algo name!");
675         return false;
676     }
677     HcfAsyKeySpecType asyKeySpecType;
678     status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
679     if (status != napi_ok) {
680         LOGE("failed to get valid asyKeySpecType!");
681         return false;
682     }
683     if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
684         return GetDsaCommonAsyKeySpec(env, arg, asyKeySpec);
685     } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
686         return GetDsaPubKeySpec(env, arg, asyKeySpec);
687     } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
688         return GetDsaKeyPairAsyKeySpec(env, arg, asyKeySpec);
689     } else {
690         return false;
691     }
692 }
693 
GetFpField(napi_env env,napi_value arg,HcfECField ** ecField)694 static bool GetFpField(napi_env env, napi_value arg, HcfECField **ecField)
695 {
696     HcfECFieldFp *fp = reinterpret_cast<HcfECFieldFp *>(HcfMalloc(sizeof(HcfECFieldFp), 0));
697     if (fp == nullptr) {
698         LOGE("malloc fp failed!");
699         return false;
700     }
701 
702     size_t fieldTpyeLen = ECC_FIELD_TYPE_FP.length();
703     fp->base.fieldType = static_cast<char *>(HcfMalloc(fieldTpyeLen + 1, 0));
704     if (fp->base.fieldType == nullptr) {
705         LOGE("malloc fieldType failed!");
706         HcfFree(fp);
707         return false;
708     }
709     (void)memcpy_s(fp->base.fieldType, fieldTpyeLen+ 1, ECC_FIELD_TYPE_FP.c_str(), fieldTpyeLen);
710 
711     napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
712     bool ret = GetBigIntFromNapiValue(env, p, &fp->p);
713     if (!ret) {
714         HcfFree(fp->base.fieldType);
715         HcfFree(fp);
716         return false;
717     }
718     *ecField = reinterpret_cast<HcfECField *>(fp);
719     return true;
720 }
721 
GetField(napi_env env,napi_value arg,HcfECField ** ecField)722 static bool GetField(napi_env env, napi_value arg, HcfECField **ecField)
723 {
724     // get fieldData in { field : fieldData, a : xxx, b : xxx, ... } of ECCCommonParamsSpec first
725     napi_value fieldData = nullptr;
726     napi_valuetype valueType = napi_undefined;
727     napi_status status = napi_get_named_property(env, arg, "field", &fieldData);
728     napi_typeof(env, fieldData, &valueType);
729     if ((status != napi_ok) || (fieldData == nullptr) || (valueType == napi_undefined)) {
730         LOGE("failed to get valid field data!");
731         return false;
732     }
733 
734     // get fieldType in { fieldType : fieldTypeData } of ECField
735     napi_value fieldTypeData = nullptr;
736     status = napi_get_named_property(env, fieldData, "fieldType", &fieldTypeData);
737     napi_typeof(env, fieldTypeData, &valueType);
738     if ((status != napi_ok) || (fieldTypeData == nullptr) || (valueType == napi_undefined)) {
739         LOGE("failed to get valid fieldType data!");
740         return false;
741     }
742     string fieldType;
743     if (!GetStringFromJSParams(env, fieldTypeData, fieldType)) {
744         LOGE("GetStringFromJSParams failed when extracting fieldType!");
745         return false;
746     }
747 
748     // get p in { p : pData } of ECField, and generateECField
749     if (fieldType.compare("Fp") == 0) {
750         return GetFpField(env, fieldData, ecField);
751     }
752     return false;
753 }
754 
InitEccDetailAsyKeySpec(napi_env env,napi_value arg,HcfEccCommParamsSpec * spec)755 static bool InitEccDetailAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec)
756 {
757     napi_value a = GetDetailAsyKeySpecValue(env, arg, "a");
758     napi_value b = GetDetailAsyKeySpecValue(env, arg, "b");
759     napi_value n = GetDetailAsyKeySpecValue(env, arg, "n");
760     napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
761     bool ret = GetBigIntFromNapiValue(env, a, &spec->a);
762     if (!ret) {
763         LOGE("get ecc asyKeySpec a failed!");
764         return false;
765     }
766     ret = GetBigIntFromNapiValue(env, b, &spec->b);
767     if (!ret) {
768         LOGE("get ecc asyKeySpec b failed!");
769         return false;
770     }
771     ret = GetBigIntFromNapiValue(env, n, &spec->n);
772     if (!ret) {
773         LOGE("get ecc asyKeySpec n failed!");
774         return false;
775     }
776     ret = GetPointFromNapiValue(env, g, &spec->g);
777     if (!ret) {
778         LOGE("get ecc asyKeySpec g failed!");
779         return false;
780     }
781     return true;
782 }
783 
InitEccCommonAsyKeySpec(napi_env env,napi_value arg,HcfEccCommParamsSpec * spec,const string & algName)784 static bool InitEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfEccCommParamsSpec *spec, const string &algName)
785 {
786     size_t algNameLen = ECC_ASY_KEY_SPEC.length();
787     spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
788     if (spec->base.algName == nullptr) {
789         LOGE("malloc ECC algName failed!");
790         return false;
791     }
792     (void)memcpy_s(spec->base.algName, algNameLen+ 1, algName.c_str(), algNameLen);
793     spec->base.specType = HCF_COMMON_PARAMS_SPEC;
794 
795     // get h
796     napi_value hData = nullptr;
797     napi_valuetype valueType = napi_undefined;
798     napi_status status = napi_get_named_property(env, arg, "h", &hData);
799     napi_typeof(env, hData, &valueType);
800     if ((status != napi_ok) || (hData == nullptr) || (valueType == napi_undefined)) {
801         LOGE("failed to get valid h!");
802         HcfFree(spec->base.algName);
803         spec->base.algName = nullptr;
804         return false;
805     }
806     if (!GetInt32FromJSParams(env, hData, spec->h)) {
807         LOGE("get ecc asyKeySpec h failed!");
808         HcfFree(spec->base.algName);
809         spec->base.algName = nullptr;
810         return false;
811     }
812     // get field
813     if (!GetField(env, arg, &spec->field)) {
814         LOGE("GetField failed!");
815         HcfFree(spec->base.algName);
816         spec->base.algName = nullptr;
817         return false;
818     }
819     bool ret = InitEccDetailAsyKeySpec(env, arg, spec);
820     if (!ret) {
821         LOGE("get ecc asyKeySpec g failed!");
822         FreeEccCommParamsSpec(spec);
823         return false;
824     }
825     return true;
826 }
827 
GetEccCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)828 static bool GetEccCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec,
829     const string &algName)
830 {
831     HcfEccCommParamsSpec *spec = reinterpret_cast<HcfEccCommParamsSpec *>(HcfMalloc(sizeof(HcfEccCommParamsSpec), 0));
832     if (spec == nullptr) {
833         LOGE("malloc failed!");
834         return false;
835     }
836     if (!InitEccCommonAsyKeySpec(env, arg, spec, algName)) {
837         LOGE("InitEccCommonAsyKeySpec failed!");
838         HcfFree(spec);
839         return false;
840     }
841     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
842     return true;
843 }
844 
GetEccPriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)845 static bool GetEccPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
846 {
847     HcfEccPriKeyParamsSpec *spec =
848         reinterpret_cast<HcfEccPriKeyParamsSpec *>(HcfMalloc(sizeof(HcfEccPriKeyParamsSpec), 0));
849     if (spec == nullptr) {
850         LOGE("malloc failed!");
851         return false;
852     }
853 
854     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
855     if (commSpecValue == nullptr) {
856         LOGE("Get comm spec napi value failed.");
857         HcfFree(spec);
858         return false;
859     }
860     if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec), algName)) {
861         LOGE("InitEccCommonAsyKeySpec failed!");
862         HcfFree(spec);
863         return false;
864     }
865     spec->base.base.specType = HCF_PRIVATE_KEY_SPEC;
866 
867     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
868     bool ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
869     if (!ret) {
870         // get big int fail, sk is null
871         FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
872         HcfFree(spec);
873         return false;
874     }
875     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
876     return true;
877 }
878 
GetEccPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)879 static bool GetEccPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
880 {
881     HcfEccPubKeyParamsSpec *spec =
882         reinterpret_cast<HcfEccPubKeyParamsSpec *>(HcfMalloc(sizeof(HcfEccPubKeyParamsSpec), 0));
883     if (spec == nullptr) {
884         LOGE("malloc failed!");
885         return false;
886     }
887 
888     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
889     if (commSpecValue == nullptr) {
890         LOGE("Get comm spec napi value failed.");
891         HcfFree(spec);
892         return false;
893     }
894     if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec), algName)) {
895         LOGE("InitEccCommonAsyKeySpec failed!");
896         HcfFree(spec);
897         return false;
898     }
899     spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
900 
901     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
902     bool ret = GetPointFromNapiValue(env, pk, &spec->pk);
903     if (!ret) {
904         DestroyEccPubKeySpec(spec);
905         return false;
906     }
907     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
908     return true;
909 }
910 
GetEccKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)911 static bool GetEccKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec,
912     const string &algName)
913 {
914     HcfEccKeyPairParamsSpec *spec =
915         reinterpret_cast<HcfEccKeyPairParamsSpec *>(HcfMalloc(sizeof(HcfEccKeyPairParamsSpec), 0));
916     if (spec == nullptr) {
917         LOGE("malloc failed!");
918         return false;
919     }
920 
921     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
922     if (commSpecValue == nullptr) {
923         LOGE("Get comm spec napi value failed.");
924         HcfFree(spec);
925         return false;
926     }
927     if (!InitEccCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfEccCommParamsSpec *>(spec), algName)) {
928         LOGE("InitEccCommonAsyKeySpec failed!");
929         HcfFree(spec);
930         return false;
931     }
932     spec->base.base.specType = HCF_KEY_PAIR_SPEC;
933 
934     // get big int fail, sk is null
935     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
936     bool ret = GetPointFromNapiValue(env, pk, &spec->pk);
937     if (!ret) {
938         FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
939         HcfFree(spec);
940         return false;
941     }
942     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
943     ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
944     if (!ret) {
945         FreeEccCommParamsSpec(reinterpret_cast<HcfEccCommParamsSpec *>(spec));
946         HcfFree(spec->pk.x.data);
947         HcfFree(spec->pk.y.data);
948         HcfFree(spec);
949         return false;
950     }
951     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
952     return true;
953 }
954 
GetEccAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)955 static bool GetEccAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
956 {
957     napi_value data = nullptr;
958     napi_valuetype valueType = napi_undefined;
959 
960     napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
961     napi_typeof(env, data, &valueType);
962     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
963         LOGE("failed to get valid algo name!");
964         return false;
965     }
966     HcfAsyKeySpecType asyKeySpecType;
967     status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
968     if (status != napi_ok) {
969         LOGE("failed to get valid asyKeySpecType!");
970         return false;
971     }
972     if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
973         return GetEccCommonAsyKeySpec(env, arg, asyKeySpec, algName);
974     } else if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
975         return GetEccPriKeySpec(env, arg, asyKeySpec, algName);
976     } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
977         return GetEccPubKeySpec(env, arg, asyKeySpec, algName);
978     } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
979         return GetEccKeyPairAsyKeySpec(env, arg, asyKeySpec, algName);
980     } else {
981         LOGE("keySpec not support!");
982         return false;
983     }
984 }
985 
InitRsaCommonAsyKeySpec(napi_env env,napi_value arg,HcfRsaCommParamsSpec * spec)986 static bool InitRsaCommonAsyKeySpec(napi_env env, napi_value arg, HcfRsaCommParamsSpec *spec)
987 {
988     size_t algNameLen = RSA_ASY_KEY_SPEC.length();
989     spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
990     if (spec->base.algName == nullptr) {
991         LOGE("malloc RSA algName failed!");
992         return false;
993     }
994     (void)memcpy_s(spec->base.algName, algNameLen+ 1, RSA_ASY_KEY_SPEC.c_str(), algNameLen);
995     spec->base.specType = HCF_COMMON_PARAMS_SPEC;
996 
997     napi_value n = GetDetailAsyKeySpecValue(env, arg, "n");
998 
999     bool ret = GetBigIntFromNapiValue(env, n, &spec->n);
1000     if (!ret) {
1001         LOGE("Rsa asyKeySpec get n failed!");
1002         HcfFree(spec->base.algName);
1003         spec->base.algName = nullptr;
1004         return false;
1005     }
1006     return true;
1007 }
1008 
GetRsaPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1009 static bool GetRsaPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1010 {
1011     HcfRsaPubKeyParamsSpec *spec =
1012         reinterpret_cast<HcfRsaPubKeyParamsSpec *>(HcfMalloc(sizeof(HcfRsaPubKeyParamsSpec), 0));
1013     if (spec == nullptr) {
1014         LOGE("malloc failed!");
1015         return false;
1016     }
1017 
1018     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1019     if (commSpecValue == nullptr) {
1020         LOGE("Get comm spec napi value failed.");
1021         HcfFree(spec);
1022         return false;
1023     }
1024     if (!InitRsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfRsaCommParamsSpec *>(spec))) {
1025         LOGE("InitRsaCommonAsyKeySpec failed!");
1026         HcfFree(spec);
1027         return false;
1028     }
1029     spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
1030 
1031     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1032     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1033     if (!ret) {
1034         DestroyRsaPubKeySpec(spec);
1035         return false;
1036     }
1037     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1038     return true;
1039 }
1040 
GetRsaKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1041 static bool GetRsaKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1042 {
1043     HcfRsaKeyPairParamsSpec *spec =
1044         reinterpret_cast<HcfRsaKeyPairParamsSpec *>(HcfMalloc(sizeof(HcfRsaKeyPairParamsSpec), 0));
1045     if (spec == nullptr) {
1046         LOGE("malloc failed!");
1047         return false;
1048     }
1049 
1050     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1051     if (commSpecValue == nullptr) {
1052         LOGE("Get comm spec napi value failed.");
1053         HcfFree(spec);
1054         return false;
1055     }
1056     if (!InitRsaCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfRsaCommParamsSpec *>(spec))) {
1057         LOGE("InitRsaCommonAsyKeySpec failed!");
1058         HcfFree(spec);
1059         return false;
1060     }
1061     spec->base.base.specType = HCF_KEY_PAIR_SPEC;
1062 
1063     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1064     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1065     if (!ret) {
1066         FreeRsaCommParamsSpec(&(spec->base));
1067         HcfFree(spec);
1068         return false;
1069     }
1070     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1071     ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1072     if (!ret) {
1073         FreeRsaCommParamsSpec(&(spec->base));
1074         HcfFree(spec->pk.data);
1075         HcfFree(spec);
1076         return false;
1077     }
1078     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1079     return true;
1080 }
1081 
GetRsaAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1082 static bool GetRsaAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1083 {
1084     napi_value data = nullptr;
1085     napi_valuetype valueType = napi_undefined;
1086 
1087     napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1088     napi_typeof(env, data, &valueType);
1089     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1090         LOGE("failed to get valid algo name!");
1091         return false;
1092     }
1093     HcfAsyKeySpecType asyKeySpecType;
1094     status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1095     if (status != napi_ok) {
1096         LOGE("failed to get valid asyKeySpecType!");
1097         return false;
1098     }
1099     if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
1100         LOGE("RSA not support comm key spec");
1101         return false;
1102     } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1103         return GetRsaPubKeySpec(env, arg, asyKeySpec);
1104     } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1105         return GetRsaKeyPairAsyKeySpec(env, arg, asyKeySpec);
1106     } else {
1107         return false;
1108     }
1109 }
1110 
InitAlg25519CommonAsyKeySpec(HcfAsyKeyParamsSpec * spec,const string & algName)1111 static bool InitAlg25519CommonAsyKeySpec(HcfAsyKeyParamsSpec *spec, const string &algName)
1112 {
1113     size_t algNameLen = algName.length();
1114     spec->algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
1115     if (spec->algName == nullptr) {
1116         LOGE("malloc alg25519 algName failed!");
1117         return false;
1118     }
1119     (void)memcpy_s(spec->algName, algNameLen + 1, algName.c_str(), algNameLen);
1120     return true;
1121 }
1122 
GetAlg25519PriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1123 static bool GetAlg25519PriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1124 {
1125     HcfAlg25519PriKeyParamsSpec *spec =
1126         reinterpret_cast<HcfAlg25519PriKeyParamsSpec *>(HcfMalloc(sizeof(HcfAlg25519PriKeyParamsSpec), 0));
1127     if (spec == nullptr) {
1128         LOGE("malloc failed!");
1129         return false;
1130     }
1131     if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast<HcfAsyKeyParamsSpec *>(spec), algName)) {
1132         LOGE("InitRsaCommonAsyKeySpec failed!");
1133         DestroyAlg25519PriKeySpec(spec);
1134         return false;
1135     }
1136     spec->base.specType = HCF_PRIVATE_KEY_SPEC;
1137 
1138     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1139     bool ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1140     if (!ret) {
1141         // get big int fail, sk is null
1142         DestroyAlg25519PriKeySpec(spec);
1143         return false;
1144     }
1145     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1146     return true;
1147 }
1148 
GetAlg25519PubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1149 static bool GetAlg25519PubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1150 {
1151     HcfAlg25519PubKeyParamsSpec *spec =
1152         reinterpret_cast<HcfAlg25519PubKeyParamsSpec *>(HcfMalloc(sizeof(HcfAlg25519PubKeyParamsSpec), 0));
1153     if (spec == nullptr) {
1154         LOGE("malloc failed!");
1155         return false;
1156     }
1157     if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast<HcfAsyKeyParamsSpec *>(spec), algName)) {
1158         LOGE("InitRsaCommonAsyKeySpec failed!");
1159         DestroyAlg25519PubKeySpec(spec);
1160         return false;
1161     }
1162     spec->base.specType = HCF_PUBLIC_KEY_SPEC;
1163 
1164     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1165     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1166     if (!ret) {
1167         DestroyAlg25519PubKeySpec(spec);
1168         return false;
1169     }
1170     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1171     return true;
1172 }
1173 
GetAlg25519KeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1174 static bool GetAlg25519KeyPairAsyKeySpec(napi_env env, napi_value arg,
1175     HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1176 {
1177     HcfAlg25519KeyPairParamsSpec *spec =
1178         reinterpret_cast<HcfAlg25519KeyPairParamsSpec *>(HcfMalloc(sizeof(HcfAlg25519KeyPairParamsSpec), 0));
1179     if (spec == nullptr) {
1180         LOGE("malloc failed!");
1181         return false;
1182     }
1183     if (!InitAlg25519CommonAsyKeySpec(reinterpret_cast<HcfAsyKeyParamsSpec *>(spec), algName)) {
1184         LOGE("InitRsaCommonAsyKeySpec failed!");
1185         HcfFree(spec);
1186         return false;
1187     }
1188     spec->base.specType = HCF_KEY_PAIR_SPEC;
1189 
1190     // get big int fail, sk is null
1191     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1192     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1193     if (!ret) {
1194         DestroyAlg25519KeyPairSpec(spec);
1195         return false;
1196     }
1197     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1198     ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1199     if (!ret) {
1200         DestroyAlg25519KeyPairSpec(spec);
1201         return false;
1202     }
1203     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1204     return true;
1205 }
1206 
GetAlg25519AsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec,const string & algName)1207 static bool GetAlg25519AsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec, const string &algName)
1208 {
1209     napi_value data = nullptr;
1210     napi_valuetype valueType = napi_undefined;
1211 
1212     napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1213     napi_typeof(env, data, &valueType);
1214     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1215         LOGE("failed to get valid algo name!");
1216         return false;
1217     }
1218     HcfAsyKeySpecType asyKeySpecType;
1219     status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1220     if (status != napi_ok) {
1221         LOGE("failed to get valid asyKeySpecType!");
1222         return false;
1223     }
1224     if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
1225         return GetAlg25519PriKeySpec(env, arg, asyKeySpec, algName);
1226     } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1227         return GetAlg25519PubKeySpec(env, arg, asyKeySpec, algName);
1228     } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1229         return GetAlg25519KeyPairAsyKeySpec(env, arg, asyKeySpec, algName);
1230     } else {
1231         LOGE("keySpec not support!");
1232         return false;
1233     }
1234 }
1235 
InitDhCommonAsyKeySpec(napi_env env,napi_value arg,HcfDhCommParamsSpec * spec)1236 static bool InitDhCommonAsyKeySpec(napi_env env, napi_value arg, HcfDhCommParamsSpec *spec)
1237 {
1238     size_t algNameLen = DH_ASY_KEY_SPEC.length();
1239     spec->base.algName = static_cast<char *>(HcfMalloc(algNameLen + 1, 0));
1240     if (spec->base.algName == nullptr) {
1241         LOGE("malloc DH algName failed!");
1242         return false;
1243     }
1244     (void)memcpy_s(spec->base.algName, algNameLen+ 1, DH_ASY_KEY_SPEC.c_str(), algNameLen);
1245     spec->base.specType = HCF_COMMON_PARAMS_SPEC;
1246 
1247     napi_value length = nullptr;
1248     napi_valuetype valueType = napi_undefined;
1249     napi_status status = napi_get_named_property(env, arg, "l", &length);
1250     napi_typeof(env, length, &valueType);
1251     if ((status != napi_ok) || (length == nullptr) || (valueType == napi_undefined)) {
1252         LOGE("failed to get valid l!");
1253         HcfFree(spec->base.algName);
1254         spec->base.algName = nullptr;
1255         return false;
1256     }
1257     if (!GetInt32FromJSParams(env, length, spec->length)) {
1258         LOGE("get dh asyKeySpec length failed!");
1259         HcfFree(spec->base.algName);
1260         spec->base.algName = nullptr;
1261         return false;
1262     }
1263     napi_value p = GetDetailAsyKeySpecValue(env, arg, "p");
1264     napi_value g = GetDetailAsyKeySpecValue(env, arg, "g");
1265     bool ret = GetBigIntFromNapiValue(env, p, &spec->p);
1266     if (!ret) {
1267         HcfFree(spec->base.algName);
1268         spec->base.algName = nullptr;
1269         return false;
1270     }
1271     ret = GetBigIntFromNapiValue(env, g, &spec->g);
1272     if (!ret) {
1273         FreeDhCommParamsSpec(spec);
1274         return false;
1275     }
1276     return true;
1277 }
1278 
GetDhCommonAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1279 static bool GetDhCommonAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1280 {
1281     HcfDhCommParamsSpec *spec = reinterpret_cast<HcfDhCommParamsSpec *>(HcfMalloc(sizeof(HcfDhCommParamsSpec), 0));
1282     if (spec == nullptr) {
1283         LOGE("malloc failed!");
1284         return false;
1285     }
1286     if (!InitDhCommonAsyKeySpec(env, arg, spec)) {
1287         LOGE("InitDhCommonAsyKeySpec failed!");
1288         HcfFree(spec);
1289         return false;
1290     }
1291     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1292     return true;
1293 }
1294 
GetDhPubKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1295 static bool GetDhPubKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1296 {
1297     HcfDhPubKeyParamsSpec *spec = reinterpret_cast<HcfDhPubKeyParamsSpec *>(
1298         HcfMalloc(sizeof(HcfDhPubKeyParamsSpec), 0));
1299     if (spec == nullptr) {
1300         LOGE("malloc failed!");
1301         return false;
1302     }
1303 
1304     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1305     if (commSpecValue == nullptr) {
1306         LOGE("Get comm spec napi value failed.");
1307         HcfFree(spec);
1308         return false;
1309     }
1310     if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDhCommParamsSpec *>(spec))) {
1311         LOGE("InitDhCommonAsyKeySpec failed.");
1312         HcfFree(spec);
1313         return false;
1314     }
1315     spec->base.base.specType = HCF_PUBLIC_KEY_SPEC;
1316 
1317     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1318     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1319     if (!ret) {
1320         DestroyDhPubKeySpec(spec);
1321         return false;
1322     }
1323     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1324     return true;
1325 }
1326 
GetDhPriKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1327 static bool GetDhPriKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1328 {
1329     HcfDhPriKeyParamsSpec *spec = reinterpret_cast<HcfDhPriKeyParamsSpec *>(
1330         HcfMalloc(sizeof(HcfDhPriKeyParamsSpec), 0));
1331     if (spec == nullptr) {
1332         LOGE("malloc failed!");
1333         return false;
1334     }
1335 
1336     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1337     if (commSpecValue == nullptr) {
1338         LOGE("Get comm spec napi value failed.");
1339         HcfFree(spec);
1340         return false;
1341     }
1342     if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDhCommParamsSpec *>(spec))) {
1343         LOGE("InitDhCommonAsyKeySpec failed.");
1344         HcfFree(spec);
1345         return false;
1346     }
1347     spec->base.base.specType = HCF_PRIVATE_KEY_SPEC;
1348 
1349     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "sk");
1350     bool ret = GetBigIntFromNapiValue(env, pk, &spec->sk);
1351     if (!ret) {
1352         DestroyDhPriKeySpec(spec);
1353         return false;
1354     }
1355     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1356     return true;
1357 }
1358 
GetDhKeyPairAsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1359 static bool GetDhKeyPairAsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1360 {
1361     HcfDhKeyPairParamsSpec *spec = reinterpret_cast<HcfDhKeyPairParamsSpec *>(
1362         HcfMalloc(sizeof(HcfDhKeyPairParamsSpec), 0));
1363     if (spec == nullptr) {
1364         LOGE("malloc failed!");
1365         return false;
1366     }
1367 
1368     napi_value commSpecValue = GetCommSpecNapiValue(env, arg);
1369     if (commSpecValue == nullptr) {
1370         LOGE("Get comm spec napi value failed.");
1371         HcfFree(spec);
1372         return false;
1373     }
1374     if (!InitDhCommonAsyKeySpec(env, commSpecValue, reinterpret_cast<HcfDhCommParamsSpec *>(spec))) {
1375         LOGE("InitDhCommonAsyKeySpec failed!");
1376         HcfFree(spec);
1377         return false;
1378     }
1379     spec->base.base.specType = HCF_KEY_PAIR_SPEC;
1380 
1381     napi_value pk = GetDetailAsyKeySpecValue(env, arg, "pk");
1382     bool ret = GetBigIntFromNapiValue(env, pk, &spec->pk);
1383     if (!ret) {
1384         FreeDhCommParamsSpec(reinterpret_cast<HcfDhCommParamsSpec *>(spec));
1385         HcfFree(spec);
1386         return false;
1387     }
1388     napi_value sk = GetDetailAsyKeySpecValue(env, arg, "sk");
1389     ret = GetBigIntFromNapiValue(env, sk, &spec->sk);
1390     if (!ret) {
1391         FreeDhCommParamsSpec(reinterpret_cast<HcfDhCommParamsSpec *>(spec));
1392         HcfFree(spec->pk.data);
1393         HcfFree(spec);
1394         return false;
1395     }
1396     *asyKeySpec = reinterpret_cast<HcfAsyKeyParamsSpec *>(spec);
1397     return true;
1398 }
1399 
GetDh25519AsyKeySpec(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1400 static bool GetDh25519AsyKeySpec(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1401 {
1402     napi_value data = nullptr;
1403     napi_valuetype valueType = napi_undefined;
1404 
1405     napi_status status = napi_get_named_property(env, arg, TAG_SPEC_TYPE.c_str(), &data);
1406     napi_typeof(env, data, &valueType);
1407     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1408         LOGE("failed to get valid algo name!");
1409         return false;
1410     }
1411     HcfAsyKeySpecType asyKeySpecType;
1412     status = napi_get_value_uint32(env, data, reinterpret_cast<uint32_t *>(&asyKeySpecType));
1413     if (status != napi_ok) {
1414         LOGE("failed to get valid asyKeySpecType!");
1415         return false;
1416     }
1417     if (asyKeySpecType == HCF_PRIVATE_KEY_SPEC) {
1418         return GetDhPriKeySpec(env, arg, asyKeySpec);
1419     } else if (asyKeySpecType == HCF_PUBLIC_KEY_SPEC) {
1420         return GetDhPubKeySpec(env, arg, asyKeySpec);
1421     } else if (asyKeySpecType == HCF_KEY_PAIR_SPEC) {
1422         return GetDhKeyPairAsyKeySpec(env, arg, asyKeySpec);
1423     } else if (asyKeySpecType == HCF_COMMON_PARAMS_SPEC) {
1424         return GetDhCommonAsyKeySpec(env, arg, asyKeySpec);
1425     } else {
1426         LOGE("keySpec not support!");
1427         return false;
1428     }
1429 }
1430 
GetAsyKeySpecFromNapiValue(napi_env env,napi_value arg,HcfAsyKeyParamsSpec ** asyKeySpec)1431 bool GetAsyKeySpecFromNapiValue(napi_env env, napi_value arg, HcfAsyKeyParamsSpec **asyKeySpec)
1432 {
1433     napi_value data = nullptr;
1434 
1435     if ((env == nullptr) || (arg == nullptr) || (asyKeySpec == nullptr)) {
1436         LOGE("Invalid params!");
1437         return false;
1438     }
1439 
1440     napi_valuetype valueType = napi_undefined;
1441 
1442     napi_status status = napi_get_named_property(env, arg, CRYPTO_TAG_ALG_NAME.c_str(), &data);
1443     napi_typeof(env, data, &valueType);
1444     if ((status != napi_ok) || (data == nullptr) || (valueType == napi_undefined)) {
1445         LOGE("failed to get valid algName!");
1446         return false;
1447     }
1448     string algName;
1449     if (!GetStringFromJSParams(env, data, algName)) {
1450         LOGE("GetStringFromJSParams failed!");
1451         return false;
1452     }
1453     if (algName.compare(DSA_ASY_KEY_SPEC) == 0) {
1454         return GetDsaAsyKeySpec(env, arg, asyKeySpec);
1455     } else if (algName.compare(ECC_ASY_KEY_SPEC) == 0 || algName.compare(SM2_ASY_KEY_SPEC) == 0) {
1456         return GetEccAsyKeySpec(env, arg, asyKeySpec, algName);
1457     } else if (algName.compare(RSA_ASY_KEY_SPEC) == 0) {
1458         return GetRsaAsyKeySpec(env, arg, asyKeySpec);
1459     } else if (algName.compare(ED25519_ASY_KEY_SPEC) == 0 || algName.compare(X25519_ASY_KEY_SPEC) == 0) {
1460         return GetAlg25519AsyKeySpec(env, arg, asyKeySpec, algName);
1461     } else if (algName.compare(DH_ASY_KEY_SPEC) == 0) {
1462         return GetDh25519AsyKeySpec(env, arg, asyKeySpec);
1463     } else {
1464         LOGE("AlgName not support! [AlgName]: %s", algName.c_str());
1465         return false;
1466     }
1467 }
1468 
ConvertBlobToNapiValue(napi_env env,HcfBlob * blob)1469 napi_value ConvertBlobToNapiValue(napi_env env, HcfBlob *blob)
1470 {
1471     if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1472         LOGD("Invalid blob!");
1473         return NapiGetNull(env);
1474     }
1475     uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1476     if (buffer == nullptr) {
1477         LOGE("malloc uint8 array buffer failed!");
1478         return NapiGetNull(env);
1479     }
1480 
1481     if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
1482         LOGE("memcpy_s data to buffer failed!");
1483         HcfFree(buffer);
1484         return NapiGetNull(env);
1485     }
1486 
1487     napi_value outBuffer = nullptr;
1488     napi_status status = napi_create_external_arraybuffer(
1489         env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1490     if (status != napi_ok) {
1491         LOGE("create uint8 array buffer failed!");
1492         HcfFree(buffer);
1493         return NapiGetNull(env);
1494     }
1495     buffer = nullptr;
1496 
1497     napi_value outData = nullptr;
1498     napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1499     napi_value dataBlob = nullptr;
1500     napi_create_object(env, &dataBlob);
1501     napi_set_named_property(env, dataBlob, CRYPTO_TAG_DATA.c_str(), outData);
1502 
1503     return dataBlob;
1504 }
1505 
ConvertCipherBlobToNapiValue(napi_env env,HcfBlob * blob)1506 napi_value ConvertCipherBlobToNapiValue(napi_env env, HcfBlob *blob)
1507 {
1508     if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1509         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1510         LOGE("Invalid blob!");
1511         return NapiGetNull(env);
1512     }
1513     uint8_t *buffer = reinterpret_cast<uint8_t *>(HcfMalloc(blob->len, 0));
1514     if (buffer == nullptr) {
1515         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc uint8 array buffer failed!"));
1516         LOGE("malloc uint8 array buffer failed!");
1517         return NapiGetNull(env);
1518     }
1519 
1520     if (memcpy_s(buffer, blob->len, blob->data, blob->len) != EOK) {
1521         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "memcpy_s data to buffer failed!"));
1522         LOGE("memcpy_s data to buffer failed!");
1523         HcfFree(buffer);
1524         return NapiGetNull(env);
1525     }
1526 
1527     napi_value outBuffer = nullptr;
1528     napi_status status = napi_create_external_arraybuffer(
1529         env, buffer, blob->len, [](napi_env env, void *data, void *hint) { HcfFree(data); }, nullptr, &outBuffer);
1530     if (status != napi_ok) {
1531         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create uint8 array buffer failed!"));
1532         LOGE("create uint8 array buffer failed!");
1533         HcfFree(buffer);
1534         return NapiGetNull(env);
1535     }
1536     buffer = nullptr;
1537 
1538     napi_value outData = nullptr;
1539     napi_create_typedarray(env, napi_uint8_array, blob->len, outBuffer, 0, &outData);
1540     return outData;
1541 }
1542 
ConvertBigIntToNapiValue(napi_env env,HcfBigInteger * blob)1543 napi_value ConvertBigIntToNapiValue(napi_env env, HcfBigInteger *blob)
1544 {
1545     if (blob == nullptr || blob->data == nullptr || blob->len == 0) {
1546         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1547         LOGE("Invalid blob!");
1548         return NapiGetNull(env);
1549     }
1550     size_t wordsCount = (blob->len / sizeof(uint64_t)) + ((blob->len % sizeof(uint64_t)) == 0 ? 0 : 1);
1551     uint64_t *words = reinterpret_cast<uint64_t *>(HcfMalloc(wordsCount * sizeof(uint64_t), 0));
1552     if (words == nullptr) {
1553         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "malloc uint8 array buffer failed!"));
1554         LOGE("malloc uint8 array buffer failed!");
1555         return NapiGetNull(env);
1556     }
1557 
1558     size_t index = 0;
1559     for (size_t i = 0; index < wordsCount; i += sizeof(uint64_t), index++) {
1560         uint64_t tmp = 0;
1561         for (size_t j = 0; j < sizeof(uint64_t); j++) {
1562             if (i + j < blob->len) {
1563                 tmp += ((uint64_t)blob->data[i + j] << (sizeof(uint64_t) * j));
1564             }
1565         }
1566         words[index] = tmp;
1567     }
1568     napi_value bigInt = nullptr;
1569     napi_status status = napi_create_bigint_words(env, 0, wordsCount, words, &bigInt);
1570     if (status != napi_ok) {
1571         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create bigint failed!"));
1572         LOGE("create bigint failed!");
1573         HcfFree(words);
1574         return NapiGetNull(env);
1575     }
1576     if (bigInt == nullptr) {
1577         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "bigInt is null!"));
1578         LOGE("bigInt is null!");
1579     }
1580     HcfFree(words);
1581     words = nullptr;
1582     return bigInt;
1583 }
1584 
GetStringFromJSParams(napi_env env,napi_value arg,string & returnStr)1585 bool GetStringFromJSParams(napi_env env, napi_value arg, string &returnStr)
1586 {
1587     napi_valuetype valueType;
1588     napi_typeof(env, arg, &valueType);
1589     if (valueType != napi_string) {
1590         LOGE("wrong argument type. expect string type.");
1591         return false;
1592     }
1593 
1594     size_t length = 0;
1595     if (napi_get_value_string_utf8(env, arg, nullptr, 0, &length) != napi_ok) {
1596         LOGE("can not get string length");
1597         return false;
1598     }
1599     returnStr.reserve(length + 1);
1600     returnStr.resize(length);
1601     if (napi_get_value_string_utf8(env, arg, returnStr.data(), (length + 1), &length) != napi_ok) {
1602         LOGE("can not get string value");
1603         return false;
1604     }
1605     return true;
1606 }
1607 
GetInt32FromJSParams(napi_env env,napi_value arg,int32_t & returnInt)1608 bool GetInt32FromJSParams(napi_env env, napi_value arg, int32_t &returnInt)
1609 {
1610     napi_valuetype valueType;
1611     napi_typeof(env, arg, &valueType);
1612     if (valueType != napi_number) {
1613         LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
1614         return false;
1615     }
1616 
1617     if (napi_get_value_int32(env, arg, &returnInt) != napi_ok) {
1618         LOGE("can not get int value");
1619         return false;
1620     }
1621     return true;
1622 }
1623 
GetUint32FromJSParams(napi_env env,napi_value arg,uint32_t & returnInt)1624 bool GetUint32FromJSParams(napi_env env, napi_value arg, uint32_t &returnInt)
1625 {
1626     napi_valuetype valueType;
1627     napi_typeof(env, arg, &valueType);
1628     if (valueType != napi_number) {
1629         LOGE("wrong argument type. expect int type. [Type]: %d", valueType);
1630         return false;
1631     }
1632 
1633     if (napi_get_value_uint32(env, arg, &returnInt) != napi_ok) {
1634         LOGE("can not get int value");
1635         return false;
1636     }
1637     return true;
1638 }
1639 
GetCallbackFromJSParams(napi_env env,napi_value arg,napi_ref * returnCb)1640 bool GetCallbackFromJSParams(napi_env env, napi_value arg, napi_ref *returnCb)
1641 {
1642     napi_valuetype valueType = napi_undefined;
1643     napi_typeof(env, arg, &valueType);
1644     if (valueType != napi_function) {
1645         LOGE("wrong argument type. expect callback type. [Type]: %d", valueType);
1646         return false;
1647     }
1648 
1649     napi_create_reference(env, arg, 1, returnCb);
1650     return true;
1651 }
1652 
GetJsErrValueByErrCode(HcfResult errCode)1653 static uint32_t GetJsErrValueByErrCode(HcfResult errCode)
1654 {
1655     switch (errCode) {
1656         case HCF_INVALID_PARAMS:
1657             return JS_ERR_INVALID_PARAMS;
1658         case HCF_NOT_SUPPORT:
1659             return JS_ERR_NOT_SUPPORT;
1660         case HCF_ERR_MALLOC:
1661             return JS_ERR_OUT_OF_MEMORY;
1662         case HCF_ERR_COPY:
1663             return JS_ERR_RUNTIME_ERROR;
1664         case HCF_ERR_CRYPTO_OPERATION:
1665             return JS_ERR_CRYPTO_OPERATION;
1666         default:
1667             return JS_ERR_DEFAULT_ERR;
1668     }
1669 }
1670 
GenerateBusinessError(napi_env env,HcfResult errCode,const char * errMsg)1671 napi_value GenerateBusinessError(napi_env env, HcfResult errCode, const char *errMsg)
1672 {
1673     napi_value businessError = nullptr;
1674 
1675     napi_value code = nullptr;
1676     napi_create_uint32(env, GetJsErrValueByErrCode(errCode), &code);
1677 
1678     napi_value msg = nullptr;
1679     napi_create_string_utf8(env, errMsg, NAPI_AUTO_LENGTH, &msg);
1680 
1681     napi_create_error(env, nullptr, msg, &businessError);
1682     napi_set_named_property(env, businessError, CRYPTO_TAG_ERR_CODE.c_str(), code);
1683 
1684     return businessError;
1685 }
1686 
CheckArgsCount(napi_env env,size_t argc,size_t expectedCount,bool isSync)1687 bool CheckArgsCount(napi_env env, size_t argc, size_t expectedCount, bool isSync)
1688 {
1689     if (isSync) {
1690         if (argc != expectedCount) {
1691             LOGE("invalid params count!");
1692             return false;
1693         }
1694     } else {
1695         if ((argc != expectedCount) && (argc != (expectedCount - ARGS_SIZE_ONE))) {
1696             LOGE("invalid params count!");
1697             return false;
1698         }
1699     }
1700     return true;
1701 }
1702 
isCallback(napi_env env,napi_value argv,size_t argc,size_t expectedArgc)1703 bool isCallback(napi_env env, napi_value argv, size_t argc, size_t expectedArgc)
1704 {
1705     if (argc == expectedArgc - 1) {
1706         return false;
1707     }
1708     napi_valuetype valueType = napi_undefined;
1709     napi_typeof(env, argv, &valueType);
1710     if (valueType == napi_undefined || valueType == napi_null) {
1711         return false;
1712     }
1713     return true;
1714 }
1715 
GetResourceName(napi_env env,const char * name)1716 napi_value GetResourceName(napi_env env, const char *name)
1717 {
1718     napi_value resourceName = nullptr;
1719     napi_create_string_utf8(env, name, NAPI_AUTO_LENGTH, &resourceName);
1720     return resourceName;
1721 }
1722 
CheckEccCommonParamSpecBase(napi_env env,HcfEccCommParamsSpec * blob)1723 static bool CheckEccCommonParamSpecBase(napi_env env, HcfEccCommParamsSpec *blob)
1724 {
1725     if (blob->a.data == nullptr || blob->a.len == 0) {
1726         LOGE("Invalid blob a!");
1727         return false;
1728     }
1729     if (blob->b.data == nullptr || blob->b.len == 0) {
1730         LOGE("Invalid blob b!");
1731         return false;
1732     }
1733     if (blob->n.data == nullptr || blob->n.len == 0) {
1734         LOGE("Invalid blob n!");
1735         return false;
1736     }
1737     return true;
1738 }
1739 
CheckEccCommonParamSpec(napi_env env,HcfEccCommParamsSpec * blob)1740 static bool CheckEccCommonParamSpec(napi_env env, HcfEccCommParamsSpec *blob)
1741 {
1742     if (blob == nullptr) {
1743         LOGE("Invalid blob!");
1744         return false;
1745     }
1746     if (!CheckEccCommonParamSpecBase(env, blob)) {
1747         LOGE("Invalid blob ecc commonParamSpec base!");
1748         return false;
1749     }
1750     if (blob->base.algName == nullptr) {
1751         LOGE("Invalid blob algName!");
1752         return false;
1753     }
1754     if (blob->field == nullptr) {
1755         LOGE("Invalid blob field!");
1756         return false;
1757     }
1758     if (blob->field->fieldType == nullptr) {
1759         LOGE("Invalid blob fieldType!");
1760         return false;
1761     }
1762     if (blob->g.x.data == nullptr || blob->g.x.len == 0) {
1763         LOGE("Invalid blob point x!");
1764         return false;
1765     }
1766     if (blob->g.y.data == nullptr || blob->g.y.len == 0) {
1767         LOGE("Invalid blob point y!");
1768         return false;
1769     }
1770     HcfECFieldFp *tmpField = reinterpret_cast<HcfECFieldFp *>(blob->field);
1771     if (tmpField->p.data == nullptr || tmpField->p.len == 0) {
1772         LOGE("Invalid blob p!");
1773         return false;
1774     }
1775     return true;
1776 }
1777 
ConvertEccCommonParamFieldFpToNapiValue(napi_env env,HcfEccCommParamsSpec * blob)1778 static napi_value ConvertEccCommonParamFieldFpToNapiValue(napi_env env, HcfEccCommParamsSpec *blob)
1779 {
1780     napi_value fieldFp;
1781     napi_value fieldType;
1782     napi_status status = napi_create_object(env, &fieldFp);
1783     if (status != napi_ok) {
1784         LOGE("create fieldFp failed!");
1785         return NapiGetNull(env);
1786     }
1787     size_t fieldTypeLength = HcfStrlen(blob->field->fieldType);
1788     if (!fieldTypeLength) {
1789         LOGE("fieldType is empty!");
1790         return NapiGetNull(env);
1791     }
1792     status = napi_create_string_utf8(env, blob->field->fieldType, fieldTypeLength, &fieldType);
1793     if (status != napi_ok) {
1794         LOGE("create object failed!");
1795         return NapiGetNull(env);
1796     }
1797     status = napi_set_named_property(env, fieldFp, "fieldType", fieldType);
1798     if (status != napi_ok) {
1799         LOGE("create object failed!");
1800         return NapiGetNull(env);
1801     }
1802     HcfECFieldFp *tmpField = reinterpret_cast<HcfECFieldFp *>(blob->field);
1803     napi_value p = ConvertBigIntToNapiValue(env, &(tmpField->p));
1804     if (p == nullptr) {
1805         LOGE("p is null!");
1806         return NapiGetNull(env);
1807     }
1808     status = napi_set_named_property(env, fieldFp, "p", p);
1809     if (status != napi_ok) {
1810         LOGE("create object failed!");
1811         return NapiGetNull(env);
1812     }
1813     return fieldFp;
1814 }
1815 
ConvertEccCommonParamPointToNapiValue(napi_env env,HcfEccCommParamsSpec * blob)1816 static napi_value ConvertEccCommonParamPointToNapiValue(napi_env env, HcfEccCommParamsSpec *blob)
1817 {
1818     napi_value point;
1819     napi_status status = napi_create_object(env, &point);
1820     if (status != napi_ok) {
1821         LOGE("create object failed!");
1822         return NapiGetNull(env);
1823     }
1824     napi_value x = ConvertBigIntToNapiValue(env, &(blob->g.x));
1825     if (x == nullptr) {
1826         LOGE("x is null!");
1827         return NapiGetNull(env);
1828     }
1829 
1830     napi_value y = ConvertBigIntToNapiValue(env, &(blob->g.y));
1831     if (y == nullptr) {
1832         LOGE("y is null!");
1833         return NapiGetNull(env);
1834     }
1835     status = napi_set_named_property(env, point, "x", x);
1836     if (status != napi_ok) {
1837         LOGE("create object failed!");
1838         return NapiGetNull(env);
1839     }
1840     status = napi_set_named_property(env, point, "y", y);
1841     if (status != napi_ok) {
1842         LOGE("create object failed!");
1843         return NapiGetNull(env);
1844     }
1845     return point;
1846 }
1847 
BuildSetNamedProperty(napi_env env,HcfBigInteger * number,const char * name,napi_value * instance)1848 bool BuildSetNamedProperty(napi_env env, HcfBigInteger *number, const char *name, napi_value *instance)
1849 {
1850     napi_value value = ConvertBigIntToNapiValue(env, number);
1851     napi_status status = napi_set_named_property(env, *instance, name, value);
1852     if (status != napi_ok) {
1853         LOGE("create value failed!");
1854         return false;
1855     }
1856     return true;
1857 }
1858 
BuildIntancePartertoNapiValueSon(napi_env env,napi_status status,HcfEccCommParamsSpec * blob,napi_value * instance)1859 static bool BuildIntancePartertoNapiValueSon(napi_env env, napi_status status, HcfEccCommParamsSpec *blob,
1860     napi_value *instance)
1861 {
1862     if (!BuildSetNamedProperty(env, &(blob->a), "a", instance)) {
1863         LOGE("build setNamedProperty a failed!");
1864         return false;
1865     }
1866     if (!BuildSetNamedProperty(env, &(blob->b), "b", instance)) {
1867         LOGE("build setNamedProperty b failed!");
1868         return false;
1869     }
1870     if (!BuildSetNamedProperty(env, &(blob->n), "n", instance)) {
1871         LOGE("build setNamedProperty n failed!");
1872         return false;
1873     }
1874     napi_value h;
1875     status = napi_create_int32(env, blob->h, &h);
1876     if (status != napi_ok) {
1877         LOGE("create h uint32 failed!");
1878         return false;
1879     }
1880     status = napi_set_named_property(env, *instance, "h", h);
1881     if (status != napi_ok) {
1882         LOGE("create h uint32 failed!");
1883         return false;
1884     }
1885     return true;
1886 }
1887 
BuildInstanceParterToNapiValue(napi_env env,HcfEccCommParamsSpec * blob,napi_value * instance)1888 static bool BuildInstanceParterToNapiValue(napi_env env, HcfEccCommParamsSpec *blob, napi_value *instance)
1889 {
1890     napi_value algName;
1891     size_t algNameLength = HcfStrlen(blob->base.algName);
1892     if (!algNameLength) {
1893         LOGE("algName is empty!");
1894         return false;
1895     }
1896     napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName);
1897     if (status != napi_ok) {
1898         LOGE("create algName failed!");
1899         return false;
1900     }
1901     napi_value specType;
1902     status = napi_create_uint32(env, blob->base.specType, &specType);
1903     if (status != napi_ok) {
1904         LOGE("create uint32 failed!");
1905         return false;
1906     }
1907     status = napi_set_named_property(env, *instance, "algName", algName);
1908     if (status != napi_ok) {
1909         LOGE("create set algName failed!");
1910         return false;
1911     }
1912     status = napi_set_named_property(env, *instance, "specType", specType);
1913     if (status != napi_ok) {
1914         LOGE("create set specType failed!");
1915         return false;
1916     }
1917     if (!BuildIntancePartertoNapiValueSon(env, status, blob, instance)) {
1918         LOGE("create intance parter napi value failed!");
1919         return false;
1920     }
1921     return true;
1922 }
1923 
ConvertEccCommParamsSpecToNapiValue(napi_env env,HcfEccCommParamsSpec * blob)1924 napi_value ConvertEccCommParamsSpecToNapiValue(napi_env env, HcfEccCommParamsSpec *blob)
1925 {
1926     if (!CheckEccCommonParamSpec(env, blob)) {
1927         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
1928         LOGE("Invalid blob!");
1929         return NapiGetNull(env);
1930     }
1931     napi_value instance;
1932     napi_status status = napi_create_object(env, &instance);
1933     if (status != napi_ok) {
1934         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!"));
1935         LOGE("create object failed!");
1936         return NapiGetNull(env);
1937     }
1938     napi_value point = ConvertEccCommonParamPointToNapiValue(env, blob);
1939     if (point == NapiGetNull(env)) {
1940         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "covert commonParam failed!"));
1941         LOGE("Covert commonParam failed!");
1942         return NapiGetNull(env);
1943     }
1944     napi_value field = ConvertEccCommonParamFieldFpToNapiValue(env, blob);
1945     if (field == NapiGetNull(env)) {
1946         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "covert commonParam fieldFp failed!"));
1947         LOGE("Covert commonParam fieldFp failed!");
1948         return NapiGetNull(env);
1949     }
1950     if (!BuildInstanceParterToNapiValue(env, blob, &instance)) {
1951         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!"));
1952         LOGE("Build object failed!");
1953         return NapiGetNull(env);
1954     }
1955     status = napi_set_named_property(env, instance, "field", field);
1956     if (status != napi_ok) {
1957         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set fieldFp failed!"));
1958         LOGE("set fieldFp failed!");
1959         return NapiGetNull(env);
1960     }
1961     status = napi_set_named_property(env, instance, "g", point);
1962     if (status != napi_ok) {
1963         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "set g failed!"));
1964         LOGE("set g failed!");
1965         return NapiGetNull(env);
1966     }
1967     return instance;
1968 }
1969 
BuildDhInstanceToNapiValueSub(napi_env env,HcfDhCommParamsSpec * blob,napi_value * instance)1970 static bool BuildDhInstanceToNapiValueSub(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance)
1971 {
1972     if (!BuildSetNamedProperty(env, &(blob->p), "p", instance)) {
1973         LOGE("build setNamedProperty a failed!");
1974         return false;
1975     }
1976     if (!BuildSetNamedProperty(env, &(blob->g), "g", instance)) {
1977         LOGE("build setNamedProperty b failed!");
1978         return false;
1979     }
1980     napi_value length;
1981     napi_status status = napi_create_int32(env, blob->length, &length);
1982     if (status != napi_ok) {
1983         LOGE("create length number failed!");
1984         return false;
1985     }
1986     status = napi_set_named_property(env, *instance, "l", length);
1987     if (status != napi_ok) {
1988         LOGE("create length number failed!");
1989         return false;
1990     }
1991     return true;
1992 }
1993 
BuildDhInstanceToNapiValue(napi_env env,HcfDhCommParamsSpec * blob,napi_value * instance)1994 static bool BuildDhInstanceToNapiValue(napi_env env, HcfDhCommParamsSpec *blob, napi_value *instance)
1995 {
1996     napi_value algName;
1997     size_t algNameLength = HcfStrlen(blob->base.algName);
1998     if (!algNameLength) {
1999         LOGE("algName is empty!");
2000         return false;
2001     }
2002     napi_status status = napi_create_string_utf8(env, blob->base.algName, algNameLength, &algName);
2003     if (status != napi_ok) {
2004         LOGE("create algName failed!");
2005         return false;
2006     }
2007     napi_value specType;
2008     status = napi_create_uint32(env, blob->base.specType, &specType);
2009     if (status != napi_ok) {
2010         LOGE("create uint32 failed!");
2011         return false;
2012     }
2013     status = napi_set_named_property(env, *instance, "algName", algName);
2014     if (status != napi_ok) {
2015         LOGE("create set algName failed!");
2016         return false;
2017     }
2018     status = napi_set_named_property(env, *instance, "specType", specType);
2019     if (status != napi_ok) {
2020         LOGE("create set specType failed!");
2021         return false;
2022     }
2023     if (!BuildDhInstanceToNapiValueSub(env, blob, instance)) {
2024         LOGE("create intance parter napi value failed!");
2025         return false;
2026     }
2027     return true;
2028 }
2029 
CheckDhCommonParamSpec(napi_env env,HcfDhCommParamsSpec * blob)2030 static bool CheckDhCommonParamSpec(napi_env env, HcfDhCommParamsSpec *blob)
2031 {
2032     if (blob == nullptr) {
2033         LOGE("Invalid blob!");
2034         return false;
2035     }
2036     if (blob->base.algName == nullptr) {
2037         LOGE("Invalid blob algName!");
2038         return false;
2039     }
2040     if (blob->p.data == nullptr || blob->p.len == 0) {
2041         LOGE("Invalid blob a!");
2042         return false;
2043     }
2044     if (blob->g.data == nullptr || blob->g.len == 0) {
2045         LOGE("Invalid blob point x!");
2046         return false;
2047     }
2048     return true;
2049 }
2050 
ConvertDhCommParamsSpecToNapiValue(napi_env env,HcfDhCommParamsSpec * blob)2051 napi_value ConvertDhCommParamsSpecToNapiValue(napi_env env, HcfDhCommParamsSpec *blob)
2052 {
2053     if (!CheckDhCommonParamSpec(env, blob)) {
2054         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "Invalid blob!"));
2055         LOGE("Invalid blob!");
2056         return NapiGetNull(env);
2057     }
2058     napi_value instance;
2059     napi_status status = napi_create_object(env, &instance);
2060     if (status != napi_ok) {
2061         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create object failed!"));
2062         LOGE("create object failed!");
2063         return NapiGetNull(env);
2064     }
2065     if (!BuildDhInstanceToNapiValue(env, blob, &instance)) {
2066         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "build object failed!"));
2067         LOGE("Build object failed!");
2068         return NapiGetNull(env);
2069     }
2070     return instance;
2071 }
2072 }  // namespace CryptoFramework
2073 }  // namespace OHOS
2074