| Line | Branch | Exec | Source |
|---|---|---|---|
| 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> | ||
| 40 | #include <time.h> | ||
| 41 | #include <string.h> | ||
| 42 | #include "avassert.h" | ||
| 43 | #include "file_open.h" | ||
| 44 | #include "internal.h" | ||
| 45 | #include "intreadwrite.h" | ||
| 46 | #include "timer.h" | ||
| 47 | #include "random_seed.h" | ||
| 48 | #include "sha.h" | ||
| 49 | |||
| 50 | #ifndef TEST | ||
| 51 | #define TEST 0 | ||
| 52 | #endif | ||
| 53 | |||
| 54 | 387 | static int read_random(uint8_t *dst, size_t len, const char *file) | |
| 55 | { | ||
| 56 | #if HAVE_UNISTD_H | ||
| 57 | 387 | FILE *fp = avpriv_fopen_utf8(file, "r"); | |
| 58 | size_t err; | ||
| 59 | |||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 387 times.
|
387 | if (!fp) |
| 61 | ✗ | return AVERROR_UNKNOWN; | |
| 62 | 387 | setvbuf(fp, NULL, _IONBF, 0); | |
| 63 | 387 | err = fread(dst, 1, len, fp); | |
| 64 | 387 | fclose(fp); | |
| 65 | |||
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 387 times.
|
387 | if (err != len) |
| 67 | ✗ | return AVERROR_UNKNOWN; | |
| 68 | |||
| 69 | 387 | return 0; | |
| 70 | #else | ||
| 71 | return AVERROR(ENOSYS); | ||
| 72 | #endif | ||
| 73 | } | ||
| 74 | |||
| 75 | 256 | static uint32_t get_generic_seed(void) | |
| 76 | { | ||
| 77 | uint64_t tmp[120/8]; | ||
| 78 | 256 | struct AVSHA *sha = (void*)tmp; | |
| 79 | 256 | clock_t last_t = 0; | |
| 80 | 256 | clock_t last_td = 0; | |
| 81 | 256 | clock_t init_t = 0; | |
| 82 | static uint64_t i = 0; | ||
| 83 | static uint32_t buffer[512] = { 0 }; | ||
| 84 | unsigned char digest[20]; | ||
| 85 | 256 | uint64_t last_i = i; | |
| 86 | 256 | int repeats[3] = { 0 }; | |
| 87 | |||
| 88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 256 times.
|
256 | av_assert0(sizeof(tmp) >= av_sha_size); |
| 89 | |||
| 90 | if(TEST){ | ||
| 91 | 256 | memset(buffer, 0, sizeof(buffer)); | |
| 92 | 256 | last_i = i = 0; | |
| 93 | }else{ | ||
| 94 | #ifdef AV_READ_TIME | ||
| 95 | ✗ | buffer[13] ^= AV_READ_TIME(); | |
| 96 | ✗ | buffer[41] ^= AV_READ_TIME()>>32; | |
| 97 | #endif | ||
| 98 | } | ||
| 99 | |||
| 100 | 21486795 | for (;;) { | |
| 101 | 21487051 | clock_t t = clock(); | |
| 102 | 21487051 | int incremented_i = 0; | |
| 103 | 21487051 | int cur_td = t - last_t; | |
| 104 |
2/2✓ Branch 0 taken 2986 times.
✓ Branch 1 taken 21484065 times.
|
21487051 | if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) < t) { |
| 105 | // If the timer incremented by more than 2*last_td at once, | ||
| 106 | // we may e.g. have had a context switch. If the timer resolution | ||
| 107 | // is high (CLOCKS_PER_SEC > 1000), require that the timer | ||
| 108 | // incremented by more than 1. If the timer resolution is low, | ||
| 109 | // it is enough that the timer incremented at all. | ||
| 110 | 2986 | buffer[++i & 511] += cur_td % 3294638521U; | |
| 111 | 2986 | incremented_i = 1; | |
| 112 |
5/6✓ Branch 0 taken 21482797 times.
✓ Branch 1 taken 1268 times.
✓ Branch 2 taken 598 times.
✓ Branch 3 taken 21482199 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 598 times.
|
21484065 | } else if (t != last_t && repeats[0] > 0 && repeats[1] > 0 && |
| 113 | ✗ | repeats[2] > 0 && repeats[0] != repeats[1] && | |
| 114 | ✗ | repeats[0] != repeats[2]) { | |
| 115 | // If the timer resolution is high, and we get the same timer | ||
| 116 | // value multiple times, use variances in the number of repeats | ||
| 117 | // of each timer value as entropy. If we get a different number of | ||
| 118 | // repeats than the last two unique cases, count that as entropy | ||
| 119 | // and proceed to the next index. | ||
| 120 | ✗ | buffer[++i & 511] += (repeats[0] + repeats[1] + repeats[2]) % 3294638521U; | |
| 121 | ✗ | incremented_i = 1; | |
| 122 | } else { | ||
| 123 | 21484065 | buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (cur_td % 3294638521U); | |
| 124 | } | ||
| 125 |
4/4✓ Branch 0 taken 2986 times.
✓ Branch 1 taken 21484065 times.
✓ Branch 2 taken 1378 times.
✓ Branch 3 taken 1608 times.
|
21487051 | if (incremented_i && (t - init_t) >= CLOCKS_PER_SEC>>5) { |
| 126 |
5/8✗ Branch 0 not taken.
✓ Branch 1 taken 1378 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1375 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1122 times.
✓ Branch 7 taken 253 times.
|
1378 | if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8) |
| 127 | break; | ||
| 128 | } | ||
| 129 |
2/2✓ Branch 0 taken 1268 times.
✓ Branch 1 taken 21485527 times.
|
21486795 | if (t == last_t) { |
| 130 | 1268 | repeats[0]++; | |
| 131 | } else { | ||
| 132 | // If we got a new unique number of repeats, update the history. | ||
| 133 |
2/2✓ Branch 0 taken 1934 times.
✓ Branch 1 taken 21483593 times.
|
21485527 | if (repeats[0] != repeats[1]) { |
| 134 | 1934 | repeats[2] = repeats[1]; | |
| 135 | 1934 | repeats[1] = repeats[0]; | |
| 136 | } | ||
| 137 | 21485527 | repeats[0] = 0; | |
| 138 | } | ||
| 139 | 21486795 | last_t = t; | |
| 140 | 21486795 | last_td = cur_td; | |
| 141 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 21486539 times.
|
21486795 | if (!init_t) |
| 142 | 256 | init_t = t; | |
| 143 | } | ||
| 144 | |||
| 145 | if(TEST) { | ||
| 146 | 256 | buffer[0] = buffer[1] = 0; | |
| 147 | } else { | ||
| 148 | #ifdef AV_READ_TIME | ||
| 149 | ✗ | buffer[111] += AV_READ_TIME(); | |
| 150 | #endif | ||
| 151 | } | ||
| 152 | |||
| 153 | 256 | av_sha_init(sha, 160); | |
| 154 | 256 | av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer)); | |
| 155 | 256 | av_sha_final(sha, digest); | |
| 156 | 256 | return AV_RB32(digest) + AV_RB32(digest + 16); | |
| 157 | } | ||
| 158 | |||
| 159 | 387 | int av_random_bytes(uint8_t* buf, size_t len) | |
| 160 | { | ||
| 161 | int err; | ||
| 162 | |||
| 163 | #if HAVE_BCRYPT | ||
| 164 | BCRYPT_ALG_HANDLE algo_handle; | ||
| 165 | NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM, | ||
| 166 | MS_PRIMITIVE_PROVIDER, 0); | ||
| 167 | if (BCRYPT_SUCCESS(ret)) { | ||
| 168 | NTSTATUS ret = BCryptGenRandom(algo_handle, (PUCHAR)buf, len, 0); | ||
| 169 | BCryptCloseAlgorithmProvider(algo_handle, 0); | ||
| 170 | if (BCRYPT_SUCCESS(ret)) | ||
| 171 | return 0; | ||
| 172 | } | ||
| 173 | #endif | ||
| 174 | |||
| 175 | #if HAVE_ARC4RANDOM_BUF | ||
| 176 | arc4random_buf(buf, len); | ||
| 177 | return 0; | ||
| 178 | #endif | ||
| 179 | |||
| 180 | 387 | err = read_random(buf, len, "/dev/urandom"); | |
| 181 |
1/2✓ Branch 0 taken 387 times.
✗ Branch 1 not taken.
|
387 | if (!err) |
| 182 | 387 | return err; | |
| 183 | |||
| 184 | #if CONFIG_GCRYPT | ||
| 185 | gcry_randomize(buf, len, GCRY_VERY_STRONG_RANDOM); | ||
| 186 | return 0; | ||
| 187 | #elif CONFIG_OPENSSL | ||
| 188 | if (RAND_bytes(buf, len) == 1) | ||
| 189 | return 0; | ||
| 190 | return AVERROR_EXTERNAL; | ||
| 191 | #else | ||
| 192 | ✗ | return err; | |
| 193 | #endif | ||
| 194 | } | ||
| 195 | |||
| 196 | 387 | uint32_t av_get_random_seed(void) | |
| 197 | { | ||
| 198 | uint32_t seed; | ||
| 199 | |||
| 200 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 387 times.
|
387 | if (av_random_bytes((uint8_t *)&seed, sizeof(seed)) < 0) |
| 201 | ✗ | return get_generic_seed(); | |
| 202 | |||
| 203 | 387 | return seed; | |
| 204 | } | ||
| 205 |