summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/chapoly/chapoly_aead.c
blob: 39d51e9f8d255fe13bb4ec26e2deff70dd53177a (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
329
330
331
332
333
/*
 * Copyright (C) 2015 Martin Willi
 * Copyright (C) 2015 revosec AG
 *
 * This program 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 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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.
 */

#include "chapoly_aead.h"
#include "chapoly_drv.h"

#include <crypto/iv/iv_gen_seq.h>

/* maximum plain message size */
#define P_MAX 247877906880

typedef struct private_chapoly_aead_t private_chapoly_aead_t;

/**
 * Private data of an chapoly_aead_t object.
 */
struct private_chapoly_aead_t {

	/**
	 * Public chapoly_aead_t interface.
	 */
	chapoly_aead_t public;

	/**
	 * IV generator.
	 */
	iv_gen_t *iv_gen;

	/**
	 * Driver backend
	 */
	chapoly_drv_t *drv;
};

/**
 * Include a partial block to ICV by padding it with zero bytes
 */
static bool poly_update_padded(private_chapoly_aead_t *this,
							   u_char *in, size_t len)
{
	u_char b[POLY_BLOCK_SIZE];

	memset(b, 0, sizeof(b));
	memcpy(b, in, len);

	return this->drv->poly(this->drv, b, 1);
}

/**
 * Include associated data with padding to ICV
 */
static bool poly_head(private_chapoly_aead_t *this, u_char *assoc, size_t len)
{
	u_int blocks, rem;

	blocks = len / POLY_BLOCK_SIZE;
	rem = len % POLY_BLOCK_SIZE;
	if (!this->drv->poly(this->drv, assoc, blocks))
	{
		return FALSE;
	}
	if (rem)
	{
		return poly_update_padded(this, assoc + blocks * POLY_BLOCK_SIZE, rem);
	}
	return TRUE;
}

/**
 * Include length fields to ICV
 */
static bool poly_tail(private_chapoly_aead_t *this, size_t alen, size_t clen)
{
	struct {
		uint64_t alen;
		uint64_t clen;
	} b;

	b.alen = htole64(alen);
	b.clen = htole64(clen);

	return this->drv->poly(this->drv, (u_char*)&b, 1);
}

/**
 * Perform ChaCha20 encryption inline and generate an ICV tag
 */
static bool do_encrypt(private_chapoly_aead_t *this, size_t len, u_char *data,
					   u_char *iv, size_t alen, u_char *assoc, u_char *icv)
{
	u_int blocks, rem, prem;

	if (!this->drv->init(this->drv, iv) ||
		!poly_head(this, assoc, alen))
	{
		return FALSE;
	}
	blocks = len / CHACHA_BLOCK_SIZE;
	if (!this->drv->encrypt(this->drv, data, blocks))
	{
		return FALSE;
	}
	rem = len % CHACHA_BLOCK_SIZE;
	if (rem)
	{
		u_char stream[CHACHA_BLOCK_SIZE];

		data += blocks * CHACHA_BLOCK_SIZE;
		if (!this->drv->chacha(this->drv, stream))
		{
			return FALSE;
		}
		memxor(data, stream, rem);

		blocks = rem / POLY_BLOCK_SIZE;
		if (!this->drv->poly(this->drv, data, blocks))
		{
			return FALSE;
		}
		prem = rem % POLY_BLOCK_SIZE;
		if (prem)
		{
			poly_update_padded(this, data + blocks * POLY_BLOCK_SIZE, prem);
		}
	}
	return poly_tail(this, alen, len) &&
		   this->drv->finish(this->drv, icv);
}

/**
 * Perform ChaCha20 decryption inline and generate an ICV tag
 */
static bool do_decrypt(private_chapoly_aead_t *this, size_t len, u_char *data,
					   u_char *iv, size_t alen, u_char *assoc, u_char *icv)
{
	u_int blocks, rem, prem;

	if (!this->drv->init(this->drv, iv) ||
		!poly_head(this, assoc, alen))
	{
		return FALSE;
	}
	blocks = len / CHACHA_BLOCK_SIZE;
	if (!this->drv->decrypt(this->drv, data, blocks))
	{
		return FALSE;
	}
	rem = len % CHACHA_BLOCK_SIZE;
	if (rem)
	{
		u_char stream[CHACHA_BLOCK_SIZE];

		data += blocks * CHACHA_BLOCK_SIZE;

		blocks = rem / POLY_BLOCK_SIZE;
		if (!this->drv->poly(this->drv, data, blocks))
		{
			return FALSE;
		}
		prem = rem % POLY_BLOCK_SIZE;
		if (prem)
		{
			poly_update_padded(this, data + blocks * POLY_BLOCK_SIZE, prem);
		}
		if (!this->drv->chacha(this->drv, stream))
		{
			return FALSE;
		}
		memxor(data, stream, rem);
	}
	return poly_tail(this, alen, len) &&
		   this->drv->finish(this->drv, icv);
}

METHOD(aead_t, encrypt, bool,
	private_chapoly_aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
	chunk_t *encr)
{
	u_char *out;

	if (sizeof(plain.len) > sizeof(uint32_t) && plain.len > P_MAX)
	{
		return FALSE;
	}
	if (iv.len != CHACHA_IV_SIZE)
	{
		return FALSE;
	}
	out = plain.ptr;
	if (encr)
	{
		*encr = chunk_alloc(plain.len + POLY_ICV_SIZE);
		out = encr->ptr;
		memcpy(out, plain.ptr, plain.len);
	}
	do_encrypt(this, plain.len, out, iv.ptr, assoc.len, assoc.ptr,
			   out + plain.len);
	return TRUE;
}

METHOD(aead_t, decrypt, bool,
	private_chapoly_aead_t *this, chunk_t encr, chunk_t assoc, chunk_t iv,
	chunk_t *plain)
{
	u_char *out, icv[POLY_ICV_SIZE];
	if (iv.len != CHACHA_IV_SIZE || encr.len < POLY_ICV_SIZE)
	{
		return FALSE;
	}
	encr.len -= POLY_ICV_SIZE;
	if (sizeof(encr.len) > sizeof(uint32_t) && encr.len > P_MAX)
	{
		return FALSE;
	}
	out = encr.ptr;
	if (plain)
	{
		*plain = chunk_alloc(encr.len);
		out = plain->ptr;
		memcpy(out, encr.ptr, encr.len);
	}
	do_decrypt(this, encr.len, out, iv.ptr, assoc.len, assoc.ptr, icv);
	return memeq_const(icv, encr.ptr + encr.len, POLY_ICV_SIZE);
}

METHOD(aead_t, get_block_size, size_t,
	private_chapoly_aead_t *this)
{
	return 1;
}

METHOD(aead_t, get_icv_size, size_t,
	private_chapoly_aead_t *this)
{
	return POLY_ICV_SIZE;
}

METHOD(aead_t, get_iv_size, size_t,
	private_chapoly_aead_t *this)
{
	return CHACHA_IV_SIZE;
}

METHOD(aead_t, get_iv_gen, iv_gen_t*,
	private_chapoly_aead_t *this)
{
	return this->iv_gen;
}

METHOD(aead_t, get_key_size, size_t,
	private_chapoly_aead_t *this)
{
	return CHACHA_KEY_SIZE + CHACHA_SALT_SIZE;
}

METHOD(aead_t, set_key, bool,
	private_chapoly_aead_t *this, chunk_t key)
{
	if (key.len != CHACHA_KEY_SIZE + CHACHA_SALT_SIZE)
	{
		return FALSE;
	}
	return this->drv->set_key(this->drv, "expand 32-byte k",
							  key.ptr, key.ptr + CHACHA_KEY_SIZE);
}

METHOD(aead_t, destroy, void,
	private_chapoly_aead_t *this)
{
	this->drv->destroy(this->drv);
	this->iv_gen->destroy(this->iv_gen);
	free(this);
}

/**
 * See header
 */
chapoly_aead_t *chapoly_aead_create(encryption_algorithm_t algo,
									size_t key_size, size_t salt_size)
{
	private_chapoly_aead_t *this;
	chapoly_drv_t *drv;

	if (algo != ENCR_CHACHA20_POLY1305)
	{
		return NULL;
	}
	if (key_size && key_size != CHACHA_KEY_SIZE)
	{
		return NULL;
	}
	if (salt_size && salt_size != CHACHA_SALT_SIZE)
	{
		return NULL;
	}
	drv = chapoly_drv_probe();
	if (!drv)
	{
		return NULL;
	}

	INIT(this,
		.public = {
			.aead = {
				.encrypt = _encrypt,
				.decrypt = _decrypt,
				.get_block_size = _get_block_size,
				.get_icv_size = _get_icv_size,
				.get_iv_size = _get_iv_size,
				.get_iv_gen = _get_iv_gen,
				.get_key_size = _get_key_size,
				.set_key = _set_key,
				.destroy = _destroy,
			},
		},
		.iv_gen = iv_gen_seq_create(),
		.drv = drv,
	);

	return &this->public;
}