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 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 22 | static_always_inline void |
| 23 | crypto_set_op_status (vnet_crypto_op_t * ops[], u32 n_ops, int status) |
| 24 | { |
| 25 | while (n_ops--) |
| 26 | { |
| 27 | ops[0]->status = status; |
| 28 | ops++; |
| 29 | } |
| 30 | } |
| 31 | |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 32 | static_always_inline u32 |
| 33 | vnet_crypto_process_ops_call_handler (vlib_main_t * vm, |
| 34 | vnet_crypto_main_t * cm, |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 35 | vnet_crypto_op_id_t opt, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 36 | vnet_crypto_op_t * ops[], |
| 37 | vnet_crypto_op_chunk_t * chunks, |
| 38 | u32 n_ops) |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 39 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 40 | u32 rv = 0; |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 41 | if (n_ops == 0) |
| 42 | return 0; |
| 43 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 44 | if (chunks) |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 45 | { |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 46 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 47 | if (cm->chained_ops_handlers[opt] == 0) |
| 48 | crypto_set_op_status (ops, n_ops, |
| 49 | VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER); |
| 50 | else |
| 51 | rv = (cm->chained_ops_handlers[opt]) (vm, ops, chunks, n_ops); |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | if (cm->ops_handlers[opt] == 0) |
| 56 | crypto_set_op_status (ops, n_ops, |
| 57 | VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER); |
| 58 | else |
| 59 | rv = (cm->ops_handlers[opt]) (vm, ops, n_ops); |
| 60 | } |
| 61 | return rv; |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 65 | static_always_inline u32 |
| 66 | vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[], |
| 67 | vnet_crypto_op_chunk_t * chunks, u32 n_ops) |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 68 | { |
| 69 | vnet_crypto_main_t *cm = &crypto_main; |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 70 | const int op_q_size = VLIB_FRAME_SIZE; |
| 71 | vnet_crypto_op_t *op_queue[op_q_size]; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 72 | vnet_crypto_op_id_t opt, current_op_type = ~0; |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 73 | u32 n_op_queue = 0; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 74 | u32 rv = 0, i; |
| 75 | |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 76 | ASSERT (n_ops >= 1); |
| 77 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 78 | for (i = 0; i < n_ops; i++) |
| 79 | { |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 80 | opt = ops[i].op; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 81 | |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 82 | if (current_op_type != opt || n_op_queue >= op_q_size) |
| 83 | { |
| 84 | rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 85 | op_queue, chunks, |
| 86 | n_op_queue); |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 87 | n_op_queue = 0; |
| 88 | current_op_type = opt; |
| 89 | } |
| 90 | |
| 91 | op_queue[n_op_queue++] = &ops[i]; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Damjan Marion | 085637f | 2019-04-03 18:39:27 +0200 | [diff] [blame] | 94 | rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type, |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 95 | op_queue, chunks, n_op_queue); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 96 | return rv; |
| 97 | } |
| 98 | |
| 99 | u32 |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 100 | vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops) |
| 101 | { |
| 102 | return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops); |
| 103 | } |
| 104 | |
| 105 | u32 |
| 106 | vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], |
| 107 | vnet_crypto_op_chunk_t * chunks, u32 n_ops) |
| 108 | { |
| 109 | return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops); |
| 110 | } |
| 111 | |
| 112 | u32 |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 113 | vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio, |
| 114 | char *desc) |
| 115 | { |
| 116 | vnet_crypto_main_t *cm = &crypto_main; |
| 117 | vnet_crypto_engine_t *p; |
| 118 | |
| 119 | vec_add2 (cm->engines, p, 1); |
| 120 | p->name = name; |
| 121 | p->desc = desc; |
| 122 | p->priority = prio; |
| 123 | |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 124 | hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines); |
| 125 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 126 | return p - cm->engines; |
| 127 | } |
| 128 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 129 | static_always_inline void |
| 130 | crypto_set_active_engine (vnet_crypto_op_data_t * od, |
| 131 | vnet_crypto_op_id_t id, u32 ei, |
| 132 | crypto_op_class_type_t oct) |
| 133 | { |
| 134 | vnet_crypto_main_t *cm = &crypto_main; |
| 135 | vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei); |
| 136 | |
| 137 | if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED) |
| 138 | { |
| 139 | if (ce->chained_ops_handlers[id]) |
| 140 | { |
| 141 | od->active_engine_index_chained = ei; |
| 142 | cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id]; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE) |
| 147 | { |
| 148 | if (ce->ops_handlers[id]) |
| 149 | { |
| 150 | od->active_engine_index_simple = ei; |
| 151 | cm->ops_handlers[id] = ce->ops_handlers[id]; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 156 | int |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 157 | vnet_crypto_set_handler2 (char *alg_name, char *engine, |
| 158 | crypto_op_class_type_t oct) |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 159 | { |
| 160 | uword *p; |
| 161 | vnet_crypto_main_t *cm = &crypto_main; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 162 | vnet_crypto_alg_data_t *ad; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 163 | int i; |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 164 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 165 | p = hash_get_mem (cm->alg_index_by_name, alg_name); |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 166 | if (!p) |
| 167 | return -1; |
| 168 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 169 | ad = vec_elt_at_index (cm->algs, p[0]); |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 170 | |
| 171 | p = hash_get_mem (cm->engine_index_by_name, engine); |
| 172 | if (!p) |
| 173 | return -1; |
| 174 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 175 | for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i += 2) |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 176 | { |
| 177 | vnet_crypto_op_data_t *od; |
| 178 | vnet_crypto_op_id_t id = ad->op_by_type[i]; |
| 179 | if (id == 0) |
| 180 | continue; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 181 | |
Lijian Zhang | b15d796 | 2019-09-27 16:25:35 +0800 | [diff] [blame] | 182 | od = cm->opt_data + id; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 183 | crypto_set_active_engine (od, id, p[0], oct); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 184 | } |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 189 | int |
| 190 | vnet_crypto_is_set_handler (vnet_crypto_alg_t alg) |
| 191 | { |
| 192 | vnet_crypto_main_t *cm = &crypto_main; |
| 193 | |
Benoît Ganne | 6545df7 | 2019-11-06 14:21:07 +0100 | [diff] [blame] | 194 | return (alg < vec_len (cm->ops_handlers) && NULL != cm->ops_handlers[alg]); |
Neale Ranns | ece2ae0 | 2019-06-21 12:44:11 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 197 | void |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 198 | vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index, |
| 199 | vnet_crypto_op_id_t opt, |
| 200 | vnet_crypto_ops_handler_t * fn, |
| 201 | vnet_crypto_chained_ops_handler_t * |
| 202 | cfn) |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 203 | { |
| 204 | vnet_crypto_main_t *cm = &crypto_main; |
| 205 | vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 206 | vnet_crypto_op_data_t *otd = cm->opt_data + opt; |
| 207 | vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1, |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 208 | CLIB_CACHE_LINE_BYTES); |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 209 | vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1, |
| 210 | CLIB_CACHE_LINE_BYTES); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 211 | |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 212 | if (fn) |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 213 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 214 | e->ops_handlers[opt] = fn; |
| 215 | if (otd->active_engine_index_simple == ~0) |
| 216 | { |
| 217 | otd->active_engine_index_simple = engine_index; |
| 218 | cm->ops_handlers[opt] = fn; |
| 219 | } |
| 220 | |
| 221 | ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple); |
| 222 | if (ae->priority < e->priority) |
| 223 | crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 224 | } |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 225 | |
| 226 | if (cfn) |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 227 | { |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 228 | e->chained_ops_handlers[opt] = cfn; |
| 229 | if (otd->active_engine_index_chained == ~0) |
| 230 | { |
| 231 | otd->active_engine_index_chained = engine_index; |
| 232 | cm->chained_ops_handlers[opt] = cfn; |
| 233 | } |
| 234 | |
| 235 | ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained); |
| 236 | if (ae->priority < e->priority) |
| 237 | crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 240 | return; |
| 241 | } |
| 242 | |
| 243 | void |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 244 | vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index, |
| 245 | vnet_crypto_op_id_t opt, |
| 246 | vnet_crypto_ops_handler_t * fn) |
| 247 | { |
| 248 | vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index, |
| 253 | vnet_crypto_op_id_t opt, |
| 254 | vnet_crypto_chained_ops_handler_t * |
| 255 | fn) |
| 256 | { |
| 257 | vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn); |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index, |
| 262 | vnet_crypto_op_id_t opt, |
| 263 | vnet_crypto_ops_handler_t * fn, |
| 264 | vnet_crypto_chained_ops_handler_t * cfn) |
| 265 | { |
| 266 | vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn); |
| 267 | } |
| 268 | |
| 269 | void |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 270 | vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index, |
| 271 | vnet_crypto_key_handler_t * key_handler) |
| 272 | { |
| 273 | vnet_crypto_main_t *cm = &crypto_main; |
| 274 | vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index); |
| 275 | e->key_op_handler = key_handler; |
| 276 | return; |
| 277 | } |
| 278 | |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 279 | static int |
| 280 | vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length) |
| 281 | { |
| 282 | switch (alg) |
| 283 | { |
| 284 | case VNET_CRYPTO_N_ALGS: |
| 285 | return 0; |
| 286 | case VNET_CRYPTO_ALG_NONE: |
| 287 | return 1; |
| 288 | |
| 289 | #define _(n, s, l) \ |
| 290 | case VNET_CRYPTO_ALG_##n: \ |
| 291 | if ((l) == length) \ |
Neale Ranns | e6be702 | 2019-06-04 15:37:34 +0000 | [diff] [blame] | 292 | return 1; \ |
| 293 | break; |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 294 | foreach_crypto_cipher_alg foreach_crypto_aead_alg |
| 295 | #undef _ |
| 296 | /* HMAC allows any key length */ |
| 297 | #define _(n, s) \ |
| 298 | case VNET_CRYPTO_ALG_HMAC_##n: \ |
| 299 | return 1; |
| 300 | foreach_crypto_hmac_alg |
| 301 | #undef _ |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 307 | u32 |
| 308 | vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data, |
| 309 | u16 length) |
| 310 | { |
| 311 | u32 index; |
| 312 | vnet_crypto_main_t *cm = &crypto_main; |
| 313 | vnet_crypto_engine_t *engine; |
| 314 | vnet_crypto_key_t *key; |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 315 | |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 316 | if (!vnet_crypto_key_len_check (alg, length)) |
| 317 | return ~0; |
| 318 | |
Damjan Marion | d1bed68 | 2019-04-24 15:20:35 +0200 | [diff] [blame] | 319 | pool_get_zero (cm->keys, key); |
| 320 | index = key - cm->keys; |
| 321 | key->alg = alg; |
| 322 | vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES); |
| 323 | clib_memcpy (key->data, data, length); |
| 324 | |
| 325 | /* *INDENT-OFF* */ |
| 326 | vec_foreach (engine, cm->engines) |
| 327 | if (engine->key_op_handler) |
| 328 | engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index); |
| 329 | /* *INDENT-ON* */ |
| 330 | return index; |
| 331 | } |
| 332 | |
| 333 | void |
| 334 | vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index) |
| 335 | { |
| 336 | vnet_crypto_main_t *cm = &crypto_main; |
| 337 | vnet_crypto_engine_t *engine; |
| 338 | vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index); |
| 339 | |
| 340 | /* *INDENT-OFF* */ |
| 341 | vec_foreach (engine, cm->engines) |
| 342 | if (engine->key_op_handler) |
| 343 | engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index); |
| 344 | /* *INDENT-ON* */ |
| 345 | |
| 346 | clib_memset (key->data, 0, vec_len (key->data)); |
| 347 | vec_free (key->data); |
| 348 | pool_put (cm->keys, key); |
| 349 | } |
| 350 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 351 | static void |
| 352 | vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid, |
| 353 | vnet_crypto_op_id_t did, char *name, u8 is_aead) |
| 354 | { |
| 355 | vnet_crypto_op_type_t eopt, dopt; |
| 356 | vnet_crypto_main_t *cm = &crypto_main; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 357 | |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 358 | cm->algs[alg].name = name; |
| 359 | cm->opt_data[eid].alg = cm->opt_data[did].alg = alg; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 360 | cm->opt_data[eid].active_engine_index_simple = ~0; |
| 361 | cm->opt_data[did].active_engine_index_simple = ~0; |
| 362 | cm->opt_data[eid].active_engine_index_chained = ~0; |
| 363 | cm->opt_data[did].active_engine_index_chained = ~0; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 364 | if (is_aead) |
| 365 | { |
| 366 | eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT; |
| 367 | dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT; |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT; |
| 372 | dopt = VNET_CRYPTO_OP_TYPE_DECRYPT; |
| 373 | } |
| 374 | cm->opt_data[eid].type = eopt; |
| 375 | cm->opt_data[did].type = dopt; |
| 376 | cm->algs[alg].op_by_type[eopt] = eid; |
| 377 | cm->algs[alg].op_by_type[dopt] = did; |
| 378 | hash_set_mem (cm->alg_index_by_name, name, alg); |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg, |
| 383 | vnet_crypto_op_id_t id, char *name) |
| 384 | { |
| 385 | vnet_crypto_main_t *cm = &crypto_main; |
| 386 | cm->algs[alg].name = name; |
| 387 | cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id; |
| 388 | cm->opt_data[id].alg = alg; |
Filip Tehlar | efcad1a | 2020-02-04 09:36:04 +0000 | [diff] [blame] | 389 | cm->opt_data[id].active_engine_index_simple = ~0; |
| 390 | cm->opt_data[id].active_engine_index_chained = ~0; |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 391 | cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC; |
| 392 | hash_set_mem (cm->alg_index_by_name, name, alg); |
| 393 | } |
| 394 | |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 395 | clib_error_t * |
| 396 | vnet_crypto_init (vlib_main_t * vm) |
| 397 | { |
| 398 | vnet_crypto_main_t *cm = &crypto_main; |
| 399 | vlib_thread_main_t *tm = vlib_get_thread_main (); |
Filip Tehlar | 1469d54 | 2019-03-25 09:04:41 -0700 | [diff] [blame] | 400 | cm->engine_index_by_name = hash_create_string ( /* size */ 0, |
| 401 | sizeof (uword)); |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 402 | cm->alg_index_by_name = hash_create_string (0, sizeof (uword)); |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 403 | vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES); |
| 404 | vec_validate (cm->algs, VNET_CRYPTO_N_ALGS); |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 405 | #define _(n, s, l) \ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 406 | vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \ |
| 407 | VNET_CRYPTO_OP_##n##_ENC, \ |
| 408 | VNET_CRYPTO_OP_##n##_DEC, s, 0); |
| 409 | foreach_crypto_cipher_alg; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 410 | #undef _ |
Benoît Ganne | be95444 | 2019-04-29 16:05:46 +0200 | [diff] [blame] | 411 | #define _(n, s, l) \ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 412 | vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \ |
| 413 | VNET_CRYPTO_OP_##n##_ENC, \ |
| 414 | VNET_CRYPTO_OP_##n##_DEC, s, 1); |
| 415 | foreach_crypto_aead_alg; |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 416 | #undef _ |
Damjan Marion | 060bfb9 | 2019-03-29 13:47:54 +0100 | [diff] [blame] | 417 | #define _(n, s) \ |
| 418 | vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \ |
| 419 | VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s); |
| 420 | foreach_crypto_hmac_alg; |
| 421 | #undef _ |
Damjan Marion | 91f17dc | 2019-03-18 18:59:25 +0100 | [diff] [blame] | 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | VLIB_INIT_FUNCTION (vnet_crypto_init); |
| 426 | |
| 427 | /* |
| 428 | * fd.io coding-style-patch-verification: ON |
| 429 | * |
| 430 | * Local Variables: |
| 431 | * eval: (c-set-style "gnu") |
| 432 | * End: |
| 433 | */ |