0391d61d90c7e574fc5a1a726aba2c27d67bd0cc
[platform/upstream/cryptsetup.git] / lib / utils_fips.c
1 /*
2  * FIPS mode utilities
3  *
4  * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include "libcryptsetup.h"
24 #include "nls.h"
25 #include "utils_fips.h"
26
27 #if !ENABLE_FIPS
28 int crypt_fips_mode(void) { return 0; }
29 void crypt_fips_libcryptsetup_check(struct crypt_device *cd) {}
30 void crypt_fips_self_check(struct crypt_device *cd) {}
31 #else
32 #include <fipscheck.h>
33
34 int crypt_fips_mode(void)
35 {
36         return FIPSCHECK_kernel_fips_mode();
37 }
38
39 static void crypt_fips_verify(struct crypt_device *cd,
40                                const char *name, const char *function)
41 {
42         if (!crypt_fips_mode())
43                 return;
44
45         if (!FIPSCHECK_verify(name, function)) {
46                 crypt_log(cd, CRYPT_LOG_ERROR, _("FIPS checksum verification failed.\n"));
47                 _exit(EXIT_FAILURE);
48         }
49
50         crypt_log(cd, CRYPT_LOG_VERBOSE, _("Running in FIPS mode.\n"));
51 }
52
53 void crypt_fips_libcryptsetup_check(struct crypt_device *cd)
54 {
55         crypt_fips_verify(cd, LIBCRYPTSETUP_VERSION_FIPS, "crypt_init");
56 }
57
58 void crypt_fips_self_check(struct crypt_device *cd)
59 {
60         crypt_fips_verify(cd, NULL, NULL);
61 }
62 #endif /* ENABLE_FIPS */