/* * Copyright (C) 2017 Red Hat, Inc. * * Author: Nikos Mavrogiannopoulos * * This file is part of GnuTLS. * * GnuTLS is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GnuTLS is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GnuTLS; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include #ifndef _WIN32 # include # include # include # include # include #endif #include "utils.h" #ifdef _WIN32 void doit(void) { exit(77); } #else /* Tests whether we can use gnutls_fips140_set_mode() and * gnutls_fips140_mode_enabled() under multiple threads. */ typedef struct thread_data_st { unsigned mode; unsigned set_mode; int line; pthread_t id; } thread_data_st; static void *test_set_per_thread(void *arg) { thread_data_st *data = arg; unsigned mode; int ret; unsigned char digest[20]; mode = gnutls_fips140_mode_enabled(); if (mode != data->mode) fail("%d: gnutls_fips140_mode_enabled: wrong mode returned (%d, exp: %d)\n", data->line, mode, data->mode); if (data->set_mode) gnutls_fips140_set_mode(data->set_mode, GNUTLS_FIPS140_SET_MODE_THREAD); mode = gnutls_fips140_mode_enabled(); if (mode != data->set_mode) { fail("%d: gnutls_fips140_mode_enabled: wrong mode returned after set (%d, exp: %d)\n", data->line, mode, data->set_mode); } /* reset mode */ gnutls_fips140_set_mode(data->mode, GNUTLS_FIPS140_SET_MODE_THREAD); mode = gnutls_fips140_mode_enabled(); if (mode != data->mode) fail("%d: gnutls_fips140_mode_enabled: wrong mode returned after set (%d, exp: %d)\n", data->line, mode, data->mode); ret = gnutls_hmac_fast(GNUTLS_MAC_MD5, "keykeykey", 9, "abcdefgh", 8, digest); if (mode == GNUTLS_FIPS140_STRICT && ret >= 0) { fail("gnutls_hmac_fast(MD5): succeeded in strict mode!\n"); } else if (mode != GNUTLS_FIPS140_STRICT && ret < 0) { fail("gnutls_hmac_fast(MD5): failed in non-strict mode!\n"); } /* put to a random state */ gnutls_fips140_set_mode(data->set_mode, GNUTLS_FIPS140_SET_MODE_THREAD); pthread_exit(0); } #define MAX_THREADS 48 void doit(void) { int ret; thread_data_st *data; unsigned i, j; unsigned mode; signal(SIGPIPE, SIG_IGN); mode = gnutls_fips140_mode_enabled(); if (mode == 0) { success("We are not in FIPS140 mode\n"); exit(77); } data = calloc(1, sizeof(thread_data_st)*MAX_THREADS); if (data == NULL) abort(); success("starting threads\n"); /* Test if changes per thread apply, and whether the global * setting will remain the same */ for (i=0;i