Better use _exit() on checksum fail.
[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  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <stdlib.h>
21 #include "libcryptsetup.h"
22 #include "nls.h"
23 #include "utils_fips.h"
24 #include "config.h"
25
26 #if !ENABLE_FIPS
27 int crypt_fips_mode(void) { return 0; }
28 void crypt_fips_libcryptsetup_check(struct crypt_device *cd) {}
29 void crypt_fips_self_check(struct crypt_device *cd) {}
30 #else
31 #include <fipscheck.h>
32
33 int crypt_fips_mode(void)
34 {
35         return FIPSCHECK_kernel_fips_mode();
36 }
37
38 static void crypt_fips_verify(struct crypt_device *cd,
39                                const char *name, const char *function)
40 {
41         if (!crypt_fips_mode())
42                 return;
43
44         if (!FIPSCHECK_verify(name, function)) {
45                 crypt_log(cd, CRYPT_LOG_ERROR, _("FIPS checksum verification failed.\n"));
46                 _exit(EXIT_FAILURE);
47         }
48
49         crypt_log(cd, CRYPT_LOG_VERBOSE, _("Running in FIPS mode.\n"));
50 }
51
52 void crypt_fips_libcryptsetup_check(struct crypt_device *cd)
53 {
54         crypt_fips_verify(cd, "libcryptsetup.so", "crypt_init");
55 }
56
57 void crypt_fips_self_check(struct crypt_device *cd)
58 {
59         crypt_fips_verify(cd, NULL, NULL);
60 }
61 #endif /* ENABLE_FIPS */