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_hash.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_hash.h"
18
tee_crypto_hash_init(uint32_t alg_type,uint32_t engine)19 struct ctx_handle_t *tee_crypto_hash_init(uint32_t alg_type, uint32_t engine)
20 {
21 int32_t ret;
22 struct ctx_handle_t *ctx = alloc_ctx_handle(alg_type, engine);
23 if (ctx == NULL) {
24 tloge("Malloc ctx handle failed\n");
25 return NULL;
26 }
27
28 if (engine == SOFT_CRYPTO)
29 ret = soft_crypto_hash_init(ctx);
30 else
31 ret = crypto_driver_hash_init(ctx);
32 if (ret != CRYPTO_SUCCESS) {
33 tloge("Hash init failed, ret=%d\n", ret);
34 tee_crypto_ctx_free(ctx);
35 return NULL;
36 }
37
38 return ctx;
39 }
40
tee_crypto_hash_update(struct ctx_handle_t * ctx,const struct memref_t * data_in)41 int32_t tee_crypto_hash_update(struct ctx_handle_t *ctx, const struct memref_t *data_in)
42 {
43 bool check = ((ctx == NULL) || (data_in == NULL));
44 if (check) {
45 tloge("Invalid params\n");
46 return CRYPTO_BAD_PARAMETERS;
47 }
48
49 if (ctx->engine == SOFT_CRYPTO)
50 return soft_crypto_hash_update(ctx, data_in);
51
52 return crypto_driver_hash_update(ctx, data_in);
53 }
54
tee_crypto_hash_dofinal(struct ctx_handle_t * ctx,const struct memref_t * data_in,struct memref_t * data_out)55 int32_t tee_crypto_hash_dofinal(struct ctx_handle_t *ctx, const struct memref_t *data_in, struct memref_t *data_out)
56 {
57 bool check = ((ctx == NULL) || (data_out == NULL));
58 if (check) {
59 tloge("Invalid params\n");
60 return CRYPTO_BAD_PARAMETERS;
61 }
62 int32_t rc = CRYPTO_SUCCESS;
63 check = (data_in != NULL && data_in->buffer != 0 && data_in->size != 0);
64 if (check)
65 rc = tee_crypto_hash_update(ctx, data_in);
66
67 if (rc != CRYPTO_SUCCESS) {
68 tloge("hash do update failed");
69 return rc;
70 }
71
72 if (ctx->engine == SOFT_CRYPTO)
73 return soft_crypto_hash_dofinal(ctx, data_out);
74
75 return crypto_driver_hash_dofinal(ctx, NULL, data_out);
76 }
77
tee_crypto_hash(uint32_t alg_type,const struct memref_t * data_in,struct memref_t * data_out,uint32_t engine)78 int32_t tee_crypto_hash(uint32_t alg_type, const struct memref_t *data_in, struct memref_t *data_out, uint32_t engine)
79 {
80 bool check = ((data_in == NULL) || (data_out == NULL));
81 if (check) {
82 tloge("Invalid params\n");
83 return CRYPTO_BAD_PARAMETERS;
84 }
85
86 if (engine == SOFT_CRYPTO)
87 return soft_crypto_hash(alg_type, data_in, data_out);
88
89 return crypto_driver_hash(alg_type, data_in, data_out, engine);
90 }
91