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