From 6cff8357023adb4d5c9daa027522f883f3905f4c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 22 Mar 2018 14:58:29 +0000 Subject: win32: allow use of shell's PRNG for /dev/urandom Allow either ISAAC or the shell's built-in pseudo-random number generator to be used for /dev/urandom. The latter is smaller so it's the default. --- Config.in | 45 +++++++++++++++++++++++--------- shell/Kbuild.src | 1 + shell/random.c | 11 ++++++++ shell/random.h | 3 +++ win32/Kbuild | 11 ++++---- win32/sh_random.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 win32/sh_random.c diff --git a/Config.in b/Config.in index dc3a6e09a..a18d26720 100644 --- a/Config.in +++ b/Config.in @@ -122,18 +122,6 @@ config LFS programs that can benefit from large file support include dd, gzip, cp, mount, tar. -config GLOBBING - bool "Allow busybox.exe to expand wildcards" - default n - depends on PLATFORM_MINGW32 - help - In Microsoft Windows expansion of wildcards on the command line - ('globbing') is handled by the C runtime while the BusyBox shell - does its own wildcard expansion. For best results when using the - shell globbing by the C runtime should be turned off. If you want - the BusyBox binary to handle wildcard expansion using the C runtime - set this to 'Y'. - config PAM bool "Support PAM (Pluggable Authentication Modules)" default n @@ -380,6 +368,39 @@ config PLATFORM_LINUX #This is automatically selected if any applet or feature requires #Linux-specific interfaces. You do not need to select it manually. +config GLOBBING + bool "Allow busybox.exe to expand wildcards" + default n + depends on PLATFORM_MINGW32 + help + In Microsoft Windows expansion of wildcards on the command line + ('globbing') is handled by the C runtime while the BusyBox shell + does its own wildcard expansion. For best results when using the + shell globbing by the C runtime should be turned off. If you want + the BusyBox binary to handle wildcard expansion using the C runtime + set this to 'Y'. + +choice + prompt "Random number generator" + default FEATURE_PRNG_SHELL + depends on PLATFORM_MINGW32 + help + BusyBox on Microsoft Windows uses a pseudo-random number + generator to emulate the Linux /dev/urandom device. There + are two options: + - The shell's built-in PRNG. + - Bob Jenkins' ISAAC. This is intended to be a secure PRNG. It's + slightly faster than the shell's PRNG but is larger both in terms + of code and runtime memory. + +config FEATURE_PRNG_SHELL + bool "Use shell PRNG" + +config FEATURE_PRNG_ISAAC + bool "Use ISAAC PRNG" + +endchoice + comment 'Build Options' config STATIC diff --git a/shell/Kbuild.src b/shell/Kbuild.src index 6bba4989f..a287fce4e 100644 --- a/shell/Kbuild.src +++ b/shell/Kbuild.src @@ -9,3 +9,4 @@ lib-y:= INSERT lib-$(CONFIG_FEATURE_SH_MATH) += math.o +lib-$(CONFIG_FEATURE_PRNG_SHELL) += random.o diff --git a/shell/random.c b/shell/random.c index 5d3620516..614172279 100644 --- a/shell/random.c +++ b/shell/random.c @@ -19,6 +19,17 @@ # include "random.h" # define RAND_BASH_MASK 0x7fff +# if ENABLE_FEATURE_PRNG_SHELL +uint32_t FAST_FUNC +next_random(random_t *rnd) +{ + return full_random(rnd) & RAND_BASH_MASK; +} +# undef RAND_BASH_MASK +# define RAND_BASH_MASK 0xffffffff +# define next_random full_random +# endif + #else # include # include diff --git a/shell/random.h b/shell/random.h index c4eb44c13..75fe0f69f 100644 --- a/shell/random.h +++ b/shell/random.h @@ -35,6 +35,9 @@ typedef struct random_t { ((rnd)->galois_LFSR = 0) uint32_t next_random(random_t *rnd) FAST_FUNC; +#if ENABLE_FEATURE_PRNG_SHELL +uint32_t full_random(random_t *rnd) FAST_FUNC; +#endif POP_SAVED_FUNCTION_VISIBILITY diff --git a/win32/Kbuild b/win32/Kbuild index 00950e0f7..f8c8fb5af 100644 --- a/win32/Kbuild +++ b/win32/Kbuild @@ -7,18 +7,19 @@ lib-y:= lib-$(CONFIG_PLATFORM_MINGW32) += env.o lib-$(CONFIG_PLATFORM_MINGW32) += fnmatch.o lib-$(CONFIG_PLATFORM_MINGW32) += fsync.o +lib-$(CONFIG_PLATFORM_MINGW32) += inet_pton.o lib-$(CONFIG_PLATFORM_MINGW32) += ioctl.o -lib-$(CONFIG_PLATFORM_MINGW32) += isaac.o +lib-$(CONFIG_FEATURE_PRNG_ISAAC) += isaac.o lib-$(CONFIG_PLATFORM_MINGW32) += mingw.o lib-$(CONFIG_PLATFORM_MINGW32) += process.o -lib-$(CONFIG_PLATFORM_MINGW32) += regex.o +lib-$(CONFIG_PLATFORM_MINGW32) += mntent.o lib-$(CONFIG_PLATFORM_MINGW32) += net.o -lib-$(CONFIG_PLATFORM_MINGW32) += inet_pton.o lib-$(CONFIG_PLATFORM_MINGW32) += poll.o -lib-$(CONFIG_PLATFORM_MINGW32) += select.o lib-$(CONFIG_PLATFORM_MINGW32) += popen.o +lib-$(CONFIG_PLATFORM_MINGW32) += regex.o +lib-$(CONFIG_PLATFORM_MINGW32) += select.o +lib-$(CONFIG_FEATURE_PRNG_SHELL) += sh_random.o lib-$(CONFIG_PLATFORM_MINGW32) += statfs.o -lib-$(CONFIG_PLATFORM_MINGW32) += mntent.o lib-$(CONFIG_PLATFORM_MINGW32) += strptime.o lib-$(CONFIG_PLATFORM_MINGW32) += system.o lib-$(CONFIG_PLATFORM_MINGW32) += termios.o diff --git a/win32/sh_random.c b/win32/sh_random.c new file mode 100644 index 000000000..2948e4a55 --- /dev/null +++ b/win32/sh_random.c @@ -0,0 +1,76 @@ +#include "libbb.h" +#include "../shell/random.h" + +/* call 'fn' to put data in 'dt' then copy it to generator state */ +#define GET_DATA(fn, dt) \ + fn(&dt); \ + u = (uint32_t *)&dt; \ + for (j=0; j 0; buf+=4, count-=4) { + value = full_random(&rnd); + memcpy(buf, ptr, count >= 4 ? 4 : count); + } + + return save_count; +} -- cgit v1.2.3-55-g6feb