random.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2016 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/random.h>
  19. #include <grub/dl.h>
  20. #include <grub/lib/hexdump.h>
  21. #include <grub/command.h>
  22. #include <grub/mm.h>
  23. GRUB_MOD_LICENSE ("GPLv3+");
  24. grub_err_t
  25. grub_crypto_get_random (void *buffer, grub_size_t sz)
  26. {
  27. /* This is an arbitrer between different methods.
  28. TODO: Add more methods in the future. */
  29. /* TODO: Add some PRNG smartness to reduce damage from bad entropy. */
  30. if (grub_crypto_arch_get_random (buffer, sz))
  31. return GRUB_ERR_NONE;
  32. return grub_error (GRUB_ERR_IO, "no random sources found");
  33. }
  34. static int
  35. get_num_digits (int val)
  36. {
  37. int ret = 0;
  38. while (val != 0)
  39. {
  40. ret++;
  41. val /= 10;
  42. }
  43. if (ret == 0)
  44. return 1;
  45. return ret;
  46. }
  47. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  48. static grub_err_t
  49. grub_cmd_hexdump_random (grub_command_t cmd __attribute__ ((unused)), int argc, char **args)
  50. {
  51. grub_size_t length = 64;
  52. grub_err_t err;
  53. void *buffer;
  54. grub_uint8_t *ptr;
  55. int stats[256];
  56. int i, digits = 2;
  57. char template[10];
  58. if (argc >= 1)
  59. length = grub_strtoull (args[0], 0, 0);
  60. if (length == 0)
  61. return grub_error (GRUB_ERR_BAD_ARGUMENT, "length pust be positive");
  62. buffer = grub_malloc (length);
  63. if (!buffer)
  64. return grub_errno;
  65. err = grub_crypto_get_random (buffer, length);
  66. if (err)
  67. {
  68. grub_free (buffer);
  69. return err;
  70. }
  71. hexdump (0, buffer, length);
  72. grub_memset(stats, 0, sizeof(stats));
  73. for (ptr = buffer; ptr < (grub_uint8_t *) buffer + length; ptr++)
  74. stats[*ptr]++;
  75. grub_printf ("Statistics:\n");
  76. for (i = 0; i < 256; i++)
  77. {
  78. int z = get_num_digits (stats[i]);
  79. if (z > digits)
  80. digits = z;
  81. }
  82. grub_snprintf (template, sizeof (template), "%%0%dd ", digits);
  83. for (i = 0; i < 256; i++)
  84. {
  85. grub_printf ("%s", template);//, stats[i]);
  86. if ((i & 0xf) == 0xf)
  87. grub_printf ("\n");
  88. }
  89. grub_free (buffer);
  90. return 0;
  91. }
  92. static grub_command_t cmd;
  93. GRUB_MOD_INIT (random)
  94. {
  95. cmd = grub_register_command ("hexdump_random", grub_cmd_hexdump_random,
  96. N_("[LENGTH]"),
  97. N_("Hexdump random data."));
  98. }
  99. GRUB_MOD_FINI (random)
  100. {
  101. grub_unregister_command (cmd);
  102. }