From patchwork Mon Jun 10 12:08:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Garg X-Patchwork-Id: 166313 Delivered-To: patches@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp949451ilk; Mon, 10 Jun 2019 05:09:04 -0700 (PDT) X-Received: by 2002:a17:90a:3ae8:: with SMTP id b95mr20535786pjc.68.1560168544350; Mon, 10 Jun 2019 05:09:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560168544; cv=none; d=google.com; s=arc-20160816; b=c4PO4Kcd2KpgP73EihDRnLwgMDBoYsPjYurD3Hx+De+0kKYBL6gcnAxs9ZWBQTcjSU yCDJK1cGZn8M5vw+uQt75Hqdzii4EM0PVqUGQaH0t82SI0R889Zx55nRTKVnkIkYz6g5 E6ZrOEGuUR+6WyHEqdUOXNt163+jvzSSGPI4NEJ/TxiXngVyLcJmH/iWOY99xZLkRIdV NgStxKal8IMLWpYvoXxpvrX00HK4bRoSGTAaSFhoWRFO2hBHxVljmkz/3jJAgK1XNGxC g95hPjZSLdU81D9cZGaNjxpqbJ9tHTFmRcgmKh+tvcGCemEI8P5pMTO2Qu9rQJIUNX+F BdDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Z8Y6doS9JUUpYqmw0XQfIa+7PVVGFChUlu/7rn/9qbI=; b=KtFxUo0hhKfkOrcJa3u1DdNj67ICI15FevOHGJQC0hnDycKsce9OGCCIO/9wGYE89+ ZvPiMHgDauG6kpFykw35rgb1LGWUSWFUNRGe/W9ANHC7YNsOwe1w7gR01619hjFuOHY/ WZ99sYUnzSjllJ1Hhn/12rHHDIjgrRhOrr/BX+E7jwJCkKewfSNnq83GZrG35IadlJ0Z KMnjGt+ONe1sK4xQiBr2fSN6MJJNKCdzDe0a7od3UQxCFk2c/XxRTPYBE0F+TOF2227w LkvxNvopbpEyO4A5alcBOCwdaPlRsL+HyfIXwpq4/y0KsL0Bgkyy55Qo9mpVtn1HSrl6 cvwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AL140DDz; spf=pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=sumit.garg@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id h1sor8693948pgc.77.2019.06.10.05.09.04 for (Google Transport Security); Mon, 10 Jun 2019 05:09:04 -0700 (PDT) Received-SPF: pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AL140DDz; spf=pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=sumit.garg@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Z8Y6doS9JUUpYqmw0XQfIa+7PVVGFChUlu/7rn/9qbI=; b=AL140DDzZggDmY6z3RmdBI9euEIciWmSyfSKQrBpIBTaiK6mAmS8XsENfEj2VwZdlM 3vLZMlLp+pkhFsytMcS85pFPz57DF2vaMEVKvKQMoDRPOAyNHMgJGeH+l17bFFqMaBAA UZO+6Dt5alnAG6IpMKHwzbbv6lvE+C74x0+etk0LDUso9Ue/smqEeUMYIQnZpCz+uDkK F8QNY6n5KqVNhvr31xdaKNh4zE9IBSl8LoJrOciKjVW/t+CPqNYzReHCaMpDgNyL0lH+ iRWmBf9v60+Rf1PQAGSMsi2BwO09jAp0iK2m1XPieQ3w3/SMT+H9B4A6MWDdkUBnNuWB 5vGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Z8Y6doS9JUUpYqmw0XQfIa+7PVVGFChUlu/7rn/9qbI=; b=sOQuXvecABTA9ohyhTfSydqhLTfxprcFNnFwYhMEbp/A1dvWJ1yrUJnlHC6HytmxHA bIVL/vRX7Wmfu3xyTpzA0SHULJnIaj6jO5kLZABVhRTcc0wdSUZdj+HnXn+84pg0dt+r mKVkY1sOS9FP1TKbz0o6xWnn4HPbl78M090fc278DuQvbdJXEKhgkHpzfR9T4nsYXq7f Nx4tZy1JUjIe76f2xSrlt5E/H6PcoGiaYqjW0tLgE7Sb9hfKXhoAwy7VB5dtbjkq1f/3 zjGoFzJzpye1Joa56m1bemcvHzNeBxaDlOBGr+FlOnUJSxm54pNhtvZJ49z+LUiO0NyD PLZQ== X-Gm-Message-State: APjAAAWVRaWdzgTImy+9aERQZBPMTKqzl+JDF4mEYyUyfdbKJV5WFGPC O26gFjXiMXZ56oL4d0Qeofg48nQw X-Google-Smtp-Source: APXvYqwCMkNcuC3hEogFbWX7oZJbrh2QNkP7o45PsPSYRHGv2k19M8YpQjL/68wkMLpSWyOrc0HYag== X-Received: by 2002:a63:2349:: with SMTP id u9mr7065985pgm.410.1560168543792; Mon, 10 Jun 2019 05:09:03 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([117.196.234.139]) by smtp.gmail.com with ESMTPSA id f7sm2452961pfd.43.2019.06.10.05.09.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 10 Jun 2019 05:09:03 -0700 (PDT) From: Sumit Garg To: daniel.thompson@linaro.org Cc: patches@linaro.org, Sumit Garg Subject: [PATCH 4/7] KEYS: trusted: Introduce TEE based Trusted Keys Date: Mon, 10 Jun 2019 17:38:32 +0530 Message-Id: <1560168515-32714-5-git-send-email-sumit.garg@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1560168515-32714-1-git-send-email-sumit.garg@linaro.org> References: <1560168515-32714-1-git-send-email-sumit.garg@linaro.org> Add support for TEE based trusted keys where TEE provides the functionality to seal and unseal trusted keys using hardware unique key. Refer to Documentation/tee.txt for detailed information about TEE. Approach taken in this patch acts as an alternative to a TPM device in case platform doesn't possess one. Signed-off-by: Sumit Garg --- include/keys/tee_trusted.h | 84 ++++++++ include/keys/trusted-type.h | 1 + security/keys/Kconfig | 3 + security/keys/Makefile | 3 + security/keys/tee_trusted.c | 506 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 597 insertions(+) create mode 100644 include/keys/tee_trusted.h create mode 100644 security/keys/tee_trusted.c -- 2.7.4 diff --git a/include/keys/tee_trusted.h b/include/keys/tee_trusted.h new file mode 100644 index 0000000..e5c0042 --- /dev/null +++ b/include/keys/tee_trusted.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2019 Linaro Ltd. + * + * Author: + * Sumit Garg + */ + +#ifndef __TEE_TRUSTED_KEY_H +#define __TEE_TRUSTED_KEY_H + +#include + +#define DRIVER_NAME "tee-trusted-key" + +/* + * Get random data for symmetric key + * + * [out] memref[0] Random data + * + * Result: + * TEE_SUCCESS - Invoke command success + * TEE_ERROR_BAD_PARAMETERS - Incorrect input param + */ +#define TA_CMD_GET_RANDOM 0x0 + +/* + * Seal trusted key using hardware unique key + * + * [in] memref[0] Plain key + * [out] memref[1] Sealed key datablob + * + * Result: + * TEE_SUCCESS - Invoke command success + * TEE_ERROR_BAD_PARAMETERS - Incorrect input param + */ +#define TA_CMD_SEAL 0x1 + +/* + * Unseal trusted key using hardware unique key + * + * [in] memref[0] Sealed key datablob + * [out] memref[1] Plain key + * + * Result: + * TEE_SUCCESS - Invoke command success + * TEE_ERROR_BAD_PARAMETERS - Incorrect input param + */ +#define TA_CMD_UNSEAL 0x2 + +/** + * struct trusted_key_private - TEE Trusted key private data + * @dev: TEE based Trusted key device. + * @ctx: TEE context handler. + * @session_id: Trusted key TA session identifier. + * @shm_pool: Memory pool shared with TEE device. + */ +struct trusted_key_private { + struct device *dev; + struct tee_context *ctx; + u32 session_id; + u32 data_rate; + struct tee_shm *shm_pool; +}; + +#define TEE_KEY_DEBUG 0 + +#if TEE_KEY_DEBUG +static inline void dump_tee_payload(struct trusted_key_payload *p) +{ + pr_info("trusted_key: key_len %d\n", p->key_len); + print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE, + 16, 1, p->key, p->key_len, 0); + pr_info("trusted_key: bloblen %d\n", p->blob_len); + print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE, + 16, 1, p->blob, p->blob_len, 0); +} +#else +static inline void dump_tee_payload(struct trusted_key_payload *p) +{ +} +#endif + +#endif diff --git a/include/keys/trusted-type.h b/include/keys/trusted-type.h index 4ea7e55..0d3c2dc 100644 --- a/include/keys/trusted-type.h +++ b/include/keys/trusted-type.h @@ -44,5 +44,6 @@ struct trusted_key_options { }; extern struct key_type key_type_trusted; +extern struct key_type key_type_tee_trusted; #endif /* _KEYS_TRUSTED_TYPE_H */ diff --git a/security/keys/Kconfig b/security/keys/Kconfig index 6462e66..7fdd4b4 100644 --- a/security/keys/Kconfig +++ b/security/keys/Kconfig @@ -69,6 +69,9 @@ config TRUSTED_KEYS if the boot PCRs and other criteria match. Userspace will only ever see encrypted blobs. + It also provides support for alternative TEE based Trusted keys + generation and sealing in case TPM isn't present. + If you are unsure as to whether this is required, answer N. config ENCRYPTED_KEYS diff --git a/security/keys/Makefile b/security/keys/Makefile index 9cef540..07ad3e2 100644 --- a/security/keys/Makefile +++ b/security/keys/Makefile @@ -30,3 +30,6 @@ obj-$(CONFIG_ASYMMETRIC_KEY_TYPE) += keyctl_pkey.o obj-$(CONFIG_BIG_KEYS) += big_key.o obj-$(CONFIG_TRUSTED_KEYS) += trusted.o obj-$(CONFIG_ENCRYPTED_KEYS) += encrypted-keys/ +ifdef CONFIG_TEE +obj-$(CONFIG_TRUSTED_KEYS) += tee_trusted.o +endif diff --git a/security/keys/tee_trusted.c b/security/keys/tee_trusted.c new file mode 100644 index 0000000..081e45e --- /dev/null +++ b/security/keys/tee_trusted.c @@ -0,0 +1,506 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2019 Linaro Ltd. + * + * Author: + * Sumit Garg + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +static struct trusted_key_private pvt_data; + +/* + * Have the TEE seal(encrypt) the symmetric key + */ +static int tee_key_seal(struct trusted_key_payload *p) +{ + int ret = 0; + struct tee_ioctl_invoke_arg inv_arg; + struct tee_param param[4]; + struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL; + + memset(&inv_arg, 0, sizeof(inv_arg)); + memset(¶m, 0, sizeof(param)); + + reg_shm_in = tee_shm_register(pvt_data.ctx, (unsigned long)p->key, + p->key_len, TEE_SHM_DMA_BUF | + TEE_SHM_KERNEL_MAPPED); + if (IS_ERR(reg_shm_in)) { + dev_err(pvt_data.dev, "key shm register failed\n"); + return PTR_ERR(reg_shm_in); + } + + reg_shm_out = tee_shm_register(pvt_data.ctx, (unsigned long)p->blob, + sizeof(p->blob), TEE_SHM_DMA_BUF | + TEE_SHM_KERNEL_MAPPED); + if (IS_ERR(reg_shm_out)) { + dev_err(pvt_data.dev, "blob shm register failed\n"); + ret = PTR_ERR(reg_shm_out); + goto out; + } + + inv_arg.func = TA_CMD_SEAL; + inv_arg.session = pvt_data.session_id; + inv_arg.num_params = 4; + + param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT; + param[0].u.memref.shm = reg_shm_in; + param[0].u.memref.size = p->key_len; + param[0].u.memref.shm_offs = 0; + param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT; + param[1].u.memref.shm = reg_shm_out; + param[1].u.memref.size = sizeof(p->blob); + param[1].u.memref.shm_offs = 0; + + ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param); + if ((ret < 0) || (inv_arg.ret != 0)) { + dev_err(pvt_data.dev, "TA_CMD_SEAL invoke err: %x\n", + inv_arg.ret); + ret = -EFAULT; + } else { + p->blob_len = param[1].u.memref.size; + } + +out: + if (reg_shm_out) + tee_shm_free(reg_shm_out); + if (reg_shm_in) + tee_shm_free(reg_shm_in); + + return ret; +} + +/* + * Have the TEE unseal(decrypt) the symmetric key + */ +static int tee_key_unseal(struct trusted_key_payload *p) +{ + int ret = 0; + struct tee_ioctl_invoke_arg inv_arg; + struct tee_param param[4]; + struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL; + + memset(&inv_arg, 0, sizeof(inv_arg)); + memset(¶m, 0, sizeof(param)); + + reg_shm_in = tee_shm_register(pvt_data.ctx, (unsigned long)p->blob, + p->blob_len, TEE_SHM_DMA_BUF | + TEE_SHM_KERNEL_MAPPED); + if (IS_ERR(reg_shm_in)) { + dev_err(pvt_data.dev, "blob shm register failed\n"); + return PTR_ERR(reg_shm_in); + } + + reg_shm_out = tee_shm_register(pvt_data.ctx, (unsigned long)p->key, + sizeof(p->key), TEE_SHM_DMA_BUF | + TEE_SHM_KERNEL_MAPPED); + if (IS_ERR(reg_shm_out)) { + dev_err(pvt_data.dev, "key shm register failed\n"); + ret = PTR_ERR(reg_shm_out); + goto out; + } + + inv_arg.func = TA_CMD_UNSEAL; + inv_arg.session = pvt_data.session_id; + inv_arg.num_params = 4; + + param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT; + param[0].u.memref.shm = reg_shm_in; + param[0].u.memref.size = p->blob_len; + param[0].u.memref.shm_offs = 0; + param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT; + param[1].u.memref.shm = reg_shm_out; + param[1].u.memref.size = sizeof(p->key); + param[1].u.memref.shm_offs = 0; + + ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param); + if ((ret < 0) || (inv_arg.ret != 0)) { + dev_err(pvt_data.dev, "TA_CMD_UNSEAL invoke err: %x\n", + inv_arg.ret); + ret = -EFAULT; + } else { + p->key_len = param[1].u.memref.size; + } + +out: + if (reg_shm_out) + tee_shm_free(reg_shm_out); + if (reg_shm_in) + tee_shm_free(reg_shm_in); + + return ret; +} + +/* + * Have the TEE generate random symmetric key + */ +static int tee_get_random(unsigned char *key, unsigned int key_len) +{ + int ret = 0; + struct tee_ioctl_invoke_arg inv_arg; + struct tee_param param[4]; + struct tee_shm *reg_shm = NULL; + + memset(&inv_arg, 0, sizeof(inv_arg)); + memset(¶m, 0, sizeof(param)); + + reg_shm = tee_shm_register(pvt_data.ctx, (unsigned long)key, key_len, + TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED); + if (IS_ERR(reg_shm)) { + dev_err(pvt_data.dev, "random key shm register failed\n"); + return PTR_ERR(reg_shm); + } + + inv_arg.func = TA_CMD_GET_RANDOM; + inv_arg.session = pvt_data.session_id; + inv_arg.num_params = 4; + + param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT; + param[0].u.memref.shm = reg_shm; + param[0].u.memref.size = key_len; + param[0].u.memref.shm_offs = 0; + + ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param); + if ((ret < 0) || (inv_arg.ret != 0)) { + dev_err(pvt_data.dev, "TA_CMD_GET_RANDOM invoke err: %x\n", + inv_arg.ret); + ret = -EFAULT; + } else { + ret = param[0].u.memref.size; + } + + tee_shm_free(reg_shm); + + return ret; +} + +enum { + Opt_err, + Opt_new, Opt_load +}; + +static const match_table_t key_tokens = { + {Opt_new, "new"}, + {Opt_load, "load"}, + {Opt_err, NULL} +}; + +/* + * datablob_parse - parse the keyctl data and fill in the + * payload structure + * + * On success returns 0, otherwise -EINVAL. + */ +static int datablob_parse(char *datablob, struct trusted_key_payload *p) +{ + substring_t args[MAX_OPT_ARGS]; + long keylen; + int ret = -EINVAL; + int key_cmd; + char *c; + + /* main command */ + c = strsep(&datablob, " \t"); + if (!c) + return -EINVAL; + + key_cmd = match_token(c, key_tokens, args); + switch (key_cmd) { + case Opt_new: + /* first argument is key size */ + c = strsep(&datablob, " \t"); + if (!c) + return -EINVAL; + ret = kstrtol(c, 10, &keylen); + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE) + return -EINVAL; + p->key_len = keylen; + ret = Opt_new; + break; + case Opt_load: + /* first argument is sealed blob */ + c = strsep(&datablob, " \t"); + if (!c) + return -EINVAL; + p->blob_len = strlen(c) / 2; + if (p->blob_len > MAX_BLOB_SIZE) + return -EINVAL; + ret = hex2bin(p->blob, c, p->blob_len); + if (ret < 0) + return -EINVAL; + ret = Opt_load; + break; + case Opt_err: + return -EINVAL; + } + + return ret; +} + +static struct trusted_key_payload *trusted_payload_alloc(struct key *key) +{ + struct trusted_key_payload *p = NULL; + int ret; + + ret = key_payload_reserve(key, sizeof(*p)); + if (ret < 0) + return p; + + p = kzalloc(sizeof(*p), GFP_KERNEL); + + return p; +} + +/* + * trusted_instantiate - create a new trusted key + * + * Unseal an existing trusted blob or, for a new key, get a + * random key, then seal and create a trusted key-type key, + * adding it to the specified keyring. + * + * On success, return 0. Otherwise return errno. + */ +static int trusted_instantiate(struct key *key, + struct key_preparsed_payload *prep) +{ + struct trusted_key_payload *payload = NULL; + size_t datalen = prep->datalen; + char *datablob; + int ret = 0; + int key_cmd; + size_t key_len; + + if (datalen <= 0 || datalen > 32767 || !prep->data) + return -EINVAL; + + datablob = kmalloc(datalen + 1, GFP_KERNEL); + if (!datablob) + return -ENOMEM; + memcpy(datablob, prep->data, datalen); + datablob[datalen] = '\0'; + + payload = trusted_payload_alloc(key); + if (!payload) { + ret = -ENOMEM; + goto out; + } + + key_cmd = datablob_parse(datablob, payload); + if (key_cmd < 0) { + ret = key_cmd; + goto out; + } + + dump_tee_payload(payload); + + switch (key_cmd) { + case Opt_load: + ret = tee_key_unseal(payload); + dump_tee_payload(payload); + if (ret < 0) + dev_err(pvt_data.dev, "key_unseal failed (%d)\n", ret); + break; + case Opt_new: + key_len = payload->key_len; + ret = tee_get_random(payload->key, key_len); + if (ret != key_len) { + dev_err(pvt_data.dev, "key_create failed (%d)\n", ret); + goto out; + } + + ret = tee_key_seal(payload); + if (ret < 0) + dev_err(pvt_data.dev, "key_seal failed (%d)\n", ret); + dump_tee_payload(payload); + break; + default: + ret = -EINVAL; + goto out; + } +out: + kzfree(datablob); + if (!ret) + rcu_assign_keypointer(key, payload); + else + kzfree(payload); + return ret; +} + +static int trusted_update(struct key *key, struct key_preparsed_payload *prep) +{ + dev_info(pvt_data.dev, "trusted key update method not supported\n"); + + return -EINVAL; +} + +/* + * trusted_read - copy the sealed blob data to userspace in hex. + * On success, return to userspace the trusted key datablob size. + */ +static long trusted_read(const struct key *key, char __user *buffer, + size_t buflen) +{ + const struct trusted_key_payload *p; + char *ascii_buf; + char *bufp; + int i; + + p = dereference_key_locked(key); + if (!p) + return -EINVAL; + + if (buffer && buflen >= 2 * p->blob_len) { + ascii_buf = kmalloc_array(2, p->blob_len, GFP_KERNEL); + if (!ascii_buf) + return -ENOMEM; + + bufp = ascii_buf; + for (i = 0; i < p->blob_len; i++) + bufp = hex_byte_pack(bufp, p->blob[i]); + if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) { + kzfree(ascii_buf); + return -EFAULT; + } + kzfree(ascii_buf); + } + return 2 * p->blob_len; +} + +/* + * trusted_destroy - clear and free the key's payload + */ +static void trusted_destroy(struct key *key) +{ + kzfree(key->payload.data[0]); +} + +struct key_type key_type_tee_trusted = { + .name = "trusted", + .instantiate = trusted_instantiate, + .update = trusted_update, + .destroy = trusted_destroy, + .describe = user_describe, + .read = trusted_read, +}; +EXPORT_SYMBOL_GPL(key_type_tee_trusted); + +static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data) +{ + if (ver->impl_id == TEE_IMPL_ID_OPTEE) + return 1; + else + return 0; +} + +static int trusted_key_probe(struct device *dev) +{ + struct tee_client_device *rng_device = to_tee_client_device(dev); + int ret = 0, err = -ENODEV; + struct tee_ioctl_open_session_arg sess_arg; + + memset(&sess_arg, 0, sizeof(sess_arg)); + + /* Open context with TEE driver */ + pvt_data.ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, + NULL); + if (IS_ERR(pvt_data.ctx)) + return -ENODEV; + + /* Open session with hwrng Trusted App */ + memcpy(sess_arg.uuid, rng_device->id.uuid.b, TEE_IOCTL_UUID_LEN); + sess_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL; + sess_arg.num_params = 0; + + ret = tee_client_open_session(pvt_data.ctx, &sess_arg, NULL); + if ((ret < 0) || (sess_arg.ret != 0)) { + dev_err(dev, "tee_client_open_session failed, err: %x\n", + sess_arg.ret); + err = -EINVAL; + goto out_ctx; + } + pvt_data.session_id = sess_arg.session; + + ret = register_key_type(&key_type_tee_trusted); + if (ret < 0) + goto out_sess; + + pvt_data.dev = dev; + + return 0; + +out_sess: + tee_client_close_session(pvt_data.ctx, pvt_data.session_id); +out_ctx: + tee_client_close_context(pvt_data.ctx); + + return err; +} + +static int trusted_key_remove(struct device *dev) +{ + unregister_key_type(&key_type_tee_trusted); + tee_client_close_session(pvt_data.ctx, pvt_data.session_id); + tee_client_close_context(pvt_data.ctx); + + return 0; +} + +static const struct tee_client_device_id trusted_key_id_table[] = { + {UUID_INIT(0xf04a0fe7, 0x1f5d, 0x4b9b, + 0xab, 0xf7, 0x61, 0x9b, 0x85, 0xb4, 0xce, 0x8c)}, + {} +}; + +MODULE_DEVICE_TABLE(tee, trusted_key_id_table); + +static struct tee_client_driver trusted_key_driver = { + .id_table = trusted_key_id_table, + .driver = { + .name = DRIVER_NAME, + .bus = &tee_bus_type, + .probe = trusted_key_probe, + .remove = trusted_key_remove, + }, +}; + +static int __init init_tee_trusted(void) +{ + struct tpm_chip *chip; + + /* + * Check for TPM availability as that is default source for trusted + * keys. If not present, then register driver for TEE based device + * providing support for trusted keys. + */ + chip = tpm_default_chip(); + if (chip) + return 0; + + return driver_register(&trusted_key_driver.driver); +} + +static void __exit cleanup_tee_trusted(void) +{ + driver_unregister(&trusted_key_driver.driver); +} + +late_initcall(init_tee_trusted); +module_exit(cleanup_tee_trusted); + +MODULE_LICENSE("GPL v2"); +MODULE_AUTHOR("Sumit Garg "); +MODULE_DESCRIPTION("TEE based trusted keys");