• 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 <stdio.h>
13 #include <inttypes.h>
14 #include <priorities.h>
15 #include <fileio.h>
16 #include "tee_driver_module.h"
17 #include "crypto_mgr_syscall.h"
18 #include <tee_log.h>
19 #include "drv_random.h"
20 #include "crypto_driver_adaptor.h"
21 #include <securec.h>
22 
23 const char *g_debug_prefix = "crypto_mgr";
24 uint8_t *g_src_ctx_buf = NULL;
25 uint8_t *g_dest_ctx_buf = NULL;
26 static int32_t g_src_fd = 0;
27 static int32_t g_dest_fd = 0;
28 
29 #define TYPE_DRV_OPEN   2
30 
crypto_mgr_init(void)31 int32_t crypto_mgr_init(void)
32 {
33     return 0;
34 }
35 
get_ctx_ctx_buf(void)36 uint8_t *get_ctx_ctx_buf(void)
37 {
38     return g_src_ctx_buf;
39 }
40 
crypto_ioctl_alloc_ctx_buf(struct drv_data * drv,uint32_t cmd,unsigned long args,uint32_t args_len)41 static int32_t crypto_ioctl_alloc_ctx_buf(struct drv_data *drv, uint32_t cmd, unsigned long args, uint32_t args_len)
42 {
43     uint32_t ctx_size = crypto_ioctl_func(drv, IOCTRL_CRYPTO_GET_CTX_SIZE, args, args_len);
44     bool check = ((ctx_size <= 0) || (ctx_size > MAX_CRYPTO_CTX_SIZE));
45     if (check) {
46         tloge("Get ctx size failed, ctx size=%d\n", ctx_size);
47         return CRYPTO_BAD_PARAMETERS;
48     }
49     uint8_t *ctx_buffer = (uint8_t *)malloc((size_t)ctx_size);
50     if (ctx_buffer == NULL) {
51         tloge("Malloc ctx buffer failed, ctx size=%d\n", ctx_size);
52         return CRYPTO_OVERFLOW;
53     }
54     if (memset_s(ctx_buffer, (size_t)ctx_size, 0, (size_t)ctx_size) != 0) {
55         tloge("memset ctx buffer failed\n");
56         free(ctx_buffer);
57         return CRYPTO_ERROR_SECURITY;
58     }
59 
60     drv->private_data = ctx_buffer;
61     if (cmd == 0) {
62         g_src_ctx_buf = ctx_buffer;
63         g_src_fd = drv->fd;
64     } else {
65         g_dest_ctx_buf = ctx_buffer;
66         g_dest_fd = drv->fd;
67     }
68 
69     return CRYPTO_SUCCESS;
70 }
71 
crypto_mgr_ioctl(struct drv_data * drv,uint32_t cmd,unsigned long args,uint32_t args_len)72 int64_t crypto_mgr_ioctl(struct drv_data *drv, uint32_t cmd, unsigned long args, uint32_t args_len)
73 {
74     if (drv == NULL) {
75         tloge("ioctl invalid drv\n");
76         return -1;
77     }
78     int32_t ret;
79     if (cmd == IOCTRL_CRYPTO_CTX_COPY) {
80         ret = crypto_ioctl_alloc_ctx_buf(drv, cmd, args, args_len);
81         if (ret != CRYPTO_SUCCESS) {
82             tloge("crypto_ioctl_alloc_ctx_buf fail\n");
83             return -1;
84         }
85     }
86     ret = crypto_ioctl_func(drv, cmd, args, args_len);
87 
88     tlogi("mgr ioctl load 0x%x ret 0x%x\n", cmd, ret);
89 
90     return ret;
91 }
92 
crypto_mgr_open(struct drv_data * drv,unsigned long args,uint32_t args_len)93 int64_t crypto_mgr_open(struct drv_data *drv, unsigned long args, uint32_t args_len)
94 {
95     if (drv == NULL) {
96         tloge("open invalid drv\n");
97         return -1;
98     }
99 
100     if (args == 0 && args_len == 0)
101         return 0;
102 
103     if (args_len < sizeof(uint32_t) || args == 0) {
104         tloge("open invalid drv\n");
105         return -1;
106     }
107 
108     /* get the drv ability */
109     if (args_len == sizeof(uint32_t) + TYPE_DRV_OPEN) {
110         int32_t ret = crypto_ioctl_alloc_ctx_buf(drv, 0, args, sizeof(uint32_t));
111         if (ret != CRYPTO_SUCCESS)
112             return -1;
113     }
114 
115     return 0;
116 }
117 
crypto_mgr_close(struct drv_data * drv)118 int64_t crypto_mgr_close(struct drv_data *drv)
119 {
120     if (drv == NULL) {
121         tloge("close invalid drv\n");
122         return -1;
123     }
124 
125     if (drv->private_data != NULL) {
126         free(drv->private_data);
127         drv->private_data = NULL;
128         if (g_src_fd == drv->fd) {
129             if (g_dest_ctx_buf == NULL) {
130                 g_src_ctx_buf = NULL;
131                 g_src_fd = 0;
132             } else {
133                 g_src_ctx_buf = g_dest_ctx_buf;
134                 g_dest_ctx_buf = NULL;
135                 g_src_fd = g_dest_fd;
136                 g_dest_fd = 0;
137             }
138         }
139     }
140 
141     return 0;
142 }
143 
crypto_mgr_suspend(void)144 int32_t crypto_mgr_suspend(void)
145 {
146     tlogd("crypto_mgr_suspend\n");
147     return crypto_ioctl_suspend();
148 }
149 
crypto_mgr_resume(void)150 int32_t crypto_mgr_resume(void)
151 {
152     tlogd("crypto_mgr_resume\n");
153     return crypto_ioctl_resume();
154 }
155 
156 tee_driver_declare(crypto_mgr, crypto_mgr_init, crypto_mgr_open, crypto_mgr_ioctl, crypto_mgr_close, \
157                    crypto_mgr_suspend, crypto_mgr_resume, NULL, NULL);
158