-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v1.1] New feature: SRANDOM with upper bound option
This adds SRANDOM, a secure random number generator similar to SRANDOM on bash 5.1+. It uses the OS implementation of arc4random(3) or, absent that, an integrated version that uses Daniel J. Bernstein's ChaCha20 cipher, which I've ported to libast from the OpenSSH distribution, which ported it from OpenBSD.[*1] This cryptographically secure generator is seeded using getentropy(2), getandom(2), /dev/urandom, or /dev/random. A feature the bash version doesn't have is that an upper bound can be set by assigning its value to SRANDOM; subsequent numbers will then be uniformly distributed between 0 and the value of the upper bound minus one, in a way that avoids "modulo bias" if the upper bound is not a power of two.[*2] src/lib/libast/comp/arc4random.c, src/lib/libast/comp/chacha_private.h, src/lib/libast/features/random: - Add arc4random and ChaCha20, ported to libast.[*1] src/lib/libast/Mamfile: - Make it all build. (See README-mamake.md for info) - Copy the result of features/random to the install root (arch/*/) as ast_random.h for ksh to include. src/lib/libast/features/api: - API version bump to 20240121 due to above change. src/cmd/ksh93/include/variables.h src/cmd/ksh93/data/variables.c, - Add SRANDNOD ("SRANDOM") built-in variable node. src/cmd/ksh93/sh/init.c: - sh_reseed_rand(): Now that we have arc4random() we might as well use it to reseed $RANDOM, simplifying this function as well as increasing the quality of the reseeding. Of course this still does not make $RANDOM actually random. (re: af6a32d) - Add discipline for SRANDOM: SRAND_init/SRAND_disc, set up to automatically call put_srand(), nget_srand() and get_srand() for assigning, arithmetic retrieval and text retrieval. - A global static srand_upper_bound variable remembers the upper bound; a value of zero (the default for static vars) deactivates it. When it is non-zero, arc4random_uniform() is called instead of arc4random(), avoiding modulo bias. [*1] https://github.com/openssh/openssh-portable/blob/master/openbsd-compat/arc4random.c https://github.com/openssh/openssh-portable/blob/master/openbsd-compat/arc4random.h https://github.com/openssh/openssh-portable/blob/master/openbsd-compat/arc4random_uniform.c https://github.com/openssh/openssh-portable/blob/master/openbsd-compat/chacha_private.h [*2] https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/
- Loading branch information
Showing
14 changed files
with
793 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,3 +141,24 @@ bsd package general copyright notice | |
# Bill Joy # | ||
# # | ||
######################################################################## | ||
|
||
OpenBSD License (arc4random.c, arc4random.h): | ||
|
||
/* | ||
* Copyright (c) 1996, David Mazieres <[email protected]> | ||
* Copyright (c) 2008, Damien Miller <[email protected]> | ||
* Copyright (c) 2013, Markus Friedl <[email protected]> | ||
* Copyright (c) 2014, Theo de Raadt <[email protected]> | ||
* | ||
* Permission to use, copy, modify, and distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.