1 /*
2 * Copyright (c) 2022 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);
94 return ConvertApiResult(result);
95 }
96
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)97 struct OH_Huks_Result OH_Huks_InitSession(const struct OH_Huks_Blob *keyAlias,
98 const struct OH_Huks_ParamSet *paramSet, struct OH_Huks_Blob *handle, struct OH_Huks_Blob *token)
99 {
100 int32_t result = HksInit((const struct HksBlob *) keyAlias,
101 (const struct HksParamSet *) paramSet, (struct HksBlob *) handle, (struct HksBlob *) token);
102 return ConvertApiResult(result);
103 }
104
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)105 struct OH_Huks_Result OH_Huks_UpdateSession(const struct OH_Huks_Blob *handle,
106 const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
107 {
108 int32_t result = HksUpdate((const struct HksBlob *) handle,
109 (const struct HksParamSet *) paramSet, (const struct HksBlob *) inData, (struct HksBlob *) outData);
110 return ConvertApiResult(result);
111 }
112
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)113 struct OH_Huks_Result OH_Huks_FinishSession(const struct OH_Huks_Blob *handle,
114 const struct OH_Huks_ParamSet *paramSet, const struct OH_Huks_Blob *inData, struct OH_Huks_Blob *outData)
115 {
116 int32_t result = HksFinish((const struct HksBlob *) handle,
117 (const struct HksParamSet *) paramSet, (const struct HksBlob *) inData, (struct HksBlob *) outData);
118 return ConvertApiResult(result);
119 }
120
OH_Huks_AbortSession(const struct OH_Huks_Blob * handle,const struct OH_Huks_ParamSet * paramSet)121 struct OH_Huks_Result OH_Huks_AbortSession(const struct OH_Huks_Blob *handle,
122 const struct OH_Huks_ParamSet *paramSet)
123 {
124 int32_t result = HksAbort((const struct HksBlob *) handle, (const struct HksParamSet *) paramSet);
125 return ConvertApiResult(result);
126 }
127