• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_sm2_ec_signature.h"
17 
18 #include <string>
19 #include "securec.h"
20 #include "log.h"
21 #include "memory.h"
22 #include "napi_crypto_framework_defines.h"
23 #include "napi_utils.h"
24 
25 namespace OHOS {
26 namespace CryptoFramework {
NapiSm2EcSignature()27 NapiSm2EcSignature::NapiSm2EcSignature() {}
~NapiSm2EcSignature()28 NapiSm2EcSignature::~NapiSm2EcSignature() {}
29 
GetSm2EcSignatureDataSpecFromNapiValue(napi_env env,napi_value arg,Sm2EcSignatureDataSpec ** returnSpec)30 static bool GetSm2EcSignatureDataSpecFromNapiValue(napi_env env, napi_value arg, Sm2EcSignatureDataSpec **returnSpec)
31 {
32     if ((env == nullptr) || (arg == nullptr) || (returnSpec == nullptr)) {
33         LOGE("Invalid params.");
34         return false;
35     }
36     Sm2EcSignatureDataSpec *tempSpec =
37         static_cast<Sm2EcSignatureDataSpec *>(HcfMalloc(sizeof(Sm2EcSignatureDataSpec), 0));
38     if (tempSpec == nullptr) {
39         LOGE("Malloc failed!");
40         return false;
41     }
42     napi_value rCoordinate = GetDetailAsyKeySpecValue(env, arg, SM2_EC_SIGNATURE_PARAM_R);
43     napi_value sCoordinate = GetDetailAsyKeySpecValue(env, arg, SM2_EC_SIGNATURE_PARAM_S);
44     if ((rCoordinate == nullptr) || (sCoordinate == nullptr)) {
45         LOGE("Invalid params!");
46         DestroySm2EcSignatureSpec(tempSpec);
47         return false;
48     }
49     bool ret = GetBigIntFromNapiValue(env, rCoordinate, &tempSpec->rCoordinate);
50     if (!ret) {
51         LOGE("Failed to get valid r coordinate.");
52         DestroySm2EcSignatureSpec(tempSpec);
53         return false;
54     }
55     ret = GetBigIntFromNapiValue(env, sCoordinate, &tempSpec->sCoordinate);
56     if (!ret) {
57         LOGE("Failed to get valid s coordinate.");
58         DestroySm2EcSignatureSpec(tempSpec);
59         return false;
60     }
61     *returnSpec = tempSpec;
62     return true;
63 }
64 
JsGenEcSignatureData(napi_env env,napi_callback_info info)65 napi_value NapiSm2EcSignature::JsGenEcSignatureData(napi_env env, napi_callback_info info)
66 {
67     size_t expectedArgc = PARAMS_NUM_ONE;
68     size_t argc = ARGS_SIZE_ONE;
69     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
70     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
71     if (argc != expectedArgc) {
72         LOGE("The input args num is invalid.");
73         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
74         return nullptr;
75     }
76     Sm2EcSignatureDataSpec *spec = nullptr;
77     if (!GetSm2EcSignatureDataSpecFromNapiValue(env, argv[0], &spec)) {
78         LOGE("Failed to get spec.");
79         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get spec."));
80         return nullptr;
81     }
82     HcfBlob *output = static_cast<HcfBlob *>(HcfMalloc(sizeof(HcfBlob), 0));
83     if (output == NULL) {
84         LOGE("Failed to allocate HcfBlob memory!");
85         DestroySm2EcSignatureSpec(spec);
86         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "Failed to allocate memory."));
87         return nullptr;
88     }
89     HcfResult res = HcfGenEcSignatureDataBySpec(spec, output);
90     if (res != HCF_SUCCESS) {
91         LOGE("Gen cipher text by spec fail.");
92         HcfFree(output);
93         output = nullptr;
94         DestroySm2EcSignatureSpec(spec);
95         napi_throw(env, GenerateBusinessError(env, res, "gen cipher text by spec fail."));
96         return nullptr;
97     }
98     napi_value instance = ConvertObjectBlobToNapiValue(env, output);
99     HcfBlobDataFree(output);
100     HcfFree(output);
101     output = nullptr;
102     DestroySm2EcSignatureSpec(spec);
103     return instance;
104 }
105 
CheckSm2CipherTextSpec(Sm2EcSignatureDataSpec * spec)106 static bool CheckSm2CipherTextSpec(Sm2EcSignatureDataSpec *spec)
107 {
108     if (spec == nullptr) {
109         LOGE("Invalid spec!");
110         return false;
111     }
112     if (spec->rCoordinate.data == nullptr || spec->rCoordinate.len == 0) {
113         LOGE("Invalid rCoordinate!");
114         return false;
115     }
116     if (spec->sCoordinate.data == nullptr || spec->sCoordinate.len == 0) {
117         LOGE("Invalid sCoordinate!");
118         return false;
119     }
120     return true;
121 }
122 
BuildSm2CipherTextSpecToNapiValue(napi_env env,Sm2EcSignatureDataSpec * spec,napi_value * instance)123 static bool BuildSm2CipherTextSpecToNapiValue(napi_env env, Sm2EcSignatureDataSpec *spec, napi_value *instance)
124 {
125     if (!BuildSetNamedProperty(env, &(spec->rCoordinate), SM2_EC_SIGNATURE_PARAM_R.c_str(), instance)) {
126         LOGE("Build rCoordinate failed!");
127         return false;
128     }
129     if (!BuildSetNamedProperty(env, &(spec->sCoordinate), SM2_EC_SIGNATURE_PARAM_S.c_str(), instance)) {
130         LOGE("Build sCoordinate failed!");
131         return false;
132     }
133     return true;
134 }
135 
ConvertSm2CipherTextSpecToNapiValue(napi_env env,Sm2EcSignatureDataSpec * spec)136 static napi_value ConvertSm2CipherTextSpecToNapiValue(napi_env env, Sm2EcSignatureDataSpec *spec)
137 {
138     if (!CheckSm2CipherTextSpec(spec)) {
139         LOGE("Invalid spec!");
140         napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "Invalid spec!"));
141         return NapiGetNull(env);
142     }
143     napi_value instance;
144     napi_status status = napi_create_object(env, &instance);
145     if (status != napi_ok) {
146         LOGE("Create object failed!");
147         napi_throw(env, GenerateBusinessError(env, HCF_ERR_MALLOC, "create object failed!"));
148         return NapiGetNull(env);
149     }
150     if (!BuildSm2CipherTextSpecToNapiValue(env, spec, &instance)) {
151         LOGE("Build object failed!");
152         napi_throw(env, GenerateBusinessError(env, HCF_ERR_NAPI, "build object failed!"));
153         return NapiGetNull(env);
154     }
155     return instance;
156 }
157 
JsGenEcSignatureDataSpec(napi_env env,napi_callback_info info)158 napi_value NapiSm2EcSignature::JsGenEcSignatureDataSpec(napi_env env, napi_callback_info info)
159 {
160     size_t expectedArgc = PARAMS_NUM_ONE;
161     size_t argc = ARGS_SIZE_ONE;
162     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
163     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
164     if (argc != expectedArgc) {
165         LOGE("The input args num is invalid.");
166         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "The input args num is invalid."));
167         return nullptr;
168     }
169     HcfBlob *cipherText = GetBlobFromNapiUint8Arr(env, argv[0]);
170     if (cipherText == nullptr) {
171         LOGE("Failed to get cipherText.");
172         napi_throw(env, GenerateBusinessError(env, HCF_INVALID_PARAMS, "failed to get cipherText."));
173         return nullptr;
174     }
175     Sm2EcSignatureDataSpec *returnSpec = nullptr;
176     HcfResult res = HcfGenEcSignatureSpecByData(cipherText, &returnSpec);
177     if (res != HCF_SUCCESS) {
178         LOGE("Get cipher text spec fail.");
179         HcfBlobDataFree(cipherText);
180         HcfFree(cipherText);
181         cipherText = nullptr;
182         napi_throw(env, GenerateBusinessError(env, res, "get cipher text spec fail."));
183         return nullptr;
184     }
185     napi_value instance = ConvertSm2CipherTextSpecToNapiValue(env, returnSpec);
186     DestroySm2EcSignatureSpec(returnSpec);
187     HcfBlobDataFree(cipherText);
188     HcfFree(cipherText);
189     cipherText = nullptr;
190     return instance;
191 }
192 
SignatureUtilsConstructor(napi_env env,napi_callback_info info)193 napi_value NapiSm2EcSignature::SignatureUtilsConstructor(napi_env env, napi_callback_info info)
194 {
195     napi_value thisVar = nullptr;
196     size_t argc = ARGS_SIZE_ONE;
197     napi_value argv[ARGS_SIZE_ONE] = { nullptr };
198     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
199     return thisVar;
200 }
201 
SignatureUtilsConstructorClass(napi_env env)202 napi_value NapiSm2EcSignature::SignatureUtilsConstructorClass(napi_env env)
203 {
204     napi_value cons = nullptr;
205     napi_property_descriptor clzDes[] = {
206         DECLARE_NAPI_STATIC_FUNCTION("genEccSignature", NapiSm2EcSignature::JsGenEcSignatureData),
207         DECLARE_NAPI_STATIC_FUNCTION("genEccSignatureSpec", NapiSm2EcSignature::JsGenEcSignatureDataSpec),
208     };
209     NAPI_CALL(env, napi_define_class(env, "SignatureUtils", NAPI_AUTO_LENGTH,
210         NapiSm2EcSignature::SignatureUtilsConstructor,
211         nullptr, sizeof(clzDes) / sizeof(clzDes[0]), clzDes, &cons));
212     return cons;
213 }
214 
DefineNapiSm2EcSignatureJSClass(napi_env env,napi_value exports)215 void NapiSm2EcSignature::DefineNapiSm2EcSignatureJSClass(napi_env env, napi_value exports)
216 {
217     napi_set_named_property(env, exports, "SignatureUtils", NapiSm2EcSignature::SignatureUtilsConstructorClass(env));
218 }
219 } // CryptoFramework
220 } // OHOS
221