fips-private.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* FIPS compliance status test for GNU/Linux systems.
  2. Copyright (C) 2012-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _FIPS_PRIVATE_H
  16. #define _FIPS_PRIVATE_H
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <not-cancel.h>
  22. #include <stdbool.h>
  23. /* Return true if FIPS mode is enabled. See
  24. sysdeps/generic/fips-private.h for more information. */
  25. static bool
  26. fips_enabled_p (void)
  27. {
  28. static enum
  29. {
  30. FIPS_UNTESTED = 0,
  31. FIPS_ENABLED = 1,
  32. FIPS_DISABLED = -1,
  33. FIPS_TEST_FAILED = -2
  34. } checked;
  35. if (checked == FIPS_UNTESTED)
  36. {
  37. int fd = __open_nocancel ("/proc/sys/crypto/fips_enabled", O_RDONLY);
  38. if (fd != -1)
  39. {
  40. /* This is more than enough, the file contains a single integer. */
  41. char buf[32];
  42. ssize_t n;
  43. n = TEMP_FAILURE_RETRY (__read_nocancel (fd, buf, sizeof (buf) - 1));
  44. __close_nocancel_nostatus (fd);
  45. if (n > 0)
  46. {
  47. /* Terminate the string. */
  48. buf[n] = '\0';
  49. char *endp;
  50. long int res = strtol (buf, &endp, 10);
  51. if (endp != buf && (*endp == '\0' || *endp == '\n'))
  52. checked = (res > 0) ? FIPS_ENABLED : FIPS_DISABLED;
  53. }
  54. }
  55. if (checked == FIPS_UNTESTED)
  56. checked = FIPS_TEST_FAILED;
  57. }
  58. return checked == FIPS_ENABLED;
  59. }
  60. #endif /* _FIPS_PRIVATE_H */