1526fd984Sopenharmony_ci/*
2526fd984Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3526fd984Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4526fd984Sopenharmony_ci * you may not use this file except in compliance with the License.
5526fd984Sopenharmony_ci * You may obtain a copy of the License at
6526fd984Sopenharmony_ci *
7526fd984Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
8526fd984Sopenharmony_ci *
9526fd984Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10526fd984Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11526fd984Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12526fd984Sopenharmony_ci * See the License for the specific language governing permissions and
13526fd984Sopenharmony_ci * limitations under the License.
14526fd984Sopenharmony_ci */
15526fd984Sopenharmony_ci
16526fd984Sopenharmony_ci#include "native_huks_api.h"
17526fd984Sopenharmony_ci
18526fd984Sopenharmony_ci#include "hks_api.h"
19526fd984Sopenharmony_ci#include "hks_errcode_adapter.h"
20526fd984Sopenharmony_ci#include "native_huks_api_adapter.h"
21526fd984Sopenharmony_ci
22526fd984Sopenharmony_cistatic struct OH_Huks_Result ConvertApiResult(int32_t ret)
23526fd984Sopenharmony_ci{
24526fd984Sopenharmony_ci    struct HksResult result = HksConvertErrCode(ret);
25526fd984Sopenharmony_ci    return *((struct OH_Huks_Result *)(&result));
26526fd984Sopenharmony_ci}
27526fd984Sopenharmony_ci
28526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_GetSdkVersion(struct OH_Huks_Blob *sdkVersion)
29526fd984Sopenharmony_ci{
30526fd984Sopenharmony_ci    int32_t result = HksGetSdkVersion((struct HksBlob *) sdkVersion);
31526fd984Sopenharmony_ci    return ConvertApiResult(result);
32526fd984Sopenharmony_ci}
33526fd984Sopenharmony_ci
34526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_GenerateKeyItem(const struct OH_Huks_Blob *keyAlias,
35526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSetIn, struct OH_Huks_ParamSet *paramSetOut)
36526fd984Sopenharmony_ci{
37526fd984Sopenharmony_ci    int32_t result = HksGenerateKey((const struct HksBlob *) keyAlias,
38526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSetIn, (struct HksParamSet *) paramSetOut);
39526fd984Sopenharmony_ci    return ConvertApiResult(result);
40526fd984Sopenharmony_ci}
41526fd984Sopenharmony_ci
42526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_ImportKeyItem(const struct OH_Huks_Blob *keyAlias,
43526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *key)
44526fd984Sopenharmony_ci{
45526fd984Sopenharmony_ci    int32_t result = HksImportKey((const struct HksBlob *) keyAlias,
46526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSet, (const struct HksBlob *) key);
47526fd984Sopenharmony_ci    return ConvertApiResult(result);
48526fd984Sopenharmony_ci}
49526fd984Sopenharmony_ci
50526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_ImportWrappedKeyItem(const struct OH_Huks_Blob *keyAlias,
51526fd984Sopenharmony_ci    const struct OH_Huks_Blob *wrappingKeyAlias, const struct OH_Huks_ParamSet *paramSet,
52526fd984Sopenharmony_ci    const struct OH_Huks_Blob *wrappedKeyData)
53526fd984Sopenharmony_ci{
54526fd984Sopenharmony_ci    int32_t result = HksImportWrappedKey((const struct HksBlob *) keyAlias,
55526fd984Sopenharmony_ci        (const struct HksBlob *) wrappingKeyAlias, (const struct HksParamSet *) paramSet,
56526fd984Sopenharmony_ci        (const struct HksBlob *) wrappedKeyData);
57526fd984Sopenharmony_ci    return ConvertApiResult(result);
58526fd984Sopenharmony_ci}
59526fd984Sopenharmony_ci
60526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_ExportPublicKeyItem(const struct OH_Huks_Blob *keyAlias,
61526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_Blob *key)
62526fd984Sopenharmony_ci{
63526fd984Sopenharmony_ci    int32_t result = HksExportPublicKey((const struct HksBlob *) keyAlias,
64526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSet, (struct HksBlob *) key);
65526fd984Sopenharmony_ci    return ConvertApiResult(result);
66526fd984Sopenharmony_ci}
67526fd984Sopenharmony_ci
68526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_DeleteKeyItem(const struct OH_Huks_Blob *keyAlias,
69526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet)
70526fd984Sopenharmony_ci{
71526fd984Sopenharmony_ci    int32_t result = HksDeleteKey((const struct HksBlob *) keyAlias, (const struct HksParamSet *) paramSet);
72526fd984Sopenharmony_ci    return ConvertApiResult(result);
73526fd984Sopenharmony_ci}
74526fd984Sopenharmony_ci
75526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_GetKeyItemParamSet(const struct OH_Huks_Blob *keyAlias,
76526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSetIn, struct OH_Huks_ParamSet *paramSetOut)
77526fd984Sopenharmony_ci{
78526fd984Sopenharmony_ci    int32_t result = HksGetKeyParamSet((const struct HksBlob *) keyAlias,
79526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSetIn, (struct HksParamSet *) paramSetOut);
80526fd984Sopenharmony_ci    return ConvertApiResult(result);
81526fd984Sopenharmony_ci}
82526fd984Sopenharmony_ci
83526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_IsKeyItemExist(const struct OH_Huks_Blob *keyAlias,
84526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet)
85526fd984Sopenharmony_ci{
86526fd984Sopenharmony_ci    int32_t result = HksKeyExist((const struct HksBlob *) keyAlias, (const struct HksParamSet *) paramSet);
87526fd984Sopenharmony_ci    return ConvertApiResult(result);
88526fd984Sopenharmony_ci}
89526fd984Sopenharmony_ci
90526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_AttestKeyItem(const struct OH_Huks_Blob *keyAlias,
91526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_CertChain *certChain)
92526fd984Sopenharmony_ci{
93526fd984Sopenharmony_ci    int32_t result = HuksAttestAdapter(keyAlias, paramSet, certChain, false);
94526fd984Sopenharmony_ci    return ConvertApiResult(result);
95526fd984Sopenharmony_ci}
96526fd984Sopenharmony_ci
97526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_AnonAttestKeyItem(const struct OH_Huks_Blob *keyAlias,
98526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_CertChain *certChain)
99526fd984Sopenharmony_ci{
100526fd984Sopenharmony_ci    int32_t result = HuksAttestAdapter(keyAlias, paramSet, certChain, true);
101526fd984Sopenharmony_ci    return ConvertApiResult(result);
102526fd984Sopenharmony_ci}
103526fd984Sopenharmony_ci
104526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_InitSession(const struct OH_Huks_Blob *keyAlias,
105526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_Blob *handle, struct OH_Huks_Blob *token)
106526fd984Sopenharmony_ci{
107526fd984Sopenharmony_ci    int32_t result = HksInit((const struct HksBlob *) keyAlias,
108526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSet, (struct HksBlob *) handle, (struct HksBlob *) token);
109526fd984Sopenharmony_ci    return ConvertApiResult(result);
110526fd984Sopenharmony_ci}
111526fd984Sopenharmony_ci
112526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_UpdateSession(const struct OH_Huks_Blob *handle,
113526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
114526fd984Sopenharmony_ci{
115526fd984Sopenharmony_ci    int32_t result = HksUpdate((const struct HksBlob *) handle,
116526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSet, (const struct HksBlob *) inData, (struct HksBlob *) outData);
117526fd984Sopenharmony_ci    return ConvertApiResult(result);
118526fd984Sopenharmony_ci}
119526fd984Sopenharmony_ci
120526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_FinishSession(const struct OH_Huks_Blob *handle,
121526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
122526fd984Sopenharmony_ci{
123526fd984Sopenharmony_ci    int32_t result = HksFinish((const struct HksBlob *) handle,
124526fd984Sopenharmony_ci        (const struct HksParamSet *) paramSet, (const struct HksBlob *) inData, (struct HksBlob *) outData);
125526fd984Sopenharmony_ci    return ConvertApiResult(result);
126526fd984Sopenharmony_ci}
127526fd984Sopenharmony_ci
128526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_AbortSession(const struct OH_Huks_Blob *handle,
129526fd984Sopenharmony_ci    const struct OH_Huks_ParamSet *paramSet)
130526fd984Sopenharmony_ci{
131526fd984Sopenharmony_ci    int32_t result = HksAbort((const struct HksBlob *) handle, (const struct HksParamSet *) paramSet);
132526fd984Sopenharmony_ci    return ConvertApiResult(result);
133526fd984Sopenharmony_ci}
134526fd984Sopenharmony_ci
135526fd984Sopenharmony_cistruct OH_Huks_Result OH_Huks_ListAliases(const struct OH_Huks_ParamSet *paramSet,
136526fd984Sopenharmony_ci    struct OH_Huks_KeyAliasSet **outData)
137526fd984Sopenharmony_ci{
138526fd984Sopenharmony_ci    int32_t result = HksListAliases((const struct HksParamSet *) paramSet, (struct HksKeyAliasSet **) outData);
139526fd984Sopenharmony_ci    return ConvertApiResult(result);
140526fd984Sopenharmony_ci}
141