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 {
28 // key size 512 plus aead 16
29 #define RESERVED_PADDING_AREA 528
30
31 typedef int32_t (*HksCallSessionFunc)(struct HksBlob *handle, struct HksParamSet *paramSet, struct HksBlob *inData,
32 struct HksBlob *outData);
33
HksCallUpdateSession(struct HksBlob * handle,struct HksParamSet * paramSet,struct HksBlob * inData,struct HksBlob * outData)34 static int32_t HksCallUpdateSession(struct HksBlob *handle, struct HksParamSet *paramSet, struct HksBlob *inData,
35 struct HksBlob *outData)
36 {
37 int32_t ret = InitHuksModule();
38 if (ret != HKS_SUCCESS) {
39 HKS_LOG_E("init huks failed");
40 return ret;
41 }
42 ret = HksUpdate(handle, paramSet, inData, outData);
43 return ret;
44 }
45
HksCallFinishSession(struct HksBlob * handle,struct HksParamSet * paramSet,struct HksBlob * inData,struct HksBlob * outData)46 static int32_t HksCallFinishSession(struct HksBlob *handle, struct HksParamSet *paramSet, struct HksBlob *inData,
47 struct HksBlob *outData)
48 {
49 int32_t ret = InitHuksModule();
50 if (ret != HKS_SUCCESS) {
51 HKS_LOG_E("init huks failed");
52 return ret;
53 }
54 ret = HksFinish(handle, paramSet, inData, outData);
55 return ret;
56 }
57
InitOutDataBuffer(struct HksBlob * inData,struct HksBlob * outData)58 static int32_t InitOutDataBuffer(struct HksBlob *inData, struct HksBlob *outData)
59 {
60 outData->size = inData->size + RESERVED_PADDING_AREA;
61 outData->data = static_cast<uint8_t *>(HksMalloc(outData->size));
62 if (outData->data == NULL) {
63 return HKS_ERROR_MALLOC_FAIL;
64 }
65 return HKS_SUCCESS;
66 }
67
InnerUpdateFinishSession(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum,HksCallSessionFunc func)68 static JSIValue InnerUpdateFinishSession(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum,
69 HksCallSessionFunc func)
70 {
71 JSIValue undefValue = JSI::CreateUndefined();
72 uint32_t minNumArgs = 3;
73 if ((argsNum < minNumArgs) || args == nullptr) {
74 HKS_LOG_E("updateSession args invalid, args num(%{public}d).", argsNum);
75 return undefValue;
76 }
77
78 struct HksParamSet *paramSet = nullptr;
79 struct HksBlob handle = { 0, nullptr };
80 struct HksBlob outData = { 0, nullptr };
81 struct HksBlob inData = { 0, nullptr };
82 int32_t ret;
83 do {
84 ret = HksParseHandle(args, ARGS_INDEX_0, &handle);
85 if (ret != HKS_SUCCESS) {
86 HKS_LOG_E("parse handle failed!");
87 break;
88 }
89
90 ret = HksParseParamSetWithAdd(args, ARGS_INDEX_1, ¶mSet, nullptr, 0);
91 if (ret != HKS_SUCCESS) {
92 HKS_LOG_E("parse paramset failed!");
93 break;
94 }
95 ret = HksParseInData(args, ARGS_INDEX_1, &inData);
96 if (ret != HKS_SUCCESS) {
97 HKS_LOG_E("parse indata failed!");
98 break;
99 }
100 ret = InitOutDataBuffer(&inData, &outData);
101 if (ret != HKS_SUCCESS) {
102 HKS_LOG_E("init outdata buffer failed!");
103 break;
104 }
105 ret = func(&handle, paramSet, &inData, &outData);
106 } while (0);
107 if (ret == HKS_SUCCESS) {
108 struct HksLiteApiResult result = { &outData, nullptr, false };
109 HksCallbackResultSuccess(thisVal, args[ARGS_INDEX_2], &result);
110 } else {
111 HksCallbackResultFailure(thisVal, args[ARGS_INDEX_2], ret);
112 }
113
114 HKS_FREE_BLOB(handle);
115 HKS_FREE_BLOB(inData);
116 HKS_FREE_BLOB(outData);
117 HksFreeParamSet(¶mSet);
118 return undefValue;
119 }
120
updateSession(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)121 JSIValue HksLiteModule::updateSession(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
122 {
123 return InnerUpdateFinishSession(thisVal, args, argsNum, HksCallUpdateSession);
124 }
125
finishSession(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)126 JSIValue HksLiteModule::finishSession(const JSIValue thisVal, const JSIValue *args, uint8_t argsNum)
127 {
128 return InnerUpdateFinishSession(thisVal, args, argsNum, HksCallFinishSession);
129 }
130 }
131 }