1 /*
2  * Copyright (c) 2020-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_mbedtls_engine.h"
17 
18 #ifdef HKS_CONFIG_FILE
19 #include HKS_CONFIG_FILE
20 #else
21 #include "hks_config.h"
22 #endif
23 
24 #include "hks_ability.h"
25 #include "hks_crypto_hal.h"
26 #include "hks_log.h"
27 #include "hks_template.h"
28 
EncryptCheckParam(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * cipherText)29 static int32_t EncryptCheckParam(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
30     const struct HksBlob *message, struct HksBlob *cipherText)
31 {
32     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
33     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(message), HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
34     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(cipherText), HKS_ERROR_INVALID_ARGUMENT, "Invalid param cipherText!")
35 
36     HKS_IF_NULL_LOGE_RETURN(usageSpec, HKS_ERROR_INVALID_ARGUMENT, "Invalid param usageSpec!")
37     return HKS_SUCCESS;
38 }
39 
HksCryptoHalHmac(const struct HksBlob * key,uint32_t digestAlg,const struct HksBlob * msg,struct HksBlob * mac)40 int32_t HksCryptoHalHmac(const struct HksBlob *key, uint32_t digestAlg, const struct HksBlob *msg,
41     struct HksBlob *mac)
42 {
43     if (CheckBlob(key) != HKS_SUCCESS || CheckBlob(msg) != HKS_SUCCESS || CheckBlob(mac) != HKS_SUCCESS) {
44         HKS_LOG_E("Crypt Hal Hmac invalid param");
45         return HKS_ERROR_INVALID_ARGUMENT;
46     }
47     Hmac func = (Hmac)GetAbility(HKS_CRYPTO_ABILITY_HMAC);
48     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
49     return func(key, digestAlg, msg, mac);
50 }
51 
HksCryptoHalHmacInit(const struct HksBlob * key,uint32_t digestAlg,void ** ctx)52 int32_t HksCryptoHalHmacInit(const struct HksBlob *key, uint32_t digestAlg, void **ctx)
53 {
54     if (CheckBlob(key) != HKS_SUCCESS || ctx == NULL) {
55         HKS_LOG_E("Crypt Hal Hmac init msg is NULL");
56         return HKS_ERROR_INVALID_ARGUMENT;
57     }
58 
59     HmacInit func = (HmacInit)GetAbility(HKS_CRYPTO_ABILITY_HMAC_INIT);
60     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
61 
62     return func(ctx, key, digestAlg);
63 }
64 
HksCryptoHalHmacUpdate(const struct HksBlob * chunk,void * ctx)65 int32_t HksCryptoHalHmacUpdate(const struct HksBlob *chunk, void *ctx)
66 {
67     if (CheckBlob(chunk) != HKS_SUCCESS || ctx == NULL) {
68         HKS_LOG_E("Crypt Hal Hmac update chunk is invalid param");
69         return HKS_ERROR_INVALID_ARGUMENT;
70     }
71 
72     HmacUpdate func = (HmacUpdate)GetAbility(HKS_CRYPTO_ABILITY_HMAC_UPDATE);
73     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
74 
75     return func(ctx, chunk);
76 }
77 
HksCryptoHalHmacFinal(const struct HksBlob * msg,void ** ctx,struct HksBlob * mac)78 int32_t HksCryptoHalHmacFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *mac)
79 {
80     if (msg == NULL || ctx == NULL || *ctx == NULL || CheckBlob(mac) != HKS_SUCCESS) {
81         HKS_LOG_E("Crypt Hal Hmac final msg or mac is NULL");
82         return HKS_ERROR_INVALID_ARGUMENT;
83     }
84 
85     HmacFinal func = (HmacFinal)GetAbility(HKS_CRYPTO_ABILITY_HMAC_FINAL);
86     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
87 
88     return func(ctx, msg, mac);
89 }
90 
HksCryptoHalHmacFreeCtx(void ** ctx)91 void HksCryptoHalHmacFreeCtx(void **ctx)
92 {
93     FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_HMAC_FREE_CTX);
94     if (func == NULL) {
95         HKS_LOG_E("CryptoHalHmacFreeCtx func is null");
96         return;
97     }
98 
99     func(ctx);
100 }
101 
102 #ifndef _CUT_AUTHENTICATE_
HksCryptoHalHash(uint32_t alg,const struct HksBlob * msg,struct HksBlob * hash)103 int32_t HksCryptoHalHash(uint32_t alg, const struct HksBlob *msg, struct HksBlob *hash)
104 {
105     Hash func = (Hash)GetAbility(HKS_CRYPTO_ABILITY_HASH);
106     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash func is null!")
107     return func(alg, msg, hash);
108 }
109 
HksCryptoHalHashInit(uint32_t alg,void ** ctx)110 int32_t HksCryptoHalHashInit(uint32_t alg, void **ctx)
111 {
112     HashInit func = (HashInit)GetAbility(HKS_CRYPTO_ABILITY_HASH_INIT);
113     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash init func is null!")
114 
115     return func(ctx, alg);
116 }
117 
HksCryptoHalHashUpdate(const struct HksBlob * msg,void * ctx)118 int32_t HksCryptoHalHashUpdate(const struct HksBlob *msg, void *ctx)
119 {
120     if (CheckBlob(msg) != HKS_SUCCESS || ctx == NULL) {
121         HKS_LOG_E("Crypt Hal Hash msg or ctx is NULL");
122         return HKS_ERROR_INVALID_ARGUMENT;
123     }
124 
125     HashUpdate func = (HashUpdate)GetAbility(HKS_CRYPTO_ABILITY_HASH_UPDATE);
126     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash update func is null!")
127 
128     return func(ctx, msg);
129 }
130 
HksCryptoHalHashFinal(const struct HksBlob * msg,void ** ctx,struct HksBlob * hash)131 int32_t HksCryptoHalHashFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *hash)
132 {
133     if (msg == NULL || CheckBlob(hash) != HKS_SUCCESS || ctx == NULL || *ctx == NULL) {
134         HKS_LOG_E("Crypt Hal Hash final msg or hash or ctx is NULL");
135         return HKS_ERROR_INVALID_ARGUMENT;
136     }
137 
138     HashFinal func = (HashFinal)GetAbility(HKS_CRYPTO_ABILITY_HASH_FINAL);
139     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash final func is null!")
140 
141     return func(ctx, msg, hash);
142 }
143 
HksCryptoHalHashFreeCtx(void ** ctx)144 void HksCryptoHalHashFreeCtx(void **ctx)
145 {
146     FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_HASH_FREE_CTX);
147     if (func == NULL) {
148         HKS_LOG_E("CryptoHalHashFreeCtx func is null");
149         return;
150     }
151 
152     func(ctx);
153 }
154 
155 #endif /* _CUT_AUTHENTICATE_ */
156 
HksCryptoHalBnExpMod(struct HksBlob * x,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n)157 int32_t HksCryptoHalBnExpMod(struct HksBlob *x, const struct HksBlob *a,
158     const struct HksBlob *e, const struct HksBlob *n)
159 {
160     BnExpMod func = (BnExpMod)GetAbility(HKS_CRYPTO_ABILITY_BN_EXP_MOD);
161     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
162     return func(x, a, e, n);
163 }
164 
165 #ifndef _CUT_AUTHENTICATE_
HksCryptoHalGenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)166 int32_t HksCryptoHalGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
167 {
168     if (spec == NULL || key == NULL) {
169         HKS_LOG_E("Crypt Hal GenerateKey msg or hash or ctx is NULL");
170         return HKS_ERROR_INVALID_ARGUMENT;
171     }
172 
173     GenerateKey func = (GenerateKey)GetAbility(HKS_CRYPTO_ABILITY_GENERATE_KEY(spec->algType));
174     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls GenerateKey func is null!")
175     return func(spec, key);
176 }
177 
HksCryptoHalGetPubKey(const struct HksBlob * keyIn,struct HksBlob * keyOut)178 int32_t HksCryptoHalGetPubKey(const struct HksBlob *keyIn, struct HksBlob *keyOut)
179 {
180     if (CheckBlob(keyIn) != HKS_SUCCESS || CheckBlob(keyOut) != HKS_SUCCESS) {
181         HKS_LOG_E("Invalid params!");
182         return HKS_ERROR_INVALID_ARGUMENT;
183     }
184 
185     /* KeyMaterialRsa, KeyMaterialEcc, KeyMaterial25519's size are same */
186     if (keyIn->size < sizeof(struct KeyMaterialRsa)) {
187         HKS_LOG_E("Crypt Hal getPubKey keyIn size is more smaller. size[%" LOG_PUBLIC "d]", keyIn->size);
188         return HKS_ERROR_INVALID_KEY_SIZE;
189     }
190 
191     struct KeyMaterialRsa *key = (struct KeyMaterialRsa *)(keyIn->data);
192     PubKey func = (PubKey)GetAbility(HKS_CRYPTO_ABILITY_GET_PUBLIC_KEY(key->keyAlg));
193     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
194     return func(keyIn, keyOut);
195 }
196 #endif /* _CUT_AUTHENTICATE_ */
197 
HksCryptoHalDeriveKey(const struct HksBlob * mainKey,const struct HksKeySpec * derivationSpec,struct HksBlob * derivedKey)198 int32_t HksCryptoHalDeriveKey(const struct HksBlob *mainKey,
199     const struct HksKeySpec *derivationSpec, struct HksBlob *derivedKey)
200 {
201     DeriveKey func = (DeriveKey)GetAbility(HKS_CRYPTO_ABILITY_DERIVE_KEY(derivationSpec->algType));
202     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls DeriveKey func is null!")
203     return func(mainKey, derivationSpec, derivedKey);
204 }
205 
206 #ifndef _CUT_AUTHENTICATE_
HksCryptoHalAgreeKey(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,const struct HksKeySpec * spec,struct HksBlob * sharedKey)207 int32_t HksCryptoHalAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey,
208     const struct HksKeySpec *spec, struct HksBlob *sharedKey)
209 {
210     if (CheckBlob(nativeKey) != HKS_SUCCESS || CheckBlob(pubKey) != HKS_SUCCESS || spec == NULL ||
211         CheckBlob(sharedKey) != HKS_SUCCESS) {
212         HKS_LOG_E("Crypt Hal AgreeKey param error");
213         return HKS_ERROR_INVALID_ARGUMENT;
214     }
215 
216     AgreeKey func = (AgreeKey)GetAbility(HKS_CRYPTO_ABILITY_AGREE_KEY(spec->algType));
217     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls AgreeKey func is null!")
218     return func(nativeKey, pubKey, spec, sharedKey);
219 }
220 
HksCryptoHalSign(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)221 int32_t HksCryptoHalSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
222     const struct HksBlob *message, struct HksBlob *signature)
223 {
224     if (CheckBlob(key) != HKS_SUCCESS || usageSpec == NULL || CheckBlob(message) != HKS_SUCCESS ||
225         CheckBlob(signature) != HKS_SUCCESS) {
226         HKS_LOG_E("Crypt Hal Sign param error");
227         return HKS_ERROR_INVALID_ARGUMENT;
228     }
229 
230     Sign func = (Sign)GetAbility(HKS_CRYPTO_ABILITY_SIGN(usageSpec->algType));
231     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
232     return func(key, usageSpec, message, signature);
233 }
234 
HksCryptoHalVerify(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)235 int32_t HksCryptoHalVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
236     const struct HksBlob *message, const struct HksBlob *signature)
237 {
238     if (CheckBlob(key) != HKS_SUCCESS || usageSpec == NULL || CheckBlob(message) != HKS_SUCCESS ||
239         CheckBlob(signature) != HKS_SUCCESS) {
240         HKS_LOG_E("Crypt Hal Verify param error");
241         return HKS_ERROR_INVALID_ARGUMENT;
242     }
243 
244     Verify func = (Verify)GetAbility(HKS_CRYPTO_ABILITY_VERIFY(usageSpec->algType));
245     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
246     return func(key, usageSpec, message, signature);
247 }
248 #endif /* _CUT_AUTHENTICATE_ */
249 
HksCryptoHalFillRandom(struct HksBlob * randomData)250 int32_t HksCryptoHalFillRandom(struct HksBlob *randomData)
251 {
252     FillRandom func = (FillRandom)GetAbility(HKS_CRYPTO_ABILITY_FILL_RANDOM);
253     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
254     return func(randomData);
255 }
256 
HksCryptoHalFillPrivRandom(struct HksBlob * randomData)257 int32_t HksCryptoHalFillPrivRandom(struct HksBlob *randomData)
258 {
259     return HksCryptoHalFillRandom(randomData);
260 }
261 
HksCryptoHalEncrypt(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * cipherText,struct HksBlob * tagAead)262 int32_t HksCryptoHalEncrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
263     const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead)
264 {
265     int32_t ret = EncryptCheckParam(key, usageSpec, message, cipherText);
266     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "Invalid params!")
267 
268     Encrypt func = (Encrypt)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT(usageSpec->algType));
269     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "EncryptAes func is null!")
270     return func(key, usageSpec, message, cipherText, tagAead);
271 }
272 
HksCryptoHalEncryptInit(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,void ** ctx)273 int32_t HksCryptoHalEncryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx)
274 {
275     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
276     HKS_IF_NULL_LOGE_RETURN(usageSpec, HKS_ERROR_INVALID_ARGUMENT, "Invalid param usageSpec!")
277 
278     EncryptInit func = (EncryptInit)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_INIT(usageSpec->algType));
279     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
280 
281     return func(ctx, key, usageSpec, true);
282 }
283 
HksCryptoHalEncryptUpdate(const struct HksBlob * message,void * ctx,struct HksBlob * out,const uint32_t algtype)284 int32_t HksCryptoHalEncryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, const uint32_t algtype)
285 {
286     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(message), HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
287     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(out), HKS_ERROR_INVALID_ARGUMENT, "Invalid param out!")
288 
289     HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_ARGUMENT, "Invalid param ctx or out !")
290 
291     EncryptUpdate func = (EncryptUpdate)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_UPDATE(algtype));
292     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
293 
294     return func(ctx, message, out, true);
295 }
296 
HksCryptoHalEncryptFinal(const struct HksBlob * message,void ** ctx,struct HksBlob * cipherText,struct HksBlob * tagAead,const uint32_t algtype)297 int32_t HksCryptoHalEncryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText,
298     struct HksBlob *tagAead, const uint32_t algtype)
299 {
300     HKS_IF_NULL_LOGE_RETURN(message, HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
301 
302     if (ctx == NULL || *ctx == NULL) {
303         HKS_LOG_E("Invalid param ctx!");
304         return HKS_ERROR_INVALID_ARGUMENT;
305     }
306 
307     EncryptFinal func = (EncryptFinal)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_FINAL(algtype));
308     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
309 
310     return func(ctx, message, cipherText, tagAead, true);
311 }
312 
HksCryptoHalEncryptFreeCtx(void ** ctx,const uint32_t algtype)313 void HksCryptoHalEncryptFreeCtx(void **ctx, const uint32_t algtype)
314 {
315     FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_FREE_CTX(algtype));
316     if (func == NULL) {
317         HKS_LOG_E("CryptoHalEncryptFreeCtx func is null");
318         return;
319     }
320 
321     func(ctx);
322 }
323 
HksCryptoHalDecrypt(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * cipherText)324 int32_t HksCryptoHalDecrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
325     const struct HksBlob *message, struct HksBlob *cipherText)
326 {
327     int32_t ret = EncryptCheckParam(key, usageSpec, message, cipherText);
328     HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "Invalid params!")
329 
330     Decrypt func = (Decrypt)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT(usageSpec->algType));
331     HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "DecryptAes func is null!")
332     return func(key, usageSpec, message, cipherText);
333 }
334 
HksCryptoHalDecryptInit(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,void ** ctx)335 int32_t HksCryptoHalDecryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx)
336 {
337     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
338     HKS_IF_NULL_LOGE_RETURN(usageSpec, HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
339 
340     DecryptInit func = (DecryptInit)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_INIT(usageSpec->algType));
341     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
342 
343     return func(ctx, key, usageSpec, false);
344 }
345 
HksCryptoHalDecryptUpdate(const struct HksBlob * message,void * ctx,struct HksBlob * out,const uint32_t algtype)346 int32_t HksCryptoHalDecryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, const uint32_t algtype)
347 {
348     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(message), HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
349     HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(out), HKS_ERROR_INVALID_ARGUMENT, "Invalid param out!")
350     HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_ARGUMENT, "Invalid param ctx or out !")
351 
352     DecryptUpdate func = (DecryptUpdate)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_UPDATE(algtype));
353     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
354 
355     return func(ctx, message, out, false);
356 }
357 
HksCryptoHalDecryptFinal(const struct HksBlob * message,void ** ctx,struct HksBlob * cipherText,struct HksBlob * tagAead,const uint32_t algtype)358 int32_t HksCryptoHalDecryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText,
359     struct HksBlob *tagAead, const uint32_t algtype)
360 {
361     HKS_IF_NULL_LOGE_RETURN(message, HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
362 
363     if (ctx == NULL || *ctx == NULL) {
364         HKS_LOG_E("Invalid param ctx!");
365         return HKS_ERROR_INVALID_ARGUMENT;
366     }
367 
368     DecryptFinal func = (DecryptFinal)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_FINAL(algtype));
369     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
370 
371     return func(ctx, message, cipherText, tagAead, false);
372 }
373 
HksCryptoHalDecryptFreeCtx(void ** ctx,const uint32_t algtype)374 void HksCryptoHalDecryptFreeCtx(void **ctx, const uint32_t algtype)
375 {
376     FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_FREE_CTX(algtype));
377     if (func == NULL) {
378         HKS_LOG_E("CryptoHalDecryptFreeCtx func is null");
379         return;
380     }
381 
382     func(ctx);
383 }
384 
HksCryptoHalGetMainKey(const struct HksBlob * message,struct HksBlob * mainKey)385 int32_t HksCryptoHalGetMainKey(const struct HksBlob *message, struct HksBlob *mainKey)
386 {
387     GetMainKey func = (GetMainKey)GetAbility(HKS_CRYPTO_ABILITY_GET_MAIN_KEY);
388     HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
389     return func(message, mainKey);
390 }