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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /* Drop to TPL_APPLICATION to allow timer tick event to take place */
  73. bs->RestoreTPL ( TPL_APPLICATION );
  74. /* Create timer tick event */
  75. if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
  76. &tick ) ) != 0 ) {
  77. rc = -EEFI ( efirc );
  78. DBGC ( &tick, "ENTROPY could not create event: %s\n",
  79. strerror ( rc ) );
  80. return rc;
  81. }
  82. return 0;
  83. }
  84. /**
  85. * Disable entropy gathering
  86. *
  87. */
  88. static void efi_entropy_disable ( void ) {
  89. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  90. /* Close timer tick event */
  91. bs->CloseEvent ( tick );
  92. /* Return to TPL_CALLBACK */
  93. bs->RaiseTPL ( TPL_CALLBACK );
  94. }
  95. /**
  96. * Wait for a timer tick
  97. *
  98. * @ret low CPU profiling low-order bits, or negative error
  99. */
  100. static int efi_entropy_tick ( void ) {
  101. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  102. UINTN index;
  103. uint16_t low;
  104. EFI_STATUS efirc;
  105. int rc;
  106. /* Wait for next timer tick */
  107. if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
  108. EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
  109. rc = -EEFI ( efirc );
  110. DBGC ( &tick, "ENTROPY could not set timer: %s\n",
  111. strerror ( rc ) );
  112. return rc;
  113. }
  114. if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
  115. rc = -EEFI ( efirc );
  116. DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
  117. strerror ( rc ) );
  118. return rc;
  119. }
  120. /* Get current CPU profiling timestamp low-order bits */
  121. low = profile_timestamp();
  122. return low;
  123. }
  124. /**
  125. * Get noise sample from timer ticks
  126. *
  127. * @ret noise Noise sample
  128. * @ret rc Return status code
  129. */
  130. static int efi_get_noise_ticks ( noise_sample_t *noise ) {
  131. int before;
  132. int after;
  133. int rc;
  134. /* Wait for a timer tick */
  135. before = efi_entropy_tick();
  136. if ( before < 0 ) {
  137. rc = before;
  138. return rc;
  139. }
  140. /* Wait for another timer tick */
  141. after = efi_entropy_tick();
  142. if ( after < 0 ) {
  143. rc = after;
  144. return rc;
  145. }
  146. /* Use TSC delta as noise sample */
  147. *noise = ( after - before );
  148. return 0;
  149. }
  150. /**
  151. * Get noise sample from RNG protocol
  152. *
  153. * @ret noise Noise sample
  154. * @ret rc Return status code
  155. */
  156. static int efi_get_noise_rng ( noise_sample_t *noise ) {
  157. uint8_t buf[EFI_ENTROPY_RNG_LEN];
  158. EFI_STATUS efirc;
  159. int rc;
  160. /* Fail if we have no EFI RNG protocol */
  161. if ( ! efirng )
  162. return -ENOTSUP;
  163. /* Get the minimum allowed number of random bytes */
  164. if ( ( efirc = efirng->GetRNG ( efirng, NULL, EFI_ENTROPY_RNG_LEN,
  165. buf ) ) != 0 ) {
  166. rc = -EEFI ( efirc );
  167. DBGC ( &tick, "ENTROPY could not read from RNG: %s\n",
  168. strerror ( rc ) );
  169. return rc;
  170. }
  171. /* Reduce random bytes to a single noise sample. This seems
  172. * like overkill, but we have no way of knowing how much
  173. * entropy is actually present in the bytes returned by the
  174. * RNG protocol.
  175. */
  176. *noise = crc32_le ( 0, buf, sizeof ( buf ) );
  177. return 0;
  178. }
  179. /**
  180. * Get noise sample
  181. *
  182. * @ret noise Noise sample
  183. * @ret rc Return status code
  184. */
  185. static int efi_get_noise ( noise_sample_t *noise ) {
  186. int rc;
  187. /* Try RNG first, falling back to timer ticks */
  188. if ( ( ( rc = efi_get_noise_rng ( noise ) ) != 0 ) &&
  189. ( ( rc = efi_get_noise_ticks ( noise ) ) != 0 ) )
  190. return rc;
  191. return 0;
  192. }
  193. PROVIDE_ENTROPY_INLINE ( efi, min_entropy_per_sample );
  194. PROVIDE_ENTROPY ( efi, entropy_enable, efi_entropy_enable );
  195. PROVIDE_ENTROPY ( efi, entropy_disable, efi_entropy_disable );
  196. PROVIDE_ENTROPY ( efi, get_noise, efi_get_noise );