X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/55e303ae13a4cf49d70f2294092726f2fffb9ef2..3903760236c30e3b5ace7a4eefac3a269d68957c:/bsd/dev/random/randomdev.c diff --git a/bsd/dev/random/randomdev.c b/bsd/dev/random/randomdev.c index 63d66a6ea..6482f6094 100644 --- a/bsd/dev/random/randomdev.c +++ b/bsd/dev/random/randomdev.c @@ -1,17 +1,20 @@ /* - * Copyright (c) 1999, 2000-2003 Apple Computer, Inc. All rights reserved. + * Copyright (c) 1999-2009 Apple, Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * - * @APPLE_LICENSE_HEADER_START@ - * - * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. - * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in - * compliance with the License. Please obtain a copy of the License at - * http://www.opensource.apple.com/apsl/ and read it before using this - * file. - * + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -19,10 +22,11 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * - * @APPLE_LICENSE_HEADER_END@ + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ + #include #include #include @@ -30,15 +34,25 @@ #include #include #include +#include #include -#include +#include +#include #include #include +#include +#include #include -#include + +#include +#include + +#include #define RANDOM_MAJOR -1 /* let the kernel pick the device number */ +#define RANDOM_MINOR 0 +#define URANDOM_MINOR 1 d_ioctl_t random_ioctl; @@ -52,9 +66,9 @@ static struct cdevsw random_cdevsw = random_close, /* close */ random_read, /* read */ random_write, /* write */ - random_ioctl, /* ioctl */ - nulldev, /* stop */ - nulldev, /* reset */ + random_ioctl, /* ioctl */ + (stop_fcn_t *)nulldev, /* stop */ + (reset_fcn_t *)nulldev, /* reset */ NULL, /* tty's */ eno_select, /* select */ eno_mmap, /* mmap */ @@ -64,103 +78,36 @@ static struct cdevsw random_cdevsw = 0 /* type */ }; -/* Used to detect whether we've already been initialized */ -static int gRandomInstalled = 0; -static PrngRef gPrngRef; -static int gRandomError = 1; -static mutex_t *gYarrowMutex = 0; - -#define RESEED_TICKS 50 /* how long a reseed operation can take */ - -/* - *Initialize ONLY the Yarrow generator. - */ -void PreliminarySetup () -{ - prng_error_status perr; - struct timeval tt; - char buffer [16]; - - /* create a Yarrow object */ - perr = prngInitialize(&gPrngRef); - if (perr != 0) { - printf ("Couldn't initialize Yarrow, /dev/random will not work.\n"); - return; - } - - /* clear the error flag, reads and write should then work */ - gRandomError = 0; - - /* get a little non-deterministic data as an initial seed. */ - microtime(&tt); - - /* - * So how much of the system clock is entropic? - * It's hard to say, but assume that at least the - * least significant byte of a 64 bit structure - * is entropic. It's probably more, how can you figure - * the exact time the user turned the computer on, for example. - */ - perr = prngInput(gPrngRef, (BYTE*) &tt, sizeof (tt), SYSTEM_SOURCE, 8); - if (perr != 0) { - /* an error, complain */ - printf ("Couldn't seed Yarrow.\n"); - return; - } - - /* turn the data around */ - perr = prngOutput(gPrngRef, (BYTE*) buffer, sizeof (buffer)); - - /* and scramble it some more */ - perr = prngForceReseed(gPrngRef, RESEED_TICKS); - - /* make a mutex to control access */ - gYarrowMutex = mutex_alloc(0); -} /* * Called to initialize our device, * and to register ourselves with devfs */ void -random_init() +random_init(void) { int ret; - if (gRandomInstalled) - return; - - /* install us in the file system */ - gRandomInstalled = 1; - - /* setup yarrow and the mutex */ - PreliminarySetup(); - ret = cdevsw_add(RANDOM_MAJOR, &random_cdevsw); if (ret < 0) { - printf("random_init: failed to allocate a major number!\n"); - gRandomInstalled = 0; - return; + panic("random_init: failed to allocate a major number!"); } - devfs_make_node(makedev (ret, 0), DEVFS_CHAR, + devfs_make_node(makedev (ret, RANDOM_MINOR), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, "random", 0); /* - * also make urandom + * also make urandom * (which is exactly the same thing in our context) */ - devfs_make_node(makedev (ret, 1), DEVFS_CHAR, + devfs_make_node(makedev (ret, URANDOM_MINOR), DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666, "urandom", 0); + } int -random_ioctl(dev, cmd, data, flag, p) - dev_t dev; - u_long cmd; - caddr_t data; - int flag; - struct proc *p; +random_ioctl( __unused dev_t dev, u_long cmd, __unused caddr_t data, + __unused int flag, __unused struct proc *p ) { switch (cmd) { case FIONBIO: @@ -177,15 +124,10 @@ random_ioctl(dev, cmd, data, flag, p) * Open the device. Make sure init happened, and make sure the caller is * authorized. */ - + int -random_open(dev_t dev, int flags, int devtype, struct proc *p) +random_open(__unused dev_t dev, int flags, __unused int devtype, __unused struct proc *p) { - if (gRandomError != 0) { - /* forget it, yarrow didn't come up */ - return (ENOTSUP); - } - /* * if we are being opened for write, * make sure that we have privledges do so @@ -194,7 +136,7 @@ random_open(dev_t dev, int flags, int devtype, struct proc *p) if (securelevel >= 2) return (EPERM); #ifndef __APPLE__ - if ((securelevel >= 1) && suser(p->p_ucred, &p->p_acflag)) + if ((securelevel >= 1) && proc_suser(p)) return (EPERM); #endif /* !__APPLE__ */ } @@ -206,9 +148,9 @@ random_open(dev_t dev, int flags, int devtype, struct proc *p) /* * close the device. */ - + int -random_close(dev_t dev, int flags, int mode, struct proc *p) +random_close(__unused dev_t dev, __unused int flags, __unused int mode, __unused struct proc *p) { return (0); } @@ -219,111 +161,82 @@ random_close(dev_t dev, int flags, int mode, struct proc *p) * prng. */ int -random_write (dev_t dev, struct uio *uio, int ioflag) +random_write (dev_t dev, struct uio *uio, __unused int ioflag) { int retCode = 0; char rdBuffer[256]; - if (gRandomError != 0) { - return (ENOTSUP); - } - - /* get control of the Yarrow instance, Yarrow is NOT thread safe */ - mutex_lock(gYarrowMutex); - + if (minor(dev) != RANDOM_MINOR) + return EPERM; + /* Security server is sending us entropy */ - while (uio->uio_resid > 0 && retCode == 0) { + while (uio_resid(uio) > 0 && retCode == 0) { /* get the user's data */ - int bytesToInput = min(uio->uio_resid, sizeof (rdBuffer)); + int bytesToInput = MIN(uio_resid(uio), + (user_ssize_t) sizeof(rdBuffer)); retCode = uiomove(rdBuffer, bytesToInput, uio); if (retCode != 0) - goto /*ugh*/ error_exit; - - /* put it in Yarrow */ - if (prngInput(gPrngRef, (BYTE*) rdBuffer, - sizeof (rdBuffer), SYSTEM_SOURCE, - sizeof (rdBuffer) * 8) != 0) { - retCode = EIO; - goto error_exit; - } - } - - /* force a reseed */ - if (prngForceReseed(gPrngRef, RESEED_TICKS) != 0) { - retCode = EIO; - goto error_exit; + break; + retCode = write_random(rdBuffer, bytesToInput); + if (retCode != 0) + break; } - - /* retCode should be 0 at this point */ - -error_exit: /* do this to make sure the mutex unlocks. */ - mutex_unlock(gYarrowMutex); - return (retCode); + + return retCode; } /* * return data to the caller. Results unpredictable. - */ + */ int -random_read(dev_t dev, struct uio *uio, int ioflag) +random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag) { - int retCode = 0; - char wrBuffer[512]; + int retCode = 0; + char buffer[512]; - if (gRandomError != 0) - return (ENOTSUP); + user_ssize_t bytes_remaining = uio_resid(uio); + while (bytes_remaining > 0 && retCode == 0) { + int bytesToRead = MIN(bytes_remaining, + (user_ssize_t) sizeof(buffer)); + read_random(buffer, bytesToRead); - /* lock down the mutex */ - mutex_lock(gYarrowMutex); + retCode = uiomove(buffer, bytesToRead, uio); + if (retCode != 0) + break; - while (uio->uio_resid > 0 && retCode == 0) { - /* get the user's data */ - int bytesToRead = min(uio->uio_resid, sizeof (wrBuffer)); - - /* get the data from Yarrow */ - if (prngOutput(gPrngRef, (BYTE *) wrBuffer, sizeof (wrBuffer)) != 0) { - printf ("Couldn't read data from Yarrow.\n"); - - /* something's really weird */ - retCode = EIO; - goto error_exit; - } - - retCode = uiomove(wrBuffer, bytesToRead, uio); - - if (retCode != 0) - goto error_exit; - } - - retCode = 0; - -error_exit: - mutex_unlock(gYarrowMutex); - return retCode; -} + bytes_remaining = uio_resid(uio); + } -/* export good random numbers to the rest of the kernel */ -void -read_random(void* buffer, u_int numbytes) -{ - if (gYarrowMutex == 0) { /* are we initialized? */ - PreliminarySetup (); - } - - mutex_lock(gYarrowMutex); - prngOutput(gPrngRef, (BYTE *) buffer, numbytes); - mutex_unlock(gYarrowMutex); + return retCode; } /* - * Return an unsigned long pseudo-random number. + * Return an u_int32_t pseudo-random number. */ -u_long -RandomULong() +u_int32_t +RandomULong(void) { - u_long buf; + u_int32_t buf; read_random(&buf, sizeof (buf)); return (buf); } + +int +getentropy(__unused struct proc * p, struct getentropy_args *gap, __unused int * ret) { + user_addr_t user_addr; + uint32_t user_size; + char buffer[256]; + + user_addr = (vm_map_offset_t)gap->buffer; + user_size = gap->size; + /* Can't request more than 256 random bytes + * at once. Complying with openbsd getentropy() + */ + if (user_size > sizeof(buffer)) { + return EINVAL; + } + read_random(buffer, user_size); + return copyout(buffer, user_addr, user_size); +}