commit fafdbbf
default
·
2023-06-05 16:29:25 +0000 UTC
parent e788a5b
Replaced usage of random() with xs_rnd_buf().
6 files changed,
+102,
-23
M
Makefile
+6,
-5
1@@ -37,19 +37,20 @@ activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \
2 xs_openssl.h xs_regex.h xs_time.h xs_set.h snac.h
3 data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h xs_set.h \
4 xs_time.h snac.h
5-format.o: format.c xs.h xs_regex.h snac.h
6+format.o: format.c xs.h xs_regex.h xs_mime.h snac.h
7 html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \
8 xs_time.h xs_mime.h snac.h
9 http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \
10 snac.h
11 httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_httpd.h xs_mime.h \
12- xs_time.h snac.h
13+ xs_time.h xs_openssl.h snac.h
14 main.o: main.c xs.h xs_io.h xs_json.h snac.h
15 mastoapi.o: mastoapi.c xs.h xs_openssl.h xs_json.h xs_io.h xs_time.h \
16- xs_glob.h xs_set.h snac.h
17+ xs_glob.h xs_set.h xs_random.h snac.h
18 snac.o: snac.c xs.h xs_io.h xs_unicode.h xs_json.h xs_curl.h xs_openssl.h \
19 xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \
20- snac.h
21+ xs_random.h snac.h
22 upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h
23-utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h
24+utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
25+ xs_random.h snac.h
26 webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h snac.h
+2,
-12
1@@ -10,6 +10,7 @@
2 #include "xs_time.h"
3 #include "xs_glob.h"
4 #include "xs_set.h"
5+#include "xs_random.h"
6
7 #include "snac.h"
8
9@@ -17,19 +18,8 @@ static xs_str *random_str(void)
10 /* just what is says in the tin */
11 {
12 unsigned int data[4] = {0};
13- FILE *f;
14-
15- if ((f = fopen("/dev/random", "r")) != NULL) {
16- fread(data, sizeof(data), 1, f);
17- fclose(f);
18- }
19- else {
20- data[0] = random() % 0xffffffff;
21- data[1] = random() % 0xffffffff;
22- data[2] = random() % 0xffffffff;
23- data[3] = random() % 0xffffffff;
24- }
25
26+ xs_rnd_buf(data, sizeof(data));
27 return xs_hex_enc((char *)data, sizeof(data));
28 }
29
M
snac.c
+4,
-1
1@@ -16,6 +16,7 @@
2 #include "xs_set.h"
3 #include "xs_time.h"
4 #include "xs_glob.h"
5+#include "xs_random.h"
6
7 #include "snac.h"
8
9@@ -122,7 +123,9 @@ xs_str *hash_password(const char *uid, const char *passwd, const char *nonce)
10 xs *hash;
11
12 if (nonce == NULL) {
13- d_nonce = xs_fmt("%08x", random());
14+ unsigned int r;
15+ xs_rnd_buf(&r, sizeof(r));
16+ d_nonce = xs_fmt("%08x", r);
17 nonce = d_nonce;
18 }
19
M
utils.c
+2,
-4
1@@ -6,6 +6,7 @@
2 #include "xs_json.h"
3 #include "xs_time.h"
4 #include "xs_openssl.h"
5+#include "xs_random.h"
6
7 #include "snac.h"
8
9@@ -204,10 +205,7 @@ void new_password(const char *uid, d_char **clear_pwd, d_char **hashed_pwd)
10 {
11 int rndbuf[3];
12
13- srandom(time(NULL) ^ getpid());
14- rndbuf[0] = random() & 0xffffffff;
15- rndbuf[1] = random() & 0xffffffff;
16- rndbuf[2] = random() & 0xffffffff;
17+ xs_rnd_buf(rndbuf, sizeof(rndbuf));
18
19 *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
20 *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
+87,
-0
1@@ -0,0 +1,87 @@
2+/* copyright (c) 2022 - 2023 grunfink / MIT license */
3+
4+#ifndef _XS_RANDOM_H
5+
6+#define _XS_RANDOM_H
7+
8+unsigned int xs_rnd_int32_d(unsigned int *seed);
9+void *xs_rnd_buf(void *buf, int size);
10+
11+#ifdef XS_IMPLEMENTATION
12+
13+#include <stdio.h>
14+#include <sys/time.h>
15+#include <unistd.h>
16+#include <stdlib.h>
17+
18+unsigned int xs_rnd_int32_d(unsigned int *seed)
19+/* returns a deterministic random integer. If seed is NULL, uses a static one */
20+{
21+ static unsigned int s = 0;
22+
23+ if (seed == NULL)
24+ seed = &s;
25+
26+ if (*seed == 0) {
27+ struct timeval tv;
28+
29+ gettimeofday(&tv, NULL);
30+ *seed = tv.tv_sec ^ tv.tv_usec ^ getpid();
31+ }
32+
33+ /* Linear congruential generator by Numerical Recipes */
34+ *seed = (*seed * 1664525) + 1013904223;
35+
36+ return *seed;
37+}
38+
39+
40+void *xs_rnd_buf(void *buf, int size)
41+/* fills buf with random data */
42+{
43+#ifdef __OpenBSD__
44+
45+ /* available since OpenBSD 2.2 */
46+ arc4random_buf(buf, size);
47+
48+#else
49+
50+ FILE *f;
51+ int done = 0;
52+
53+ if ((f = fopen("/dev/urandom", "r")) != NULL) {
54+ /* fill with great random data from the system */
55+ if (fread(buf, size, 1, f) == 1)
56+ done = 1;
57+
58+ fclose(f);
59+ }
60+
61+ if (!done) {
62+ /* fill the buffer with poor quality, deterministic data */
63+ unsigned int s = 0;
64+ unsigned char *p = (unsigned char *)buf;
65+ int n = size / sizeof(s);
66+
67+ /* fill with full integers */
68+ while (n--) {
69+ xs_rnd_int32_d(&s);
70+ p = memcpy(p, &s, sizeof(s)) + sizeof(s);
71+ }
72+
73+ if ((n = size % sizeof(s))) {
74+ /* fill the remaining */
75+ xs_rnd_int32_d(&s);
76+ memcpy(p, &s, n);
77+ }
78+ }
79+
80+#endif /* __OpenBSD__ */
81+
82+ return buf;
83+}
84+
85+
86+#endif /* XS_IMPLEMENTATION */
87+
88+#endif /* XS_RANDOM_H */
+1,
-1
1@@ -1 +1 @@
2-/* 3588cbb7859917f1c5965254f8a53c3349c773ea */
3+/* 5c255b45c8cd5d6c01c983b03e635936db12da03 */