DO NOT USE IN PRODUCTION SYSTEMS
+config LIBUKRANDOM_DTB_SEED
+ bool "Obtain seed from the device-tree"
+ depends on LIBUKOFW
+ select LIBUKBOOT
+ default y
+ help
+ Selecting this option will cause libukrandom to obtain its
+ seed from the fdt, if the `/chosen/rng-seed` node is present
+ and populated with a 256-bit key formatted as an array of
+ 32-bit words. In that case any hardware RNG driver enabled
+ will be ignored.
+
config LIBUKRANDOM_GETRANDOM
bool "getrandom() system call"
depends on LIBSYSCALL_SHIM
LIBUKRANDOM_CINCLUDES-y += -I$(LIBUKRANDOM_BASE)
+# TODO Remove as soon as plat dependencies go away
+LIBUKRANDOM_CINCLUDES-y += -I$(UK_PLAT_COMMON_BASE)/include
+
UK_PROVIDED_SYSCALLS-$(CONFIG_LIBUKRANDOM_GETRANDOM) += getrandom-3
#include <uk/random/driver.h>
#include <uk/plat/lcpu.h>
+#if CONFIG_LIBUKRANDOM_DTB_SEED
+#include <uk/ofw/fdt.h>
+#endif /* CONFIG_LIBUKRANDOM_DTB_SEED */
+
/* This implements the original version of ChaCha20 as presented
* in http://cr.yp.to/chacha/chacha-20080128.pdf. For the differences
* with the IRTF version see RFC-8439 Sect. 2.3.
r->k = 16;
}
+#if CONFIG_LIBUKRANDOM_DTB_SEED
+int uk_swrand_fdt_init(void *fdt, struct uk_random_driver **drv)
+{
+ __u32 *seedv;
+ __sz seedc;
+ int rc;
+
+ rc = fdt_chosen_rng_seed(fdt, &seedv, &seedc);
+ if (unlikely(rc))
+ return -ENOTSUP;
+
+ if (unlikely(seedc < CHACHA_SEED_LENGTH)) {
+ uk_pr_err("/chosen/rng-seed does not provide enough randomness\n");
+ return -ENOTSUP;
+ }
+
+ uk_pr_warn("The CSPRNG is initialized from the dtb\n");
+
+ /* prevent drivers from registering */
+ *drv = (void *)UK_SWRAND_DRIVER_NONE;
+
+ chacha_init(&uk_swrand_def, seedv);
+
+ return rc;
+}
+#endif /* CONFIG_LIBUKRANDOM_DTB_SEED */
+
int uk_swrand_init(struct uk_random_driver **drv)
{
unsigned int seedc = CHACHA_SEED_LENGTH;
#include <uk/random.h>
#include <uk/random/driver.h>
+#if CONFIG_LIBUKRANDOM_DTB_SEED
+#include <uk/boot/earlytab.h>
+#include <uk/plat/common/bootinfo.h>
+#include <uk/prio.h>
+#endif /* CONFIG_LIBUKRANDOM_DTB_SEED */
+
#include "swrand.h"
struct uk_random_driver *driver;
return uk_swrand_init(&driver);
}
+
+#if CONFIG_LIBUKRANDOM_DTB_SEED
+int uk_random_early_init(struct ukplat_bootinfo __maybe_unused *bi)
+{
+ return uk_swrand_fdt_init((void *)bi->dtb, &driver);
+}
+
+UK_BOOT_EARLYTAB_ENTRY(uk_random_early_init, UK_PRIO_AFTER(2));
+#endif /* CONFIG_LIBUKRANDOM_DTB_SEED */
#include <uk/random/driver.h>
-/* Initialize the software CSPRNG */
+/* Invalid pointer to differentiate between not initialized
+ * libukrandom and initialized without an underlying driver.
+ */
+#define UK_SWRAND_DRIVER_NONE 0xb0b0cafe
+
+/* Initialize the CSPRNG. The CSPRNG is seeded with randomness
+ * provided by the dtb's `/chosen/rng-seed` node.
+ */
+int uk_swrand_fdt_init(void *fdt, struct uk_random_driver **drv);
+
+/* Initialize the CSPRNG. The CSPRNG is seeded with randomness
+ * provided by an RNG device.
+ */
int uk_swrand_init(struct uk_random_driver **drv);
/* Get a 32-bit random number */