* [PATCH] tpm: Move Linux RNG connection to hwrng
@ 2017-10-24 18:42 Jason Gunthorpe
2017-10-24 21:34 ` Jason Gunthorpe
2017-10-25 14:45 ` PrasannaKumar Muralidharan
0 siblings, 2 replies; 10+ messages in thread
From: Jason Gunthorpe @ 2017-10-24 18:42 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: PrasannaKumar Muralidharan, Stefan Berger, linux-integrity,
David Howells, Herbert Xu, Dmitry Kasatkin, open list,
linux-security-module, open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
The tpm-rng.c approach is completely inconsistent with how the kernel
handles hotplug. Instead manage a hwrng device for each TPM. This will
cause the kernel to read entropy from the TPM when it is plugged in,
and allow access to the TPM rng via /dev/hwrng.
Signed-off-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
Reviewed-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
---
drivers/char/hw_random/Kconfig | 13 -----------
drivers/char/hw_random/Makefile | 1 -
drivers/char/hw_random/tpm-rng.c | 50 ----------------------------------------
drivers/char/tpm/Kconfig | 13 +++++++++++
drivers/char/tpm/tpm-chip.c | 41 ++++++++++++++++++++++++++++----
drivers/char/tpm/tpm-interface.c | 37 ++++++++++++++++-------------
drivers/char/tpm/tpm.h | 5 ++++
7 files changed, 76 insertions(+), 84 deletions(-)
delete mode 100644 drivers/char/hw_random/tpm-rng.c
All,
It is such a good idea, I took PrasannaKumar's patch, reviewed and
revised it to the level it could be merged.
This is compile tested only.
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index 95a031e9eced07..a20fed182cbcce 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -306,19 +306,6 @@ config HW_RANDOM_POWERNV
If unsure, say Y.
-config HW_RANDOM_TPM
- tristate "TPM HW Random Number Generator support"
- depends on TCG_TPM
- default HW_RANDOM
- ---help---
- This driver provides kernel-side support for the Random Number
- Generator in the Trusted Platform Module
-
- To compile this driver as a module, choose M here: the
- module will be called tpm-rng.
-
- If unsure, say Y.
-
config HW_RANDOM_HISI
tristate "Hisilicon Random Number Generator support"
depends on HW_RANDOM && ARCH_HISI
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 39a67defac67cb..91cb8e8213e7c1 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -26,7 +26,6 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
obj-$(CONFIG_HW_RANDOM_HISI) += hisi-rng.o
-obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
diff --git a/drivers/char/hw_random/tpm-rng.c b/drivers/char/hw_random/tpm-rng.c
deleted file mode 100644
index d6d448266f0752..00000000000000
--- a/drivers/char/hw_random/tpm-rng.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2012 Kent Yoder IBM Corporation
- *
- * HWRNG interfaces to pull RNG data from a TPM
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
-#include <linux/module.h>
-#include <linux/hw_random.h>
-#include <linux/tpm.h>
-
-#define MODULE_NAME "tpm-rng"
-
-static int tpm_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
-{
- return tpm_get_random(TPM_ANY_NUM, data, max);
-}
-
-static struct hwrng tpm_rng = {
- .name = MODULE_NAME,
- .read = tpm_rng_read,
-};
-
-static int __init rng_init(void)
-{
- return hwrng_register(&tpm_rng);
-}
-module_init(rng_init);
-
-static void __exit rng_exit(void)
-{
- hwrng_unregister(&tpm_rng);
-}
-module_exit(rng_exit);
-
-MODULE_LICENSE("GPL v2");
-MODULE_AUTHOR("Kent Yoder <key@linux.vnet.ibm.com>");
-MODULE_DESCRIPTION("RNG driver for TPM devices");
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index a30352202f1fdc..a95725fa77789e 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -26,6 +26,19 @@ menuconfig TCG_TPM
if TCG_TPM
+config HW_RANDOM_TPM
+ tristate "TPM HW Random Number Generator support"
+ depends on TCG_TPM && HW_RANDOM
+ default HW_RANDOM
+ ---help---
+ This driver provides kernel-side support for the Random Number
+ Generator in the Trusted Platform Module
+
+ To compile this driver as a module, choose M here: the
+ module will be called tpm-rng.
+
+ If unsure, say Y.
+
config TCG_TIS_CORE
tristate
---help---
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index 0eca20c5a80cf2..f3571406fb2995 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -26,6 +26,7 @@
#include <linux/spinlock.h>
#include <linux/freezer.h>
#include <linux/major.h>
+#include <linux/hw_random.h>
#include "tpm.h"
#include "tpm_eventlog.h"
@@ -387,6 +388,26 @@ static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
return 0;
}
+
+static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
+{
+ struct tpm_chip *chip = container_of(rng, struct tpm_chip, tpm_hwrng);
+
+ return tpm_get_random_dev(chip, data, max);
+}
+
+static int tpm_add_hwrng(struct tpm_chip *chip)
+{
+ if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+ return 0;
+
+ snprintf(chip->tpm_hwrng_name, sizeof(chip->tpm_hwrng_name),
+ "tpm-rng-%d", chip->dev_num);
+ chip->tpm_hwrng.name = chip->tpm_hwrng_name;
+ chip->tpm_hwrng.read = tpm_hwrng_read;
+ return hwrng_register(&chip->tpm_hwrng);
+}
+
/*
* tpm_chip_register() - create a character device for the TPM chip
* @chip: TPM chip to use.
@@ -419,11 +440,13 @@ int tpm_chip_register(struct tpm_chip *chip)
tpm_add_ppi(chip);
+ rc = tpm_add_hwrng(chip);
+ if (rc)
+ goto out_ppi;
+
rc = tpm_add_char_device(chip);
- if (rc) {
- tpm_bios_log_teardown(chip);
- return rc;
- }
+ if (rc)
+ goto out_hwrng;
rc = tpm_add_legacy_sysfs(chip);
if (rc) {
@@ -432,6 +455,14 @@ int tpm_chip_register(struct tpm_chip *chip)
}
return 0;
+
+out_hwrng:
+ if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+ hwrng_unregister(&chip->tpm_hwrng);
+out_ppi:
+ tpm_bios_log_teardown(chip);
+
+ return rc;
}
EXPORT_SYMBOL_GPL(tpm_chip_register);
@@ -451,6 +482,8 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
void tpm_chip_unregister(struct tpm_chip *chip)
{
tpm_del_legacy_sysfs(chip);
+ if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
+ hwrng_unregister(&chip->tpm_hwrng);
tpm_bios_log_teardown(chip);
if (chip->flags & TPM_CHIP_FLAG_TPM2)
cdev_device_del(&chip->cdevs, &chip->devs);
diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index 1d6729be4cd637..310de288549818 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -1194,9 +1194,8 @@ static const struct tpm_input_header tpm_getrandom_header = {
*
* Returns < 0 on error and the number of bytes read on success
*/
-int tpm_get_random(u32 chip_num, u8 *out, size_t max)
+int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max)
{
- struct tpm_chip *chip;
struct tpm_cmd_t tpm_cmd;
u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
int err, total = 0, retries = 5;
@@ -1205,15 +1204,8 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
return -EINVAL;
- chip = tpm_chip_find_get(chip_num);
- if (chip == NULL)
- return -ENODEV;
-
- if (chip->flags & TPM_CHIP_FLAG_TPM2) {
- err = tpm2_get_random(chip, out, max);
- tpm_put_ops(chip);
- return err;
- }
+ if (chip->flags & TPM_CHIP_FLAG_TPM2)
+ return tpm2_get_random(chip, out, max);
do {
tpm_cmd.header.in = tpm_getrandom_header;
@@ -1231,10 +1223,9 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
rlength = be32_to_cpu(tpm_cmd.header.out.length);
if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
- recd) {
- total = -EFAULT;
- break;
- }
+ recd)
+ return -EFAULT;
+
memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
dest += recd;
@@ -1242,9 +1233,23 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
num_bytes -= recd;
} while (retries-- && total < max);
- tpm_put_ops(chip);
return total ? total : -EIO;
}
+
+int tpm_get_random(u32 chip_num, u8 *out, size_t max)
+{
+ int rc;
+ struct tpm_chip *chip;
+
+ chip = tpm_chip_find_get(chip_num);
+ if (chip == NULL)
+ return -ENODEV;
+
+ rc = tpm_get_random_dev(chip, out, max);
+
+ tpm_put_ops(chip);
+ return rc;
+}
EXPORT_SYMBOL_GPL(tpm_get_random);
/**
diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
index 2d5466a72e40f8..0e41e044d81be2 100644
--- a/drivers/char/tpm/tpm.h
+++ b/drivers/char/tpm/tpm.h
@@ -26,6 +26,7 @@
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/fs.h>
+#include <linux/hw_random.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/platform_device.h>
@@ -210,6 +211,9 @@ struct tpm_chip {
int dev_num; /* /dev/tpm# */
unsigned long is_open; /* only one allowed */
+ char tpm_hwrng_name[64];
+ struct hwrng tpm_hwrng;
+
struct mutex tpm_mutex; /* tpm is processing */
unsigned long timeout_a; /* jiffies */
@@ -548,6 +552,7 @@ void tpm_chip_unregister(struct tpm_chip *chip);
void tpm_sysfs_add_device(struct tpm_chip *chip);
int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
+int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max);
#ifdef CONFIG_ACPI
extern void tpm_add_ppi(struct tpm_chip *chip);
--
2.7.4
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-24 18:42 [PATCH] tpm: Move Linux RNG connection to hwrng Jason Gunthorpe
@ 2017-10-24 21:34 ` Jason Gunthorpe
2017-10-24 22:29 ` Jarkko Sakkinen
2017-10-25 14:45 ` PrasannaKumar Muralidharan
1 sibling, 1 reply; 10+ messages in thread
From: Jason Gunthorpe @ 2017-10-24 21:34 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: PrasannaKumar Muralidharan, Stefan Berger, linux-integrity,
David Howells, Herbert Xu, Dmitry Kasatkin, open list,
linux-security-module, open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Tue, Oct 24, 2017 at 12:42:35PM -0600, Jason Gunthorpe wrote:
> This is compile tested only.
0day says the kconfig has a problem when randomized, here is the fix I
will roll into a v2 in a few days:
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index a95725fa77789e..ca89da3e4b2ae9 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -27,16 +27,13 @@ menuconfig TCG_TPM
if TCG_TPM
config HW_RANDOM_TPM
- tristate "TPM HW Random Number Generator support"
- depends on TCG_TPM && HW_RANDOM
- default HW_RANDOM
+ bool "TPM HW Random Number Generator support"
+ depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)
+ default y
---help---
This driver provides kernel-side support for the Random Number
Generator in the Trusted Platform Module
- To compile this driver as a module, choose M here: the
- module will be called tpm-rng.
-
If unsure, say Y.
config TCG_TIS_CORE
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-24 21:34 ` Jason Gunthorpe
@ 2017-10-24 22:29 ` Jarkko Sakkinen
0 siblings, 0 replies; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-10-24 22:29 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: PrasannaKumar Muralidharan, Stefan Berger, linux-integrity,
David Howells, Herbert Xu, Dmitry Kasatkin, open list,
linux-security-module, open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Tue, Oct 24, 2017 at 03:34:49PM -0600, Jason Gunthorpe wrote:
> On Tue, Oct 24, 2017 at 12:42:35PM -0600, Jason Gunthorpe wrote:
>
> > This is compile tested only.
>
> 0day says the kconfig has a problem when randomized, here is the fix I
> will roll into a v2 in a few days:
I will probably have to postpone the review to next week anyway so take
your time :-)
/Jarkko
>
> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
> index a95725fa77789e..ca89da3e4b2ae9 100644
> --- a/drivers/char/tpm/Kconfig
> +++ b/drivers/char/tpm/Kconfig
> @@ -27,16 +27,13 @@ menuconfig TCG_TPM
> if TCG_TPM
>
> config HW_RANDOM_TPM
> - tristate "TPM HW Random Number Generator support"
> - depends on TCG_TPM && HW_RANDOM
> - default HW_RANDOM
> + bool "TPM HW Random Number Generator support"
> + depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)
> + default y
> ---help---
> This driver provides kernel-side support for the Random Number
> Generator in the Trusted Platform Module
>
> - To compile this driver as a module, choose M here: the
> - module will be called tpm-rng.
> -
> If unsure, say Y.
>
> config TCG_TIS_CORE
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-24 18:42 [PATCH] tpm: Move Linux RNG connection to hwrng Jason Gunthorpe
2017-10-24 21:34 ` Jason Gunthorpe
@ 2017-10-25 14:45 ` PrasannaKumar Muralidharan
2017-10-25 15:18 ` Jason Gunthorpe
2017-10-25 18:58 ` Jarkko Sakkinen
1 sibling, 2 replies; 10+ messages in thread
From: PrasannaKumar Muralidharan @ 2017-10-25 14:45 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Jarkko Sakkinen, Stefan Berger, linux-integrity, David Howells,
Herbert Xu, Dmitry Kasatkin, open list, linux-security-module,
open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
Hi Jason,
Nice to see this patch. Some minor comments below.
On 25 October 2017 at 00:12, Jason Gunthorpe
<jgunthorpe@obsidianresearch.com> wrote:
> The tpm-rng.c approach is completely inconsistent with how the kernel
> handles hotplug. Instead manage a hwrng device for each TPM. This will
> cause the kernel to read entropy from the TPM when it is plugged in,
> and allow access to the TPM rng via /dev/hwrng.
>
> Signed-off-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
> Signed-off-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
> Reviewed-by: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
> ---
> drivers/char/hw_random/Kconfig | 13 -----------
> drivers/char/hw_random/Makefile | 1 -
> drivers/char/hw_random/tpm-rng.c | 50 ----------------------------------------
> drivers/char/tpm/Kconfig | 13 +++++++++++
> drivers/char/tpm/tpm-chip.c | 41 ++++++++++++++++++++++++++++----
> drivers/char/tpm/tpm-interface.c | 37 ++++++++++++++++-------------
> drivers/char/tpm/tpm.h | 5 ++++
> 7 files changed, 76 insertions(+), 84 deletions(-)
> delete mode 100644 drivers/char/hw_random/tpm-rng.c
>
> All,
>
> It is such a good idea, I took PrasannaKumar's patch, reviewed and
> revised it to the level it could be merged.
Thank you for this.
> This is compile tested only.
>
> diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
> index 95a031e9eced07..a20fed182cbcce 100644
> --- a/drivers/char/hw_random/Kconfig
> +++ b/drivers/char/hw_random/Kconfig
> @@ -306,19 +306,6 @@ config HW_RANDOM_POWERNV
>
> If unsure, say Y.
>
> -config HW_RANDOM_TPM
> - tristate "TPM HW Random Number Generator support"
> - depends on TCG_TPM
> - default HW_RANDOM
> - ---help---
> - This driver provides kernel-side support for the Random Number
> - Generator in the Trusted Platform Module
> -
> - To compile this driver as a module, choose M here: the
> - module will be called tpm-rng.
> -
> - If unsure, say Y.
> -
> config HW_RANDOM_HISI
> tristate "Hisilicon Random Number Generator support"
> depends on HW_RANDOM && ARCH_HISI
> diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
> index 39a67defac67cb..91cb8e8213e7c1 100644
> --- a/drivers/char/hw_random/Makefile
> +++ b/drivers/char/hw_random/Makefile
> @@ -26,7 +26,6 @@ obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
> obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
> obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
> obj-$(CONFIG_HW_RANDOM_HISI) += hisi-rng.o
> -obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
> obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
> obj-$(CONFIG_HW_RANDOM_IPROC_RNG200) += iproc-rng200.o
> obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
> diff --git a/drivers/char/hw_random/tpm-rng.c b/drivers/char/hw_random/tpm-rng.c
> deleted file mode 100644
> index d6d448266f0752..00000000000000
> --- a/drivers/char/hw_random/tpm-rng.c
> +++ /dev/null
> @@ -1,50 +0,0 @@
> -/*
> - * Copyright (C) 2012 Kent Yoder IBM Corporation
> - *
> - * HWRNG interfaces to pull RNG data from a TPM
> - *
> - * This program is free software; you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License version 2 as
> - * published by the Free Software Foundation.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY; without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> - * GNU General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program; if not, write to the Free Software
> - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
> - */
> -
> -#include <linux/module.h>
> -#include <linux/hw_random.h>
> -#include <linux/tpm.h>
> -
> -#define MODULE_NAME "tpm-rng"
> -
> -static int tpm_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> -{
> - return tpm_get_random(TPM_ANY_NUM, data, max);
> -}
> -
> -static struct hwrng tpm_rng = {
> - .name = MODULE_NAME,
> - .read = tpm_rng_read,
> -};
> -
> -static int __init rng_init(void)
> -{
> - return hwrng_register(&tpm_rng);
> -}
> -module_init(rng_init);
> -
> -static void __exit rng_exit(void)
> -{
> - hwrng_unregister(&tpm_rng);
> -}
> -module_exit(rng_exit);
> -
> -MODULE_LICENSE("GPL v2");
> -MODULE_AUTHOR("Kent Yoder <key@linux.vnet.ibm.com>");
> -MODULE_DESCRIPTION("RNG driver for TPM devices");
> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
> index a30352202f1fdc..a95725fa77789e 100644
> --- a/drivers/char/tpm/Kconfig
> +++ b/drivers/char/tpm/Kconfig
> @@ -26,6 +26,19 @@ menuconfig TCG_TPM
>
> if TCG_TPM
>
> +config HW_RANDOM_TPM
> + tristate "TPM HW Random Number Generator support"
> + depends on TCG_TPM && HW_RANDOM
> + default HW_RANDOM
> + ---help---
> + This driver provides kernel-side support for the Random Number
> + Generator in the Trusted Platform Module
> +
> + To compile this driver as a module, choose M here: the
> + module will be called tpm-rng.
> +
> + If unsure, say Y.
> +
> config TCG_TIS_CORE
> tristate
> ---help---
> diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
> index 0eca20c5a80cf2..f3571406fb2995 100644
> --- a/drivers/char/tpm/tpm-chip.c
> +++ b/drivers/char/tpm/tpm-chip.c
> @@ -26,6 +26,7 @@
> #include <linux/spinlock.h>
> #include <linux/freezer.h>
> #include <linux/major.h>
> +#include <linux/hw_random.h>
> #include "tpm.h"
> #include "tpm_eventlog.h"
>
> @@ -387,6 +388,26 @@ static int tpm_add_legacy_sysfs(struct tpm_chip *chip)
>
> return 0;
> }
> +
> +static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
> +{
> + struct tpm_chip *chip = container_of(rng, struct tpm_chip, tpm_hwrng);
> +
> + return tpm_get_random_dev(chip, data, max);
> +}
> +
> +static int tpm_add_hwrng(struct tpm_chip *chip)
> +{
> + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> + return 0;
Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if
condition can be avoided.
> +
> + snprintf(chip->tpm_hwrng_name, sizeof(chip->tpm_hwrng_name),
> + "tpm-rng-%d", chip->dev_num);
> + chip->tpm_hwrng.name = chip->tpm_hwrng_name;
> + chip->tpm_hwrng.read = tpm_hwrng_read;
> + return hwrng_register(&chip->tpm_hwrng);
> +}
> +
> /*
> * tpm_chip_register() - create a character device for the TPM chip
> * @chip: TPM chip to use.
> @@ -419,11 +440,13 @@ int tpm_chip_register(struct tpm_chip *chip)
>
> tpm_add_ppi(chip);
>
> + rc = tpm_add_hwrng(chip);
> + if (rc)
> + goto out_ppi;
> +
> rc = tpm_add_char_device(chip);
> - if (rc) {
> - tpm_bios_log_teardown(chip);
> - return rc;
> - }
> + if (rc)
> + goto out_hwrng;
>
> rc = tpm_add_legacy_sysfs(chip);
> if (rc) {
> @@ -432,6 +455,14 @@ int tpm_chip_register(struct tpm_chip *chip)
> }
>
> return 0;
> +
> +out_hwrng:
> + if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> + hwrng_unregister(&chip->tpm_hwrng);
> +out_ppi:
> + tpm_bios_log_teardown(chip);
> +
> + return rc;
> }
> EXPORT_SYMBOL_GPL(tpm_chip_register);
>
> @@ -451,6 +482,8 @@ EXPORT_SYMBOL_GPL(tpm_chip_register);
> void tpm_chip_unregister(struct tpm_chip *chip)
> {
> tpm_del_legacy_sysfs(chip);
> + if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> + hwrng_unregister(&chip->tpm_hwrng);
> tpm_bios_log_teardown(chip);
> if (chip->flags & TPM_CHIP_FLAG_TPM2)
> cdev_device_del(&chip->cdevs, &chip->devs);
> diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
> index 1d6729be4cd637..310de288549818 100644
> --- a/drivers/char/tpm/tpm-interface.c
> +++ b/drivers/char/tpm/tpm-interface.c
> @@ -1194,9 +1194,8 @@ static const struct tpm_input_header tpm_getrandom_header = {
> *
> * Returns < 0 on error and the number of bytes read on success
> */
> -int tpm_get_random(u32 chip_num, u8 *out, size_t max)
> +int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max)
> {
> - struct tpm_chip *chip;
> struct tpm_cmd_t tpm_cmd;
> u32 recd, num_bytes = min_t(u32, max, TPM_MAX_RNG_DATA), rlength;
> int err, total = 0, retries = 5;
> @@ -1205,15 +1204,8 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
> if (!out || !num_bytes || max > TPM_MAX_RNG_DATA)
> return -EINVAL;
>
> - chip = tpm_chip_find_get(chip_num);
> - if (chip == NULL)
> - return -ENODEV;
> -
> - if (chip->flags & TPM_CHIP_FLAG_TPM2) {
> - err = tpm2_get_random(chip, out, max);
> - tpm_put_ops(chip);
> - return err;
> - }
> + if (chip->flags & TPM_CHIP_FLAG_TPM2)
> + return tpm2_get_random(chip, out, max);
>
> do {
> tpm_cmd.header.in = tpm_getrandom_header;
> @@ -1231,10 +1223,9 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
>
> rlength = be32_to_cpu(tpm_cmd.header.out.length);
> if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
> - recd) {
> - total = -EFAULT;
> - break;
> - }
> + recd)
> + return -EFAULT;
> +
> memcpy(dest, tpm_cmd.params.getrandom_out.rng_data, recd);
>
> dest += recd;
> @@ -1242,9 +1233,23 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)
> num_bytes -= recd;
> } while (retries-- && total < max);
>
> - tpm_put_ops(chip);
> return total ? total : -EIO;
> }
> +
> +int tpm_get_random(u32 chip_num, u8 *out, size_t max)
> +{
> + int rc;
> + struct tpm_chip *chip;
> +
> + chip = tpm_chip_find_get(chip_num);
> + if (chip == NULL)
> + return -ENODEV;
> +
> + rc = tpm_get_random_dev(chip, out, max);
> +
> + tpm_put_ops(chip);
> + return rc;
> +}
> EXPORT_SYMBOL_GPL(tpm_get_random);
>
> /**
> diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h
> index 2d5466a72e40f8..0e41e044d81be2 100644
> --- a/drivers/char/tpm/tpm.h
> +++ b/drivers/char/tpm/tpm.h
> @@ -26,6 +26,7 @@
> #include <linux/module.h>
> #include <linux/delay.h>
> #include <linux/fs.h>
> +#include <linux/hw_random.h>
> #include <linux/mutex.h>
> #include <linux/sched.h>
> #include <linux/platform_device.h>
> @@ -210,6 +211,9 @@ struct tpm_chip {
> int dev_num; /* /dev/tpm# */
> unsigned long is_open; /* only one allowed */
>
> + char tpm_hwrng_name[64];
> + struct hwrng tpm_hwrng;
> +
Can this also be put inside the #ifdef?
> struct mutex tpm_mutex; /* tpm is processing */
>
> unsigned long timeout_a; /* jiffies */
> @@ -548,6 +552,7 @@ void tpm_chip_unregister(struct tpm_chip *chip);
> void tpm_sysfs_add_device(struct tpm_chip *chip);
>
> int tpm_pcr_read_dev(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
> +int tpm_get_random_dev(struct tpm_chip *chip, u8 *out, size_t max);
>
> #ifdef CONFIG_ACPI
> extern void tpm_add_ppi(struct tpm_chip *chip);
> --
> 2.7.4
>
Regards,
PrasannaKumar
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-25 14:45 ` PrasannaKumar Muralidharan
@ 2017-10-25 15:18 ` Jason Gunthorpe
2017-10-25 15:25 ` PrasannaKumar Muralidharan
2017-10-25 18:58 ` Jarkko Sakkinen
1 sibling, 1 reply; 10+ messages in thread
From: Jason Gunthorpe @ 2017-10-25 15:18 UTC (permalink / raw)
To: PrasannaKumar Muralidharan
Cc: Jarkko Sakkinen, Stefan Berger, linux-integrity, David Howells,
Herbert Xu, Dmitry Kasatkin, open list, linux-security-module,
open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan
wrote:
> > +static int tpm_add_hwrng(struct tpm_chip *chip)
> > +{
> > + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> > + return 0;
>
> Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if
> condition can be avoided.
Generally speaking IS_ENABLED is prefered over #ifdef as it reduces the
set of compilation combinations.
Jason
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-25 15:18 ` Jason Gunthorpe
@ 2017-10-25 15:25 ` PrasannaKumar Muralidharan
0 siblings, 0 replies; 10+ messages in thread
From: PrasannaKumar Muralidharan @ 2017-10-25 15:25 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: Jarkko Sakkinen, Stefan Berger, linux-integrity, David Howells,
Herbert Xu, Dmitry Kasatkin, open list, linux-security-module,
open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
Hi Jason,
On 25 October 2017 at 20:48, Jason Gunthorpe
<jgunthorpe@obsidianresearch.com> wrote:
> On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan
> wrote:
>
>> > +static int tpm_add_hwrng(struct tpm_chip *chip)
>> > +{
>> > + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
>> > + return 0;
>>
>> Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if
>> condition can be avoided.
>
> Generally speaking IS_ENABLED is prefered over #ifdef as it reduces the
> set of compilation combinations.
Oh okay. No issues then.
Regards,
PrasannaKumar
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-25 14:45 ` PrasannaKumar Muralidharan
2017-10-25 15:18 ` Jason Gunthorpe
@ 2017-10-25 18:58 ` Jarkko Sakkinen
2017-10-25 19:37 ` Jason Gunthorpe
1 sibling, 1 reply; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-10-25 18:58 UTC (permalink / raw)
To: PrasannaKumar Muralidharan
Cc: Jason Gunthorpe, Stefan Berger, linux-integrity, David Howells,
Herbert Xu, Dmitry Kasatkin, open list, linux-security-module,
open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan wrote:
> > + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> > + return 0;
>
> Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if
> condition can be avoided.
Nope. There is no reason to avoid the if-condition. Compiler will take
care of it. IS_ENABLED() macro is available just for the purpose Jason
is using it.
> > + char tpm_hwrng_name[64];
> > + struct hwrng tpm_hwrng;
> > +
>
> Can this also be put inside the #ifdef?
Yes. It should be inside #ifdef.
/Jarkko
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-25 18:58 ` Jarkko Sakkinen
@ 2017-10-25 19:37 ` Jason Gunthorpe
2017-10-25 20:00 ` Jarkko Sakkinen
0 siblings, 1 reply; 10+ messages in thread
From: Jason Gunthorpe @ 2017-10-25 19:37 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: PrasannaKumar Muralidharan, Stefan Berger, linux-integrity,
David Howells, Herbert Xu, Dmitry Kasatkin, open list,
linux-security-module, open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Wed, Oct 25, 2017 at 08:58:17PM +0200, Jarkko Sakkinen wrote:
> On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan wrote:
> > > + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> > > + return 0;
> >
> > Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if
> > condition can be avoided.
>
> Nope. There is no reason to avoid the if-condition. Compiler will take
> care of it. IS_ENABLED() macro is available just for the purpose Jason
> is using it.
>
> > > + char tpm_hwrng_name[64];
> > > + struct hwrng tpm_hwrng;
> > > +
> >
> > Can this also be put inside the #ifdef?
>
> Yes. It should be inside #ifdef.
Then we need #idefs in the .c code, IS_ENABLED is not enough :\ I
don't think the few bytes matters enough to bother.
Jason
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-25 19:37 ` Jason Gunthorpe
@ 2017-10-25 20:00 ` Jarkko Sakkinen
2017-10-25 20:04 ` Jason Gunthorpe
0 siblings, 1 reply; 10+ messages in thread
From: Jarkko Sakkinen @ 2017-10-25 20:00 UTC (permalink / raw)
To: Jason Gunthorpe
Cc: PrasannaKumar Muralidharan, Stefan Berger, linux-integrity,
David Howells, Herbert Xu, Dmitry Kasatkin, open list,
linux-security-module, open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Wed, Oct 25, 2017 at 01:37:07PM -0600, Jason Gunthorpe wrote:
> On Wed, Oct 25, 2017 at 08:58:17PM +0200, Jarkko Sakkinen wrote:
> > On Wed, Oct 25, 2017 at 08:15:09PM +0530, PrasannaKumar Muralidharan wrote:
> > > > + if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
> > > > + return 0;
> > >
> > > Can #ifndef CONFIG_HW_RANDOM_TPM be used instead? That way an if
> > > condition can be avoided.
> >
> > Nope. There is no reason to avoid the if-condition. Compiler will take
> > care of it. IS_ENABLED() macro is available just for the purpose Jason
> > is using it.
> >
> > > > + char tpm_hwrng_name[64];
> > > > + struct hwrng tpm_hwrng;
> > > > +
> > >
> > > Can this also be put inside the #ifdef?
> >
> > Yes. It should be inside #ifdef.
>
> Then we need #idefs in the .c code, IS_ENABLED is not enough :\ I
> don't think the few bytes matters enough to bother.
>
> Jason
I'll buy that!
A minor tidbit: could the tpm_ prefix removed from those fields?
/Jarkko
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH] tpm: Move Linux RNG connection to hwrng
2017-10-25 20:00 ` Jarkko Sakkinen
@ 2017-10-25 20:04 ` Jason Gunthorpe
0 siblings, 0 replies; 10+ messages in thread
From: Jason Gunthorpe @ 2017-10-25 20:04 UTC (permalink / raw)
To: Jarkko Sakkinen
Cc: PrasannaKumar Muralidharan, Stefan Berger, linux-integrity,
David Howells, Herbert Xu, Dmitry Kasatkin, open list,
linux-security-module, open list:KEYS-TRUSTED,
open list:HARDWARE RANDOM NUMBER GENERATOR CORE, James Morris,
Matt Mackall, David Safford, Mimi Zohar, Serge E. Hallyn
On Wed, Oct 25, 2017 at 10:00:30PM +0200, Jarkko Sakkinen wrote:
> A minor tidbit: could the tpm_ prefix removed from those fields?
Yes, in v2
I will send v2 when you land your rework patch as this will depend on it.
Jason
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2017-10-25 20:04 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-24 18:42 [PATCH] tpm: Move Linux RNG connection to hwrng Jason Gunthorpe
2017-10-24 21:34 ` Jason Gunthorpe
2017-10-24 22:29 ` Jarkko Sakkinen
2017-10-25 14:45 ` PrasannaKumar Muralidharan
2017-10-25 15:18 ` Jason Gunthorpe
2017-10-25 15:25 ` PrasannaKumar Muralidharan
2017-10-25 18:58 ` Jarkko Sakkinen
2017-10-25 19:37 ` Jason Gunthorpe
2017-10-25 20:00 ` Jarkko Sakkinen
2017-10-25 20:04 ` Jason Gunthorpe
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).