Skip to content

Commit

Permalink
tpm: Move Linux RNG connection to hwrng
Browse files Browse the repository at this point in the history
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: Jason Gunthorpe <jgg@ziepe.ca>
Tested-by: PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  • Loading branch information
jgunthorpe authored and Jarkko Sakkinen committed Jan 8, 2018
1 parent aad887f commit 6e592a0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 69 deletions.
13 changes: 0 additions & 13 deletions drivers/char/hw_random/Kconfig
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion drivers/char/hw_random/Makefile
Expand Up @@ -27,7 +27,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
Expand Down
50 changes: 0 additions & 50 deletions drivers/char/hw_random/tpm-rng.c

This file was deleted.

11 changes: 11 additions & 0 deletions drivers/char/tpm/Kconfig
Expand Up @@ -26,6 +26,17 @@ menuconfig TCG_TPM

if TCG_TPM

config HW_RANDOM_TPM
bool "TPM HW Random Number Generator support"
depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)
default y
---help---
This setting exposes the TPM's Random Number Generator as a hwrng
device. This allows the kernel to collect randomness from the TPM at
boot, and provides the TPM randomines in /dev/hwrng.

If unsure, say Y.

config TCG_TIS_CORE
tristate
---help---
Expand Down
42 changes: 37 additions & 5 deletions drivers/char/tpm/tpm-chip.c
Expand Up @@ -27,7 +27,7 @@
#include <linux/freezer.h>
#include <linux/major.h>
#include <linux/tpm_eventlog.h>

#include <linux/hw_random.h>
#include "tpm.h"

DEFINE_IDR(dev_nums_idr);
Expand Down Expand Up @@ -392,6 +392,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, hwrng);

return tpm_get_random(chip, data, max);
}

static int tpm_add_hwrng(struct tpm_chip *chip)
{
if (!IS_ENABLED(CONFIG_HW_RANDOM_TPM))
return 0;

snprintf(chip->hwrng_name, sizeof(chip->hwrng_name),
"tpm-rng-%d", chip->dev_num);
chip->hwrng.name = chip->hwrng_name;
chip->hwrng.read = tpm_hwrng_read;
return hwrng_register(&chip->hwrng);
}

/*
* tpm_chip_register() - create a character device for the TPM chip
* @chip: TPM chip to use.
Expand Down Expand Up @@ -424,11 +444,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) {
Expand All @@ -437,6 +459,14 @@ int tpm_chip_register(struct tpm_chip *chip)
}

return 0;

out_hwrng:
if (IS_ENABLED(CONFIG_HW_RANDOM_TPM))
hwrng_unregister(&chip->hwrng);
out_ppi:
tpm_bios_log_teardown(chip);

return rc;
}
EXPORT_SYMBOL_GPL(tpm_chip_register);

Expand All @@ -456,6 +486,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->hwrng);
tpm_bios_log_teardown(chip);
if (chip->flags & TPM_CHIP_FLAG_TPM2)
cdev_device_del(&chip->cdevs, &chip->devs);
Expand Down
4 changes: 4 additions & 0 deletions drivers/char/tpm/tpm.h
Expand Up @@ -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>
Expand Down Expand Up @@ -211,6 +212,9 @@ struct tpm_chip {
int dev_num; /* /dev/tpm# */
unsigned long is_open; /* only one allowed */

char hwrng_name[64];
struct hwrng hwrng;

struct mutex tpm_mutex; /* tpm is processing */

unsigned long timeout_a; /* jiffies */
Expand Down

0 comments on commit 6e592a0

Please sign in to comment.