File: cipher-api-test.c

package info (click to toggle)
gnutls28 3.8.12-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 82,380 kB
  • sloc: ansic: 392,233; asm: 117,804; sh: 18,758; makefile: 6,804; yacc: 1,858; python: 1,399; cpp: 1,243; perl: 995; sed: 39
file content (562 lines) | stat: -rw-r--r-- 14,499 bytes parent folder | download | duplicates (4)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

#include "config.h"
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include <gnutls/abstract.h>
#include <gnutls/x509.h>

/* Test legal and illegal use of gnutls_cipher_* and gnutls_aead_cipher_*
 * API. This test is written using fork, because some of the test
 * cases may hit assertion failure in Nettle and crash the process.
 */

#if defined(WIN32)
int main(int argc, char **argv)
{
	exit(77);
}
#else

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <assert.h>
#include "utils.h"

#define AES_GCM_ENCRYPT_PLAINTEXT_MAX ((1ULL << 36) - 32)
#if SIZE_MAX >= AES_GCM_ENCRYPT_PLAINTEXT_MAX
#define TEST_AES_GCM_ENCRYPT_PLAINTEXT_SIZE 1
#endif

static void tls_log_func(int level, const char *str)
{
	fprintf(stderr, "<%d>| %s", level, str);
}

/* (Non-AEAD) Test a happy path where everything works */
static void test_cipher_happy(int algo)
{
	int ret;
	gnutls_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t data[128];
	gnutls_datum_t key, iv;

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(data, 0xfa, sizeof(data));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0) {
		fail("Cannot initialize library\n");
	}

	ret = gnutls_cipher_init(&ch, algo, &key, &iv);
	if (ret < 0)
		fail("gnutls_cipher_init failed\n");

	ret = gnutls_cipher_encrypt(ch, data, sizeof(data));
	if (ret < 0)
		fail("gnutls_cipher_encrypt failed\n");

	gnutls_cipher_deinit(ch);

	gnutls_global_deinit();
}

/* Test whether an invalid call to gnutls_cipher_encrypt() is caught */
static void test_cipher_invalid_partial(int algo)
{
	int ret;
	gnutls_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t data[128];
	gnutls_datum_t key, iv;

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(data, 0xfa, sizeof(data));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0) {
		fail("Cannot initialize library\n"); /*errcode 1 */
	}

	ret = gnutls_cipher_init(&ch, algo, &key, &iv);
	if (ret < 0)
		fail("gnutls_cipher_init failed\n"); /*errcode 1 */

	/* try encrypting in a way that violates nettle's block conventions */
	ret = gnutls_cipher_encrypt(ch, data, sizeof(data) - 1);
	if (ret >= 0)
		fail("succeeded in encrypting partial data on block cipher\n");
	if (ret != GNUTLS_E_INVALID_REQUEST)
		fail("wrong kind of error on decrypting onto a short buffer,"
		     "%s instead of GNUTLS_E_INVALID_REQUEST\n",
		     gnutls_strerror_name(ret));

	gnutls_cipher_deinit(ch);

	gnutls_global_deinit();
}

/* Test AEAD encryption/decryption */
static void test_aead_happy(int algo)
{
	int ret;
	gnutls_aead_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t auth[32];
	uint8_t ctext[128 + 32];
	size_t ctext_len;
	uint8_t ptext[128];
	uint8_t otext[128];
	size_t ptext_len;
	gnutls_datum_t key, iv;
	size_t tag_len;

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	ptext_len = sizeof(ptext);
	tag_len = gnutls_cipher_get_tag_size(algo);

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(ptext, 0xfa, sizeof(ptext));
	memset(otext, 0xfc, sizeof(otext));
	memset(ctext, 0xfa, sizeof(ctext));
	memset(auth, 0xfb, sizeof(auth));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0)
		fail("Cannot initialize library\n");

	ret = gnutls_aead_cipher_init(&ch, algo, &key);
	if (ret < 0)
		fail("gnutls_aead_cipher_init failed\n");

	ctext_len = sizeof(ctext);
	ret = gnutls_aead_cipher_encrypt(ch, iv.data, iv.size, auth,
					 sizeof(auth), tag_len, ptext,
					 sizeof(ptext), ctext, &ctext_len);
	if (ret < 0)
		fail("could not encrypt data\n");

	if (ctext_len != sizeof(ptext) + tag_len)
		fail("output ciphertext length mismatch\n");

	ret = gnutls_aead_cipher_decrypt(ch, iv.data, iv.size, auth,
					 sizeof(auth), tag_len, ctext,
					 ctext_len, ptext, &ptext_len);
	if (ret < 0)
		fail("could not decrypt data: %s\n", gnutls_strerror(ret));

	if (!memcmp(ptext, otext, sizeof(ptext)))
		fail("mismatch of decrypted data\n");

	gnutls_aead_cipher_deinit(ch);

	gnutls_global_deinit();
	return;
}

/* Test whether an invalid gnutls_cipher_add_auth() is caught */
static void test_aead_invalid_add_auth(int algo)
{
	int ret;
	gnutls_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t data[128];
	gnutls_datum_t key, iv;

	if (algo == GNUTLS_CIPHER_CHACHA20_POLY1305)
		return;

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(data, 0xfa, sizeof(data));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0) {
		fail("Cannot initialize library\n"); /*errcode 1 */
	}

	ret = gnutls_cipher_init(&ch, algo, &key, &iv);
	if (ret < 0)
		fail("gnutls_cipher_init failed\n"); /*errcode 1 */

	ret = gnutls_cipher_add_auth(ch, data, sizeof(data) - 1);
	if (ret < 0)
		fail("could not add auth data\n");

	ret = gnutls_cipher_add_auth(ch, data, 16);
	if (ret >= 0)
		fail("succeeded in adding auth data after partial data were given\n");
	if (ret != GNUTLS_E_INVALID_REQUEST)
		fail("wrong kind of error on decrypting onto a short buffer,"
		     "%s instead of GNUTLS_E_INVALID_REQUEST\n",
		     gnutls_strerror_name(ret));

	gnutls_cipher_deinit(ch);

	gnutls_global_deinit();
	return;
}

/* Test whether an invalid call to gnutls_cipher_encrypt() is caught */
static void test_aead_invalid_partial_encrypt(int algo)
{
	int ret;
	gnutls_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t data[128];
	gnutls_datum_t key, iv;

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(data, 0xfa, sizeof(data));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0) {
		fail("Cannot initialize library\n"); /*errcode 1 */
	}

	ret = gnutls_cipher_init(&ch, algo, &key, &iv);
	if (ret < 0)
		fail("gnutls_cipher_init failed\n"); /*errcode 1 */

	/* try encrypting in a way that violates nettle's AEAD conventions */
	ret = gnutls_cipher_encrypt(ch, data, sizeof(data) - 1);
	if (ret < 0)
		fail("could not encrypt data\n");

	ret = gnutls_cipher_encrypt(ch, data, sizeof(data));
	if (ret >= 0)
		fail("succeeded in encrypting partial data after partial data were given\n");
	if (ret != GNUTLS_E_INVALID_REQUEST)
		fail("wrong kind of error on decrypting onto a short buffer,"
		     "%s instead of GNUTLS_E_INVALID_REQUEST\n",
		     gnutls_strerror_name(ret));

	gnutls_cipher_deinit(ch);

	gnutls_global_deinit();
	return;
}

/* Test whether an invalid call to gnutls_aead_cipher_decrypt() is caught */
static void test_aead_invalid_short_decrypt(int algo)
{
	int ret;
	gnutls_aead_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t auth[32];
	uint8_t ctext[128 + 32];
	size_t ctext_len;
	uint8_t ptext[128];
	size_t ptext_len;
	gnutls_datum_t key, iv;
	size_t tag_len;

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	tag_len = gnutls_cipher_get_tag_size(algo);

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(ptext, 0xfa, sizeof(ptext));
	memset(ctext, 0xfa, sizeof(ctext));
	memset(auth, 0xfb, sizeof(auth));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0)
		fail("Cannot initialize library\n");

	ret = gnutls_aead_cipher_init(&ch, algo, &key);
	if (ret < 0)
		fail("gnutls_aead_cipher_init failed\n");

	ctext_len = sizeof(ctext);
	ret = gnutls_aead_cipher_encrypt(ch, iv.data, iv.size, auth,
					 sizeof(auth), tag_len, ptext,
					 sizeof(ptext), ctext, &ctext_len);
	if (ret < 0)
		fail("could not encrypt data\n");

	if (ctext_len != sizeof(ptext) + tag_len)
		fail("output ciphertext length mismatch\n");

	ptext_len = 0;
	ret = gnutls_aead_cipher_decrypt(ch, iv.data, iv.size, auth,
					 sizeof(auth), tag_len, ctext,
					 ctext_len, ptext, &ptext_len);
	if (ret >= 0)
		fail("succeeded in decrypting data onto a short buffer\n");
	if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
		fail("wrong kind of error on decrypting onto a short buffer,"
		     "%s instead of GNUTLS_E_SHORT_MEMORY_BUFFER\n",
		     gnutls_strerror_name(ret));

	gnutls_aead_cipher_deinit(ch);

	gnutls_global_deinit();
	return;
}

#ifdef TEST_AES_GCM_ENCRYPT_PLAINTEXT_SIZE
/* Test whether an invalid call to gnutls_cipher_encrypt() with too
 * long message is caught */
static void test_aead_invalid_too_long_encrypt(int algo)
{
	int ret;
	gnutls_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t data[128];
	gnutls_datum_t key, iv;

	if (algo != GNUTLS_CIPHER_AES_128_GCM &&
	    algo != GNUTLS_CIPHER_AES_192_GCM &&
	    algo != GNUTLS_CIPHER_AES_256_GCM) {
		return;
	}

	key.data = key16;
	key.size = gnutls_cipher_get_key_size(algo);
	assert(key.size <= sizeof(key16));

	iv.data = iv16;
	iv.size = gnutls_cipher_get_iv_size(algo);
	assert(iv.size <= sizeof(iv16));

	memset(iv.data, 0xff, iv.size);
	memset(key.data, 0xfe, key.size);
	memset(data, 0xfa, sizeof(data));

	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	ret = global_init();
	if (ret < 0) {
		fail("Cannot initialize library\n"); /*errcode 1 */
	}

	ret = gnutls_cipher_init(&ch, algo, &key, &iv);
	if (ret < 0)
		fail("gnutls_cipher_init failed\n"); /*errcode 1 */

	/* Test exceeding AES-GCM plaintext limit */
	ret = gnutls_cipher_encrypt(ch, data, sizeof(data));
	if (ret < 0)
		fail("could not encrypt data\n");

	/* A few blocks larger than AES_GCM_ENCRYPT_PLAINTEXT_MAX combined with
	 * the previous call.  Use NULL for PLAINTEXT so the access to the first
	 * block always results in page fault (in case the limit is not
	 * enforced).
	 */
	ret = gnutls_cipher_encrypt(ch, NULL, AES_GCM_ENCRYPT_PLAINTEXT_MAX);
	if (ret >= 0)
		fail("succeeded in encrypting too long data\n");
	if (ret != GNUTLS_E_INVALID_REQUEST)
		fail("wrong kind of error on encrypting too long data,"
		     "%s instead of GNUTLS_E_INVALID_REQUEST\n",
		     gnutls_strerror_name(ret));

	gnutls_cipher_deinit(ch);

	gnutls_global_deinit();
	return;
}
#endif

static void check_status(int status)
{
	if (WEXITSTATUS(status) != 0 ||
	    (WIFSIGNALED(status) && WTERMSIG(status) != SIGABRT)) {
		if (WIFSIGNALED(status)) {
			fail("Child died with signal %d\n", WTERMSIG(status));
		} else {
			fail("Child died with status %d\n",
			     WEXITSTATUS(status));
		}
	}
}

typedef void subtest(int algo);

static void fork_subtest(subtest func, int algo)
{
	pid_t child;

	child = fork();
	if (child < 0) {
		perror("fork");
		fail("fork");
		return;
	}

	if (child) {
		int status;
		/* parent */
		wait(&status);
		check_status(status);
	} else {
		func(algo);
		exit(0);
	}
};

static void start(const char *name, int algo, unsigned aead)
{
	success("trying %s\n", name);

	signal(SIGPIPE, SIG_IGN);

	success("trying %s: test_cipher_happy\n", name);
	fork_subtest(test_cipher_happy, algo);

	if (!aead) {
		success("trying %s: test_cipher_invalid_partial\n", name);
		fork_subtest(test_cipher_invalid_partial, algo);
	}

	if (aead) {
		success("trying %s: test_aead_happy\n", name);
		fork_subtest(test_aead_happy, algo);

		success("trying %s: test_aead_invalid_add_auth\n", name);
		fork_subtest(test_aead_invalid_add_auth, algo);

		success("trying %s: test_aead_invalid_partial_encrypt\n", name);
		fork_subtest(test_aead_invalid_partial_encrypt, algo);

		success("trying %s: test_aead_invalid_short_decrypt\n", name);
		fork_subtest(test_aead_invalid_short_decrypt, algo);

#if TEST_AES_GCM_ENCRYPT_PLAINTEXT_SIZE
		success("trying %s: test_aead_invalid_too_long_encrypt\n",
			name);
		fork_subtest(test_aead_invalid_too_long_encrypt, algo);
#endif
	}
}

void doit(void)
{
	start("aes128-gcm", GNUTLS_CIPHER_AES_128_GCM, 1);
	start("aes192-gcm", GNUTLS_CIPHER_AES_192_GCM, 1);
	start("aes256-gcm", GNUTLS_CIPHER_AES_256_GCM, 1);
	start("aes128-cbc", GNUTLS_CIPHER_AES_128_CBC, 0);
	start("aes192-cbc", GNUTLS_CIPHER_AES_192_CBC, 0);
	start("aes256-cbc", GNUTLS_CIPHER_AES_256_CBC, 0);
	if (!gnutls_fips140_mode_enabled()) {
		start("3des-cbc", GNUTLS_CIPHER_3DES_CBC, 0);
		start("camellia128-gcm", GNUTLS_CIPHER_CAMELLIA_128_GCM, 1);
		start("camellia256-gcm", GNUTLS_CIPHER_CAMELLIA_256_GCM, 1);
		start("chacha20-poly1305", GNUTLS_CIPHER_CHACHA20_POLY1305, 1);
	}
}

#endif