• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "jsi_api.h"
17 #include "jsi_api_common.h"
18 #include "jsi_api_errcode.h"
19 #include "jsi_utils.h"
20 #include "jsi_list.h"
21 #include "securec.h"
22 #include "jsi.h"
23 #include "jsi_types.h"
24 #include "log.h"
25 
26 namespace OHOS {
27 namespace ACELite {
28 
CreateRandom(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)29 JSIValue CryptoFrameworkLiteModule::CreateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
30 {
31     (void)argsNum;
32     (void)thisVal;
33     (void)args;
34     HcfRand *randObj = nullptr;
35     HcfResult res = HcfRandCreate(&randObj);
36     if (res != HCF_SUCCESS) {
37         LOGE("CreateRandom is randObj err %d!", res);
38         return ThrowErrorCodeResult(res);
39     }
40 
41     res = ListAddObjNode(JSI_ALG_RAND, (uint32_t)randObj);
42     if (res != HCF_SUCCESS) {
43         LOGE("rand add node is %d err!", res);
44         HcfObjDestroy(static_cast<void *>(randObj));
45         return ThrowErrorCodeResult(res);
46     }
47 
48     JSIValue serviceObj = JSI::CreateObject();
49     JSIValue generateRandom = JSI::CreateFunction(GenerateRandom);
50     JSIValue generateRandomSync = JSI::CreateFunction(GenerateRandomSync);
51     JSIValue setSeed = JSI::CreateFunction(SetSeed);
52 
53     JSI::SetNamedProperty(serviceObj, "generateRandom", generateRandom);
54     JSI::SetNamedProperty(serviceObj, "generateRandomSync", generateRandomSync);
55     JSI::SetNamedProperty(serviceObj, "setSeed", setSeed);
56     JSI::SetNumberProperty(serviceObj, "randObj", (double)(uint32_t)randObj);
57     JSI::ReleaseValueList(generateRandom, generateRandomSync, setSeed, ARGS_END);
58 
59     return serviceObj;
60 }
61 
GenerateRandom(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)62 JSIValue CryptoFrameworkLiteModule::GenerateRandom(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
63 {
64     if ((args == nullptr) || (argsNum != ARRAY_MAX_SIZE) || (args[ARRAY_INDEX_ONE] == nullptr)) {
65         LOGE("GenerateRandom params is err!");
66         return JSI::CreateUndefined();
67     }
68 
69     HcfRand *randObj = reinterpret_cast<HcfRand *>((uint32_t)JSI::GetNumberProperty(thisVal, "randObj"));
70     if (randObj == nullptr) {
71         LOGE("GenerateRandom randObj is null!");
72         CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined());
73         return JSI::CreateUndefined();
74     }
75 
76     int32_t numBytes = (int32_t)JSI::ValueToNumber(args[0]);
77     if (numBytes <= 0) {
78         LOGE("GenerateRandom numBytes too small!");
79         CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], HCF_INVALID_PARAMS, JSI::CreateUndefined());
80         return JSI::CreateUndefined();
81     }
82     HcfBlob randBlob = { .data = nullptr, .len = 0 };
83     HcfResult res = randObj->generateRandom(randObj, numBytes, &randBlob);
84     if (res != HCF_SUCCESS) {
85         LOGE("GenerateRandom randObj not is success!");
86         CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], res, JSI::CreateUndefined());
87         return JSI::CreateUndefined();
88     }
89 
90     JSIValue outVlaue = ConstructJSIReturnResult(&randBlob);
91     CallbackErrorCodeOrDataResult(thisVal, args[ARRAY_INDEX_ONE], res, outVlaue);
92     HcfBlobDataClearAndFree(&randBlob);
93 
94     return JSI::CreateUndefined();
95 }
96 
GenerateRandomSync(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)97 JSIValue CryptoFrameworkLiteModule::GenerateRandomSync(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
98 {
99     if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) {
100         LOGE("GenerateRandomSync params is err");
101         return ThrowErrorCodeResult(HCF_INVALID_PARAMS);
102     }
103 
104     HcfRand *randObj = reinterpret_cast<HcfRand *>((uint32_t)JSI::GetNumberProperty(thisVal, "randObj"));
105     if (randObj == nullptr) {
106         LOGE("GenerateRandom randObj is null!!");
107         return ThrowErrorCodeResult(HCF_INVALID_PARAMS);
108     }
109 
110     int32_t numBytes = (int32_t)JSI::ValueToNumber(args[0]);
111     if (numBytes <= 0) {
112         LOGE("GenerateRandomSync numBytes too small!");
113         return ThrowErrorCodeResult(HCF_INVALID_PARAMS);
114     }
115     HcfBlob randBlob = { .data = nullptr, .len = 0 };
116     HcfResult res = randObj->generateRandom(randObj, numBytes, &randBlob);
117     if (res != HCF_SUCCESS) {
118         LOGE("GenerateRandomSync randObj not is success!");
119         HcfBlobDataClearAndFree(&randBlob);
120         return ThrowErrorCodeResult(res);
121     }
122     JSIValue randomSyncData = ConstructJSIReturnResult(&randBlob);
123     HcfBlobDataClearAndFree(&randBlob);
124 
125     return randomSyncData;
126 }
127 
SetSeed(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)128 JSIValue CryptoFrameworkLiteModule::SetSeed(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
129 {
130     HcfRand *randObj = reinterpret_cast<HcfRand *>((uint32_t)JSI::GetNumberProperty(thisVal, "randObj"));
131     if (randObj == nullptr) {
132         LOGE("SetSeed randObj is null!!");
133         return ThrowErrorCodeResult(HCF_INVALID_PARAMS);
134     }
135     if ((args == nullptr) || (argsNum != ARRAY_INDEX_ONE)) {
136         LOGE("SetSeed params is null");
137         return ThrowErrorCodeResult(HCF_INVALID_PARAMS);
138     }
139     JSIValue inVlaue = JSI::GetNamedProperty(args[ARRAY_INDEX_ZERO], "data");
140     HcfBlob seedBlob = { .data = nullptr, .len = 0 };
141     HcfResult errCode = ParseUint8ArrayToBlob(inVlaue, &seedBlob);
142     if (errCode != HCF_SUCCESS) {
143         LOGE("SetSeed seedBlob is null!");
144         return ThrowErrorCodeResult(HCF_ERR_MALLOC);
145     }
146 
147     HcfResult res = randObj->setSeed(randObj, &seedBlob);
148     HcfBlobDataClearAndFree(&seedBlob);
149     if (res != HCF_SUCCESS) {
150         LOGE("setSeed randObj not is success!");
151         return ThrowErrorCodeResult(res);
152     }
153 
154     return ThrowErrorCodeResult(HCF_SUCCESS);
155 }
156 
RandomDestroy(void)157 void CryptoFrameworkLiteModule::RandomDestroy(void)
158 {
159     ListDestroy(JSI_ALG_RAND);
160 }
161 
162 }  // namespace ACELite
163 }  // namespace OHOS
164