diff options
Diffstat (limited to 'random128')
| -rw-r--r-- | random128/Makefile.am | 9 | ||||
| -rw-r--r-- | random128/configure.in | 101 | ||||
| -rw-r--r-- | random128/random128.c | 125 | ||||
| -rw-r--r-- | random128/random128.h | 41 | ||||
| -rw-r--r-- | random128/random128alpha.c | 29 | ||||
| -rw-r--r-- | random128/random128binary.c | 38 | 
6 files changed, 343 insertions, 0 deletions
| diff --git a/random128/Makefile.am b/random128/Makefile.am new file mode 100644 index 0000000..29cfa48 --- /dev/null +++ b/random128/Makefile.am @@ -0,0 +1,9 @@ +# +# Copyright 1998 - 2005 Double Precision, Inc.  See COPYING for +# distribution information. + + +noinst_LTLIBRARIES=librandom128.la + +librandom128_la_SOURCES=random128.c random128.h random128alpha.c \ +	random128binary.c diff --git a/random128/configure.in b/random128/configure.in new file mode 100644 index 0000000..0733a27 --- /dev/null +++ b/random128/configure.in @@ -0,0 +1,101 @@ +dnl Process this file with autoconf to produce a configure script. +dnl +dnl Copyright 1998 - 2002 Double Precision, Inc.  See COPYING for +dnl distribution information. + +AC_INIT(random128, 0.10, [courier-users@lists.sourceforge.net]) + +>confdefs.h  # Kill PACKAGE_ macros + +AC_CONFIG_SRCDIR(random128.c) +AC_CONFIG_AUX_DIR(../..) +AM_INIT_AUTOMAKE([foreign no-define]) +LPATH="$PATH:/usr/local/bin" + + +dnl Checks for programs. +AM_CONFIG_HEADER(config.h) +AC_USE_SYSTEM_EXTENSIONS +AC_PROG_INSTALL +AC_PROG_LN_S +AC_PROG_CC +AC_PATH_PROGS(PS, ps, ps, $LPATH) +AC_PATH_PROGS(W, w, w, $LPATH) +AC_PROG_LIBTOOL + +if test "$PS" = "ps" +then +	AC_MSG_ERROR(Cannot find pathname to ps) +fi + +if test x$GXX = xyes +then +	CFLAGS="-Wall $CFLAGS" +fi + +CFLAGS="-I.. -I$srcdir/.. $CFLAGS" + +dnl Checks for libraries. + +dnl Checks for header files. +AC_HEADER_SYS_WAIT +AC_CHECK_HEADERS(unistd.h fcntl.h) + +AC_TYPE_PID_T +AC_SYS_LARGEFILE + +AC_ARG_WITH(random, [ --with-random=/dev/urandom - location of the system random file generator +--without-random            - there is no system random file generator ], +	random="$withval", +	random="y") + +case "$random" in +/*) +	;; +y*|1*) +	AC_CACHE_CHECK([for random source],random_cv_RANDOM, + +		if test -c /dev/urandom +		then +			random_cv_RANDOM=/dev/urandom +		else +			if test -c /dev/random +			then +				random_cv_RANDOM=/dev/random +			else +				random_cv_RANDOM="none" +			fi +		fi +	) +	random="$random_cv_RANDOM" +	;; +*) +	random="none" +	;; +esac + +if test "$random" != "none" +then +	AC_DEFINE_UNQUOTED(RANDOM, "$random", [ Entropy source ]) +fi + +AC_CACHE_CHECK([for some good options for ps],random_cv_PS_OPTIONS, + +	for opts in -Afl -Afw -Af -Al -afl -afw -af -al Afl Afw Af Al afl afw af al +	do +		ps $opts >/dev/null 2>/dev/null || continue +		break +	done +	random_cv_PS_OPTIONS="$opts" +) + +AC_DEFINE_UNQUOTED(PS_OPTIONS,"$random_cv_PS_OPTIONS", +	[ How to make ps(1) spit out lots of crap ]) +AC_DEFINE_UNQUOTED(PS,"$PS", [ The PS program ]) + +if test "$W" != "w" +then +	AC_DEFINE_UNQUOTED(W, "$w", [ The W program ]) +fi + +AC_OUTPUT(Makefile) diff --git a/random128/random128.c b/random128/random128.c new file mode 100644 index 0000000..da3b39e --- /dev/null +++ b/random128/random128.c @@ -0,0 +1,125 @@ +/* +** Copyright 1998 - 2006 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if	HAVE_CONFIG_H +#include	"config.h" +#endif + +#if	HAVE_UNISTD_H +#include	<unistd.h> +#endif +#if	HAVE_FCNTL_H +#include	<fcntl.h> +#endif +#include	<time.h> +#include	<string.h> +#include	<errno.h> +#include	<stdio.h> +#include	<sys/types.h> +#include	<sys/wait.h> + +#define	MD5_INTERNAL +#include	"md5/md5.h" + +#include	"random128.h" + + +const char *random128() +{ +static char randombuf[sizeof(MD5_DIGEST)*2+1]; + +#ifdef	RANDOM +	{ +	int	fd=open(RANDOM, O_RDONLY); +	char	buf2[sizeof(MD5_DIGEST)]; +	int	i; + +		if (fd >= 0) +		{ +			if (read(fd, buf2, sizeof(buf2)) == sizeof(buf2)) +			{ +				for (i=0; i<sizeof(buf2); i++) +					sprintf(randombuf+i*2, +						"%02X", +						(int)(unsigned char)buf2[i]); +				close(fd); +				return (randombuf); +			} +			close(fd); +		} +	} +#endif + +	/* /dev/urandom not available or broken?  Create some noise */ + +	{ +	int pipefd[2]; +	int s; +	char	buf[512]; +	struct MD5_CONTEXT context; +	MD5_DIGEST	digest; +	int	n; +	time_t	t; +	pid_t	p, p2; +	unsigned long l; + +		time(&t); +		p=getpid(); + +		if (pipe(pipefd))	return (0); +		while ((p=fork()) == -1) +		{ +			sleep (5); +		} +		if (p == 0) +		{ +			dup2(pipefd[1], 1); +			dup2(pipefd[1], 2); +			close(pipefd[0]); +			close(pipefd[1]); + +#ifdef	W +			while ((p=fork()) == -1) +			{ +				sleep (5); +			} +			if (p == 0) +			{ +				execl(W, W, (char *)0); +				perror(W); +				_exit(0); +			} +			while (wait(&s) >= 0) +				; +#endif + +			execl(PS, PS, PS_OPTIONS, (char *)0); +			perror(PS); +			_exit(0); +		} +		close(pipefd[1]); +		md5_context_init(&context); +		md5_context_hashstream(&context, &t, sizeof(t)); +		md5_context_hashstream(&context, &p, sizeof(p)); +		l=sizeof(t)+sizeof(p); + +		while ((n=read(pipefd[0], buf, sizeof(buf))) > 0) +		{ +			md5_context_hashstream(&context, buf, n); +			l += n; +		} +		md5_context_endstream(&context, l); +		md5_context_digest(&context, digest); +		close(pipefd[0]); +		while ((p2=wait(&s)) >= 0 && p != p2) +			; + +		for (n=0; n<sizeof(digest); n++) +			sprintf(randombuf+n*2, +				"%02X", (int)(unsigned char)digest[n]); +	} + +	return (randombuf); +} diff --git a/random128/random128.h b/random128/random128.h new file mode 100644 index 0000000..6f18a90 --- /dev/null +++ b/random128/random128.h @@ -0,0 +1,41 @@ +#ifndef	random128_h +#define	random128_h + +/* +** Copyright 1998 - 2002 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#ifdef	__cplusplus +extern "C" { +#endif + +/* +	random128 returns 128 random bits from the entropy.  random128 +	returns a pointer to 32 hexadecimal uppercase nibbles, all total +	being 128 bits. +*/ + +const char *random128(); + +/* +	random128_alpha does the same thing, except that the return +	string contains uppercase alphabetic letters only (letters 'A' +	through 'P'). +*/ + +const char *random128_alpha(); + +/* +** random128_bin(), saves the 128 random bits in 16 bytes. +*/ + +typedef unsigned char random128binbuf[16]; + +void random128_binary(random128binbuf *); + +#ifdef	__cplusplus +} +#endif + +#endif diff --git a/random128/random128alpha.c b/random128/random128alpha.c new file mode 100644 index 0000000..282c822 --- /dev/null +++ b/random128/random128alpha.c @@ -0,0 +1,29 @@ +/* +** Copyright 1998 - 2000 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if	HAVE_CONFIG_H +#include	"config.h" +#endif + +#if	HAVE_UNISTD_H +#include	<unistd.h> +#endif +#include	<ctype.h> +#include	<string.h> +#include	"random128.h" + + +const char *random128_alpha() +{ +static char randombuf[ 128 / 8 * 2 + 1]; +char *p; + +	strcpy(randombuf, random128()); + +	for (p=randombuf; *p; p++) +		if ( isdigit((int)(unsigned char)*p)) +			*p= "GHIJKLMNOP"[ *p - '0' ]; +	return (randombuf); +} diff --git a/random128/random128binary.c b/random128/random128binary.c new file mode 100644 index 0000000..e59218c --- /dev/null +++ b/random128/random128binary.c @@ -0,0 +1,38 @@ +/* +** Copyright 2002 Double Precision, Inc. +** See COPYING for distribution information. +*/ + +#if	HAVE_CONFIG_H +#include	"config.h" +#endif + +#if	HAVE_UNISTD_H +#include	<unistd.h> +#endif +#include	<ctype.h> +#include	<string.h> +#include	"random128.h" + + +static int nyb(char c) +{ +	static const char xdigit[]="0123456789ABCDEF"; + +	const char *p=strchr(xdigit, c); + +	if (p) +		return (p-xdigit); +	return 0; +} + +void random128_binary(random128binbuf *bytes) +{ +	char randombuf[ 128 / 8 * 2 + 1]; +	int i; + +	strcpy(randombuf, random128()); + +	for (i=0; i<128/8; i++) +		(*bytes)[i]=(nyb(randombuf[i*2]) << 4) | nyb(randombuf[i*2+1]); +} | 
