summaryrefslogtreecommitdiff
path: root/tests/aead-cipher-vec.c
blob: 6a30a35f7b855737e357495b25f1542cbfbde2bf (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
/*
 * Copyright (C) 2019 Red Hat, Inc.
 *
 * Author: Daiki Ueno
 *
 * 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 Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 */

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

#include <gnutls/crypto.h>

#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "utils.h"

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

/* Test whether gnutls_aead_cipher_{en,de}crypt_vec works */
static void start(const char *name, int algo)
{
	int ret;
	gnutls_aead_cipher_hd_t ch;
	uint8_t key16[64];
	uint8_t iv16[32];
	uint8_t auth[128];
	uint8_t data[64+56+36];
	gnutls_datum_t key, iv;
	giovec_t iov[3];
	giovec_t auth_iov[2];
	uint8_t tag[64];
	size_t tag_size = 0;
	size_t i;

	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));
	memset(auth, 0xaa, sizeof(auth));

	iov[0].iov_base = data;
	iov[0].iov_len = 64;
	iov[1].iov_base = data + 64;
	iov[1].iov_len = 56;
	iov[2].iov_base = data + 64 + 56;
	iov[2].iov_len = 36;

	auth_iov[0].iov_base = auth;
	auth_iov[0].iov_len = 64;
	auth_iov[1].iov_base = auth + 64;
	auth_iov[1].iov_len = 64;

	success("trying %s\n", name);

	ret =
	    gnutls_aead_cipher_init(&ch, algo, &key);
	if (ret < 0)
		fail("gnutls_cipher_init: %s\n", gnutls_strerror(ret));

	for (i = 0; i < 2; i++) {
		ret = gnutls_aead_cipher_encryptv2(ch,
						   iv.data, iv.size,
						   auth_iov, 2,
						   iov, i + 1,
						   tag, &tag_size);
		if (ret < 0)
			fail("could not encrypt data: %s\n", gnutls_strerror(ret));

		ret = gnutls_aead_cipher_decryptv2(ch,
						   iv.data, iv.size,
						   auth_iov, 2,
						   iov, i + 1,
						   tag, tag_size);
		if (ret < 0)
			fail("could not decrypt data: %s\n", gnutls_strerror(ret));
	}

	gnutls_aead_cipher_deinit(ch);
}

void
doit(void)
{
	int ret;

	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 */
	}

	start("aes-128-gcm", GNUTLS_CIPHER_AES_128_GCM);
	start("aes-192-gcm", GNUTLS_CIPHER_AES_192_GCM);
	start("aes-256-gcm", GNUTLS_CIPHER_AES_256_GCM);
	start("aes-128-ccm", GNUTLS_CIPHER_AES_128_CCM);
	if (!gnutls_fips140_mode_enabled())
		start("chacha20-poly1305", GNUTLS_CIPHER_CHACHA20_POLY1305);

	gnutls_global_deinit();
}