diff --git a/.gitignore b/.gitignore index 9e3363b..f222fb9 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ custom/libcustcalc* endian endian_calc.h fposval.h +have_arc4random.h have_ban_pragma.h have_const.h have_environ.h diff --git a/Makefile b/Makefile index 2a123de..06de238 100644 --- a/Makefile +++ b/Makefile @@ -534,6 +534,16 @@ have_uid_t.h: ${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR} ${V} echo '=-=-=-=-= private Makefile $@ rule end =-=-=-=-=' +have_environ.h: + ${V} echo '=-=-=-=-= private Makefile $@ rule start =-=-=-=-=' + ${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR} + ${V} echo '=-=-=-=-= private Makefile $@ rule end =-=-=-=-=' + +have_arc4random.h: + ${V} echo '=-=-=-=-= private Makefile $@ rule start =-=-=-=-=' + ${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR} + ${V} echo '=-=-=-=-= private Makefile $@ rule end =-=-=-=-=' + have_newstr.h: ${V} echo '=-=-=-=-= private Makefile $@ rule start =-=-=-=-=' ${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR} diff --git a/Makefile.ship b/Makefile.ship index 53c21fd..f1e88e9 100644 --- a/Makefile.ship +++ b/Makefile.ship @@ -379,19 +379,32 @@ HAVE_CONST= HAVE_UID_T= #HAVE_UID_T= -DHAVE_NO_UID_T -# Determine if we have user environment external: +# Determine if we have a non-NULL user environment external: # -# extern char **environ; +# extern char **environ; /* user environment */ # # If HAVE_ENVIRON is empty, this Makefile will run the have_environ program -# to determine if environ exsuts. If HAVE_ENVIRON is set to -DHAVE_NO_ENVIRON, -# then calc will assume there is no external environ symbol. +# to determine if environ exists and is non-NULL. If HAVE_ENVIRON is set +# to -DHAVE_NO_ENVIRON, then calc will assume there is no external environ +# symbol. # # If in doubt, leave HAVE_ENVIRON empty and this Makefile will figure it out. # HAVE_ENVIRON= #HAVE_ENVIRON= -DHAVE_NO_ENVIRON +# Determine if we have the arc4random_buf() function +# +# If HAVE_ARC4RANDOM is empty, this Makefile will run the have_arc4random +# program to determine if arc4random_buf() function exists. If +# HAVE_ARC4RANDOM is set to -DHAVE_NO_ARC4RANDOM, then calc will assume +# there is no arc4random_buf() function. +# +# If in doubt, leave HAVE_ARC4RANDOM empty and this Makefile will figure it out. +# +HAVE_ARC4RANDOM= +#HAVE_ARC4RANDOM= -DHAVE_NO_ARC4RANDOM + # Determine if we have memcpy(), memset() and strchr() # # If HAVE_NEWSTR is empty, this Makefile will run the have_newstr program @@ -1927,7 +1940,7 @@ BUILD_H_SRC= align32.h args.h calcerr.h conf.h endian_calc.h \ have_posscl.h have_rusage.h have_stdlib.h have_strdup.h \ have_string.h have_strlcat.h have_strlcpy.h have_times.h \ have_uid_t.h have_unistd.h have_unused.h have_urandom.h \ - have_ustat.h longbits.h terminal.h have_environ.h + have_ustat.h longbits.h terminal.h have_environ.h have_arc4random.h # we build these .c files during the make # @@ -1942,7 +1955,8 @@ UTIL_C_SRC= align32.c endian.c longbits.c have_newstr.c have_uid_t.c \ have_fpos_pos.c have_offscl.c have_posscl.c have_memmv.c \ have_ustat.c have_getsid.c have_getpgid.c have_environ.c \ have_gettime.c have_getprid.c have_rusage.c have_strdup.c \ - have_unused.c have_ban_pragma.c have_strlcpy.c have_strlcat.c + have_unused.c have_ban_pragma.c have_strlcpy.c have_strlcat.c \ + have_arc4random.c # these awk and sed tools are used in the process of building BUILD_H_SRC # and BUILD_C_SRC @@ -1959,7 +1973,8 @@ UTIL_OBJS= endian.o longbits.o have_newstr.o have_uid_t.o \ try_strarg.o have_stdvs.o have_varvs.o have_posscl.o have_memmv.o \ have_ustat.o have_getsid.o have_getpgid.o have_environ.o \ have_gettime.o have_getprid.o ver_calc.o have_rusage.o have_strdup.o \ - have_unused.o have_ban_pragma.o have_strlcpy.o have_strlcat.o + have_unused.o have_ban_pragma.o have_strlcpy.o have_strlcat.o \ + have_arc4random.o # these temp files may be created (and removed) during the build of BUILD_C_SRC # @@ -1977,7 +1992,7 @@ UTIL_PROGS= align32${EXT} fposval${EXT} have_uid_t${EXT} have_const${EXT} \ ver_calc${EXT} have_strdup${EXT} have_environ{EXT} \ have_unused${EXT} have_fpos${EXT} have_fpos_pos${EXT} \ have_offscl${EXT} have_rusage${EXT} have_ban_pragma${EXT} \ - have_strlcpy${EXT} have_strlcat${EXT} + have_strlcpy${EXT} have_strlcat${EXT} have_arc4random${EXT} # these utility files and scripts may be created in the process of building # the BUILD_H_SRC file set @@ -3302,6 +3317,48 @@ have_environ.h: have_environ.c \ ${TRUE}; \ fi +have_arc4random.h: have_arc4random.c have_stdlib.h \ + banned.h have_ban_pragma.h ${MAKE_FILE} ${LOC_MKF} + ${Q} ${RM} -f have_arc4random arc4random_tmp $@ + ${H} echo 'forming $@' + ${Q} echo '/*' > $@ + ${Q} echo ' * DO NOT EDIT -- generated by the Makefile' >> $@ + ${Q} echo ' */' >> $@ + ${Q} echo '' >> $@ + ${Q} echo '' >> $@ + ${Q} echo '#if !defined(HAVE_ARC4RANDOM)' >> $@ + ${Q} echo '#define HAVE_ARC4RANDOM' >> $@ + ${Q} echo '' >> $@ + ${Q} echo '' >> $@ + ${Q} echo '/* do we have or want arc4random? */' >> $@ + ${Q} ${RM} -f have_arc4random.o have_arc4random${EXT} + -${Q} ${LCC} ${ICFLAGS} ${HAVE_arc4random} have_arc4random.c -c ${S} \ + || ${TRUE} + -${Q} ${LCC} ${ILDFLAGS} have_arc4random.o \ + -o have_arc4random${EXT} ${S} \ + || ${TRUE} + -${Q} ./have_arc4random${EXT} > arc4random_tmp ${E} \ + || ${TRUE} + -${Q} if [ -s arc4random_tmp ]; then \ + ${CAT} arc4random_tmp >> $@; \ + else \ + echo '#undef HAVE_arc4random /* no */' >> $@; \ + fi + ${Q} echo '' >> $@ + ${Q} echo '' >> $@ + ${Q} echo '#endif /* !HAVE_ARC4RANDOM */' >> $@ + ${Q} ${RM} -f have_arc4random${EXT} have_arc4random.o arc4random_tmp + ${H} echo '$@ formed' + -@if [ -z "${Q}" ]; then \ + echo ''; \ + echo '=-=-= start of $@ =-=-='; \ + ${CAT} $@; \ + echo '=-=-= end of $@ =-=-='; \ + echo ''; \ + else \ + ${TRUE}; \ + fi + have_newstr.h: have_newstr.c banned.h have_ban_pragma.h ${MAKE_FILE} ${LOC_MKF} ${Q} ${RM} -f newstr_tmp $@ ${H} echo 'forming $@' @@ -4478,6 +4535,7 @@ env: @echo 'FPOS_POS_BITS=${FPOS_POS_BITS}'; echo '' @echo 'GREP=${GREP}'; echo '' @echo 'H=${H}'; echo '' + @echo 'HAVE_ARC4RANDOM=${HAVE_ARC4RANDOM}'; echo '' @echo 'HAVE_CONST=${HAVE_CONST}'; echo '' @echo 'HAVE_ENVIRON=${HAVE_ENVIRON}'; echo '' @echo 'HAVE_FPOS=${HAVE_FPOS}'; echo '' @@ -5866,6 +5924,10 @@ hash.o: value.h hash.o: zmath.h hash.o: zrand.h hash.o: zrandom.h +have_arc4random.o: banned.h +have_arc4random.o: have_arc4random.c +have_arc4random.o: have_ban_pragma.h +have_arc4random.o: have_stdlib.h have_ban_pragma.o: banned.h have_ban_pragma.o: have_ban_pragma.c have_ban_pragma.o: have_ban_pragma.h @@ -6512,6 +6574,7 @@ seed.o: banned.h seed.o: byteswap.h seed.o: decl.h seed.o: endian_calc.h +seed.o: have_arc4random.h seed.o: have_ban_pragma.h seed.o: have_const.h seed.o: have_environ.h diff --git a/have_arc4random.c b/have_arc4random.c new file mode 100644 index 0000000..082698f --- /dev/null +++ b/have_arc4random.c @@ -0,0 +1,72 @@ +/* + * have_arc4random - Determine if we have the arc4random_buf() RNG + * + * Copyright (C) 2021 Landon Curt Noll + * + * Calc is open software; you can redistribute it and/or modify it under + * the terms of the version 2.1 of the GNU Lesser General Public License + * as published by the Free Software Foundation. + * + * Calc 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 Lesser General + * Public License for more details. + * + * A copy of version 2.1 of the GNU Lesser General Public License is + * distributed with calc under the filename COPYING-LGPL. You should have + * received a copy with calc; if not, write to Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Under source code control: 2021/12/06 23:58:51 + * File existed as early as: 2021 + * + * chongo /\oo/\ http://www.isthe.com/chongo/ + * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/ + */ + +/* + * usage: + * have_arc4random + * + * Not all enviroments have the arc4random_buf() function, + * so this may not compile on your system. + * + * This prog outputs: + * + * HAVE_ARC4RANDOM + * defined ==> have arc4random_buf() call + * undefined ==> do not have arc4random_buf() call + */ + +#include "have_stdlib.h" +#if defined(HAVE_STDLIB_H) +#include +#endif +#include + + +#include "banned.h" /* include after system header <> includes */ + + +#define BUFLEN (32) /* length of the buffer to fill */ + + +int +main(void) +{ +#if defined(HAVE_NO_ARC4RANDOM) + + printf("#undef HAVE_ARC4RANDOM /* no */\n"); + +#else /* HAVE_NO_ARC4RANDOM */ + + static char buf[BUFLEN]; /* buffer for arc4random_buf() to fill */ + + arc4random_buf(buf, BUFLEN); + printf("#define HAVE_ARC4RANDOM /* yes */\n"); + +#endif /* HAVE_NO_ARC4RANDOM */ + + /* exit(0); */ + return 0; +} diff --git a/have_environ.c b/have_environ.c index 3efbe31..56a16c2 100644 --- a/have_environ.c +++ b/have_environ.c @@ -1,5 +1,5 @@ /* - * have_environ - Determine if we have the environ user environment value + * have_environ - Determine if we have the environ user environment synbol * * Copyright (C) 2021 Landon Curt Noll * @@ -37,8 +37,8 @@ * This prog outputs: * * HAVE_ENVIRON - * defined ==> environ is an extern symbol - * undefined ==> environ is NOT an extern symbol + * defined ==> environ is an non-NULL extern symbol + * undefined ==> environ is NOT an extern symbol or is NULL */ #include @@ -47,9 +47,6 @@ #include "banned.h" /* include after system header <> includes */ -extern char **environ; /* user environment */ - - int main(void) { @@ -57,13 +54,17 @@ main(void) printf("#undef HAVE_ENVIRON /* no */\n"); -#else /* HAVE_NO_CONST */ +#else /* HAVE_NO_ENVIRON */ - const char * const str = "const"; + extern char **environ; - printf("#define HAVE_ENVIRON /* yes */\n"); + if (environ == NULL) { + printf("#define HAVE_ENVIRON /* no */\n"); + } else { + printf("#define HAVE_ENVIRON /* yes */\n"); + } -#endif /* HAVE_NO_CONST */ +#endif /* HAVE_NO_ENVIRON */ /* exit(0); */ return 0; diff --git a/seed.c b/seed.c index 542a2f9..88276fc 100644 --- a/seed.c +++ b/seed.c @@ -80,7 +80,7 @@ #endif #if defined(HAVE_STDLIB_H) # include -# define RANDOM_CNT (32) /* double random() call repeat count */ +# define RANDOM_CNT (64) /* double random() call repeat count */ # define INITSTATE_SIZE (256) /* initstate pool size */ #endif #include @@ -96,6 +96,7 @@ #include "have_rusage.h" #include "have_uid_t.h" #include "have_environ.h" +#include "have_arc4random.h" #if defined(HAVE_USTAT) # include #endif @@ -127,6 +128,11 @@ static hash64 prev_hash64 = { 0, 0 }; /* previous pseudo_seed() return or 0 */ extern char **environ; /* user environment */ #endif /* HAVE_ENVIRON */ +#if defined(HAVE_ARC4RANDOM) +#define ARC4_BUFLEN (256) +static char arc4_buf[ARC4_BUFLEN]; +#endif /* HAVE_ARC4RANDOM */ + /* * call counter - number of times pseudo_seed() as been called @@ -562,10 +568,11 @@ pseudo_seed(void) pash_hash = (unsigned)(prev_hash64.w32[0] ^ prev_hash64.w32[1]); #endif /* HAVE_B64 */ - /* classic random seeded with time of day, count, prev hash */ + /* classic 31-bit random seeded with time of day, count, prev hash */ srandom((unsigned)(sdata.time) ^ (unsigned)call_count ^ past_hash); - for (j=0; j < RANDOM_CNT; ++j) { + for (j=0; j < RANDOM_CNT; j += 2) { random_before[j] = random(); + random_before[j+1] = (random() << 1); } /* initialize random state with the FNV hash of sdata */ @@ -579,9 +586,10 @@ pseudo_seed(void) INITSTATE_SIZE); #endif /* HAVE_B64 */ - /* use random again with the new random state */ - for (j=0; j < RANDOM_CNT; ++j) { - random_after[j] = random() ^ (random() << 1); + /* use 31-bit random some more with the new random state */ + for (j=0; j < RANDOM_CNT; j += 2) { + random_after[j] = random(); + random_after[j+1] = (random() << 1); } /* @@ -604,6 +612,16 @@ pseudo_seed(void) sizeof(random_after)); #endif /* HAVE_STDLIB_H */ +#if defined(HAVE_ARC4RANDOM) + /* + * hash from a cryptographic pseudo-random number generator + */ + arc4random_buf(arc4_buf, ARC4_BUFLEN); + hash_val = private_hash64_buf(hash_val, + (char *)arc4_buf, + ARC4_BUFLEN); +#endif /* HAVE_ARC4RANDOM */ + /* * load the hash data into the ZVALUE * diff --git a/win32.mkdef b/win32.mkdef index 1bcb634..4106ad7 100644 --- a/win32.mkdef +++ b/win32.mkdef @@ -33,6 +33,7 @@ HAVE_TIME_H=YES HAVE_SYS_TIME_H=NO HAVE_UNISTD_H=NO HAVE_ENVIRON=-DHAVE_NO_ENVIRON +HAVE_ARC4RANDOM=-DHAVE_NO_ARC4RANDOM BINDIR=/usr/bin/calc LIBDIR=/lib/calc