Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rbg.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * RBG mechanism
  23. *
  24. * This mechanism is designed to comply with ANS X9.82 Part 4 (April
  25. * 2011 Draft) Section 10. This standard is unfortunately not freely
  26. * available.
  27. *
  28. * The chosen RBG design is that of a DRBG with a live entropy source
  29. * with no conditioning function. Only a single security strength is
  30. * supported. No seedfile is used since there may be no non-volatile
  31. * storage available. The system UUID is used as the personalisation
  32. * string.
  33. */
  34. #include <stdint.h>
  35. #include <string.h>
  36. #include <ipxe/init.h>
  37. #include <ipxe/settings.h>
  38. #include <ipxe/uuid.h>
  39. #include <ipxe/crypto.h>
  40. #include <ipxe/drbg.h>
  41. #include <ipxe/rbg.h>
  42. /** The RBG */
  43. struct random_bit_generator rbg;
  44. /**
  45. * Start up RBG
  46. *
  47. * @ret rc Return status code
  48. *
  49. * This is the RBG_Startup function defined in ANS X9.82 Part 4 (April
  50. * 2011 Draft) Section 9.1.2.2.
  51. */
  52. static int rbg_startup ( void ) {
  53. union uuid uuid;
  54. int len;
  55. int rc;
  56. /* Try to obtain system UUID for use as personalisation
  57. * string, in accordance with ANS X9.82 Part 3-2007 Section
  58. * 8.5.2. If no UUID is available, proceed without a
  59. * personalisation string.
  60. */
  61. if ( ( len = fetch_uuid_setting ( NULL, &uuid_setting, &uuid ) ) < 0 ) {
  62. rc = len;
  63. DBGC ( &rbg, "RBG could not fetch personalisation string: "
  64. "%s\n", strerror ( rc ) );
  65. len = 0;
  66. }
  67. /* Instantiate DRBG */
  68. if ( ( rc = drbg_instantiate ( &rbg.state, &uuid, len ) ) != 0 ) {
  69. DBGC ( &rbg, "RBG could not instantiate DRBG: %s\n",
  70. strerror ( rc ) );
  71. return rc;
  72. }
  73. return 0;
  74. }
  75. /**
  76. * Shut down RBG
  77. *
  78. */
  79. static void rbg_shutdown ( void ) {
  80. /* Uninstantiate DRBG */
  81. drbg_uninstantiate ( &rbg.state );
  82. }
  83. /** RBG startup function */
  84. static void rbg_startup_fn ( void ) {
  85. /* Start up RBG. There is no way to report an error at this
  86. * stage, but a failed startup will result in an invalid DRBG
  87. * that refuses to generate bits.
  88. */
  89. rbg_startup();
  90. }
  91. /** RBG shutdown function */
  92. static void rbg_shutdown_fn ( int booting __unused ) {
  93. /* Shut down RBG */
  94. rbg_shutdown();
  95. }
  96. /** RBG startup table entry */
  97. struct startup_fn startup_rbg __startup_fn ( STARTUP_NORMAL ) = {
  98. .startup = rbg_startup_fn,
  99. .shutdown = rbg_shutdown_fn,
  100. };