]> git.sesse.net Git - vlc/blob - src/posix/rand.c
wasapi: audio capture client module (fixes #7205)
[vlc] / src / posix / rand.c
1 /*****************************************************************************
2  * rand.c : non-predictible random bytes generator
3  *****************************************************************************
4  * Copyright © 2007 Rémi Denis-Courmont
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #include <vlc_common.h>
27 #include <vlc_rand.h>
28
29 #include <stdint.h>
30 #include <string.h>
31 #include <stdlib.h>
32
33 #include <sys/types.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <pthread.h>
37 #include <vlc_fs.h>
38
39 #include <vlc_md5.h>
40
41 /*
42  * Pseudo-random number generator using a HMAC-MD5 in counter mode.
43  * Probably not very secure (expert patches welcome) but definitely
44  * better than rand() which is defined to be reproducible...
45  */
46 #define BLOCK_SIZE 64
47
48 static uint8_t okey[BLOCK_SIZE], ikey[BLOCK_SIZE];
49
50 static void vlc_rand_init (void)
51 {
52     uint8_t key[BLOCK_SIZE];
53
54     /* Get non-predictible value as key for HMAC */
55     int fd = vlc_open ("/dev/urandom", O_RDONLY);
56     if (fd == -1)
57         return; /* Uho! */
58
59     for (size_t i = 0; i < sizeof (key);)
60     {
61          ssize_t val = read (fd, key + i, sizeof (key) - i);
62          if (val > 0)
63              i += val;
64     }
65
66     /* Precompute outer and inner keys for HMAC */
67     for (size_t i = 0; i < sizeof (key); i++)
68     {
69         okey[i] = key[i] ^ 0x5c;
70         ikey[i] = key[i] ^ 0x36;
71     }
72
73     close (fd);
74 }
75
76
77 void vlc_rand_bytes (void *buf, size_t len)
78 {
79     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
80     static uint64_t counter = 0;
81
82     uint64_t stamp = NTPtime64 ();
83
84     while (len > 0)
85     {
86         uint64_t val;
87         struct md5_s mdi, mdo;
88
89         InitMD5 (&mdi);
90         InitMD5 (&mdo);
91
92         pthread_mutex_lock (&lock);
93         if (counter == 0)
94             vlc_rand_init ();
95         val = counter++;
96
97         AddMD5 (&mdi, ikey, sizeof (ikey));
98         AddMD5 (&mdo, okey, sizeof (okey));
99         pthread_mutex_unlock (&lock);
100
101         AddMD5 (&mdi, &stamp, sizeof (stamp));
102         AddMD5 (&mdi, &val, sizeof (val));
103         EndMD5 (&mdi);
104         AddMD5 (&mdo, mdi.buf, 16);
105         EndMD5 (&mdo);
106
107         if (len < 16)
108         {
109             memcpy (buf, mdo.buf, len);
110             break;
111         }
112
113         memcpy (buf, mdo.buf, 16);
114         len -= 16;
115         buf = ((uint8_t *)buf) + 16;
116     }
117 }