random.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Linaro Ltd; <ard.biesheuvel@linaro.org>
  4. */
  5. #include <linux/efi.h>
  6. #include <asm/efi.h>
  7. #include "efistub.h"
  8. typedef union efi_rng_protocol efi_rng_protocol_t;
  9. union efi_rng_protocol {
  10. struct {
  11. efi_status_t (__efiapi *get_info)(efi_rng_protocol_t *,
  12. unsigned long *,
  13. efi_guid_t *);
  14. efi_status_t (__efiapi *get_rng)(efi_rng_protocol_t *,
  15. efi_guid_t *, unsigned long,
  16. u8 *out);
  17. };
  18. struct {
  19. u32 get_info;
  20. u32 get_rng;
  21. } mixed_mode;
  22. };
  23. /**
  24. * efi_get_random_bytes() - fill a buffer with random bytes
  25. * @size: size of the buffer
  26. * @out: caller allocated buffer to receive the random bytes
  27. *
  28. * The call will fail if either the firmware does not implement the
  29. * EFI_RNG_PROTOCOL or there are not enough random bytes available to fill
  30. * the buffer.
  31. *
  32. * Return: status code
  33. */
  34. efi_status_t efi_get_random_bytes(unsigned long size, u8 *out)
  35. {
  36. efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
  37. efi_status_t status;
  38. efi_rng_protocol_t *rng = NULL;
  39. status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
  40. if (status != EFI_SUCCESS)
  41. return status;
  42. return efi_call_proto(rng, get_rng, NULL, size, out);
  43. }
  44. /**
  45. * efi_random_get_seed() - provide random seed as configuration table
  46. *
  47. * The EFI_RNG_PROTOCOL is used to read random bytes. These random bytes are
  48. * saved as a configuration table which can be used as entropy by the kernel
  49. * for the initialization of its pseudo random number generator.
  50. *
  51. * If the EFI_RNG_PROTOCOL is not available or there are not enough random bytes
  52. * available, the configuration table will not be installed and an error code
  53. * will be returned.
  54. *
  55. * Return: status code
  56. */
  57. efi_status_t efi_random_get_seed(void)
  58. {
  59. efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
  60. efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
  61. efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
  62. struct linux_efi_random_seed *prev_seed, *seed = NULL;
  63. int prev_seed_size = 0, seed_size = EFI_RANDOM_SEED_SIZE;
  64. unsigned long nv_seed_size = 0, offset = 0;
  65. efi_rng_protocol_t *rng = NULL;
  66. efi_status_t status;
  67. status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
  68. if (status != EFI_SUCCESS)
  69. seed_size = 0;
  70. // Call GetVariable() with a zero length buffer to obtain the size
  71. get_efi_var(L"RandomSeed", &rng_table_guid, NULL, &nv_seed_size, NULL);
  72. if (!seed_size && !nv_seed_size)
  73. return status;
  74. seed_size += nv_seed_size;
  75. /*
  76. * Check whether a seed was provided by a prior boot stage. In that
  77. * case, instead of overwriting it, let's create a new buffer that can
  78. * hold both, and concatenate the existing and the new seeds.
  79. * Note that we should read the seed size with caution, in case the
  80. * table got corrupted in memory somehow.
  81. */
  82. prev_seed = get_efi_config_table(rng_table_guid);
  83. if (prev_seed && prev_seed->size <= 512U) {
  84. prev_seed_size = prev_seed->size;
  85. seed_size += prev_seed_size;
  86. }
  87. /*
  88. * Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the
  89. * allocation will survive a kexec reboot (although we refresh the seed
  90. * beforehand)
  91. */
  92. status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
  93. struct_size(seed, bits, seed_size),
  94. (void **)&seed);
  95. if (status != EFI_SUCCESS) {
  96. efi_warn("Failed to allocate memory for RNG seed.\n");
  97. goto err_warn;
  98. }
  99. if (rng) {
  100. status = efi_call_proto(rng, get_rng, &rng_algo_raw,
  101. EFI_RANDOM_SEED_SIZE, seed->bits);
  102. if (status == EFI_UNSUPPORTED)
  103. /*
  104. * Use whatever algorithm we have available if the raw algorithm
  105. * is not implemented.
  106. */
  107. status = efi_call_proto(rng, get_rng, NULL,
  108. EFI_RANDOM_SEED_SIZE, seed->bits);
  109. if (status == EFI_SUCCESS)
  110. offset = EFI_RANDOM_SEED_SIZE;
  111. }
  112. if (nv_seed_size) {
  113. status = get_efi_var(L"RandomSeed", &rng_table_guid, NULL,
  114. &nv_seed_size, seed->bits + offset);
  115. if (status == EFI_SUCCESS)
  116. /*
  117. * We delete the seed here, and /hope/ that this causes
  118. * EFI to also zero out its representation on disk.
  119. * This is somewhat idealistic, but overwriting the
  120. * variable with zeros is likely just as fraught too.
  121. * TODO: in the future, maybe we can hash it forward
  122. * instead, and write a new seed.
  123. */
  124. status = set_efi_var(L"RandomSeed", &rng_table_guid, 0,
  125. 0, NULL);
  126. if (status == EFI_SUCCESS)
  127. offset += nv_seed_size;
  128. else
  129. memzero_explicit(seed->bits + offset, nv_seed_size);
  130. }
  131. if (!offset)
  132. goto err_freepool;
  133. if (prev_seed_size) {
  134. memcpy(seed->bits + offset, prev_seed->bits, prev_seed_size);
  135. offset += prev_seed_size;
  136. }
  137. seed->size = offset;
  138. status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
  139. if (status != EFI_SUCCESS)
  140. goto err_freepool;
  141. if (prev_seed_size) {
  142. /* wipe and free the old seed if we managed to install the new one */
  143. memzero_explicit(prev_seed->bits, prev_seed_size);
  144. efi_bs_call(free_pool, prev_seed);
  145. }
  146. return EFI_SUCCESS;
  147. err_freepool:
  148. memzero_explicit(seed, struct_size(seed, bits, seed_size));
  149. efi_bs_call(free_pool, seed);
  150. efi_warn("Failed to obtain seed from EFI_RNG_PROTOCOL or EFI variable\n");
  151. err_warn:
  152. if (prev_seed)
  153. efi_warn("Retaining bootloader-supplied seed only");
  154. return status;
  155. }