|
@@ -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 );
|