Updated FIPS140 initialization and added a self test for it.
[gnutls:gnutls.git] / lib / fips.h
1 /*
2  * Copyright (C) 2013 Red Hat
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>
20  *
21  */
22
23 #ifndef FIPS_H
24 # define FIPS_H
25
26 #include <gnutls_int.h>
27 #include <gnutls/gnutls.h>
28
29 typedef enum {
30   FIPS_STATE_POWERON,
31   FIPS_STATE_INIT,
32   FIPS_STATE_SELFTEST,
33   FIPS_STATE_OPERATIONAL,
34   FIPS_STATE_ERROR,
35   FIPS_STATE_SHUTDOWN
36 } gnutls_fips_state_t;
37
38 #ifdef ENABLE_FIPS140
39
40 /* do not access directly */
41 extern unsigned int _gnutls_fips_mode;
42
43 inline static 
44 void _gnutls_switch_fips_state(gnutls_fips_state_t state)
45 {
46         _gnutls_fips_mode = state;
47 }
48
49 inline static gnutls_fips_state_t _gnutls_get_fips_state(void)
50 {
51         return _gnutls_fips_mode;
52 }
53
54 int _gnutls_fips_perform_self_checks(void);
55 unsigned _gnutls_fips_mode_enabled(void);
56
57 # define FAIL_IF_FIPS_ERROR \
58         if (_gnutls_get_fips_state() != FIPS_STATE_OPERATIONAL && \
59           _gnutls_get_fips_state() != FIPS_STATE_SELFTEST) return GNUTLS_E_LIB_IN_ERROR_STATE
60
61 void _gnutls_switch_fips_state(gnutls_fips_state_t state);
62
63 void _gnutls_fips140_simulate_error(void);
64
65 #else
66
67 # define _gnutls_switch_fips_state(x) 0
68 # define _gnutls_get_fips_state() STATE_OPERATIONAL
69 # define FAIL_IF_FIPS_ERROR 0
70 # define _gnutls_fips_perform_self_checks() 0
71 # define _gnutls_fips_mode_enabled() 0
72 #endif
73
74 #endif /* FIPS_H */