1e9ac2743SConrad Meyer /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3e9ac2743SConrad Meyer * 4e9ac2743SConrad Meyer * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org> 5e9ac2743SConrad Meyer * All rights reserved. 6e9ac2743SConrad Meyer * 7e9ac2743SConrad Meyer * Redistribution and use in source and binary forms, with or without 8e9ac2743SConrad Meyer * modification, are permitted provided that the following conditions 9e9ac2743SConrad Meyer * are met: 10e9ac2743SConrad Meyer * 1. Redistributions of source code must retain the above copyright 11e9ac2743SConrad Meyer * notice, this list of conditions and the following disclaimer. 12e9ac2743SConrad Meyer * 2. Redistributions in binary form must reproduce the above copyright 13e9ac2743SConrad Meyer * notice, this list of conditions and the following disclaimer in the 14e9ac2743SConrad Meyer * documentation and/or other materials provided with the distribution. 15e9ac2743SConrad Meyer * 16e9ac2743SConrad Meyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17e9ac2743SConrad Meyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18e9ac2743SConrad Meyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19e9ac2743SConrad Meyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20e9ac2743SConrad Meyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21e9ac2743SConrad Meyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22e9ac2743SConrad Meyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23e9ac2743SConrad Meyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24e9ac2743SConrad Meyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25e9ac2743SConrad Meyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26e9ac2743SConrad Meyer * SUCH DAMAGE. 27e9ac2743SConrad Meyer */ 28e9ac2743SConrad Meyer 29e9ac2743SConrad Meyer #include <sys/param.h> 30e9ac2743SConrad Meyer #include <sys/random.h> 31e9ac2743SConrad Meyer 32e9ac2743SConrad Meyer #include <errno.h> 33*473681a1SEd Maste #include <limits.h> 340a3a36cdSConrad Meyer #include <signal.h> 3566439659SEd Maste #include <unistd.h> 36cf8e5289SKyle Evans #include <ssp/ssp.h> 37e9ac2743SConrad Meyer 38e9ac2743SConrad Meyer #include "libc_private.h" 39e9ac2743SConrad Meyer 400a3a36cdSConrad Meyer static inline void 410a3a36cdSConrad Meyer _getentropy_fail(void) 420a3a36cdSConrad Meyer { 430a3a36cdSConrad Meyer raise(SIGKILL); 440a3a36cdSConrad Meyer } 450a3a36cdSConrad Meyer 46e9ac2743SConrad Meyer int 47cf8e5289SKyle Evans __ssp_real(getentropy)(void *buf, size_t buflen) 48e9ac2743SConrad Meyer { 49e9ac2743SConrad Meyer ssize_t rd; 50e9ac2743SConrad Meyer 51*473681a1SEd Maste if (buflen > GETENTROPY_MAX) { 52*473681a1SEd Maste errno = EINVAL; 53e9ac2743SConrad Meyer return (-1); 54e9ac2743SConrad Meyer } 55e9ac2743SConrad Meyer 56e9ac2743SConrad Meyer while (buflen > 0) { 57e9ac2743SConrad Meyer rd = getrandom(buf, buflen, 0); 58e9ac2743SConrad Meyer if (rd == -1) { 5959488f25SXin LI switch (errno) { 6059488f25SXin LI case EINTR: 6159488f25SXin LI continue; 620a3a36cdSConrad Meyer case EFAULT: 63e9ac2743SConrad Meyer return (-1); 640a3a36cdSConrad Meyer default: 650a3a36cdSConrad Meyer _getentropy_fail(); 66e9ac2743SConrad Meyer } 6759488f25SXin LI } 68e9ac2743SConrad Meyer 69e9ac2743SConrad Meyer /* This cannot happen. */ 70e9ac2743SConrad Meyer if (rd == 0) 710a3a36cdSConrad Meyer _getentropy_fail(); 72e9ac2743SConrad Meyer 73e9ac2743SConrad Meyer buf = (char *)buf + rd; 74e9ac2743SConrad Meyer buflen -= rd; 75e9ac2743SConrad Meyer } 76e9ac2743SConrad Meyer 77e9ac2743SConrad Meyer return (0); 78e9ac2743SConrad Meyer } 79