2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * Linux entropy source
34 #include <linux_api.h>
35 #include <ipxe/entropy.h>
37 /** Entropy source filename */
38 static const char entropy_filename[] = "/dev/random";
40 /** Entropy source file handle */
41 static int entropy_fd;
44 * Enable entropy gathering
46 * @ret rc Return status code
48 static int linux_entropy_enable ( void ) {
50 /* Open entropy source */
51 entropy_fd = linux_open ( entropy_filename, O_RDONLY );
52 if ( entropy_fd < 0 ) {
53 DBGC ( &entropy_fd, "ENTROPY could not open %s: %s\n",
54 entropy_filename, linux_strerror ( linux_errno ) );
62 * Disable entropy gathering
65 static void linux_entropy_disable ( void ) {
67 /* Close entropy source */
68 linux_close ( entropy_fd );
74 * @ret noise Noise sample
75 * @ret rc Return status code
77 static int linux_get_noise ( noise_sample_t *noise ) {
81 /* Read a single byte from entropy source */
82 len = linux_read ( entropy_fd, &byte, sizeof ( byte ) );
84 DBGC ( &entropy_fd, "ENTROPY could not read from %s: %s\n",
85 entropy_filename, linux_strerror ( linux_errno ) );
89 DBGC ( &entropy_fd, "ENTROPY EOF on reading from %s: %s\n",
90 entropy_filename, linux_strerror ( linux_errno ) );
98 PROVIDE_ENTROPY_INLINE ( linux, min_entropy_per_sample );
99 PROVIDE_ENTROPY ( linux, entropy_enable, linux_entropy_enable );
100 PROVIDE_ENTROPY ( linux, entropy_disable, linux_entropy_disable );
101 PROVIDE_ENTROPY ( linux, get_noise, linux_get_noise );