]> git.sesse.net Git - vlc/blob - src/misc/rand.c
fd6faea82f64f66adacf33b60382725b53150de2
[vlc] / src / misc / 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
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20  *****************************************************************************/
21
22 #include <vlc/vlc.h>
23 #include <vlc_rand.h>
24
25 #ifndef WIN32
26 #include <stdint.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include <sys/types.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <pthread.h>
34
35 #include <vlc_md5.h>
36
37 /*
38  * Pseudo-random number generator using a HMAC-MD5 in counter mode.
39  * Probably not very secure (expert patches welcome) but definitely
40  * better than rand() which is defined to be reproducible...
41  */
42 #define BLOCK_SIZE 64
43
44 static uint8_t okey[BLOCK_SIZE], ikey[BLOCK_SIZE];
45
46 static void vlc_rand_init (void)
47 {
48 #if defined (__OpenBSD__) || defined (__OpenBSD_kernel__)
49     static const char randfile[] = "/dev/random";
50 #else
51     static const char randfile[] = "/dev/urandom";
52 #endif
53     uint8_t key[BLOCK_SIZE];
54
55     /* Get non-predictible value as key for HMAC */
56     int fd = open (randfile, O_RDONLY);
57     if (fd == -1)
58         return; /* Uho! */
59
60     for (size_t i = 0; i < sizeof (key);)
61     {
62          ssize_t val = read (fd, key + i, sizeof (key) - i);
63          if (val > 0)
64              i += val;
65     }
66
67     /* Precompute outer and inner keys for HMAC */
68     for (size_t i = 0; i < sizeof (key); i++)
69     {
70         okey[i] = key[i] ^ 0x5c;
71         ikey[i] = key[i] ^ 0x36;
72     }
73
74     close (fd);
75 }
76
77
78 void vlc_rand_bytes (void *buf, size_t len)
79 {
80     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
81     static uint64_t counter = 0;
82
83     uint64_t stamp = NTPtime64 ();
84
85     while (len > 0)
86     {
87         uint64_t val;
88         struct md5_s mdi, mdo;
89
90         pthread_mutex_lock (&lock);
91         if (counter == 0)
92             vlc_rand_init ();
93         val = counter++;
94         pthread_mutex_unlock (&lock);
95
96         InitMD5 (&mdi);
97         AddMD5 (&mdi, ikey, sizeof (ikey));
98         AddMD5 (&mdi, &stamp, sizeof (stamp));
99         AddMD5 (&mdi, &val, sizeof (val));
100         EndMD5 (&mdi);
101         InitMD5 (&mdo);
102         AddMD5 (&mdo, okey, sizeof (okey));
103         AddMD5 (&mdo, mdi.p_digest, sizeof (mdi.p_digest));
104         EndMD5 (&mdo);
105
106         if (len < sizeof (mdo.p_digest))
107         {
108             memcpy (buf, mdo.p_digest, len);
109             break;
110         }
111
112         memcpy (buf, mdo.p_digest, sizeof (mdo.p_digest));
113         len -= sizeof (mdo.p_digest);
114         buf = ((uint8_t *)buf) + sizeof (mdo.p_digest);
115     }
116 }
117
118 #else /* WIN32 */
119
120 #include <wincrypt.h>
121
122 void vlc_rand_bytes (void *buf, size_t len)
123 {
124     HCRYPTPROV hProv;
125     size_t count = len;
126     uint8_t *p_buf = (uint8_t *)buf;
127
128     /* fill buffer with pseudo-random data */
129     while (count > 0)
130     {
131         unsigned int val;
132         val = rand();
133         if (count < sizeof (val))
134         {
135             memcpy (p_buf, &val, count);
136             break;
137         }
138         
139         memcpy (p_buf, &val, sizeof (val));
140         count -= sizeof (val);
141         p_buf += sizeof (val);
142     }
143
144     /* acquire default encryption context */
145     if( CryptAcquireContext(
146         &hProv,            // Variable to hold returned handle.
147         NULL,              // Use default key container.
148         MS_DEF_PROV,       // Use default CSP.
149         PROV_RSA_FULL,     // Type of provider to acquire.
150         0) )
151     {
152         /* fill buffer with pseudo-random data, intial buffer content
153            is used as auxillary random seed */
154         CryptGenRandom(hProv, len, buf);
155         CryptReleaseContext(hProv, 0);
156     }
157 }
158 #endif