home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1835 < prev    next >
Encoding:
Internet Message Format  |  1990-12-28  |  3.2 KB

  1. From: istvan@hhb.UUCP (Istvan Mohos)
  2. Newsgroups: alt.sources,sci.crypt
  3. Subject: padrand (one-time pad to random; take two)
  4. Message-ID: <579@hhb.UUCP>
  5. Date: 17 Sep 90 17:04:36 GMT
  6.  
  7.  
  8. My efforts to provide a "well rounded" functionality with my earlier
  9. posting of padrand(), nevertheless neglected a segment of readership
  10. keen on speed and on doing things with inter-process pipes.  By way of
  11. atonement, I'm posting a second, stand-alone implementation of padrand
  12. (padrand.c).  The routine can be made a lot faster still, by changing
  13. to buffered I/O and by swapping the inner for-loop for an expanded
  14. block using a fixed "bit-width" parameter.
  15.  
  16. ==============================CUT HERE===============================
  17. /************************************************
  18. * padrand.c --- random numbers from one-time pads
  19. * Istvan Mohos, 1990 --- in the Public Domain
  20. *************************************************/
  21.  
  22. #include <stdio.h>
  23. #ifdef RAW_INT
  24. #define OUTPUT write(1,(char*)(&rand),sizeof(int))
  25. #else
  26. #define OUTPUT printf("%d\n",rand)
  27. #endif
  28.  
  29. main (argc, argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.     register int bits, rand, silkie = 0;
  34.     register char *bp, *end;
  35.     char buf[sizeof(int)<<3];
  36.  
  37.     if (argc != 2)
  38.         fprintf(stderr, "Usage: %s bits\n", argv[0]), exit(1);
  39.     if ((bits = abs(atoi(argv[1]))) > (sizeof(int)<<3) || !bits)
  40.         fprintf(stderr, "Maximum bits %d, minimum 1\n", sizeof(int)<<3),
  41.             exit(1);
  42.     for (; read(0, buf, bits) == bits; OUTPUT)
  43.         for (rand = 0, bp = buf, end = bp + bits; bp < end; bp++)
  44.             rand <<= 1, rand += (*bp + silkie)&1, silkie = !silkie;
  45.     exit (0);
  46. }
  47. ==============================CUT HERE===============================
  48.  
  49. The next two paragraphs are a continuation of the original description
  50. of the one-time pad method of random number generation, and are
  51. "Copyright 1990, Istvan Mohos, All Rights Reserved".
  52.  
  53.    Just as with encryption, a caveat may be in order: the warning that
  54.    one-time pads not be monotone is not to be taken lightly.  The track
  55.    record of one-time pad security may lull one into believing that the
  56.    method is forgiving of minor breaches in the ground rules.
  57.    Surrounded by mountainous ballasts of idle source code, the average
  58.    programmer may even strive to be convinced that source files are
  59.    suitable for one-time pads, to be able to bring otherwise static data
  60.    back into play.  And yet observing that C text lines inevitably start
  61.    with spaces or tabs, the code breaker could blow cyphertext encrypted
  62.    with C code "chock-full-o-cribs" on a first attempt by globally
  63.    XOR-ing with spaces or tabs, and in addition to clearing parts of the
  64.    plaintext gain significant insights about the the key.
  65.    
  66.    At least with padrand, monotone pads only destroy the perfect
  67.    distribution of random numbers in the output.  Still, it is best to
  68.    strip pad text of redundancy.  Run the pad through compress for
  69.    example, or (consider it as an incentive for saving space!) keep
  70.    entire /pub or /src directories compressed.
  71.  
  72. And since this gives you FAST TRUE RANDOM numbers in SOFTWARE, don't
  73. let me catch anyone manufacturing pseudo-random numbers again!  :-)
  74. -- 
  75.         Istvan Mohos
  76.         ...uunet!pyrdc!pyrnj!hhb!istvan
  77.         1000 Wyckoff Ave. Mahwah NJ 07430 201-848-8000
  78. ======================================================
  79.