From 91f17dc7c4e79343b8fba924e7cea6380a0e6653 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Mon, 18 Mar 2019 18:59:25 +0100 Subject: crypto: introduce crypto infra Change-Id: Ibf320b3e7b054b686f3af9a55afd5d5bda9b1048 Signed-off-by: Damjan Marion Signed-off-by: Filip Tehlar --- src/vnet/crypto/cli.c | 101 +++++++++++++++++++++++++++++ src/vnet/crypto/crypto.c | 127 ++++++++++++++++++++++++++++++++++++ src/vnet/crypto/crypto.h | 163 +++++++++++++++++++++++++++++++++++++++++++++++ src/vnet/crypto/format.c | 61 ++++++++++++++++++ 4 files changed, 452 insertions(+) create mode 100644 src/vnet/crypto/cli.c create mode 100644 src/vnet/crypto/crypto.c create mode 100644 src/vnet/crypto/crypto.h create mode 100644 src/vnet/crypto/format.c (limited to 'src/vnet/crypto') diff --git a/src/vnet/crypto/cli.c b/src/vnet/crypto/cli.c new file mode 100644 index 00000000000..d93577ed113 --- /dev/null +++ b/src/vnet/crypto/cli.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +static clib_error_t * +show_crypto_engines_command_fn (vlib_main_t * vm, + unformat_input_t * input, + vlib_cli_command_t * cmd) +{ + unformat_input_t _line_input, *line_input = &_line_input; + vnet_crypto_main_t *cm = &crypto_main; + vnet_crypto_engine_t *p; + + if (unformat_user (input, unformat_line_input, line_input)) + unformat_free (line_input); + + if (vec_len (cm->engines) == 0) + { + vlib_cli_output (vm, "No crypto engines registered"); + return 0; + } + + vlib_cli_output (vm, "%-20s%-8s%s", "Name", "Prio", "Description"); + /* *INDENT-OFF* */ + vec_foreach (p, cm->engines) + { + vlib_cli_output (vm, "%-20s%-8u%s", p->name, p->priority, p->desc); + } + /* *INDENT-ON* */ + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (show_crypto_engines_command, static) = +{ + .path = "show crypto engines", + .short_help = "show crypto engines", + .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) +{ + 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); + + vlib_cli_output (vm, "%-40s%-20s%s", "Name", "Active", "Candidates"); + for (int i = 1; i < VNET_CRYPTO_N_OP_TYPES; i++) + { + vnet_crypto_op_type_data_t *otd = cm->opt_data + i; + vnet_crypto_engine_t *e; + + vec_reset_length (s); + vec_foreach (e, cm->engines) + { + if (e->ops_handlers[i] != 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); + } + vec_free (s); + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (show_crypto_handlers_command, static) = +{ + .path = "show crypto handlers", + .short_help = "show crypto handlers", + .function = show_crypto_handlers_command_fn, +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/crypto/crypto.c b/src/vnet/crypto/crypto.c new file mode 100644 index 00000000000..a6f45be053f --- /dev/null +++ b/src/vnet/crypto/crypto.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2018 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +vnet_crypto_main_t crypto_main; + +u32 +vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops) +{ + vnet_crypto_main_t *cm = &crypto_main; + u32 rv = 0, i; + + for (i = 0; i < n_ops; i++) + { + vnet_crypto_op_type_t opt = ops[i].op; + vnet_crypto_op_t *opp = &ops[i]; + + if (cm->ops_handlers[opt]) + rv += (cm->ops_handlers[opt]) (vm, &opp, 1); + else + ops[i].status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER; + } + + return rv; +} + +u32 +vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, + char *desc) +{ + vnet_crypto_main_t *cm = &crypto_main; + vnet_crypto_engine_t *p; + + vec_add2 (cm->engines, p, 1); + p->name = name; + p->desc = desc; + p->priority = prio; + + return p - cm->engines; +} + +vlib_error_t * +vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, + vnet_crypto_op_type_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, + CLIB_CACHE_LINE_BYTES); + e->ops_handlers[opt] = fn; + + if (otd->active_engine_index == ~0) + { + otd->active_engine_index = engine_index; + cm->ops_handlers[opt] = fn; + return 0; + } + ae = vec_elt_at_index (cm->engines, otd->active_engine_index); + if (ae->priority < e->priority) + { + otd->active_engine_index = engine_index; + cm->ops_handlers[opt] = fn; + } + + return 0; +} + +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 (); + const char *enc = "encrypt"; + const char *dec = "decrypt"; + const char *hmac = "hmac"; + + 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 = enc; \ + cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].desc = dec; \ + cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].active_engine_index = ~0; \ + cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].active_engine_index = ~0; + foreach_crypto_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 = hmac; \ + cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].active_engine_index = ~0; + foreach_hmac_alg; +#undef _ + + return 0; +} + +VLIB_INIT_FUNCTION (vnet_crypto_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/crypto/crypto.h b/src/vnet/crypto/crypto.h new file mode 100644 index 00000000000..9f4c85b36f5 --- /dev/null +++ b/src/vnet/crypto/crypto.h @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef included_vnet_crypto_crypto_h +#define included_vnet_crypto_crypto_h + +#define VNET_CRYPTO_RING_SIZE 512 + +#include + +#define foreach_crypto_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 \ + _(SHA1, "sha-1") \ + _(SHA224, "sha-224") \ + _(SHA256, "sha-256") \ + _(SHA384, "sha-384") \ + _(SHA512, "sha-512") + +/* *INDENT-OFF* */ +typedef enum +{ +#define _(n, s) VNET_CRYPTO_ALG_##n, + foreach_crypto_alg +#undef _ +#define _(n, s) VNET_CRYPTO_ALG_##n, + foreach_hmac_alg +#undef _ + VNET_CRYPTO_N_ALGS, +} vnet_crypto_alg_t; + +typedef enum +{ + VNET_CRYPTO_OP_NONE = 0, +#define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC, + foreach_crypto_alg +#undef _ +#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC, + foreach_hmac_alg +#undef _ + VNET_CRYPTO_N_OP_TYPES, +} vnet_crypto_op_type_t; +/* *INDENT-ON* */ + +typedef struct +{ + char *name; +} 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_t; + +typedef struct +{ + CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); + vnet_crypto_op_type_t op:16; + vnet_crypto_op_status_t status:8; + u8 key_len; + u16 flags; +#define VNET_CRYPTO_OP_FLAG_INIT_IV 1 + u32 len; + u8 *key; + u8 *iv; + u8 *src; + u8 *dst; +} vnet_crypto_op_t; + +typedef struct +{ + 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; + +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, + vnet_crypto_op_t * ops[], u32 n_ops); + +u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, + char *desc); + +vlib_error_t *vnet_crypto_register_ops_handler (vlib_main_t * vm, + u32 provider_index, + vnet_crypto_op_type_t opt, + vnet_crypto_ops_handler_t * + f); + +typedef struct +{ + char *name; + char *desc; + int priority; + vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_TYPES]; +} vnet_crypto_engine_t; + +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_engine_t *engines; +} vnet_crypto_main_t; + +extern vnet_crypto_main_t crypto_main; + +u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs, + u32 n_jobs); + +u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], + u32 n_ops); + +format_function_t format_vnet_crypto_alg; +format_function_t format_vnet_crypto_engine; +format_function_t format_vnet_crypto_op; + +#endif /* included_vnet_crypto_crypto_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/vnet/crypto/format.c b/src/vnet/crypto/format.c new file mode 100644 index 00000000000..88c7c0fe7f1 --- /dev/null +++ b/src/vnet/crypto/format.c @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +u8 * +format_vnet_crypto_alg (u8 * s, va_list * args) +{ + vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t); + vnet_crypto_main_t *cm = &crypto_main; + vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg); + return format (s, "%s", d->name); +} + +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, vnet_crypto_op_type_t); + vnet_crypto_op_type_data_t *otd = cm->opt_data + op; + + return format (s, "%s-%U", otd->desc, format_vnet_crypto_alg, otd->alg); +} + +u8 * +format_vnet_crypto_engine (u8 * s, va_list * args) +{ + vnet_crypto_main_t *cm = &crypto_main; + u32 crypto_engine_index = va_arg (*args, u32); + vnet_crypto_engine_t *e; + + if (crypto_engine_index == ~0) + return s; + + e = vec_elt_at_index (cm->engines, crypto_engine_index); + + return format (s, "%s", e->name); +} + + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ -- cgit 1.2.3-korg