1
0

LinuxRandomUuidSysctlRandomSeed.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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/LinuxRandomUuidSysctlRandomSeed.h"
  16. #include "util/Identity.h"
  17. #include "util/Bits.h"
  18. #include "util/Hex.h"
  19. #include <unistd.h>
  20. #include <sys/sysctl.h>
  21. static int getUUID(uint64_t output[2])
  22. {
  23. int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
  24. size_t sixteen = 16;
  25. Bits_memset(output, 0, 16);
  26. if (sysctl(mib, 3, output, &sixteen, NULL, 0)
  27. || Bits_isZero(output, 16))
  28. {
  29. return -1;
  30. }
  31. return 0;
  32. }
  33. static int get(struct RandomSeed* randomSeed, uint64_t output[8])
  34. {
  35. if (getUUID(output) || getUUID(output+2) || getUUID(output+4) || getUUID(output+6)) {
  36. return -1;
  37. }
  38. return 0;
  39. }
  40. struct RandomSeed* LinuxRandomUuidSysctlRandomSeed_new(struct Allocator* alloc)
  41. {
  42. return Allocator_clone(alloc, (&(struct RandomSeed) {
  43. .get = get,
  44. .name = "sysctl(RANDOM_UUID) (Linux)"
  45. }));
  46. }