1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci  *Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci  *Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci  *you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci  *You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci  *    http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci  *Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci  *distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci  *See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci  *limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <securec.h>
17094332d3Sopenharmony_ci
18094332d3Sopenharmony_ci#include "v1_0/ihuks.h"
19094332d3Sopenharmony_ci#include "v1_0/ihuks_types.h"
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci#include "huks_hdi_passthrough_adapter.h"
22094332d3Sopenharmony_ci#include "huks_hdi_template.h"
23094332d3Sopenharmony_ci#include "huks_sa_type.h"
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ci#define HDF_LOG_TAG    huks_hdi_service
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cistruct HuksService {
28094332d3Sopenharmony_ci    struct IHuks interface;
29094332d3Sopenharmony_ci};
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cistatic int32_t HuksModuleInit(struct IHuks *self)
32094332d3Sopenharmony_ci{
33094332d3Sopenharmony_ci    (void)self;
34094332d3Sopenharmony_ci    return HuksHdiAdapterModuleInit();
35094332d3Sopenharmony_ci}
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_cistatic int32_t HuksModuleDestroy(struct IHuks *self)
38094332d3Sopenharmony_ci{
39094332d3Sopenharmony_ci    (void)self;
40094332d3Sopenharmony_ci    return HuksHdiAdapterModuleDestroy();
41094332d3Sopenharmony_ci}
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_cistatic int32_t HuksGenerateKey(struct IHuks *self, const struct HuksBlob *keyAlias, const struct HuksParamSet *paramSet,
44094332d3Sopenharmony_ci    const struct HuksBlob *keyIn, struct HuksBlob *encKeyOut)
45094332d3Sopenharmony_ci{
46094332d3Sopenharmony_ci    (void)self;
47094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
48094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_GENERATEKEY(keyAlias, paramSet, keyIn, encKeyOut, ret, HuksHdiAdapterGenerateKey)
49094332d3Sopenharmony_ci    return ret;
50094332d3Sopenharmony_ci}
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_cistatic int32_t HuksImportKey(struct IHuks *self, const struct HuksBlob *keyAlias, const struct HuksBlob *key,
53094332d3Sopenharmony_ci    const struct HuksParamSet *paramSet, struct HuksBlob *encKeyOut)
54094332d3Sopenharmony_ci{
55094332d3Sopenharmony_ci    (void)self;
56094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
57094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_IMPORTKEY(keyAlias, key, paramSet, encKeyOut, ret, HuksHdiAdapterImportKey)
58094332d3Sopenharmony_ci    return ret;
59094332d3Sopenharmony_ci}
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_cistatic int32_t HuksImportWrappedKey(struct IHuks *self, const struct HuksBlob *wrappingKeyAlias,
62094332d3Sopenharmony_ci    const struct HuksBlob *wrappingEncKey, const struct HuksBlob *wrappedKeyData, const struct HuksParamSet *paramSet,
63094332d3Sopenharmony_ci    struct HuksBlob *encKeyOut)
64094332d3Sopenharmony_ci{
65094332d3Sopenharmony_ci    (void)self;
66094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
67094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_IMPORTWRAPPEDKEY(wrappingKeyAlias, wrappingEncKey, wrappedKeyData, paramSet, encKeyOut, ret,
68094332d3Sopenharmony_ci        HuksHdiAdapterImportWrappedKey)
69094332d3Sopenharmony_ci    return ret;
70094332d3Sopenharmony_ci}
71094332d3Sopenharmony_ci
72094332d3Sopenharmony_cistatic int32_t HuksExportPublicKey(struct IHuks *self, const struct HuksBlob *encKey,
73094332d3Sopenharmony_ci    const struct HuksParamSet *paramSet, struct HuksBlob *keyOut)
74094332d3Sopenharmony_ci{
75094332d3Sopenharmony_ci    (void)self;
76094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
77094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_EXPORTPUBLICKEY(encKey, paramSet, keyOut, ret, HuksHdiAdapterExportPublicKey)
78094332d3Sopenharmony_ci    return ret;
79094332d3Sopenharmony_ci}
80094332d3Sopenharmony_ci
81094332d3Sopenharmony_cistatic int32_t HuksInit(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
82094332d3Sopenharmony_ci    struct HuksBlob *handle, struct HuksBlob *token)
83094332d3Sopenharmony_ci{
84094332d3Sopenharmony_ci    (void)self;
85094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
86094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_INIT(encKey, paramSet, handle, token, ret, HuksHdiAdapterInit)
87094332d3Sopenharmony_ci    return ret;
88094332d3Sopenharmony_ci}
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_cistatic int32_t HuksUpdate(struct IHuks *self, const struct HuksBlob *handle, const struct HuksParamSet *paramSet,
91094332d3Sopenharmony_ci    const struct HuksBlob *inData, struct HuksBlob *outData)
92094332d3Sopenharmony_ci{
93094332d3Sopenharmony_ci    (void)self;
94094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
95094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_UPDATE(handle, paramSet, inData, outData, ret, HuksHdiAdapterUpdate)
96094332d3Sopenharmony_ci    return ret;
97094332d3Sopenharmony_ci}
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_cistatic int32_t HuksFinish(struct IHuks *self, const struct HuksBlob *handle, const struct HuksParamSet *paramSet,
100094332d3Sopenharmony_ci    const struct HuksBlob *inData, struct HuksBlob *outData)
101094332d3Sopenharmony_ci{
102094332d3Sopenharmony_ci    (void)self;
103094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
104094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_FINISH(handle, paramSet, inData, outData, ret, HuksHdiAdapterFinish)
105094332d3Sopenharmony_ci    return ret;
106094332d3Sopenharmony_ci}
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_cistatic int32_t HuksAbort(struct IHuks *self, const struct HuksBlob *handle, const struct HuksParamSet *paramSet)
109094332d3Sopenharmony_ci{
110094332d3Sopenharmony_ci    (void)self;
111094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
112094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_ABORT(handle, paramSet, ret, HuksHdiAdapterAbort)
113094332d3Sopenharmony_ci    return ret;
114094332d3Sopenharmony_ci}
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_cistatic int32_t HuksCheckKeyValidity(struct IHuks *self, const struct HuksParamSet *paramSet,
117094332d3Sopenharmony_ci    const struct HuksBlob *encKey)
118094332d3Sopenharmony_ci{
119094332d3Sopenharmony_ci    (void)self;
120094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
121094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_CHECKKEYVALIDITY(paramSet, encKey, ret, HuksHdiAdapterGetKeyProperties)
122094332d3Sopenharmony_ci    return ret;
123094332d3Sopenharmony_ci}
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_cistatic int32_t HuksAttestKey(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
126094332d3Sopenharmony_ci    struct HuksBlob *certChain)
127094332d3Sopenharmony_ci{
128094332d3Sopenharmony_ci    (void)self;
129094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
130094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_ATTESTKEY(encKey, paramSet, certChain, ret, HuksHdiAdapterAttestKey)
131094332d3Sopenharmony_ci    return ret;
132094332d3Sopenharmony_ci}
133094332d3Sopenharmony_ci
134094332d3Sopenharmony_cistatic int32_t HuksGenerateRandom(struct IHuks *self, const struct HuksParamSet *paramSet, struct HuksBlob *random)
135094332d3Sopenharmony_ci{
136094332d3Sopenharmony_ci    (void)self;
137094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
138094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_GENERATERANDOM(paramSet, random, ret, HuksHdiAdapterGenerateRandom)
139094332d3Sopenharmony_ci    return ret;
140094332d3Sopenharmony_ci}
141094332d3Sopenharmony_ci
142094332d3Sopenharmony_cistatic int32_t HuksSign(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
143094332d3Sopenharmony_ci    const struct HuksBlob *srcData, struct HuksBlob *signature)
144094332d3Sopenharmony_ci{
145094332d3Sopenharmony_ci    (void)self;
146094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
147094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_SIGN(encKey, paramSet, srcData, signature, ret, HuksHdiAdapterSign)
148094332d3Sopenharmony_ci    return ret;
149094332d3Sopenharmony_ci}
150094332d3Sopenharmony_ci
151094332d3Sopenharmony_cistatic int32_t HuksVerify(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
152094332d3Sopenharmony_ci    const struct HuksBlob *srcData, const struct HuksBlob *signature)
153094332d3Sopenharmony_ci{
154094332d3Sopenharmony_ci    (void)self;
155094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
156094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_VERIFY(encKey, paramSet, srcData, signature, ret, HuksHdiAdapterVerify)
157094332d3Sopenharmony_ci    return ret;
158094332d3Sopenharmony_ci}
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_cistatic int32_t HuksEncrypt(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
161094332d3Sopenharmony_ci    const struct HuksBlob *plainText, struct HuksBlob *cipherText)
162094332d3Sopenharmony_ci{
163094332d3Sopenharmony_ci    (void)self;
164094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
165094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_ENCRYPT(encKey, paramSet, plainText, cipherText, ret, HuksHdiAdapterEncrypt)
166094332d3Sopenharmony_ci    return ret;
167094332d3Sopenharmony_ci}
168094332d3Sopenharmony_ci
169094332d3Sopenharmony_cistatic int32_t HuksDecrypt(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
170094332d3Sopenharmony_ci    const struct HuksBlob *cipherText, struct HuksBlob *plainText)
171094332d3Sopenharmony_ci{
172094332d3Sopenharmony_ci    (void)self;
173094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
174094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_DECRYPT(encKey, paramSet, cipherText, plainText, ret, HuksHdiAdapterDecrypt)
175094332d3Sopenharmony_ci    return ret;
176094332d3Sopenharmony_ci}
177094332d3Sopenharmony_ci
178094332d3Sopenharmony_cistatic int32_t HuksAgreeKey(struct IHuks *self, const struct HuksParamSet *paramSet,
179094332d3Sopenharmony_ci    const struct HuksBlob *encPrivateKey, const struct HuksBlob *peerPublicKey, struct HuksBlob *agreedKey)
180094332d3Sopenharmony_ci{
181094332d3Sopenharmony_ci    (void)self;
182094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
183094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_AGREEKEY(paramSet, encPrivateKey, peerPublicKey, agreedKey, ret, HuksHdiAdapterAgreeKey)
184094332d3Sopenharmony_ci    return ret;
185094332d3Sopenharmony_ci}
186094332d3Sopenharmony_ci
187094332d3Sopenharmony_cistatic int32_t HuksDeriveKey(struct IHuks *self, const struct HuksParamSet *paramSet, const struct HuksBlob *encKdfKey,
188094332d3Sopenharmony_ci    struct HuksBlob *derivedKey)
189094332d3Sopenharmony_ci{
190094332d3Sopenharmony_ci    (void)self;
191094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
192094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_DERIVEKEY(paramSet, encKdfKey, derivedKey, ret, HuksHdiAdapterDeriveKey)
193094332d3Sopenharmony_ci    return ret;
194094332d3Sopenharmony_ci}
195094332d3Sopenharmony_ci
196094332d3Sopenharmony_cistatic int32_t HuksMac(struct IHuks *self, const struct HuksBlob *encKey, const struct HuksParamSet *paramSet,
197094332d3Sopenharmony_ci    const struct HuksBlob *srcData, struct HuksBlob *mac)
198094332d3Sopenharmony_ci{
199094332d3Sopenharmony_ci    (void)self;
200094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
201094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_MAC(encKey, paramSet, srcData, mac, ret, HuksHdiAdapterMac)
202094332d3Sopenharmony_ci    return ret;
203094332d3Sopenharmony_ci}
204094332d3Sopenharmony_ci
205094332d3Sopenharmony_cistatic int32_t HuksUpgradeKey(struct IHuks *self, const struct HuksBlob *encOldKey, const struct HuksParamSet *paramSet,
206094332d3Sopenharmony_ci    struct HuksBlob *encNewKey)
207094332d3Sopenharmony_ci{
208094332d3Sopenharmony_ci    (void)self;
209094332d3Sopenharmony_ci    int32_t ret = HUKS_FAILURE;
210094332d3Sopenharmony_ci    HDI_CONVERTER_FUNC_UPGRADEKEY(encOldKey, paramSet, encNewKey, ret, HuksHdiAdapterUpgradeKey)
211094332d3Sopenharmony_ci    return ret;
212094332d3Sopenharmony_ci}
213094332d3Sopenharmony_ci
214094332d3Sopenharmony_cistatic int32_t HuksExportChipsetPlatformPublicKey(struct IHuks *self, const struct HuksBlob *salt,
215094332d3Sopenharmony_ci    enum HuksChipsetPlatformDecryptScene scene, struct HuksBlob *publicKey)
216094332d3Sopenharmony_ci{
217094332d3Sopenharmony_ci    (void)self;
218094332d3Sopenharmony_ci    (void)salt;
219094332d3Sopenharmony_ci    (void)scene;
220094332d3Sopenharmony_ci    (void)publicKey;
221094332d3Sopenharmony_ci    return HUKS_ERROR_API_NOT_SUPPORTED;
222094332d3Sopenharmony_ci}
223094332d3Sopenharmony_ci
224094332d3Sopenharmony_cistatic int32_t HuksGetVersion(struct IHuks *self, uint32_t *majorVer, uint32_t *minorVer)
225094332d3Sopenharmony_ci{
226094332d3Sopenharmony_ci    *majorVer = IHUKS_MAJOR_VERSION;
227094332d3Sopenharmony_ci    *minorVer = IHUKS_MINOR_VERSION;
228094332d3Sopenharmony_ci    return HUKS_SUCCESS;
229094332d3Sopenharmony_ci}
230094332d3Sopenharmony_ci
231094332d3Sopenharmony_cistruct IHuks *HuksImplGetInstance(void)
232094332d3Sopenharmony_ci{
233094332d3Sopenharmony_ci    struct HuksService *service = (struct HuksService *)malloc(sizeof(struct HuksService));
234094332d3Sopenharmony_ci    if (service == NULL) {
235094332d3Sopenharmony_ci        return NULL;
236094332d3Sopenharmony_ci    }
237094332d3Sopenharmony_ci
238094332d3Sopenharmony_ci    service->interface.ModuleInit = HuksModuleInit;
239094332d3Sopenharmony_ci    service->interface.ModuleDestroy = HuksModuleDestroy;
240094332d3Sopenharmony_ci    service->interface.GenerateKey = HuksGenerateKey;
241094332d3Sopenharmony_ci    service->interface.ImportKey = HuksImportKey;
242094332d3Sopenharmony_ci    service->interface.ImportWrappedKey = HuksImportWrappedKey;
243094332d3Sopenharmony_ci    service->interface.ExportPublicKey = HuksExportPublicKey;
244094332d3Sopenharmony_ci    service->interface.Init = HuksInit;
245094332d3Sopenharmony_ci    service->interface.Update = HuksUpdate;
246094332d3Sopenharmony_ci    service->interface.Finish = HuksFinish;
247094332d3Sopenharmony_ci    service->interface.Abort = HuksAbort;
248094332d3Sopenharmony_ci    service->interface.CheckKeyValidity = HuksCheckKeyValidity;
249094332d3Sopenharmony_ci    service->interface.AttestKey = HuksAttestKey;
250094332d3Sopenharmony_ci    service->interface.GenerateRandom = HuksGenerateRandom;
251094332d3Sopenharmony_ci    service->interface.Sign = HuksSign;
252094332d3Sopenharmony_ci    service->interface.Verify = HuksVerify;
253094332d3Sopenharmony_ci    service->interface.Encrypt = HuksEncrypt;
254094332d3Sopenharmony_ci    service->interface.Decrypt = HuksDecrypt;
255094332d3Sopenharmony_ci    service->interface.AgreeKey = HuksAgreeKey;
256094332d3Sopenharmony_ci    service->interface.DeriveKey = HuksDeriveKey;
257094332d3Sopenharmony_ci    service->interface.Mac = HuksMac;
258094332d3Sopenharmony_ci    service->interface.ExportChipsetPlatformPublicKey = HuksExportChipsetPlatformPublicKey;
259094332d3Sopenharmony_ci    service->interface.UpgradeKey = HuksUpgradeKey;
260094332d3Sopenharmony_ci    service->interface.GetVersion = HuksGetVersion;
261094332d3Sopenharmony_ci    return &service->interface;
262094332d3Sopenharmony_ci}
263094332d3Sopenharmony_ci
264094332d3Sopenharmony_civoid HuksImplRelease(struct IHuks *instance)
265094332d3Sopenharmony_ci{
266094332d3Sopenharmony_ci    if (instance == NULL) {
267094332d3Sopenharmony_ci        return;
268094332d3Sopenharmony_ci    }
269094332d3Sopenharmony_ci    (void)HuksReleaseCoreEngine();
270094332d3Sopenharmony_ci    free(instance);
271094332d3Sopenharmony_ci}
272