1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2019-2021 Linaro Ltd.
6 * Sumit Garg <sumit.garg@linaro.org>
10 #include <linux/key-type.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/tee_drv.h>
15 #include <linux/uuid.h>
17 #include <keys/trusted_tee.h>
19 #define DRIVER_NAME "trusted-key-tee"
22 * Get random data for symmetric key
24 * [out] memref[0] Random data
26 #define TA_CMD_GET_RANDOM 0x0
29 * Seal trusted key using hardware unique key
31 * [in] memref[0] Plain key
32 * [out] memref[1] Sealed key datablob
34 #define TA_CMD_SEAL 0x1
37 * Unseal trusted key using hardware unique key
39 * [in] memref[0] Sealed key datablob
40 * [out] memref[1] Plain key
42 #define TA_CMD_UNSEAL 0x2
45 * struct trusted_key_tee_private - TEE Trusted key private data
46 * @dev: TEE based Trusted key device.
47 * @ctx: TEE context handler.
48 * @session_id: Trusted key TA session identifier.
49 * @shm_pool: Memory pool shared with TEE device.
51 struct trusted_key_tee_private
{
53 struct tee_context
*ctx
;
55 struct tee_shm
*shm_pool
;
58 static struct trusted_key_tee_private pvt_data
;
61 * Have the TEE seal(encrypt) the symmetric key
63 static int trusted_tee_seal(struct trusted_key_payload
*p
, char *datablob
)
66 struct tee_ioctl_invoke_arg inv_arg
;
67 struct tee_param param
[4];
68 struct tee_shm
*reg_shm
= NULL
;
70 memset(&inv_arg
, 0, sizeof(inv_arg
));
71 memset(¶m
, 0, sizeof(param
));
73 reg_shm
= tee_shm_register_kernel_buf(pvt_data
.ctx
, p
->key
,
74 sizeof(p
->key
) + sizeof(p
->blob
));
75 if (IS_ERR(reg_shm
)) {
76 dev_err(pvt_data
.dev
, "shm register failed\n");
77 return PTR_ERR(reg_shm
);
80 inv_arg
.func
= TA_CMD_SEAL
;
81 inv_arg
.session
= pvt_data
.session_id
;
82 inv_arg
.num_params
= 4;
84 param
[0].attr
= TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
;
85 param
[0].u
.memref
.shm
= reg_shm
;
86 param
[0].u
.memref
.size
= p
->key_len
;
87 param
[0].u
.memref
.shm_offs
= 0;
88 param
[1].attr
= TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
;
89 param
[1].u
.memref
.shm
= reg_shm
;
90 param
[1].u
.memref
.size
= sizeof(p
->blob
);
91 param
[1].u
.memref
.shm_offs
= sizeof(p
->key
);
93 ret
= tee_client_invoke_func(pvt_data
.ctx
, &inv_arg
, param
);
94 if ((ret
< 0) || (inv_arg
.ret
!= 0)) {
95 dev_err(pvt_data
.dev
, "TA_CMD_SEAL invoke err: %x\n",
99 p
->blob_len
= param
[1].u
.memref
.size
;
102 tee_shm_free(reg_shm
);
108 * Have the TEE unseal(decrypt) the symmetric key
110 static int trusted_tee_unseal(struct trusted_key_payload
*p
, char *datablob
)
113 struct tee_ioctl_invoke_arg inv_arg
;
114 struct tee_param param
[4];
115 struct tee_shm
*reg_shm
= NULL
;
117 memset(&inv_arg
, 0, sizeof(inv_arg
));
118 memset(¶m
, 0, sizeof(param
));
120 reg_shm
= tee_shm_register_kernel_buf(pvt_data
.ctx
, p
->key
,
121 sizeof(p
->key
) + sizeof(p
->blob
));
122 if (IS_ERR(reg_shm
)) {
123 dev_err(pvt_data
.dev
, "shm register failed\n");
124 return PTR_ERR(reg_shm
);
127 inv_arg
.func
= TA_CMD_UNSEAL
;
128 inv_arg
.session
= pvt_data
.session_id
;
129 inv_arg
.num_params
= 4;
131 param
[0].attr
= TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT
;
132 param
[0].u
.memref
.shm
= reg_shm
;
133 param
[0].u
.memref
.size
= p
->blob_len
;
134 param
[0].u
.memref
.shm_offs
= sizeof(p
->key
);
135 param
[1].attr
= TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
;
136 param
[1].u
.memref
.shm
= reg_shm
;
137 param
[1].u
.memref
.size
= sizeof(p
->key
);
138 param
[1].u
.memref
.shm_offs
= 0;
140 ret
= tee_client_invoke_func(pvt_data
.ctx
, &inv_arg
, param
);
141 if ((ret
< 0) || (inv_arg
.ret
!= 0)) {
142 dev_err(pvt_data
.dev
, "TA_CMD_UNSEAL invoke err: %x\n",
146 p
->key_len
= param
[1].u
.memref
.size
;
149 tee_shm_free(reg_shm
);
155 * Have the TEE generate random symmetric key
157 static int trusted_tee_get_random(unsigned char *key
, size_t key_len
)
160 struct tee_ioctl_invoke_arg inv_arg
;
161 struct tee_param param
[4];
162 struct tee_shm
*reg_shm
= NULL
;
164 memset(&inv_arg
, 0, sizeof(inv_arg
));
165 memset(¶m
, 0, sizeof(param
));
167 reg_shm
= tee_shm_register_kernel_buf(pvt_data
.ctx
, key
, key_len
);
168 if (IS_ERR(reg_shm
)) {
169 dev_err(pvt_data
.dev
, "key shm register failed\n");
170 return PTR_ERR(reg_shm
);
173 inv_arg
.func
= TA_CMD_GET_RANDOM
;
174 inv_arg
.session
= pvt_data
.session_id
;
175 inv_arg
.num_params
= 4;
177 param
[0].attr
= TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT
;
178 param
[0].u
.memref
.shm
= reg_shm
;
179 param
[0].u
.memref
.size
= key_len
;
180 param
[0].u
.memref
.shm_offs
= 0;
182 ret
= tee_client_invoke_func(pvt_data
.ctx
, &inv_arg
, param
);
183 if ((ret
< 0) || (inv_arg
.ret
!= 0)) {
184 dev_err(pvt_data
.dev
, "TA_CMD_GET_RANDOM invoke err: %x\n",
188 ret
= param
[0].u
.memref
.size
;
191 tee_shm_free(reg_shm
);
196 static int optee_ctx_match(struct tee_ioctl_version_data
*ver
, const void *data
)
198 if (ver
->impl_id
== TEE_IMPL_ID_OPTEE
&&
199 ver
->gen_caps
& TEE_GEN_CAP_REG_MEM
)
205 static int trusted_key_probe(struct device
*dev
)
207 struct tee_client_device
*rng_device
= to_tee_client_device(dev
);
209 struct tee_ioctl_open_session_arg sess_arg
;
211 memset(&sess_arg
, 0, sizeof(sess_arg
));
213 pvt_data
.ctx
= tee_client_open_context(NULL
, optee_ctx_match
, NULL
,
215 if (IS_ERR(pvt_data
.ctx
))
218 memcpy(sess_arg
.uuid
, rng_device
->id
.uuid
.b
, TEE_IOCTL_UUID_LEN
);
219 sess_arg
.clnt_login
= TEE_IOCTL_LOGIN_REE_KERNEL
;
220 sess_arg
.num_params
= 0;
222 ret
= tee_client_open_session(pvt_data
.ctx
, &sess_arg
, NULL
);
223 if ((ret
< 0) || (sess_arg
.ret
!= 0)) {
224 dev_err(dev
, "tee_client_open_session failed, err: %x\n",
229 pvt_data
.session_id
= sess_arg
.session
;
231 ret
= register_key_type(&key_type_trusted
);
240 tee_client_close_session(pvt_data
.ctx
, pvt_data
.session_id
);
242 tee_client_close_context(pvt_data
.ctx
);
247 static int trusted_key_remove(struct device
*dev
)
249 unregister_key_type(&key_type_trusted
);
250 tee_client_close_session(pvt_data
.ctx
, pvt_data
.session_id
);
251 tee_client_close_context(pvt_data
.ctx
);
256 static const struct tee_client_device_id trusted_key_id_table
[] = {
257 {UUID_INIT(0xf04a0fe7, 0x1f5d, 0x4b9b,
258 0xab, 0xf7, 0x61, 0x9b, 0x85, 0xb4, 0xce, 0x8c)},
261 MODULE_DEVICE_TABLE(tee
, trusted_key_id_table
);
263 static struct tee_client_driver trusted_key_driver
= {
264 .id_table
= trusted_key_id_table
,
267 .bus
= &tee_bus_type
,
268 .probe
= trusted_key_probe
,
269 .remove
= trusted_key_remove
,
273 static int trusted_tee_init(void)
275 return driver_register(&trusted_key_driver
.driver
);
278 static void trusted_tee_exit(void)
280 driver_unregister(&trusted_key_driver
.driver
);
283 struct trusted_key_ops trusted_key_tee_ops
= {
284 .migratable
= 0, /* non-migratable */
285 .init
= trusted_tee_init
,
286 .seal
= trusted_tee_seal
,
287 .unseal
= trusted_tee_unseal
,
288 .get_random
= trusted_tee_get_random
,
289 .exit
= trusted_tee_exit
,