summaryrefslogtreecommitdiffstats
path: root/tests/fips-rsa-sizes.c
blob: 84b9affabbef085a8e13ed369c1a3085d28450ac (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (C) 2022 Red Hat, Inc.
 *
 * Author: Alexander Sosedkin
 *
 * 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
 */

#include <assert.h>
#include <stdio.h>
#include <utils.h>
#include <gnutls/gnutls.h>
#include <gnutls/abstract.h>
#include <gnutls/x509.h>

#define FIPS_PUSH_CONTEXT() do {				\
	ret = gnutls_fips140_push_context(fips_context);	\
	if (ret < 0) {						\
		fail("gnutls_fips140_push_context failed\n");	\
	}							\
} while (0)

#define FIPS_POP_CONTEXT(state) do {					\
	ret = gnutls_fips140_pop_context();				\
	if (ret < 0) {							\
		fail("gnutls_fips140_context_pop failed\n");		\
	}								\
	fips_state = gnutls_fips140_get_operation_state(fips_context);	\
	if (fips_state != GNUTLS_FIPS140_OP_ ## state) {		\
		fail("operation state is not " # state " (%d)\n",	\
		     fips_state);					\
	}								\
} while (0)


void generate_successfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
                           unsigned int size);
void generate_unsuccessfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
                             unsigned int size);
void sign_verify_successfully(gnutls_privkey_t privkey, gnutls_pubkey_t pubkey);
void sign_verify_unsuccessfully(gnutls_privkey_t privkey,
                                gnutls_pubkey_t pubkey);
void nosign_verify(gnutls_privkey_t privkey, gnutls_pubkey_t pubkey);


void generate_successfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
                           unsigned int size)
{
	int ret;
	gnutls_x509_privkey_t xprivkey;
	gnutls_fips140_context_t fips_context;
	gnutls_fips140_operation_state_t fips_state;
	assert(gnutls_fips140_context_init(&fips_context) == 0);

	fprintf(stderr, "%d-bit\n", size);

	/* x509 generation as well just because why not */
	FIPS_PUSH_CONTEXT();
	assert(gnutls_x509_privkey_init(&xprivkey) == 0);
	ret = gnutls_x509_privkey_generate(xprivkey, GNUTLS_PK_RSA, size, 0);
	if (ret != GNUTLS_E_SUCCESS)
		fail("%d-bit x509_privkey_init (%d)\n", size, ret);
	FIPS_POP_CONTEXT(APPROVED);
	gnutls_x509_privkey_deinit(xprivkey);

	FIPS_PUSH_CONTEXT();
	assert(gnutls_privkey_init(privkey) == 0);
	ret = gnutls_privkey_generate(*privkey, GNUTLS_PK_RSA, size, 0);
	if (ret != GNUTLS_E_SUCCESS)
		fail("%d-bit privkey_init (%d)\n", size, ret);
	FIPS_POP_CONTEXT(APPROVED);

	assert(gnutls_pubkey_init(pubkey) == 0);
	FIPS_PUSH_CONTEXT();
	ret = gnutls_pubkey_import_privkey(*pubkey, *privkey,
					   GNUTLS_KEY_DIGITAL_SIGNATURE, 0);
	if (ret != GNUTLS_E_SUCCESS)
		fail("%d-bit pubkey_import_privkey (%d)\n", size, ret);
	FIPS_POP_CONTEXT(INITIAL);

	gnutls_fips140_context_deinit(fips_context);
}


void generate_unsuccessfully(gnutls_privkey_t* privkey, gnutls_pubkey_t* pubkey,
                             unsigned int size)
{
	int ret;
	gnutls_x509_privkey_t xprivkey;
	gnutls_fips140_context_t fips_context;
	gnutls_fips140_operation_state_t fips_state;
	assert(gnutls_fips140_context_init(&fips_context) == 0);

	fprintf(stderr, "%d-bit\n", size);

	/* short x509 generation: ERROR, blocked */
	FIPS_PUSH_CONTEXT();
	assert(gnutls_x509_privkey_init(&xprivkey) == 0);
	ret = gnutls_x509_privkey_generate(xprivkey, GNUTLS_PK_RSA, size, 0);
	if (ret != GNUTLS_E_PK_GENERATION_ERROR)
		fail("%d-bit x509_privkey_init (%d)\n", size, ret);
	FIPS_POP_CONTEXT(ERROR);
	gnutls_x509_privkey_deinit(xprivkey);

	/* short key generation: ERROR, blocked */
	FIPS_PUSH_CONTEXT();
	assert(gnutls_privkey_init(privkey) == 0);
	ret = gnutls_privkey_generate(*privkey, GNUTLS_PK_RSA, size, 0);
	if (ret != GNUTLS_E_PK_GENERATION_ERROR)
		fail("%d-bit privkey_init (%d)\n", size, ret);
	FIPS_POP_CONTEXT(ERROR);
	gnutls_privkey_deinit(*privkey);

	/* Disable FIPS to generate them anyway */
	gnutls_fips140_set_mode(GNUTLS_FIPS140_LAX, 0);
	assert(gnutls_fips140_mode_enabled() == GNUTLS_FIPS140_LAX);

	assert(gnutls_x509_privkey_init(&xprivkey) == 0);
	ret = gnutls_x509_privkey_generate(xprivkey, GNUTLS_PK_RSA, size, 0);
	if (ret != GNUTLS_E_SUCCESS)
		fail("%d-bit x509_privkey_init (%d)\n", size, ret);
	gnutls_x509_privkey_deinit(xprivkey);

	assert(gnutls_privkey_init(privkey) == 0);
	ret = gnutls_privkey_generate(*privkey, GNUTLS_PK_RSA, size, 0);
	if (ret != GNUTLS_E_SUCCESS)
		fail("%d-bit privkey_init (%d)\n", size, ret);

	assert(gnutls_pubkey_init(pubkey) == 0);
	ret = gnutls_pubkey_import_privkey(*pubkey, *privkey,
					   GNUTLS_KEY_DIGITAL_SIGNATURE, 0);
	if (ret != GNUTLS_E_SUCCESS)
		fail("%d-bit pubkey_import_privkey (%d)\n", size, ret);

	gnutls_fips140_set_mode(GNUTLS_FIPS140_STRICT, 0);
	assert(gnutls_fips140_mode_enabled());

	gnutls_fips140_context_deinit(fips_context);
}


void sign_verify_successfully(gnutls_privkey_t privkey, gnutls_pubkey_t pubkey) {
	int ret;
	gnutls_fips140_context_t fips_context;
	gnutls_fips140_operation_state_t fips_state;

	gnutls_datum_t signature;
	gnutls_datum_t plaintext = {
		.data = (unsigned char* const) "Hello world!",
		.size = 12
	};
	assert(gnutls_fips140_context_init(&fips_context) == 0);

	/* RSA sign: approved */
	FIPS_PUSH_CONTEXT();
	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA256, 0,
	                               &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_privkey_sign_data failed\n");
	FIPS_POP_CONTEXT(APPROVED);

	/* RSA verify: approved */
	FIPS_PUSH_CONTEXT();
	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA256, 0,
	                                 &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_pubkey_verify_data2 failed\n");
	FIPS_POP_CONTEXT(APPROVED);

	gnutls_free(signature.data);
	gnutls_fips140_context_deinit(fips_context);
}


void sign_verify_unsuccessfully(gnutls_privkey_t privkey,
                                gnutls_pubkey_t pubkey) {
	int ret;
	gnutls_fips140_context_t fips_context;
	gnutls_fips140_operation_state_t fips_state;

	gnutls_datum_t signature;
	gnutls_datum_t plaintext = {
		.data = (unsigned char* const) "Hello world!",
		.size = 12
	};
	assert(gnutls_fips140_context_init(&fips_context) == 0);

	/* small key RSA sign: not approved */
	FIPS_PUSH_CONTEXT();
	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA256, 0,
	                               &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_privkey_sign_data failed\n");
	FIPS_POP_CONTEXT(NOT_APPROVED);

	/* small key RSA verify: not approved */
	FIPS_PUSH_CONTEXT();
	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA256, 0,
	                                 &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_pubkey_verify_data2 failed\n");
	FIPS_POP_CONTEXT(NOT_APPROVED);

	gnutls_free(signature.data);
	gnutls_pubkey_deinit(pubkey);
	gnutls_privkey_deinit(privkey);
	gnutls_fips140_context_deinit(fips_context);
}


void nosign_verify(gnutls_privkey_t privkey, gnutls_pubkey_t pubkey) {
	int ret;
	gnutls_fips140_context_t fips_context;
	gnutls_fips140_operation_state_t fips_state;

	gnutls_datum_t signature;
	gnutls_datum_t plaintext = {
		.data = (unsigned char* const) "Hello world!",
		.size = 12
	};
	assert(gnutls_fips140_context_init(&fips_context) == 0);

	/* 1024, 1280, 1536, 1792 key RSA sign: not approved */
	FIPS_PUSH_CONTEXT();
	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA256, 0,
	                               &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_privkey_sign_data failed\n");
	FIPS_POP_CONTEXT(NOT_APPROVED);

	/* Disable FIPS to sign them anyway */
	gnutls_fips140_set_mode(GNUTLS_FIPS140_LAX, 0);
	assert(gnutls_fips140_mode_enabled() == GNUTLS_FIPS140_LAX);

	ret = gnutls_privkey_sign_data(privkey, GNUTLS_DIG_SHA256, 0,
	                               &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_privkey_sign_data failed\n");

	gnutls_fips140_set_mode(GNUTLS_FIPS140_STRICT, 0);
	assert(gnutls_fips140_mode_enabled());

	/* 1024, 1280, 1536, 1792 key RSA verify: approved (exception) */
	FIPS_PUSH_CONTEXT();
	ret = gnutls_pubkey_verify_data2(pubkey, GNUTLS_SIGN_RSA_SHA256, 0,
	                                 &plaintext, &signature);
	if (ret < 0)
		fail("gnutls_pubkey_verify_data2 failed\n");
	FIPS_POP_CONTEXT(APPROVED);

	gnutls_free(signature.data);
	gnutls_pubkey_deinit(pubkey);
	gnutls_privkey_deinit(privkey);
	gnutls_fips140_context_deinit(fips_context);
}


void doit(void)
{
	gnutls_fips140_context_t fips_context;
	gnutls_privkey_t privkey;
	gnutls_pubkey_t pubkey;

	if (gnutls_fips140_mode_enabled() == 0) {
		success("We are not in FIPS140 mode\n");
		exit(77);  /* SKIP */
	}

	assert(gnutls_fips140_context_init(&fips_context) == 0);

	/* 512-bit RSA: no generate, no sign, no verify */
	generate_unsuccessfully(&privkey, &pubkey, 512);
	sign_verify_unsuccessfully(privkey, pubkey);
	/* 512-bit RSA again (to be safer about going in and out of FIPS) */
	generate_unsuccessfully(&privkey, &pubkey, 512);
	sign_verify_unsuccessfully(privkey, pubkey);
	/* 600-bit RSA: no generate, no sign, no verify */
	generate_unsuccessfully(&privkey, &pubkey, 600);
	sign_verify_unsuccessfully(privkey, pubkey);

	/* 768-bit RSA not-an-exception: nogenerate, nosign, verify */
	generate_unsuccessfully(&privkey, &pubkey, 768);
	sign_verify_unsuccessfully(privkey, pubkey);
	/* 1024-bit RSA exception: nogenerate, nosign, verify */
	generate_unsuccessfully(&privkey, &pubkey, 1024);
	nosign_verify(privkey, pubkey);
	/* 1280-bit RSA exception: nogenerate, nosign, verify */
	generate_unsuccessfully(&privkey, &pubkey, 1280);
	nosign_verify(privkey, pubkey);
	/* 1500-bit RSA not-an-exception: nogenerate, nosign, noverify */
	generate_unsuccessfully(&privkey, &pubkey, 1500);
	sign_verify_unsuccessfully(privkey, pubkey);
	/* 1536-bit RSA exception: nogenerate, nosign, verify */
	generate_unsuccessfully(&privkey, &pubkey, 1536);
	nosign_verify(privkey, pubkey);
	/* 1792-bit RSA exception: nogenerate, nosign, verify */
	generate_unsuccessfully(&privkey, &pubkey, 1792);
	nosign_verify(privkey, pubkey);
	/* 2000-bit RSA not-an-exception: nogenerate, nosign, noverify */
	generate_unsuccessfully(&privkey, &pubkey, 2000);
	sign_verify_unsuccessfully(privkey, pubkey);

	/* 2048-bit RSA: generate, sign, verify */
	generate_successfully(&privkey, &pubkey, 2048);
	sign_verify_successfully(privkey, pubkey);
	/* 2432-bit RSA: nogenerate, sign, verify */
	generate_unsuccessfully(&privkey, &pubkey, 2432);
	sign_verify_successfully(privkey, pubkey);
	/* 3072-bit RSA: generate, sign, verify */
	generate_successfully(&privkey, &pubkey, 3072);
	sign_verify_successfully(privkey, pubkey);

	gnutls_fips140_context_deinit(fips_context);
}