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
|
/*
* init.c : ipsecmb common code
*
* Copyright (c) 2015 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>
#include <ipsecmb/ipsecmb.h>
ipsecmb_main_t ipsecmb_main;
int
sa_expand_keys (u32 sa_index)
{
ipsecmb_main_t *imbm = &ipsecmb_main;
ipsec_sa_t *sa = pool_elt_at_index (ipsec_main.sad, sa_index);
ipsecmb_sa_t *samb = pool_elt_at_index (imbm->sad, sa_index);
if (sa->crypto_key_len > 0)
{
const keyexp_t keyexp_fn = imbm->crypto_algs[sa->crypto_alg].keyexp_fn;
keyexp_fn (sa->crypto_key, samb->aes_enc_key_expanded,
samb->aes_dec_key_expanded);
}
if (sa->integ_key_len > 0)
{
const u8 block_size = imbm->integ_algs[sa->integ_alg].block_size;
const hash_one_block_t hash_one_block_fn =
imbm->integ_algs[sa->integ_alg].hash_one_block_fn;
u8 buf[block_size];
int i = 0;
if (sa->integ_key_len > block_size)
{
return VNET_API_ERROR_SYSCALL_ERROR_1; // FIXME use correct value
}
memset (buf, 0x36, sizeof (buf));
for (i = 0; i < sa->integ_key_len; i++)
{
buf[i] ^= sa->integ_key[i];
}
hash_one_block_fn (buf, samb->ipad_hash);
memset (buf, 0x5c, sizeof (buf));
for (i = 0; i < sa->integ_key_len; i++)
{
buf[i] ^= sa->integ_key[i];
}
hash_one_block_fn (buf, samb->opad_hash);
}
return 0;
}
static clib_error_t *
ipsecmb_add_del_sa_session (u32 sa_index, u8 is_add)
{
ipsecmb_main_t *imbm = &ipsecmb_main;
if (is_add)
{
ipsecmb_sa_t *samb = NULL;
pool_get (imbm->sad, samb);
ASSERT (samb == pool_elt_at_index (imbm->sad, sa_index));
sa_expand_keys (sa_index);
}
else
{
pool_put_index (imbm->sad, sa_index);
}
return 0;
}
clib_error_t *
ipsecmb_check_esp_support (ipsec_sa_t * sa)
{
switch (sa->crypto_alg)
{
case IPSEC_CRYPTO_ALG_NONE:
break;
case IPSEC_CRYPTO_ALG_DES_CBC:
case IPSEC_CRYPTO_ALG_3DES_CBC:
return clib_error_return (0, "unsupported (3)des crypto-alg");
case IPSEC_CRYPTO_ALG_AES_CBC_128:
case IPSEC_CRYPTO_ALG_AES_CBC_192:
case IPSEC_CRYPTO_ALG_AES_CBC_256:
break;
case IPSEC_CRYPTO_ALG_AES_CTR_128:
case IPSEC_CRYPTO_ALG_AES_CTR_192:
case IPSEC_CRYPTO_ALG_AES_CTR_256:
return clib_error_return (0, "unsupported aes-ctr crypto-alg");
case IPSEC_CRYPTO_ALG_AES_GCM_128:
case IPSEC_CRYPTO_ALG_AES_GCM_192:
case IPSEC_CRYPTO_ALG_AES_GCM_256:
return clib_error_return (0, "unsupported aes-gcm crypto-alg");
case IPSEC_CRYPTO_N_ALG:
return clib_error_return (0, "invalid crypto-alg");
}
switch (sa->integ_alg)
{
case IPSEC_INTEG_ALG_NONE:
return clib_error_return (0, "unsupported none integ-alg");
case IPSEC_INTEG_ALG_MD5_96:
return clib_error_return (0, "unsupported md5 integ-alg");
case IPSEC_INTEG_ALG_SHA1_96:
case IPSEC_INTEG_ALG_SHA_256_96:
case IPSEC_INTEG_ALG_SHA_256_128:
case IPSEC_INTEG_ALG_SHA_384_192:
case IPSEC_INTEG_ALG_SHA_512_256:
break;
case IPSEC_INTEG_N_ALG:
return clib_error_return (0, "invalid integ-alg");
}
return 0;
}
clib_error_t *
ipsecmb_check_ah_support (ipsec_sa_t * sa)
{
switch (sa->integ_alg)
{
case IPSEC_INTEG_ALG_NONE:
return clib_error_return (0, "unsupported none integ-alg");
case IPSEC_INTEG_ALG_MD5_96:
return clib_error_return (0, "unsupported md5 integ-alg");
case IPSEC_INTEG_ALG_SHA1_96:
case IPSEC_INTEG_ALG_SHA_256_96:
case IPSEC_INTEG_ALG_SHA_256_128:
case IPSEC_INTEG_ALG_SHA_384_192:
case IPSEC_INTEG_ALG_SHA_512_256:
break;
case IPSEC_INTEG_N_ALG:
return clib_error_return (0, "invalid integ-alg");
}
return 0;
}
static clib_error_t *
ipsecmb_init (vlib_main_t * vm)
{
ipsecmb_main_t *imbm = &ipsecmb_main;
imbm->dev_urandom_fd = open ("/dev/urandom", O_RDONLY);
if (!imbm->dev_urandom_fd)
{
return clib_error_return_unix_fatal (0,
"Can't open /dev/urandom for read");
}
vlib_thread_main_t *tm = vlib_get_thread_main ();
imbm->crypto_algs = NULL;
imbm->integ_algs = NULL;
vec_validate (imbm->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
vec_validate (imbm->integ_algs, IPSEC_INTEG_N_ALG - 1);
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].block_size = 16;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size = 16;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size = 16;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].block_size = 8;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].block_size = 8;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].iv_size = 16;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].iv_size = 16;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].iv_size = 16;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].iv_size = 8;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].iv_size = 8;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].cipher_mode = CBC;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].cipher_mode = CBC;
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].cipher_mode = CBC;
ipsecmb_integ_alg_t *i;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
i->hash_alg = SHA1;
i->block_size = SHA1_BLOCK_SIZE;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
i->hash_alg = SHA_256;
i->block_size = SHA_256_BLOCK_SIZE;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
i->hash_alg = SHA_256;
i->block_size = SHA_256_BLOCK_SIZE;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
i->hash_alg = SHA_384;
i->block_size = SHA_384_BLOCK_SIZE;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
i->hash_alg = SHA_512;
i->block_size = SHA_512_BLOCK_SIZE;
vec_validate (imbm->mb_mgr, tm->n_vlib_mains - 1);
MB_MGR **mgr;
#define __set_funcs(arch) \
do \
{ \
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].keyexp_fn = \
aes_keyexp_128_##arch; \
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].keyexp_fn = \
aes_keyexp_192_##arch; \
imbm->crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].keyexp_fn = \
aes_keyexp_256_##arch; \
imbm->integ_algs[IPSEC_INTEG_ALG_SHA1_96].hash_one_block_fn = \
sha1_one_block_##arch; \
imbm->integ_algs[IPSEC_INTEG_ALG_SHA_256_96].hash_one_block_fn = \
sha256_one_block_##arch; \
imbm->integ_algs[IPSEC_INTEG_ALG_SHA_256_128].hash_one_block_fn = \
sha256_one_block_##arch; \
imbm->integ_algs[IPSEC_INTEG_ALG_SHA_384_192].hash_one_block_fn = \
sha384_one_block_##arch; \
imbm->integ_algs[IPSEC_INTEG_ALG_SHA_512_256].hash_one_block_fn = \
sha512_one_block_##arch; \
} \
while (0);
if (clib_cpu_supports_avx512f ())
{
__set_funcs (avx512);
vec_foreach (mgr, imbm->mb_mgr)
{
*mgr = alloc_mb_mgr (0);
init_mb_mgr_avx512 (*mgr);
}
}
else if (clib_cpu_supports_avx2 ())
{
__set_funcs (avx2);
vec_foreach (mgr, imbm->mb_mgr)
{
*mgr = alloc_mb_mgr (0);
init_mb_mgr_avx2 (*mgr);
}
}
else
{
__set_funcs (sse);
vec_foreach (mgr, imbm->mb_mgr)
{
*mgr = alloc_mb_mgr (0);
init_mb_mgr_sse (*mgr);
}
}
#undef __set_funcs
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
i->hash_output_length = 12;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
i->hash_output_length = 12;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
i->hash_output_length = 16;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
i->hash_output_length = 24;
i = &imbm->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
i->hash_output_length = 32;
vec_validate (imbm->per_thread_data, tm->n_vlib_mains - 1);
return 0;
}
VLIB_INIT_FUNCTION (ipsecmb_init);
static uword
ipsecmb_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
{
ipsec_main_t *im = &ipsec_main;
ipsec_register_ah_backend (vm, im, "ipsecmb backend",
"ah4-encrypt-ipsecmb",
"ah4-decrypt-ipsecmb",
"ah6-encrypt-ipsecmb",
"ah6-decrypt-ipsecmb",
ipsecmb_check_ah_support,
ipsecmb_add_del_sa_session);
ipsec_register_esp_backend (vm, im, "ipsecmb backend",
"esp4-encrypt-ipsecmb",
"esp4-decrypt-ipsecmb",
"esp6-encrypt-ipsecmb",
"esp6-decrypt-ipsecmb",
ipsecmb_check_esp_support,
ipsecmb_add_del_sa_session);
return 0;
}
/* *INDENT-OFF* */
VLIB_REGISTER_NODE (ipsecmb_process_node, static) = {
.function = ipsecmb_process,
.type = VLIB_NODE_TYPE_PROCESS,
.name = "ipsecmb-process",
.process_log2_n_stack_bytes = 17,
};
/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () = {
.version = VPP_BUILD_VER,
.description = "IPsecMB plugin",
.default_disabled = 1,
};
/* *INDENT-ON* */
/*
* fd.io coding-style-patch-verification: ON
*
* Local Variables:
* eval: (c-set-style "gnu")
* End:
*/
|