Key.c revision 515ec619
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>
3444dda7b2Smrg
35fc544a13Smrg#ifndef HAVE_ARC4RANDOM_BUF
3644dda7b2Smrgstatic void
3744dda7b2Smrggetbits (long data, unsigned char *dst)
3844dda7b2Smrg{
3944dda7b2Smrg    dst[0] = (data      ) & 0xff;
4044dda7b2Smrg    dst[1] = (data >>  8) & 0xff;
4144dda7b2Smrg    dst[2] = (data >> 16) & 0xff;
4244dda7b2Smrg    dst[3] = (data >> 24) & 0xff;
4344dda7b2Smrg}
44fc544a13Smrg#endif
4544dda7b2Smrg
4644dda7b2Smrg#define Time_t time_t
4744dda7b2Smrg
4844dda7b2Smrg#include <stdlib.h>
4944dda7b2Smrg
5044dda7b2Smrg#if defined(HAVE_LRAND48) && defined(HAVE_SRAND48)
5144dda7b2Smrg#define srandom srand48
5244dda7b2Smrg#define random lrand48
5344dda7b2Smrg#endif
5444dda7b2Smrg#ifdef WIN32
5544dda7b2Smrg#include <process.h>
5644dda7b2Smrg#define srandom srand
5744dda7b2Smrg#define random rand
5844dda7b2Smrg#define getpid(x) _getpid(x)
5944dda7b2Smrg#endif
6044dda7b2Smrg
61fc544a13Smrg#ifndef HAVE_ARC4RANDOM_BUF
626fc0178dSmrg
63515ec619Smrg/* Solaris 11.3.0 - 11.4.15 only define getentropy() in <sys/random.h> */
64515ec619Smrg#if HAVE_GETENTROPY && HAVE_SYS_RANDOM_H
65515ec619Smrg# include <sys/random.h>
66515ec619Smrg#endif
67515ec619Smrg
686fc0178dSmrgstatic void
696fc0178dSmrginsecure_getrandom_buf (unsigned char *auth, int len)
706fc0178dSmrg{
7144dda7b2Smrg    long    lowbits, highbits;
7244dda7b2Smrg
7344dda7b2Smrg    srandom ((int)getpid() ^ time((Time_t *)0));
7444dda7b2Smrg    lowbits = random ();
7544dda7b2Smrg    highbits = random ();
766fc0178dSmrg    getbits (lowbits, auth);
776fc0178dSmrg    getbits (highbits, auth + 4);
786fc0178dSmrg}
796fc0178dSmrg
806fc0178dSmrgstatic void
816fc0178dSmrgarc4random_buf (void *auth, int len)
826fc0178dSmrg{
83515ec619Smrg#if HAVE_GETENTROPY
846fc0178dSmrg    int	    ret;
856fc0178dSmrg
866fc0178dSmrg    /* weak emulation of arc4random through the getentropy libc call */
876fc0178dSmrg    ret = getentropy (auth, len);
886fc0178dSmrg    if (ret == 0)
896fc0178dSmrg	return;
906fc0178dSmrg#endif /* HAVE_GETENTROPY */
916fc0178dSmrg
926fc0178dSmrg    insecure_getrandom_buf (auth, len);
936fc0178dSmrg}
946fc0178dSmrg
956fc0178dSmrg#endif /* !defined(HAVE_ARC4RANDOM_BUF) */
966fc0178dSmrg
976fc0178dSmrgvoid
986fc0178dSmrgXdmcpGenerateKey (XdmAuthKeyPtr key)
996fc0178dSmrg{
100fc544a13Smrg    arc4random_buf(key->data, 8);
10144dda7b2Smrg}
10244dda7b2Smrg
10344dda7b2Smrgint
10444dda7b2SmrgXdmcpCompareKeys (const XdmAuthKeyPtr a, const XdmAuthKeyPtr b)
10544dda7b2Smrg{
10644dda7b2Smrg    int	i;
10744dda7b2Smrg
10844dda7b2Smrg    for (i = 0; i < 8; i++)
10944dda7b2Smrg	if (a->data[i] != b->data[i])
11044dda7b2Smrg	    return FALSE;
11144dda7b2Smrg    return TRUE;
11244dda7b2Smrg}
11344dda7b2Smrg
11444dda7b2Smrgvoid
11544dda7b2SmrgXdmcpIncrementKey (XdmAuthKeyPtr key)
11644dda7b2Smrg{
11744dda7b2Smrg    int	i;
11844dda7b2Smrg
11944dda7b2Smrg    i = 7;
12044dda7b2Smrg    while (++key->data[i] == 0)
12144dda7b2Smrg	if (--i < 0)
12244dda7b2Smrg	    break;
12344dda7b2Smrg}
12444dda7b2Smrg
12544dda7b2Smrgvoid
12644dda7b2SmrgXdmcpDecrementKey (XdmAuthKeyPtr key)
12744dda7b2Smrg{
12844dda7b2Smrg    int	i;
12944dda7b2Smrg
13044dda7b2Smrg    i = 7;
13144dda7b2Smrg    while (key->data[i]-- == 0)
13244dda7b2Smrg	if (--i < 0)
13344dda7b2Smrg	    break;
13444dda7b2Smrg}
135