• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tee_crypto_common_hash.h"
13 #include <string.h>
14 #include <tee_log.h>
15 #include <tee_property_inner.h>
16 #include <tee_object_api.h>
17 #include <crypto_inner_defines.h>
18 #include <crypto_hal_hash.h>
19 #include <crypto_driver_adaptor.h>
20 #include "tee_operation.h"
21 
22 struct digest_op_config_s {
23     uint32_t expect_class;
24     uint32_t expect_mode;
25     uint32_t algorithm;
26 };
27 
28 static const struct digest_op_config_s g_digest_config[] = {
29     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_MD5 },
30     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_SHA1 },
31     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_SHA224 },
32     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_SHA256 },
33     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_SHA384 },
34     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_SHA512 },
35     { TEE_OPERATION_DIGEST, TEE_MODE_DIGEST, TEE_ALG_SM3 },
36 };
37 
digest_operation_state_check(const TEE_OperationHandle operation)38 TEE_Result digest_operation_state_check(const TEE_OperationHandle operation)
39 {
40     const struct digest_op_config_s *config = NULL;
41     uint32_t index;
42 
43     if (operation == NULL)
44         return TEE_ERROR_BAD_PARAMETERS;
45 
46     uint32_t api_level = tee_get_ta_api_level();
47     if (api_level >= API_LEVEL1_1_1) {
48         if ((operation->handleState & TEE_HANDLE_FLAG_KEY_SET) != TEE_HANDLE_FLAG_KEY_SET) {
49             tloge("Invalid operation key state for this operation\n");
50             return TEE_ERROR_BAD_STATE;
51         }
52 
53         if ((operation->handleState & TEE_HANDLE_FLAG_INITIALIZED) != TEE_HANDLE_FLAG_INITIALIZED) {
54             tloge("Cipher is not initialized yet\n");
55             return TEE_ERROR_BAD_STATE;
56         }
57     }
58 
59     for (index = 0; index < ELEM_NUM(g_digest_config); index++) {
60         if (operation->algorithm == g_digest_config[index].algorithm) {
61             config = &g_digest_config[index];
62             break;
63         }
64     }
65 
66     bool check = (config == NULL || operation->operationClass != config->expect_class ||
67         operation->mode != config->expect_mode);
68     if (check) {
69         tloge("This operation is Invalid!\n");
70         return TEE_ERROR_BAD_PARAMETERS;
71     }
72 
73     return TEE_SUCCESS;
74 }
75 
proc_hal_digest_init(TEE_OperationHandle operation)76 TEE_Result proc_hal_digest_init(TEE_OperationHandle operation)
77 {
78     if (operation == NULL)
79         return TEE_ERROR_BAD_PARAMETERS;
80 
81     crypto_hal_info *crypto_hal_data = operation->hal_info;
82     if (crypto_hal_data == NULL) {
83         tloge("crypto hal data is null");
84         return TEE_ERROR_BAD_PARAMETERS;
85     }
86 
87     if (crypto_hal_data->digestalloc_flag == DIGEST_ALLOC_CTX)
88         return TEE_SUCCESS;
89 
90     free_operation_ctx(operation);
91     operation->crypto_ctxt = tee_crypto_hash_init(operation->algorithm, crypto_hal_data->crypto_flag);
92     if (operation->crypto_ctxt == NULL)
93         return TEE_ERROR_GENERIC;
94 
95     crypto_hal_data->digestalloc_flag = DIGEST_ALLOC_CTX;
96     return TEE_SUCCESS;
97 }
98 
proc_hal_digest_update(TEE_OperationHandle operation,const void * chunk,size_t chunk_size)99 TEE_Result proc_hal_digest_update(TEE_OperationHandle operation, const void *chunk, size_t chunk_size)
100 {
101     struct memref_t data_in = {0};
102 
103     if (operation == NULL || chunk == NULL)
104         return TEE_ERROR_BAD_PARAMETERS;
105 
106     data_in.buffer = (uint64_t)(uintptr_t)chunk;
107     data_in.size = (uint32_t)chunk_size;
108 
109     TEE_Result result = proc_hal_digest_init(operation);
110     if (result != TEE_SUCCESS)
111         return result;
112 
113     int32_t ret = tee_crypto_hash_update(operation->crypto_ctxt, &data_in);
114     if (ret != TEE_SUCCESS)
115         return change_hal_ret_to_gp(ret);
116 
117     return TEE_SUCCESS;
118 }
119 
120