random.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2016 Linaro Ltd; <[email protected]>
  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. efi_rng_protocol_t *rng = NULL;
  65. efi_status_t status;
  66. status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
  67. if (status != EFI_SUCCESS)
  68. return status;
  69. /*
  70. * Check whether a seed was provided by a prior boot stage. In that
  71. * case, instead of overwriting it, let's create a new buffer that can
  72. * hold both, and concatenate the existing and the new seeds.
  73. * Note that we should read the seed size with caution, in case the
  74. * table got corrupted in memory somehow.
  75. */
  76. prev_seed = get_efi_config_table(LINUX_EFI_RANDOM_SEED_TABLE_GUID);
  77. if (prev_seed && prev_seed->size <= 512U) {
  78. prev_seed_size = prev_seed->size;
  79. seed_size += prev_seed_size;
  80. }
  81. /*
  82. * Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the
  83. * allocation will survive a kexec reboot (although we refresh the seed
  84. * beforehand)
  85. */
  86. status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
  87. struct_size(seed, bits, seed_size),
  88. (void **)&seed);
  89. if (status != EFI_SUCCESS) {
  90. efi_warn("Failed to allocate memory for RNG seed.\n");
  91. goto err_warn;
  92. }
  93. status = efi_call_proto(rng, get_rng, &rng_algo_raw,
  94. EFI_RANDOM_SEED_SIZE, seed->bits);
  95. if (status == EFI_UNSUPPORTED)
  96. /*
  97. * Use whatever algorithm we have available if the raw algorithm
  98. * is not implemented.
  99. */
  100. status = efi_call_proto(rng, get_rng, NULL,
  101. EFI_RANDOM_SEED_SIZE, seed->bits);
  102. if (status != EFI_SUCCESS)
  103. goto err_freepool;
  104. seed->size = seed_size;
  105. if (prev_seed_size)
  106. memcpy(seed->bits + EFI_RANDOM_SEED_SIZE, prev_seed->bits,
  107. prev_seed_size);
  108. status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
  109. if (status != EFI_SUCCESS)
  110. goto err_freepool;
  111. if (prev_seed_size) {
  112. /* wipe and free the old seed if we managed to install the new one */
  113. memzero_explicit(prev_seed->bits, prev_seed_size);
  114. efi_bs_call(free_pool, prev_seed);
  115. }
  116. return EFI_SUCCESS;
  117. err_freepool:
  118. memzero_explicit(seed, struct_size(seed, bits, seed_size));
  119. efi_bs_call(free_pool, seed);
  120. efi_warn("Failed to obtain seed from EFI_RNG_PROTOCOL\n");
  121. err_warn:
  122. if (prev_seed)
  123. efi_warn("Retaining bootloader-supplied seed only");
  124. return status;
  125. }