/*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2018 Conrad Meyer * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include "libc_private.h" /* * If a newer libc is accidentally installed on an older kernel, provide high * quality random data anyway. The sysctl interface is not as fast and does * not block by itself, but is provided by even very old kernels. */ static int getentropy_fallback(void *buf, size_t buflen) { /* * oldp (buf) == NULL has a special meaning for sysctl that results in * no EFAULT. For compatibility with the kernel getrandom(2), detect * this case and return the appropriate error. */ if (buf == NULL && buflen > 0) { errno = EFAULT; return (-1); } if (__arc4_sysctl(buf, buflen) != buflen) { if (errno == EFAULT) return (-1); /* * This cannot happen. _arc4_sysctl() spins until the random * device is seeded and then repeatedly reads until the full * request is satisfied. The only way for this to return a zero * byte or short read is if sysctl(2) on the kern.arandom MIB * fails. In this case, exceping the user-provided-a-bogus- * buffer EFAULT, give up (like for arc4random(3)'s arc4_stir). */ abort(); } return (0); } int getentropy(void *buf, size_t buflen) { ssize_t rd; if (buflen > 256) { errno = EIO; return (-1); } while (buflen > 0) { rd = getrandom(buf, buflen, 0); if (rd == -1) { if (errno == EINTR) continue; else if (errno == ENOSYS) return (getentropy_fallback(buf, buflen)); else return (-1); } /* This cannot happen. */ if (rd == 0) abort(); buf = (char *)buf + rd; buflen -= rd; } return (0); }