1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  *
  26  * Copyright 2017 Jason King.
  27  * Copyright (c) 2017 Joyent, Inc.
  28  */
  29 
  30 #include <sys/types.h>
  31 #include <sys/stat.h>
  32 #include <fcntl.h>
  33 #include <errno.h>
  34 #include <string.h>
  35 #include <locale.h>
  36 #include <ipsec_util.h>
  37 #include "random.h"
  38 #include "defs.h"
  39 
  40 /* Start off with invalid values until init */
  41 static int low_random = -1;
  42 static int high_random = -1;
  43 
  44 void
  45 random_init(void)
  46 {
  47         if ((low_random = open("/dev/urandom", 0)) == -1)
  48                 err(EXIT_FAILURE, "/dev/urandom open failed");
  49 
  50         if ((high_random = open("/dev/random", 0)) == -1)
  51                 err(EXIT_FAILURE, "/dev/random open failed");
  52 }
  53 
  54 uint64_t
  55 random_high_64(void)
  56 {
  57         uint64_t rc;
  58 
  59         random_high(&rc, sizeof (rc));
  60         return (rc);
  61 }
  62 
  63 uint64_t
  64 random_low_64(void)
  65 {
  66         uint64_t rc;
  67 
  68         random_low(&rc, sizeof (rc));
  69         return (rc);
  70 }
  71 
  72 void
  73 random_high(void *buf, size_t nbytes)
  74 {
  75         ssize_t rc;
  76 
  77         if ((rc = read(high_random, buf, nbytes)) == -1)
  78                 err(EXIT_FAILURE, "/dev/random read failed");
  79 
  80         if (rc < nbytes) {
  81                 errx(EXIT_FAILURE, "/dev/random read insufficient bytes, "
  82                     "%zd instead of %zu.", rc, nbytes);
  83         }
  84 }
  85 
  86 void
  87 random_low(void *buf, size_t nbytes)
  88 {
  89         ssize_t rc;
  90 
  91         if ((rc = read(low_random, buf, nbytes)) == -1)
  92                 err(EXIT_FAILURE, "/dev/urandom read failed");
  93 
  94         if (rc < nbytes) {
  95                 errx(EXIT_FAILURE, "/dev/urandom read insufficient bytes, "
  96                     "%zd instead of %zu.", rc, nbytes);
  97         }
  98 }
  99 
 100 extern uint32_t random_high_32(void);
 101 extern uint16_t random_high_16(void);
 102 extern uint8_t random_high_8(void);
 103 extern uint32_t random_low_32(void);
 104 extern uint16_t random_low_16(void);
 105 extern uint8_t random_low_8(void);