Skip to content

Commit 3b3e120

Browse files
committed
Use srandomdev() if it exists for more randomness. Also, fix clobbering of already defined variable. Fixes issue #1.
1 parent e9c5d89 commit 3b3e120

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

mod_cspnonce.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
# pragma comment(lib, "Bcrypt")
3838
#else
3939
# include <stdlib.h>
40-
# include <time.h>
40+
# ifndef __APPLE__
41+
# include <time.h>
42+
# endif
4143
#endif
4244

4345
typedef unsigned char byte;
@@ -80,28 +82,32 @@ const char * GenSecureCSPNonce(const request_rec * r)
8082
// depending on the system. With modern kernels this
8183
// will be true.
8284
// https://man7.org/linux/man-pages/man3/random.3.html
83-
int r;
85+
int h;
8486

87+
// Seed the PRNG
88+
# ifdef __APPLE__
89+
srandomdev();
90+
# else
8591
struct timespec ts;
8692
if (timespec_get(&ts, TIME_UTC) == 0)
8793
return NULL;
8894

89-
// Seed the PRNG
9095
srandom(ts.tv_nsec ^ ts.tv_sec);
96+
# endif
9197

9298
// Generate a random integer
9399
// fill up bytes 0,1,2,3
94-
r = random();
95-
memcpy(random_bytes, &r, 4);
100+
h = random();
101+
memcpy(random_bytes, &h, 4);
96102

97103
// fill up bytes 4,5,6,7
98-
r = random();
99-
memcpy(random_bytes + 4, &r, 4);
104+
h = random();
105+
memcpy(random_bytes + 4, &h, 4);
100106

101107
// fill up bytes 5,6,7,8
102108
// Yes, there's overlap.
103-
r = random();
104-
memcpy(random_bytes + 5, &r, 4);
109+
h = random();
110+
memcpy(random_bytes + 5, &h, 4);
105111
#endif
106112

107113
char * cspNonce;

0 commit comments

Comments
 (0)