1
/* $XdotOrg: xc/programs/xdm/genauth.c,v 1.1.4.5 2003/12/20 00:28:31 kaleb Exp $ */
2
/* $Xorg: genauth.c,v 1.5 2001/02/09 02:05:40 xorgcvs Exp $ */
5
3
Copyright 1988, 1998 The Open Group
61
58
typedef struct auth_ks_struct { auth_cblock _; } auth_wrapper_schedule[16];
63
60
extern int _XdmcpAuthSetup(unsigned char *, auth_wrapper_schedule);
64
extern int _XdmcpAuthDoIt(unsigned char *, unsigned char *,
61
extern int _XdmcpAuthDoIt(unsigned char *, unsigned char *,
65
62
auth_wrapper_schedule, int);
66
63
extern void _XdmcpWrapperToOddParity(unsigned char *, unsigned char *);
89
86
return poll(&fds, 1, 5000);
92
#define pollRandomDevice(fd) 1
89
# define pollRandomDevice(fd) 1
95
92
#if !defined(ARC4_RANDOM)
168
165
/* The four core functions - F1 is optimized somewhat */
169
#define F1(x, y, z) (z ^ (x & (y ^ z)))
170
#define F2(x, y, z) F1 (z, x, y)
171
#define F3(x, y, z) (x ^ y ^ z)
172
#define F4(x, y, z) (y ^ (x | ~z))
166
# define F1(x, y, z) (z ^ (x & (y ^ z)))
167
# define F2(x, y, z) F1 (z, x, y)
168
# define F3(x, y, z) (x ^ y ^ z)
169
# define F4(x, y, z) (y ^ (x | ~z))
174
171
/* This is the central step in the MD5 algorithm. */
175
#define pmd5_step(f, w, x, y, z, data, s) \
172
# define pmd5_step(f, w, x, y, z, data, s) \
176
173
( w += (f(x, y, z) + data) & 0xffffffff, w = w<<s | w>>(32-s), w += x )
179
176
* The core of the MD5 algorithm, this alters an existing MD5 hash to
180
177
* reflect the addition of 16 longwords of new data.
183
180
pmd5_hash (CARD32 *out, const CARD32 in[16])
185
182
CARD32 a, b, c, d;
205
202
pmd5_step(F1, d, a, b, c, in[13] + 0xfd987193, 12);
206
203
pmd5_step(F1, c, d, a, b, in[14] + 0xa679438e, 17);
207
204
pmd5_step(F1, b, c, d, a, in[15] + 0x49b40821, 22);
209
206
pmd5_step(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
210
207
pmd5_step(F2, d, a, b, c, in[6] + 0xc040b340, 9);
211
208
pmd5_step(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
222
219
pmd5_step(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
223
220
pmd5_step(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
224
221
pmd5_step(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
226
223
pmd5_step(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
227
224
pmd5_step(F3, d, a, b, c, in[8] + 0x8771f681, 11);
228
225
pmd5_step(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
239
236
pmd5_step(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
240
237
pmd5_step(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
241
238
pmd5_step(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
243
240
pmd5_step(F4, a, b, c, d, in[0] + 0xf4292244, 6);
244
241
pmd5_step(F4, d, a, b, c, in[7] + 0x432aff97, 10);
245
242
pmd5_step(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
325
322
AddTimerEntropy();
326
323
if ((readlen = sumFile (randomFile, BSIZ, SEEK_SET, offset)) == BSIZ) {
327
324
offset += readlen;
328
#ifdef FRAGILE_DEV_MEM
325
# ifdef FRAGILE_DEV_MEM
329
326
if (!strcmp (randomFile, "/dev/mem")) {
330
327
if (offset == 0xa0000) /* skip 640kB-1MB ROM mappings */
331
328
offset = 0x100000;
332
329
else if (offset == 0xf00000) /* skip 15-16MB memory hole */
333
330
offset = 0x1000000;
337
334
} else if (readlen >= 0 && offset) {
338
335
if ((offset = sumFile (randomFile, BSIZ, SEEK_SET, 0)) == BSIZ)
353
350
sum[0] = arc4random();
354
351
sum[1] = arc4random();
355
352
*(u_char *)sum = 0;
357
354
_XdmcpWrapperToOddParity((unsigned char *)sum, key);
359
356
unsigned char tmpkey[8];
364
361
if ((fd = open(randomDevice, O_RDONLY)) >= 0) {
365
362
if (pollRandomDevice(fd) && read(fd, tmpkey, 8) == 8) {
367
364
_XdmcpWrapperToOddParity(tmpkey, key);
374
LogError("Cannot open randomDevice \"%s\", errno = %d\n",
371
LogError("Cannot open randomDevice \"%s\", errno = %d\n",
375
372
randomDevice, errno);
378
375
/* Try some pseudo-random number genrator daemon next */
379
376
if (prngdSocket != NULL || prngdPort != 0) {
380
if (get_prngd_bytes(tmpkey, sizeof(tmpkey), prngdPort,
377
if (get_prngd_bytes((char *)tmpkey, sizeof(tmpkey), prngdPort,
381
378
prngdSocket) == 0) {
383
380
_XdmcpWrapperToOddParity(tmpkey, key);
411
408
static int xdmcpAuthInited;
415
412
struct timeval now;
417
414
X_GETTIMEOFDAY (&now);
418
415
ldata[0] = now.tv_usec;
419
416
ldata[1] = now.tv_sec;
421
418
ldata[0] = time ((long *) 0);
422
419
ldata[1] = getpid ();
425
422
longtochars (ldata[0], data+0);
426
423
longtochars (ldata[1], data+4);
427
424
if (!xdmcpAuthInited)
442
439
#else /* !XDMAUTH */
444
441
CARD32 *rnd = (CARD32 *)auth;
447
444
for (i = 0; i < len; i += 4)
448
445
rnd[i / 4] = arc4random();
450
#else /* !ARC4_RANDOM */
447
# else /* !ARC4_RANDOM */
451
448
CARD32 tmp[4] = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
455
452
if ((fd = open(randomDevice, O_RDONLY)) >= 0) {
456
if (pollRandomDevice(fd) &&
453
if (pollRandomDevice(fd) &&
457
454
read(fd, auth, len) == len) {
462
459
LogError("Cannot read randomDevice \"%s\", errno=%d\n",
463
460
randomDevice, errno);
465
LogError("Cannot open randomDevice \"%s\", errno = %d\n",
462
LogError("Cannot open randomDevice \"%s\", errno = %d\n",
466
463
randomDevice, errno);
467
#endif /* DEV_RANDOM */
464
# endif /* DEV_RANDOM */
468
465
/* Try some pseudo-random number genrator daemon next */
469
466
if (prngdSocket != NULL || prngdPort != 0) {
470
467
if (get_prngd_bytes(auth, len, prngdPort, prngdSocket) == 0) {