• 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 "perm_srv_common.h"
13 #include <sys/mman.h>
14 #include <mem_ops.h>
15 #include <securec.h>
16 #include <tee_log.h>
17 
18 #define SHA256_LEN         32
19 #define HASH_UPDATA_LEN    1024
20 
perm_srv_map_from_task(uint32_t taskid,uint64_t src_vaddr,uint32_t size,uint64_t * dst_vaddr)21 int32_t perm_srv_map_from_task(uint32_t taskid, uint64_t src_vaddr, uint32_t size, uint64_t *dst_vaddr)
22 {
23     uint64_t vaddr = 0;
24 
25     if (dst_vaddr == NULL)
26         return -1;
27 
28     int32_t ret = map_sharemem(taskid, src_vaddr, size, &vaddr);
29     if (ret == 0)
30         *dst_vaddr = vaddr;
31 
32     return ret;
33 }
34 
perm_srv_unmap_from_task(uint64_t vaddr,uint32_t size)35 void perm_srv_unmap_from_task(uint64_t vaddr, uint32_t size)
36 {
37     if (vaddr == 0)
38         return;
39 
40     if (unmap_sharemem((void *)(uintptr_t)vaddr, size) != 0)
41         tloge("perm unmap error\n");
42 }
43 
perm_srv_get_buffer(uint64_t src_buffer,uint32_t src_len,uint32_t sndr_taskid,uint8_t * dst_buffer,uint32_t dst_len)44 TEE_Result perm_srv_get_buffer(uint64_t src_buffer, uint32_t src_len, uint32_t sndr_taskid,
45                                uint8_t *dst_buffer, uint32_t dst_len)
46 {
47     uint64_t temp_shared = 0;
48     errno_t rc;
49 
50     if (dst_buffer == NULL || dst_len < src_len)
51         return TEE_ERROR_BAD_PARAMETERS;
52 
53     /* must to be map the shared memory */
54     if (perm_srv_map_from_task(sndr_taskid, src_buffer, src_len, &temp_shared) != 0) {
55         tloge("map writeBuffer from 0x%x fail\n", sndr_taskid);
56         return TEE_ERROR_GENERIC;
57     }
58 
59     rc = memcpy_s(dst_buffer, dst_len, (uint8_t *)(uintptr_t)temp_shared, src_len);
60     if (rc != EOK) {
61         tloge("Failed to copy config to config buffer\n");
62         perm_srv_unmap_from_task(temp_shared, src_len);
63         return TEE_ERROR_SECURITY;
64     }
65 
66     perm_srv_unmap_from_task(temp_shared, src_len);
67     return TEE_SUCCESS;
68 }
69 
perm_srv_calc_hash(const uint8_t * hash_body,size_t hash_body_size,uint8_t * hash_result,size_t hash_result_size,uint32_t alg)70 TEE_Result perm_srv_calc_hash(const uint8_t *hash_body, size_t hash_body_size, uint8_t *hash_result,
71                                     size_t hash_result_size, uint32_t alg)
72 {
73     TEE_Result tee_ret;
74     TEE_OperationHandle crypto_ops = NULL;
75     size_t per_op_len; /* TEE_ALG_SHA256 */
76 
77     bool is_invalid =
78         (hash_body == NULL || hash_result == NULL || hash_body_size == 0 || hash_result_size < SHA256_LEN);
79     if (is_invalid)
80         return TEE_ERROR_BAD_PARAMETERS;
81 
82     /*
83      * Calculate the hash value of configure package
84      * sha1 with DX driver
85      */
86     tee_ret = TEE_AllocateOperation(&crypto_ops, alg, TEE_MODE_DIGEST, 0);
87     if (tee_ret != TEE_SUCCESS)
88         return tee_ret;
89 
90     tee_ret = TEE_SetCryptoFlag(crypto_ops, SOFT_CRYPTO);
91     if (tee_ret != TEE_SUCCESS) {
92         tloge("set soft engine failed ret = 0x%x\n", tee_ret);
93         TEE_FreeOperation(crypto_ops);
94         return tee_ret;
95     }
96 
97     while (hash_body_size > 0) {
98         per_op_len = (hash_body_size > HASH_UPDATA_LEN ? HASH_UPDATA_LEN : hash_body_size);
99         if (TEE_DigestUpdate(crypto_ops, hash_body, per_op_len) != TEE_SUCCESS) {
100             TEE_FreeOperation(crypto_ops);
101             crypto_ops = NULL;
102             tloge("Failed to call\n");
103             return TEE_ERROR_GENERIC;
104         }
105 
106         hash_body_size -= per_op_len;
107         hash_body += per_op_len;
108     }
109 
110     tee_ret = TEE_DigestDoFinal(crypto_ops, NULL, 0, hash_result, &hash_result_size);
111     TEE_FreeOperation(crypto_ops);
112 
113     return tee_ret;
114 }
115