plugin-loader: Optionally use load option in each plugin section to load plugins
[strongswan.git] / src / libcharon / sa / keymat.c
1 /*
2 * Copyright (C) 2011 Tobias Brunner
3 * 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 "keymat.h"
17
18 #include <sa/ikev1/keymat_v1.h>
19 #include <sa/ikev2/keymat_v2.h>
20
21 static keymat_constructor_t keymat_v1_ctor = NULL, keymat_v2_ctor = NULL;
22
23 /**
24 * See header
25 */
26 keymat_t *keymat_create(ike_version_t version, bool initiator)
27 {
28 keymat_t *keymat = NULL;
29
30 switch (version)
31 {
32 case IKEV1:
33 #ifdef USE_IKEV1
34 keymat = keymat_v1_ctor ? keymat_v1_ctor(initiator)
35 : &keymat_v1_create(initiator)->keymat;
36 #endif
37 break;
38 case IKEV2:
39 #ifdef USE_IKEV2
40 keymat = keymat_v2_ctor ? keymat_v2_ctor(initiator)
41 : &keymat_v2_create(initiator)->keymat;
42 #endif
43 break;
44 default:
45 break;
46 }
47 return keymat;
48 }
49
50 /**
51 * Implicit key length for an algorithm
52 */
53 typedef struct {
54 /** IKEv2 algorithm identifier */
55 int alg;
56 /** key length in bits */
57 int len;
58 } keylen_entry_t;
59
60 /**
61 * See header.
62 */
63 int keymat_get_keylen_encr(encryption_algorithm_t alg)
64 {
65 keylen_entry_t map[] = {
66 {ENCR_DES, 64},
67 {ENCR_3DES, 192},
68 };
69 int i;
70
71 for (i = 0; i < countof(map); i++)
72 {
73 if (map[i].alg == alg)
74 {
75 return map[i].len;
76 }
77 }
78 return 0;
79 }
80
81 /**
82 * See header.
83 */
84 int keymat_get_keylen_integ(integrity_algorithm_t alg)
85 {
86 keylen_entry_t map[] = {
87 {AUTH_HMAC_MD5_96, 128},
88 {AUTH_HMAC_MD5_128, 128},
89 {AUTH_HMAC_SHA1_96, 160},
90 {AUTH_HMAC_SHA1_160, 160},
91 {AUTH_HMAC_SHA2_256_96, 256},
92 {AUTH_HMAC_SHA2_256_128, 256},
93 {AUTH_HMAC_SHA2_384_192, 384},
94 {AUTH_HMAC_SHA2_512_256, 512},
95 {AUTH_AES_XCBC_96, 128},
96 {AUTH_AES_CMAC_96, 128},
97 };
98 int i;
99
100 for (i = 0; i < countof(map); i++)
101 {
102 if (map[i].alg == alg)
103 {
104 return map[i].len;
105 }
106 }
107 return 0;
108 }
109
110 /**
111 * See header.
112 */
113 void keymat_register_constructor(ike_version_t version,
114 keymat_constructor_t create)
115 {
116 switch (version)
117 {
118 case IKEV1:
119 keymat_v1_ctor = create;
120 break;
121 case IKEV2:
122 keymat_v2_ctor = create;
123 break;
124 default:
125 break;
126 }
127 }