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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16
17 #include "hks_lite_api.h"
18 #include "hks_log.h"
19 #include "jsi.h"
20 #include "jsi_types.h"
21
22 #include "hks_api.h"
23 #include "hks_param.h"
24 #include "hks_lite_api_common.h"
25
26 namespace OHOS {
27 namespace ACELite {
HksCallInitSession(const struct HksBlob * aliasBlob,struct HksParamSet * paramSet,struct HksBlob * handle)28 static int32_t HksCallInitSession(const struct HksBlob *aliasBlob, struct HksParamSet *paramSet,
29 struct HksBlob *handle)
30 {
31 handle->data = static_cast<uint8_t *>(HksMalloc(HKS_HANDLE_SIZE));
32 if (handle->data == nullptr) {
33 HKS_LOG_E("malloc handle data failed");
34 return HKS_ERROR_MALLOC_FAIL;
35 }
36 handle->size = HKS_HANDLE_SIZE;
37 int32_t ret = InitHuksModule();
38 if (ret != HKS_SUCCESS) {
39 HKS_LOG_E("init huks failed");
40 return ret;
41 }
42 ret = HksInit(aliasBlob, paramSet, handle, nullptr);
43 return ret;
44 }
45
initSession(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)46 JSIValue HksLiteModule::initSession(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
47 {
48 JSIValue undefValue = JSI::CreateUndefined();
49 uint32_t minNumArgs = 3;
50 if (argsNum < minNumArgs || args == nullptr) {
51 HKS_LOG_E("initSession args invalid, args num(%{public}d).", argsNum);
52 return undefValue;
53 }
54
55 struct HksBlob aliasBlob = { 0, nullptr };
56 struct HksParamSet *paramSet = nullptr;
57 struct HksBlob handle = { 0, nullptr };
58 int32_t ret;
59 do {
60 ret = HksParseKeyAlias(args, ARGS_INDEX_0, &aliasBlob);
61 if (ret != HKS_SUCCESS) {
62 break;
63 }
64 ret = HksParseParamSetWithAdd(args, ARGS_INDEX_1, ¶mSet, nullptr, 0);
65 if (ret != HKS_SUCCESS) {
66 break;
67 }
68 ret = HksCallInitSession(&aliasBlob, paramSet, &handle);
69 } while (0);
70 if (ret == HKS_SUCCESS) {
71 struct HksLiteApiResult result = { nullptr, &handle, false };
72 HksCallbackResultSuccess(thisVal, args[ARGS_INDEX_2], &result);
73 } else {
74 HksCallbackResultFailure(thisVal, args[ARGS_INDEX_2], ret);
75 }
76
77 HKS_FREE_BLOB(aliasBlob);
78 HKS_FREE_BLOB(handle);
79 HksFreeParamSet(¶mSet);
80 return undefValue;
81 }
82 }
83 }