Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2018-2019 Linaro Ltd.
0004  */
0005 
0006 #include <linux/delay.h>
0007 #include <linux/of.h>
0008 #include <linux/hw_random.h>
0009 #include <linux/kernel.h>
0010 #include <linux/module.h>
0011 #include <linux/slab.h>
0012 #include <linux/tee_drv.h>
0013 #include <linux/uuid.h>
0014 
0015 #define DRIVER_NAME "optee-rng"
0016 
0017 #define TEE_ERROR_HEALTH_TEST_FAIL  0x00000001
0018 
0019 /*
0020  * TA_CMD_GET_ENTROPY - Get Entropy from RNG
0021  *
0022  * param[0] (inout memref) - Entropy buffer memory reference
0023  * param[1] unused
0024  * param[2] unused
0025  * param[3] unused
0026  *
0027  * Result:
0028  * TEE_SUCCESS - Invoke command success
0029  * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
0030  * TEE_ERROR_NOT_SUPPORTED - Requested entropy size greater than size of pool
0031  * TEE_ERROR_HEALTH_TEST_FAIL - Continuous health testing failed
0032  */
0033 #define TA_CMD_GET_ENTROPY      0x0
0034 
0035 /*
0036  * TA_CMD_GET_RNG_INFO - Get RNG information
0037  *
0038  * param[0] (out value) - value.a: RNG data-rate in bytes per second
0039  *                        value.b: Quality/Entropy per 1024 bit of data
0040  * param[1] unused
0041  * param[2] unused
0042  * param[3] unused
0043  *
0044  * Result:
0045  * TEE_SUCCESS - Invoke command success
0046  * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
0047  */
0048 #define TA_CMD_GET_RNG_INFO     0x1
0049 
0050 #define MAX_ENTROPY_REQ_SZ      (4 * 1024)
0051 
0052 /**
0053  * struct optee_rng_private - OP-TEE Random Number Generator private data
0054  * @dev:        OP-TEE based RNG device.
0055  * @ctx:        OP-TEE context handler.
0056  * @session_id:     RNG TA session identifier.
0057  * @data_rate:      RNG data rate.
0058  * @entropy_shm_pool:   Memory pool shared with RNG device.
0059  * @optee_rng:      OP-TEE RNG driver structure.
0060  */
0061 struct optee_rng_private {
0062     struct device *dev;
0063     struct tee_context *ctx;
0064     u32 session_id;
0065     u32 data_rate;
0066     struct tee_shm *entropy_shm_pool;
0067     struct hwrng optee_rng;
0068 };
0069 
0070 #define to_optee_rng_private(r) \
0071         container_of(r, struct optee_rng_private, optee_rng)
0072 
0073 static size_t get_optee_rng_data(struct optee_rng_private *pvt_data,
0074                  void *buf, size_t req_size)
0075 {
0076     int ret = 0;
0077     u8 *rng_data = NULL;
0078     size_t rng_size = 0;
0079     struct tee_ioctl_invoke_arg inv_arg;
0080     struct tee_param param[4];
0081 
0082     memset(&inv_arg, 0, sizeof(inv_arg));
0083     memset(&param, 0, sizeof(param));
0084 
0085     /* Invoke TA_CMD_GET_ENTROPY function of Trusted App */
0086     inv_arg.func = TA_CMD_GET_ENTROPY;
0087     inv_arg.session = pvt_data->session_id;
0088     inv_arg.num_params = 4;
0089 
0090     /* Fill invoke cmd params */
0091     param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT;
0092     param[0].u.memref.shm = pvt_data->entropy_shm_pool;
0093     param[0].u.memref.size = req_size;
0094     param[0].u.memref.shm_offs = 0;
0095 
0096     ret = tee_client_invoke_func(pvt_data->ctx, &inv_arg, param);
0097     if ((ret < 0) || (inv_arg.ret != 0)) {
0098         dev_err(pvt_data->dev, "TA_CMD_GET_ENTROPY invoke err: %x\n",
0099             inv_arg.ret);
0100         return 0;
0101     }
0102 
0103     rng_data = tee_shm_get_va(pvt_data->entropy_shm_pool, 0);
0104     if (IS_ERR(rng_data)) {
0105         dev_err(pvt_data->dev, "tee_shm_get_va failed\n");
0106         return 0;
0107     }
0108 
0109     rng_size = param[0].u.memref.size;
0110     memcpy(buf, rng_data, rng_size);
0111 
0112     return rng_size;
0113 }
0114 
0115 static int optee_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
0116 {
0117     struct optee_rng_private *pvt_data = to_optee_rng_private(rng);
0118     size_t read = 0, rng_size;
0119     int timeout = 1;
0120     u8 *data = buf;
0121 
0122     if (max > MAX_ENTROPY_REQ_SZ)
0123         max = MAX_ENTROPY_REQ_SZ;
0124 
0125     while (read < max) {
0126         rng_size = get_optee_rng_data(pvt_data, data, (max - read));
0127 
0128         data += rng_size;
0129         read += rng_size;
0130 
0131         if (wait && pvt_data->data_rate) {
0132             if ((timeout-- == 0) || (read == max))
0133                 return read;
0134             msleep((1000 * (max - read)) / pvt_data->data_rate);
0135         } else {
0136             return read;
0137         }
0138     }
0139 
0140     return read;
0141 }
0142 
0143 static int optee_rng_init(struct hwrng *rng)
0144 {
0145     struct optee_rng_private *pvt_data = to_optee_rng_private(rng);
0146     struct tee_shm *entropy_shm_pool = NULL;
0147 
0148     entropy_shm_pool = tee_shm_alloc_kernel_buf(pvt_data->ctx,
0149                             MAX_ENTROPY_REQ_SZ);
0150     if (IS_ERR(entropy_shm_pool)) {
0151         dev_err(pvt_data->dev, "tee_shm_alloc_kernel_buf failed\n");
0152         return PTR_ERR(entropy_shm_pool);
0153     }
0154 
0155     pvt_data->entropy_shm_pool = entropy_shm_pool;
0156 
0157     return 0;
0158 }
0159 
0160 static void optee_rng_cleanup(struct hwrng *rng)
0161 {
0162     struct optee_rng_private *pvt_data = to_optee_rng_private(rng);
0163 
0164     tee_shm_free(pvt_data->entropy_shm_pool);
0165 }
0166 
0167 static struct optee_rng_private pvt_data = {
0168     .optee_rng = {
0169         .name       = DRIVER_NAME,
0170         .init       = optee_rng_init,
0171         .cleanup    = optee_rng_cleanup,
0172         .read       = optee_rng_read,
0173     }
0174 };
0175 
0176 static int get_optee_rng_info(struct device *dev)
0177 {
0178     int ret = 0;
0179     struct tee_ioctl_invoke_arg inv_arg;
0180     struct tee_param param[4];
0181 
0182     memset(&inv_arg, 0, sizeof(inv_arg));
0183     memset(&param, 0, sizeof(param));
0184 
0185     /* Invoke TA_CMD_GET_RNG_INFO function of Trusted App */
0186     inv_arg.func = TA_CMD_GET_RNG_INFO;
0187     inv_arg.session = pvt_data.session_id;
0188     inv_arg.num_params = 4;
0189 
0190     /* Fill invoke cmd params */
0191     param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT;
0192 
0193     ret = tee_client_invoke_func(pvt_data.ctx, &inv_arg, param);
0194     if ((ret < 0) || (inv_arg.ret != 0)) {
0195         dev_err(dev, "TA_CMD_GET_RNG_INFO invoke err: %x\n",
0196             inv_arg.ret);
0197         return -EINVAL;
0198     }
0199 
0200     pvt_data.data_rate = param[0].u.value.a;
0201     pvt_data.optee_rng.quality = param[0].u.value.b;
0202 
0203     return 0;
0204 }
0205 
0206 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
0207 {
0208     if (ver->impl_id == TEE_IMPL_ID_OPTEE)
0209         return 1;
0210     else
0211         return 0;
0212 }
0213 
0214 static int optee_rng_probe(struct device *dev)
0215 {
0216     struct tee_client_device *rng_device = to_tee_client_device(dev);
0217     int ret = 0, err = -ENODEV;
0218     struct tee_ioctl_open_session_arg sess_arg;
0219 
0220     memset(&sess_arg, 0, sizeof(sess_arg));
0221 
0222     /* Open context with TEE driver */
0223     pvt_data.ctx = tee_client_open_context(NULL, optee_ctx_match, NULL,
0224                            NULL);
0225     if (IS_ERR(pvt_data.ctx))
0226         return -ENODEV;
0227 
0228     /* Open session with hwrng Trusted App */
0229     export_uuid(sess_arg.uuid, &rng_device->id.uuid);
0230     sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
0231     sess_arg.num_params = 0;
0232 
0233     ret = tee_client_open_session(pvt_data.ctx, &sess_arg, NULL);
0234     if ((ret < 0) || (sess_arg.ret != 0)) {
0235         dev_err(dev, "tee_client_open_session failed, err: %x\n",
0236             sess_arg.ret);
0237         err = -EINVAL;
0238         goto out_ctx;
0239     }
0240     pvt_data.session_id = sess_arg.session;
0241 
0242     err = get_optee_rng_info(dev);
0243     if (err)
0244         goto out_sess;
0245 
0246     err = devm_hwrng_register(dev, &pvt_data.optee_rng);
0247     if (err) {
0248         dev_err(dev, "hwrng registration failed (%d)\n", err);
0249         goto out_sess;
0250     }
0251 
0252     pvt_data.dev = dev;
0253 
0254     return 0;
0255 
0256 out_sess:
0257     tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
0258 out_ctx:
0259     tee_client_close_context(pvt_data.ctx);
0260 
0261     return err;
0262 }
0263 
0264 static int optee_rng_remove(struct device *dev)
0265 {
0266     tee_client_close_session(pvt_data.ctx, pvt_data.session_id);
0267     tee_client_close_context(pvt_data.ctx);
0268 
0269     return 0;
0270 }
0271 
0272 static const struct tee_client_device_id optee_rng_id_table[] = {
0273     {UUID_INIT(0xab7a617c, 0xb8e7, 0x4d8f,
0274            0x83, 0x01, 0xd0, 0x9b, 0x61, 0x03, 0x6b, 0x64)},
0275     {}
0276 };
0277 
0278 MODULE_DEVICE_TABLE(tee, optee_rng_id_table);
0279 
0280 static struct tee_client_driver optee_rng_driver = {
0281     .id_table   = optee_rng_id_table,
0282     .driver     = {
0283         .name       = DRIVER_NAME,
0284         .bus        = &tee_bus_type,
0285         .probe      = optee_rng_probe,
0286         .remove     = optee_rng_remove,
0287     },
0288 };
0289 
0290 static int __init optee_rng_mod_init(void)
0291 {
0292     return driver_register(&optee_rng_driver.driver);
0293 }
0294 
0295 static void __exit optee_rng_mod_exit(void)
0296 {
0297     driver_unregister(&optee_rng_driver.driver);
0298 }
0299 
0300 module_init(optee_rng_mod_init);
0301 module_exit(optee_rng_mod_exit);
0302 
0303 MODULE_LICENSE("GPL v2");
0304 MODULE_AUTHOR("Sumit Garg <sumit.garg@linaro.org>");
0305 MODULE_DESCRIPTION("OP-TEE based random number generator driver");