aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/crand.c
blob: 735135fb81a416de72ad849cf433bc5823c6aac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Prosody IM
-- Copyright (C) 2008-2016 Matthew Wild
-- Copyright (C) 2008-2016 Waqas Hussain
-- Copyright (C) 2016 Kim Alvefur
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
*/

/*
* crand.c
* C PRNG interface
*/

#include "lualib.h"
#include "lauxlib.h"

#include <string.h>
#include <errno.h>

/*
 * TODO: Decide on fixed size or dynamically allocated buffer
 */
#if 1
#include <stdlib.h>
#else
#define BUFLEN 256
#endif

#if defined(WITH_GETRANDOM)
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>

#ifndef SYS_getrandom
#error getrandom() requires Linux 3.17 or later
#endif

/* Was this not supposed to be a function? */
int getrandom(char *buf, size_t len, int flags) {
	return syscall(SYS_getrandom, buf, len, flags);
}

#elif defined(WITH_ARC4RANDOM)
#include <stdlib.h>
#elif defined(WITH_OPENSSL)
#include <openssl/rand.h>
#else
#error util.crand compiled without a random source
#endif

int Lrandom(lua_State *L) {
#ifdef BUFLEN
	unsigned char buf[BUFLEN];
#else
	unsigned char *buf;
#endif
	int ret = 0;
	size_t len = (size_t)luaL_checkint(L, 1);
#ifdef BUFLEN
	len = len > BUFLEN ? BUFLEN : len;
#else
	buf = malloc(len);

	if(buf == NULL) {
		lua_pushnil(L);
		lua_pushstring(L, "out of memory");
		/* or it migth be better to
		 * return lua_error(L);
		 */
		return 2;
	}
#endif

#if defined(WITH_GETRANDOM)
	ret = getrandom(buf, len, 0);

	if(ret < 0) {
#ifndef BUFLEN
		free(buf);
#endif
		lua_pushnil(L);
		lua_pushstring(L, strerror(errno));
		lua_pushinteger(L, errno);
		return 3;
	}

#elif defined(WITH_ARC4RANDOM)
	arc4random_buf(buf, len);
	ret = len;
#elif defined(WITH_OPENSSL)
	ret = RAND_bytes(buf, len);

	if(ret == 1) {
		ret = len;
	} else {
#ifndef BUFLEN
		free(buf);
#endif
		lua_pushnil(L);
		lua_pushstring(L, "failed");
		/* lua_pushinteger(L, ERR_get_error()); */
		return 2;
	}

#endif

	lua_pushlstring(L, buf, ret);
#ifndef BUFLEN
	free(buf);
#endif
	return 1;
}

#ifdef ENABLE_SEEDING
int Lseed(lua_State *L) {
	size_t len;
	const char *seed = lua_tolstring(L, 1, &len);

#if defined(WITH_OPENSSL)
	RAND_add(seed, len, len);
	return 0;
#else
	lua_pushnil(L);
	lua_pushliteral(L, "not-supported");
	return 2;
#endif
}
#endif

int luaopen_util_crand(lua_State *L) {
	lua_newtable(L);
	lua_pushcfunction(L, Lrandom);
	lua_setfield(L, -2, "bytes");
#ifdef ENABLE_SEEDING
	lua_pushcfunction(L, Lseed);
	lua_setfield(L, -2, "seed");
#endif

#if defined(WITH_GETRANDOM)
	lua_pushstring(L, "Linux");
#elif defined(WITH_ARC4RANDOM)
	lua_pushstring(L, "arc4random()");
#elif defined(WITH_OPENSSL)
	lua_pushstring(L, "OpenSSL");
#endif
	lua_setfield(L, -2, "_source");

#if defined(WITH_OPENSSL) && defined(_WIN32)
	/* Do we need to seed this on Windows? */
#endif

	return 1;
}