summaryrefslogtreecommitdiff
path: root/tests/fips-mode-pthread.c
blob: 08b463bb26132ba9f9d103d4f38f1279e57e5b78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * 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, see <https://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include <signal.h>
#include <unistd.h>
#ifndef _WIN32
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <pthread.h>
#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 < MAX_THREADS; i++) {
		data[i].line = __LINE__;
		data[i].mode = mode;

		j = i % 3;
		if (j == 0)
			data[i].set_mode = GNUTLS_FIPS140_LAX;
		else if (j == 1)
			data[i].set_mode = GNUTLS_FIPS140_LOG;
		else
			data[i].set_mode = GNUTLS_FIPS140_STRICT;

		ret = pthread_create(&data[i].id, NULL, test_set_per_thread,
				     &data[i]);
		if (ret != 0) {
			abort();
		}
	}

	success("waiting for threads to finish\n");
	for (i = 0; i < MAX_THREADS; i++) {
		ret = pthread_join(data[i].id, NULL);
		if (ret != 0)
			abort();
	}

	success("checking main process mode\n");

	/* main thread should be in the same state */
	if (mode != gnutls_fips140_mode_enabled())
		fail("gnutls_fips140_mode_enabled: main thread changed mode (%d, exp: %d)\n",
		     gnutls_fips140_mode_enabled(), mode);

	success("checking whether global changes are seen in threads\n");
	/* Test if changes globally are visible in threads */
	mode = GNUTLS_FIPS140_LOG;
	gnutls_fips140_set_mode(mode, 0);

	for (i = 0; i < MAX_THREADS; i++) {
		data[i].line = __LINE__;
		data[i].mode = mode;
		data[i].set_mode = GNUTLS_FIPS140_LAX;
		ret = pthread_create(&data[i].id, NULL, test_set_per_thread,
				     &data[i]);
		if (ret != 0)
			abort();
	}

	success("waiting for threads to finish\n");
	for (i = 0; i < MAX_THREADS; i++) {
		ret = pthread_join(data[i].id, NULL);
		if (ret != 0)
			abort();
	}

	if (mode != gnutls_fips140_mode_enabled())
		fail("gnutls_fips140_mode_enabled: main thread changed mode (%d, exp: %d)\n",
		     gnutls_fips140_mode_enabled(), mode);

	gnutls_fips140_set_mode(GNUTLS_FIPS140_SELFTESTS, 0);
	if (GNUTLS_FIPS140_SELFTESTS == gnutls_fips140_mode_enabled())
		fail("gnutls_fips140_mode_enabled: setting to GNUTLS_FIPS140_SELFTESTS succeeded!\n");

	free(data);
}

#endif /* _WIN32 */