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 
16 #include "hks_lite_api.h"
17 #include "hks_log.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 
25 namespace OHOS {
26 namespace ACELite {
HksCallGenerateKey(const struct HksBlob * aliasBlob,const struct HksParamSet * paramSet)27 static int32_t HksCallGenerateKey(const struct HksBlob *aliasBlob, const struct HksParamSet *paramSet)
28 {
29     int32_t ret = InitHuksModule();
30     if (ret != HKS_SUCCESS) {
31         HKS_LOG_E("init huks failed");
32         return ret;
33     }
34     ret = HksGenerateKey(aliasBlob, paramSet, nullptr);
35     return ret;
36 }
37 
generateKeyItem(const JSIValue thisVal,const JSIValue * args,uint8_t argsNum)38 JSIValue HksLiteModule::generateKeyItem(const JSIValue thisVal, const JSIValue* args, uint8_t argsNum)
39 {
40     JSIValue undefValue = JSI::CreateUndefined();
41     uint32_t minNumArgs = 3;
42     if (argsNum < minNumArgs || args == nullptr) {
43         HKS_LOG_E("generateKeyItem args invalid, args num(%{public}d).", argsNum);
44         return undefValue;
45     }
46 
47     struct HksBlob aliasBlob = { 0, nullptr };
48     struct HksParamSet *paramSet = nullptr;
49     int32_t ret;
50     do {
51         ret = HksParseKeyAlias(args, ARGS_INDEX_0, &aliasBlob);
52         if (ret != HKS_SUCCESS) {
53             break;
54         }
55         ret = HksParseParamSetWithAdd(args, ARGS_INDEX_1, &paramSet, nullptr, 0);
56         if (ret != HKS_SUCCESS) {
57             break;
58         }
59         ret = HksCallGenerateKey(&aliasBlob, paramSet);
60     } while (0);
61     if (ret == HKS_SUCCESS) {
62         struct HksLiteApiResult result = { nullptr, nullptr, false };
63         HksCallbackResultSuccess(thisVal, args[ARGS_INDEX_2], &result);
64     } else {
65         HksCallbackResultFailure(thisVal, args[ARGS_INDEX_2], ret);
66     }
67 
68     HKS_FREE_BLOB(aliasBlob);
69     HksFreeParamSet(&paramSet);
70     return undefValue;
71 }
72 }
73 }