1 /* fips.c - FIPS mode management
2 * Copyright (C) 2008 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt 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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
26 /* #include <dlfcn.h> /\* FIXME: GNU only *\/ */
30 #include "cipher-proto.h"
32 /* The states of the finite state machine used in fips mode. */
35 /* POWEROFF cannot be represented. */
46 /* Flag telling whether we are in fips mode. It uses inverse logic so
47 that fips mode is the default unless changed by the intialization
48 code. To check whether fips mode is enabled, use the function
50 static int no_fips_mode_required;
52 /* This is the lock we use to protect the FSM. */
53 static ath_mutex_t fsm_lock = ATH_MUTEX_INITIALIZER;
55 /* The current state of the FSM. The whole state machinery is only
56 used while in fips mode. Change this only while holding fsm_lock. */
57 static enum module_states current_state;
62 static void fips_new_state (enum module_states new_state);
67 /* Check whether the OS is in FIPS mode and record that in a module
68 local variable. If FORCE is passed as true, fips mode will be
69 enabled anyway. Note: This function is not thread-safe and should
70 be called before any threads are created. This function may only
73 _gcry_initialize_fips_mode (int force)
78 /* Make sure we are not accidently called twice. */
83 fips_new_state (STATE_FATALERROR);
86 /* If not in fips mode an assert is sufficient. */
91 /* If the calling applicatione explicitly requested fipsmode, do so. */
94 gcry_assert (!no_fips_mode_required);
98 /* For testing the system it is useful to override the system
99 provided detection of the FIPS mode and force FIPS mode using a
100 file. The filename is hardwired so that there won't be any
101 confusion on whether /etc/gcrypt/ or /usr/local/etc/gcrypt/ is
102 actually used. The file itself may be empty. A comment may be
103 included in the file, but comment lines need to be prefixed with
104 a hash mark; only such comment lines and empty lines are
106 if ( !access ("/etc/gcrypt/fips140.force", F_OK) )
108 gcry_assert (!no_fips_mode_required);
112 /* Checking based on /proc file properties. */
117 fp = fopen ("/proc/fips140", "r");
122 if (fgets (line, sizeof line, fp) && atoi (line) == 1)
124 /* System is in fips mode. */
126 gcry_assert (!no_fips_mode_required);
131 else if ((saved_errno = errno) != ENOENT
132 && !access ("/proc/version", F_OK) )
134 /* Problem reading the fips file despite that we have the proc
135 file system. We better stop right away. */
136 log_info ("FATAL: error reading `%s' in libgcrypt: %s\n",
137 "/proc/fips140", strerror (saved_errno));
142 /* Fips not not requested, set flag. */
143 no_fips_mode_required = 1;
146 if (!no_fips_mode_required)
148 /* Yes, we are in FIPS mode. */
150 /* Intitialize the lock to protect the FSM. */
151 err = ath_mutex_init (&fsm_lock);
154 /* If that fails we can't do anything but abort the
155 process. We need to use log_info so that the FSM won't
157 log_info ("FATAL: failed to create the FSM lock in libgcrypt: %s\n",
162 /* Now get us into the INIT state. */
163 fips_new_state (STATE_INIT);
174 err = ath_mutex_lock (&fsm_lock);
177 log_info ("FATAL: failed to acquire the FSM lock in libgrypt: %s\n",
188 err = ath_mutex_unlock (&fsm_lock);
191 log_info ("FATAL: failed to release the FSM lock in libgrypt: %s\n",
198 /* This function returns true if fips mode is enabled. This is
199 independent of the fips required finite state machine and only used
200 to enable run fips specific code. Please use the fips_mode macro
201 instead of calling this fucntion directly. */
203 _gcry_fips_mode (void)
205 /* No locking is required becuase we have the requirement that this
206 variable is only intialized once with no other threads
208 return !no_fips_mode_required;
213 state2str (enum module_states state)
219 case STATE_POWERON: s = "Power-On"; break;
220 case STATE_INIT: s = "Init"; break;
221 case STATE_SELFTEST: s = "Self-Test"; break;
222 case STATE_OPERATIONAL: s = "Operational"; break;
223 case STATE_ERROR: s = "Error"; break;
224 case STATE_FATALERROR: s = "Fatal-Error"; break;
225 case STATE_SHUTDOWN: s = "Shutdown"; break;
226 default: s = "?"; break;
232 /* Return true if the library is in the operational state. */
234 _gcry_fips_is_operational (void)
243 if (current_state == STATE_INIT)
245 /* If we are still in the INIT state, we need to run the
246 selftests so that the FSM can eventually get into
247 operational state. Given that we would need a 2-phase
248 initialization of libgcrypt, but that has traditionally
249 not been enforced, we use this on demand self-test
250 checking. Note that Proper applications would do the
251 application specific libgcrypt initialization between a
252 gcry_check_version() and gcry_control
253 (GCRYCTL_INITIALIZATION_FINISHED) where the latter will
254 run the selftests. The drawback of these on-demand
255 self-tests are a small chance that self-tests are
256 performed by severeal threads; that is no problem because
257 our FSM make sure that we won't oversee any error. */
259 _gcry_fips_run_selftests ();
263 result = (current_state == STATE_OPERATIONAL);
270 /* This is test on wether the library is in the operational state. In
271 contrast to _gcry_fips_is_operational this function won't do a
272 state transition on the fly. */
274 _gcry_fips_test_operational (void)
283 result = (current_state == STATE_OPERATIONAL);
291 reporter (const char *domain, int algo, const char *what, const char *errtxt)
293 log_info ("libgcrypt selftest: %s %s%s (%d): %s%s%s%s\n",
294 !strcmp (domain, "hmac")? "digest":domain,
295 !strcmp (domain, "hmac")? "HMAC-":"",
296 !strcmp (domain, "cipher")? _gcry_cipher_algo_name (algo) :
297 !strcmp (domain, "digest")? _gcry_md_algo_name (algo) :
298 !strcmp (domain, "hmac")? _gcry_md_algo_name (algo) :
299 !strcmp (domain, "pubkey")? _gcry_pk_algo_name (algo) : "",
300 algo, errtxt? errtxt:"Okay",
301 what?" (":"", what? what:"", what?")":"");
304 /* Run self-tests for all required cipher algorithms. Return 0 on
307 run_cipher_selftests (void)
321 for (idx=0; algos[idx]; idx++)
323 err = _gcry_cipher_selftest (algos[idx], reporter);
324 reporter ("cipher", algos[idx], NULL,
325 err? gpg_strerror (err):NULL);
333 /* Run self-tests for all required hash algorithms. Return 0 on
336 run_digest_selftests (void)
351 for (idx=0; algos[idx]; idx++)
353 err = _gcry_md_selftest (algos[idx], reporter);
354 reporter ("digest", algos[idx], NULL,
355 err? gpg_strerror (err):NULL);
363 /* Run self-tests for all HMAC algorithms. Return 0 on success. */
365 run_hmac_selftests (void)
380 for (idx=0; algos[idx]; idx++)
382 err = _gcry_hmac_selftest (algos[idx], reporter);
383 reporter ("hmac", algos[idx], NULL,
384 err? gpg_strerror (err):NULL);
392 /* Run self-tests for all required public key algorithms. Return 0 on
395 run_pubkey_selftests (void)
401 /* GCRY_PK_ECDSA is not enabled in fips mode. */
408 for (idx=0; algos[idx]; idx++)
410 err = _gcry_pk_selftest (algos[idx], reporter);
411 reporter ("pubkey", algos[idx], NULL,
412 err? gpg_strerror (err):NULL);
420 /* Run self-tests for the random number generator. Return 0 on
423 run_random_selftests (void)
430 /* Run the self-tests. */
432 _gcry_fips_run_selftests (void)
434 enum module_states result = STATE_ERROR;
436 fips_new_state (STATE_SELFTEST);
441 /* if (dladdr ("gcry_check_version", &info)) */
442 /* log_info ("DL_info: fname=`%s'\n", */
443 /* info.dli_fname); */
447 if (run_cipher_selftests ())
450 if (run_digest_selftests ())
453 if (run_hmac_selftests ())
456 if (run_pubkey_selftests ())
459 if (run_random_selftests ())
462 /* All selftests passed. */
463 result = STATE_OPERATIONAL;
466 fips_new_state (result);
470 /* This function is used to tell the FSM about errors in the library.
471 The FSM will be put into an error state. This function should not
472 be called directly but by one of the macros
474 fips_signal_error (description)
475 fips_signal_fatal_error (description)
477 where DESCRIPTION is a string describing the error. */
479 _gcry_fips_signal_error (const char *srcfile, int srcline, const char *srcfunc,
480 int is_fatal, const char *description)
483 return; /* Not required. */
485 /* Set new state before printing an error. */
486 fips_new_state (is_fatal? STATE_FATALERROR : STATE_ERROR);
489 log_info ("%serror in libgcrypt, file %s, line %d%s%s: %s\n",
490 is_fatal? "fatal ":"",
492 srcfunc? ", function ":"", srcfunc? srcfunc:"",
493 description? description : "no description available");
497 /* Perform a state transition to NEW_STATE. If this is an invalid
498 transition, the module will go into a fatal error state. */
500 fips_new_state (enum module_states new_state)
503 enum module_states last_state;
507 last_state = current_state;
508 switch (current_state)
511 if (new_state == STATE_INIT
512 || new_state == STATE_ERROR
513 || new_state == STATE_FATALERROR)
518 if (new_state == STATE_SELFTEST )
523 if (new_state == STATE_OPERATIONAL
524 || new_state == STATE_ERROR
525 || new_state == STATE_FATALERROR)
529 case STATE_OPERATIONAL:
530 if (new_state == STATE_SHUTDOWN
531 || new_state == STATE_SELFTEST
532 || new_state == STATE_ERROR
533 || new_state == STATE_FATALERROR)
538 if (new_state == STATE_SHUTDOWN
539 || new_state == STATE_INIT)
543 case STATE_FATALERROR:
544 if (new_state == STATE_SHUTDOWN )
549 /* We won't see any transition *from* Shutdown because the only
550 allowed new state is Power-Off and that one can't be
558 current_state = new_state;
563 log_info ("libgcrypt state transition %s => %s %s\n",
564 state2str (last_state), state2str (new_state),
565 ok? "granted":"denied");
569 /* Invalid state transition. Halting library. */
577 /* This function should be called to ensure that the execution shall
580 _gcry_fips_noreturn (void)