1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include "crypto_hal_hmac.h"
13 #include <tee_log.h>
14 #include <tee_crypto_hal.h>
15 #include "crypto_manager.h"
16 #include "crypto_hal.h"
17 #include "soft_hmac.h"
18
tee_crypto_hmac_init(uint32_t alg_type,const struct symmerit_key_t * key,uint32_t engine)19 struct ctx_handle_t *tee_crypto_hmac_init(uint32_t alg_type, const struct symmerit_key_t *key, uint32_t engine)
20 {
21 if (key == NULL) {
22 tloge("Invalid key\n");
23 return NULL;
24 }
25
26 struct ctx_handle_t *ctx = alloc_ctx_handle(alg_type, engine);
27 if (ctx == NULL) {
28 tloge("Malloc ctx handle failed\n");
29 return NULL;
30 }
31
32 int32_t ret;
33 if (engine == SOFT_CRYPTO)
34 ret = soft_crypto_hmac_init(ctx, key);
35 else
36 ret = crypto_driver_hmac_init(ctx, key);
37 if (ret != CRYPTO_SUCCESS) {
38 tloge("Hmac init failed, ret=%d\n", ret);
39 tee_crypto_ctx_free(ctx);
40 return NULL;
41 }
42
43 return ctx;
44 }
45
tee_crypto_hmac_update(struct ctx_handle_t * ctx,const struct memref_t * data_in)46 int32_t tee_crypto_hmac_update(struct ctx_handle_t *ctx, const struct memref_t *data_in)
47 {
48 bool check = ((ctx == NULL) || (data_in == NULL));
49 if (check) {
50 tloge("Invalid params\n");
51 return CRYPTO_BAD_PARAMETERS;
52 }
53
54 if (ctx->engine == SOFT_CRYPTO)
55 return soft_crypto_hmac_update(ctx, data_in);
56
57 return crypto_driver_hmac_update(ctx, data_in);
58 }
59
tee_crypto_hmac_dofinal(struct ctx_handle_t * ctx,const struct memref_t * data_in,struct memref_t * data_out)60 int32_t tee_crypto_hmac_dofinal(struct ctx_handle_t *ctx, const struct memref_t *data_in, struct memref_t *data_out)
61 {
62 bool check = ((ctx == NULL) || (data_out == NULL));
63 if (check) {
64 tloge("Invalid params\n");
65 return CRYPTO_BAD_PARAMETERS;
66 }
67
68 int32_t rc = CRYPTO_SUCCESS;
69 check = (data_in != NULL && data_in->buffer != 0 && data_in->size != 0);
70 if (check)
71 rc = tee_crypto_hmac_update(ctx, data_in);
72
73 if (rc != CRYPTO_SUCCESS) {
74 tloge("hmac update failed, rc = %d", rc);
75 return rc;
76 }
77
78 if (ctx->engine == SOFT_CRYPTO)
79 return soft_crypto_hmac_dofinal(ctx, data_out);
80
81 return crypto_driver_hmac_dofinal(ctx, NULL, data_out);
82 }
83
tee_crypto_hmac(uint32_t alg_type,const struct symmerit_key_t * key,const struct memref_t * data_in,struct memref_t * data_out,uint32_t engine)84 int32_t tee_crypto_hmac(uint32_t alg_type, const struct symmerit_key_t *key,
85 const struct memref_t *data_in, struct memref_t *data_out, uint32_t engine)
86 {
87 bool check = ((data_in == NULL) || (data_out == NULL));
88 if (check) {
89 tloge("Invalid params\n");
90 return CRYPTO_BAD_PARAMETERS;
91 }
92 if (engine == SOFT_CRYPTO)
93 return soft_crypto_hmac(alg_type, key, data_in, data_out);
94
95 return crypto_driver_hmac(alg_type, key, data_in, data_out, engine);
96 }
97
98