MCPcopy Index your code
hub / github.com/git/git / csprng_bytes

Function csprng_bytes

wrapper.c:785–853  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

783}
784
785int csprng_bytes(void *buf, size_t len, MAYBE_UNUSED unsigned flags)
786{
787#if defined(HAVE_ARC4RANDOM) || defined(HAVE_ARC4RANDOM_LIBBSD)
788 /* This function never returns an error. */
789 arc4random_buf(buf, len);
790 return 0;
791#elif defined(HAVE_GETRANDOM)
792 ssize_t res;
793 char *p = buf;
794 while (len) {
795 res = getrandom(p, len, 0);
796 if (res < 0)
797 return -1;
798 len -= res;
799 p += res;
800 }
801 return 0;
802#elif defined(HAVE_GETENTROPY)
803 int res;
804 char *p = buf;
805 while (len) {
806 /* getentropy has a maximum size of 256 bytes. */
807 size_t chunk = len < 256 ? len : 256;
808 res = getentropy(p, chunk);
809 if (res < 0)
810 return -1;
811 len -= chunk;
812 p += chunk;
813 }
814 return 0;
815#elif defined(HAVE_RTLGENRANDOM)
816 if (!RtlGenRandom(buf, len))
817 return -1;
818 return 0;
819#elif defined(HAVE_OPENSSL_CSPRNG)
820 switch (RAND_pseudo_bytes(buf, len)) {
821 case 1:
822 return 0;
823 case 0:
824 if (flags & CSPRNG_BYTES_INSECURE)
825 return 0;
826 errno = EIO;
827 return -1;
828 default:
829 errno = ENOTSUP;
830 return -1;
831 }
832#else
833 ssize_t res;
834 char *p = buf;
835 int fd, err;
836 fd = open("/dev/urandom", O_RDONLY);
837 if (fd < 0)
838 return -1;
839 while (len) {
840 res = xread(fd, p, len);
841 if (res < 0) {
842 err = errno;

Callers 3

git_mkdstemps_modeFunction · 0.85
git_randFunction · 0.85
cmd__csprngFunction · 0.85

Calls 1

xreadFunction · 0.85

Tested by 1

cmd__csprngFunction · 0.68