10Sstevel@tonic-gate/* $NetBSD: rumpuser_random.c,v 1.3 2014/08/24 14:37:31 pooka Exp $ */ 20Sstevel@tonic-gate 30Sstevel@tonic-gate/* 40Sstevel@tonic-gate * Copyright (c) 2014 Justin Cormack. All Rights Reserved. 50Sstevel@tonic-gate * 60Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 70Sstevel@tonic-gate * modification, are permitted provided that the following conditions 80Sstevel@tonic-gate * are met: 90Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 110Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 120Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 130Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 140Sstevel@tonic-gate * 150Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 160Sstevel@tonic-gate * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 170Sstevel@tonic-gate * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 180Sstevel@tonic-gate * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 190Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 200Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 210Sstevel@tonic-gate * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 220Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 230Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 240Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 250Sstevel@tonic-gate * SUCH DAMAGE. 260Sstevel@tonic-gate */ 270Sstevel@tonic-gate 280Sstevel@tonic-gate#include "rumpuser_port.h" 290Sstevel@tonic-gate 300Sstevel@tonic-gate#if !defined(lint) 310Sstevel@tonic-gate__RCSID("$NetBSD: rumpuser_random.c,v 1.3 2014/08/24 14:37:31 pooka Exp $"); 320Sstevel@tonic-gate#endif /* !lint */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate#include <sys/types.h> 350Sstevel@tonic-gate 360Sstevel@tonic-gate#include <assert.h> 370Sstevel@tonic-gate#include <errno.h> 380Sstevel@tonic-gate#include <fcntl.h> 390Sstevel@tonic-gate#include <stdint.h> 400Sstevel@tonic-gate#include <stdio.h> 410Sstevel@tonic-gate#include <stdlib.h> 420Sstevel@tonic-gate#include <string.h> 430Sstevel@tonic-gate#include <unistd.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate#include <rump/rumpuser.h> 460Sstevel@tonic-gate 470Sstevel@tonic-gate#include "rumpuser_int.h" 480Sstevel@tonic-gate 490Sstevel@tonic-gatestatic const size_t random_maxread = 32; 500Sstevel@tonic-gate 510Sstevel@tonic-gate#ifdef HAVE_ARC4RANDOM_BUF 520Sstevel@tonic-gateint 530Sstevel@tonic-gaterumpuser__random_init(void) 540Sstevel@tonic-gate{ 550Sstevel@tonic-gate 560Sstevel@tonic-gate return 0; 570Sstevel@tonic-gate} 580Sstevel@tonic-gate#else 590Sstevel@tonic-gatestatic const char *random_device = "/dev/urandom"; 600Sstevel@tonic-gatestatic int random_fd = -1; 610Sstevel@tonic-gate 620Sstevel@tonic-gateint 630Sstevel@tonic-gaterumpuser__random_init(void) 640Sstevel@tonic-gate{ 650Sstevel@tonic-gate 660Sstevel@tonic-gate random_fd = open(random_device, O_RDONLY); 670Sstevel@tonic-gate if (random_fd < 0) { 680Sstevel@tonic-gate fprintf(stderr, "random init open failed\n"); 690Sstevel@tonic-gate return errno; 700Sstevel@tonic-gate } 710Sstevel@tonic-gate return 0; 720Sstevel@tonic-gate} 730Sstevel@tonic-gate#endif 740Sstevel@tonic-gate 750Sstevel@tonic-gateint 760Sstevel@tonic-gaterumpuser_getrandom(void *buf, size_t buflen, int flags, size_t *retp) 770Sstevel@tonic-gate{ 780Sstevel@tonic-gate#ifndef HAVE_ARC4RANDOM_BUF 790Sstevel@tonic-gate ssize_t rv; 800Sstevel@tonic-gate 810Sstevel@tonic-gate rv = read(random_fd, buf, buflen > random_maxread ? random_maxread : buflen); 820Sstevel@tonic-gate if (rv < 0) { 830Sstevel@tonic-gate ET(errno); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate *retp = rv; 860Sstevel@tonic-gate#else 870Sstevel@tonic-gate buflen = buflen > random_maxread ? random_maxread : buflen; 880Sstevel@tonic-gate arc4random_buf(buf, buflen); 890Sstevel@tonic-gate *retp = buflen; 900Sstevel@tonic-gate#endif 910Sstevel@tonic-gate 920Sstevel@tonic-gate return 0; 930Sstevel@tonic-gate} 940Sstevel@tonic-gate