• 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 #define HUKS_DISABLE_LOG_AT_FILE_TO_REDUCE_ROM_SIZE
16 
17 #include "hks_lite_api.h"
18 #include "jsi.h"
19 #include "jsi_types.h"
20 
21 #include "hks_api.h"
22 #include "hks_param.h"
23 #include "hks_lite_api_common.h"
24 #include "hks_log.h"
25 
26 namespace OHOS {
27 namespace ACELite {
28 // key size 512 plus aead 16
29 #define RESERVED_PADDING_AREA 528
30 
HksCallImportKeyItem(const struct HksBlob * aliasBlob,const struct HksParamSet * paramSet,struct HksBlob * inData)31 static int32_t HksCallImportKeyItem(
32     const struct HksBlob *aliasBlob, const struct HksParamSet *paramSet, struct HksBlob *inData)
33 {
34     int32_t ret = InitHuksModule();
35     if (ret != HKS_SUCCESS) {
36         HKS_LOG_E("init huks failed");
37         return ret;
38     }
39     ret = HksImportKey(aliasBlob, paramSet, inData);
40     return ret;
41 }
42 
InitOutDataBuffer(struct HksBlob * inData,struct HksBlob * outData)43 static int32_t InitOutDataBuffer(struct HksBlob *inData, struct HksBlob *outData)
44 {
45     outData->size = inData->size + RESERVED_PADDING_AREA;
46     outData->data = static_cast<uint8_t *>(HksMalloc(outData->size));
47     if (outData->data == NULL) {
48         return HKS_ERROR_MALLOC_FAIL;
49     }
50     return HKS_SUCCESS;
51 }
52 
importKeyItem(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)53 JSIValue HksLiteModule::importKeyItem(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
54 {
55     JSIValue undefValue = JSI::CreateUndefined();
56     uint32_t minNumArgs = 3;
57     if (argsNum < minNumArgs || args == nullptr) {
58         HKS_LOG_E("importKeyItem args invalid, args num(%{public}d).", argsNum);
59         return undefValue;
60     }
61 
62     struct HksBlob aliasBlob = { 0, nullptr };
63     struct HksParamSet *paramSet = nullptr;
64     struct HksBlob outData = { 0, nullptr };
65     struct HksBlob inData = { 0, nullptr };
66     int32_t ret;
67     do {
68         ret = HksParseKeyAlias(args, ARGS_INDEX_0, &aliasBlob);
69         if (ret != HKS_SUCCESS) {
70             break;
71         }
72         ret = HksParseParamSetWithAdd(args, ARGS_INDEX_1, &paramSet, nullptr, 0);
73         if (ret != HKS_SUCCESS) {
74             break;
75         }
76         ret = HksParseInData(args, ARGS_INDEX_1, &inData);
77         if (ret != HKS_SUCCESS) {
78             HKS_LOG_E("parse indata failed!");
79             break;
80         }
81         ret = InitOutDataBuffer(&inData, &outData);
82         if (ret != HKS_SUCCESS) {
83             HKS_LOG_E("init outdata buffer failed!");
84             break;
85         }
86         ret = HksCallImportKeyItem(&aliasBlob, paramSet, &inData);
87     } while (0);
88     if (ret == HKS_SUCCESS) {
89         struct HksLiteApiResult result = { nullptr, nullptr, false };
90         HksCallbackResultSuccess(thisVal, args[ARGS_INDEX_2], &result);
91     } else {
92         HksCallbackResultFailure(thisVal, args[ARGS_INDEX_2], ret);
93     }
94 
95     HKS_FREE_BLOB(aliasBlob);
96     HKS_FREE_BLOB(inData);
97     HKS_FREE_BLOB(outData);
98     HksFreeParamSet(&paramSet);
99     return undefValue;
100 }
101 }
102 }