1 /*
2 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg 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.
10 *
11 * FFmpeg 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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "config.h"
22
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #if HAVE_IO_H
27 #include <io.h>
28 #endif
29 #if HAVE_BCRYPT
30 #include <windows.h>
31 #include <bcrypt.h>
32 #endif
33 #if CONFIG_GCRYPT
34 #include <gcrypt.h>
35 #elif CONFIG_OPENSSL
36 #include <openssl/rand.h>
37 #endif
38 #include <fcntl.h>
39 #include <math.h>
41 #include <string.h>
49
50 #ifndef TEST
52 #endif
53
55 {
56 #if HAVE_UNISTD_H
58 size_t err;
59
60 if (!fp)
62 setvbuf(fp,
NULL, _IONBF, 0);
63 err = fread(
dst, 1,
len, fp);
64 fclose(fp);
65
68
69 return 0;
70 #else
72 #endif
73 }
74
76 {
79 clock_t last_t = 0;
80 clock_t last_td = 0;
81 clock_t init_t = 0;
82 static uint64_t
i = 0;
83 static uint32_t
buffer[512] = { 0 };
84 unsigned char digest[20];
86
88
92 }else{
93 #ifdef AV_READ_TIME
96 #endif
97 }
98
99 for (;;) {
100 clock_t t = clock();
101 if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t) {
102 last_td = t - last_t;
103 buffer[
i & 511] = 1664525*
buffer[
i & 511] + 1013904223 + (last_td % 3294638521
U);
104 } else {
105 last_td = t - last_t;
106 buffer[++
i & 511] += last_td % 3294638521
U;
107 if ((t - init_t) >= CLOCKS_PER_SEC>>5)
108 if (last_i &&
i - last_i > 4 ||
i - last_i > 64 ||
TEST &&
i - last_i > 8)
109 break;
110 }
111 last_t = t;
112 if (!init_t)
113 init_t = t;
114 }
115
118 } else {
119 #ifdef AV_READ_TIME
121 #endif
122 }
123
128 }
129
131 {
132 int err;
133
134 #if HAVE_BCRYPT
135 BCRYPT_ALG_HANDLE algo_handle;
136 NTSTATUS
ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
137 MS_PRIMITIVE_PROVIDER, 0);
138 if (BCRYPT_SUCCESS(
ret)) {
139 NTSTATUS
ret = BCryptGenRandom(algo_handle, (PUCHAR)buf,
len, 0);
140 BCryptCloseAlgorithmProvider(algo_handle, 0);
141 if (BCRYPT_SUCCESS(
ret))
142 return 0;
143 }
144 #endif
145
146 #if HAVE_ARC4RANDOM_BUF
147 arc4random_buf(buf,
len);
148 return 0;
149 #endif
150
152 if (!err)
153 return err;
154
155 #if CONFIG_GCRYPT
156 gcry_randomize(buf,
len, GCRY_VERY_STRONG_RANDOM);
157 return 0;
158 #elif CONFIG_OPENSSL
159 if (RAND_bytes(buf,
len) == 1)
160 return 0;
162 #else
163 return err;
164 #endif
165 }
166
168 {
170
173
175 }