Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

linux_entropy.c 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Linux entropy source
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <errno.h>
  27. #include <linux_api.h>
  28. #include <ipxe/entropy.h>
  29. /** Entropy source filename */
  30. static const char entropy_filename[] = "/dev/random";
  31. /** Entropy source file handle */
  32. static int entropy_fd;
  33. /**
  34. * Enable entropy gathering
  35. *
  36. * @ret rc Return status code
  37. */
  38. static int linux_entropy_enable ( void ) {
  39. /* Open entropy source */
  40. entropy_fd = linux_open ( entropy_filename, O_RDONLY );
  41. if ( entropy_fd < 0 ) {
  42. DBGC ( &entropy_fd, "ENTROPY could not open %s: %s\n",
  43. entropy_filename, linux_strerror ( linux_errno ) );
  44. return entropy_fd;
  45. }
  46. return 0;
  47. }
  48. /**
  49. * Disable entropy gathering
  50. *
  51. */
  52. static void linux_entropy_disable ( void ) {
  53. /* Close entropy source */
  54. linux_close ( entropy_fd );
  55. }
  56. /**
  57. * Get noise sample
  58. *
  59. * @ret noise Noise sample
  60. * @ret rc Return status code
  61. */
  62. static int linux_get_noise ( noise_sample_t *noise ) {
  63. uint8_t byte;
  64. ssize_t len;
  65. /* Read a single byte from entropy source */
  66. len = linux_read ( entropy_fd, &byte, sizeof ( byte ) );
  67. if ( len < 0 ) {
  68. DBGC ( &entropy_fd, "ENTROPY could not read from %s: %s\n",
  69. entropy_filename, linux_strerror ( linux_errno ) );
  70. return len;
  71. }
  72. if ( len == 0 ) {
  73. DBGC ( &entropy_fd, "ENTROPY EOF on reading from %s: %s\n",
  74. entropy_filename, linux_strerror ( linux_errno ) );
  75. return -EPIPE;
  76. }
  77. *noise = byte;
  78. return 0;
  79. }
  80. PROVIDE_ENTROPY_INLINE ( linux, min_entropy_per_sample );
  81. PROVIDE_ENTROPY ( linux, entropy_enable, linux_entropy_enable );
  82. PROVIDE_ENTROPY ( linux, entropy_disable, linux_entropy_disable );
  83. PROVIDE_ENTROPY ( linux, get_noise, linux_get_noise );