mirror of
https://github.com/git/git.git
synced 2024-12-11 19:03:50 +08:00
05cd988dce
There are many situations in which having access to a cryptographically secure pseudorandom number generator (CSPRNG) is helpful. In the future, we'll encounter one of these when dealing with temporary files. To make this possible, let's add a function which reads from a system CSPRNG and returns some bytes. We know that all systems will have such an interface. A CSPRNG is required for a secure TLS or SSH implementation and a Git implementation which provided neither would be of little practical use. In addition, POSIX is set to standardize getentropy(2) in the next version, so in the (potentially distant) future we can rely on that. For systems which lack one of the other interfaces, we provide the ability to use OpenSSL's CSPRNG. OpenSSL is highly portable and functions on practically every known OS, and we know it will have access to some source of cryptographically secure randomness. We also provide support for the arc4random in libbsd for folks who would prefer to use that. Because this is a security sensitive interface, we take some precautions. We either succeed by filling the buffer completely as we requested, or we fail. We don't return partial data because the caller will almost never find that to be a useful behavior. Specify a makefile knob which users can use to specify one or more suitable CSPRNGs, and turn the multiple string options into a set of defines, since we cannot match on strings in the preprocessor. We allow multiple options to make the job of handling this in autoconf easier. The order of options is important here. On systems with arc4random, which is most of the BSDs, we use that, since, except on MirBSD and macOS, it uses ChaCha20, which is extremely fast, and sits entirely in userspace, avoiding a system call. We then prefer getrandom over getentropy, because the former has been available longer on Linux, and then OpenSSL. Finally, if none of those are available, we use /dev/urandom, because most Unix-like operating systems provide that API. We prefer options that don't involve device files when possible because those work in some restricted environments where device files may not be available. Set the configuration variables appropriately for Linux and the BSDs, including macOS, as well as Windows and NonStop. We specifically only consider versions which receive publicly available security support here. For the same reason, we don't specify getrandom(2) on Linux, because CentOS 7 doesn't support it in glibc (although its kernel does) and we don't want to resort to making syscalls. Finally, add a test helper to allow this to be tested by hand and in tests. We don't add any tests, since invoking the CSPRNG is not likely to produce interesting, reproducible results. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
30 lines
554 B
C
30 lines
554 B
C
#include "test-tool.h"
|
|
#include "git-compat-util.h"
|
|
|
|
|
|
int cmd__csprng(int argc, const char **argv)
|
|
{
|
|
unsigned long count;
|
|
unsigned char buf[1024];
|
|
|
|
if (argc > 2) {
|
|
fprintf(stderr, "usage: %s [<size>]\n", argv[0]);
|
|
return 2;
|
|
}
|
|
|
|
count = (argc == 2) ? strtoul(argv[1], NULL, 0) : -1L;
|
|
|
|
while (count) {
|
|
unsigned long chunk = count < sizeof(buf) ? count : sizeof(buf);
|
|
if (csprng_bytes(buf, chunk) < 0) {
|
|
perror("failed to read");
|
|
return 5;
|
|
}
|
|
if (fwrite(buf, chunk, 1, stdout) != chunk)
|
|
return 1;
|
|
count -= chunk;
|
|
}
|
|
|
|
return 0;
|
|
}
|