2 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #if HAVE_CRYPTGENRANDOM
34 #include "random_seed.h"
36 static int read_random(uint32_t *dst, const char *file)
39 int fd = open(file, O_RDONLY);
44 err = read(fd, dst, sizeof(*dst));
53 static uint32_t get_generic_seed(void)
59 float s = 0.000000000001;
61 for (i = 0; bits < 64; i++) {
63 if (last_t && fabs(t - last_t) > s || t == (clock_t) -1) {
64 if (i < 10000 && s < (1 << 24)) {
68 random = 2 * random + (i & 1);
75 random ^= AV_READ_TIME();
80 random += random >> 32;
85 uint32_t av_get_random_seed(void)
89 #if HAVE_CRYPTGENRANDOM
91 if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
92 CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
93 BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
94 CryptReleaseContext(provider, 0);
100 if (read_random(&seed, "/dev/urandom") == sizeof(seed))
102 if (read_random(&seed, "/dev/random") == sizeof(seed))
104 return get_generic_seed();