summaryrefslogtreecommitdiff
path: root/package/rng-tools/patches/rngd_tpm_support.patch
blob: 0edc47109e0af9658518461b43aea3c5e0bbe112 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
Patch taken from http://sourceforge.net/tracker/?func=detail&aid=2261574&group_id=3242&atid=353242

diff -uNr rng-tools-2-orig/rngd.c rng-tools-2/rngd.c
--- rng-tools-2-orig/rngd.c	2004-08-24 23:30:00.000000000 +0530
+++ rng-tools-2/rngd.c	2008-11-11 15:39:31.000000000 +0530
@@ -91,6 +91,8 @@
 
 	{ "timeout", 't', "nnn", 0,
 	  "Interval written to random-device when the entropy pool is full, in seconds (default: 60)" },
+	{ "no-tpm", 'n', "1|0", 0,
+	  "do not use tpm as a source of random number input (default: 0)" },
 
 	{ 0 },
 };
@@ -102,6 +104,7 @@
 	.random_step	= 64,
 	.fill_watermark = 2048,
 	.daemon		= 1,
+	.no_tpm		=0,
 };
 struct arguments *arguments = &default_arguments;
 
@@ -147,6 +150,15 @@
 			arguments->fill_watermark = n;
 		break;
 	}
+	case 'n': {
+		int n;
+		if ((sscanf(arg,"%i", &n) == 0) || ((n | 1)!=1))
+			argp_usage(state);
+		else
+			arguments->no_tpm=0;
+		break;
+			
+	}
 
 	default:
 		return ARGP_ERR_UNKNOWN;
@@ -162,26 +174,41 @@
 		    double poll_timeout)
 {
 	unsigned char buf[FIPS_RNG_BUFFER_SIZE];
-	unsigned char *p;
-	int fips;
+	int fips,retval;
 
 	for (;;) {
-		xread(buf, sizeof buf);
+		if (arguments->no_tpm == 0) {
+			retval=xread_tpm(buf, sizeof buf);
+			if (retval < 0)
+				sleep(1);
+			else
+				update_kernel_random(random_step,
+					poll_timeout, buf, &tpm_fipsctx);
+		}
+		retval=xread(buf, sizeof buf);
+		if (retval > 0)
+			update_kernel_random(random_step,
+				poll_timeout, buf, &fipsctx);
+	}
+}
 
-		fips = fips_run_rng_test(&fipsctx, buf);
+int update_kernel_random(int random_step, double poll_timeout,
+	unsigned char *buf, fips_ctx_t *fipsctx) {
 
-		if (fips) {
-			message(LOG_DAEMON|LOG_ERR, "failed fips test\n");
-			sleep(1);
-			continue;
-		}
+	int fips;
+	unsigned char *p;
+	fips = fips_run_rng_test(fipsctx, buf);
+	if (fips) {
+		message(LOG_DAEMON|LOG_ERR, "failed fips test\n");
+		return 1;
+	}
 
-		for (p = buf; p + random_step <= &buf[sizeof buf];
-		     p += random_step) {
-			random_add_entropy(p, random_step);
-			random_sleep(poll_timeout);
-		}
+	for (p = buf; p + random_step <= &buf[FIPS_RNG_BUFFER_SIZE];
+		 p += random_step) {
+		random_add_entropy(p, random_step);
+		random_sleep(poll_timeout);
 	}
+	return 0;
 }
 
 
diff -uNr rng-tools-2-orig/rngd_entsource.c rng-tools-2/rngd_entsource.c
--- rng-tools-2-orig/rngd_entsource.c	2004-04-15 10:36:17.000000000 +0530
+++ rng-tools-2/rngd_entsource.c	2008-11-11 15:39:31.000000000 +0530
@@ -35,6 +35,7 @@
 #include <errno.h>
 #include <syslog.h>
 #include <string.h>
+#include <signal.h>
 
 #include "rngd.h"
 #include "fips.h"
@@ -42,17 +43,27 @@
 #include "rngd_entsource.h"
 
 
-/* Logic and contexts */
-static int rng_fd;			/* rng data source */
-fips_ctx_t fipsctx;			/* Context for the FIPS tests */
+/* The overhead incured when tpm returns the random nos as per TCG spec 
+ * it is 14 bytes.*/
+#define TPM_GET_RNG_OVERHEAD	14
 
+static const char *rng_device="/dev/tpm0";
+/* Logic and contexts */
+static int rng_fd;	/* rng data source */
+fips_ctx_t fipsctx;	/* Context for the FIPS tests */
+fips_ctx_t tpm_fipsctx;	/* Context for the tpm FIPS tests */
 
 /* Read data from the entropy source */
-void xread(void *buf, size_t size)
+int xread(void *buf, size_t size)
 {
 	size_t off = 0;
 	ssize_t r;
 
+	/* Do nothing if we have no hw rng, maybe we have tpm */
+	if (rng_fd < 0) {
+		message(LOG_DAEMON|LOG_ERR, "Invalid file handle\n");
+		return -1;
+	}
 	while (size > 0) {
 		do {
 			r = read(rng_fd, buf + off, size);
@@ -65,8 +76,85 @@
 
 	if (size) {
 		message(LOG_DAEMON|LOG_ERR, "read error\n");
-		exit(1);
+		return -1;
+	}
+	return 0;
+}
+
+alarm_handler(int i) {
+	;
+}
+/* tpm rng read call to kernel has 13 bytes of overhead
+ * the logic to process this involves reading to a temporary_buf
+ * and copying the no generated to buf*/
+int xread_tpm(void *buf, size_t size)
+{
+	size_t bytes_read = 0;
+	ssize_t r;
+	int retval,rngtpm_fd;
+	unsigned char *temp_buf=NULL;
+	unsigned char rng_cmd[] = {
+		0, 193,            /* TPM_TAG_RQU_COMMAND */
+		0, 0, 0, 14,       /* length */
+		0, 0, 0, 70,       /* TPM_ORD_GetRandom */
+		0, 0, 0, 0,        /* number of bytes to return */
+	};
+	char *offset;
+	
+	rngtpm_fd=open(rng_device, O_RDWR);
+	if (rngtpm_fd < 0) {
+		message(LOG_ERR|LOG_INFO,
+			"Unable to open %s: %s\n",rng_device,strerror(errno));
+		return -1;
+	}
+
+	temp_buf= (unsigned char *) malloc(size + TPM_GET_RNG_OVERHEAD);
+	memset(temp_buf,0,(size+TPM_GET_RNG_OVERHEAD));
+	if (temp_buf == NULL) {
+		message(LOG_ERR|LOG_INFO,"No memory");
+		return -1;
+	}
+	/* 32 bits has been reserved for random byte size */
+	rng_cmd[13]=(unsigned char)(size & 0xFF);
+	rng_cmd[12]=(unsigned char)((size >> 8) & 0xFF);
+	rng_cmd[11]=(unsigned char)((size >> 16) & 0xFF);
+	rng_cmd[10]=(unsigned char)((size >> 24) & 0xFF);
+	offset=buf;
+	while (bytes_read < size) {
+		r=0;
+		while (r < sizeof(rng_cmd)) {
+			retval=write(rngtpm_fd,rng_cmd + r,sizeof(rng_cmd)-r);
+			if (retval < 0) {
+				message(LOG_ERR|LOG_INFO,
+					"Error writing %s\n",rng_device);
+				retval=-1;
+				goto error_out;
+			}
+			r+=retval;
+		}
+		if (r < sizeof(rng_cmd)) {
+			message(LOG_ERR|LOG_INFO,
+				"Error writing %s\n",rng_device);
+			retval=-1;
+			goto error_out;
+		}
+		r=read(rngtpm_fd,temp_buf,size);
+		r=(r - TPM_GET_RNG_OVERHEAD);
+		bytes_read=bytes_read + r;
+		if (bytes_read > size) {
+			memcpy(offset,temp_buf + TPM_GET_RNG_OVERHEAD,
+				r - (bytes_read - size));
+			break;
+		}
+		memcpy(offset, temp_buf + TPM_GET_RNG_OVERHEAD,
+			r);
+		offset=offset+r;
 	}
+	retval=0;
+error_out:
+	free(temp_buf);
+	close(rngtpm_fd);
+	return retval;
 }
 
 /* Initialize entropy source */
@@ -93,14 +181,31 @@
  */
 void init_entropy_source(const char* sourcedev)
 {
+	/* We cannot keep the tpm device open always.
+	 * We need to open get random data and close
+	 * to allow tpm-tools and other utilities
+	 * access to /dev/tpm */
+	int tpm_fd;
 	rng_fd = open(sourcedev, O_RDONLY);
 	if (rng_fd == -1) {
 		message(LOG_DAEMON|LOG_ERR, "can't open %s: %s",
 			sourcedev, strerror(errno));
-		exit(EXIT_FAIL);
+		/* Try to open tpm this is just a test, no point in proceeding further
+		 * if no source of entropy is present
+		 */
+		tpm_fd = open(rng_device, O_RDONLY);
+		if (tpm_fd < 0 ) {
+			message(LOG_DAEMON|LOG_ERR,
+				"can't open entropy source(tpm or intel/amd rng) %s",
+				strerror(errno));
+			message(LOG_DAEMON|LOG_ERR,"Maybe RNG device modules are not loaded\n");
+			exit(1);
+		}
+		close(tpm_fd);
 	}
 
 	/* Bootstrap FIPS tests */
 	fips_init(&fipsctx, discard_initial_data());
+	fips_init(&tpm_fipsctx, 0);
 }
 
diff -uNr rng-tools-2-orig/rngd_entsource.h rng-tools-2/rngd_entsource.h
--- rng-tools-2-orig/rngd_entsource.h	2004-04-15 10:34:45.000000000 +0530
+++ rng-tools-2/rngd_entsource.h	2008-11-11 15:39:31.000000000 +0530
@@ -28,7 +28,7 @@
 
 /* Logic and contexts */
 extern fips_ctx_t fipsctx;		/* Context for the FIPS tests */
-
+extern fips_ctx_t tpm_fipsctx;	/* Context for the tpm FIPS tests */
 /*
  * Initialize entropy source and entropy conditioning
  *
@@ -37,6 +37,6 @@
 extern void init_entropy_source(const char* sourcedev);
 
 /* Read data from the entropy source */
-void xread(void *buf, size_t size);
+int xread(void *buf, size_t size);
 
 #endif /* RNGD_ENTSOURCE__H */
diff -uNr rng-tools-2-orig/rngd.h rng-tools-2/rngd.h
--- rng-tools-2-orig/rngd.h	2004-08-24 23:23:04.000000000 +0530
+++ rng-tools-2/rngd.h	2008-11-11 15:39:31.000000000 +0530
@@ -42,6 +42,7 @@
 	double poll_timeout;
 
 	int daemon;
+	int no_tpm;
 };
 extern struct arguments *arguments;