2 * Copyright (c) 2019 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.
16 #ifndef included_vnet_crypto_crypto_h
17 #define included_vnet_crypto_crypto_h
19 #include <vlib/vlib.h>
21 #define VNET_CRYPTO_FRAME_SIZE 64
23 /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */
24 #define foreach_crypto_cipher_alg \
25 _(DES_CBC, "des-cbc", 7) \
26 _(3DES_CBC, "3des-cbc", 24) \
27 _(AES_128_CBC, "aes-128-cbc", 16) \
28 _(AES_192_CBC, "aes-192-cbc", 24) \
29 _(AES_256_CBC, "aes-256-cbc", 32) \
30 _(AES_128_CTR, "aes-128-ctr", 16) \
31 _(AES_192_CTR, "aes-192-ctr", 24) \
32 _(AES_256_CTR, "aes-256-ctr", 32)
34 /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */
35 #define foreach_crypto_aead_alg \
36 _(AES_128_GCM, "aes-128-gcm", 16) \
37 _(AES_192_GCM, "aes-192-gcm", 24) \
38 _(AES_256_GCM, "aes-256-gcm", 32) \
39 _(CHACHA20_POLY1305, "chacha20-poly1305", 32)
41 #define foreach_crypto_hmac_alg \
44 _(SHA224, "sha-224") \
45 _(SHA256, "sha-256") \
46 _(SHA384, "sha-384") \
49 #define foreach_crypto_op_type \
50 _(ENCRYPT, "encrypt") \
51 _(DECRYPT, "decrypt") \
52 _(AEAD_ENCRYPT, "aead-encrypt") \
53 _(AEAD_DECRYPT, "aead-decrypt") \
58 #define _(n, s) VNET_CRYPTO_OP_TYPE_##n,
59 foreach_crypto_op_type
61 VNET_CRYPTO_OP_N_TYPES,
62 } vnet_crypto_op_type_t;
64 #define foreach_crypto_op_status \
66 _(PENDING, "pending") \
67 _(WORK_IN_PROGRESS, "work-in-progress") \
68 _(COMPLETED, "completed") \
69 _(FAIL_NO_HANDLER, "no-handler") \
70 _(FAIL_BAD_HMAC, "bad-hmac") \
71 _(FAIL_ENGINE_ERR, "engine-error")
75 /* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, TAG_LEN, AAD_LEN */
76 #define foreach_crypto_aead_async_alg \
77 _(AES_128_GCM, "aes-128-gcm-aad8", 16, 16, 8) \
78 _(AES_128_GCM, "aes-128-gcm-aad12", 16, 16, 12) \
79 _(AES_192_GCM, "aes-192-gcm-aad8", 24, 16, 8) \
80 _(AES_192_GCM, "aes-192-gcm-aad12", 24, 16, 12) \
81 _(AES_256_GCM, "aes-256-gcm-aad8", 32, 16, 8) \
82 _(AES_256_GCM, "aes-256-gcm-aad12", 32, 16, 12) \
83 _(CHACHA20_POLY1305, "chacha20-poly1305-aad8", 32, 16, 8) \
84 _(CHACHA20_POLY1305, "chacha20-poly1305-aad12", 32, 16, 12)
86 /* CRYPTO_ID, INTEG_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, DIGEST_LEN */
87 #define foreach_crypto_link_async_alg \
88 _ (AES_128_CBC, SHA1, "aes-128-cbc-hmac-sha-1", 16, 12) \
89 _ (AES_192_CBC, SHA1, "aes-192-cbc-hmac-sha-1", 24, 12) \
90 _ (AES_256_CBC, SHA1, "aes-256-cbc-hmac-sha-1", 32, 12) \
91 _ (AES_128_CBC, SHA224, "aes-128-cbc-hmac-sha-224", 16, 14) \
92 _ (AES_192_CBC, SHA224, "aes-192-cbc-hmac-sha-224", 24, 14) \
93 _ (AES_256_CBC, SHA224, "aes-256-cbc-hmac-sha-224", 32, 14) \
94 _ (AES_128_CBC, SHA256, "aes-128-cbc-hmac-sha-256", 16, 16) \
95 _ (AES_192_CBC, SHA256, "aes-192-cbc-hmac-sha-256", 24, 16) \
96 _ (AES_256_CBC, SHA256, "aes-256-cbc-hmac-sha-256", 32, 16) \
97 _ (AES_128_CBC, SHA384, "aes-128-cbc-hmac-sha-384", 16, 24) \
98 _ (AES_192_CBC, SHA384, "aes-192-cbc-hmac-sha-384", 24, 24) \
99 _ (AES_256_CBC, SHA384, "aes-256-cbc-hmac-sha-384", 32, 24) \
100 _ (AES_128_CBC, SHA512, "aes-128-cbc-hmac-sha-512", 16, 32) \
101 _ (AES_192_CBC, SHA512, "aes-192-cbc-hmac-sha-512", 24, 32) \
102 _ (AES_256_CBC, SHA512, "aes-256-cbc-hmac-sha-512", 32, 32)
104 #define foreach_crypto_async_op_type \
105 _(ENCRYPT, "async-encrypt") \
106 _(DECRYPT, "async-decrypt")
110 VNET_CRYPTO_KEY_OP_ADD,
111 VNET_CRYPTO_KEY_OP_DEL,
112 VNET_CRYPTO_KEY_OP_MODIFY,
113 } vnet_crypto_key_op_t;
117 #define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
118 foreach_crypto_op_status
120 VNET_CRYPTO_OP_N_STATUS,
121 } vnet_crypto_op_status_t;
126 VNET_CRYPTO_ALG_NONE = 0,
127 #define _(n, s, l) VNET_CRYPTO_ALG_##n,
128 foreach_crypto_cipher_alg
129 foreach_crypto_aead_alg
131 #define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
132 foreach_crypto_hmac_alg
139 #define _(n, s) VNET_CRYPTO_ASYNC_OP_TYPE_##n,
140 foreach_crypto_async_op_type
142 VNET_CRYPTO_ASYNC_OP_N_TYPES,
143 } vnet_crypto_async_op_type_t;
147 VNET_CRYPTO_ASYNC_ALG_NONE = 0,
148 #define _(n, s, k, t, a) \
149 VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a,
150 foreach_crypto_aead_async_alg
152 #define _(c, h, s, k ,d) \
153 VNET_CRYPTO_ALG_##c##_##h##_TAG##d,
154 foreach_crypto_link_async_alg
156 VNET_CRYPTO_N_ASYNC_ALGS,
157 } vnet_crypto_async_alg_t;
161 VNET_CRYPTO_ASYNC_OP_NONE = 0,
162 #define _(n, s, k, t, a) \
163 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
164 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC,
165 foreach_crypto_aead_async_alg
167 #define _(c, h, s, k ,d) \
168 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
169 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC,
170 foreach_crypto_link_async_alg
172 VNET_CRYPTO_ASYNC_OP_N_IDS,
173 } vnet_crypto_async_op_id_t;
182 vnet_crypto_alg_t alg:8;
188 vnet_crypto_async_alg_t async_alg:8;
191 #define VNET_CRYPTO_KEY_TYPE_DATA 0
192 #define VNET_CRYPTO_KEY_TYPE_LINK 1
198 VNET_CRYPTO_OP_NONE = 0,
199 #define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
200 foreach_crypto_cipher_alg
201 foreach_crypto_aead_alg
203 #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
204 foreach_crypto_hmac_alg
206 VNET_CRYPTO_N_OP_IDS,
207 } vnet_crypto_op_id_t;
216 } crypto_op_class_type_t;
221 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
222 } vnet_crypto_alg_data_t;
229 } vnet_crypto_op_chunk_t;
233 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
235 vnet_crypto_op_id_t op:16;
236 vnet_crypto_op_status_t status:8;
238 #define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
239 #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
240 #define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS (1 << 2)
257 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
264 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
279 STATIC_ASSERT_SIZEOF (vnet_crypto_op_t, CLIB_CACHE_LINE_BYTES);
283 vnet_crypto_op_type_t type;
284 vnet_crypto_alg_t alg;
285 u32 active_engine_index_simple;
286 u32 active_engine_index_chained;
287 } vnet_crypto_op_data_t;
291 vnet_crypto_async_op_type_t type;
292 vnet_crypto_async_alg_t alg;
293 u32 active_engine_index_async;
294 } vnet_crypto_async_op_data_t;
299 vnet_crypto_async_op_id_t op_by_type[VNET_CRYPTO_ASYNC_OP_N_TYPES];
300 } vnet_crypto_async_alg_data_t;
304 vnet_crypto_op_status_t status:8;
306 i16 crypto_start_offset; /* first buffer offset */
307 i16 integ_start_offset;
308 u32 crypto_total_length;
309 /* adj total_length for integ, e.g.4 bytes for IPSec ESN */
310 u16 integ_length_adj;
318 u8 flags; /**< share same VNET_CRYPTO_OP_FLAG_* values */
319 } vnet_crypto_async_frame_elt_t;
323 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
324 #define VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED 0
325 #define VNET_CRYPTO_FRAME_STATE_PENDING 1 /* frame waiting to be processed */
326 #define VNET_CRYPTO_FRAME_STATE_WORK_IN_PROGRESS 2
327 #define VNET_CRYPTO_FRAME_STATE_SUCCESS 3
328 #define VNET_CRYPTO_FRAME_STATE_ELT_ERROR 4
330 vnet_crypto_async_op_id_t op:8;
332 vnet_crypto_async_frame_elt_t elts[VNET_CRYPTO_FRAME_SIZE];
333 u32 buffer_indices[VNET_CRYPTO_FRAME_SIZE];
334 u16 next_node_index[VNET_CRYPTO_FRAME_SIZE];
335 u32 enqueue_thread_index;
336 } vnet_crypto_async_frame_t;
340 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
341 vnet_crypto_async_frame_t *frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
342 vnet_crypto_async_frame_t *frame_pool;
345 } vnet_crypto_thread_t;
347 typedef u32 vnet_crypto_key_index_t;
349 typedef u32 (vnet_crypto_chained_ops_handler_t) (vlib_main_t * vm,
350 vnet_crypto_op_t * ops[],
351 vnet_crypto_op_chunk_t *
354 typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
355 vnet_crypto_op_t * ops[], u32 n_ops);
357 typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
358 vnet_crypto_key_op_t kop,
359 vnet_crypto_key_index_t idx);
361 /** async crypto function handlers **/
363 (vnet_crypto_frame_enqueue_t) (vlib_main_t * vm,
364 vnet_crypto_async_frame_t * frame);
365 typedef vnet_crypto_async_frame_t *
366 (vnet_crypto_frame_dequeue_t) (vlib_main_t * vm, u32 * nb_elts_processed,
367 u32 * enqueue_thread_idx);
370 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
373 void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
374 vnet_crypto_op_id_t opt,
375 vnet_crypto_ops_handler_t * oph);
377 void vnet_crypto_register_chained_ops_handler (vlib_main_t * vm,
379 vnet_crypto_op_id_t opt,
380 vnet_crypto_chained_ops_handler_t
383 void vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
384 vnet_crypto_op_id_t opt,
385 vnet_crypto_ops_handler_t * fn,
386 vnet_crypto_chained_ops_handler_t *
389 void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
390 vnet_crypto_key_handler_t * keyh);
392 /** async crypto register functions */
393 u32 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name);
394 void vnet_crypto_register_async_handler (vlib_main_t * vm,
396 vnet_crypto_async_op_id_t opt,
397 vnet_crypto_frame_enqueue_t * enq_fn,
398 vnet_crypto_frame_dequeue_t *
406 vnet_crypto_key_handler_t *key_op_handler;
407 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
408 vnet_crypto_chained_ops_handler_t
409 * chained_ops_handlers[VNET_CRYPTO_N_OP_IDS];
410 vnet_crypto_frame_enqueue_t *enqueue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
411 vnet_crypto_frame_dequeue_t *dequeue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
412 } vnet_crypto_engine_t;
418 } vnet_crypto_async_next_node_t;
422 vnet_crypto_alg_data_t *algs;
423 vnet_crypto_thread_t *threads;
424 vnet_crypto_ops_handler_t **ops_handlers;
425 vnet_crypto_chained_ops_handler_t **chained_ops_handlers;
426 vnet_crypto_frame_enqueue_t **enqueue_handlers;
427 vnet_crypto_frame_dequeue_t **dequeue_handlers;
428 clib_bitmap_t *async_active_ids;
429 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
430 vnet_crypto_async_op_data_t async_opt_data[VNET_CRYPTO_ASYNC_OP_N_IDS];
431 vnet_crypto_engine_t *engines;
432 vnet_crypto_key_t *keys;
433 uword *engine_index_by_name;
434 uword *alg_index_by_name;
435 uword *async_alg_index_by_name;
436 vnet_crypto_async_alg_data_t *async_algs;
438 vnet_crypto_async_next_node_t *next_nodes;
439 u32 crypto_node_index;
440 #define VNET_CRYPTO_ASYNC_DISPATCH_POLLING 0
441 #define VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT 1
443 } vnet_crypto_main_t;
445 extern vnet_crypto_main_t crypto_main;
447 u32 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
448 vnet_crypto_op_chunk_t * chunks,
450 u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
454 int vnet_crypto_set_handler2 (char *ops_handler_name, char *engine,
455 crypto_op_class_type_t oct);
456 int vnet_crypto_is_set_handler (vnet_crypto_alg_t alg);
458 u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
459 u8 * data, u16 length);
460 void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
463 * Use 2 created keys to generate new key for linked algs (cipher + integ)
464 * The returned key index is to be used for linked alg only.
466 u32 vnet_crypto_key_add_linked (vlib_main_t * vm,
467 vnet_crypto_key_index_t index_crypto,
468 vnet_crypto_key_index_t index_integ);
470 clib_error_t *crypto_dispatch_enable_disable (int is_enable);
472 int vnet_crypto_set_async_handler2 (char *alg_name, char *engine);
474 int vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t opt);
476 void vnet_crypto_request_async_mode (int is_enable);
478 void vnet_crypto_set_async_dispatch_mode (u8 mode);
480 vnet_crypto_async_alg_t vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
481 vnet_crypto_alg_t integ_alg);
483 clib_error_t *crypto_dispatch_enable_disable (int is_enable);
485 format_function_t format_vnet_crypto_alg;
486 format_function_t format_vnet_crypto_engine;
487 format_function_t format_vnet_crypto_op;
488 format_function_t format_vnet_crypto_op_type;
489 format_function_t format_vnet_crypto_op_status;
490 unformat_function_t unformat_vnet_crypto_alg;
492 format_function_t format_vnet_crypto_async_op;
493 format_function_t format_vnet_crypto_async_alg;
494 format_function_t format_vnet_crypto_async_op_type;
496 static_always_inline void
497 vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
500 clib_memset (op, 0xfe, sizeof (*op));
507 static_always_inline vnet_crypto_op_type_t
508 vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
510 vnet_crypto_main_t *cm = &crypto_main;
511 ASSERT (id < VNET_CRYPTO_N_OP_IDS);
512 vnet_crypto_op_data_t *od = cm->opt_data + id;
516 static_always_inline vnet_crypto_key_t *
517 vnet_crypto_get_key (vnet_crypto_key_index_t index)
519 vnet_crypto_main_t *cm = &crypto_main;
520 return vec_elt_at_index (cm->keys, index);
523 static_always_inline int
524 vnet_crypto_set_handler (char *alg_name, char *engine)
526 return vnet_crypto_set_handler2 (alg_name, engine, CRYPTO_OP_BOTH);
529 /** async crypto inline functions **/
531 static_always_inline vnet_crypto_async_frame_t *
532 vnet_crypto_async_get_frame (vlib_main_t * vm, vnet_crypto_async_op_id_t opt)
534 vnet_crypto_main_t *cm = &crypto_main;
535 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
536 vnet_crypto_async_frame_t *f = ct->frames[opt];
540 pool_get_aligned (ct->frame_pool, f, CLIB_CACHE_LINE_BYTES);
542 clib_memset (f, 0xfe, sizeof (*f));
543 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
551 static_always_inline void
552 vnet_crypto_async_free_frame (vlib_main_t * vm,
553 vnet_crypto_async_frame_t * frame)
555 vnet_crypto_main_t *cm = &crypto_main;
556 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
557 pool_put (ct->frame_pool, frame);
560 static_always_inline int
561 vnet_crypto_async_submit_open_frame (vlib_main_t * vm,
562 vnet_crypto_async_frame_t * frame)
564 vnet_crypto_main_t *cm = &crypto_main;
565 vlib_thread_main_t *tm = vlib_get_thread_main ();
566 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
567 vnet_crypto_async_op_id_t opt = frame->op;
568 u32 i = vlib_num_workers () > 0;
570 int ret = (cm->enqueue_handlers[frame->op]) (vm, frame);
571 frame->enqueue_thread_index = vm->thread_index;
572 clib_bitmap_set_no_check (cm->async_active_ids, opt, 1);
573 if (PREDICT_TRUE (ret == 0))
575 vnet_crypto_async_frame_t *nf = 0;
576 frame->state = VNET_CRYPTO_FRAME_STATE_PENDING;
577 pool_get_aligned (ct->frame_pool, nf, CLIB_CACHE_LINE_BYTES);
579 clib_memset (nf, 0xfe, sizeof (*nf));
580 nf->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
583 ct->frames[opt] = nf;
586 if (cm->dispatch_mode == VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT)
588 for (; i < tm->n_vlib_mains; i++)
590 vlib_node_set_interrupt_pending (vlib_mains[i],
591 cm->crypto_node_index);
597 static_always_inline int
598 vnet_crypto_async_add_to_frame (vlib_main_t * vm,
599 vnet_crypto_async_frame_t ** frame,
601 u32 crypto_len, i16 integ_len_adj,
602 i16 crypto_start_offset,
603 u16 integ_start_offset,
606 u8 * iv, u8 * tag, u8 * aad, u8 flags)
608 vnet_crypto_async_frame_t *f = *frame;
609 vnet_crypto_async_frame_elt_t *fe;
612 if (PREDICT_FALSE (f->n_elts == VNET_CRYPTO_FRAME_SIZE))
614 vnet_crypto_async_op_id_t opt = f->op;
616 ret = vnet_crypto_async_submit_open_frame (vm, f);
617 if (PREDICT_FALSE (ret < 0))
619 f = vnet_crypto_async_get_frame (vm, opt);
624 fe = &f->elts[index];
626 fe->key_index = key_index;
627 fe->crypto_total_length = crypto_len;
628 fe->crypto_start_offset = crypto_start_offset;
629 fe->integ_start_offset = integ_start_offset;
630 fe->integ_length_adj = integ_len_adj;
635 f->buffer_indices[index] = buffer_index;
636 f->next_node_index[index] = next_node;
641 static_always_inline void
642 vnet_crypto_async_reset_frame (vnet_crypto_async_frame_t * f)
644 vnet_crypto_async_op_id_t opt;
646 ASSERT (f->state == VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED);
649 clib_memset (f, 0xfe, sizeof (*f));
650 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
655 #endif /* included_vnet_crypto_crypto_h */
658 * fd.io coding-style-patch-verification: ON
661 * eval: (c-set-style "gnu")