Key.c revision 6fc0178d
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#ifdef HAVE_LIBBSD 36#include <bsd/stdlib.h> /* for arc4random_buf() */ 37#endif 38 39#ifndef HAVE_ARC4RANDOM_BUF 40static void 41getbits (long data, unsigned char *dst) 42{ 43 dst[0] = (data ) & 0xff; 44 dst[1] = (data >> 8) & 0xff; 45 dst[2] = (data >> 16) & 0xff; 46 dst[3] = (data >> 24) & 0xff; 47} 48#endif 49 50#define Time_t time_t 51 52#include <stdlib.h> 53 54#if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) 55#define srandom srand48 56#define random lrand48 57#endif 58#ifdef WIN32 59#include <process.h> 60#define srandom srand 61#define random rand 62#define getpid(x) _getpid(x) 63#endif 64 65#ifndef HAVE_ARC4RANDOM_BUF 66 67static void 68insecure_getrandom_buf (unsigned char *auth, int len) 69{ 70 long lowbits, highbits; 71 72 srandom ((int)getpid() ^ time((Time_t *)0)); 73 lowbits = random (); 74 highbits = random (); 75 getbits (lowbits, auth); 76 getbits (highbits, auth + 4); 77} 78 79static void 80arc4random_buf (void *auth, int len) 81{ 82 int ret; 83 84#if HAVE_GETENTROPY 85 /* weak emulation of arc4random through the getentropy libc call */ 86 ret = getentropy (auth, len); 87 if (ret == 0) 88 return; 89#endif /* HAVE_GETENTROPY */ 90 91 insecure_getrandom_buf (auth, len); 92} 93 94#endif /* !defined(HAVE_ARC4RANDOM_BUF) */ 95 96void 97XdmcpGenerateKey (XdmAuthKeyPtr key) 98{ 99 arc4random_buf(key->data, 8); 100} 101 102int 103XdmcpCompareKeys (const XdmAuthKeyPtr a, const XdmAuthKeyPtr b) 104{ 105 int i; 106 107 for (i = 0; i < 8; i++) 108 if (a->data[i] != b->data[i]) 109 return FALSE; 110 return TRUE; 111} 112 113void 114XdmcpIncrementKey (XdmAuthKeyPtr key) 115{ 116 int i; 117 118 i = 7; 119 while (++key->data[i] == 0) 120 if (--i < 0) 121 break; 122} 123 124void 125XdmcpDecrementKey (XdmAuthKeyPtr key) 126{ 127 int i; 128 129 i = 7; 130 while (key->data[i]-- == 0) 131 if (--i < 0) 132 break; 133} 134