File: entropy.c

package info (click to toggle)
scrypt 1.3.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ansic: 5,763; sh: 5,504; makefile: 257
file content (173 lines) | stat: -rw-r--r-- 3,222 bytes parent folder | download | duplicates (3)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

#include "warnp.h"

#include "entropy.h"

/**
 * XXX Portability
 * XXX We obtain random bytes from the operating system by opening
 * XXX /dev/urandom and reading them from that device; this works on
 * XXX modern UNIX-like operating systems but not on systems like
 * XXX win32 where there is no concept of /dev/urandom.
 */

/**
 * Entropy reader state.  At present it holds a file descriptor for
 * /dev/urandom, but in the future this structure may gain other OS-dependent
 * state, e.g. a Windows Handle.
 */
struct entropy_read_cookie {
	int fd;
};

/**
 * entropy_read_init(void):
 * Initialize the ability to produce random bytes from the operating system,
 * and return a cookie.
 */
struct entropy_read_cookie *
entropy_read_init(void)
{
	struct entropy_read_cookie * er;

	/* Allocate cookie. */
	if ((er = malloc(sizeof(struct entropy_read_cookie))) == NULL) {
		warnp("malloc");
		goto err0;
	}

	/* Open /dev/urandom. */
	if ((er->fd = open("/dev/urandom", O_RDONLY)) == -1) {
		warnp("open(/dev/urandom)");
		goto err1;
	}

	/* Success! */
	return (er);

err1:
	free(er);
err0:
	/* Failure! */
	return (NULL);
}

/**
 * entropy_read_fill(er, buf, buflen):
 * Fill the given buffer with random bytes provided by the operating system
 * using the resources in ${er}.
 */
int
entropy_read_fill(struct entropy_read_cookie * er, uint8_t * buf,
    size_t buflen)
{
	ssize_t lenread;

	/* Sanity checks. */
	assert(er != NULL);
	assert(buflen <= SSIZE_MAX);

	/* Read bytes until we have filled the buffer. */
	while (buflen > 0) {
		if ((lenread = read(er->fd, buf, buflen)) == -1) {
			warnp("read(/dev/urandom)");
			goto err0;
		}

		/* The random device should never EOF. */
		if (lenread == 0) {
			warn0("EOF on /dev/urandom?");
			goto err0;
		}

		/* We've filled a portion of the buffer. */
		buf += (size_t)lenread;
		buflen -= (size_t)lenread;
	}

	/* Success! */
	return (0);

err0:
	/* Failure! */
	return (-1);
}

/**
 * entropy_read_done(er):
 * Release any resources used by ${er}.
 */
int
entropy_read_done(struct entropy_read_cookie * er)
{

	/* Sanity check. */
	assert(er != NULL);

	/* Close the device. */
	while (close(er->fd) == -1) {
		if (errno != EINTR) {
			warnp("close(/dev/urandom)");
			goto err1;
		}
	}

	/* Clean up. */
	free(er);

	/* Success! */
	return (0);

err1:
	free(er);

	/* Failure! */
	return (-1);
}

/**
 * entropy_read(buf, buflen):
 * Fill the given buffer with random bytes provided by the operating system.
 */
int
entropy_read(uint8_t * buf, size_t buflen)
{
	struct entropy_read_cookie * er;

	/* Sanity-check the buffer size. */
	assert(buflen <= SSIZE_MAX);

	/* Open /dev/urandom. */
	if ((er = entropy_read_init()) == NULL) {
		warn0("entropy_read_init");
		goto err0;
	}

	/* Read bytes until we have filled the buffer. */
	if (entropy_read_fill(er, buf, buflen)) {
		warn0("entropy_read_fill");
		goto err1;
	}

	/* Close the device. */
	if (entropy_read_done(er)) {
		warn0("entropy_read_done");
		goto err0;
	}

	/* Success! */
	return (0);

err1:
	entropy_read_done(er);
err0:
	/* Failure! */
	return (-1);
}