Key.c revision 515ec619
1/* 2Copyright 1989, 1998 The Open Group 3 4Permission to use, copy, modify, distribute, and sell this software and its 5documentation for any purpose is hereby granted without fee, provided that 6the above copyright notice appear in all copies and that both that 7copyright notice and this permission notice appear in supporting 8documentation. 9 10The above copyright notice and this permission notice shall be included in 11all copies or substantial portions of the Software. 12 13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 17AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 20Except as contained in this notice, the name of The Open Group shall not be 21used in advertising or otherwise to promote the sale, use or other dealings 22in this Software without prior written authorization from The Open Group. 23 * 24 * Author: Keith Packard, MIT X Consortium 25 */ 26 27#ifdef HAVE_CONFIG_H 28#include <config.h> 29#endif 30#include <X11/Xos.h> 31#include <X11/X.h> 32#include <X11/Xmd.h> 33#include <X11/Xdmcp.h> 34 35#ifndef HAVE_ARC4RANDOM_BUF 36static void 37getbits (long data, unsigned char *dst) 38{ 39 dst[0] = (data ) & 0xff; 40 dst[1] = (data >> 8) & 0xff; 41 dst[2] = (data >> 16) & 0xff; 42 dst[3] = (data >> 24) & 0xff; 43} 44#endif 45 46#define Time_t time_t 47 48#include <stdlib.h> 49 50#if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) 51#define srandom srand48 52#define random lrand48 53#endif 54#ifdef WIN32 55#include <process.h> 56#define srandom srand 57#define random rand 58#define getpid(x) _getpid(x) 59#endif 60 61#ifndef HAVE_ARC4RANDOM_BUF 62 63/* Solaris 11.3.0 - 11.4.15 only define getentropy() in <sys/random.h> */ 64#if HAVE_GETENTROPY && HAVE_SYS_RANDOM_H 65# include <sys/random.h> 66#endif 67 68static void 69insecure_getrandom_buf (unsigned char *auth, int len) 70{ 71 long lowbits, highbits; 72 73 srandom ((int)getpid() ^ time((Time_t *)0)); 74 lowbits = random (); 75 highbits = random (); 76 getbits (lowbits, auth); 77 getbits (highbits, auth + 4); 78} 79 80static void 81arc4random_buf (void *auth, int len) 82{ 83#if HAVE_GETENTROPY 84 int ret; 85 86 /* weak emulation of arc4random through the getentropy libc call */ 87 ret = getentropy (auth, len); 88 if (ret == 0) 89 return; 90#endif /* HAVE_GETENTROPY */ 91 92 insecure_getrandom_buf (auth, len); 93} 94 95#endif /* !defined(HAVE_ARC4RANDOM_BUF) */ 96 97void 98XdmcpGenerateKey (XdmAuthKeyPtr key) 99{ 100 arc4random_buf(key->data, 8); 101} 102 103int 104XdmcpCompareKeys (const XdmAuthKeyPtr a, const XdmAuthKeyPtr b) 105{ 106 int i; 107 108 for (i = 0; i < 8; i++) 109 if (a->data[i] != b->data[i]) 110 return FALSE; 111 return TRUE; 112} 113 114void 115XdmcpIncrementKey (XdmAuthKeyPtr key) 116{ 117 int i; 118 119 i = 7; 120 while (++key->data[i] == 0) 121 if (--i < 0) 122 break; 123} 124 125void 126XdmcpDecrementKey (XdmAuthKeyPtr key) 127{ 128 int i; 129 130 i = 7; 131 while (key->data[i]-- == 0) 132 if (--i < 0) 133 break; 134} 135