1 /*
2 * Copyright (c) 2022-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 "native_huks_api.h"
17
18 #include "hks_api.h"
19 #include "hks_errcode_adapter.h"
20 #include "native_huks_api_adapter.h"
21
ConvertApiResult(int32_t ret)22 static struct OH_Huks_Result ConvertApiResult(int32_t ret)
23 {
24 struct HksResult result = HksConvertErrCode(ret);
25 return *((struct OH_Huks_Result *)(&result));
26 }
27
OH_Huks_GetSdkVersion(struct OH_Huks_Blob * sdkVersion)28 struct OH_Huks_Result OH_Huks_GetSdkVersion(struct OH_Huks_Blob *sdkVersion)
29 {
30 int32_t result = HksGetSdkVersion((struct HksBlob *) sdkVersion);
31 return ConvertApiResult(result);
32 }
33
OH_Huks_GenerateKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSetIn,struct OH_Huks_ParamSet * paramSetOut)34 struct OH_Huks_Result OH_Huks_GenerateKeyItem(const struct OH_Huks_Blob *keyAlias,
35 const struct OH_Huks_ParamSet *paramSetIn, struct OH_Huks_ParamSet *paramSetOut)
36 {
37 int32_t result = HksGenerateKey((const struct HksBlob *) keyAlias,
38 (const struct HksParamSet *) paramSetIn, (struct HksParamSet *) paramSetOut);
39 return ConvertApiResult(result);
40 }
41
OH_Huks_ImportKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * key)42 struct OH_Huks_Result OH_Huks_ImportKeyItem(const struct OH_Huks_Blob *keyAlias,
43 const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *key)
44 {
45 int32_t result = HksImportKey((const struct HksBlob *) keyAlias,
46 (const struct HksParamSet *) paramSet, (const struct HksBlob *) key);
47 return ConvertApiResult(result);
48 }
49
OH_Huks_ImportWrappedKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_Blob * wrappingKeyAlias,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * wrappedKeyData)50 struct OH_Huks_Result OH_Huks_ImportWrappedKeyItem(const struct OH_Huks_Blob *keyAlias,
51 const struct OH_Huks_Blob *wrappingKeyAlias, const struct OH_Huks_ParamSet *paramSet,
52 const struct OH_Huks_Blob *wrappedKeyData)
53 {
54 int32_t result = HksImportWrappedKey((const struct HksBlob *) keyAlias,
55 (const struct HksBlob *) wrappingKeyAlias, (const struct HksParamSet *) paramSet,
56 (const struct HksBlob *) wrappedKeyData);
57 return ConvertApiResult(result);
58 }
59
OH_Huks_ExportPublicKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet,struct OH_Huks_Blob * key)60 struct OH_Huks_Result OH_Huks_ExportPublicKeyItem(const struct OH_Huks_Blob *keyAlias,
61 const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_Blob *key)
62 {
63 int32_t result = HksExportPublicKey((const struct HksBlob *) keyAlias,
64 (const struct HksParamSet *) paramSet, (struct HksBlob *) key);
65 return ConvertApiResult(result);
66 }
67
OH_Huks_DeleteKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet)68 struct OH_Huks_Result OH_Huks_DeleteKeyItem(const struct OH_Huks_Blob *keyAlias,
69 const struct OH_Huks_ParamSet *paramSet)
70 {
71 int32_t result = HksDeleteKey((const struct HksBlob *) keyAlias, (const struct HksParamSet *) paramSet);
72 return ConvertApiResult(result);
73 }
74
OH_Huks_GetKeyItemParamSet(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSetIn,struct OH_Huks_ParamSet * paramSetOut)75 struct OH_Huks_Result OH_Huks_GetKeyItemParamSet(const struct OH_Huks_Blob *keyAlias,
76 const struct OH_Huks_ParamSet *paramSetIn, struct OH_Huks_ParamSet *paramSetOut)
77 {
78 int32_t result = HksGetKeyParamSet((const struct HksBlob *) keyAlias,
79 (const struct HksParamSet *) paramSetIn, (struct HksParamSet *) paramSetOut);
80 return ConvertApiResult(result);
81 }
82
OH_Huks_IsKeyItemExist(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet)83 struct OH_Huks_Result OH_Huks_IsKeyItemExist(const struct OH_Huks_Blob *keyAlias,
84 const struct OH_Huks_ParamSet *paramSet)
85 {
86 int32_t result = HksKeyExist((const struct HksBlob *) keyAlias, (const struct HksParamSet *) paramSet);
87 return ConvertApiResult(result);
88 }
89
OH_Huks_AttestKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet,struct OH_Huks_CertChain * certChain)90 struct OH_Huks_Result OH_Huks_AttestKeyItem(const struct OH_Huks_Blob *keyAlias,
91 const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_CertChain *certChain)
92 {
93 int32_t result = HuksAttestAdapter(keyAlias, paramSet, certChain, false);
94 return ConvertApiResult(result);
95 }
96
OH_Huks_AnonAttestKeyItem(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet,struct OH_Huks_CertChain * certChain)97 struct OH_Huks_Result OH_Huks_AnonAttestKeyItem(const struct OH_Huks_Blob *keyAlias,
98 const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_CertChain *certChain)
99 {
100 int32_t result = HuksAttestAdapter(keyAlias, paramSet, certChain, true);
101 return ConvertApiResult(result);
102 }
103
OH_Huks_InitSession(const struct OH_Huks_Blob * keyAlias,const struct OH_Huks_ParamSet * paramSet,struct OH_Huks_Blob * handle,struct OH_Huks_Blob * token)104 struct OH_Huks_Result OH_Huks_InitSession(const struct OH_Huks_Blob *keyAlias,
105 const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_Blob *handle, struct OH_Huks_Blob *token)
106 {
107 int32_t result = HksInit((const struct HksBlob *) keyAlias,
108 (const struct HksParamSet *) paramSet, (struct HksBlob *) handle, (struct HksBlob *) token);
109 return ConvertApiResult(result);
110 }
111
OH_Huks_UpdateSession(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)112 struct OH_Huks_Result OH_Huks_UpdateSession(const struct OH_Huks_Blob *handle,
113 const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
114 {
115 int32_t result = HksUpdate((const struct HksBlob *) handle,
116 (const struct HksParamSet *) paramSet, (const struct HksBlob *) inData, (struct HksBlob *) outData);
117 return ConvertApiResult(result);
118 }
119
OH_Huks_FinishSession(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet,const struct OH_Huks_Blob * inData,struct OH_Huks_Blob * outData)120 struct OH_Huks_Result OH_Huks_FinishSession(const struct OH_Huks_Blob *handle,
121 const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
122 {
123 int32_t result = HksFinish((const struct HksBlob *) handle,
124 (const struct HksParamSet *) paramSet, (const struct HksBlob *) inData, (struct HksBlob *) outData);
125 return ConvertApiResult(result);
126 }
127
OH_Huks_AbortSession(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet)128 struct OH_Huks_Result OH_Huks_AbortSession(const struct OH_Huks_Blob *handle,
129 const struct OH_Huks_ParamSet *paramSet)
130 {
131 int32_t result = HksAbort((const struct HksBlob *) handle, (const struct HksParamSet *) paramSet);
132 return ConvertApiResult(result);
133 }
134
OH_Huks_ListAliases(const struct OH_Huks_ParamSet * paramSet,struct OH_Huks_KeyAliasSet ** outData)135 struct OH_Huks_Result OH_Huks_ListAliases(const struct OH_Huks_ParamSet *paramSet,
136 struct OH_Huks_KeyAliasSet **outData)
137 {
138 int32_t result = HksListAliases((const struct HksParamSet *) paramSet, (struct HksKeyAliasSet **) outData);
139 return ConvertApiResult(result);
140 }
141