1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright (c) 2015, Joyent, Inc.
  14  */
  15 
  16 /*
  17  * getrandom system call implementation
  18  */
  19 
  20 #include <sys/types.h>
  21 #include <sys/errno.h>
  22 #include <sys/systm.h>
  23 #include <sys/random.h>
  24 #include <sys/ddi.h>
  25 #include <sys/sunddi.h>
  26 #include <sys/sysmacros.h>
  27 
  28 #include <sys/random.h>
  29 
  30 /*
  31  * Impose a maximum upper bound on the number of bytes that we'll read in one
  32  * go, ala a read of /dev/random. For /dev/urandom, we clamp it based on our
  33  * return value, because the system call returns an int, we can't handle more
  34  * than INT_MAX.
  35  */
  36 #define MAXRANDBYTES    1024
  37 #define MAXURANDBYTES   INT_MAX
  38 
  39 int
  40 getrandom(void *bufp, size_t buflen, int flags)
  41 {
  42         int out = 0;
  43         uint8_t rbytes[128];
  44         uint8_t *buf = bufp;
  45 
  46         if (flags & ~(GRND_NONBLOCK | GRND_RANDOM))
  47                 return (set_errno(EINVAL));
  48 
  49         if ((flags & GRND_RANDOM) && buflen > MAXRANDBYTES) {
  50                 buflen = MAXRANDBYTES;
  51         } else if (buflen > MAXURANDBYTES) {
  52                 buflen = MAXURANDBYTES;
  53         }
  54 
  55         while (out < buflen) {
  56                 int err;
  57                 size_t len = MIN(sizeof (rbytes), buflen - out);
  58 
  59                 if (flags & GRND_RANDOM) {
  60                         if (flags & GRND_NONBLOCK)
  61                                 err = random_get_bytes(rbytes, len);
  62                         else
  63                                 err = random_get_blocking_bytes(rbytes, len);
  64                 } else {
  65                         err = random_get_pseudo_bytes(rbytes, len);
  66                 }
  67 
  68                 if (err == 0) {
  69                         if (ddi_copyout(rbytes, buf + out, len, 0) != 0)
  70                                 return (set_errno(EFAULT));
  71                         out += len;
  72                 } else if (err == EAGAIN && out > 0) {
  73                         break;
  74                 } else {
  75                         return (set_errno(err));
  76                 }
  77         }
  78 
  79         return (out);
  80 }