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