You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_entropy.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2015 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 (at your option) 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. * 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.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <errno.h>
  25. #include <ipxe/entropy.h>
  26. #include <ipxe/crc32.h>
  27. #include <ipxe/profile.h>
  28. #include <ipxe/efi/efi.h>
  29. #include <ipxe/efi/Protocol/Rng.h>
  30. /** @file
  31. *
  32. * EFI entropy source
  33. *
  34. */
  35. /** Random number generator protocol */
  36. static EFI_RNG_PROTOCOL *efirng;
  37. EFI_REQUEST_PROTOCOL ( EFI_RNG_PROTOCOL, &efirng );
  38. /** Minimum number of bytes to request from RNG
  39. *
  40. * The UEFI spec states (for no apparently good reason) that "When a
  41. * Deterministic Random Bit Generator (DRBG) is used on the output of
  42. * a (raw) entropy source, its security level must be at least 256
  43. * bits." The EDK2 codebase (mis)interprets this to mean that the
  44. * call to GetRNG() should fail if given a buffer less than 32 bytes.
  45. *
  46. * Incidentally, nothing in the EFI RNG protocol provides any way to
  47. * report the actual amount of entropy returned by GetRNG().
  48. */
  49. #define EFI_ENTROPY_RNG_LEN 32
  50. /** Time (in 100ns units) to delay waiting for timer tick
  51. *
  52. * In theory, UEFI allows us to specify a trigger time of zero to
  53. * simply wait for the next timer tick. In practice, specifying zero
  54. * seems to often return immediately, which produces almost no
  55. * entropy. Specify a delay of 1000ns to try to force an existent
  56. * delay.
  57. */
  58. #define EFI_ENTROPY_TRIGGER_TIME 10
  59. /** Event used to wait for timer tick */
  60. static EFI_EVENT tick;
  61. /**
  62. * Enable entropy gathering
  63. *
  64. * @ret rc Return status code
  65. */
  66. static int efi_entropy_enable ( void ) {
  67. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  68. EFI_STATUS efirc;
  69. int rc;
  70. DBGC ( &tick, "ENTROPY %s RNG protocol\n",
  71. ( efirng ? "has" : "has no" ) );
  72. /* Create timer tick event */
  73. if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
  74. &tick ) ) != 0 ) {
  75. rc = -EEFI ( efirc );
  76. DBGC ( &tick, "ENTROPY could not create event: %s\n",
  77. strerror ( rc ) );
  78. return rc;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * Disable entropy gathering
  84. *
  85. */
  86. static void efi_entropy_disable ( void ) {
  87. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  88. /* Close timer tick event */
  89. bs->CloseEvent ( tick );
  90. }
  91. /**
  92. * Wait for a timer tick
  93. *
  94. * @ret low CPU profiling low-order bits, or negative error
  95. */
  96. static int efi_entropy_tick ( void ) {
  97. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  98. UINTN index;
  99. uint16_t low;
  100. EFI_STATUS efirc;
  101. int rc;
  102. /* Wait for next timer tick */
  103. if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
  104. EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
  105. rc = -EEFI ( efirc );
  106. DBGC ( &tick, "ENTROPY could not set timer: %s\n",
  107. strerror ( rc ) );
  108. return rc;
  109. }
  110. if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
  111. rc = -EEFI ( efirc );
  112. DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
  113. strerror ( rc ) );
  114. return rc;
  115. }
  116. /* Get current CPU profiling timestamp low-order bits */
  117. low = profile_timestamp();
  118. return low;
  119. }
  120. /**
  121. * Get noise sample from timer ticks
  122. *
  123. * @ret noise Noise sample
  124. * @ret rc Return status code
  125. */
  126. static int efi_get_noise_ticks ( noise_sample_t *noise ) {
  127. int before;
  128. int after;
  129. int rc;
  130. /* Wait for a timer tick */
  131. before = efi_entropy_tick();
  132. if ( before < 0 ) {
  133. rc = before;
  134. return rc;
  135. }
  136. /* Wait for another timer tick */
  137. after = efi_entropy_tick();
  138. if ( after < 0 ) {
  139. rc = after;
  140. return rc;
  141. }
  142. /* Use TSC delta as noise sample */
  143. *noise = ( after - before );
  144. return 0;
  145. }
  146. /**
  147. * Get noise sample from RNG protocol
  148. *
  149. * @ret noise Noise sample
  150. * @ret rc Return status code
  151. */
  152. static int efi_get_noise_rng ( noise_sample_t *noise ) {
  153. uint8_t buf[EFI_ENTROPY_RNG_LEN];
  154. EFI_STATUS efirc;
  155. int rc;
  156. /* Fail if we have no EFI RNG protocol */
  157. if ( ! efirng )
  158. return -ENOTSUP;
  159. /* Get the minimum allowed number of random bytes */
  160. if ( ( efirc = efirng->GetRNG ( efirng, NULL, EFI_ENTROPY_RNG_LEN,
  161. buf ) ) != 0 ) {
  162. rc = -EEFI ( efirc );
  163. DBGC ( &tick, "ENTROPY could not read from RNG: %s\n",
  164. strerror ( rc ) );
  165. return rc;
  166. }
  167. /* Reduce random bytes to a single noise sample. This seems
  168. * like overkill, but we have no way of knowing how much
  169. * entropy is actually present in the bytes returned by the
  170. * RNG protocol.
  171. */
  172. *noise = crc32_le ( 0, buf, sizeof ( buf ) );
  173. return 0;
  174. }
  175. /**
  176. * Get noise sample
  177. *
  178. * @ret noise Noise sample
  179. * @ret rc Return status code
  180. */
  181. static int efi_get_noise ( noise_sample_t *noise ) {
  182. int rc;
  183. /* Try RNG first, falling back to timer ticks */
  184. if ( ( ( rc = efi_get_noise_rng ( noise ) ) != 0 ) &&
  185. ( ( rc = efi_get_noise_ticks ( noise ) ) != 0 ) )
  186. return rc;
  187. return 0;
  188. }
  189. PROVIDE_ENTROPY_INLINE ( efi, min_entropy_per_sample );
  190. PROVIDE_ENTROPY ( efi, entropy_enable, efi_entropy_enable );
  191. PROVIDE_ENTROPY ( efi, entropy_disable, efi_entropy_disable );
  192. PROVIDE_ENTROPY ( efi, get_noise, efi_get_noise );