summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/ntru/ntru_ke.c
blob: efc660bedb8b917572f7c5e0dc1e7d5adf980329 (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
/*
 * Copyright (C) 2013-2014 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * 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 "ntru_ke.h"
#include "ntru_drbg.h"
#include "ntru_param_set.h"
#include "ntru_private_key.h"
#include "ntru_public_key.h"

#include <crypto/diffie_hellman.h>
#include <utils/debug.h>

typedef struct private_ntru_ke_t private_ntru_ke_t;

/* Best bandwidth and speed, no X9.98 compatibility */
static ntru_param_set_id_t param_sets_optimum[] = {
	NTRU_EES401EP2, NTRU_EES439EP1, NTRU_EES593EP1, NTRU_EES743EP1
};

/* X9.98/IEEE 1363.1 parameter sets for best speed */
static ntru_param_set_id_t param_sets_x9_98_speed[] = {
	NTRU_EES659EP1, NTRU_EES761EP1, NTRU_EES1087EP1, NTRU_EES1499EP1
};

/* X9.98/IEEE 1363.1 parameter sets for best bandwidth (smallest size) */
static ntru_param_set_id_t param_sets_x9_98_bandwidth[] = {
	NTRU_EES401EP1, NTRU_EES449EP1, NTRU_EES677EP1, NTRU_EES1087EP2
};

/* X9.98/IEEE 1363.1 parameter sets balancing speed and bandwidth */
static ntru_param_set_id_t param_sets_x9_98_balance[] = {
	NTRU_EES541EP1, NTRU_EES613EP1, NTRU_EES887EP1, NTRU_EES1171EP1
};

/**
 * Private data of an ntru_ke_t object.
 */
struct private_ntru_ke_t {
	/**
	 * Public ntru_ke_t interface.
	 */
	ntru_ke_t public;

	/**
	 * Diffie Hellman group number.
	 */
	diffie_hellman_group_t group;

	/**
	 * NTRU Parameter Set
	 */
	ntru_param_set_t *param_set;

	/**
	 * Cryptographical strength in bits of the NTRU Parameter Set
	 */
	uint32_t strength;

	/**
	 * NTRU Public Key
	 */
	ntru_public_key_t *pubkey;

	/**
	 * NTRU Private Key
	 */
	ntru_private_key_t *privkey;

	/**
	 * NTRU encrypted shared secret
	 */
	chunk_t ciphertext;

	/**
	 * Shared secret
	 */
	chunk_t shared_secret;

	/**
	 * True if peer is responder
	 */
	bool responder;

	/**
	 * True if shared secret is computed
	 */
	bool computed;

	/**
	 * True Random Generator
	 */
	rng_t *entropy;

	/**
	 * Deterministic Random Bit Generator
	 */
	ntru_drbg_t *drbg;
};

METHOD(diffie_hellman_t, get_my_public_value, bool,
	private_ntru_ke_t *this, chunk_t *value)
{
	*value = chunk_empty;

	if (this->responder)
	{
		if (this->ciphertext.len)
		{
			*value = chunk_clone(this->ciphertext);
		}
	}
	else
	{
		if (!this->pubkey)
		{
			/* generate a random NTRU public/private key pair */
			this->privkey = ntru_private_key_create(this->drbg, this->param_set);
			if (!this->privkey)
			{
				DBG1(DBG_LIB, "NTRU keypair generation failed");
				return FALSE;
			}
			this->pubkey = this->privkey->get_public_key(this->privkey);
		}
		*value = chunk_clone(this->pubkey->get_encoding(this->pubkey));
		DBG3(DBG_LIB, "NTRU public key: %B", value);
	}
	return TRUE;
}

METHOD(diffie_hellman_t, get_shared_secret, bool,
	private_ntru_ke_t *this, chunk_t *secret)
{
	if (!this->computed || !this->shared_secret.len)
	{
		*secret = chunk_empty;
		return FALSE;
	}
	*secret = chunk_clone(this->shared_secret);

	return TRUE;
}

METHOD(diffie_hellman_t, set_other_public_value, bool,
	private_ntru_ke_t *this, chunk_t value)
{
	if (this->privkey)
	{
		/* initiator decrypting shared secret */
		if (value.len == 0)
		{
			DBG1(DBG_LIB, "empty NTRU ciphertext");
			return FALSE;
		}
		DBG3(DBG_LIB, "NTRU ciphertext: %B", &value);

		/* decrypt the shared secret */
		if (!this->privkey->decrypt(this->privkey, value, &this->shared_secret))
		{
			DBG1(DBG_LIB, "NTRU decryption of shared secret failed");
			return FALSE;
		}
		this->computed = TRUE;
	}
	else
	{
		ntru_public_key_t *pubkey;

		/* responder generating and encrypting the shared secret */
		this->responder = TRUE;

		DBG3(DBG_LIB, "NTRU public key: %B", &value);
		pubkey = ntru_public_key_create_from_data(this->drbg, value);
		if (!pubkey)
		{
			return FALSE;
		}
		if (pubkey->get_id(pubkey) != this->param_set->id)
		{
			DBG1(DBG_LIB, "received NTRU public key with wrong OUI");
			pubkey->destroy(pubkey);
			return FALSE;
		}
		this->pubkey = pubkey;

		/* shared secret size is chosen as twice the cryptographical strength */
		this->shared_secret = chunk_alloc(2 * this->strength / BITS_PER_BYTE);

		/* generate the random shared secret */
		if (!this->drbg->generate(this->drbg, this->strength,
				this->shared_secret.len, this->shared_secret.ptr))
		{
			DBG1(DBG_LIB, "generation of shared secret failed");
			chunk_free(&this->shared_secret);
			return FALSE;
		}
		this->computed = TRUE;

		/* encrypt the shared secret */
		if (!pubkey->encrypt(pubkey, this->shared_secret, &this->ciphertext))
		{
			DBG1(DBG_LIB, "NTRU encryption of shared secret failed");
			return FALSE;
		}
		DBG3(DBG_LIB, "NTRU ciphertext: %B", &this->ciphertext);
	}
	return this->computed;
}

METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
	private_ntru_ke_t *this)
{
	return this->group;
}

METHOD(diffie_hellman_t, destroy, void,
	private_ntru_ke_t *this)
{
	DESTROY_IF(this->privkey);
	DESTROY_IF(this->pubkey);
	this->drbg->destroy(this->drbg);
	this->entropy->destroy(this->entropy);
	chunk_free(&this->ciphertext);
	chunk_clear(&this->shared_secret);
	free(this);
}

/*
 * Described in header.
 */
ntru_ke_t *ntru_ke_create(diffie_hellman_group_t group, chunk_t g, chunk_t p)
{
	private_ntru_ke_t *this;
	ntru_param_set_id_t *param_sets, param_set_id;
	rng_t *entropy;
	ntru_drbg_t *drbg;
	char *parameter_set;
	uint32_t strength;

	parameter_set = lib->settings->get_str(lib->settings,
						"%s.plugins.ntru.parameter_set", "optimum", lib->ns);

	if (streq(parameter_set, "x9_98_speed"))
	{
		param_sets = param_sets_x9_98_speed;
	}
	else if (streq(parameter_set, "x9_98_bandwidth"))
	{
		param_sets = param_sets_x9_98_bandwidth;
	}
	else if (streq(parameter_set, "x9_98_balance"))
	{
		param_sets = param_sets_x9_98_balance;
	}
	else
	{
		param_sets = param_sets_optimum;
	}

	switch (group)
	{
		case NTRU_112_BIT:
			strength = 112;
			param_set_id = param_sets[0];
			break;
		case NTRU_128_BIT:
			strength = 128;
			param_set_id = param_sets[1];
			break;
		case NTRU_192_BIT:
			strength = 192;
			param_set_id = param_sets[2];
			break;
		case NTRU_256_BIT:
			strength = 256;
			param_set_id = param_sets[3];
			break;
		default:
			return NULL;
	}
	DBG1(DBG_LIB, "%u bit %s NTRU parameter set %N selected", strength,
				   parameter_set, ntru_param_set_id_names, param_set_id);

	entropy = lib->crypto->create_rng(lib->crypto, RNG_TRUE);
	if (!entropy)
	{
		DBG1(DBG_LIB, "could not attach entropy source for DRBG");
		return NULL;
	}

	drbg = ntru_drbg_create(strength, chunk_from_str("IKE NTRU-KE"), entropy);
	if (!drbg)
	{
		DBG1(DBG_LIB, "could not instantiate DRBG at %u bit security", strength);
		entropy->destroy(entropy);
		return NULL;
	}

	INIT(this,
		.public = {
			.dh = {
				.get_shared_secret = _get_shared_secret,
				.set_other_public_value = _set_other_public_value,
				.get_my_public_value = _get_my_public_value,
				.get_dh_group = _get_dh_group,
				.destroy = _destroy,
			},
		},
		.group = group,
		.param_set = ntru_param_set_get_by_id(param_set_id),
		.strength = strength,
		.entropy = entropy,
		.drbg = drbg,
	);

	return &this->public;
}