summaryrefslogtreecommitdiff
path: root/libdes/random_key.c
blob: 8c20d8532222d9a7715a2a9652db461eff58abb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * $Source$
 * $Author$
 *
 * Copyright 1988 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * These routines perform encryption and decryption using the DES
 * private key algorithm, or else a subset of it-- fewer inner loops.
 * ( AUTH_DES_ITER defaults to 16, may be less)
 *
 * Under U.S. law, this software may not be exported outside the US
 * without license from the U.S. Commerce department.
 *
 * The key schedule is passed as an arg, as well as the cleartext or
 * ciphertext.	 The cleartext and ciphertext should be in host order.
 *
 * These routines form the library interface to the des facilities.
 *
 * spm	8/85	MIT project athena
 */

#ifndef	lint
static char rcsid_random_key_c[] =
    "$Id$";
#endif

#include <mit-copyright.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

#include <des.h>

#ifdef USE_RAND48
#define random	lrand48
#define srandom	srand48
#endif

/* random_key */
int
des_random_key(key)
    des_cblock *key;
{
    /*
     * create a random des key; should force odd parity per byte;
     * parity is bits 8,16,...64 in des order, implies 0, 8, 16, ...
     * vax order
     */

    register unsigned int temp;
    register int odd;
    register unsigned char *c = (unsigned char *) key;
    unsigned long *k = (unsigned long *) key;
    static long p = 0;
    static long n = 0;

    int i,j;

    static struct timeval time;

    if (!p) {
	p = getpid();
#ifndef SOLARIS
	p ^= gethostid();
#endif
    }

    (void) gettimeofday(&time,(struct timezone *)0);
    /* randomize start */
    srandom(time.tv_usec ^ time.tv_sec ^ p ^ n++);

    *k++ = random();
    *k = random();

    /* make each byte parity odd */
    for (i = 0; i <= 7; i++) {
	odd = 0;
	temp = (unsigned int) *c;
	/* ignore bit 0, lsb,  it will be parity (on vax) */
	/* should do this with a table lookup */
	for (j = 0; j <= 6; j++) {
	    temp = temp >> 1;
	    odd ^= temp & 01;
	}
	/* set odd parity in lsb */
	if (!odd)
	    *c |= 1;
	else
	    *c &= ~1;
	c++;
    }

    return 0;
}