xref: /freebsd/contrib/bearssl/src/rand/sysrng.c (revision cc9e6590773dba57440750c124173ed531349a06)
10957b409SSimon J. Gerraty /*
20957b409SSimon J. Gerraty  * Copyright (c) 2017 Thomas Pornin <pornin@bolet.org>
30957b409SSimon J. Gerraty  *
40957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
50957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
60957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
70957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
80957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
90957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
100957b409SSimon J. Gerraty  * the following conditions:
110957b409SSimon J. Gerraty  *
120957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
130957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
140957b409SSimon J. Gerraty  *
150957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
160957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
170957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
180957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
190957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
200957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
210957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
220957b409SSimon J. Gerraty  * SOFTWARE.
230957b409SSimon J. Gerraty  */
240957b409SSimon J. Gerraty 
250957b409SSimon J. Gerraty #define BR_ENABLE_INTRINSICS   1
260957b409SSimon J. Gerraty #include "inner.h"
270957b409SSimon J. Gerraty 
28*cc9e6590SSimon J. Gerraty #if BR_USE_GETENTROPY
29*cc9e6590SSimon J. Gerraty #include <unistd.h>
30*cc9e6590SSimon J. Gerraty #endif
31*cc9e6590SSimon J. Gerraty 
320957b409SSimon J. Gerraty #if BR_USE_URANDOM
330957b409SSimon J. Gerraty #include <sys/types.h>
340957b409SSimon J. Gerraty #include <unistd.h>
350957b409SSimon J. Gerraty #include <fcntl.h>
360957b409SSimon J. Gerraty #include <errno.h>
370957b409SSimon J. Gerraty #endif
380957b409SSimon J. Gerraty 
390957b409SSimon J. Gerraty #if BR_USE_WIN32_RAND
400957b409SSimon J. Gerraty #include <windows.h>
410957b409SSimon J. Gerraty #include <wincrypt.h>
420957b409SSimon J. Gerraty #pragma comment(lib, "advapi32")
430957b409SSimon J. Gerraty #endif
440957b409SSimon J. Gerraty 
45*cc9e6590SSimon J. Gerraty /*
46*cc9e6590SSimon J. Gerraty  * Seeder that uses the RDRAND opcodes (on x86 CPU).
47*cc9e6590SSimon J. Gerraty  */
480957b409SSimon J. Gerraty #if BR_RDRAND
490957b409SSimon J. Gerraty BR_TARGETS_X86_UP
500957b409SSimon J. Gerraty BR_TARGET("rdrnd")
510957b409SSimon J. Gerraty static int
seeder_rdrand(const br_prng_class ** ctx)520957b409SSimon J. Gerraty seeder_rdrand(const br_prng_class **ctx)
530957b409SSimon J. Gerraty {
540957b409SSimon J. Gerraty 	unsigned char tmp[32];
550957b409SSimon J. Gerraty 	size_t u;
560957b409SSimon J. Gerraty 
570957b409SSimon J. Gerraty 	for (u = 0; u < sizeof tmp; u += sizeof(uint32_t)) {
580957b409SSimon J. Gerraty 		int j;
590957b409SSimon J. Gerraty 		uint32_t x;
600957b409SSimon J. Gerraty 
610957b409SSimon J. Gerraty 		/*
620957b409SSimon J. Gerraty 		 * We use the 32-bit intrinsic so that code is compatible
630957b409SSimon J. Gerraty 		 * with both 32-bit and 64-bit architectures.
640957b409SSimon J. Gerraty 		 *
650957b409SSimon J. Gerraty 		 * Intel recommends trying at least 10 times in case of
660957b409SSimon J. Gerraty 		 * failure.
67*cc9e6590SSimon J. Gerraty 		 *
68*cc9e6590SSimon J. Gerraty 		 * AMD bug: there are reports that some AMD processors
69*cc9e6590SSimon J. Gerraty 		 * have a bug that makes them fail silently after a
70*cc9e6590SSimon J. Gerraty 		 * suspend/resume cycle, in which case RDRAND will report
71*cc9e6590SSimon J. Gerraty 		 * a success but always return 0xFFFFFFFF.
72*cc9e6590SSimon J. Gerraty 		 * see: https://bugzilla.kernel.org/show_bug.cgi?id=85911
73*cc9e6590SSimon J. Gerraty 		 *
74*cc9e6590SSimon J. Gerraty 		 * As a mitigation, if the 32-bit value is 0 or -1, then
75*cc9e6590SSimon J. Gerraty 		 * it is considered a failure and tried again. This should
76*cc9e6590SSimon J. Gerraty 		 * reliably detect the buggy case, at least. This also
77*cc9e6590SSimon J. Gerraty 		 * implies that the selected seed values can never be
78*cc9e6590SSimon J. Gerraty 		 * 0x00000000 or 0xFFFFFFFF, which is not a problem since
79*cc9e6590SSimon J. Gerraty 		 * we are generating a seed for a PRNG, and we overdo it
80*cc9e6590SSimon J. Gerraty 		 * a bit (we generate 32 bytes of randomness, and 256 bits
81*cc9e6590SSimon J. Gerraty 		 * of entropy are really overkill).
820957b409SSimon J. Gerraty 		 */
830957b409SSimon J. Gerraty 		for (j = 0; j < 10; j ++) {
84*cc9e6590SSimon J. Gerraty 			if (_rdrand32_step(&x) && x != 0 && x != (uint32_t)-1) {
850957b409SSimon J. Gerraty 				goto next_word;
860957b409SSimon J. Gerraty 			}
870957b409SSimon J. Gerraty 		}
880957b409SSimon J. Gerraty 		return 0;
890957b409SSimon J. Gerraty 	next_word:
900957b409SSimon J. Gerraty 		br_enc32le(tmp + u, x);
910957b409SSimon J. Gerraty 	}
920957b409SSimon J. Gerraty 	(*ctx)->update(ctx, tmp, sizeof tmp);
930957b409SSimon J. Gerraty 	return 1;
940957b409SSimon J. Gerraty }
950957b409SSimon J. Gerraty BR_TARGETS_X86_DOWN
960957b409SSimon J. Gerraty 
970957b409SSimon J. Gerraty static int
rdrand_supported(void)980957b409SSimon J. Gerraty rdrand_supported(void)
990957b409SSimon J. Gerraty {
1000957b409SSimon J. Gerraty 	/*
1010957b409SSimon J. Gerraty 	 * The RDRND support is bit 30 of ECX, as returned by CPUID.
1020957b409SSimon J. Gerraty 	 */
1030957b409SSimon J. Gerraty 	return br_cpuid(0, 0, 0x40000000, 0);
1040957b409SSimon J. Gerraty }
1050957b409SSimon J. Gerraty #endif
1060957b409SSimon J. Gerraty 
107*cc9e6590SSimon J. Gerraty /*
108*cc9e6590SSimon J. Gerraty  * Seeder that uses /dev/urandom (on Unix-like systems).
109*cc9e6590SSimon J. Gerraty  */
1100957b409SSimon J. Gerraty #if BR_USE_URANDOM
1110957b409SSimon J. Gerraty static int
seeder_urandom(const br_prng_class ** ctx)1120957b409SSimon J. Gerraty seeder_urandom(const br_prng_class **ctx)
1130957b409SSimon J. Gerraty {
1140957b409SSimon J. Gerraty 	int f;
1150957b409SSimon J. Gerraty 
1160957b409SSimon J. Gerraty 	f = open("/dev/urandom", O_RDONLY);
1170957b409SSimon J. Gerraty 	if (f >= 0) {
1180957b409SSimon J. Gerraty 		unsigned char tmp[32];
1190957b409SSimon J. Gerraty 		size_t u;
1200957b409SSimon J. Gerraty 
1210957b409SSimon J. Gerraty 		for (u = 0; u < sizeof tmp;) {
1220957b409SSimon J. Gerraty 			ssize_t len;
1230957b409SSimon J. Gerraty 
1240957b409SSimon J. Gerraty 			len = read(f, tmp + u, (sizeof tmp) - u);
1250957b409SSimon J. Gerraty 			if (len < 0) {
1260957b409SSimon J. Gerraty 				if (errno == EINTR) {
1270957b409SSimon J. Gerraty 					continue;
1280957b409SSimon J. Gerraty 				}
1290957b409SSimon J. Gerraty 				break;
1300957b409SSimon J. Gerraty 			}
1310957b409SSimon J. Gerraty 			u += (size_t)len;
1320957b409SSimon J. Gerraty 		}
1330957b409SSimon J. Gerraty 		close(f);
1340957b409SSimon J. Gerraty 		if (u == sizeof tmp) {
1350957b409SSimon J. Gerraty 			(*ctx)->update(ctx, tmp, sizeof tmp);
1360957b409SSimon J. Gerraty 			return 1;
1370957b409SSimon J. Gerraty 		}
1380957b409SSimon J. Gerraty 	}
1390957b409SSimon J. Gerraty 	return 0;
1400957b409SSimon J. Gerraty }
1410957b409SSimon J. Gerraty #endif
1420957b409SSimon J. Gerraty 
143*cc9e6590SSimon J. Gerraty /*
144*cc9e6590SSimon J. Gerraty  * Seeder that uses getentropy() (backed by getrandom() on some systems,
145*cc9e6590SSimon J. Gerraty  * e.g. Linux). On failure, it will use the /dev/urandom seeder (if
146*cc9e6590SSimon J. Gerraty  * enabled).
147*cc9e6590SSimon J. Gerraty  */
148*cc9e6590SSimon J. Gerraty #if BR_USE_GETENTROPY
149*cc9e6590SSimon J. Gerraty static int
seeder_getentropy(const br_prng_class ** ctx)150*cc9e6590SSimon J. Gerraty seeder_getentropy(const br_prng_class **ctx)
151*cc9e6590SSimon J. Gerraty {
152*cc9e6590SSimon J. Gerraty 	unsigned char tmp[32];
153*cc9e6590SSimon J. Gerraty 
154*cc9e6590SSimon J. Gerraty 	if (getentropy(tmp, sizeof tmp) == 0) {
155*cc9e6590SSimon J. Gerraty 		(*ctx)->update(ctx, tmp, sizeof tmp);
156*cc9e6590SSimon J. Gerraty 		return 1;
157*cc9e6590SSimon J. Gerraty 	}
158*cc9e6590SSimon J. Gerraty #if BR_USE_URANDOM
159*cc9e6590SSimon J. Gerraty 	return seeder_urandom(ctx);
160*cc9e6590SSimon J. Gerraty #else
161*cc9e6590SSimon J. Gerraty 	return 0;
162*cc9e6590SSimon J. Gerraty #endif
163*cc9e6590SSimon J. Gerraty }
164*cc9e6590SSimon J. Gerraty #endif
165*cc9e6590SSimon J. Gerraty 
166*cc9e6590SSimon J. Gerraty /*
167*cc9e6590SSimon J. Gerraty  * Seeder that uses CryptGenRandom() (on Windows).
168*cc9e6590SSimon J. Gerraty  */
1690957b409SSimon J. Gerraty #if BR_USE_WIN32_RAND
1700957b409SSimon J. Gerraty static int
seeder_win32(const br_prng_class ** ctx)1710957b409SSimon J. Gerraty seeder_win32(const br_prng_class **ctx)
1720957b409SSimon J. Gerraty {
1730957b409SSimon J. Gerraty 	HCRYPTPROV hp;
1740957b409SSimon J. Gerraty 
1750957b409SSimon J. Gerraty 	if (CryptAcquireContext(&hp, 0, 0, PROV_RSA_FULL,
1760957b409SSimon J. Gerraty 		CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
1770957b409SSimon J. Gerraty 	{
1780957b409SSimon J. Gerraty 		BYTE buf[32];
1790957b409SSimon J. Gerraty 		BOOL r;
1800957b409SSimon J. Gerraty 
1810957b409SSimon J. Gerraty 		r = CryptGenRandom(hp, sizeof buf, buf);
1820957b409SSimon J. Gerraty 		CryptReleaseContext(hp, 0);
1830957b409SSimon J. Gerraty 		if (r) {
1840957b409SSimon J. Gerraty 			(*ctx)->update(ctx, buf, sizeof buf);
1850957b409SSimon J. Gerraty 			return 1;
1860957b409SSimon J. Gerraty 		}
1870957b409SSimon J. Gerraty 	}
1880957b409SSimon J. Gerraty 	return 0;
1890957b409SSimon J. Gerraty }
1900957b409SSimon J. Gerraty #endif
1910957b409SSimon J. Gerraty 
192*cc9e6590SSimon J. Gerraty /*
193*cc9e6590SSimon J. Gerraty  * An aggregate seeder that uses RDRAND, and falls back to an OS-provided
194*cc9e6590SSimon J. Gerraty  * source if RDRAND fails.
195*cc9e6590SSimon J. Gerraty  */
196*cc9e6590SSimon J. Gerraty #if BR_RDRAND && (BR_USE_GETENTROPY || BR_USE_URANDOM || BR_USE_WIN32_RAND)
197*cc9e6590SSimon J. Gerraty static int
seeder_rdrand_with_fallback(const br_prng_class ** ctx)198*cc9e6590SSimon J. Gerraty seeder_rdrand_with_fallback(const br_prng_class **ctx)
199*cc9e6590SSimon J. Gerraty {
200*cc9e6590SSimon J. Gerraty 	if (!seeder_rdrand(ctx)) {
201*cc9e6590SSimon J. Gerraty #if BR_USE_GETENTROPY
202*cc9e6590SSimon J. Gerraty 		return seeder_getentropy(ctx);
203*cc9e6590SSimon J. Gerraty #elif BR_USE_URANDOM
204*cc9e6590SSimon J. Gerraty 		return seeder_urandom(ctx);
205*cc9e6590SSimon J. Gerraty #elif BR_USE_WIN32_RAND
206*cc9e6590SSimon J. Gerraty 		return seeder_win32(ctx);
207*cc9e6590SSimon J. Gerraty #else
208*cc9e6590SSimon J. Gerraty #error "macro selection has gone wrong"
209*cc9e6590SSimon J. Gerraty #endif
210*cc9e6590SSimon J. Gerraty 	}
211*cc9e6590SSimon J. Gerraty 	return 1;
212*cc9e6590SSimon J. Gerraty }
213*cc9e6590SSimon J. Gerraty #endif
214*cc9e6590SSimon J. Gerraty 
2150957b409SSimon J. Gerraty /* see bearssl_rand.h */
2160957b409SSimon J. Gerraty br_prng_seeder
br_prng_seeder_system(const char ** name)2170957b409SSimon J. Gerraty br_prng_seeder_system(const char **name)
2180957b409SSimon J. Gerraty {
2190957b409SSimon J. Gerraty #if BR_RDRAND
2200957b409SSimon J. Gerraty 	if (rdrand_supported()) {
2210957b409SSimon J. Gerraty 		if (name != NULL) {
2220957b409SSimon J. Gerraty 			*name = "rdrand";
2230957b409SSimon J. Gerraty 		}
224*cc9e6590SSimon J. Gerraty #if BR_USE_GETENTROPY || BR_USE_URANDOM || BR_USE_WIN32_RAND
225*cc9e6590SSimon J. Gerraty 		return &seeder_rdrand_with_fallback;
226*cc9e6590SSimon J. Gerraty #else
2270957b409SSimon J. Gerraty 		return &seeder_rdrand;
228*cc9e6590SSimon J. Gerraty #endif
2290957b409SSimon J. Gerraty 	}
2300957b409SSimon J. Gerraty #endif
231*cc9e6590SSimon J. Gerraty #if BR_USE_GETENTROPY
232*cc9e6590SSimon J. Gerraty 	if (name != NULL) {
233*cc9e6590SSimon J. Gerraty 		*name = "getentropy";
234*cc9e6590SSimon J. Gerraty 	}
235*cc9e6590SSimon J. Gerraty 	return &seeder_getentropy;
236*cc9e6590SSimon J. Gerraty #elif BR_USE_URANDOM
2370957b409SSimon J. Gerraty 	if (name != NULL) {
2380957b409SSimon J. Gerraty 		*name = "urandom";
2390957b409SSimon J. Gerraty 	}
2400957b409SSimon J. Gerraty 	return &seeder_urandom;
2410957b409SSimon J. Gerraty #elif BR_USE_WIN32_RAND
2420957b409SSimon J. Gerraty 	if (name != NULL) {
2430957b409SSimon J. Gerraty 		*name = "win32";
2440957b409SSimon J. Gerraty 	}
2450957b409SSimon J. Gerraty 	return &seeder_win32;
2460957b409SSimon J. Gerraty #else
2470957b409SSimon J. Gerraty 	if (name != NULL) {
2480957b409SSimon J. Gerraty 		*name = "none";
2490957b409SSimon J. Gerraty 	}
2500957b409SSimon J. Gerraty 	return 0;
2510957b409SSimon J. Gerraty #endif
2520957b409SSimon J. Gerraty }
253