Improve seed() function

This commit is contained in:
Landon Curt Noll
2021-12-07 00:44:57 -08:00
parent f9464652fe
commit 7ffbaf922e
7 changed files with 190 additions and 24 deletions

1
.gitignore vendored
View File

@@ -33,6 +33,7 @@ custom/libcustcalc*
endian endian
endian_calc.h endian_calc.h
fposval.h fposval.h
have_arc4random.h
have_ban_pragma.h have_ban_pragma.h
have_const.h have_const.h
have_environ.h have_environ.h

View File

@@ -534,6 +534,16 @@ have_uid_t.h:
${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR} ${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR}
${V} echo '=-=-=-=-= private Makefile $@ rule end =-=-=-=-=' ${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: have_newstr.h:
${V} echo '=-=-=-=-= private Makefile $@ rule start =-=-=-=-=' ${V} echo '=-=-=-=-= private Makefile $@ rule start =-=-=-=-='
${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR} ${Q} ${MAKE} ${XARG} -f ${MAKE_FILE} $@ ${XVAR}

View File

@@ -379,19 +379,32 @@ HAVE_CONST=
HAVE_UID_T= HAVE_UID_T=
#HAVE_UID_T= -DHAVE_NO_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 # 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, # to determine if environ exists and is non-NULL. If HAVE_ENVIRON is set
# then calc will assume there is no external environ symbol. # 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. # If in doubt, leave HAVE_ENVIRON empty and this Makefile will figure it out.
# #
HAVE_ENVIRON= HAVE_ENVIRON=
#HAVE_ENVIRON= -DHAVE_NO_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() # Determine if we have memcpy(), memset() and strchr()
# #
# If HAVE_NEWSTR is empty, this Makefile will run the have_newstr program # 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_posscl.h have_rusage.h have_stdlib.h have_strdup.h \
have_string.h have_strlcat.h have_strlcpy.h have_times.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_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 # 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_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_ustat.c have_getsid.c have_getpgid.c have_environ.c \
have_gettime.c have_getprid.c have_rusage.c have_strdup.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 # these awk and sed tools are used in the process of building BUILD_H_SRC
# and BUILD_C_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 \ 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_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_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 # 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} \ ver_calc${EXT} have_strdup${EXT} have_environ{EXT} \
have_unused${EXT} have_fpos${EXT} have_fpos_pos${EXT} \ have_unused${EXT} have_fpos${EXT} have_fpos_pos${EXT} \
have_offscl${EXT} have_rusage${EXT} have_ban_pragma${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 # these utility files and scripts may be created in the process of building
# the BUILD_H_SRC file set # the BUILD_H_SRC file set
@@ -3302,6 +3317,48 @@ have_environ.h: have_environ.c \
${TRUE}; \ ${TRUE}; \
fi 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} have_newstr.h: have_newstr.c banned.h have_ban_pragma.h ${MAKE_FILE} ${LOC_MKF}
${Q} ${RM} -f newstr_tmp $@ ${Q} ${RM} -f newstr_tmp $@
${H} echo 'forming $@' ${H} echo 'forming $@'
@@ -4478,6 +4535,7 @@ env:
@echo 'FPOS_POS_BITS=${FPOS_POS_BITS}'; echo '' @echo 'FPOS_POS_BITS=${FPOS_POS_BITS}'; echo ''
@echo 'GREP=${GREP}'; echo '' @echo 'GREP=${GREP}'; echo ''
@echo 'H=${H}'; echo '' @echo 'H=${H}'; echo ''
@echo 'HAVE_ARC4RANDOM=${HAVE_ARC4RANDOM}'; echo ''
@echo 'HAVE_CONST=${HAVE_CONST}'; echo '' @echo 'HAVE_CONST=${HAVE_CONST}'; echo ''
@echo 'HAVE_ENVIRON=${HAVE_ENVIRON}'; echo '' @echo 'HAVE_ENVIRON=${HAVE_ENVIRON}'; echo ''
@echo 'HAVE_FPOS=${HAVE_FPOS}'; echo '' @echo 'HAVE_FPOS=${HAVE_FPOS}'; echo ''
@@ -5866,6 +5924,10 @@ hash.o: value.h
hash.o: zmath.h hash.o: zmath.h
hash.o: zrand.h hash.o: zrand.h
hash.o: zrandom.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: banned.h
have_ban_pragma.o: have_ban_pragma.c have_ban_pragma.o: have_ban_pragma.c
have_ban_pragma.o: have_ban_pragma.h have_ban_pragma.o: have_ban_pragma.h
@@ -6512,6 +6574,7 @@ seed.o: banned.h
seed.o: byteswap.h seed.o: byteswap.h
seed.o: decl.h seed.o: decl.h
seed.o: endian_calc.h seed.o: endian_calc.h
seed.o: have_arc4random.h
seed.o: have_ban_pragma.h seed.o: have_ban_pragma.h
seed.o: have_const.h seed.o: have_const.h
seed.o: have_environ.h seed.o: have_environ.h

72
have_arc4random.c Normal file
View File

@@ -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 <was here> /\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 <stdlib.h>
#endif
#include <stdio.h>
#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;
}

View File

@@ -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 * Copyright (C) 2021 Landon Curt Noll
* *
@@ -37,8 +37,8 @@
* This prog outputs: * This prog outputs:
* *
* HAVE_ENVIRON * HAVE_ENVIRON
* defined ==> environ is an extern symbol * defined ==> environ is an non-NULL extern symbol
* undefined ==> environ is NOT an extern symbol * undefined ==> environ is NOT an extern symbol or is NULL
*/ */
#include <stdio.h> #include <stdio.h>
@@ -47,9 +47,6 @@
#include "banned.h" /* include after system header <> includes */ #include "banned.h" /* include after system header <> includes */
extern char **environ; /* user environment */
int int
main(void) main(void)
{ {
@@ -57,13 +54,17 @@ main(void)
printf("#undef HAVE_ENVIRON /* no */\n"); 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); */ /* exit(0); */
return 0; return 0;

30
seed.c
View File

@@ -80,7 +80,7 @@
#endif #endif
#if defined(HAVE_STDLIB_H) #if defined(HAVE_STDLIB_H)
# include <stdlib.h> # include <stdlib.h>
# 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 */ # define INITSTATE_SIZE (256) /* initstate pool size */
#endif #endif
#include <setjmp.h> #include <setjmp.h>
@@ -96,6 +96,7 @@
#include "have_rusage.h" #include "have_rusage.h"
#include "have_uid_t.h" #include "have_uid_t.h"
#include "have_environ.h" #include "have_environ.h"
#include "have_arc4random.h"
#if defined(HAVE_USTAT) #if defined(HAVE_USTAT)
# include <ustat.h> # include <ustat.h>
#endif #endif
@@ -127,6 +128,11 @@ static hash64 prev_hash64 = { 0, 0 }; /* previous pseudo_seed() return or 0 */
extern char **environ; /* user environment */ extern char **environ; /* user environment */
#endif /* HAVE_ENVIRON */ #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 * 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]); pash_hash = (unsigned)(prev_hash64.w32[0] ^ prev_hash64.w32[1]);
#endif /* HAVE_B64 */ #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); 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] = random();
random_before[j+1] = (random() << 1);
} }
/* initialize random state with the FNV hash of sdata */ /* initialize random state with the FNV hash of sdata */
@@ -579,9 +586,10 @@ pseudo_seed(void)
INITSTATE_SIZE); INITSTATE_SIZE);
#endif /* HAVE_B64 */ #endif /* HAVE_B64 */
/* use random again with the new random state */ /* use 31-bit random some more with the new random state */
for (j=0; j < RANDOM_CNT; ++j) { for (j=0; j < RANDOM_CNT; j += 2) {
random_after[j] = random() ^ (random() << 1); random_after[j] = random();
random_after[j+1] = (random() << 1);
} }
/* /*
@@ -604,6 +612,16 @@ pseudo_seed(void)
sizeof(random_after)); sizeof(random_after));
#endif /* HAVE_STDLIB_H */ #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 * load the hash data into the ZVALUE
* *

View File

@@ -33,6 +33,7 @@ HAVE_TIME_H=YES
HAVE_SYS_TIME_H=NO HAVE_SYS_TIME_H=NO
HAVE_UNISTD_H=NO HAVE_UNISTD_H=NO
HAVE_ENVIRON=-DHAVE_NO_ENVIRON HAVE_ENVIRON=-DHAVE_NO_ENVIRON
HAVE_ARC4RANDOM=-DHAVE_NO_ARC4RANDOM
BINDIR=/usr/bin/calc BINDIR=/usr/bin/calc
LIBDIR=/lib/calc LIBDIR=/lib/calc