From 76cfbde9330c90f493c0b8597abe032ad387c150 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 3 Aug 2020 20:53:32 -0500 Subject: [PATCH] Add JANET_HASHSEED environment variable. --- CHANGELOG.md | 5 ++++- janet.1 | 8 ++++++++ jpm | 11 +++++++++-- meson.build | 2 +- src/conf/janetconf.h | 6 +++--- src/mainclient/shell.c | 12 ++++++++++-- 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37b227c4..bfe71c3c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Changelog All notable changes to this project will be documented in this file. -## 1.11.2 - 2020-08-03 +## 1.11.3 - 2020-08-03 +- Add `JANET_HASHSEED` environment variable when `JANET_PRF` is enabled. +- Expose `janet_cryptorand` in C API. +- Properly initialize PRF in default janet program - Add `index-of` to core library. - Add `-fPIC` back to core CFLAGS (non-optional when compiling default client with Makefile) - Fix defaults on Windows for ARM diff --git a/janet.1 b/janet.1 index 21b38e46..61c65b64 100644 --- a/janet.1 +++ b/janet.1 @@ -213,5 +213,13 @@ find native and source code modules. If no JANET_PATH is set, Janet will look in the default location set at compile time. .RE +.B JANET_HASHSEED +.RS +To disable randomization of Janet's PRF on start up, one can set this variable. This can have the +effect of making programs deterministic that otherwise would depend on the random seed chosen at program start. +This variable does nothing in the default configuration of Janet, as PRF is disabled by default. Also, JANET_REDUCED_OS +cannot be defined for this variable to have an effect. +.RE + .SH AUTHOR Written by Calvin Rose diff --git a/jpm b/jpm index c3433334..4d363831 100755 --- a/jpm +++ b/jpm @@ -537,8 +537,15 @@ int main(int argc, const char **argv) { #if defined(JANET_PRF) - uint8_t hash_key[JANET_HASH_KEY_SIZE]; - if (janet_cryptorand(hash_key, JANET_HASH_KEY_SIZE) != 0) { + uint8_t hash_key[JANET_HASH_KEY_SIZE + 1]; +#ifdef JANET_REDUCED_OS + char *envvar = NULL; +#else + char *envvar = getenv("JANET_HASHSEED"); +#endif + if (NULL != envvar) { + strncpy((char *) hash_key, envvar, sizeof(hash_key) - 1); + } else if (janet_cryptorand(hash_key, JANET_HASH_KEY_SIZE) != 0) { fputs("unable to initialize janet PRF hash function.\n", stderr); return 1; } diff --git a/meson.build b/meson.build index f7667cad..cf631a4f 100644 --- a/meson.build +++ b/meson.build @@ -20,7 +20,7 @@ project('janet', 'c', default_options : ['c_std=c99', 'b_lundef=false', 'default_library=both'], - version : '1.11.2') + version : '1.11.3') # Global settings janet_path = join_paths(get_option('prefix'), get_option('libdir'), 'janet') diff --git a/src/conf/janetconf.h b/src/conf/janetconf.h index 9a47662c..adcdde42 100644 --- a/src/conf/janetconf.h +++ b/src/conf/janetconf.h @@ -28,9 +28,9 @@ #define JANET_VERSION_MAJOR 1 #define JANET_VERSION_MINOR 11 -#define JANET_VERSION_PATCH 2 +#define JANET_VERSION_PATCH 3 #define JANET_VERSION_EXTRA "" -#define JANET_VERSION "1.11.2" +#define JANET_VERSION "1.11.3" /* #define JANET_BUILD "local" */ @@ -57,7 +57,7 @@ /* #define JANET_NO_UMASK */ /* Other settings */ -/* #define JANET_PRF */ +#define JANET_PRF /* #define JANET_NO_UTC_MKTIME */ /* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */ /* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */ diff --git a/src/mainclient/shell.c b/src/mainclient/shell.c index 99bb0a30..b3836c5c 100644 --- a/src/mainclient/shell.c +++ b/src/mainclient/shell.c @@ -1018,14 +1018,22 @@ int main(int argc, char **argv) { #endif #if defined(JANET_PRF) - uint8_t hash_key[JANET_HASH_KEY_SIZE]; - if (janet_cryptorand(hash_key, JANET_HASH_KEY_SIZE) != 0) { + uint8_t hash_key[JANET_HASH_KEY_SIZE + 1]; +#ifdef JANET_REDUCED_OS + char *envvar = NULL; +#else + char *envvar = getenv("JANET_HASHSEED"); +#endif + if (NULL != envvar) { + strncpy((char *) hash_key, envvar, sizeof(hash_key) - 1); + } else if (janet_cryptorand(hash_key, JANET_HASH_KEY_SIZE) != 0) { fputs("unable to initialize janet PRF hash function.\n", stderr); return 1; } janet_init_hash_key(hash_key); #endif + /* Set up VM */ janet_init();