Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 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 | u32 |
| 23 | vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops) |
| 24 | { |
| 25 | vnet_crypto_main_t *cm = &crypto_main; |
| 26 | u32 rv = 0, i; |
| 27 | |
| 28 | for (i = 0; i < n_ops; i++) |
| 29 | { |
| 30 | vnet_crypto_op_type_t opt = ops[i].op; |
| 31 | vnet_crypto_op_t *opp = &ops[i]; |
| 32 | |
| 33 | if (cm->ops_handlers[opt]) |
| 34 | rv += (cm->ops_handlers[opt]) (vm, &opp, 1); |
| 35 | else |
| 36 | ops[i].status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER; |
| 37 | } |
| 38 | |
| 39 | return rv; |
| 40 | } |
| 41 | |
| 42 | u32 |
| 43 | vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, |
| 44 | char *desc) |
| 45 | { |
| 46 | vnet_crypto_main_t *cm = &crypto_main; |
| 47 | vnet_crypto_engine_t *p; |
| 48 | |
| 49 | vec_add2 (cm->engines, p, 1); |
| 50 | p->name = name; |
| 51 | p->desc = desc; |
| 52 | p->priority = prio; |
| 53 | |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame^] | 54 | hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines); |
| 55 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 56 | return p - cm->engines; |
| 57 | } |
| 58 | |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame^] | 59 | int |
| 60 | vnet_crypto_set_handler (char *ops_handler_name, char *engine) |
| 61 | { |
| 62 | uword *p; |
| 63 | vnet_crypto_main_t *cm = &crypto_main; |
| 64 | vnet_crypto_op_type_t ot; |
| 65 | vnet_crypto_op_type_data_t *otd; |
| 66 | vnet_crypto_engine_t *ce; |
| 67 | |
| 68 | p = hash_get_mem (cm->ops_handler_index_by_name, ops_handler_name); |
| 69 | if (!p) |
| 70 | return -1; |
| 71 | |
| 72 | ot = p[0]; |
| 73 | otd = cm->opt_data + ot; |
| 74 | |
| 75 | p = hash_get_mem (cm->engine_index_by_name, engine); |
| 76 | if (!p) |
| 77 | return -1; |
| 78 | |
| 79 | ce = cm->engines + p[0]; |
| 80 | otd->active_engine_index = p[0]; |
| 81 | cm->ops_handlers[ot] = ce->ops_handlers[ot]; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 86 | vlib_error_t * |
| 87 | vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, |
| 88 | vnet_crypto_op_type_t opt, |
| 89 | vnet_crypto_ops_handler_t * fn) |
| 90 | { |
| 91 | vnet_crypto_main_t *cm = &crypto_main; |
| 92 | vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index); |
| 93 | vnet_crypto_op_type_data_t *otd = cm->opt_data + opt; |
| 94 | vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_TYPES - 1, |
| 95 | CLIB_CACHE_LINE_BYTES); |
| 96 | e->ops_handlers[opt] = fn; |
| 97 | |
| 98 | if (otd->active_engine_index == ~0) |
| 99 | { |
| 100 | otd->active_engine_index = engine_index; |
| 101 | cm->ops_handlers[opt] = fn; |
| 102 | return 0; |
| 103 | } |
| 104 | ae = vec_elt_at_index (cm->engines, otd->active_engine_index); |
| 105 | if (ae->priority < e->priority) |
| 106 | { |
| 107 | otd->active_engine_index = engine_index; |
| 108 | cm->ops_handlers[opt] = fn; |
| 109 | } |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | clib_error_t * |
| 115 | vnet_crypto_init (vlib_main_t * vm) |
| 116 | { |
| 117 | vnet_crypto_main_t *cm = &crypto_main; |
| 118 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame^] | 119 | #define CRYPTO_ENC_STR "encrypt" |
| 120 | #define CRYPTO_DEC_STR "decrypt" |
| 121 | #define CRYPTO_HMAC_STR "hmac" |
| 122 | |
| 123 | cm->engine_index_by_name = hash_create_string ( /* size */ 0, |
| 124 | sizeof (uword)); |
| 125 | cm->ops_handler_index_by_name = hash_create_string (0, sizeof (uword)); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 126 | |
| 127 | vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES); |
| 128 | vec_validate (cm->algs, VNET_CRYPTO_N_ALGS); |
| 129 | |
| 130 | #define _(n, s) \ |
| 131 | cm->algs[VNET_CRYPTO_ALG_##n].name = s; \ |
| 132 | cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].alg = VNET_CRYPTO_ALG_##n; \ |
| 133 | cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].alg = VNET_CRYPTO_ALG_##n; \ |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame^] | 134 | cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].desc = CRYPTO_ENC_STR; \ |
| 135 | cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].desc = CRYPTO_DEC_STR; \ |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 136 | cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].active_engine_index = ~0; \ |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame^] | 137 | cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].active_engine_index = ~0; \ |
| 138 | hash_set_mem (cm->ops_handler_index_by_name, CRYPTO_ENC_STR "-" s, \ |
| 139 | VNET_CRYPTO_OP_##n##_ENC); \ |
| 140 | hash_set_mem (cm->ops_handler_index_by_name, CRYPTO_DEC_STR "-" s, \ |
| 141 | VNET_CRYPTO_OP_##n##_DEC); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 142 | foreach_crypto_alg; |
| 143 | #undef _ |
| 144 | |
| 145 | #define _(n, s) \ |
| 146 | cm->algs[VNET_CRYPTO_ALG_##n].name = s; \ |
| 147 | cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].alg = VNET_CRYPTO_ALG_##n; \ |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame^] | 148 | cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].desc = CRYPTO_HMAC_STR; \ |
| 149 | cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].active_engine_index = ~0; \ |
| 150 | hash_set_mem (cm->ops_handler_index_by_name, CRYPTO_HMAC_STR "-" s, \ |
| 151 | VNET_CRYPTO_OP_##n##_HMAC); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 152 | foreach_hmac_alg; |
| 153 | #undef _ |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | VLIB_INIT_FUNCTION (vnet_crypto_init); |
| 159 | |
| 160 | /* |
| 161 | * fd.io coding-style-patch-verification: ON |
| 162 | * |
| 163 | * Local Variables: |
| 164 | * eval: (c-set-style "gnu") |
| 165 | * End: |
| 166 | */ |