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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/crypto/crypto.h>
20 vnet_crypto_main_t crypto_main;
22 static_always_inline void
23 crypto_set_op_status (vnet_crypto_op_t * ops[], u32 n_ops, int status)
27 ops[0]->status = status;
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,
47 if (cm->chained_ops_handlers[opt] == 0)
48 crypto_set_op_status (ops, n_ops,
49 VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
51 rv = (cm->chained_ops_handlers[opt]) (vm, ops, chunks, n_ops);
55 if (cm->ops_handlers[opt] == 0)
56 crypto_set_op_status (ops, n_ops,
57 VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER);
59 rv = (cm->ops_handlers[opt]) (vm, ops, n_ops);
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)
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;
77 for (i = 0; i < n_ops; i++)
81 if (current_op_type != opt || n_op_queue >= op_q_size)
83 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
87 current_op_type = opt;
90 op_queue[n_op_queue++] = &ops[i];
93 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
94 op_queue, chunks, n_op_queue);
99 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
101 return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops);
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)
108 return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops);
112 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
115 vnet_crypto_main_t *cm = &crypto_main;
116 vnet_crypto_engine_t *p;
118 vec_add2 (cm->engines, p, 1);
123 hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
125 return p - cm->engines;
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)
133 vnet_crypto_main_t *cm = &crypto_main;
134 vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
136 if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED)
138 if (ce->chained_ops_handlers[id])
140 od->active_engine_index_chained = ei;
141 cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id];
145 if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE)
147 if (ce->ops_handlers[id])
149 od->active_engine_index_simple = ei;
150 cm->ops_handlers[id] = ce->ops_handlers[id];
156 vnet_crypto_set_handler2 (char *alg_name, char *engine,
157 crypto_op_class_type_t oct)
160 vnet_crypto_main_t *cm = &crypto_main;
161 vnet_crypto_alg_data_t *ad;
164 p = hash_get_mem (cm->alg_index_by_name, alg_name);
168 ad = vec_elt_at_index (cm->algs, p[0]);
170 p = hash_get_mem (cm->engine_index_by_name, engine);
174 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
176 vnet_crypto_op_data_t *od;
177 vnet_crypto_op_id_t id = ad->op_by_type[i];
181 od = cm->opt_data + id;
182 crypto_set_active_engine (od, id, p[0], oct);
189 vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
191 vnet_crypto_main_t *cm = &crypto_main;
193 return (alg < vec_len (cm->ops_handlers) && NULL != cm->ops_handlers[alg]);
197 vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index,
198 vnet_crypto_op_id_t opt,
199 vnet_crypto_ops_handler_t * fn,
200 vnet_crypto_chained_ops_handler_t *
203 vnet_crypto_main_t *cm = &crypto_main;
204 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
205 vnet_crypto_op_data_t *otd = cm->opt_data + opt;
206 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
207 CLIB_CACHE_LINE_BYTES);
208 vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
209 CLIB_CACHE_LINE_BYTES);
213 e->ops_handlers[opt] = fn;
214 if (otd->active_engine_index_simple == ~0)
216 otd->active_engine_index_simple = engine_index;
217 cm->ops_handlers[opt] = fn;
220 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple);
221 if (ae->priority < e->priority)
222 crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE);
227 e->chained_ops_handlers[opt] = cfn;
228 if (otd->active_engine_index_chained == ~0)
230 otd->active_engine_index_chained = engine_index;
231 cm->chained_ops_handlers[opt] = cfn;
234 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained);
235 if (ae->priority < e->priority)
236 crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED);
243 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
244 vnet_crypto_op_id_t opt,
245 vnet_crypto_ops_handler_t * fn)
247 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0);
251 vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index,
252 vnet_crypto_op_id_t opt,
253 vnet_crypto_chained_ops_handler_t *
256 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn);
260 vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
261 vnet_crypto_op_id_t opt,
262 vnet_crypto_ops_handler_t * fn,
263 vnet_crypto_chained_ops_handler_t * cfn)
265 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn);
269 vnet_crypto_register_async_handler (vlib_main_t * vm, u32 engine_index,
270 vnet_crypto_async_op_id_t opt,
271 vnet_crypto_frame_enqueue_t * enqueue_hdl,
272 vnet_crypto_frame_dequeue_t * dequeue_hdl)
274 vnet_crypto_main_t *cm = &crypto_main;
275 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
276 vnet_crypto_async_op_data_t *otd = cm->async_opt_data + opt;
277 vec_validate_aligned (cm->enqueue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS - 1,
278 CLIB_CACHE_LINE_BYTES);
279 vec_validate_aligned (cm->dequeue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS - 1,
280 CLIB_CACHE_LINE_BYTES);
282 /* both enqueue hdl and dequeue hdl should present */
283 if (!enqueue_hdl && !dequeue_hdl)
286 e->enqueue_handlers[opt] = enqueue_hdl;
287 e->dequeue_handlers[opt] = dequeue_hdl;
288 if (otd->active_engine_index_async == ~0)
290 otd->active_engine_index_async = engine_index;
291 cm->enqueue_handlers[opt] = enqueue_hdl;
292 cm->dequeue_handlers[opt] = dequeue_hdl;
295 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_async);
296 if (ae->priority < e->priority)
298 otd->active_engine_index_async = engine_index;
299 cm->enqueue_handlers[opt] = enqueue_hdl;
300 cm->dequeue_handlers[opt] = dequeue_hdl;
307 vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
308 vnet_crypto_key_handler_t * key_handler)
310 vnet_crypto_main_t *cm = &crypto_main;
311 vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
312 e->key_op_handler = key_handler;
317 vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
321 case VNET_CRYPTO_N_ALGS:
323 case VNET_CRYPTO_ALG_NONE:
327 case VNET_CRYPTO_ALG_##n: \
331 foreach_crypto_cipher_alg foreach_crypto_aead_alg
333 /* HMAC allows any key length */
335 case VNET_CRYPTO_ALG_HMAC_##n: \
337 foreach_crypto_hmac_alg
345 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
349 vnet_crypto_main_t *cm = &crypto_main;
350 vnet_crypto_engine_t *engine;
351 vnet_crypto_key_t *key;
353 if (!vnet_crypto_key_len_check (alg, length))
356 pool_get_zero (cm->keys, key);
357 index = key - cm->keys;
358 key->type = VNET_CRYPTO_KEY_TYPE_DATA;
360 vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
361 clib_memcpy (key->data, data, length);
363 vec_foreach (engine, cm->engines)
364 if (engine->key_op_handler)
365 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
371 vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
373 vnet_crypto_main_t *cm = &crypto_main;
374 vnet_crypto_engine_t *engine;
375 vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
378 vec_foreach (engine, cm->engines)
379 if (engine->key_op_handler)
380 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
383 if (key->type == VNET_CRYPTO_KEY_TYPE_DATA)
385 clib_memset (key->data, 0, vec_len (key->data));
386 vec_free (key->data);
388 else if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
390 key->index_crypto = key->index_integ = 0;
393 pool_put (cm->keys, key);
396 vnet_crypto_async_alg_t
397 vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
398 vnet_crypto_alg_t integ_alg)
400 #define _(c, h, s, k ,d) \
401 if (crypto_alg == VNET_CRYPTO_ALG_##c && \
402 integ_alg == VNET_CRYPTO_ALG_HMAC_##h) \
403 return VNET_CRYPTO_ALG_##c##_##h##_TAG##d;
404 foreach_crypto_link_async_alg
410 vnet_crypto_key_add_linked (vlib_main_t * vm,
411 vnet_crypto_key_index_t index_crypto,
412 vnet_crypto_key_index_t index_integ)
415 vnet_crypto_main_t *cm = &crypto_main;
416 vnet_crypto_engine_t *engine;
417 vnet_crypto_key_t *key_crypto, *key_integ, *key;
418 vnet_crypto_async_alg_t linked_alg;
420 key_crypto = pool_elt_at_index (cm->keys, index_crypto);
421 key_integ = pool_elt_at_index (cm->keys, index_integ);
423 if (!key_crypto || !key_integ)
426 linked_alg = vnet_crypto_link_algs (key_crypto->alg, key_integ->alg);
427 if (linked_alg == ~0)
430 pool_get_zero (cm->keys, key);
431 index = key - cm->keys;
432 key->type = VNET_CRYPTO_KEY_TYPE_LINK;
433 key->index_crypto = index_crypto;
434 key->index_integ = index_integ;
435 key->async_alg = linked_alg;
438 vec_foreach (engine, cm->engines)
439 if (engine->key_op_handler)
440 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
447 crypto_dispatch_enable_disable (int is_enable)
449 vlib_main_t *vm = vlib_get_main ();
450 vlib_thread_main_t *tm = vlib_get_thread_main ();
451 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
452 vnet_crypto_main_t *cm = &crypto_main;
453 u32 skip_master = vlib_num_workers () > 0, i;
454 u32 state_change = 0;
455 vlib_node_state_t state;
457 if (is_enable && cm->async_refcnt > 0)
460 state = VLIB_NODE_STATE_POLLING;
463 if (!is_enable && cm->async_refcnt == 0)
466 state = VLIB_NODE_STATE_DISABLED;
470 for (i = skip_master; i < tm->n_vlib_mains; i++)
471 vlib_node_set_state (vlib_mains[i], node->index, state);
476 static_always_inline void
477 crypto_set_active_async_engine (vnet_crypto_async_op_data_t * od,
478 vnet_crypto_async_op_id_t id, u32 ei)
480 vnet_crypto_main_t *cm = &crypto_main;
481 vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
483 if (ce->enqueue_handlers[id] && ce->dequeue_handlers[id])
485 od->active_engine_index_async = ei;
486 cm->enqueue_handlers[id] = ce->enqueue_handlers[id];
487 cm->dequeue_handlers[id] = ce->dequeue_handlers[id];
492 vnet_crypto_set_async_handler2 (char *alg_name, char *engine)
495 vnet_crypto_main_t *cm = &crypto_main;
496 vnet_crypto_async_alg_data_t *ad;
499 p = hash_get_mem (cm->async_alg_index_by_name, alg_name);
503 ad = vec_elt_at_index (cm->async_algs, p[0]);
505 p = hash_get_mem (cm->engine_index_by_name, engine);
509 for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
511 vnet_crypto_async_op_data_t *od;
512 vnet_crypto_async_op_id_t id = ad->op_by_type[i];
516 od = cm->async_opt_data + id;
517 crypto_set_active_async_engine (od, id, p[0]);
524 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name)
526 vnet_crypto_main_t *cm = &crypto_main;
527 vnet_crypto_async_next_node_t *nn = 0;
528 vlib_node_t *cc, *pn;
529 uword index = vec_len (cm->next_nodes);
531 pn = vlib_get_node_by_name (vm, (u8 *) post_node_name);
536 vec_foreach (cm->next_nodes, nn)
538 if (nn->node_idx == pn->index)
543 vec_validate (cm->next_nodes, index);
544 nn = vec_elt_at_index (cm->next_nodes, index);
546 cc = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
547 nn->next_idx = vlib_node_add_named_next (vm, cc->index, post_node_name);
548 nn->node_idx = pn->index;
554 vnet_crypto_request_async_mode (int is_enable)
556 vlib_main_t *vm = vlib_get_main ();
557 vlib_thread_main_t *tm = vlib_get_thread_main ();
558 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
559 vnet_crypto_main_t *cm = &crypto_main;
560 u32 skip_master = vlib_num_workers () > 0, i;
561 u32 state_change = 0;
562 vlib_node_state_t state;
564 if (is_enable && cm->async_refcnt == 0)
567 state = VLIB_NODE_STATE_POLLING;
570 if (!is_enable && cm->async_refcnt == 1)
573 state = VLIB_NODE_STATE_DISABLED;
577 for (i = skip_master; i < tm->n_vlib_mains; i++)
578 vlib_node_set_state (vlib_mains[i], node->index, state);
581 cm->async_refcnt += 1;
582 else if (cm->async_refcnt > 0)
583 cm->async_refcnt -= 1;
587 vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t op)
589 vnet_crypto_main_t *cm = &crypto_main;
591 return (op < vec_len (cm->enqueue_handlers) &&
592 NULL != cm->enqueue_handlers[op]);
596 vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
597 vnet_crypto_op_id_t did, char *name, u8 is_aead)
599 vnet_crypto_op_type_t eopt, dopt;
600 vnet_crypto_main_t *cm = &crypto_main;
602 cm->algs[alg].name = name;
603 cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
604 cm->opt_data[eid].active_engine_index_simple = ~0;
605 cm->opt_data[did].active_engine_index_simple = ~0;
606 cm->opt_data[eid].active_engine_index_chained = ~0;
607 cm->opt_data[did].active_engine_index_chained = ~0;
610 eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
611 dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
615 eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
616 dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
618 cm->opt_data[eid].type = eopt;
619 cm->opt_data[did].type = dopt;
620 cm->algs[alg].op_by_type[eopt] = eid;
621 cm->algs[alg].op_by_type[dopt] = did;
622 hash_set_mem (cm->alg_index_by_name, name, alg);
626 vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
627 vnet_crypto_op_id_t id, char *name)
629 vnet_crypto_main_t *cm = &crypto_main;
630 cm->algs[alg].name = name;
631 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
632 cm->opt_data[id].alg = alg;
633 cm->opt_data[id].active_engine_index_simple = ~0;
634 cm->opt_data[id].active_engine_index_chained = ~0;
635 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
636 hash_set_mem (cm->alg_index_by_name, name, alg);
640 vnet_crypto_init_async_data (vnet_crypto_async_alg_t alg,
641 vnet_crypto_async_op_id_t eid,
642 vnet_crypto_async_op_id_t did, char *name)
644 vnet_crypto_main_t *cm = &crypto_main;
646 cm->async_algs[alg].name = name;
647 cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT] = eid;
648 cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT] = did;
649 cm->async_opt_data[eid].type = VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT;
650 cm->async_opt_data[eid].alg = alg;
651 cm->async_opt_data[eid].active_engine_index_async = ~0;
652 cm->async_opt_data[eid].active_engine_index_async = ~0;
653 cm->async_opt_data[did].type = VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT;
654 cm->async_opt_data[did].alg = alg;
655 cm->async_opt_data[did].active_engine_index_async = ~0;
656 cm->async_opt_data[did].active_engine_index_async = ~0;
657 hash_set_mem (cm->async_alg_index_by_name, name, alg);
661 vnet_crypto_init (vlib_main_t * vm)
663 vnet_crypto_main_t *cm = &crypto_main;
664 vlib_thread_main_t *tm = vlib_get_thread_main ();
665 vnet_crypto_thread_t *ct = 0;
666 cm->engine_index_by_name = hash_create_string ( /* size */ 0,
668 cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
669 cm->async_alg_index_by_name = hash_create_string (0, sizeof (uword));
670 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
671 vec_foreach (ct, cm->threads)
672 pool_alloc_aligned (ct->frame_pool, 256, CLIB_CACHE_LINE_BYTES);
673 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
674 vec_validate (cm->async_algs, VNET_CRYPTO_N_ASYNC_ALGS);
675 clib_bitmap_validate (cm->async_active_ids, VNET_CRYPTO_ASYNC_OP_N_IDS - 1);
678 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
679 VNET_CRYPTO_OP_##n##_ENC, \
680 VNET_CRYPTO_OP_##n##_DEC, s, 0);
681 foreach_crypto_cipher_alg;
684 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
685 VNET_CRYPTO_OP_##n##_ENC, \
686 VNET_CRYPTO_OP_##n##_DEC, s, 1);
687 foreach_crypto_aead_alg;
690 vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
691 VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
692 foreach_crypto_hmac_alg;
694 #define _(n, s, k, t, a) \
695 vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, \
696 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
697 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, \
699 foreach_crypto_aead_async_alg
701 #define _(c, h, s, k ,d) \
702 vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##c##_##h##_TAG##d, \
703 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
704 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, \
706 foreach_crypto_link_async_alg
711 VLIB_INIT_FUNCTION (vnet_crypto_init);
714 * fd.io coding-style-patch-verification: ON
717 * eval: (c-set-style "gnu")