xref: /freebsd/lib/libc/gen/getentropy.c (revision 66439659982c00f66d0fe002820eaa6610f69ff1)
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>
330a3a36cdSConrad Meyer #include <signal.h>
34*66439659SEd Maste #include <unistd.h>
35cf8e5289SKyle Evans #include <ssp/ssp.h>
36e9ac2743SConrad Meyer 
37e9ac2743SConrad Meyer #include "libc_private.h"
38e9ac2743SConrad Meyer 
390a3a36cdSConrad Meyer static inline void
400a3a36cdSConrad Meyer _getentropy_fail(void)
410a3a36cdSConrad Meyer {
420a3a36cdSConrad Meyer 	raise(SIGKILL);
430a3a36cdSConrad Meyer }
440a3a36cdSConrad Meyer 
45e9ac2743SConrad Meyer int
46cf8e5289SKyle Evans __ssp_real(getentropy)(void *buf, size_t buflen)
47e9ac2743SConrad Meyer {
48e9ac2743SConrad Meyer 	ssize_t rd;
49e9ac2743SConrad Meyer 
50e9ac2743SConrad Meyer 	if (buflen > 256) {
51e9ac2743SConrad Meyer 		errno = EIO;
52e9ac2743SConrad Meyer 		return (-1);
53e9ac2743SConrad Meyer 	}
54e9ac2743SConrad Meyer 
55e9ac2743SConrad Meyer 	while (buflen > 0) {
56e9ac2743SConrad Meyer 		rd = getrandom(buf, buflen, 0);
57e9ac2743SConrad Meyer 		if (rd == -1) {
5859488f25SXin LI 			switch (errno) {
5959488f25SXin LI 			case EINTR:
6059488f25SXin LI 				continue;
610a3a36cdSConrad Meyer 			case EFAULT:
62e9ac2743SConrad Meyer 				return (-1);
630a3a36cdSConrad Meyer 			default:
640a3a36cdSConrad Meyer 				_getentropy_fail();
65e9ac2743SConrad Meyer 			}
6659488f25SXin LI 		}
67e9ac2743SConrad Meyer 
68e9ac2743SConrad Meyer 		/* This cannot happen. */
69e9ac2743SConrad Meyer 		if (rd == 0)
700a3a36cdSConrad Meyer 			_getentropy_fail();
71e9ac2743SConrad Meyer 
72e9ac2743SConrad Meyer 		buf = (char *)buf + rd;
73e9ac2743SConrad Meyer 		buflen -= rd;
74e9ac2743SConrad Meyer 	}
75e9ac2743SConrad Meyer 
76e9ac2743SConrad Meyer 	return (0);
77e9ac2743SConrad Meyer }
78