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 #ifdef HKS_SUPPORT_CMAC_C
HksCryptoHalCmacInit(const struct HksBlob * key,void ** ctx,const struct HksUsageSpec * usageSpec)103 int32_t HksCryptoHalCmacInit(const struct HksBlob *key, void **ctx, const struct HksUsageSpec *usageSpec)
104 {
105 if (CheckBlob(key) != HKS_SUCCESS || ctx == NULL || usageSpec == NULL) {
106 HKS_LOG_E("Crypt Hal Cmac init msg is NULL");
107 return HKS_ERROR_INVALID_ARGUMENT;
108 }
109
110 CmacInit func = (CmacInit)GetAbility(HKS_CRYPTO_ABILITY_CMAC_INIT);
111 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
112
113 return func(ctx, key, usageSpec);
114 }
115
HksCryptoHalCmacUpdate(const struct HksBlob * chunk,void * ctx,const struct HksUsageSpec * usageSpec)116 int32_t HksCryptoHalCmacUpdate(const struct HksBlob *chunk, void *ctx, const struct HksUsageSpec *usageSpec)
117 {
118 if (CheckBlob(chunk) != HKS_SUCCESS || ctx == NULL || usageSpec == NULL) {
119 HKS_LOG_E("Crypt Hal Cmac update chunk is invalid param");
120 return HKS_ERROR_INVALID_ARGUMENT;
121 }
122
123 CmacUpdate func = (CmacUpdate)GetAbility(HKS_CRYPTO_ABILITY_CMAC_UPDATE);
124 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
125
126 return func(ctx, chunk, usageSpec);
127 }
128
HksCryptoHalCmacFinal(const struct HksBlob * msg,void ** ctx,struct HksBlob * mac,const struct HksUsageSpec * usageSpec)129 int32_t HksCryptoHalCmacFinal(
130 const struct HksBlob *msg, void **ctx, struct HksBlob *mac, const struct HksUsageSpec *usageSpec)
131 {
132 if (msg == NULL || ctx == NULL || *ctx == NULL || usageSpec == NULL || CheckBlob(mac) != HKS_SUCCESS) {
133 HKS_LOG_E("Crypt Hal Cmac final msg or mac is NULL");
134 return HKS_ERROR_INVALID_ARGUMENT;
135 }
136
137 CmacFinal func = (CmacFinal)GetAbility(HKS_CRYPTO_ABILITY_CMAC_FINAL);
138 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
139
140 return func(ctx, msg, mac, usageSpec);
141 }
142
HksCryptoHalCmacFreeCtx(void ** ctx)143 void HksCryptoHalCmacFreeCtx(void **ctx)
144 {
145 FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_CMAC_FREE_CTX);
146 if (func == NULL) {
147 HKS_LOG_E("CryptoHalCmacFreeCtx func is null");
148 return;
149 }
150
151 func(ctx);
152 }
153 #endif
154
155 #ifndef _CUT_AUTHENTICATE_
HksCryptoHalHash(uint32_t alg,const struct HksBlob * msg,struct HksBlob * hash)156 int32_t HksCryptoHalHash(uint32_t alg, const struct HksBlob *msg, struct HksBlob *hash)
157 {
158 Hash func = (Hash)GetAbility(HKS_CRYPTO_ABILITY_HASH);
159 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash func is null!")
160 return func(alg, msg, hash);
161 }
162
HksCryptoHalHashInit(uint32_t alg,void ** ctx)163 int32_t HksCryptoHalHashInit(uint32_t alg, void **ctx)
164 {
165 HashInit func = (HashInit)GetAbility(HKS_CRYPTO_ABILITY_HASH_INIT);
166 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash init func is null!")
167
168 return func(ctx, alg);
169 }
170
HksCryptoHalHashUpdate(const struct HksBlob * msg,void * ctx)171 int32_t HksCryptoHalHashUpdate(const struct HksBlob *msg, void *ctx)
172 {
173 if (CheckBlob(msg) != HKS_SUCCESS || ctx == NULL) {
174 HKS_LOG_E("Crypt Hal Hash msg or ctx is NULL");
175 return HKS_ERROR_INVALID_ARGUMENT;
176 }
177
178 HashUpdate func = (HashUpdate)GetAbility(HKS_CRYPTO_ABILITY_HASH_UPDATE);
179 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash update func is null!")
180
181 return func(ctx, msg);
182 }
183
HksCryptoHalHashFinal(const struct HksBlob * msg,void ** ctx,struct HksBlob * hash)184 int32_t HksCryptoHalHashFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *hash)
185 {
186 if (msg == NULL || CheckBlob(hash) != HKS_SUCCESS || ctx == NULL || *ctx == NULL) {
187 HKS_LOG_E("Crypt Hal Hash final msg or hash or ctx is NULL");
188 return HKS_ERROR_INVALID_ARGUMENT;
189 }
190
191 HashFinal func = (HashFinal)GetAbility(HKS_CRYPTO_ABILITY_HASH_FINAL);
192 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls Hash final func is null!")
193
194 return func(ctx, msg, hash);
195 }
196
HksCryptoHalHashFreeCtx(void ** ctx)197 void HksCryptoHalHashFreeCtx(void **ctx)
198 {
199 FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_HASH_FREE_CTX);
200 if (func == NULL) {
201 HKS_LOG_E("CryptoHalHashFreeCtx func is null");
202 return;
203 }
204
205 func(ctx);
206 }
207
208 #endif /* _CUT_AUTHENTICATE_ */
209
HksCryptoHalBnExpMod(struct HksBlob * x,const struct HksBlob * a,const struct HksBlob * e,const struct HksBlob * n)210 int32_t HksCryptoHalBnExpMod(struct HksBlob *x, const struct HksBlob *a,
211 const struct HksBlob *e, const struct HksBlob *n)
212 {
213 BnExpMod func = (BnExpMod)GetAbility(HKS_CRYPTO_ABILITY_BN_EXP_MOD);
214 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
215 return func(x, a, e, n);
216 }
217
218 #ifndef _CUT_AUTHENTICATE_
HksCryptoHalGenerateKey(const struct HksKeySpec * spec,struct HksBlob * key)219 int32_t HksCryptoHalGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key)
220 {
221 if (spec == NULL || key == NULL) {
222 HKS_LOG_E("Crypt Hal GenerateKey msg or hash or ctx is NULL");
223 return HKS_ERROR_INVALID_ARGUMENT;
224 }
225
226 GenerateKey func = (GenerateKey)GetAbility(HKS_CRYPTO_ABILITY_GENERATE_KEY(spec->algType));
227 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls GenerateKey func is null!")
228 return func(spec, key);
229 }
230
HksCryptoHalGetPubKey(const struct HksBlob * keyIn,struct HksBlob * keyOut)231 int32_t HksCryptoHalGetPubKey(const struct HksBlob *keyIn, struct HksBlob *keyOut)
232 {
233 if (CheckBlob(keyIn) != HKS_SUCCESS || CheckBlob(keyOut) != HKS_SUCCESS) {
234 HKS_LOG_E("Invalid params!");
235 return HKS_ERROR_INVALID_ARGUMENT;
236 }
237
238 /* KeyMaterialRsa, KeyMaterialEcc, KeyMaterial25519's size are same */
239 if (keyIn->size < sizeof(struct KeyMaterialRsa)) {
240 HKS_LOG_E("Crypt Hal getPubKey keyIn size is more smaller. size[%" LOG_PUBLIC "d]", keyIn->size);
241 return HKS_ERROR_INVALID_KEY_SIZE;
242 }
243
244 struct KeyMaterialRsa *key = (struct KeyMaterialRsa *)(keyIn->data);
245 PubKey func = (PubKey)GetAbility(HKS_CRYPTO_ABILITY_GET_PUBLIC_KEY(key->keyAlg));
246 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
247 return func(keyIn, keyOut);
248 }
249 #endif /* _CUT_AUTHENTICATE_ */
250
HksCryptoHalDeriveKey(const struct HksBlob * mainKey,const struct HksKeySpec * derivationSpec,struct HksBlob * derivedKey)251 int32_t HksCryptoHalDeriveKey(const struct HksBlob *mainKey,
252 const struct HksKeySpec *derivationSpec, struct HksBlob *derivedKey)
253 {
254 DeriveKey func = (DeriveKey)GetAbility(HKS_CRYPTO_ABILITY_DERIVE_KEY(derivationSpec->algType));
255 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls DeriveKey func is null!")
256 return func(mainKey, derivationSpec, derivedKey);
257 }
258
259 #ifndef _CUT_AUTHENTICATE_
HksCryptoHalAgreeKey(const struct HksBlob * nativeKey,const struct HksBlob * pubKey,const struct HksKeySpec * spec,struct HksBlob * sharedKey)260 int32_t HksCryptoHalAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey,
261 const struct HksKeySpec *spec, struct HksBlob *sharedKey)
262 {
263 if (CheckBlob(nativeKey) != HKS_SUCCESS || CheckBlob(pubKey) != HKS_SUCCESS || spec == NULL ||
264 CheckBlob(sharedKey) != HKS_SUCCESS) {
265 HKS_LOG_E("Crypt Hal AgreeKey param error");
266 return HKS_ERROR_INVALID_ARGUMENT;
267 }
268
269 AgreeKey func = (AgreeKey)GetAbility(HKS_CRYPTO_ABILITY_AGREE_KEY(spec->algType));
270 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "Mbedtls AgreeKey func is null!")
271 return func(nativeKey, pubKey, spec, sharedKey);
272 }
273
HksCryptoHalSign(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)274 int32_t HksCryptoHalSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
275 const struct HksBlob *message, struct HksBlob *signature)
276 {
277 if (CheckBlob(key) != HKS_SUCCESS || usageSpec == NULL || CheckBlob(message) != HKS_SUCCESS ||
278 CheckBlob(signature) != HKS_SUCCESS) {
279 HKS_LOG_E("Crypt Hal Sign param error");
280 return HKS_ERROR_INVALID_ARGUMENT;
281 }
282
283 Sign func = (Sign)GetAbility(HKS_CRYPTO_ABILITY_SIGN(usageSpec->algType));
284 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
285 return func(key, usageSpec, message, signature);
286 }
287
HksCryptoHalVerify(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)288 int32_t HksCryptoHalVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
289 const struct HksBlob *message, const struct HksBlob *signature)
290 {
291 if (CheckBlob(key) != HKS_SUCCESS || usageSpec == NULL || CheckBlob(message) != HKS_SUCCESS ||
292 CheckBlob(signature) != HKS_SUCCESS) {
293 HKS_LOG_E("Crypt Hal Verify param error");
294 return HKS_ERROR_INVALID_ARGUMENT;
295 }
296
297 Verify func = (Verify)GetAbility(HKS_CRYPTO_ABILITY_VERIFY(usageSpec->algType));
298 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
299 return func(key, usageSpec, message, signature);
300 }
301
HksCryptoHalSignIsoIec97962(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * signature)302 int32_t HksCryptoHalSignIsoIec97962(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
303 const struct HksBlob *message, struct HksBlob *signature)
304
305 {
306 if (CheckBlob(key) != HKS_SUCCESS || usageSpec == NULL || CheckBlob(message) != HKS_SUCCESS ||
307 CheckBlob(signature) != HKS_SUCCESS) {
308 HKS_LOG_E("Crypt Hal Sign param error");
309 return HKS_ERROR_INVALID_ARGUMENT;
310 }
311
312 Sign func = (Sign)GetAbility(HKS_CRYPTO_ABILITY_SIGN_ISO_IEC_9796_2);
313 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
314 return func(key, usageSpec, message, signature);
315 }
316
HksCryptoHalVerifyIsoIec97962(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,const struct HksBlob * signature)317 int32_t HksCryptoHalVerifyIsoIec97962(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
318 const struct HksBlob *message, const struct HksBlob *signature)
319 {
320 if (CheckBlob(key) != HKS_SUCCESS || usageSpec == NULL || CheckBlob(message) != HKS_SUCCESS ||
321 CheckBlob(signature) != HKS_SUCCESS) {
322 HKS_LOG_E("Crypt Hal Verify param error");
323 return HKS_ERROR_INVALID_ARGUMENT;
324 }
325
326 Verify func = (Verify)GetAbility(HKS_CRYPTO_ABILITY_VERIFY_ISO_IEC_9796_2);
327 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
328 return func(key, usageSpec, message, signature);
329 }
330 #endif /* _CUT_AUTHENTICATE_ */
331
HksCryptoHalFillRandom(struct HksBlob * randomData)332 int32_t HksCryptoHalFillRandom(struct HksBlob *randomData)
333 {
334 FillRandom func = (FillRandom)GetAbility(HKS_CRYPTO_ABILITY_FILL_RANDOM);
335 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
336 return func(randomData);
337 }
338
HksCryptoHalFillPrivRandom(struct HksBlob * randomData)339 int32_t HksCryptoHalFillPrivRandom(struct HksBlob *randomData)
340 {
341 return HksCryptoHalFillRandom(randomData);
342 }
343
HksCryptoHalEncrypt(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * cipherText,struct HksBlob * tagAead)344 int32_t HksCryptoHalEncrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
345 const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead)
346 {
347 int32_t ret = EncryptCheckParam(key, usageSpec, message, cipherText);
348 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "Invalid params!")
349
350 Encrypt func = (Encrypt)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT(usageSpec->algType));
351 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "EncryptAes func is null!")
352 return func(key, usageSpec, message, cipherText, tagAead);
353 }
354
HksCryptoHalEncryptInit(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,void ** ctx)355 int32_t HksCryptoHalEncryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx)
356 {
357 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
358 HKS_IF_NULL_LOGE_RETURN(usageSpec, HKS_ERROR_INVALID_ARGUMENT, "Invalid param usageSpec!")
359
360 EncryptInit func = (EncryptInit)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_INIT(usageSpec->algType));
361 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
362
363 return func(ctx, key, usageSpec, true);
364 }
365
HksCryptoHalEncryptUpdate(const struct HksBlob * message,void * ctx,struct HksBlob * out,const uint32_t algtype)366 int32_t HksCryptoHalEncryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, const uint32_t algtype)
367 {
368 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(message), HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
369 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(out), HKS_ERROR_INVALID_ARGUMENT, "Invalid param out!")
370
371 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_ARGUMENT, "Invalid param ctx or out !")
372
373 EncryptUpdate func = (EncryptUpdate)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_UPDATE(algtype));
374 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
375
376 return func(ctx, message, out, true);
377 }
378
HksCryptoHalEncryptFinal(const struct HksBlob * message,void ** ctx,struct HksBlob * cipherText,struct HksBlob * tagAead,const uint32_t algtype)379 int32_t HksCryptoHalEncryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText,
380 struct HksBlob *tagAead, const uint32_t algtype)
381 {
382 HKS_IF_NULL_LOGE_RETURN(message, HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
383
384 if (ctx == NULL || *ctx == NULL) {
385 HKS_LOG_E("Invalid param ctx!");
386 return HKS_ERROR_INVALID_ARGUMENT;
387 }
388
389 EncryptFinal func = (EncryptFinal)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_FINAL(algtype));
390 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
391
392 return func(ctx, message, cipherText, tagAead, true);
393 }
394
HksCryptoHalEncryptFreeCtx(void ** ctx,const uint32_t algtype)395 void HksCryptoHalEncryptFreeCtx(void **ctx, const uint32_t algtype)
396 {
397 FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_ENCRYPT_FREE_CTX(algtype));
398 if (func == NULL) {
399 HKS_LOG_E("CryptoHalEncryptFreeCtx func is null");
400 return;
401 }
402
403 func(ctx);
404 }
405
HksCryptoHalDecrypt(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,const struct HksBlob * message,struct HksBlob * cipherText)406 int32_t HksCryptoHalDecrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec,
407 const struct HksBlob *message, struct HksBlob *cipherText)
408 {
409 int32_t ret = EncryptCheckParam(key, usageSpec, message, cipherText);
410 HKS_IF_NOT_SUCC_LOGE_RETURN(ret, HKS_ERROR_INVALID_ARGUMENT, "Invalid params!")
411
412 Decrypt func = (Decrypt)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT(usageSpec->algType));
413 HKS_IF_NULL_LOGE_RETURN(func, HKS_ERROR_INVALID_ARGUMENT, "DecryptAes func is null!")
414 return func(key, usageSpec, message, cipherText);
415 }
416
HksCryptoHalDecryptInit(const struct HksBlob * key,const struct HksUsageSpec * usageSpec,void ** ctx)417 int32_t HksCryptoHalDecryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx)
418 {
419 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(key), HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
420 HKS_IF_NULL_LOGE_RETURN(usageSpec, HKS_ERROR_INVALID_ARGUMENT, "Invalid param key!")
421
422 DecryptInit func = (DecryptInit)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_INIT(usageSpec->algType));
423 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
424
425 return func(ctx, key, usageSpec, false);
426 }
427
HksCryptoHalDecryptUpdate(const struct HksBlob * message,void * ctx,struct HksBlob * out,const uint32_t algtype)428 int32_t HksCryptoHalDecryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, const uint32_t algtype)
429 {
430 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(message), HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
431 HKS_IF_NOT_SUCC_LOGE_RETURN(CheckBlob(out), HKS_ERROR_INVALID_ARGUMENT, "Invalid param out!")
432 HKS_IF_NULL_LOGE_RETURN(ctx, HKS_ERROR_INVALID_ARGUMENT, "Invalid param ctx or out !")
433
434 DecryptUpdate func = (DecryptUpdate)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_UPDATE(algtype));
435 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
436
437 return func(ctx, message, out, false);
438 }
439
HksCryptoHalDecryptFinal(const struct HksBlob * message,void ** ctx,struct HksBlob * cipherText,struct HksBlob * tagAead,const uint32_t algtype)440 int32_t HksCryptoHalDecryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText,
441 struct HksBlob *tagAead, const uint32_t algtype)
442 {
443 HKS_IF_NULL_LOGE_RETURN(message, HKS_ERROR_INVALID_ARGUMENT, "Invalid param message!")
444
445 if (ctx == NULL || *ctx == NULL) {
446 HKS_LOG_E("Invalid param ctx!");
447 return HKS_ERROR_INVALID_ARGUMENT;
448 }
449
450 if ((algtype == HKS_ALG_DES) || (algtype == HKS_ALG_3DES)) {
451 DecryptFinalDes func = (DecryptFinalDes)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_FINAL(algtype));
452 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
453 return func(ctx, message, cipherText, false);
454 }
455
456 DecryptFinal func = (DecryptFinal)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_FINAL(algtype));
457 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
458
459 return func(ctx, message, cipherText, tagAead, false);
460 }
461
HksCryptoHalDecryptFreeCtx(void ** ctx,const uint32_t algtype)462 void HksCryptoHalDecryptFreeCtx(void **ctx, const uint32_t algtype)
463 {
464 FreeCtx func = (FreeCtx)GetAbility(HKS_CRYPTO_ABILITY_DECRYPT_FREE_CTX(algtype));
465 if (func == NULL) {
466 HKS_LOG_E("CryptoHalDecryptFreeCtx func is null");
467 return;
468 }
469
470 func(ctx);
471 }
472
HksCryptoHalGetMainKey(const struct HksBlob * message,struct HksBlob * mainKey)473 int32_t HksCryptoHalGetMainKey(const struct HksBlob *message, struct HksBlob *mainKey)
474 {
475 GetMainKey func = (GetMainKey)GetAbility(HKS_CRYPTO_ABILITY_GET_MAIN_KEY);
476 HKS_IF_NULL_RETURN(func, HKS_ERROR_INVALID_ARGUMENT)
477 return func(message, mainKey);
478 }