aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2024-07-04 01:26:29 +0200
committerMike Pall <mike>2024-07-04 01:26:29 +0200
commit04dca7911ea255f37be799c18d74c305b921c1a6 (patch)
tree0979ae369fbb82e7e5fb1c53fcf5cf5955c95f7b
parent7421a1b33c7ea46f12bba9700c15b5c90253fee0 (diff)
downloadluajit-04dca7911ea255f37be799c18d74c305b921c1a6.tar.gz
luajit-04dca7911ea255f37be799c18d74c305b921c1a6.tar.bz2
luajit-04dca7911ea255f37be799c18d74c305b921c1a6.zip
Call math.randomseed() without arguments to seed from system entropy.
Reminder: the math.random() PRNG is NOT SUITABLE FOR CRYPTOGRAPHIC USE.
-rw-r--r--doc/extensions.html6
-rw-r--r--src/Makefile.dep3
-rw-r--r--src/lib_math.c6
-rw-r--r--src/lj_errmsg.h1
4 files changed, 13 insertions, 3 deletions
diff --git a/doc/extensions.html b/doc/extensions.html
index c1c9a808..e9aaa096 100644
--- a/doc/extensions.html
+++ b/doc/extensions.html
@@ -265,7 +265,7 @@ and let the GC do its work.
265LuaJIT uses a Tausworthe PRNG with period 2^223 to implement 265LuaJIT uses a Tausworthe PRNG with period 2^223 to implement
266<tt>math.random()</tt> and <tt>math.randomseed()</tt>. The quality of 266<tt>math.random()</tt> and <tt>math.randomseed()</tt>. The quality of
267the PRNG results is much superior compared to the standard Lua 267the PRNG results is much superior compared to the standard Lua
268implementation, which uses the platform-specific ANSI rand(). 268implementation, which uses the platform-specific ANSI <tt>rand()</tt>.
269</p> 269</p>
270<p> 270<p>
271The PRNG generates the same sequences from the same seeds on all 271The PRNG generates the same sequences from the same seeds on all
@@ -276,6 +276,10 @@ It's correctly scaled up and rounded for <tt>math.random(n&nbsp;[,m])</tt> to
276preserve uniformity. 276preserve uniformity.
277</p> 277</p>
278<p> 278<p>
279Call <tt>math.randomseed()</tt> without any arguments to seed it from
280system entropy.
281</p>
282<p>
279Important: Neither this nor any other PRNG based on the simplistic 283Important: Neither this nor any other PRNG based on the simplistic
280<tt>math.random()</tt> API is suitable for cryptographic use. 284<tt>math.random()</tt> API is suitable for cryptographic use.
281</p> 285</p>
diff --git a/src/Makefile.dep b/src/Makefile.dep
index fda77c83..e9f83399 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -32,7 +32,8 @@ lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
32 lj_target.h lj_target_*.h lj_trace.h lj_dispatch.h lj_traceerr.h \ 32 lj_target.h lj_target_*.h lj_trace.h lj_dispatch.h lj_traceerr.h \
33 lj_vm.h lj_vmevent.h lj_lib.h luajit.h lj_libdef.h 33 lj_vm.h lj_vmevent.h lj_lib.h luajit.h lj_libdef.h
34lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 34lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
35 lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_prng.h lj_libdef.h 35 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_vm.h lj_prng.h \
36 lj_libdef.h
36lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ 37lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
37 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \ 38 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \
38 lj_libdef.h 39 lj_libdef.h
diff --git a/src/lib_math.c b/src/lib_math.c
index 4539f804..08bb7673 100644
--- a/src/lib_math.c
+++ b/src/lib_math.c
@@ -13,6 +13,7 @@
13#include "lualib.h" 13#include "lualib.h"
14 14
15#include "lj_obj.h" 15#include "lj_obj.h"
16#include "lj_err.h"
16#include "lj_lib.h" 17#include "lj_lib.h"
17#include "lj_vm.h" 18#include "lj_vm.h"
18#include "lj_prng.h" 19#include "lj_prng.h"
@@ -183,7 +184,10 @@ LJLIB_PUSH(top-2) /* Upvalue holds userdata with PRNGState. */
183LJLIB_CF(math_randomseed) 184LJLIB_CF(math_randomseed)
184{ 185{
185 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1)))); 186 PRNGState *rs = (PRNGState *)(uddata(udataV(lj_lib_upvalue(L, 1))));
186 random_seed(rs, lj_lib_checknum(L, 1)); 187 if (L->base != L->top)
188 random_seed(rs, lj_lib_checknum(L, 1));
189 else if (!lj_prng_seed_secure(rs))
190 lj_err_caller(L, LJ_ERR_PRNGSD);
187 return 0; 191 return 0;
188} 192}
189 193
diff --git a/src/lj_errmsg.h b/src/lj_errmsg.h
index 127c06da..109e909c 100644
--- a/src/lj_errmsg.h
+++ b/src/lj_errmsg.h
@@ -79,6 +79,7 @@ ERRDEF(SETFENV, LUA_QL("setfenv") " cannot change environment of given object")
79ERRDEF(CORUN, "cannot resume running coroutine") 79ERRDEF(CORUN, "cannot resume running coroutine")
80ERRDEF(CODEAD, "cannot resume dead coroutine") 80ERRDEF(CODEAD, "cannot resume dead coroutine")
81ERRDEF(COSUSP, "cannot resume non-suspended coroutine") 81ERRDEF(COSUSP, "cannot resume non-suspended coroutine")
82ERRDEF(PRNGSD, "PRNG seeding failed")
82ERRDEF(TABINS, "wrong number of arguments to " LUA_QL("insert")) 83ERRDEF(TABINS, "wrong number of arguments to " LUA_QL("insert"))
83ERRDEF(TABCAT, "invalid value (%s) at index %d in table for " LUA_QL("concat")) 84ERRDEF(TABCAT, "invalid value (%s) at index %d in table for " LUA_QL("concat"))
84ERRDEF(TABSORT, "invalid order function for sorting") 85ERRDEF(TABSORT, "invalid order function for sorting")