summaryrefslogtreecommitdiff
path: root/tests/slow/cipher-openssl-compat.c
blob: 64adf25a450f8f3cf5fbb3fdcff76efb087ef0be (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
#include <config.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <utils.h>
#include <stdlib.h>
#include <assert.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include <openssl/evp.h>
#include <openssl/opensslv.h>

/* This does check the AES and CHACHA20 implementations for compatibility
 * with openssl.
 */

#define BSIZE (64*1024+12)
#define B2SIZE (1024+7)
static unsigned char buffer_auth[B2SIZE];
static unsigned char orig_plain_data[BSIZE];
static unsigned char enc_data[BSIZE + 32];	/* allow for tag */
static unsigned char dec_data[BSIZE];

static int cipher_test(const char *ocipher, gnutls_cipher_algorithm_t gcipher,
		       unsigned tag_size)
{
	int ret;
	gnutls_aead_cipher_hd_t hd;
	gnutls_datum_t dkey, dnonce;
	unsigned char key[32];
	unsigned char nonce[32];
	size_t enc_data_size, dec_data_size;
	int dec_data_size2;
	EVP_CIPHER_CTX *ctx;
	const EVP_CIPHER *evp_cipher;
	unsigned char tag[64];
	unsigned i;

	success("cipher: %s\n", ocipher);

	/* decrypt with openssl */
	evp_cipher = EVP_get_cipherbyname(ocipher);
	if (!evp_cipher) {
		/* XXX: fix version check later when LibreSSL fixes support for aes-ccm and chacha20-poly1305 */
#ifdef LIBRESSL_VERSION_NUMBER
		fprintf(stderr, "EVP_get_cipherbyname failed for %s\n", ocipher);
		return -1;
#else
		/* OpenSSL should always work! */
		fail("EVP_get_cipherbyname failed for %s\n", ocipher);
#endif
	}

	for (i = 0; i < 32; i++) {	/* try with multiple keys and nonces */
		assert(gnutls_rnd
		       (GNUTLS_RND_NONCE, orig_plain_data,
			sizeof(orig_plain_data)) >= 0);
		assert(gnutls_rnd
		       (GNUTLS_RND_NONCE, buffer_auth,
			sizeof(buffer_auth)) >= 0);
		assert(gnutls_rnd(GNUTLS_RND_NONCE, key, sizeof(key)) >= 0);
		assert(gnutls_rnd(GNUTLS_RND_NONCE, nonce, sizeof(nonce)) >= 0);

		dkey.data = (void *)key;
		dkey.size = gnutls_cipher_get_key_size(gcipher);
		assert(gnutls_aead_cipher_init(&hd, gcipher, &dkey) >= 0);

		dnonce.data = (void *)nonce;
		dnonce.size = gnutls_cipher_get_iv_size(gcipher);

		enc_data_size = sizeof(enc_data);
		assert(gnutls_aead_cipher_encrypt(hd, dnonce.data, dnonce.size,
						  buffer_auth,
						  sizeof(buffer_auth), tag_size,
						  orig_plain_data,
						  sizeof(orig_plain_data),
						  enc_data,
						  &enc_data_size) >= 0);

		if (debug)
			success("encrypted %d bytes, to %d\n",
				(int)sizeof(orig_plain_data),
				(int)enc_data_size);

		dec_data_size = sizeof(dec_data);
		ret = gnutls_aead_cipher_decrypt(hd, dnonce.data, dnonce.size,
						 buffer_auth,
						 sizeof(buffer_auth), tag_size,
						 enc_data, enc_data_size,
						 dec_data, &dec_data_size);
		if (ret < 0) {
			fail("error in gnutls_aead_cipher_decrypt for %s: %s\n",
			     ocipher, gnutls_strerror(ret));
		}

		if (dec_data_size != sizeof(orig_plain_data)
		    || memcmp(dec_data, orig_plain_data,
			      sizeof(orig_plain_data)) != 0) {
			fail("gnutls encrypt-decrypt failed (got: %d, expected: %d)\n", (int)dec_data_size, (int)sizeof(orig_plain_data));
		}

		gnutls_aead_cipher_deinit(hd);

		ctx = EVP_CIPHER_CTX_new();

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
		if (gcipher == GNUTLS_CIPHER_AES_128_CCM
		    || gcipher == GNUTLS_CIPHER_AES_256_CCM) {
			assert(EVP_CipherInit_ex(ctx, evp_cipher, 0, 0, 0, 0) >
			       0);

			assert(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, dnonce.size, 0)==1);
			assert(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_size,
					    enc_data + enc_data_size - tag_size) == 1);

			assert(EVP_CipherInit_ex(ctx, 0, 0, key, nonce, 0) >
			       0);

			dec_data_size2 = sizeof(dec_data);
			/* Add plain size */
			assert(EVP_CipherUpdate
			       (ctx, NULL, &dec_data_size2, NULL,
				enc_data_size - tag_size) > 0);

			/* Add AAD */
			assert(EVP_CipherUpdate
			       (ctx, NULL, &dec_data_size2, buffer_auth,
				sizeof(buffer_auth)) > 0);

			/* Decrypt */
			assert(EVP_CipherUpdate
			       (ctx, dec_data, &dec_data_size2, enc_data,
				enc_data_size - tag_size) > 0);

			dec_data_size = dec_data_size2;
			dec_data_size2 = tag_size;

			if (dec_data_size != sizeof(orig_plain_data)
			    || memcmp(dec_data, orig_plain_data,
				      sizeof(orig_plain_data)) != 0) {
				fail("openssl decrypt failed for %s\n", ocipher);
			}
		} else
#endif
		{
			assert(EVP_CipherInit_ex(ctx, evp_cipher, NULL, key, nonce, 0) >
			       0);

			EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag_size,
					    enc_data + enc_data_size - tag_size);

			dec_data_size2 = sizeof(dec_data);

			/* Add AAD */
			assert(EVP_CipherUpdate
			       (ctx, NULL, &dec_data_size2, buffer_auth,
				sizeof(buffer_auth)) > 0);

			/* Decrypt */
			assert(EVP_CipherUpdate
			       (ctx, dec_data, &dec_data_size2, enc_data,
				enc_data_size - tag_size) > 0);

			dec_data_size = dec_data_size2;
			dec_data_size2 = tag_size;
			assert(EVP_CipherFinal_ex(ctx, tag, &dec_data_size2) > 0);

			if (dec_data_size != sizeof(orig_plain_data)
			    || memcmp(dec_data, orig_plain_data,
				      sizeof(orig_plain_data)) != 0) {
				fail("openssl decrypt failed for %s\n", ocipher);
			}

		}
		EVP_CIPHER_CTX_free(ctx);

	}

	return 0;
}

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

void doit(void)
{
	gnutls_global_set_log_function(tls_log_func);
	if (debug)
		gnutls_global_set_log_level(4711);

	global_init();
	OpenSSL_add_all_algorithms();

	/* ciphers */
	cipher_test("aes-128-gcm", GNUTLS_CIPHER_AES_128_GCM, 16);
	cipher_test("aes-256-gcm", GNUTLS_CIPHER_AES_256_GCM, 16);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
	if (!gnutls_fips140_mode_enabled()) {
		cipher_test("chacha20-poly1305", GNUTLS_CIPHER_CHACHA20_POLY1305, 16);
	}
	cipher_test("aes-128-ccm", GNUTLS_CIPHER_AES_128_CCM, 16);
	cipher_test("aes-256-ccm", GNUTLS_CIPHER_AES_256_CCM, 16);
#endif

	gnutls_global_deinit();
	return;
}