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 #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 static int read_random(uint8_t *dst, size_t len, const char *file)
55 {
56 #if HAVE_UNISTD_H
57  FILE *fp = avpriv_fopen_utf8(file, "r");
58  size_t err;
59 
60  if (!fp)
61  return AVERROR_UNKNOWN;
62  setvbuf(fp, NULL, _IONBF, 0);
63  err = fread(dst, 1, len, fp);
64  fclose(fp);
65 
66  if (err != len)
67  return AVERROR_UNKNOWN;
68 
69  return 0;
70 #else
71  return AVERROR(ENOSYS);
72 #endif
73 }
74 
75 static uint32_t get_generic_seed(void)
76 {
77  uint64_t tmp[120/8];
78  struct AVSHA *sha = (void*)tmp;
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];
85  uint64_t last_i = i;
86 
87  av_assert0(sizeof(tmp) >= av_sha_size);
88 
89  if(TEST){
90  memset(buffer, 0, sizeof(buffer));
91  last_i = i = 0;
92  }else{
93 #ifdef AV_READ_TIME
94  buffer[13] ^= AV_READ_TIME();
95  buffer[41] ^= AV_READ_TIME()>>32;
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 % 3294638521U);
104  } else {
105  last_td = t - last_t;
106  buffer[++i & 511] += last_td % 3294638521U;
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 
116  if(TEST) {
117  buffer[0] = buffer[1] = 0;
118  } else {
119 #ifdef AV_READ_TIME
120  buffer[111] += AV_READ_TIME();
121 #endif
122  }
123 
124  av_sha_init(sha, 160);
125  av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer));
126  av_sha_final(sha, digest);
127  return AV_RB32(digest) + AV_RB32(digest + 16);
128 }
129 
130 int av_random_bytes(uint8_t* buf, size_t len)
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 
151  err = read_random(buf, len, "/dev/urandom");
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;
161  err = AVERROR_EXTERNAL;
162 #endif
163 
164  return err;
165 }
166 
167 uint32_t av_get_random_seed(void)
168 {
169  uint32_t seed;
170 
171  if (av_random_bytes((uint8_t *)&seed, sizeof(seed)) < 0)
172  return get_generic_seed();
173 
174  return seed;
175 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
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:51
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
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:167
avassert.h
intreadwrite.h
AV_READ_TIME
#define AV_READ_TIME
Definition: timer.h:28
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
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
NULL
#define NULL
Definition: coverity.c:32
sha.h
timer.h
time.h
fp
#define fp
Definition: regdef.h:44
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
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
internal.h
av_random_bytes
int av_random_bytes(uint8_t *buf, size_t len)
Generate cryptographically secure random data, i.e.
Definition: random_seed.c:130
len
int len
Definition: vorbis_enc_data.h:426
avpriv_fopen_utf8
FILE * avpriv_fopen_utf8(const char *path, const char *mode)
Open a file using a UTF-8 filename.
Definition: file_open.c:159
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:75
read_random
static int read_random(uint8_t *dst, size_t len, const char *file)
Definition: random_seed.c:54