Repository navigation
Add an option to take all random values directly from the OS - #315
Merged
Merged
Conversation
Owner
Yes, in the new variant
It is. The amount of random bytes needed ist relatively small, so the runtime penalty should usually be small.
For now I'd leave it as is, but let's discuss this again, when the next release will be prepared. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
sqlite3mc gets its IVs, nonces, salts and the keys for temporary files from
chacha20_rng, a ChaCha20 generator seeded from the OS. This PR adds the compile-time optionSQLITE3MC_USE_OS_RNG. With the option set to 1,chacha20_rngtakes every value directly from the operating system: fromentropy(), the source that seeds the generator today, and on Windows from BCryptGenRandom. If the OS fails, it aborts, as it does now. The default is 0, so nothing changes unless someone switches it on.Two cases where this helps:
It costs one OS call per request, which is small compared with encrypting a page. In the smallest write transactions (one row, no fsync) it added 1 to 3.5 % on macOS; on Windows the difference was within the noise. With fsync the share is much smaller, and reading needs no random values at all.
On Windows, entropy() uses rand_s by default, which delivers only 4 bytes per call: fine for the seed, but the 128 bytes the sqlcipher scheme asks for per page would take 32 calls. So with the option on, Windows uses BCryptGenRandom, which fills the whole buffer in one call and is also available to UWP apps.
Build options:
-DSQLITE3MC_USE_OS_RNG=ONfor CMake and--enable-os-rngfor configure. On Windows both link bcrypt; with MSVC the#pragma comment(lib, "bcrypt.lib")in the code does that as well. I left premake and the makefiles in build/ alone; there the define has to be set by hand, and MinGW builds need -lbcrypt.A new CI step runs the tests a second time with the option (only in the gcc jobs, to keep the CI time down).
entropy() rejects an all-zero result. That is fine because every caller asks for at least 8 bytes; a comment says so, so that nobody adds a one-byte request later.
With the option on, the name
chacha20_rngno longer fits. I kept it so that none of the callers change, but feel free to rename it.Testing, with the option on, all CI test programs (cryptotest, tempfiletest, oomtest, rekeytest, threadtest, crashtest, vfsstacktest) and a small check program, not part of this PR, that writes and reopens a database with every cipher:
The check program sends about 110,000 requests to getrandom on Linux and to crypto.getRandomValues under WASM with the option, and 2 to 3 (the seed) without it.
You wrote in October that taking all random bytes from the OS would be fine with you. If you like, I can also switch the default to 1 in this PR.