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 2.9KB

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