144dda7b2Smrg/*
244dda7b2SmrgCopyright 1989, 1998  The Open Group
344dda7b2Smrg
444dda7b2SmrgPermission to use, copy, modify, distribute, and sell this software and its
544dda7b2Smrgdocumentation for any purpose is hereby granted without fee, provided that
644dda7b2Smrgthe above copyright notice appear in all copies and that both that
744dda7b2Smrgcopyright notice and this permission notice appear in supporting
844dda7b2Smrgdocumentation.
944dda7b2Smrg
1044dda7b2SmrgThe above copyright notice and this permission notice shall be included in
1144dda7b2Smrgall copies or substantial portions of the Software.
1244dda7b2Smrg
1344dda7b2SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1444dda7b2SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1544dda7b2SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1644dda7b2SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1744dda7b2SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1844dda7b2SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1944dda7b2Smrg
2044dda7b2SmrgExcept as contained in this notice, the name of The Open Group shall not be
2144dda7b2Smrgused in advertising or otherwise to promote the sale, use or other dealings
2244dda7b2Smrgin this Software without prior written authorization from The Open Group.
2344dda7b2Smrg *
2444dda7b2Smrg * Author:  Keith Packard, MIT X Consortium
2544dda7b2Smrg */
2644dda7b2Smrg
2744dda7b2Smrg#ifdef HAVE_CONFIG_H
2844dda7b2Smrg#include <config.h>
2944dda7b2Smrg#endif
3044dda7b2Smrg#include <X11/Xos.h>
3144dda7b2Smrg#include <X11/X.h>
3244dda7b2Smrg#include <X11/Xmd.h>
3344dda7b2Smrg#include <X11/Xdmcp.h>
34e08b657dSmrg#include <stdlib.h>
3544dda7b2Smrg
36fc544a13Smrg#ifndef HAVE_ARC4RANDOM_BUF
3744dda7b2Smrgstatic void
3844dda7b2Smrggetbits (long data, unsigned char *dst)
3944dda7b2Smrg{
4044dda7b2Smrg    dst[0] = (data      ) & 0xff;
4144dda7b2Smrg    dst[1] = (data >>  8) & 0xff;
4244dda7b2Smrg    dst[2] = (data >> 16) & 0xff;
4344dda7b2Smrg    dst[3] = (data >> 24) & 0xff;
4444dda7b2Smrg}
4544dda7b2Smrg
4644dda7b2Smrg#define Time_t time_t
4744dda7b2Smrg
4844dda7b2Smrg#if defined(HAVE_LRAND48) && defined(HAVE_SRAND48)
4944dda7b2Smrg#define srandom srand48
5044dda7b2Smrg#define random lrand48
5144dda7b2Smrg#endif
5244dda7b2Smrg#ifdef WIN32
5344dda7b2Smrg#include <process.h>
5444dda7b2Smrg#define srandom srand
5544dda7b2Smrg#define random rand
5644dda7b2Smrg#define getpid(x) _getpid(x)
5744dda7b2Smrg#endif
5844dda7b2Smrg
59515ec619Smrg/* Solaris 11.3.0 - 11.4.15 only define getentropy() in <sys/random.h> */
60515ec619Smrg#if HAVE_GETENTROPY && HAVE_SYS_RANDOM_H
61515ec619Smrg# include <sys/random.h>
62515ec619Smrg#endif
63515ec619Smrg
646fc0178dSmrgstatic void
656fc0178dSmrginsecure_getrandom_buf (unsigned char *auth, int len)
666fc0178dSmrg{
6744dda7b2Smrg    long    lowbits, highbits;
6844dda7b2Smrg
6944dda7b2Smrg    srandom ((int)getpid() ^ time((Time_t *)0));
7044dda7b2Smrg    lowbits = random ();
7144dda7b2Smrg    highbits = random ();
726fc0178dSmrg    getbits (lowbits, auth);
736fc0178dSmrg    getbits (highbits, auth + 4);
746fc0178dSmrg}
756fc0178dSmrg
766fc0178dSmrgstatic void
776fc0178dSmrgarc4random_buf (void *auth, int len)
786fc0178dSmrg{
79515ec619Smrg#if HAVE_GETENTROPY
806fc0178dSmrg    int	    ret;
816fc0178dSmrg
826fc0178dSmrg    /* weak emulation of arc4random through the getentropy libc call */
836fc0178dSmrg    ret = getentropy (auth, len);
846fc0178dSmrg    if (ret == 0)
856fc0178dSmrg	return;
866fc0178dSmrg#endif /* HAVE_GETENTROPY */
876fc0178dSmrg
886fc0178dSmrg    insecure_getrandom_buf (auth, len);
896fc0178dSmrg}
906fc0178dSmrg
916fc0178dSmrg#endif /* !defined(HAVE_ARC4RANDOM_BUF) */
926fc0178dSmrg
936fc0178dSmrgvoid
946fc0178dSmrgXdmcpGenerateKey (XdmAuthKeyPtr key)
956fc0178dSmrg{
96fc544a13Smrg    arc4random_buf(key->data, 8);
9744dda7b2Smrg}
9844dda7b2Smrg
9944dda7b2Smrgint
10044dda7b2SmrgXdmcpCompareKeys (const XdmAuthKeyPtr a, const XdmAuthKeyPtr b)
10144dda7b2Smrg{
10244dda7b2Smrg    int	i;
10344dda7b2Smrg
10444dda7b2Smrg    for (i = 0; i < 8; i++)
10544dda7b2Smrg	if (a->data[i] != b->data[i])
10644dda7b2Smrg	    return FALSE;
10744dda7b2Smrg    return TRUE;
10844dda7b2Smrg}
10944dda7b2Smrg
11044dda7b2Smrgvoid
11144dda7b2SmrgXdmcpIncrementKey (XdmAuthKeyPtr key)
11244dda7b2Smrg{
11344dda7b2Smrg    int	i;
11444dda7b2Smrg
11544dda7b2Smrg    i = 7;
11644dda7b2Smrg    while (++key->data[i] == 0)
11744dda7b2Smrg	if (--i < 0)
11844dda7b2Smrg	    break;
11944dda7b2Smrg}
12044dda7b2Smrg
12144dda7b2Smrgvoid
12244dda7b2SmrgXdmcpDecrementKey (XdmAuthKeyPtr key)
12344dda7b2Smrg{
12444dda7b2Smrg    int	i;
12544dda7b2Smrg
12644dda7b2Smrg    i = 7;
12744dda7b2Smrg    while (key->data[i]-- == 0)
12844dda7b2Smrg	if (--i < 0)
12944dda7b2Smrg	    break;
13044dda7b2Smrg}
131