FFmpeg
random_seed.c
Go to the documentation of this file.
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 #include <fcntl.h>
34 #include <math.h>
35 #include <time.h>
36 #include <string.h>
37 #include "avassert.h"
38 #include "file_open.h"
39 #include "internal.h"
40 #include "intreadwrite.h"
41 #include "timer.h"
42 #include "random_seed.h"
43 #include "sha.h"
44 
45 #ifndef TEST
46 #define TEST 0
47 #endif
48 
49 static int read_random(uint32_t *dst, const char *file)
50 {
51 #if HAVE_UNISTD_H
52  int fd = avpriv_open(file, O_RDONLY);
53  int err = -1;
54 
55  if (fd == -1)
56  return -1;
57  err = read(fd, dst, sizeof(*dst));
58  close(fd);
59 
60  return err;
61 #else
62  return -1;
63 #endif
64 }
65 
66 static uint32_t get_generic_seed(void)
67 {
68  uint64_t tmp[120/8];
69  struct AVSHA *sha = (void*)tmp;
70  clock_t last_t = 0;
71  clock_t last_td = 0;
72  clock_t init_t = 0;
73  static uint64_t i = 0;
74  static uint32_t buffer[512] = { 0 };
75  unsigned char digest[20];
76  uint64_t last_i = i;
77 
78  av_assert0(sizeof(tmp) >= av_sha_size);
79 
80  if(TEST){
81  memset(buffer, 0, sizeof(buffer));
82  last_i = i = 0;
83  }else{
84 #ifdef AV_READ_TIME
85  buffer[13] ^= AV_READ_TIME();
86  buffer[41] ^= AV_READ_TIME()>>32;
87 #endif
88  }
89 
90  for (;;) {
91  clock_t t = clock();
92  if (last_t + 2*last_td + (CLOCKS_PER_SEC > 1000) >= t) {
93  last_td = t - last_t;
94  buffer[i & 511] = 1664525*buffer[i & 511] + 1013904223 + (last_td % 3294638521U);
95  } else {
96  last_td = t - last_t;
97  buffer[++i & 511] += last_td % 3294638521U;
98  if ((t - init_t) >= CLOCKS_PER_SEC>>5)
99  if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8)
100  break;
101  }
102  last_t = t;
103  if (!init_t)
104  init_t = t;
105  }
106 
107  if(TEST) {
108  buffer[0] = buffer[1] = 0;
109  } else {
110 #ifdef AV_READ_TIME
111  buffer[111] += AV_READ_TIME();
112 #endif
113  }
114 
115  av_sha_init(sha, 160);
116  av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer));
117  av_sha_final(sha, digest);
118  return AV_RB32(digest) + AV_RB32(digest + 16);
119 }
120 
121 uint32_t av_get_random_seed(void)
122 {
123  uint32_t seed;
124 
125 #if HAVE_BCRYPT
126  BCRYPT_ALG_HANDLE algo_handle;
127  NTSTATUS ret = BCryptOpenAlgorithmProvider(&algo_handle, BCRYPT_RNG_ALGORITHM,
128  MS_PRIMITIVE_PROVIDER, 0);
129  if (BCRYPT_SUCCESS(ret)) {
130  NTSTATUS ret = BCryptGenRandom(algo_handle, (UCHAR*)&seed, sizeof(seed), 0);
131  BCryptCloseAlgorithmProvider(algo_handle, 0);
132  if (BCRYPT_SUCCESS(ret))
133  return seed;
134  }
135 #endif
136 
137 #if HAVE_ARC4RANDOM
138  return arc4random();
139 #endif
140 
141  if (read_random(&seed, "/dev/urandom") == sizeof(seed))
142  return seed;
143  if (read_random(&seed, "/dev/random") == sizeof(seed))
144  return seed;
145  return get_generic_seed();
146 }
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
av_sha_init
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:274
TEST
#define TEST
Definition: random_seed.c:46
av_get_random_seed
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
Definition: random_seed.c:121
avassert.h
intreadwrite.h
avpriv_open
int avpriv_open(const char *filename, int flags,...)
A wrapper for open() setting O_CLOEXEC.
Definition: file_open.c:67
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
file_open.h
av_sha_final
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:347
av_sha_size
const int av_sha_size
Definition: sha.c:44
sha.h
timer.h
time.h
seed
static unsigned int seed
Definition: videogen.c:78
av_sha_update
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, size_t len)
Update hash value.
Definition: sha.c:315
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
AV_READ_TIME
#define AV_READ_TIME
Definition: timer.h:26
read_random
static int read_random(uint32_t *dst, const char *file)
Definition: random_seed.c:49
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
AVSHA
hash context
Definition: sha.c:35
ret
ret
Definition: filter_design.txt:187
U
#define U(x)
Definition: vpx_arith.h:37
random_seed.h
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
get_generic_seed
static uint32_t get_generic_seed(void)
Definition: random_seed.c:66
read
static uint32_t BS_FUNC() read(BSCTX *bc, unsigned int n)
Return n bits from the buffer, n has to be in the 0-32 range.
Definition: bitstream_template.h:231