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/efi/efi.h>
  28. #include <ipxe/efi/Protocol/Rng.h>
  29. /** @file
  30. *
  31. * EFI entropy source
  32. *
  33. */
  34. /** Random number generator protocol */
  35. static EFI_RNG_PROTOCOL *efirng;
  36. EFI_REQUEST_PROTOCOL ( EFI_RNG_PROTOCOL, &efirng );
  37. /** Minimum number of bytes to request from RNG
  38. *
  39. * The UEFI spec states (for no apparently good reason) that "When a
  40. * Deterministic Random Bit Generator (DRBG) is used on the output of
  41. * a (raw) entropy source, its security level must be at least 256
  42. * bits." The EDK2 codebase (mis)interprets this to mean that the
  43. * call to GetRNG() should fail if given a buffer less than 32 bytes.
  44. *
  45. * Incidentally, nothing in the EFI RNG protocol provides any way to
  46. * report the actual amount of entropy returned by GetRNG().
  47. */
  48. #define EFI_ENTROPY_RNG_LEN 32
  49. /** Time (in 100ns units) to delay waiting for timer tick
  50. *
  51. * In theory, UEFI allows us to specify a trigger time of zero to
  52. * simply wait for the next timer tick. In practice, specifying zero
  53. * seems to often return immediately, which produces almost no
  54. * entropy. Specify a delay of 1000ns to try to force an existent
  55. * delay.
  56. */
  57. #define EFI_ENTROPY_TRIGGER_TIME 10
  58. /** Event used to wait for timer tick */
  59. static EFI_EVENT tick;
  60. /**
  61. * Enable entropy gathering
  62. *
  63. * @ret rc Return status code
  64. */
  65. static int efi_entropy_enable ( void ) {
  66. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  67. EFI_STATUS efirc;
  68. int rc;
  69. DBGC ( &tick, "ENTROPY %s RNG protocol\n",
  70. ( efirng ? "has" : "has no" ) );
  71. /* Create timer tick event */
  72. if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
  73. &tick ) ) != 0 ) {
  74. rc = -EEFI ( efirc );
  75. DBGC ( &tick, "ENTROPY could not create event: %s\n",
  76. strerror ( rc ) );
  77. return rc;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * Disable entropy gathering
  83. *
  84. */
  85. static void efi_entropy_disable ( void ) {
  86. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  87. /* Close timer tick event */
  88. bs->CloseEvent ( tick );
  89. }
  90. /**
  91. * Wait for a timer tick
  92. *
  93. * @ret low TSC low-order bits, or negative error
  94. */
  95. static int efi_entropy_tick ( void ) {
  96. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  97. UINTN index;
  98. uint16_t low;
  99. uint32_t discard_d;
  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 TSC low-order bits */
  117. __asm__ __volatile__ ( "rdtsc" : "=a" ( low ), "=d" ( discard_d ) );
  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 );