diff options
Diffstat (limited to 'src/vnet/crypto')
-rw-r--r-- | src/vnet/crypto/cli.c | 59 | ||||
-rw-r--r-- | src/vnet/crypto/crypto.c | 118 | ||||
-rw-r--r-- | src/vnet/crypto/crypto.h | 116 | ||||
-rw-r--r-- | src/vnet/crypto/format.c | 39 |
4 files changed, 228 insertions, 104 deletions
diff --git a/src/vnet/crypto/cli.c b/src/vnet/crypto/cli.c index 792cc4bf243..4b0e093b02e 100644 --- a/src/vnet/crypto/cli.c +++ b/src/vnet/crypto/cli.c @@ -53,33 +53,58 @@ VLIB_CLI_COMMAND (show_crypto_engines_command, static) = .function = show_crypto_engines_command_fn, }; -static clib_error_t * -show_crypto_handlers_command_fn (vlib_main_t * vm, - unformat_input_t * input, vlib_cli_command_t * cmd) +static u8 * +format_vnet_crypto_handlers (u8 * s, va_list * args) { + vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t); vnet_crypto_main_t *cm = &crypto_main; - unformat_input_t _line_input, *line_input = &_line_input; - u8 *s = 0; - - if (unformat_user (input, unformat_line_input, line_input)) - unformat_free (line_input); + vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg); + u32 indent = format_get_indent (s); + int i, first = 1; - vlib_cli_output (vm, "%-40s%-20s%s", "Name", "Active", "Candidates"); - for (int i = 1; i < VNET_CRYPTO_N_OP_TYPES; i++) + for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++) { - vnet_crypto_op_type_data_t *otd = cm->opt_data + i; + vnet_crypto_op_data_t *od; vnet_crypto_engine_t *e; + vnet_crypto_op_id_t id = d->op_by_type[i]; + + if (id == 0) + continue; + + od = vec_elt_at_index (cm->opt_data, id); + if (first == 0) + s = format (s, "\n%U", format_white_space, indent); + s = format (s, "%-20U%-20U", format_vnet_crypto_op_type, od->type, + format_vnet_crypto_engine, od->active_engine_index,s); - vec_reset_length (s); vec_foreach (e, cm->engines) { - if (e->ops_handlers[i] != 0) + if (e->ops_handlers[id] != 0) s = format (s, "%U ", format_vnet_crypto_engine, e - cm->engines); } - vlib_cli_output (vm, "%-40U%-20U%v", format_vnet_crypto_op, i, - format_vnet_crypto_engine, otd->active_engine_index,s); + first = 0; } - vec_free (s); + return s; +} + + +static clib_error_t * +show_crypto_handlers_command_fn (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + unformat_input_t _line_input, *line_input = &_line_input; + int i; + + if (unformat_user (input, unformat_line_input, line_input)) + unformat_free (line_input); + + vlib_cli_output (vm, "%-20s%-20s%-20s%s", "Algo", "Type", "Active", + "Candidates"); + + for (i = 0; i < VNET_CRYPTO_N_ALGS; i++) + vlib_cli_output (vm, "%-20U%U", format_vnet_crypto_alg, i, + format_vnet_crypto_handlers, i); + return 0; } @@ -135,7 +160,7 @@ set_crypto_handler_command_fn (vlib_main_t * vm, u8 *value; /* *INDENT-OFF* */ - hash_foreach_mem (key, value, cm->ops_handler_index_by_name, + hash_foreach_mem (key, value, cm->alg_index_by_name, ({ (void) value; rc += vnet_crypto_set_handler (key, engine); diff --git a/src/vnet/crypto/crypto.c b/src/vnet/crypto/crypto.c index 9d0ad8b13ca..58b13638924 100644 --- a/src/vnet/crypto/crypto.c +++ b/src/vnet/crypto/crypto.c @@ -22,7 +22,7 @@ vnet_crypto_main_t crypto_main; static_always_inline u32 vnet_crypto_process_ops_call_handler (vlib_main_t * vm, vnet_crypto_main_t * cm, - vnet_crypto_op_type_t opt, + vnet_crypto_op_id_t opt, vnet_crypto_op_t * ops[], u32 n_ops) { if (n_ops == 0) @@ -48,7 +48,7 @@ vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops) vnet_crypto_main_t *cm = &crypto_main; const int op_q_size = VLIB_FRAME_SIZE; vnet_crypto_op_t *op_queue[op_q_size]; - vnet_crypto_op_type_t opt, current_op_type = ~0; + vnet_crypto_op_id_t opt, current_op_type = ~0; u32 n_op_queue = 0; u32 rv = 0, i; @@ -92,41 +92,49 @@ vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, } int -vnet_crypto_set_handler (char *ops_handler_name, char *engine) +vnet_crypto_set_handler (char *alg_name, char *engine) { uword *p; vnet_crypto_main_t *cm = &crypto_main; - vnet_crypto_op_type_t ot; - vnet_crypto_op_type_data_t *otd; + vnet_crypto_alg_data_t *ad; vnet_crypto_engine_t *ce; + int i; - p = hash_get_mem (cm->ops_handler_index_by_name, ops_handler_name); + p = hash_get_mem (cm->alg_index_by_name, alg_name); if (!p) return -1; - ot = p[0]; - otd = cm->opt_data + ot; + ad = vec_elt_at_index (cm->algs, p[0]); p = hash_get_mem (cm->engine_index_by_name, engine); if (!p) return -1; - ce = cm->engines + p[0]; - otd->active_engine_index = p[0]; - cm->ops_handlers[ot] = ce->ops_handlers[ot]; + ce = vec_elt_at_index (cm->engines, p[0]); + + for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++) + { + vnet_crypto_op_data_t *od; + vnet_crypto_op_id_t id = ad->op_by_type[i]; + if (id == 0) + continue; + od = vec_elt_at_index (cm->opt_data, id); + od->active_engine_index = p[0]; + cm->ops_handlers[id] = ce->ops_handlers[id]; + } return 0; } vlib_error_t * vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, - vnet_crypto_op_type_t opt, + vnet_crypto_op_id_t opt, vnet_crypto_ops_handler_t * fn) { vnet_crypto_main_t *cm = &crypto_main; vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index); - vnet_crypto_op_type_data_t *otd = cm->opt_data + opt; - vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_TYPES - 1, + vnet_crypto_op_data_t *otd = cm->opt_data + opt; + vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1, CLIB_CACHE_LINE_BYTES); e->ops_handlers[opt] = fn; @@ -146,47 +154,73 @@ vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, return 0; } +static void +vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid, + vnet_crypto_op_id_t did, char *name, u8 is_aead) +{ + vnet_crypto_op_type_t eopt, dopt; + vnet_crypto_main_t *cm = &crypto_main; + cm->algs[alg].name = name; + cm->opt_data[eid].alg = cm->opt_data[did].alg = alg; + cm->opt_data[eid].active_engine_index = ~0; + cm->opt_data[did].active_engine_index = ~0; + if (is_aead) + { + eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT; + dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT; + } + else + { + eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT; + dopt = VNET_CRYPTO_OP_TYPE_DECRYPT; + } + cm->opt_data[eid].type = eopt; + cm->opt_data[did].type = dopt; + cm->algs[alg].op_by_type[eopt] = eid; + cm->algs[alg].op_by_type[dopt] = did; + hash_set_mem (cm->alg_index_by_name, name, alg); +} + +static void +vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg, + vnet_crypto_op_id_t id, char *name) +{ + vnet_crypto_main_t *cm = &crypto_main; + cm->algs[alg].name = name; + cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id; + cm->opt_data[id].alg = alg; + cm->opt_data[id].active_engine_index = ~0; + cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC; + hash_set_mem (cm->alg_index_by_name, name, alg); +} + clib_error_t * vnet_crypto_init (vlib_main_t * vm) { vnet_crypto_main_t *cm = &crypto_main; vlib_thread_main_t *tm = vlib_get_thread_main (); -#define CRYPTO_ENC_STR "encrypt" -#define CRYPTO_DEC_STR "decrypt" -#define CRYPTO_HMAC_STR "hmac" - cm->engine_index_by_name = hash_create_string ( /* size */ 0, sizeof (uword)); - cm->ops_handler_index_by_name = hash_create_string (0, sizeof (uword)); - + cm->alg_index_by_name = hash_create_string (0, sizeof (uword)); vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES); vec_validate (cm->algs, VNET_CRYPTO_N_ALGS); - #define _(n, s) \ - cm->algs[VNET_CRYPTO_ALG_##n].name = s; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].alg = VNET_CRYPTO_ALG_##n; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].alg = VNET_CRYPTO_ALG_##n; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].desc = CRYPTO_ENC_STR; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].desc = CRYPTO_DEC_STR; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].active_engine_index = ~0; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].active_engine_index = ~0; \ - hash_set_mem (cm->ops_handler_index_by_name, CRYPTO_ENC_STR "-" s, \ - VNET_CRYPTO_OP_##n##_ENC); \ - hash_set_mem (cm->ops_handler_index_by_name, CRYPTO_DEC_STR "-" s, \ - VNET_CRYPTO_OP_##n##_DEC); - foreach_crypto_alg; + vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \ + VNET_CRYPTO_OP_##n##_ENC, \ + VNET_CRYPTO_OP_##n##_DEC, s, 0); + foreach_crypto_cipher_alg; #undef _ - #define _(n, s) \ - cm->algs[VNET_CRYPTO_ALG_##n].name = s; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].alg = VNET_CRYPTO_ALG_##n; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].desc = CRYPTO_HMAC_STR; \ - cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].active_engine_index = ~0; \ - hash_set_mem (cm->ops_handler_index_by_name, CRYPTO_HMAC_STR "-" s, \ - VNET_CRYPTO_OP_##n##_HMAC); - foreach_hmac_alg; + vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \ + VNET_CRYPTO_OP_##n##_ENC, \ + VNET_CRYPTO_OP_##n##_DEC, s, 1); + foreach_crypto_aead_alg; +#undef _ +#define _(n, s) \ + vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \ + VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s); + foreach_crypto_hmac_alg; #undef _ - return 0; } diff --git a/src/vnet/crypto/crypto.h b/src/vnet/crypto/crypto.h index 2bd40dc9c75..5d03d52756e 100644 --- a/src/vnet/crypto/crypto.h +++ b/src/vnet/crypto/crypto.h @@ -20,14 +20,19 @@ #include <vlib/vlib.h> -#define foreach_crypto_alg \ - _(DES_CBC, "des-cbc") \ - _(3DES_CBC, "3des-cbc") \ +#define foreach_crypto_cipher_alg \ + _(DES_CBC, "des-cbc") \ + _(3DES_CBC, "3des-cbc") \ _(AES_128_CBC, "aes-128-cbc") \ _(AES_192_CBC, "aes-192-cbc") \ _(AES_256_CBC, "aes-256-cbc") -#define foreach_hmac_alg \ +#define foreach_crypto_aead_alg \ + _(AES_128_GCM, "aes-128-gcm") \ + _(AES_192_GCM, "aes-192-gcm") \ + _(AES_256_GCM, "aes-256-gcm") + +#define foreach_crypto_hmac_alg \ _(MD5, "md5") \ _(SHA1, "sha-1") \ _(SHA224, "sha-224") \ @@ -35,14 +40,46 @@ _(SHA384, "sha-384") \ _(SHA512, "sha-512") + +#define foreach_crypto_op_type \ + _(ENCRYPT, "encrypt") \ + _(DECRYPT, "decrypt") \ + _(AEAD_ENCRYPT, "aead-encrypt") \ + _(AEAD_DECRYPT, "aead-decrypt") \ + _(HMAC, "hmac") + +typedef enum +{ +#define _(n, s) VNET_CRYPTO_OP_TYPE_##n, + foreach_crypto_op_type +#undef _ + VNET_CRYPTO_OP_N_TYPES, +} vnet_crypto_op_type_t; + +#define foreach_crypto_op_status \ + _(PENDING, "pending") \ + _(COMPLETED, "completed") \ + _(FAIL_NO_HANDLER, "no-handler") \ + _(FAIL_BAD_HMAC, "bad-hmac") \ + _(FAIL_DECRYPT, "decrypt-fail") + +typedef enum +{ +#define _(n, s) VNET_CRYPTO_OP_STATUS_##n, + foreach_crypto_op_status +#undef _ + VNET_CRYPTO_OP_N_STATUS, +} vnet_crypto_op_status_t; + /* *INDENT-OFF* */ typedef enum { #define _(n, s) VNET_CRYPTO_ALG_##n, - foreach_crypto_alg + foreach_crypto_cipher_alg + foreach_crypto_aead_alg #undef _ -#define _(n, s) VNET_CRYPTO_ALG_##n, - foreach_hmac_alg +#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n, + foreach_crypto_hmac_alg #undef _ VNET_CRYPTO_N_ALGS, } vnet_crypto_alg_t; @@ -51,68 +88,54 @@ typedef enum { VNET_CRYPTO_OP_NONE = 0, #define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC, - foreach_crypto_alg + foreach_crypto_cipher_alg + foreach_crypto_aead_alg #undef _ #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC, - foreach_hmac_alg + foreach_crypto_hmac_alg #undef _ - VNET_CRYPTO_N_OP_TYPES, -} vnet_crypto_op_type_t; + VNET_CRYPTO_N_OP_IDS, +} vnet_crypto_op_id_t; /* *INDENT-ON* */ typedef struct { char *name; + vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES]; } vnet_crypto_alg_data_t; -typedef enum -{ - VNET_CRYPTO_OP_STATUS_PENDING, - VNET_CRYPTO_OP_STATUS_COMPLETED, - VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER, - VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC, -} vnet_crypto_op_status_t; - typedef struct { CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); - vnet_crypto_op_type_t op:8; + vnet_crypto_op_id_t op:16; vnet_crypto_op_status_t status:8; - u8 key_len, hmac_trunc_len; - u16 flags; + u8 flags; #define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0) #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1) u32 len; + u16 aad_len; + u8 key_len, iv_len, digest_len, tag_len; u8 *key; u8 *iv; u8 *src; u8 *dst; + u8 *aad; + u8 *tag; + u8 *digest; uword user_data; } vnet_crypto_op_t; typedef struct { + vnet_crypto_op_type_t type; vnet_crypto_alg_t alg; - const char *desc; u32 active_engine_index; -} vnet_crypto_op_type_data_t; - -typedef struct -{ - CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); - u32 head; - u32 tail; - u32 size; - vnet_crypto_alg_t alg:8; - vnet_crypto_op_type_t op:8; - vnet_crypto_op_t *jobs[0]; -} vnet_crypto_queue_t; +} vnet_crypto_op_data_t; typedef struct { CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); clib_bitmap_t *act_queues; - vnet_crypto_queue_t *queues[VNET_CRYPTO_N_OP_TYPES]; } vnet_crypto_thread_t; typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm, @@ -123,7 +146,7 @@ u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, vlib_error_t *vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 provider_index, - vnet_crypto_op_type_t opt, + vnet_crypto_op_id_t opt, vnet_crypto_ops_handler_t * f); @@ -132,7 +155,7 @@ typedef struct char *name; char *desc; int priority; - vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_TYPES]; + vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS]; } vnet_crypto_engine_t; typedef struct @@ -140,10 +163,10 @@ typedef struct vnet_crypto_alg_data_t *algs; vnet_crypto_thread_t *threads; vnet_crypto_ops_handler_t **ops_handlers; - vnet_crypto_op_type_data_t opt_data[VNET_CRYPTO_N_OP_TYPES]; + vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS]; vnet_crypto_engine_t *engines; uword *engine_index_by_name; - uword *ops_handler_index_by_name; + uword *alg_index_by_name; } vnet_crypto_main_t; extern vnet_crypto_main_t crypto_main; @@ -160,10 +183,11 @@ int vnet_crypto_set_handler (char *ops_handler_name, char *engine); format_function_t format_vnet_crypto_alg; format_function_t format_vnet_crypto_engine; format_function_t format_vnet_crypto_op; - +format_function_t format_vnet_crypto_op_type; +format_function_t format_vnet_crypto_op_status; static_always_inline void -vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_type_t type) +vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type) { if (CLIB_DEBUG > 0) clib_memset (op, 0xfe, sizeof (*op)); @@ -171,6 +195,14 @@ vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_type_t type) op->flags = 0; } +static_always_inline vnet_crypto_op_type_t +vnet_crypto_get_op_type (vnet_crypto_op_id_t id) +{ + vnet_crypto_main_t *cm = &crypto_main; + vnet_crypto_op_data_t *od = vec_elt_at_index (cm->opt_data, id); + return od->type; +} + #endif /* included_vnet_crypto_crypto_h */ /* diff --git a/src/vnet/crypto/format.c b/src/vnet/crypto/format.c index c2786ee5afd..df811fe4f2f 100644 --- a/src/vnet/crypto/format.c +++ b/src/vnet/crypto/format.c @@ -30,10 +30,43 @@ u8 * format_vnet_crypto_op (u8 * s, va_list * args) { vnet_crypto_main_t *cm = &crypto_main; - vnet_crypto_op_type_t op = va_arg (*args, int); // vnet_crypto_op_type_t); - vnet_crypto_op_type_data_t *otd = cm->opt_data + op; + vnet_crypto_op_id_t op = va_arg (*args, int); // vnet_crypto_op_id_t); + vnet_crypto_op_data_t *otd = cm->opt_data + op; - return format (s, "%s-%U", otd->desc, format_vnet_crypto_alg, otd->alg); + return format (s, "%U-%U", format_vnet_crypto_op_type, otd->type, + format_vnet_crypto_alg, otd->alg); +} + +u8 * +format_vnet_crypto_op_type (u8 * s, va_list * args) +{ + vnet_crypto_op_type_t opt = va_arg (*args, vnet_crypto_op_type_t); + char *strings[] = { +#define _(n, s) [VNET_CRYPTO_OP_TYPE_##n] = s, + foreach_crypto_op_type +#undef _ + }; + + if (opt >= VNET_CRYPTO_OP_N_TYPES) + return format (s, "unknown"); + + return format (s, "%s", strings[opt]); +} + +u8 * +format_vnet_crypto_op_status (u8 * s, va_list * args) +{ + vnet_crypto_op_status_t st = va_arg (*args, vnet_crypto_op_status_t); + char *strings[] = { +#define _(n, s) [VNET_CRYPTO_OP_STATUS_##n] = s, + foreach_crypto_op_status +#undef _ + }; + + if (st >= VNET_CRYPTO_OP_N_STATUS) + return format (s, "unknown"); + + return format (s, "%s", strings[st]); } u8 * |