aboutsummaryrefslogtreecommitdiffstats
path: root/util-src/crand.c
blob: ed68534258e4d6488679b5d6522cf39edb92d013 (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
/* Prosody IM
-- Copyright (C) 2008-2017 Matthew Wild
-- Copyright (C) 2008-2017 Waqas Hussain
-- Copyright (C) 2016-2017 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
*
* The purpose of this module is to provide access to a PRNG in
* environments without /dev/urandom
*
* Caution! This has not been extensively tested.
*
*/

#define _DEFAULT_SOURCE

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

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

#if defined(WITH_GETRANDOM)

#ifndef __GLIBC_PREREQ
/* Not compiled with glibc at all */
#define __GLIBC_PREREQ(a,b) 0
#endif

#if ! __GLIBC_PREREQ(2,25)
/* Not compiled with a glibc that provides getrandom() */
#include <unistd.h>
#include <sys/syscall.h>

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

/* This wasn't present before glibc 2.25 */
int getrandom(void *buf, size_t buflen, unsigned int flags) {
	return syscall(SYS_getrandom, buf, buflen, flags);
}
#else
#include <sys/random.h>
#endif

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

int Lrandom(lua_State *L) {
	const size_t len = (size_t)luaL_checkinteger(L, 1);
	void *buf = lua_newuserdata(L, len);

#if defined(WITH_GETRANDOM)
	/*
	 * This acts like a read from /dev/urandom with the exception that it
	 * *does* block if the entropy pool is not yet initialized.
	 */
	int left = len;
	char *p = buf;

	do {
		int ret = getrandom(p, left, 0);

		if(ret < 0) {
			lua_pushstring(L, strerror(errno));
			return lua_error(L);
		}

		p += ret;
		left -= ret;
	} while(left > 0);

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

	if(!RAND_status()) {
		lua_pushliteral(L, "OpenSSL PRNG not seeded");
		return lua_error(L);
	}

	if(RAND_bytes((unsigned char *)buf, len) != 1) {
		/* TODO ERR_get_error() */
		lua_pushstring(L, "RAND_bytes() failed");
		return lua_error(L);
	}

#endif

	lua_pushlstring(L, buf, len);
	return 1;
}

int luaopen_util_crand(lua_State *L) {
#if (LUA_VERSION_NUM > 501)
	luaL_checkversion(L);
#endif

	lua_createtable(L, 0, 2);
	lua_pushcfunction(L, Lrandom);
	lua_setfield(L, -2, "bytes");

#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");

	return 1;
}