Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 1 | /* |
2 | * Copyright (c) 2009 Baptiste Coudurier |
||
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 | #define HAVE_CRYPTGENRANDOM 0 |
||
24 | |||
25 | #if HAVE_UNISTD_H |
||
26 | #include |
||
27 | #endif |
||
28 | #if HAVE_CRYPTGENRANDOM |
||
29 | #include |
||
30 | #include |
||
31 | #endif |
||
32 | #include |
||
33 | #include |
||
34 | #include |
||
35 | #include |
||
36 | #include "avassert.h" |
||
37 | #include "internal.h" |
||
38 | #include "timer.h" |
||
39 | #include "random_seed.h" |
||
40 | #include "sha.h" |
||
41 | #include "intreadwrite.h" |
||
42 | |||
43 | #ifndef TEST |
||
44 | #define TEST 0 |
||
45 | #endif |
||
46 | |||
47 | static int read_random(uint32_t *dst, const char *file) |
||
48 | { |
||
49 | #if HAVE_UNISTD_H |
||
50 | int fd = avpriv_open(file, O_RDONLY); |
||
51 | int err = -1; |
||
52 | |||
53 | if (fd == -1) |
||
54 | return -1; |
||
55 | err = read(fd, dst, sizeof(*dst)); |
||
56 | close(fd); |
||
57 | |||
58 | return err; |
||
59 | #else |
||
60 | return -1; |
||
61 | #endif |
||
62 | } |
||
63 | |||
64 | static uint32_t get_generic_seed(void) |
||
65 | { |
||
66 | uint8_t tmp[120]; |
||
67 | struct AVSHA *sha = (void*)tmp; |
||
68 | clock_t last_t = 0; |
||
69 | static uint64_t i = 0; |
||
70 | static uint32_t buffer[512] = {0}; |
||
71 | unsigned char digest[20]; |
||
72 | uint64_t last_i = i; |
||
73 | |||
74 | av_assert0(sizeof(tmp) >= av_sha_size); |
||
75 | |||
76 | if(TEST){ |
||
77 | memset(buffer, 0, sizeof(buffer)); |
||
78 | last_i = i = 0; |
||
79 | }else{ |
||
80 | #ifdef AV_READ_TIME |
||
81 | buffer[13] ^= AV_READ_TIME(); |
||
82 | buffer[41] ^= AV_READ_TIME()>>32; |
||
83 | #endif |
||
84 | } |
||
85 | |||
86 | for (;;) { |
||
87 | clock_t t = clock(); |
||
88 | |||
89 | if(last_t == t){ |
||
90 | buffer[i&511]++; |
||
91 | }else{ |
||
92 | buffer[++i&511]+= (t-last_t) % 3294638521U; |
||
93 | if(last_i && i-last_i > 4 || i-last_i > 64 || TEST && i-last_i > 8) |
||
94 | break; |
||
95 | } |
||
96 | last_t = t; |
||
97 | } |
||
98 | |||
99 | if(TEST) |
||
100 | buffer[0] = buffer[1] = 0; |
||
101 | |||
102 | av_sha_init(sha, 160); |
||
103 | av_sha_update(sha, (uint8_t*)buffer, sizeof(buffer)); |
||
104 | av_sha_final(sha, digest); |
||
105 | return AV_RB32(digest) + AV_RB32(digest+16); |
||
106 | } |
||
107 | |||
108 | uint32_t av_get_random_seed(void) |
||
109 | { |
||
110 | uint32_t seed; |
||
111 | |||
112 | #if HAVE_CRYPTGENRANDOM |
||
113 | HCRYPTPROV provider; |
||
114 | if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, |
||
115 | CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) { |
||
116 | BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed); |
||
117 | CryptReleaseContext(provider, 0); |
||
118 | if (ret) |
||
119 | return seed; |
||
120 | } |
||
121 | #endif |
||
122 | |||
123 | if (read_random(&seed, "/dev/urandom") == sizeof(seed)) |
||
124 | return seed; |
||
125 | if (read_random(&seed, "/dev/random") == sizeof(seed)) |
||
126 | return seed; |
||
127 | return get_generic_seed(); |
||
128 | } |
||
129 | |||
130 | #if TEST |
||
131 | #undef printf |
||
132 | #define N 256 |
||
133 | #include |
||
134 | |||
135 | int main(void) |
||
136 | { |
||
137 | int i, j, retry; |
||
138 | uint32_t seeds[N]; |
||
139 | |||
140 | for (retry=0; retry<3; retry++){ |
||
141 | for (i=0; i |
||
142 | seeds[i] = av_get_random_seed(); |
||
143 | for (j=0; j |
||
144 | if (seeds[j] == seeds[i]) |
||
145 | goto retry; |
||
146 | } |
||
147 | printf("seeds OK\n"); |
||
148 | return 0; |
||
149 | retry:; |
||
150 | } |
||
151 | printf("FAIL at %d with %X\n", j, seeds[j]); |
||
152 | return 1; |
||
153 | } |
||
154 | #endif3;> |