use the Diffie-Hellman functionality of libstrongswan
[strongswan.git] / src / pluto / crypto.c
1 /* crypto interfaces
2 * Copyright (C) 1998-2001 D. Hugh Redelmeier
3 * Copyright (C) 2007-2009 Andreas Steffen - Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <freeswan.h>
17 #include <ipsec_policy.h>
18
19 #include "constants.h"
20 #include "defs.h"
21 #include "crypto.h"
22 #include "ike_alg.h"
23 #include "log.h"
24
25 extern struct encrypt_desc encrypt_desc_3des;
26 extern struct encrypt_desc encrypt_desc_blowfish;
27 extern struct encrypt_desc encrypt_desc_aes;
28 extern struct encrypt_desc encrypt_desc_twofish;
29 extern struct encrypt_desc encrypt_desc_twofish_ssh;
30 extern struct encrypt_desc encrypt_desc_serpent;
31
32 extern struct hash_desc hash_desc_md5;
33 extern struct hash_desc hash_desc_sha1;
34 extern struct hash_desc hash_desc_sha2_256;
35 extern struct hash_desc hash_desc_sha2_384;
36 extern struct hash_desc hash_desc_sha2_512;
37
38 extern struct dh_desc dh_desc_modp_1024;
39 extern struct dh_desc dh_desc_modp_1536;
40 extern struct dh_desc dh_desc_modp_2048;
41 extern struct dh_desc dh_desc_modp_3072;
42 extern struct dh_desc dh_desc_modp_4096;
43 extern struct dh_desc dh_desc_modp_6144;
44 extern struct dh_desc dh_desc_modp_8192;
45
46 extern struct dh_desc dh_desc_ecp_256;
47 extern struct dh_desc dh_desc_ecp_384;
48 extern struct dh_desc dh_desc_ecp_521;
49 extern struct dh_desc dh_desc_ecp_192;
50 extern struct dh_desc dh_desc_ecp_224;
51
52 void init_crypto(void)
53 {
54 enumerator_t *enumerator;
55 encryption_algorithm_t encryption_alg;
56 hash_algorithm_t hash_alg;
57 diffie_hellman_group_t dh_group;
58 bool no_md5 = TRUE;
59 bool no_sha1 = TRUE;
60
61 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
62 while (enumerator->enumerate(enumerator, &hash_alg))
63 {
64 const struct hash_desc *desc;
65
66 switch (hash_alg)
67 {
68 case HASH_SHA1:
69 desc = &hash_desc_sha1;
70 no_sha1 = FALSE;
71 break;
72 case HASH_SHA256:
73 desc = &hash_desc_sha2_256;
74 break;
75 case HASH_SHA384:
76 desc = &hash_desc_sha2_384;
77 break;
78 case HASH_SHA512:
79 desc = &hash_desc_sha2_512;
80 break;
81 case HASH_MD5:
82 desc = &hash_desc_md5;
83 no_md5 = FALSE;
84 break;
85 default:
86 continue;
87 }
88 ike_alg_add((struct ike_alg *)desc);
89 }
90 enumerator->destroy(enumerator);
91
92 if (no_sha1)
93 {
94 exit_log("pluto cannot run without a SHA-1 hasher");
95 }
96 if (no_md5)
97 {
98 exit_log("pluto cannot run without an MD5 hasher");
99 }
100
101 enumerator = lib->crypto->create_crypter_enumerator(lib->crypto);
102 while (enumerator->enumerate(enumerator, &encryption_alg))
103 {
104 const struct encrypt_desc *desc;
105
106 switch (encryption_alg)
107 {
108 case ENCR_3DES:
109 desc = &encrypt_desc_3des;
110 break;
111 case ENCR_BLOWFISH:
112 desc = &encrypt_desc_blowfish;
113 break;
114 case ENCR_AES_CBC:
115 desc = &encrypt_desc_aes;
116 break;
117 case ENCR_TWOFISH_CBC:
118 desc = &encrypt_desc_twofish;
119 ike_alg_add((struct ike_alg *)&encrypt_desc_twofish_ssh);
120 break;
121 case ENCR_SERPENT_CBC:
122 desc = &encrypt_desc_serpent;
123 break;
124 default:
125 continue;
126 }
127 ike_alg_add((struct ike_alg *)desc);
128 }
129 enumerator->destroy(enumerator);
130
131 enumerator = lib->crypto->create_dh_enumerator(lib->crypto);
132 while (enumerator->enumerate(enumerator, &dh_group))
133 {
134 const struct dh_desc *desc;
135
136 switch (dh_group)
137 {
138 case MODP_1024_BIT:
139 desc = &dh_desc_modp_1024;
140 break;
141 case MODP_1536_BIT:
142 desc = &dh_desc_modp_1536;
143 break;
144 case MODP_2048_BIT:
145 desc = &dh_desc_modp_2048;
146 break;
147 case MODP_3072_BIT:
148 desc = &dh_desc_modp_3072;
149 break;
150 case MODP_4096_BIT:
151 desc = &dh_desc_modp_4096;
152 break;
153 case MODP_6144_BIT:
154 desc = &dh_desc_modp_6144;
155 break;
156 case MODP_8192_BIT:
157 desc = &dh_desc_modp_8192;
158 break;
159 case ECP_256_BIT:
160 desc = &dh_desc_ecp_256;
161 break;
162 case ECP_384_BIT:
163 desc = &dh_desc_ecp_384;
164 break;
165 case ECP_521_BIT:
166 desc = &dh_desc_ecp_521;
167 break;
168 case ECP_192_BIT:
169 desc = &dh_desc_ecp_192;
170 break;
171 case ECP_224_BIT:
172 desc = &dh_desc_ecp_224;
173 break;
174 default:
175 continue;
176 }
177 ike_alg_add((struct ike_alg *)desc);
178 }
179 enumerator->destroy(enumerator);
180
181 #ifdef SELF_TEST
182 if (!ike_alg_test())
183 {
184 exit_log("pluto cannot run due to failed crypto self-test");
185 }
186 #endif
187 }
188
189 void free_crypto(void)
190 {
191 /* currently nothing to do */
192 }
193
194 /**
195 * Converts IKEv1 encryption algorithm name to crypter name
196 */
197 encryption_algorithm_t oakley_to_encryption_algorithm(int alg)
198 {
199 switch (alg)
200 {
201 case OAKLEY_DES_CBC:
202 return ENCR_DES;
203 case OAKLEY_IDEA_CBC:
204 return ENCR_IDEA;
205 case OAKLEY_BLOWFISH_CBC:
206 return ENCR_BLOWFISH;
207 case OAKLEY_RC5_R16_B64_CBC:
208 return ENCR_RC5;
209 case OAKLEY_3DES_CBC:
210 return ENCR_3DES;
211 case OAKLEY_CAST_CBC:
212 return ENCR_CAST;
213 case OAKLEY_AES_CBC:
214 return ENCR_AES_CBC;
215 case OAKLEY_SERPENT_CBC:
216 return ENCR_SERPENT_CBC;
217 case OAKLEY_TWOFISH_CBC:
218 case OAKLEY_TWOFISH_CBC_SSH:
219 return ENCR_TWOFISH_CBC;
220 default:
221 return ENCR_UNDEFINED;
222 }
223 }
224
225 /**
226 * Converts IKEv1 hash algorithm name to hasher name
227 */
228 hash_algorithm_t oakley_to_hash_algorithm(int alg)
229 {
230 switch (alg)
231 {
232 case OAKLEY_MD5:
233 return HASH_MD5;
234 case OAKLEY_SHA:
235 return HASH_SHA1;
236 case OAKLEY_SHA2_256:
237 return HASH_SHA256;
238 case OAKLEY_SHA2_384:
239 return HASH_SHA384;
240 case OAKLEY_SHA2_512:
241 return HASH_SHA512;
242 default:
243 return HASH_UNKNOWN;
244 }
245 }
246
247 /**
248 * Converts IKEv1 hash algorithm name to IKEv2 prf name
249 */
250 pseudo_random_function_t oakley_to_prf(int alg)
251 {
252 switch (alg)
253 {
254 case OAKLEY_MD5:
255 return PRF_HMAC_MD5;
256 case OAKLEY_SHA:
257 return PRF_HMAC_SHA1;
258 case OAKLEY_SHA2_256:
259 return PRF_HMAC_SHA2_256;
260 case OAKLEY_SHA2_384:
261 return PRF_HMAC_SHA2_384;
262 case OAKLEY_SHA2_512:
263 return PRF_HMAC_SHA2_512;
264 default:
265 return PRF_UNDEFINED;
266 }
267 }
268
269 /**
270 * Converts IKEv2 encryption to IKEv1 encryption algorithm
271 */
272 int oakley_from_encryption_algorithm(encryption_algorithm_t alg)
273 {
274 switch (alg)
275 {
276 case ENCR_DES:
277 return OAKLEY_DES_CBC;
278 case ENCR_3DES:
279 return OAKLEY_3DES_CBC;
280 case ENCR_RC5:
281 return OAKLEY_RC5_R16_B64_CBC;
282 case ENCR_IDEA:
283 return OAKLEY_IDEA_CBC;
284 case ENCR_CAST:
285 return OAKLEY_CAST_CBC;
286 case ENCR_BLOWFISH:
287 return OAKLEY_BLOWFISH_CBC;
288 case ENCR_AES_CBC:
289 return OAKLEY_AES_CBC;
290 case ENCR_CAMELLIA_CBC:
291 return OAKLEY_CAMELLIA_CBC;
292 case ENCR_SERPENT_CBC:
293 return OAKLEY_SERPENT_CBC;
294 case ENCR_TWOFISH_CBC:
295 return OAKLEY_TWOFISH_CBC;
296 default:
297 return 0;
298 }
299 }
300
301 /**
302 * Converts IKEv2 integrity to IKEv1 hash algorithm
303 */
304 int oakley_from_integrity_algorithm(integrity_algorithm_t alg)
305 {
306 switch (alg)
307 {
308 case AUTH_HMAC_MD5_96:
309 return OAKLEY_MD5;
310 case AUTH_HMAC_SHA1_96:
311 return OAKLEY_SHA;
312 case AUTH_HMAC_SHA2_256_128:
313 return OAKLEY_SHA2_256;
314 case AUTH_HMAC_SHA2_384_192:
315 return OAKLEY_SHA2_384;
316 case AUTH_HMAC_SHA2_512_256:
317 return OAKLEY_SHA2_512;
318 default:
319 return 0;
320 }
321 }
322
323 /**
324 * Converts IKEv2 encryption to IKEv1 ESP encryption algorithm
325 */
326 int esp_from_encryption_algorithm(encryption_algorithm_t alg)
327 {
328 switch (alg)
329 {
330 case ENCR_DES:
331 return ESP_DES;
332 case ENCR_3DES:
333 return ESP_3DES;
334 case ENCR_RC5:
335 return ESP_RC5;
336 case ENCR_IDEA:
337 return ESP_IDEA;
338 case ENCR_CAST:
339 return ESP_CAST;
340 case ENCR_BLOWFISH:
341 return ESP_BLOWFISH;
342 case ENCR_NULL:
343 return ESP_NULL;
344 case ENCR_AES_CBC:
345 return ESP_AES;
346 case ENCR_AES_CTR:
347 return ESP_AES_CTR;
348 case ENCR_AES_CCM_ICV8:
349 return ESP_AES_CCM_8;
350 case ENCR_AES_CCM_ICV12:
351 return ESP_AES_CCM_12;
352 case ENCR_AES_CCM_ICV16:
353 return ESP_AES_CCM_16;
354 case ENCR_AES_GCM_ICV8:
355 return ESP_AES_GCM_8;
356 case ENCR_AES_GCM_ICV12:
357 return ESP_AES_GCM_12;
358 case ENCR_AES_GCM_ICV16:
359 return ESP_AES_GCM_16;
360 case ENCR_CAMELLIA_CBC:
361 return ESP_CAMELLIA;
362 case ENCR_SERPENT_CBC:
363 return ESP_SERPENT;
364 case ENCR_TWOFISH_CBC:
365 return ESP_TWOFISH;
366 default:
367 return 0;
368 }
369 }
370
371 /**
372 * Converts IKEv2 integrity to IKEv1 ESP authentication algorithm
373 */
374 int esp_from_integrity_algorithm(integrity_algorithm_t alg)
375 {
376 switch (alg)
377 {
378 case AUTH_HMAC_MD5_96:
379 return AUTH_ALGORITHM_HMAC_MD5;
380 case AUTH_HMAC_SHA1_96:
381 return AUTH_ALGORITHM_HMAC_SHA1;
382 case AUTH_AES_XCBC_96:
383 return AUTH_ALGORITHM_AES_XCBC_MAC;
384 case AUTH_HMAC_SHA2_256_128:
385 return AUTH_ALGORITHM_HMAC_SHA2_256;
386 case AUTH_HMAC_SHA2_384_192:
387 return AUTH_ALGORITHM_HMAC_SHA2_384;
388 case AUTH_HMAC_SHA2_512_256:
389 return AUTH_ALGORITHM_HMAC_SHA2_512;
390 default:
391 return 0;
392 }
393 }