1 /*
2  * Copyright (C) 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_dh_key_util.h"
17 #include "securec.h"
18 #include "detailed_ecc_key_params.h"
19 #include "log.h"
20 
21 #include "napi_crypto_framework_defines.h"
22 #include "napi_utils.h"
23 #include "napi_key_pair.h"
24 #include "napi_pri_key.h"
25 #include "napi_pub_key.h"
26 
27 namespace OHOS {
28 namespace CryptoFramework {
NapiDHKeyUtil()29 NapiDHKeyUtil::NapiDHKeyUtil() {}
30 
~NapiDHKeyUtil()31 NapiDHKeyUtil::~NapiDHKeyUtil() {}
32 
JsGenDHCommonParamsSpec(napi_env env,napi_callback_info info)33 napi_value NapiDHKeyUtil::JsGenDHCommonParamsSpec(napi_env env, napi_callback_info info)
34 {
35     size_t expectedArgc = PARAMS_NUM_TWO;
36     size_t argc = ARGS_SIZE_TWO;
37     napi_value argv[ARGS_SIZE_TWO] = { nullptr };
38     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
39 
40     if ((argc != expectedArgc) && (argc != (expectedArgc - 1))) {
41         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
42         LOGE("The input args num is invalid.");
43         return nullptr;
44     }
45 
46     int32_t pLen = 0;
47     if (!GetInt32FromJSParams(env, argv[0], pLen)) {
48         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get pLen."));
49         LOGE("failed to get pLen.");
50         return NapiGetNull(env);
51     }
52 
53     int32_t skLen = 0;
54     if (argc == expectedArgc) {
55         if (!GetInt32FromJSParams(env, argv[1], skLen)) {
56             napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get skLen."));
57             LOGE("failed to get skLen.");
58             return NapiGetNull(env);
59         }
60     }
61     HcfDhCommParamsSpec *dhCommParamsSpec = nullptr;
62     if (HcfDhKeyUtilCreate(pLen, skLen, &dhCommParamsSpec) != HCF_SUCCESS) {
63         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "create c generator fail."));
64         LOGE("create c generator fail.");
65         return NapiGetNull(env);
66     }
67 
68     napi_value instance = ConvertDhCommParamsSpecToNapiValue(env, dhCommParamsSpec);
69     FreeDhCommParamsSpec(dhCommParamsSpec);
70     return instance;
71 }
72 
DHKeyUtilConstructor(napi_env env,napi_callback_info info)73 napi_value NapiDHKeyUtil::DHKeyUtilConstructor(napi_env env, napi_callback_info info)
74 {
75     napi_value thisVar = nullptr;
76     size_t argc = ARGS_SIZE_ONE;
77     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
78     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
79     return thisVar;
80 }
81 
GenDHCommonParamSpec(napi_env env)82 napi_value NapiDHKeyUtil::GenDHCommonParamSpec(napi_env env)
83 {
84     napi_value cons = nullptr;
85     napi_property_descriptor clzDes[] = {
86         DECLARE_NAPI_STATIC_FUNCTION("genDHCommonParamsSpec", NapiDHKeyUtil::JsGenDHCommonParamsSpec),
87     };
88     NAPI_CALL(env, napi_define_class(env, "DHKeyUtil", NAPI_AUTO_LENGTH, NapiDHKeyUtil::DHKeyUtilConstructor,
89         nullptr, sizeof(clzDes) / sizeof(clzDes[0]), clzDes, &cons));
90     return cons;
91 }
92 
DefineNapiDHKeyUtilJSClass(napi_env env,napi_value exports)93 void NapiDHKeyUtil::DefineNapiDHKeyUtilJSClass(napi_env env, napi_value exports)
94 {
95     napi_set_named_property(env, exports, "DHKeyUtil", NapiDHKeyUtil::GenDHCommonParamSpec(env));
96 }
97 } // CryptoFramework
98 } // OHOS
99