ipsec: add support for chained buffers
[vpp.git] / src / vnet / crypto / crypto.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <stdbool.h>
17 #include <vlib/vlib.h>
18 #include <vnet/crypto/crypto.h>
19
20 vnet_crypto_main_t crypto_main;
21
22 static_always_inline void
23 crypto_set_op_status (vnet_crypto_op_t * ops[], u32 n_ops, int status)
24 {
25   while (n_ops--)
26     {
27       ops[0]->status = status;
28       ops++;
29     }
30 }
31
32 static_always_inline u32
33 vnet_crypto_process_ops_call_handler (vlib_main_t * vm,
34                                       vnet_crypto_main_t * cm,
35                                       vnet_crypto_op_id_t opt,
36                                       vnet_crypto_op_t * ops[],
37                                       vnet_crypto_op_chunk_t * chunks,
38                                       u32 n_ops)
39 {
40   u32 rv = 0;
41   if (n_ops == 0)
42     return 0;
43
44   if (chunks)
45     {
46
47       if (cm->chained_ops_handlers[opt] == 0)
48         crypto_set_op_status (ops, n_ops,
49                               VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
50       else
51         rv = (cm->chained_ops_handlers[opt]) (vm, ops, chunks, n_ops);
52     }
53   else
54     {
55       if (cm->ops_handlers[opt] == 0)
56         crypto_set_op_status (ops, n_ops,
57                               VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
58       else
59         rv = (cm->ops_handlers[opt]) (vm, ops, n_ops);
60     }
61   return rv;
62 }
63
64
65 static_always_inline u32
66 vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[],
67                                 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
68 {
69   vnet_crypto_main_t *cm = &crypto_main;
70   const int op_q_size = VLIB_FRAME_SIZE;
71   vnet_crypto_op_t *op_queue[op_q_size];
72   vnet_crypto_op_id_t opt, current_op_type = ~0;
73   u32 n_op_queue = 0;
74   u32 rv = 0, i;
75
76   ASSERT (n_ops >= 1);
77
78   for (i = 0; i < n_ops; i++)
79     {
80       opt = ops[i].op;
81
82       if (current_op_type != opt || n_op_queue >= op_q_size)
83         {
84           rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
85                                                       op_queue, chunks,
86                                                       n_op_queue);
87           n_op_queue = 0;
88           current_op_type = opt;
89         }
90
91       op_queue[n_op_queue++] = &ops[i];
92     }
93
94   rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
95                                               op_queue, chunks, n_op_queue);
96   return rv;
97 }
98
99 u32
100 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
101 {
102   return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops);
103 }
104
105 u32
106 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
107                                  vnet_crypto_op_chunk_t * chunks, u32 n_ops)
108 {
109   return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops);
110 }
111
112 u32
113 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
114                              char *desc)
115 {
116   vnet_crypto_main_t *cm = &crypto_main;
117   vnet_crypto_engine_t *p;
118
119   vec_add2 (cm->engines, p, 1);
120   p->name = name;
121   p->desc = desc;
122   p->priority = prio;
123
124   hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
125
126   return p - cm->engines;
127 }
128
129 static_always_inline void
130 crypto_set_active_engine (vnet_crypto_op_data_t * od,
131                           vnet_crypto_op_id_t id, u32 ei,
132                           crypto_op_class_type_t oct)
133 {
134   vnet_crypto_main_t *cm = &crypto_main;
135   vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
136
137   if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED)
138     {
139       if (ce->chained_ops_handlers[id])
140         {
141           od->active_engine_index_chained = ei;
142           cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id];
143         }
144     }
145
146   if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE)
147     {
148       if (ce->ops_handlers[id])
149         {
150           od->active_engine_index_simple = ei;
151           cm->ops_handlers[id] = ce->ops_handlers[id];
152         }
153     }
154 }
155
156 int
157 vnet_crypto_set_handler2 (char *alg_name, char *engine,
158                           crypto_op_class_type_t oct)
159 {
160   uword *p;
161   vnet_crypto_main_t *cm = &crypto_main;
162   vnet_crypto_alg_data_t *ad;
163   int i;
164
165   p = hash_get_mem (cm->alg_index_by_name, alg_name);
166   if (!p)
167     return -1;
168
169   ad = vec_elt_at_index (cm->algs, p[0]);
170
171   p = hash_get_mem (cm->engine_index_by_name, engine);
172   if (!p)
173     return -1;
174
175   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i += 2)
176     {
177       vnet_crypto_op_data_t *od;
178       vnet_crypto_op_id_t id = ad->op_by_type[i];
179       if (id == 0)
180         continue;
181
182       od = cm->opt_data + id;
183       crypto_set_active_engine (od, id, p[0], oct);
184     }
185
186   return 0;
187 }
188
189 int
190 vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
191 {
192   vnet_crypto_main_t *cm = &crypto_main;
193
194   return (alg < vec_len (cm->ops_handlers) && NULL != cm->ops_handlers[alg]);
195 }
196
197 void
198 vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index,
199                                          vnet_crypto_op_id_t opt,
200                                          vnet_crypto_ops_handler_t * fn,
201                                          vnet_crypto_chained_ops_handler_t *
202                                          cfn)
203 {
204   vnet_crypto_main_t *cm = &crypto_main;
205   vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
206   vnet_crypto_op_data_t *otd = cm->opt_data + opt;
207   vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
208                         CLIB_CACHE_LINE_BYTES);
209   vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
210                         CLIB_CACHE_LINE_BYTES);
211
212   if (fn)
213     {
214       e->ops_handlers[opt] = fn;
215       if (otd->active_engine_index_simple == ~0)
216         {
217           otd->active_engine_index_simple = engine_index;
218           cm->ops_handlers[opt] = fn;
219         }
220
221       ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple);
222       if (ae->priority < e->priority)
223         crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE);
224     }
225
226   if (cfn)
227     {
228       e->chained_ops_handlers[opt] = cfn;
229       if (otd->active_engine_index_chained == ~0)
230         {
231           otd->active_engine_index_chained = engine_index;
232           cm->chained_ops_handlers[opt] = cfn;
233         }
234
235       ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained);
236       if (ae->priority < e->priority)
237         crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED);
238     }
239
240   return;
241 }
242
243 void
244 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
245                                   vnet_crypto_op_id_t opt,
246                                   vnet_crypto_ops_handler_t * fn)
247 {
248   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0);
249 }
250
251 void
252 vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index,
253                                           vnet_crypto_op_id_t opt,
254                                           vnet_crypto_chained_ops_handler_t *
255                                           fn)
256 {
257   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn);
258 }
259
260 void
261 vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
262                                    vnet_crypto_op_id_t opt,
263                                    vnet_crypto_ops_handler_t * fn,
264                                    vnet_crypto_chained_ops_handler_t * cfn)
265 {
266   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn);
267 }
268
269 void
270 vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
271                                   vnet_crypto_key_handler_t * key_handler)
272 {
273   vnet_crypto_main_t *cm = &crypto_main;
274   vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
275   e->key_op_handler = key_handler;
276   return;
277 }
278
279 static int
280 vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
281 {
282   switch (alg)
283     {
284     case VNET_CRYPTO_N_ALGS:
285       return 0;
286     case VNET_CRYPTO_ALG_NONE:
287       return 1;
288
289 #define _(n, s, l) \
290       case VNET_CRYPTO_ALG_##n: \
291         if ((l) == length) \
292           return 1;        \
293         break;
294       foreach_crypto_cipher_alg foreach_crypto_aead_alg
295 #undef _
296         /* HMAC allows any key length */
297 #define _(n, s) \
298       case VNET_CRYPTO_ALG_HMAC_##n: \
299         return 1;
300         foreach_crypto_hmac_alg
301 #undef _
302     }
303
304   return 0;
305 }
306
307 u32
308 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
309                      u16 length)
310 {
311   u32 index;
312   vnet_crypto_main_t *cm = &crypto_main;
313   vnet_crypto_engine_t *engine;
314   vnet_crypto_key_t *key;
315
316   if (!vnet_crypto_key_len_check (alg, length))
317     return ~0;
318
319   pool_get_zero (cm->keys, key);
320   index = key - cm->keys;
321   key->alg = alg;
322   vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
323   clib_memcpy (key->data, data, length);
324
325   /* *INDENT-OFF* */
326   vec_foreach (engine, cm->engines)
327     if (engine->key_op_handler)
328       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
329   /* *INDENT-ON* */
330   return index;
331 }
332
333 void
334 vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
335 {
336   vnet_crypto_main_t *cm = &crypto_main;
337   vnet_crypto_engine_t *engine;
338   vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
339
340   /* *INDENT-OFF* */
341   vec_foreach (engine, cm->engines)
342     if (engine->key_op_handler)
343       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
344   /* *INDENT-ON* */
345
346   clib_memset (key->data, 0, vec_len (key->data));
347   vec_free (key->data);
348   pool_put (cm->keys, key);
349 }
350
351 static void
352 vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
353                               vnet_crypto_op_id_t did, char *name, u8 is_aead)
354 {
355   vnet_crypto_op_type_t eopt, dopt;
356   vnet_crypto_main_t *cm = &crypto_main;
357
358   cm->algs[alg].name = name;
359   cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
360   cm->opt_data[eid].active_engine_index_simple = ~0;
361   cm->opt_data[did].active_engine_index_simple = ~0;
362   cm->opt_data[eid].active_engine_index_chained = ~0;
363   cm->opt_data[did].active_engine_index_chained = ~0;
364   if (is_aead)
365     {
366       eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
367       dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
368     }
369   else
370     {
371       eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
372       dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
373     }
374   cm->opt_data[eid].type = eopt;
375   cm->opt_data[did].type = dopt;
376   cm->algs[alg].op_by_type[eopt] = eid;
377   cm->algs[alg].op_by_type[dopt] = did;
378   hash_set_mem (cm->alg_index_by_name, name, alg);
379 }
380
381 static void
382 vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
383                             vnet_crypto_op_id_t id, char *name)
384 {
385   vnet_crypto_main_t *cm = &crypto_main;
386   cm->algs[alg].name = name;
387   cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
388   cm->opt_data[id].alg = alg;
389   cm->opt_data[id].active_engine_index_simple = ~0;
390   cm->opt_data[id].active_engine_index_chained = ~0;
391   cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
392   hash_set_mem (cm->alg_index_by_name, name, alg);
393 }
394
395 clib_error_t *
396 vnet_crypto_init (vlib_main_t * vm)
397 {
398   vnet_crypto_main_t *cm = &crypto_main;
399   vlib_thread_main_t *tm = vlib_get_thread_main ();
400   cm->engine_index_by_name = hash_create_string ( /* size */ 0,
401                                                  sizeof (uword));
402   cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
403   vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
404   vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
405 #define _(n, s, l) \
406   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
407                                 VNET_CRYPTO_OP_##n##_ENC, \
408                                 VNET_CRYPTO_OP_##n##_DEC, s, 0);
409   foreach_crypto_cipher_alg;
410 #undef _
411 #define _(n, s, l) \
412   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
413                                 VNET_CRYPTO_OP_##n##_ENC, \
414                                 VNET_CRYPTO_OP_##n##_DEC, s, 1);
415   foreach_crypto_aead_alg;
416 #undef _
417 #define _(n, s) \
418   vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
419                               VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
420   foreach_crypto_hmac_alg;
421 #undef _
422   return 0;
423 }
424
425 VLIB_INIT_FUNCTION (vnet_crypto_init);
426
427 /*
428  * fd.io coding-style-patch-verification: ON
429  *
430  * Local Variables:
431  * eval: (c-set-style "gnu")
432  * End:
433  */