home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / des / random_key.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-21  |  2.3 KB  |  104 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/des/RCS/random_key.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>.
  9.  *
  10.  * These routines perform encryption and decryption using the DES
  11.  * private key algorithm, or else a subset of it-- fewer inner loops.
  12.  * ( AUTH_DES_ITER defaults to 16, may be less)
  13.  *
  14.  * Under U.S. law, this software may not be exported outside the US
  15.  * without license from the U.S. Commerce department.
  16.  *
  17.  * The key schedule is passed as an arg, as well as the cleartext or
  18.  * ciphertext.     The cleartext and ciphertext should be in host order.
  19.  *
  20.  * These routines form the library interface to the des facilities.
  21.  *
  22.  * spm    8/85    MIT project athena
  23.  */
  24.  
  25. #ifndef    lint
  26. static char rcsid_random_key_c[] =
  27. "$Header: random_key.c,v 4.8 89/01/21 16:50:39 jtkohl Exp $";
  28. #endif    lint
  29.  
  30. #include <mit-copyright.h>
  31. #include <stdio.h>
  32.  
  33. #include <des.h>
  34. #include "des_internal.h"
  35.  
  36. #ifdef BSDUNIX
  37. #include <sys/time.h>
  38. #endif
  39.  
  40. extern int des_debug;
  41. extern int des_debug_print();
  42.  
  43. /* random_key */
  44. int
  45. des_random_key(key)
  46.     des_cblock *key;
  47. {
  48.     /*
  49.      * create a random des key; should force odd parity per byte;
  50.      * parity is bits 8,16,...64 in des order, implies 0, 8, 16, ...
  51.      * vax order
  52.      */
  53.  
  54.     register unsigned int temp;
  55.     register int odd;
  56.     register unsigned char *c = (unsigned char *) key;
  57.     unsigned long *k = (unsigned long *) key;
  58.     static long p = 0;
  59.     static long n = 0;
  60.     long gethostid(), random();
  61.  
  62.     int i,j;
  63.  
  64. #ifdef BSDUNIX
  65.     static struct timeval time;
  66.  
  67.     if (!p) {
  68.     p = getpid();
  69.     p ^= gethostid();
  70.     }
  71.  
  72.     (void) gettimeofday(&time,(struct timezone *)0);
  73.     /* randomize start */
  74.     srandom(time.tv_usec ^ time.tv_sec ^ p ^ n++);
  75.  
  76.     *k++ = random();
  77.     *k = random();
  78.  
  79.     /* make each byte parity odd */
  80.     for (i = 0; i <= 7; i++) {
  81.     odd = 0;
  82.     temp = (unsigned int) *c;
  83.     /* ignore bit 0, lsb,  it will be parity (on vax) */
  84.     /* should do this with a table lookup */
  85.     for (j = 0; j <= 6; j++) {
  86.         temp = temp >> 1;
  87.         odd ^= temp & 01;
  88.     }
  89.     /* set odd parity in lsb */
  90.     if (!odd)
  91.         *c |= 1;
  92.     else
  93.         *c &= ~1;
  94.     c++;
  95.     }
  96.  
  97.     /* **** */
  98. #else
  99.     dont know how to do random numbers for this machine;
  100. #endif
  101.  
  102.     return 0;
  103. }
  104.