wireguard: notify key changes to crypto engine
[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 static_always_inline u32
65 vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[],
66                                 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
67 {
68   vnet_crypto_main_t *cm = &crypto_main;
69   const int op_q_size = VLIB_FRAME_SIZE;
70   vnet_crypto_op_t *op_queue[op_q_size];
71   vnet_crypto_op_id_t opt, current_op_type = ~0;
72   u32 n_op_queue = 0;
73   u32 rv = 0, i;
74
75   ASSERT (n_ops >= 1);
76
77   for (i = 0; i < n_ops; i++)
78     {
79       opt = ops[i].op;
80
81       if (current_op_type != opt || n_op_queue >= op_q_size)
82         {
83           rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
84                                                       op_queue, chunks,
85                                                       n_op_queue);
86           n_op_queue = 0;
87           current_op_type = opt;
88         }
89
90       op_queue[n_op_queue++] = &ops[i];
91     }
92
93   rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
94                                               op_queue, chunks, n_op_queue);
95   return rv;
96 }
97
98 u32
99 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
100 {
101   return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops);
102 }
103
104 u32
105 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
106                                  vnet_crypto_op_chunk_t * chunks, u32 n_ops)
107 {
108   return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops);
109 }
110
111 u32
112 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
113                              char *desc)
114 {
115   vnet_crypto_main_t *cm = &crypto_main;
116   vnet_crypto_engine_t *p;
117
118   vec_add2 (cm->engines, p, 1);
119   p->name = name;
120   p->desc = desc;
121   p->priority = prio;
122
123   hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
124
125   return p - cm->engines;
126 }
127
128 static_always_inline void
129 crypto_set_active_engine (vnet_crypto_op_data_t * od,
130                           vnet_crypto_op_id_t id, u32 ei,
131                           crypto_op_class_type_t oct)
132 {
133   vnet_crypto_main_t *cm = &crypto_main;
134   vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
135
136   if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED)
137     {
138       if (ce->chained_ops_handlers[id])
139         {
140           od->active_engine_index_chained = ei;
141           cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id];
142         }
143     }
144
145   if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE)
146     {
147       if (ce->ops_handlers[id])
148         {
149           od->active_engine_index_simple = ei;
150           cm->ops_handlers[id] = ce->ops_handlers[id];
151         }
152     }
153 }
154
155 int
156 vnet_crypto_set_handler2 (char *alg_name, char *engine,
157                           crypto_op_class_type_t oct)
158 {
159   uword *p;
160   vnet_crypto_main_t *cm = &crypto_main;
161   vnet_crypto_alg_data_t *ad;
162   int i;
163
164   p = hash_get_mem (cm->alg_index_by_name, alg_name);
165   if (!p)
166     return -1;
167
168   ad = vec_elt_at_index (cm->algs, p[0]);
169
170   p = hash_get_mem (cm->engine_index_by_name, engine);
171   if (!p)
172     return -1;
173
174   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
175     {
176       vnet_crypto_op_data_t *od;
177       vnet_crypto_op_id_t id = ad->op_by_type[i];
178       if (id == 0)
179         continue;
180
181       od = cm->opt_data + id;
182       crypto_set_active_engine (od, id, p[0], oct);
183     }
184
185   return 0;
186 }
187
188 int
189 vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
190 {
191   vnet_crypto_main_t *cm = &crypto_main;
192   vnet_crypto_op_id_t opt = 0;
193   int i;
194
195   if (alg >= vec_len (cm->algs))
196     return 0;
197
198   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
199     if ((opt = cm->algs[alg].op_by_type[i]) != 0)
200       break;
201
202   if (opt >= vec_len (cm->ops_handlers))
203     return 0;
204
205   return NULL != cm->ops_handlers[opt];
206 }
207
208 void
209 vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index,
210                                          vnet_crypto_op_id_t opt,
211                                          vnet_crypto_ops_handler_t * fn,
212                                          vnet_crypto_chained_ops_handler_t *
213                                          cfn)
214 {
215   vnet_crypto_main_t *cm = &crypto_main;
216   vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
217   vnet_crypto_op_data_t *otd = cm->opt_data + opt;
218   vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
219                         CLIB_CACHE_LINE_BYTES);
220   vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
221                         CLIB_CACHE_LINE_BYTES);
222
223   if (fn)
224     {
225       e->ops_handlers[opt] = fn;
226       if (otd->active_engine_index_simple == ~0)
227         {
228           otd->active_engine_index_simple = engine_index;
229           cm->ops_handlers[opt] = fn;
230         }
231
232       ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple);
233       if (ae->priority < e->priority)
234         crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE);
235     }
236
237   if (cfn)
238     {
239       e->chained_ops_handlers[opt] = cfn;
240       if (otd->active_engine_index_chained == ~0)
241         {
242           otd->active_engine_index_chained = engine_index;
243           cm->chained_ops_handlers[opt] = cfn;
244         }
245
246       ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained);
247       if (ae->priority < e->priority)
248         crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED);
249     }
250
251   return;
252 }
253
254 void
255 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
256                                   vnet_crypto_op_id_t opt,
257                                   vnet_crypto_ops_handler_t * fn)
258 {
259   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0);
260 }
261
262 void
263 vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index,
264                                           vnet_crypto_op_id_t opt,
265                                           vnet_crypto_chained_ops_handler_t *
266                                           fn)
267 {
268   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn);
269 }
270
271 void
272 vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
273                                    vnet_crypto_op_id_t opt,
274                                    vnet_crypto_ops_handler_t * fn,
275                                    vnet_crypto_chained_ops_handler_t * cfn)
276 {
277   vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn);
278 }
279
280 void
281 vnet_crypto_register_enqueue_handler (vlib_main_t *vm, u32 engine_index,
282                                       vnet_crypto_async_op_id_t opt,
283                                       vnet_crypto_frame_enqueue_t *enqueue_hdl)
284 {
285   vnet_crypto_main_t *cm = &crypto_main;
286   vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
287   vnet_crypto_async_op_data_t *otd = cm->async_opt_data + opt;
288   vec_validate_aligned (cm->enqueue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS,
289                         CLIB_CACHE_LINE_BYTES);
290
291   if (!enqueue_hdl)
292     return;
293
294   e->enqueue_handlers[opt] = enqueue_hdl;
295   if (otd->active_engine_index_async == ~0)
296     {
297       otd->active_engine_index_async = engine_index;
298       cm->enqueue_handlers[opt] = enqueue_hdl;
299     }
300
301   ae = vec_elt_at_index (cm->engines, otd->active_engine_index_async);
302   if (ae->priority <= e->priority)
303     {
304       otd->active_engine_index_async = engine_index;
305       cm->enqueue_handlers[opt] = enqueue_hdl;
306     }
307
308   return;
309 }
310
311 static int
312 engine_index_cmp (void *v1, void *v2)
313 {
314   u32 *a1 = v1;
315   u32 *a2 = v2;
316
317   if (*a1 > *a2)
318     return 1;
319   if (*a1 < *a2)
320     return -1;
321   return 0;
322 }
323
324 static void
325 vnet_crypto_update_cm_dequeue_handlers (void)
326 {
327   vnet_crypto_main_t *cm = &crypto_main;
328   vnet_crypto_async_op_data_t *otd;
329   vnet_crypto_engine_t *e;
330   u32 *active_engines = 0, *ei, last_ei = ~0, i;
331
332   vec_reset_length (cm->dequeue_handlers);
333
334   for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_IDS; i++)
335     {
336       otd = cm->async_opt_data + i;
337       if (otd->active_engine_index_async == ~0)
338         continue;
339       e = cm->engines + otd->active_engine_index_async;
340       if (!e->dequeue_handler)
341         continue;
342       vec_add1 (active_engines, otd->active_engine_index_async);
343     }
344
345   vec_sort_with_function (active_engines, engine_index_cmp);
346
347   vec_foreach (ei, active_engines)
348     {
349       if (ei[0] == last_ei)
350         continue;
351       if (ei[0] == ~0)
352         continue;
353
354       e = cm->engines + ei[0];
355       vec_add1 (cm->dequeue_handlers, e->dequeue_handler);
356       last_ei = ei[0];
357     }
358
359   vec_free (active_engines);
360 }
361
362 void
363 vnet_crypto_register_dequeue_handler (vlib_main_t *vm, u32 engine_index,
364                                       vnet_crypto_frame_dequeue_t *deq_fn)
365 {
366   vnet_crypto_main_t *cm = &crypto_main;
367   vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
368
369   if (!deq_fn)
370     return;
371
372   e->dequeue_handler = deq_fn;
373
374   vnet_crypto_update_cm_dequeue_handlers ();
375
376   return;
377 }
378
379 void
380 vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
381                                   vnet_crypto_key_handler_t * key_handler)
382 {
383   vnet_crypto_main_t *cm = &crypto_main;
384   vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
385   e->key_op_handler = key_handler;
386   return;
387 }
388
389 static int
390 vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
391 {
392   switch (alg)
393     {
394     case VNET_CRYPTO_N_ALGS:
395       return 0;
396     case VNET_CRYPTO_ALG_NONE:
397       return 1;
398
399 #define _(n, s, l) \
400       case VNET_CRYPTO_ALG_##n: \
401         if ((l) == length) \
402           return 1;        \
403         break;
404       foreach_crypto_cipher_alg foreach_crypto_aead_alg
405 #undef _
406         /* HMAC allows any key length */
407 #define _(n, s) \
408       case VNET_CRYPTO_ALG_HMAC_##n: \
409         return 1;
410         foreach_crypto_hmac_alg
411 #undef _
412
413 #define _(n, s)                                                               \
414   case VNET_CRYPTO_ALG_HASH_##n:                                              \
415     return 1;
416           foreach_crypto_hash_alg
417 #undef _
418     }
419
420   return 0;
421 }
422
423 u32
424 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
425                      u16 length)
426 {
427   u32 index;
428   vnet_crypto_main_t *cm = &crypto_main;
429   vnet_crypto_engine_t *engine;
430   vnet_crypto_key_t *key;
431
432   u8 need_barrier_sync = 0;
433
434   if (!vnet_crypto_key_len_check (alg, length))
435     return ~0;
436
437   need_barrier_sync = pool_get_will_expand (cm->keys);
438   /* If the cm->keys will expand, stop the parade. */
439   if (need_barrier_sync)
440     vlib_worker_thread_barrier_sync (vm);
441
442   pool_get_zero (cm->keys, key);
443
444   if (need_barrier_sync)
445     vlib_worker_thread_barrier_release (vm);
446
447   index = key - cm->keys;
448   key->type = VNET_CRYPTO_KEY_TYPE_DATA;
449   key->alg = alg;
450   vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
451   clib_memcpy (key->data, data, length);
452   /* *INDENT-OFF* */
453   vec_foreach (engine, cm->engines)
454     if (engine->key_op_handler)
455       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
456   /* *INDENT-ON* */
457   return index;
458 }
459
460 void
461 vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
462 {
463   vnet_crypto_main_t *cm = &crypto_main;
464   vnet_crypto_engine_t *engine;
465   vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
466
467   /* *INDENT-OFF* */
468   vec_foreach (engine, cm->engines)
469     if (engine->key_op_handler)
470       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
471   /* *INDENT-ON* */
472
473   if (key->type == VNET_CRYPTO_KEY_TYPE_DATA)
474     {
475       clib_memset (key->data, 0xfe, vec_len (key->data));
476       vec_free (key->data);
477     }
478   else if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
479     {
480       key->index_crypto = key->index_integ = ~0;
481     }
482
483   pool_put (cm->keys, key);
484 }
485
486 void
487 vnet_crypto_key_update (vlib_main_t *vm, vnet_crypto_key_index_t index)
488 {
489   vnet_crypto_main_t *cm = &crypto_main;
490   vnet_crypto_engine_t *engine;
491
492   vec_foreach (engine, cm->engines)
493     if (engine->key_op_handler)
494       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_MODIFY, index);
495 }
496
497 vnet_crypto_async_alg_t
498 vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
499                        vnet_crypto_alg_t integ_alg)
500 {
501 #define _(c, h, s, k ,d) \
502   if (crypto_alg == VNET_CRYPTO_ALG_##c && \
503       integ_alg == VNET_CRYPTO_ALG_HMAC_##h) \
504     return VNET_CRYPTO_ALG_##c##_##h##_TAG##d;
505   foreach_crypto_link_async_alg
506 #undef _
507     return ~0;
508 }
509
510 u32
511 vnet_crypto_key_add_linked (vlib_main_t * vm,
512                             vnet_crypto_key_index_t index_crypto,
513                             vnet_crypto_key_index_t index_integ)
514 {
515   u32 index;
516   vnet_crypto_main_t *cm = &crypto_main;
517   vnet_crypto_engine_t *engine;
518   vnet_crypto_key_t *key_crypto, *key_integ, *key;
519   vnet_crypto_async_alg_t linked_alg;
520
521   key_crypto = pool_elt_at_index (cm->keys, index_crypto);
522   key_integ = pool_elt_at_index (cm->keys, index_integ);
523
524   linked_alg = vnet_crypto_link_algs (key_crypto->alg, key_integ->alg);
525   if (linked_alg == ~0)
526     return ~0;
527
528   pool_get_zero (cm->keys, key);
529   index = key - cm->keys;
530   key->type = VNET_CRYPTO_KEY_TYPE_LINK;
531   key->index_crypto = index_crypto;
532   key->index_integ = index_integ;
533   key->async_alg = linked_alg;
534
535   /* *INDENT-OFF* */
536   vec_foreach (engine, cm->engines)
537     if (engine->key_op_handler)
538       engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
539   /* *INDENT-ON* */
540
541   return index;
542 }
543
544 static_always_inline void
545 crypto_set_active_async_engine (vnet_crypto_async_op_data_t * od,
546                                 vnet_crypto_async_op_id_t id, u32 ei)
547 {
548   vnet_crypto_main_t *cm = &crypto_main;
549   vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
550
551   if (ce->enqueue_handlers[id] && ce->dequeue_handler)
552     {
553       od->active_engine_index_async = ei;
554       cm->enqueue_handlers[id] = ce->enqueue_handlers[id];
555     }
556 }
557
558 int
559 vnet_crypto_set_async_handler2 (char *alg_name, char *engine)
560 {
561   uword *p;
562   vnet_crypto_main_t *cm = &crypto_main;
563   vnet_crypto_async_alg_data_t *ad;
564   int i;
565
566   p = hash_get_mem (cm->async_alg_index_by_name, alg_name);
567   if (!p)
568     return -1;
569
570   ad = vec_elt_at_index (cm->async_algs, p[0]);
571
572   p = hash_get_mem (cm->engine_index_by_name, engine);
573   if (!p)
574     return -1;
575
576   for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
577     {
578       vnet_crypto_async_op_data_t *od;
579       vnet_crypto_async_op_id_t id = ad->op_by_type[i];
580       if (id == 0)
581         continue;
582
583       od = cm->async_opt_data + id;
584       crypto_set_active_async_engine (od, id, p[0]);
585     }
586
587   vnet_crypto_update_cm_dequeue_handlers ();
588
589   return 0;
590 }
591
592 u32
593 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name)
594 {
595   vnet_crypto_main_t *cm = &crypto_main;
596   vnet_crypto_async_next_node_t *nn = 0;
597   vlib_node_t *cc, *pn;
598   uword index = vec_len (cm->next_nodes);
599
600   pn = vlib_get_node_by_name (vm, (u8 *) post_node_name);
601   if (!pn)
602     return ~0;
603
604   /* *INDENT-OFF* */
605   vec_foreach (nn, cm->next_nodes)
606     {
607       if (nn->node_idx == pn->index)
608         return nn->next_idx;
609     }
610   /* *INDENT-ON* */
611
612   vec_validate (cm->next_nodes, index);
613   nn = vec_elt_at_index (cm->next_nodes, index);
614
615   cc = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
616   nn->next_idx = vlib_node_add_named_next (vm, cc->index, post_node_name);
617   nn->node_idx = pn->index;
618
619   return nn->next_idx;
620 }
621
622 void
623 vnet_crypto_set_async_dispatch (u8 mode, u8 adaptive)
624 {
625   vlib_thread_main_t *tm = vlib_get_thread_main ();
626   u32 i, node_index = crypto_main.crypto_node_index;
627   vlib_node_state_t state =
628     mode ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_POLLING;
629
630   for (i = vlib_num_workers () > 0; i < tm->n_vlib_mains; i++)
631     {
632       vlib_main_t *ovm = vlib_get_main_by_index (i);
633       vlib_node_set_state (ovm, node_index, state);
634       vlib_node_set_flag (ovm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
635                           adaptive);
636     }
637 }
638
639 int
640 vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t op)
641 {
642   vnet_crypto_main_t *cm = &crypto_main;
643
644   return (op < vec_len (cm->enqueue_handlers) &&
645           NULL != cm->enqueue_handlers[op]);
646 }
647
648 static void
649 vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
650                               vnet_crypto_op_id_t did, char *name, u8 is_aead)
651 {
652   vnet_crypto_op_type_t eopt, dopt;
653   vnet_crypto_main_t *cm = &crypto_main;
654
655   cm->algs[alg].name = name;
656   cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
657   cm->opt_data[eid].active_engine_index_simple = ~0;
658   cm->opt_data[did].active_engine_index_simple = ~0;
659   cm->opt_data[eid].active_engine_index_chained = ~0;
660   cm->opt_data[did].active_engine_index_chained = ~0;
661   if (is_aead)
662     {
663       eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
664       dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
665     }
666   else
667     {
668       eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
669       dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
670     }
671   cm->opt_data[eid].type = eopt;
672   cm->opt_data[did].type = dopt;
673   cm->algs[alg].op_by_type[eopt] = eid;
674   cm->algs[alg].op_by_type[dopt] = did;
675   hash_set_mem (cm->alg_index_by_name, name, alg);
676 }
677
678 static void
679 vnet_crypto_init_hash_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t id,
680                             char *name)
681 {
682   vnet_crypto_main_t *cm = &crypto_main;
683   cm->algs[alg].name = name;
684   cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HASH] = id;
685   cm->opt_data[id].alg = alg;
686   cm->opt_data[id].active_engine_index_simple = ~0;
687   cm->opt_data[id].active_engine_index_chained = ~0;
688   cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HASH;
689   hash_set_mem (cm->alg_index_by_name, name, alg);
690 }
691
692 static void
693 vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
694                             vnet_crypto_op_id_t id, char *name)
695 {
696   vnet_crypto_main_t *cm = &crypto_main;
697   cm->algs[alg].name = name;
698   cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
699   cm->opt_data[id].alg = alg;
700   cm->opt_data[id].active_engine_index_simple = ~0;
701   cm->opt_data[id].active_engine_index_chained = ~0;
702   cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
703   hash_set_mem (cm->alg_index_by_name, name, alg);
704 }
705
706 static void
707 vnet_crypto_init_async_data (vnet_crypto_async_alg_t alg,
708                              vnet_crypto_async_op_id_t eid,
709                              vnet_crypto_async_op_id_t did, char *name)
710 {
711   vnet_crypto_main_t *cm = &crypto_main;
712
713   cm->async_algs[alg].name = name;
714   cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT] = eid;
715   cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT] = did;
716   cm->async_opt_data[eid].type = VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT;
717   cm->async_opt_data[eid].alg = alg;
718   cm->async_opt_data[eid].active_engine_index_async = ~0;
719   cm->async_opt_data[eid].active_engine_index_async = ~0;
720   cm->async_opt_data[did].type = VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT;
721   cm->async_opt_data[did].alg = alg;
722   cm->async_opt_data[did].active_engine_index_async = ~0;
723   cm->async_opt_data[did].active_engine_index_async = ~0;
724   hash_set_mem (cm->async_alg_index_by_name, name, alg);
725 }
726
727 clib_error_t *
728 vnet_crypto_init (vlib_main_t * vm)
729 {
730   vnet_crypto_main_t *cm = &crypto_main;
731   vlib_thread_main_t *tm = vlib_get_thread_main ();
732   vnet_crypto_thread_t *ct = 0;
733
734   cm->engine_index_by_name = hash_create_string ( /* size */ 0,
735                                                  sizeof (uword));
736   cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
737   cm->async_alg_index_by_name = hash_create_string (0, sizeof (uword));
738   vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
739   vec_foreach (ct, cm->threads)
740     pool_init_fixed (ct->frame_pool, VNET_CRYPTO_FRAME_POOL_SIZE);
741   vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
742   vec_validate (cm->async_algs, VNET_CRYPTO_N_ASYNC_ALGS);
743
744 #define _(n, s, l) \
745   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
746                                 VNET_CRYPTO_OP_##n##_ENC, \
747                                 VNET_CRYPTO_OP_##n##_DEC, s, 0);
748   foreach_crypto_cipher_alg;
749 #undef _
750 #define _(n, s, l) \
751   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
752                                 VNET_CRYPTO_OP_##n##_ENC, \
753                                 VNET_CRYPTO_OP_##n##_DEC, s, 1);
754   foreach_crypto_aead_alg;
755 #undef _
756 #define _(n, s) \
757   vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
758                               VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
759   foreach_crypto_hmac_alg;
760 #undef _
761 #define _(n, s)                                                               \
762   vnet_crypto_init_hash_data (VNET_CRYPTO_ALG_HASH_##n,                       \
763                               VNET_CRYPTO_OP_##n##_HASH, s);
764   foreach_crypto_hash_alg;
765 #undef _
766 #define _(n, s, k, t, a) \
767   vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, \
768                                VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
769                                VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, \
770                                s);
771   foreach_crypto_aead_async_alg
772 #undef _
773 #define _(c, h, s, k ,d) \
774   vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##c##_##h##_TAG##d, \
775                                VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
776                                VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, \
777                                s);
778     foreach_crypto_link_async_alg
779 #undef _
780     cm->crypto_node_index =
781     vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch")->index;
782
783   return 0;
784 }
785
786 VLIB_INIT_FUNCTION (vnet_crypto_init);
787
788 /*
789  * fd.io coding-style-patch-verification: ON
790  *
791  * Local Variables:
792  * eval: (c-set-style "gnu")
793  * End:
794  */