1 /* $NetBSD: rnd_keys.c,v 1.2 2014/03/27 16:26:22 apb Exp $ */
13 des_set_random_generator_seed(des_cblock
*seed
)
16 des_random_seed(seed
);
20 * Generate a sequence of random des keys
21 * using the random block sequence, fixup
22 * parity and skip weak keys.
25 des_new_random_key(des_cblock
*key
)
30 urandom
= open("/dev/urandom", O_RDONLY
);
35 if (read(urandom
, key
,
36 sizeof(des_cblock
)) != sizeof(des_cblock
)) {
43 /* random key must have odd parity and not be weak */
44 des_set_odd_parity(key
);
45 if (des_is_weak_key(key
))
52 * des_init_random_number_generator:
54 * This routine takes a secret key possibly shared by a number of servers
55 * and uses it to generate a random number stream that is not shared by
56 * any of the other servers. It does this by using the current process id,
57 * host id, and the current time to the nearest second. The resulting
58 * stream seed is not useful information for cracking the secret key.
59 * Moreover, this routine keeps no copy of the secret key.
62 des_init_random_number_generator(des_cblock
*seed
)
69 char hname
[64], accum
[512];
75 gethostname(hname
, sizeof(hname
) - 1);
76 gettimeofday(&when
, NULL
);
78 memcpy(&seed_q
, seed
, sizeof(seed_q
));
80 snprintf(accum
, sizeof(accum
), "%ld%ld%d%s%d%lld",
81 when
.tv_sec
, when
.tv_usec
, getpid(), hname
, getuid(),
84 SHA1Update(&sha
, (u_char
*) accum
, strlen(accum
));
86 memset(accum
, 0, sizeof(accum
));
88 SHA1Final(results
, &sha
);
90 memcpy(seed_new
, results
, sizeof(seed_new
));
91 des_random_seed(&seed_new
);
93 memset(seed_new
, 0, sizeof(seed_new
));
94 memset(results
, 0, sizeof(results
));