Browse Source

[rng] Add Linux entropy source using /dev/random

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
5af9e62196

+ 1
- 1
src/config/defaults/linux.h View File

@@ -14,7 +14,7 @@
14 14
 #define NAP_LINUX
15 15
 #define SMBIOS_LINUX
16 16
 #define SANBOOT_NULL
17
-#define ENTROPY_NULL
17
+#define ENTROPY_LINUX
18 18
 
19 19
 #define DRIVERS_LINUX
20 20
 

+ 1
- 0
src/include/ipxe/entropy.h View File

@@ -54,6 +54,7 @@ typedef uint8_t entropy_sample_t;
54 54
 
55 55
 /* Include all architecture-independent entropy API headers */
56 56
 #include <ipxe/null_entropy.h>
57
+#include <ipxe/linux/linux_entropy.h>
57 58
 
58 59
 /* Include all architecture-dependent entropy API headers */
59 60
 #include <bits/entropy.h>

+ 1
- 0
src/include/ipxe/errfile.h View File

@@ -247,6 +247,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
247 247
 #define ERRFILE_drbg		      ( ERRFILE_OTHER | 0x00250000 )
248 248
 #define ERRFILE_entropy		      ( ERRFILE_OTHER | 0x00260000 )
249 249
 #define ERRFILE_rsa		      ( ERRFILE_OTHER | 0x00270000 )
250
+#define ERRFILE_linux_entropy	      ( ERRFILE_OTHER | 0x00280000 )
250 251
 
251 252
 /** @} */
252 253
 

+ 32
- 0
src/include/ipxe/linux/linux_entropy.h View File

@@ -0,0 +1,32 @@
1
+#ifndef _IPXE_LINUX_ENTROPY_H
2
+#define _IPXE_LINUX_ENTROPY_H
3
+
4
+/** @file
5
+ *
6
+ * iPXE entropy API for linux
7
+ *
8
+ */
9
+
10
+FILE_LICENCE(GPL2_OR_LATER);
11
+
12
+#ifdef ENTROPY_LINUX
13
+#define ENTROPY_PREFIX_linux
14
+#else
15
+#define ENTROPY_PREFIX_linux __linux_
16
+#endif
17
+
18
+/**
19
+ * min-entropy per sample
20
+ *
21
+ * @ret min_entropy	min-entropy of each sample
22
+ */
23
+static inline __always_inline double
24
+ENTROPY_INLINE ( linux, min_entropy_per_sample ) ( void ) {
25
+
26
+	/* We read single bytes from /dev/random and assume that each
27
+	 * contains full entropy.
28
+	 */
29
+	return 8;
30
+}
31
+
32
+#endif /* _IPXE_LINUX_ENTROPY_H */

+ 96
- 0
src/interface/linux/linux_entropy.c View File

@@ -0,0 +1,96 @@
1
+/*
2
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/** @file
22
+ *
23
+ * Linux entropy source
24
+ *
25
+ */
26
+
27
+#include <stdint.h>
28
+#include <errno.h>
29
+#include <linux_api.h>
30
+#include <ipxe/entropy.h>
31
+
32
+/** Entropy source filename */
33
+static const char entropy_filename[] = "/dev/random";
34
+
35
+/** Entropy source file handle */
36
+static int entropy_fd;
37
+
38
+/**
39
+ * Enable entropy gathering
40
+ *
41
+ * @ret rc		Return status code
42
+ */
43
+static int linux_entropy_enable ( void ) {
44
+
45
+	/* Open entropy source */
46
+	entropy_fd = linux_open ( entropy_filename, O_RDONLY );
47
+	if ( entropy_fd < 0 ) {
48
+		DBGC ( &entropy_fd, "ENTROPY could not open %s: %s\n",
49
+		       entropy_filename, linux_strerror ( linux_errno ) );
50
+		return entropy_fd;
51
+	}
52
+
53
+	return 0;
54
+}
55
+
56
+/**
57
+ * Disable entropy gathering
58
+ *
59
+ */
60
+static void linux_entropy_disable ( void ) {
61
+
62
+	/* Close entropy source */
63
+	linux_close ( entropy_fd );
64
+}
65
+
66
+/**
67
+ * Get noise sample
68
+ *
69
+ * @ret noise		Noise sample
70
+ * @ret rc		Return status code
71
+ */
72
+static int linux_get_noise ( noise_sample_t *noise ) {
73
+	uint8_t byte;
74
+	ssize_t len;
75
+
76
+	/* Read a single byte from entropy source */
77
+	len = linux_read ( entropy_fd, &byte, sizeof ( byte ) );
78
+	if ( len < 0 ) {
79
+		DBGC ( &entropy_fd, "ENTROPY could not read from %s: %s\n",
80
+		       entropy_filename, linux_strerror ( linux_errno ) );
81
+		return len;
82
+	}
83
+	if ( len == 0 ) {
84
+		DBGC ( &entropy_fd, "ENTROPY EOF on reading from %s: %s\n",
85
+		       entropy_filename, linux_strerror ( linux_errno ) );
86
+		return -EPIPE;
87
+	}
88
+	*noise = byte;
89
+
90
+	return 0;
91
+}
92
+
93
+PROVIDE_ENTROPY_INLINE ( linux, min_entropy_per_sample );
94
+PROVIDE_ENTROPY ( linux, entropy_enable, linux_entropy_enable );
95
+PROVIDE_ENTROPY ( linux, entropy_disable, linux_entropy_disable );
96
+PROVIDE_ENTROPY ( linux, get_noise, linux_get_noise );

Loading…
Cancel
Save