1/* 2 * Copyright (c) 1999-2009 Apple, Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/proc.h> 33#include <sys/errno.h> 34#include <sys/ioctl.h> 35#include <sys/conf.h> 36#include <sys/fcntl.h> 37#include <string.h> 38#include <miscfs/devfs/devfs.h> 39#include <kern/locks.h> 40#include <kern/clock.h> 41#include <sys/time.h> 42#include <sys/malloc.h> 43#include <sys/uio_internal.h> 44 45#include <dev/random/randomdev.h> 46 47#include <libkern/OSByteOrder.h> 48#include <libkern/OSAtomic.h> 49 50#include <mach/mach_time.h> 51 52#define RANDOM_MAJOR -1 /* let the kernel pick the device number */ 53#define RANDOM_MINOR 0 54#define URANDOM_MINOR 1 55 56d_ioctl_t random_ioctl; 57 58/* 59 * A struct describing which functions will get invoked for certain 60 * actions. 61 */ 62static struct cdevsw random_cdevsw = 63{ 64 random_open, /* open */ 65 random_close, /* close */ 66 random_read, /* read */ 67 random_write, /* write */ 68 random_ioctl, /* ioctl */ 69 (stop_fcn_t *)nulldev, /* stop */ 70 (reset_fcn_t *)nulldev, /* reset */ 71 NULL, /* tty's */ 72 eno_select, /* select */ 73 eno_mmap, /* mmap */ 74 eno_strat, /* strategy */ 75 eno_getc, /* getc */ 76 eno_putc, /* putc */ 77 0 /* type */ 78}; 79 80 81/* 82 * Called to initialize our device, 83 * and to register ourselves with devfs 84 */ 85void 86random_init(void) 87{ 88 int ret; 89 90 ret = cdevsw_add(RANDOM_MAJOR, &random_cdevsw); 91 if (ret < 0) { 92 panic("random_init: failed to allocate a major number!"); 93 } 94 95 devfs_make_node(makedev (ret, RANDOM_MINOR), DEVFS_CHAR, 96 UID_ROOT, GID_WHEEL, 0666, "random", 0); 97 98 /* 99 * also make urandom 100 * (which is exactly the same thing in our context) 101 */ 102 devfs_make_node(makedev (ret, URANDOM_MINOR), DEVFS_CHAR, 103 UID_ROOT, GID_WHEEL, 0666, "urandom", 0); 104 105} 106 107int 108random_ioctl( __unused dev_t dev, u_long cmd, __unused caddr_t data, 109 __unused int flag, __unused struct proc *p ) 110{ 111 switch (cmd) { 112 case FIONBIO: 113 case FIOASYNC: 114 break; 115 default: 116 return ENODEV; 117 } 118 119 return (0); 120} 121 122/* 123 * Open the device. Make sure init happened, and make sure the caller is 124 * authorized. 125 */ 126 127int 128random_open(__unused dev_t dev, int flags, __unused int devtype, __unused struct proc *p) 129{ 130 /* 131 * if we are being opened for write, 132 * make sure that we have privledges do so 133 */ 134 if (flags & FWRITE) { 135 if (securelevel >= 2) 136 return (EPERM); 137#ifndef __APPLE__ 138 if ((securelevel >= 1) && proc_suser(p)) 139 return (EPERM); 140#endif /* !__APPLE__ */ 141 } 142 143 return (0); 144} 145 146 147/* 148 * close the device. 149 */ 150 151int 152random_close(__unused dev_t dev, __unused int flags, __unused int mode, __unused struct proc *p) 153{ 154 return (0); 155} 156 157 158/* 159 * Get entropic data from the Security Server, and use it to reseed the 160 * prng. 161 */ 162int 163random_write (dev_t dev, struct uio *uio, __unused int ioflag) 164{ 165 int retCode = 0; 166 char rdBuffer[256]; 167 168 if (minor(dev) != RANDOM_MINOR) 169 return EPERM; 170 171 /* Security server is sending us entropy */ 172 173 while (uio_resid(uio) > 0 && retCode == 0) { 174 /* get the user's data */ 175 int bytesToInput = MIN(uio_resid(uio), 176 (user_ssize_t) sizeof(rdBuffer)); 177 retCode = uiomove(rdBuffer, bytesToInput, uio); 178 if (retCode != 0) 179 break; 180 retCode = write_random(rdBuffer, bytesToInput); 181 if (retCode != 0) 182 break; 183 } 184 185 return retCode; 186} 187 188/* 189 * return data to the caller. Results unpredictable. 190 */ 191int 192random_read(__unused dev_t dev, struct uio *uio, __unused int ioflag) 193{ 194 int retCode = 0; 195 char buffer[512]; 196 197 user_ssize_t bytes_remaining = uio_resid(uio); 198 while (bytes_remaining > 0 && retCode == 0) { 199 int bytesToRead = MIN(bytes_remaining, 200 (user_ssize_t) sizeof(buffer)); 201 read_random(buffer, bytesToRead); 202 203 retCode = uiomove(buffer, bytesToRead, uio); 204 if (retCode != 0) 205 break; 206 207 bytes_remaining = uio_resid(uio); 208 } 209 210 return retCode; 211} 212 213/* 214 * Return an u_int32_t pseudo-random number. 215 */ 216u_int32_t 217RandomULong(void) 218{ 219 u_int32_t buf; 220 read_random(&buf, sizeof (buf)); 221 return (buf); 222} 223 224