Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/ci4sqlite3mc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ jobs:
make vfsstacktest
./vfsstacktest

- name: Tests with random values directly from the OS
if: matrix.cc_compiler == 'gcc'
run: |
make distclean
./configure CFLAGS="-g -O2 -DSQLITE3MC_USE_OS_RNG=1"
make cryptotest tempfiletest oomtest rekeytest threadtest crashtest vfsstacktest
./cryptotest
./tempfiletest 20000
./tempfiletest 20000 1 faults
./oomtest
./rekeytest
./threadtest
./crashtest
./vfsstacktest

android_build:
name: Android (API ${{ matrix.api-level }})
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ OPTION(SQLITE3MC_SECURE_MEMORY "Enable pragma to secure freed memory" OFF)

# Omit AES hardware support
OPTION(SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT "Omit AES hardware support" OFF)
OPTION(SQLITE3MC_USE_OS_RNG "Take all random values directly from the operating system" OFF)

# TCL
#OPTION(SQLITE_ENABLE_TCL "Enable TCL interface" OFF)
Expand Down Expand Up @@ -126,6 +127,7 @@ set(SQLITE3MC_BASE_DEFINITIONS
$<$<BOOL:${SQLITE_ENABLE_SESSION}>:SQLITE_ENABLE_SESSION=1>
$<$<BOOL:${SQLITE3MC_USE_MINIZ}>:SQLITE3MC_USE_MINIZ=1>
$<$<BOOL:${SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT}>:SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT=1>
$<$<BOOL:${SQLITE3MC_USE_OS_RNG}>:SQLITE3MC_USE_OS_RNG=1>
# $<$<BOOL:${SQLITE_USE_TCL}>:SQLITE_USE_TCL=1>
$<$<BOOL:${SQLITE_DEFAULT_FOREIGN_KEYS}>:SQLITE_DEFAULT_FOREIGN_KEYS=1>
$<$<BOOL:${SQLITE_LIKE_DOESNT_MATCH_BLOBS}>:SQLITE_LIKE_DOESNT_MATCH_BLOBS=1>
Expand Down Expand Up @@ -282,6 +284,11 @@ else()
set(SHARED_LIB_EXPORT_DEFINITION "__declspec(dllexport)")
endif()

# With SQLITE3MC_USE_OS_RNG, Windows builds use BCryptGenRandom
if(SQLITE3MC_USE_OS_RNG AND WIN32)
list(APPEND SQLITE3MC_LINK_LIBRARIES bcrypt)
endif()

set(_LIB_DIFINITIONS
_LIB
)
Expand Down
6 changes: 6 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ noinst_HEADERS = \
lib@SQLITE3MC_LIBNAME@_la_LDFLAGS = -no-undefined $(AM_LDFLAGS)
if HOST_WINDOWS
lib@SQLITE3MC_LIBNAME@_la_LIBADD = src/sqlite3mc.res_o
if USE_OS_RNG
lib@SQLITE3MC_LIBNAME@_la_LIBADD += -lbcrypt
endif
else
lib@SQLITE3MC_LIBNAME@_la_LIBADD = -ldl -lm
endif
Expand Down Expand Up @@ -152,6 +155,9 @@ endif
if HOST_WINDOWS
sqlite3shell_SOURCES += src/sqlite3mc_shell.rc
sqlite3shell_LDADD += src/sqlite3mc_shell.res_o
if USE_OS_RNG
sqlite3shell_LDADD += -lbcrypt
endif
CLEANFILES = src/sqlite3mc_shell.res_o
else
# libtool complains about unknown "-no-install" option when targetting MSW, so
Expand Down
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ AC_ARG_WITH([aegis],
AS_IF([test "x$with_aegis" = xno],
[AC_DEFINE([HAVE_CIPHER_AEGIS], [0], [Define if you have AEGIS disabled])])

dnl Take all random values directly from the operating system

AC_ARG_ENABLE([os-rng],
[AS_HELP_STRING([--enable-os-rng],
[Take all random values directly from the operating system])],
[],
[enable_os_rng=no])

AS_IF([test "x$enable_os_rng" = xyes],
[AC_DEFINE([SQLITE3MC_USE_OS_RNG], [1], [Define to take all random values directly from the operating system])])
AM_CONDITIONAL([USE_OS_RNG], [test "x$enable_os_rng" = xyes])

dnl Enable cipher codec

AC_ARG_ENABLE(codec,
Expand Down
44 changes: 44 additions & 0 deletions src/csprng_entropy.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,48 @@ static size_t entropy(void* buf, size_t n)
# error "Secure pseudorandom number generator not implemented for this OS"
#endif

#ifndef SQLITE3MC_USE_OS_RNG
#define SQLITE3MC_USE_OS_RNG 0
#endif

#if SQLITE3MC_USE_OS_RNG

#if defined(_WIN32)
/*
* On Windows, BCryptGenRandom fills the whole buffer in one call, while
* rand_s delivers only 4 bytes per call. It is also available to UWP apps.
* MinGW builds have to link with -lbcrypt.
*/
#include <windows.h>
#include <bcrypt.h>
#if defined(_MSC_VER)
#pragma comment(lib, "bcrypt.lib")
#endif

static size_t osRandom(void* buf, size_t n)
{
NTSTATUS status = BCryptGenRandom(NULL, (PUCHAR) buf, (ULONG) n, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
return BCRYPT_SUCCESS(status) ? n : 0;
}
#else
#define osRandom entropy
#endif

/*
* Every random value is taken directly from the operating system,
* without a generator of our own in between. entropy() rejects an
* all-zero result, so a request must not be too short: all callers
* ask for 8 bytes or more.
*/
SQLITE_PRIVATE
void chacha20_rng(void* out, size_t n)
{
if (n > 0 && osRandom(out, n) != n)
abort();
}

#else

/*
* ChaCha20 random number generator with fast key erasure: each refill
* replaces the key with the first 32 bytes of new keystream.
Expand Down Expand Up @@ -405,3 +447,5 @@ void chacha20_rng(void* out, size_t n)
sqlite3_mutex_leave(mutex);
#endif
}

#endif /* SQLITE3MC_USE_OS_RNG */
Loading