Add fips_mode check for kernel.

Akso add a separate function so we can detect that kernel and crypto
lib is in different FIPS state (only for testing).
This commit is contained in:
Milan Broz
2025-11-13 21:56:05 +01:00
parent 7fba92260a
commit ccc0c69cd7
3 changed files with 24 additions and 1 deletions

View File

@@ -8,6 +8,8 @@
#include <errno.h> #include <errno.h>
#include <strings.h> #include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include "crypto_backend.h" #include "crypto_backend.h"
struct cipher_alg { struct cipher_alg {
@@ -77,3 +79,21 @@ int crypt_cipher_wrapped_key(const char *name, const char *mode)
return ca ? (int)ca->wrapped_key : 0; return ca ? (int)ca->wrapped_key : 0;
} }
bool crypt_fips_mode_kernel(void)
{
int fd;
char buf = 0;
fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
if (fd < 0)
return false;
if (read(fd, &buf, 1) != 1)
buf = '0';
close(fd);
return (buf == '1');
}

View File

@@ -148,6 +148,9 @@ int crypt_backend_memeq(const void *m1, const void *m2, size_t n);
/* crypto backend running in FIPS mode */ /* crypto backend running in FIPS mode */
bool crypt_fips_mode(void); bool crypt_fips_mode(void);
/* kernel running in FIPS mode */
bool crypt_fips_mode_kernel(void);
# ifdef __cplusplus # ifdef __cplusplus
} }
# endif # endif

View File

@@ -408,5 +408,5 @@ int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
bool crypt_fips_mode(void) bool crypt_fips_mode(void)
{ {
return false; return crypt_fips_mode_kernel();
} }