From: Alejandro Colomar Date: Fri, 30 Dec 2022 18:46:09 +0000 (+0100) Subject: Move csrand() to a new file csrand.c X-Git-Tag: 4.14.0-rc1~214 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be1f4f7972ad7ec151c31ad408083828bbdefd4b;p=thirdparty%2Fshadow.git Move csrand() to a new file csrand.c A set of APIs similar to arc4random(3) is complex enough to deserve its own file. Cc: "Jason A. Donenfeld" Cc: Cristian Rodríguez Cc: Adhemerval Zanella Cc: Björn Esser Cc: Yann Droneaud Cc: Joseph Myers Signed-off-by: Alejandro Colomar --- diff --git a/lib/prototypes.h b/lib/prototypes.h index 49e3db53f..3a04faa8b 100644 --- a/lib/prototypes.h +++ b/lib/prototypes.h @@ -356,6 +356,9 @@ extern /*@dependent@*/ /*@null@*/struct commonio_entry *__pw_get_head (void); extern /*@null@*/ /*@only@*/struct passwd *__pw_dup (const struct passwd *pwent); extern void pw_free (/*@out@*/ /*@only@*/struct passwd *pwent); +/* csrand.c */ +unsigned long csrand (void); + /* remove_tree.c */ extern int remove_tree (const char *root, bool remove_root); diff --git a/libmisc/Makefile.am b/libmisc/Makefile.am index c2277a023..b4ca708d7 100644 --- a/libmisc/Makefile.am +++ b/libmisc/Makefile.am @@ -52,6 +52,7 @@ libmisc_la_SOURCES = \ pwd2spwd.c \ pwdcheck.c \ pwd_init.c \ + csrand.c \ remove_tree.c \ rlogin.c \ root_flag.c \ diff --git a/libmisc/csrand.c b/libmisc/csrand.c new file mode 100644 index 000000000..a02378502 --- /dev/null +++ b/libmisc/csrand.c @@ -0,0 +1,61 @@ +#include + +#ident "$Id$" + +#include +#include +#include +#include +#if HAVE_SYS_RANDOM_H +#include +#endif +#include "bit.h" +#include "prototypes.h" +#include "shadowlog.h" + + +/* + * Return a uniformly-distributed CS random u_long value. + */ +unsigned long +csrand(void) +{ + FILE *fp; + unsigned long r; + +#ifdef HAVE_GETENTROPY + /* getentropy may exist but lack kernel support. */ + if (getentropy(&r, sizeof(r)) == 0) + return r; +#endif + +#ifdef HAVE_GETRANDOM + /* Likewise getrandom. */ + if (getrandom(&r, sizeof(r), 0) == sizeof(r)) + return r; +#endif + +#ifdef HAVE_ARC4RANDOM_BUF + /* arc4random_buf can never fail. */ + arc4random_buf(&r, sizeof(r)); + return r; +#endif + + /* Use /dev/urandom as a last resort. */ + fp = fopen("/dev/urandom", "r"); + if (NULL == fp) { + goto fail; + } + + if (fread(&r, sizeof(r), 1, fp) != 1) { + fclose(fp); + goto fail; + } + + fclose(fp); + return r; + +fail: + fprintf(log_get_logfd(), _("Unable to obtain random bytes.\n")); + exit(1); +} diff --git a/libmisc/salt.c b/libmisc/salt.c index 90ff702fb..c3f3d23a6 100644 --- a/libmisc/salt.c +++ b/libmisc/salt.c @@ -20,9 +20,6 @@ #include #include #include -#if HAVE_SYS_RANDOM_H -#include -#endif #include "prototypes.h" #include "defines.h" #include "getdef.h" @@ -89,7 +86,6 @@ #define GENSALT_SETTING_SIZE 100 /* local function prototypes */ -static long csrand (void); #if !USE_XCRYPT_GENSALT static /*@observer@*/const char *gensalt (size_t salt_size); #endif /* !USE_XCRYPT_GENSALT */ @@ -109,53 +105,6 @@ static /*@observer@*/unsigned long YESCRYPT_get_salt_cost (/*@null@*/const int * static /*@observer@*/void YESCRYPT_salt_cost_to_buf (char *buf, unsigned long cost); #endif /* USE_YESCRYPT */ -/* Read sizeof (long) random bytes from /dev/urandom. */ -static long csrand (void) -{ - long randval = 0; - -#ifdef HAVE_GETENTROPY - /* getentropy may exist but lack kernel support. */ - if (getentropy (&randval, sizeof (randval)) == 0) { - goto end; - } -#endif - -#ifdef HAVE_GETRANDOM - /* Likewise getrandom. */ - if ((size_t) getrandom (&randval, sizeof (randval), 0) == sizeof (randval)) { - goto end; - } -#endif - -#ifdef HAVE_ARC4RANDOM_BUF - /* arc4random_buf, if it exists, can never fail. */ - arc4random_buf (&randval, sizeof (randval)); - goto end; -#endif - - /* Use /dev/urandom as a last resort. */ - FILE *f = fopen ("/dev/urandom", "r"); - if (NULL == f) { - goto fail; - } - - if (fread (&randval, sizeof (randval), 1, f) != 1) { - fclose(f); - goto fail; - } - - fclose(f); - goto end; - -fail: - fprintf (log_get_logfd(), - _("Unable to obtain random bytes.\n")); - exit (1); - -end: - return randval; -} #if defined(USE_SHA_CRYPT) || defined(USE_BCRYPT) /*