annotate ffmpeg/libavutil/random_seed.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public
yading@11 8 * License as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 14 * Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public
yading@11 17 * License along with FFmpeg; if not, write to the Free Software
yading@11 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20
yading@11 21 #include "config.h"
yading@11 22
yading@11 23 #if HAVE_UNISTD_H
yading@11 24 #include <unistd.h>
yading@11 25 #endif
yading@11 26 #if HAVE_CRYPTGENRANDOM
yading@11 27 #include <windows.h>
yading@11 28 #include <wincrypt.h>
yading@11 29 #endif
yading@11 30 #include <fcntl.h>
yading@11 31 #include <math.h>
yading@11 32 #include <time.h>
yading@11 33 #include <string.h>
yading@11 34 #include "avassert.h"
yading@11 35 #include "timer.h"
yading@11 36 #include "random_seed.h"
yading@11 37 #include "sha.h"
yading@11 38 #include "intreadwrite.h"
yading@11 39
yading@11 40 #ifndef TEST
yading@11 41 #define TEST 0
yading@11 42 #endif
yading@11 43
yading@11 44 static int read_random(uint32_t *dst, const char *file)
yading@11 45 {
yading@11 46 #if HAVE_UNISTD_H
yading@11 47 int fd = open(file, O_RDONLY);
yading@11 48 int err = -1;
yading@11 49
yading@11 50 if (fd == -1)
yading@11 51 return -1;
yading@11 52 err = read(fd, dst, sizeof(*dst));
yading@11 53 close(fd);
yading@11 54
yading@11 55 return err;
yading@11 56 #else
yading@11 57 return -1;
yading@11 58 #endif
yading@11 59 }
yading@11 60
yading@11 61 static uint32_t get_generic_seed(void)
yading@11 62 {
yading@11 63 uint8_t tmp[120];
yading@11 64 struct AVSHA *sha = (void*)tmp;
yading@11 65 clock_t last_t = 0;
yading@11 66 static uint64_t i = 0;
yading@11 67 static uint32_t buffer[512] = {0};
yading@11 68 unsigned char digest[20];
yading@11 69 uint64_t last_i = i;
yading@11 70
yading@11 71 av_assert0(sizeof(tmp) >= av_sha_size);
yading@11 72
yading@11 73 if(TEST){
yading@11 74 memset(buffer, 0, sizeof(buffer));
yading@11 75 last_i = i = 0;
yading@11 76 }else{
yading@11 77 #ifdef AV_READ_TIME
yading@11 78 buffer[13] ^= AV_READ_TIME();
yading@11 79 buffer[41] ^= AV_READ_TIME()>>32;
yading@11 80 #endif
yading@11 81 }
yading@11 82
yading@11 83 for (;;) {
yading@11 84 clock_t t = clock();
yading@11 85
yading@11 86 if(last_t == t){
yading@11 87 buffer[i&511]++;
yading@11 88 }else{
yading@11 89 buffer[++i&511]+= (t-last_t) % 3294638521U;
yading@11 90 if(last_i && i-last_i > 4 || i-last_i > 64 || TEST && i-last_i > 8)
yading@11 91 break;
yading@11 92 }
yading@11 93 last_t = t;
yading@11 94 }
yading@11 95
yading@11 96 if(TEST)
yading@11 97 buffer[0] = buffer[1] = 0;
yading@11 98
yading@11 99 av_sha_init(sha, 160);
yading@11 100 av_sha_update(sha, (uint8_t*)buffer, sizeof(buffer));
yading@11 101 av_sha_final(sha, digest);
yading@11 102 return AV_RB32(digest) + AV_RB32(digest+16);
yading@11 103 }
yading@11 104
yading@11 105 uint32_t av_get_random_seed(void)
yading@11 106 {
yading@11 107 uint32_t seed;
yading@11 108
yading@11 109 #if HAVE_CRYPTGENRANDOM
yading@11 110 HCRYPTPROV provider;
yading@11 111 if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
yading@11 112 CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
yading@11 113 BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
yading@11 114 CryptReleaseContext(provider, 0);
yading@11 115 if (ret)
yading@11 116 return seed;
yading@11 117 }
yading@11 118 #endif
yading@11 119
yading@11 120 if (read_random(&seed, "/dev/urandom") == sizeof(seed))
yading@11 121 return seed;
yading@11 122 if (read_random(&seed, "/dev/random") == sizeof(seed))
yading@11 123 return seed;
yading@11 124 return get_generic_seed();
yading@11 125 }
yading@11 126
yading@11 127 #if TEST
yading@11 128 #undef printf
yading@11 129 #define N 256
yading@11 130 #include <stdio.h>
yading@11 131
yading@11 132 int main(void)
yading@11 133 {
yading@11 134 int i, j, retry;
yading@11 135 uint32_t seeds[N];
yading@11 136
yading@11 137 for (retry=0; retry<3; retry++){
yading@11 138 for (i=0; i<N; i++){
yading@11 139 seeds[i] = av_get_random_seed();
yading@11 140 for (j=0; j<i; j++)
yading@11 141 if (seeds[j] == seeds[i])
yading@11 142 goto retry;
yading@11 143 }
yading@11 144 printf("seeds OK\n");
yading@11 145 return 0;
yading@11 146 retry:;
yading@11 147 }
yading@11 148 printf("FAIL at %d with %X\n", j, seeds[j]);
yading@11 149 return 1;
yading@11 150 }
yading@11 151 #endif