ProcSysKernelRandomUuidRandomSeed.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/seed/ProcSysKernelRandomUuidRandomSeed.h"
  16. #include "util/Identity.h"
  17. #include "util/Bits.h"
  18. #include "util/Hex.h"
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. /** Number of times to try each operation. */
  25. #define MAX_TRIES 10
  26. static int getUUID(uint64_t output[2])
  27. {
  28. uint8_t buffer[40] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  29. {
  30. int fd = -1;
  31. int tries = 0;
  32. while ((fd = open("/proc/sys/kernel/random/uuid", O_RDONLY, 0)) < 0) {
  33. if (++tries > MAX_TRIES || errno == ENOENT) {
  34. return -1;
  35. }
  36. sleep(1);
  37. }
  38. tries = 0;
  39. uint8_t* buff = (uint8_t*) buffer;
  40. int count = 37;
  41. while (count > 0) {
  42. int r = read(fd, buff, count);
  43. if (r < 1) {
  44. if (++tries > MAX_TRIES) {
  45. break;
  46. }
  47. sleep(1);
  48. continue;
  49. }
  50. buff += r;
  51. count -= r;
  52. }
  53. close(fd);
  54. if (count != 0) {
  55. return -1;
  56. }
  57. }
  58. // If it isn't in perfect form, fail.
  59. if (!(buffer[8] == '-'
  60. && buffer[13] == '-'
  61. && buffer[18] == '-'
  62. && buffer[23] == '-'
  63. && buffer[36] == '\n'))
  64. {
  65. return -1;
  66. }
  67. // fold back the last 4 characters into the locations of the dashes.
  68. buffer[8] = buffer[35];
  69. buffer[13] = buffer[34];
  70. buffer[18] = buffer[33];
  71. buffer[23] = buffer[32];
  72. buffer[32] = '\0';
  73. if (Hex_decode((uint8_t*)output, 16, buffer, 32) != 16) {
  74. return -1;
  75. }
  76. return 0;
  77. }
  78. static int get(struct RandomSeed* randomSeed, uint64_t output[8])
  79. {
  80. if (getUUID(output) || getUUID(output+2) || getUUID(output+4) || getUUID(output+6)) {
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. struct RandomSeed* ProcSysKernelRandomUuidRandomSeed_new(struct Allocator* alloc)
  86. {
  87. return Allocator_clone(alloc, (&(struct RandomSeed) {
  88. .get = get,
  89. .name = "/proc/sys/kernel/random/uuid (Linux)"
  90. }));
  91. }