padrand (one-time pad to random; take two)

Istvan Mohos istvan at hhb.UUCP
Tue Sep 18 03:04:36 AEST 1990


My efforts to provide a "well rounded" functionality with my earlier
posting of padrand(), nevertheless neglected a segment of readership
keen on speed and on doing things with inter-process pipes.  By way of
atonement, I'm posting a second, stand-alone implementation of padrand
(padrand.c).  The routine can be made a lot faster still, by changing
to buffered I/O and by swapping the inner for-loop for an expanded
block using a fixed "bit-width" parameter.

==============================CUT HERE===============================
/************************************************
* padrand.c --- random numbers from one-time pads
* Istvan Mohos, 1990 --- in the Public Domain
*************************************************/

#include <stdio.h>
#ifdef RAW_INT
#define OUTPUT write(1,(char*)(&rand),sizeof(int))
#else
#define OUTPUT printf("%d\n",rand)
#endif

main (argc, argv)
int argc;
char *argv[];
{
	register int bits, rand, silkie = 0;
	register char *bp, *end;
	char buf[sizeof(int)<<3];

	if (argc != 2)
		fprintf(stderr, "Usage: %s bits\n", argv[0]), exit(1);
	if ((bits = abs(atoi(argv[1]))) > (sizeof(int)<<3) || !bits)
		fprintf(stderr, "Maximum bits %d, minimum 1\n", sizeof(int)<<3),
			exit(1);
	for (; read(0, buf, bits) == bits; OUTPUT)
		for (rand = 0, bp = buf, end = bp + bits; bp < end; bp++)
			rand <<= 1, rand += (*bp + silkie)&1, silkie = !silkie;
	exit (0);
}
==============================CUT HERE===============================

The next two paragraphs are a continuation of the original description
of the one-time pad method of random number generation, and are
"Copyright 1990, Istvan Mohos, All Rights Reserved".

   Just as with encryption, a caveat may be in order: the warning that
   one-time pads not be monotone is not to be taken lightly.  The track
   record of one-time pad security may lull one into believing that the
   method is forgiving of minor breaches in the ground rules.
   Surrounded by mountainous ballasts of idle source code, the average
   programmer may even strive to be convinced that source files are
   suitable for one-time pads, to be able to bring otherwise static data
   back into play.  And yet observing that C text lines inevitably start
   with spaces or tabs, the code breaker could blow cyphertext encrypted
   with C code "chock-full-o-cribs" on a first attempt by globally
   XOR-ing with spaces or tabs, and in addition to clearing parts of the
   plaintext gain significant insights about the the key.
   
   At least with padrand, monotone pads only destroy the perfect
   distribution of random numbers in the output.  Still, it is best to
   strip pad text of redundancy.  Run the pad through compress for
   example, or (consider it as an incentive for saving space!) keep
   entire /pub or /src directories compressed.

And since this gives you FAST TRUE RANDOM numbers in SOFTWARE, don't
let me catch anyone manufacturing pseudo-random numbers again!  :-)
-- 
        Istvan Mohos
        ...uunet!pyrdc!pyrnj!hhb!istvan
        1000 Wyckoff Ave. Mahwah NJ 07430 201-848-8000
======================================================



More information about the Alt.sources mailing list