Check-in [e302d11ff2]
Overview
Comment:Improved random number generation mechanisms
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e302d11ff25f4219620339a0e41cb7038294eabf4c87a75f713ed48609707ae0
User & Date: rkeene on 2018-07-05 17:19:31
Other Links: manifest | tags
Context
2018-07-06
02:35
Made API more consistent check-in: 6881dd597e user: rkeene tags: trunk
2018-07-05
17:19
Improved random number generation mechanisms check-in: e302d11ff2 user: rkeene tags: trunk
16:25
Made default prefix "nano_" again check-in: c3df9b5991 user: rkeene tags: trunk
Changes

Modified configure.ac from [2119a8f7e0] to [635fc0b8f5].

51
52
53
54
55
56
57

58
59
60
61
62
63
64
AX_CHECK_COMPILE_FLAG([-Wno-sign-compare], [CFLAGS="$CFLAGS -Wno-sign-compare"])

dnl Enable compiler warnings
AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS="-Wall $CFLAGS"])
AX_CHECK_COMPILE_FLAG([-W], [
	CFLAGS="-W $CFLAGS"
	AX_CHECK_COMPILE_FLAG([-Wno-self-assign], [CFLAGS="$CFLAGS -Wno-self-assign"])

])

dnl Enable hardening
AX_CHECK_COMPILE_FLAG([-fstack-protector-all], [CFLAGS="$CFLAGS -fstack-protector-all"])
AX_CHECK_COMPILE_FLAG([-fno-strict-overflow], [CFLAGS="$CFLAGS -fno-strict-overflow"])
AC_DEFINE([_FORTIFY_SOURCE], [2], [Enable fortification])








>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
AX_CHECK_COMPILE_FLAG([-Wno-sign-compare], [CFLAGS="$CFLAGS -Wno-sign-compare"])

dnl Enable compiler warnings
AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS="-Wall $CFLAGS"])
AX_CHECK_COMPILE_FLAG([-W], [
	CFLAGS="-W $CFLAGS"
	AX_CHECK_COMPILE_FLAG([-Wno-self-assign], [CFLAGS="$CFLAGS -Wno-self-assign"])
	AX_CHECK_COMPILE_FLAG([-Wno-tautological-constant-out-of-range-compare], [CFLAGS="$CFLAGS -Wno-tautological-constant-out-of-range-compare"])
])

dnl Enable hardening
AX_CHECK_COMPILE_FLAG([-fstack-protector-all], [CFLAGS="$CFLAGS -fstack-protector-all"])
AX_CHECK_COMPILE_FLAG([-fno-strict-overflow], [CFLAGS="$CFLAGS -fno-strict-overflow"])
AC_DEFINE([_FORTIFY_SOURCE], [2], [Enable fortification])

Modified randombytes.c from [643507802d] to [9959d0caf1].


1
2
3
4
5
6
7

8
9

10




11
12










13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


31
32
33
34
35
36
37
38
39
40
41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

95
96
97
98


99
100

101
102
103
104
105
106

107
108
109
110
111

112
113
114

#include "randombytes.h"

#if defined(HAVE_GETRANDOM)
#  ifdef HAVE_SYS_RANDOM_H
#    include <sys/random.h>
#  endif


void randombytes(uint8_t *buffer, uint64_t length) {
	ssize_t gr_ret;






	while (length > 0) {
		gr_ret = getrandom(buffer, length, 0);










		if (gr_ret <= 0) {
			continue;
		}

		buffer += gr_ret;
		length -= gr_ret;
	}

	return;
}
#elif defined(HAVE_GETENTROPY)
void randombytes(uint8_t *buffer, uint64_t length) {
	int ge_ret;
	int current_length;

	while (length > 0) {
		current_length = length;
		if (current_length > 256) {


			current_length = 256;
		}

		ge_ret = getentropy(buffer, current_length);
		if (ge_ret != 0) {
			continue;
		}

		buffer += current_length;
		length -= current_length;
	}


	return;
}
#elif 1
#include <tcl.h>

void randombytes(uint8_t *buffer, uint64_t length) {

	Tcl_Panic("Random data is not available");

#if 0
	Tcl_Channel fd;

	fd = Tcl_FSOpenFileChannel(NULL, Tcl_NewStringObj("/dev/urandom", -1), "rb", 0644);
	if (!fd) {
		Tcl_Panic("Unable to get random data");
	}

	while (length > 0) {
		read_ret = Tcl_ReadChars(fd, buffer, length);
		if (read_ret <= 0) {
			continue;
		}

		buffer += read_ret;
		length -= read_ret;
	}

	Tcl_Close(fd);
#endif

	return;

	/* NOTREACH */
	buffer = buffer;
	length = length;
}

#else
#  ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#  endif
#  ifdef HAVE_SYS_STAT_H
#    include <sys/stat.h>
#  endif
#  ifdef HAVE_FCNTL_H
#    include <fcntl.h>
# endif
# ifdef HAVE_UNISTD_H
#    include <unistd.h>
# endif
void randombytes(uint8_t *buffer, uint64_t length) {
	ssize_t read_ret;

	int fd = -1;

	while (fd < 0) {
		fd = open("/dev/urandom", O_RDONLY);


	}


	while (length > 0) {
		read_ret = read(fd, buffer, length);
		if (read_ret <= 0) {
			continue;
		}


		buffer += read_ret;
		length -= read_ret;
	}

	close(fd);

	return;
}
#endif
>
|

<
<
|
<

>
|
|
>

>
>
>
>

|
>
>
>
>
>
>
>
>
>
>
|









<
<
<
<

<
<
|
>
>
|
|
|
<
|
<
|
|
<
<
|
>
|
<
|
|
|

|
|
<

|
<
|
<
<
<


<
|
|
|
|

|
<
|
|
|
<
|
<
|
<
<
<

<













|

>


<
|
>
>


>
|
|




>
|
|



>
|


1
2
3


4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36




37


38
39
40
41
42
43

44

45
46


47
48
49

50
51
52
53
54
55

56
57

58



59
60

61
62
63
64
65
66

67
68
69

70

71



72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <limits.h>
#include <tcl.h>



#include "randombytes.h"


long long getrandom_impl(void *buf, unsigned int buflen);
void randombytes(unsigned char *buffer, unsigned long long length) {
	long long gr_ret;
	int errorCount = 0;

	if (length > UINT_MAX || length > LONG_LONG_MAX) {
		Tcl_Panic("Buffer length is too large");
	}

	while (length > 0) {
		gr_ret = getrandom_impl(buffer, length);

		if (gr_ret < 0) {
			errorCount++;
			if (errorCount > 10) {
				Tcl_Panic("Unable to generate random numbers");
			}
			continue;
		}
		errorCount = 0;

		if (gr_ret == 0) {
			continue;
		}

		buffer += gr_ret;
		length -= gr_ret;
	}

	return;
}







#if defined(HAVE_GETRANDOM)
#  ifdef HAVE_SYS_RANDOM_H
#    include <sys/random.h>
#  endif

long long getrandom_impl(void *buf, unsigned int buflen) {

	ssize_t gr_ret;


	gr_ret = getrandom(buf, buflen, 0);



	return(gr_ret);
}


#elif defined(HAVE_GETENTROPY)
#include <unistd.h>

long long getrandom_impl(void *buf, unsigned int buflen) {
	int ge_ret;


	if (buflen > 255) {

		buflen = 255;



	}


	ge_ret = getentropy(buf, buflen);
	if (ge_ret != 0) {
		return(-1);
	}

	return(buflen);

}
#elif defined(HAVE_CRYPTGENRANDOM) && 0
#include <tcl.h>

long long getrandom_impl(void *buf, unsigned int buflen) {

	Tcl_Panic("Incomplete CryptGenRandom");



}

#else
#  ifdef HAVE_SYS_TYPES_H
#    include <sys/types.h>
#  endif
#  ifdef HAVE_SYS_STAT_H
#    include <sys/stat.h>
#  endif
#  ifdef HAVE_FCNTL_H
#    include <fcntl.h>
# endif
# ifdef HAVE_UNISTD_H
#    include <unistd.h>
# endif
long long getrandom_impl(void *buf, unsigned int buflen) {
	ssize_t read_ret;
	long long retval;
	int fd = -1;


	fd = open("/dev/urandom", O_RDONLY);
	if (fd < 0) {
		return(-1);
	}

	retval = 0;
	while (buflen > 0) {
		read_ret = read(fd, buf, buflen);
		if (read_ret <= 0) {
			continue;
		}

		buf    += read_ret;
		retval += read_ret;
		buflen -= read_ret;
	}

	close(fd);

	return(retval);
}
#endif

Modified randombytes.h from [424e250e05] to [f8bae10419].

1
2
#include <stdint.h>
void randombytes(uint8_t *buffer, uint64_t length);
<
|

1

void randombytes(unsigned char *buffer, unsigned long long length);