xref: /dflybsd-src/crypto/openssh/entropy.c (revision ba1276acd1c8c22d225b1bcf370a14c878644f44)
118de8d7fSPeter Avalos /*
218de8d7fSPeter Avalos  * Copyright (c) 2001 Damien Miller.  All rights reserved.
318de8d7fSPeter Avalos  *
418de8d7fSPeter Avalos  * Redistribution and use in source and binary forms, with or without
518de8d7fSPeter Avalos  * modification, are permitted provided that the following conditions
618de8d7fSPeter Avalos  * are met:
718de8d7fSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
818de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
918de8d7fSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
1018de8d7fSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
1118de8d7fSPeter Avalos  *    documentation and/or other materials provided with the distribution.
1218de8d7fSPeter Avalos  *
1318de8d7fSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1418de8d7fSPeter Avalos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1518de8d7fSPeter Avalos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1618de8d7fSPeter Avalos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1718de8d7fSPeter Avalos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1818de8d7fSPeter Avalos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1918de8d7fSPeter Avalos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2018de8d7fSPeter Avalos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2118de8d7fSPeter Avalos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2218de8d7fSPeter Avalos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2318de8d7fSPeter Avalos  */
2418de8d7fSPeter Avalos 
2518de8d7fSPeter Avalos #include "includes.h"
2618de8d7fSPeter Avalos 
27664f4763Szrj #define RANDOM_SEED_SIZE 48
28664f4763Szrj 
29e9778795SPeter Avalos #ifdef WITH_OPENSSL
30e9778795SPeter Avalos 
3118de8d7fSPeter Avalos #include <sys/types.h>
321c188a7fSPeter Avalos 
331c188a7fSPeter Avalos #include <errno.h>
3418de8d7fSPeter Avalos #include <signal.h>
35*0cbfa66cSDaniel Fojt #include <stdlib.h>
361c188a7fSPeter Avalos #include <string.h>
3718de8d7fSPeter Avalos #include <unistd.h>
3818de8d7fSPeter Avalos 
3918de8d7fSPeter Avalos #include <openssl/rand.h>
4018de8d7fSPeter Avalos #include <openssl/crypto.h>
4118de8d7fSPeter Avalos #include <openssl/err.h>
4218de8d7fSPeter Avalos 
4336e94dc5SPeter Avalos #include "openbsd-compat/openssl-compat.h"
4436e94dc5SPeter Avalos 
4518de8d7fSPeter Avalos #include "ssh.h"
4618de8d7fSPeter Avalos #include "misc.h"
4718de8d7fSPeter Avalos #include "xmalloc.h"
4818de8d7fSPeter Avalos #include "atomicio.h"
4918de8d7fSPeter Avalos #include "pathnames.h"
5018de8d7fSPeter Avalos #include "log.h"
51664f4763Szrj #include "sshbuf.h"
52664f4763Szrj #include "ssherr.h"
5318de8d7fSPeter Avalos 
5418de8d7fSPeter Avalos /*
5518de8d7fSPeter Avalos  * Portable OpenSSH PRNG seeding:
5618de8d7fSPeter Avalos  * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
571c188a7fSPeter Avalos  * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
581c188a7fSPeter Avalos  * PRNGd.
5918de8d7fSPeter Avalos  */
601c188a7fSPeter Avalos 
611c188a7fSPeter Avalos void
seed_rng(void)621c188a7fSPeter Avalos seed_rng(void)
631c188a7fSPeter Avalos {
641c188a7fSPeter Avalos 	unsigned char buf[RANDOM_SEED_SIZE];
65664f4763Szrj 
66664f4763Szrj 	/* Initialise libcrypto */
67664f4763Szrj 	ssh_libcrypto_init();
68664f4763Szrj 
69664f4763Szrj 	if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER,
70664f4763Szrj 	    OpenSSL_version_num()))
711c188a7fSPeter Avalos 		fatal("OpenSSL version mismatch. Built against %lx, you "
72664f4763Szrj 		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER,
73664f4763Szrj 		    OpenSSL_version_num());
741c188a7fSPeter Avalos 
751c188a7fSPeter Avalos #ifndef OPENSSL_PRNG_ONLY
76664f4763Szrj 	if (RAND_status() == 1)
771c188a7fSPeter Avalos 		debug3("RNG is ready, skipping seeding");
78664f4763Szrj 	else {
791c188a7fSPeter Avalos 		if (seed_from_prngd(buf, sizeof(buf)) == -1)
801c188a7fSPeter Avalos 			fatal("Could not obtain seed from PRNGd");
811c188a7fSPeter Avalos 		RAND_add(buf, sizeof(buf), sizeof(buf));
82664f4763Szrj 	}
831c188a7fSPeter Avalos #endif /* OPENSSL_PRNG_ONLY */
84664f4763Szrj 
851c188a7fSPeter Avalos 	if (RAND_status() != 1)
861c188a7fSPeter Avalos 		fatal("PRNG is not seeded");
87664f4763Szrj 
88664f4763Szrj 	/* Ensure arc4random() is primed */
89664f4763Szrj 	arc4random_buf(buf, sizeof(buf));
90664f4763Szrj 	explicit_bzero(buf, sizeof(buf));
911c188a7fSPeter Avalos }
92e9778795SPeter Avalos 
93e9778795SPeter Avalos #else /* WITH_OPENSSL */
94e9778795SPeter Avalos 
95*0cbfa66cSDaniel Fojt #include <stdlib.h>
96*0cbfa66cSDaniel Fojt #include <string.h>
97*0cbfa66cSDaniel Fojt 
98*0cbfa66cSDaniel Fojt /* Actual initialisation is handled in arc4random() */
99e9778795SPeter Avalos void
seed_rng(void)100e9778795SPeter Avalos seed_rng(void)
101e9778795SPeter Avalos {
102664f4763Szrj 	unsigned char buf[RANDOM_SEED_SIZE];
103664f4763Szrj 
104664f4763Szrj 	/* Ensure arc4random() is primed */
105664f4763Szrj 	arc4random_buf(buf, sizeof(buf));
106664f4763Szrj 	explicit_bzero(buf, sizeof(buf));
107e9778795SPeter Avalos }
108e9778795SPeter Avalos 
109e9778795SPeter Avalos #endif /* WITH_OPENSSL */
110