aboutsummaryrefslogtreecommitdiffstats
path: root/src/pluto/crypto.c
blob: dc51bdf79fc7d895b8d647f189d3d2093d90bb88 (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
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
/* crypto interfaces
 * Copyright (C) 1998-2001  D. Hugh Redelmeier
 * Copyright (C) 2007-2009 Andreas Steffen
 *
 * 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 <gmp.h>

#include <freeswan.h>
#include <ipsec_policy.h>

#include "constants.h"
#include "defs.h"
#include "crypto.h"
#include "ike_alg.h"
#include "log.h"

extern struct encrypt_desc encrypt_desc_3des;
extern struct encrypt_desc encrypt_desc_blowfish;
extern struct encrypt_desc encrypt_desc_aes;
extern struct encrypt_desc encrypt_desc_twofish;
extern struct encrypt_desc encrypt_desc_twofish_ssh;
extern struct encrypt_desc encrypt_desc_serpent;

extern struct hash_desc hash_desc_md5;
extern struct hash_desc hash_desc_sha1;
extern struct hash_desc hash_desc_sha2_256;
extern struct hash_desc hash_desc_sha2_384;
extern struct hash_desc hash_desc_sha2_512;

/* moduli and generator. */

static MP_INT
	modp1024_modulus,
	modp1536_modulus,
	modp2048_modulus,
	modp3072_modulus,
	modp4096_modulus,
	modp6144_modulus,
	modp8192_modulus;

MP_INT groupgenerator;  /* MODP group generator (2) */



void init_crypto(void)
{
	enumerator_t *enumerator;
	encryption_algorithm_t encryption_alg;
	hash_algorithm_t hash_alg;
	bool no_md5  = TRUE;
	bool no_sha1 = TRUE;

	enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
	while (enumerator->enumerate(enumerator, &hash_alg))
	{
		const struct hash_desc *desc;

		switch (hash_alg)
		{
			case HASH_SHA1:
				desc = &hash_desc_sha1;
				no_sha1 = FALSE;
				break;
			case HASH_SHA256:
				desc = &hash_desc_sha2_256;
				break;
			case HASH_SHA384:
				desc = &hash_desc_sha2_384;
				break;
			case HASH_SHA512:
				desc = &hash_desc_sha2_512;
				break;
			case HASH_MD5:
				desc = &hash_desc_md5;
				no_md5 = FALSE;
				break;
			default:
				continue;
		}
		ike_alg_add((struct ike_alg *)desc);
	}
	enumerator->destroy(enumerator);

	if (no_sha1)
	{
		exit_log("pluto cannot run without a SHA-1 hasher");
	}
	if (no_md5)
	{
		exit_log("pluto cannot run without an MD5 hasher");
	}
		
	enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
	while (enumerator->enumerate(enumerator, &encryption_alg))
	{
		const struct encrypt_desc *desc;
 
		switch (encryption_alg)
		{
			case ENCR_3DES:
				desc = &encrypt_desc_3des;
				break;
			case ENCR_BLOWFISH:
				desc = &encrypt_desc_blowfish;
				break;
			case ENCR_AES_CBC:
				desc = &encrypt_desc_aes;
				break;
			case ENCR_TWOFISH_CBC:
				desc = &encrypt_desc_twofish;
				ike_alg_add((struct ike_alg *)&encrypt_desc_twofish_ssh);
				break;
			case ENCR_SERPENT_CBC:
				desc = &encrypt_desc_serpent;
				break;
			default:
				continue;			
		}
		ike_alg_add((struct ike_alg *)desc);
	}
	enumerator->destroy(enumerator);

	if (mpz_init_set_str(&groupgenerator, MODP_GENERATOR, 10) != 0
	 || mpz_init_set_str(&modp1024_modulus, MODP1024_MODULUS, 16) != 0
	 || mpz_init_set_str(&modp1536_modulus, MODP1536_MODULUS, 16) != 0
	 || mpz_init_set_str(&modp2048_modulus, MODP2048_MODULUS, 16) != 0
	 || mpz_init_set_str(&modp3072_modulus, MODP3072_MODULUS, 16) != 0
	 || mpz_init_set_str(&modp4096_modulus, MODP4096_MODULUS, 16) != 0
	 || mpz_init_set_str(&modp6144_modulus, MODP6144_MODULUS, 16) != 0
	 || mpz_init_set_str(&modp8192_modulus, MODP8192_MODULUS, 16) != 0)
	{
		exit_log("mpz_init_set_str() failed in init_crypto()");
	}
#ifdef SELF_TEST
	if (!ike_alg_test())
	{
		exit_log("pluto cannot run due to failed crypto self-test");
	}
#endif
}

void free_crypto(void)
{
	mpz_clear(&groupgenerator);
	mpz_clear(&modp1024_modulus);
	mpz_clear(&modp1536_modulus);
	mpz_clear(&modp2048_modulus);
	mpz_clear(&modp3072_modulus);
	mpz_clear(&modp4096_modulus);
	mpz_clear(&modp6144_modulus);
	mpz_clear(&modp8192_modulus);
}

/* Oakley group description
 *
 * See RFC2409 "The Internet key exchange (IKE)" 6.
 */

const struct oakley_group_desc unset_group = {0, NULL, 0};      /* magic signifier */

const struct oakley_group_desc oakley_group[OAKLEY_GROUP_SIZE] = {
#   define BYTES(bits) (((bits) + BITS_PER_BYTE - 1) / BITS_PER_BYTE)
	{ OAKLEY_GROUP_MODP1024, &modp1024_modulus, BYTES(1024) },
	{ OAKLEY_GROUP_MODP1536, &modp1536_modulus, BYTES(1536) },
	{ OAKLEY_GROUP_MODP2048, &modp2048_modulus, BYTES(2048) },
	{ OAKLEY_GROUP_MODP3072, &modp3072_modulus, BYTES(3072) },
	{ OAKLEY_GROUP_MODP4096, &modp4096_modulus, BYTES(4096) },
	{ OAKLEY_GROUP_MODP6144, &modp6144_modulus, BYTES(6144) },
	{ OAKLEY_GROUP_MODP8192, &modp8192_modulus, BYTES(8192) },
#   undef BYTES
};

const struct oakley_group_desc *lookup_group(u_int16_t group)
{
	int i;

	for (i = 0; i != countof(oakley_group); i++)
		if (group == oakley_group[i].group)
			return &oakley_group[i];
	return NULL;
}

/**
 * Converts IKEv1 encryption algorithm name to crypter name
 */
encryption_algorithm_t oakley_to_encryption_algorithm(int alg)
{
	switch (alg)
	{
		case OAKLEY_DES_CBC:
			return ENCR_DES;
		case OAKLEY_IDEA_CBC:
			return ENCR_IDEA; 
		case OAKLEY_BLOWFISH_CBC:
			return ENCR_BLOWFISH;
		case OAKLEY_RC5_R16_B64_CBC:
			return ENCR_RC5;
		case OAKLEY_3DES_CBC:
			return ENCR_3DES;
		case OAKLEY_CAST_CBC:
			return ENCR_CAST;
		case OAKLEY_AES_CBC:
			return ENCR_AES_CBC;
		case OAKLEY_SERPENT_CBC:
			return ENCR_SERPENT_CBC;
		case OAKLEY_TWOFISH_CBC:
		case OAKLEY_TWOFISH_CBC_SSH:
			return ENCR_TWOFISH_CBC;
		default:
			return ENCR_UNDEFINED;
	}
}

/**
 * Converts IKEv1 hash algorithm name to hasher name
 */
hash_algorithm_t oakley_to_hash_algorithm(int alg)
{
	switch (alg)
	{
		case OAKLEY_MD5:
			return HASH_MD5;
		case OAKLEY_SHA:
			return HASH_SHA1;
		case OAKLEY_SHA2_256:
			return HASH_SHA256;
		case OAKLEY_SHA2_384:
			return HASH_SHA384;
		case OAKLEY_SHA2_512:
			return HASH_SHA512;
		default:
			return HASH_UNKNOWN;
	}
}

/**
 * Converts IKEv1 hash algorithm name to IKEv2 prf name
 */
pseudo_random_function_t oakley_to_prf(int alg)
{
	switch (alg)
	{
		case OAKLEY_MD5:
			return PRF_HMAC_MD5;
		case OAKLEY_SHA:
			return PRF_HMAC_SHA1;
		case OAKLEY_SHA2_256:
			return PRF_HMAC_SHA2_256;
		case OAKLEY_SHA2_384:
			return PRF_HMAC_SHA2_384;
		case OAKLEY_SHA2_512:
			return PRF_HMAC_SHA2_512;
		default:
			return PRF_UNDEFINED;
	}
}

/**
 * Converts IKEv2 encryption to IKEv1 encryption algorithm
 */
int oakley_from_encryption_algorithm(encryption_algorithm_t alg)
{
	switch (alg)
	{
		case ENCR_DES:
			return OAKLEY_DES_CBC;
		case ENCR_3DES:
			return OAKLEY_3DES_CBC;
		case ENCR_RC5:
			return OAKLEY_RC5_R16_B64_CBC;
		case ENCR_IDEA:
			return OAKLEY_IDEA_CBC;
		case ENCR_CAST:
			return OAKLEY_CAST_CBC;
		case ENCR_BLOWFISH:
			return OAKLEY_BLOWFISH_CBC;
		case ENCR_AES_CBC:
			return OAKLEY_AES_CBC;
		case ENCR_CAMELLIA_CBC:
			return OAKLEY_CAMELLIA_CBC;
		case ENCR_SERPENT_CBC:
			return OAKLEY_SERPENT_CBC;
    	case ENCR_TWOFISH_CBC:
			return OAKLEY_TWOFISH_CBC;
		default:
			return 0;
	}
}

/**
 * Converts IKEv2 integrity to IKEv1 hash algorithm
 */
int oakley_from_integrity_algorithm(integrity_algorithm_t alg)
{
	switch (alg)
	{
		case AUTH_HMAC_MD5_96:
			return OAKLEY_MD5;
		case AUTH_HMAC_SHA1_96:
			return OAKLEY_SHA;
		case AUTH_HMAC_SHA2_256_128:
			return OAKLEY_SHA2_256;
		case AUTH_HMAC_SHA2_384_192:
			return OAKLEY_SHA2_384;
		case AUTH_HMAC_SHA2_512_256:
			return OAKLEY_SHA2_512;
		default:
			return 0;
	}
}

/**
 * Converts IKEv2 encryption to IKEv1 ESP encryption algorithm
 */
int esp_from_encryption_algorithm(encryption_algorithm_t alg)
{
	switch (alg)
	{
		case ENCR_DES:
			return ESP_DES;
		case ENCR_3DES:
			return ESP_3DES;
		case ENCR_RC5:
			return ESP_RC5;
		case ENCR_IDEA:
			return ESP_IDEA;
		case ENCR_CAST:
			return ESP_CAST;
		case ENCR_BLOWFISH:
			return ESP_BLOWFISH;
		case ENCR_NULL:
			return ESP_NULL;
		case ENCR_AES_CBC:
			return ESP_AES;
		case ENCR_AES_CTR:
			return ESP_AES_CTR;
		case ENCR_AES_CCM_ICV8:
			return ESP_AES_CCM_8;
		case ENCR_AES_CCM_ICV12:
			return ESP_AES_CCM_12;
		case ENCR_AES_CCM_ICV16:
			return ESP_AES_CCM_16;
		case ENCR_AES_GCM_ICV8:
			return ESP_AES_GCM_8;
		case ENCR_AES_GCM_ICV12:
			return ESP_AES_GCM_12;
		case ENCR_AES_GCM_ICV16:
			return ESP_AES_GCM_16;
		case ENCR_CAMELLIA_CBC:
			return ESP_CAMELLIA;
		case ENCR_SERPENT_CBC:
			return ESP_SERPENT;
    	case ENCR_TWOFISH_CBC:
			return ESP_TWOFISH;
		default:
			return 0;
	}
}

/**
 * Converts IKEv2 integrity to IKEv1 ESP authentication algorithm
 */
int esp_from_integrity_algorithm(integrity_algorithm_t alg)
{
	switch (alg)
	{
		case AUTH_HMAC_MD5_96:
			return AUTH_ALGORITHM_HMAC_MD5;
		case AUTH_HMAC_SHA1_96:
			return AUTH_ALGORITHM_HMAC_SHA1;
		case AUTH_AES_XCBC_96:
			return AUTH_ALGORITHM_AES_XCBC_MAC;
		case AUTH_HMAC_SHA2_256_128:
			return AUTH_ALGORITHM_HMAC_SHA2_256;
		case AUTH_HMAC_SHA2_384_192:
			return AUTH_ALGORITHM_HMAC_SHA2_384;
		case AUTH_HMAC_SHA2_512_256:
			return AUTH_ALGORITHM_HMAC_SHA2_512;
		default:
			return 0;
	}
}