blob: c8e7ca90c9dd39f3da865f1b0decf10cc55505bc [file] [log] [blame]
Damjan Marion91f17dc2019-03-18 18:59:25 +01001/*
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
20vnet_crypto_main_t crypto_main;
21
Filip Tehlarefcad1a2020-02-04 09:36:04 +000022static_always_inline void
23crypto_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 Marion085637f2019-04-03 18:39:27 +020032static_always_inline u32
33vnet_crypto_process_ops_call_handler (vlib_main_t * vm,
34 vnet_crypto_main_t * cm,
Damjan Marion060bfb92019-03-29 13:47:54 +010035 vnet_crypto_op_id_t opt,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000036 vnet_crypto_op_t * ops[],
37 vnet_crypto_op_chunk_t * chunks,
38 u32 n_ops)
Damjan Marion085637f2019-04-03 18:39:27 +020039{
Filip Tehlarefcad1a2020-02-04 09:36:04 +000040 u32 rv = 0;
Damjan Marion085637f2019-04-03 18:39:27 +020041 if (n_ops == 0)
42 return 0;
43
Filip Tehlarefcad1a2020-02-04 09:36:04 +000044 if (chunks)
Damjan Marion085637f2019-04-03 18:39:27 +020045 {
Damjan Marion085637f2019-04-03 18:39:27 +020046
Filip Tehlarefcad1a2020-02-04 09:36:04 +000047 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 Marion085637f2019-04-03 18:39:27 +020062}
63
Filip Tehlarefcad1a2020-02-04 09:36:04 +000064static_always_inline u32
65vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[],
66 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
Damjan Marion91f17dc2019-03-18 18:59:25 +010067{
68 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion085637f2019-04-03 18:39:27 +020069 const int op_q_size = VLIB_FRAME_SIZE;
70 vnet_crypto_op_t *op_queue[op_q_size];
Damjan Marion060bfb92019-03-29 13:47:54 +010071 vnet_crypto_op_id_t opt, current_op_type = ~0;
Damjan Marion085637f2019-04-03 18:39:27 +020072 u32 n_op_queue = 0;
Damjan Marion91f17dc2019-03-18 18:59:25 +010073 u32 rv = 0, i;
74
Damjan Marion085637f2019-04-03 18:39:27 +020075 ASSERT (n_ops >= 1);
76
Damjan Marion91f17dc2019-03-18 18:59:25 +010077 for (i = 0; i < n_ops; i++)
78 {
Damjan Marion085637f2019-04-03 18:39:27 +020079 opt = ops[i].op;
Damjan Marion91f17dc2019-03-18 18:59:25 +010080
Damjan Marion085637f2019-04-03 18:39:27 +020081 if (current_op_type != opt || n_op_queue >= op_q_size)
82 {
83 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000084 op_queue, chunks,
85 n_op_queue);
Damjan Marion085637f2019-04-03 18:39:27 +020086 n_op_queue = 0;
87 current_op_type = opt;
88 }
89
90 op_queue[n_op_queue++] = &ops[i];
Damjan Marion91f17dc2019-03-18 18:59:25 +010091 }
92
Damjan Marion085637f2019-04-03 18:39:27 +020093 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000094 op_queue, chunks, n_op_queue);
Damjan Marion91f17dc2019-03-18 18:59:25 +010095 return rv;
96}
97
98u32
Filip Tehlarefcad1a2020-02-04 09:36:04 +000099vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
100{
101 return vnet_crypto_process_ops_inline (vm, ops, 0, n_ops);
102}
103
104u32
105vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
106 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
107{
108 return vnet_crypto_process_ops_inline (vm, ops, chunks, n_ops);
109}
110
111u32
Damjan Marion91f17dc2019-03-18 18:59:25 +0100112vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
113 char *desc)
114{
115 vnet_crypto_main_t *cm = &crypto_main;
116 vnet_crypto_engine_t *p;
117
118 vec_add2 (cm->engines, p, 1);
119 p->name = name;
120 p->desc = desc;
121 p->priority = prio;
122
Filip Tehlar1469d542019-03-25 09:04:41 -0700123 hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
124
Damjan Marion91f17dc2019-03-18 18:59:25 +0100125 return p - cm->engines;
126}
127
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000128static_always_inline void
129crypto_set_active_engine (vnet_crypto_op_data_t * od,
130 vnet_crypto_op_id_t id, u32 ei,
131 crypto_op_class_type_t oct)
132{
133 vnet_crypto_main_t *cm = &crypto_main;
134 vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
135
136 if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_CHAINED)
137 {
138 if (ce->chained_ops_handlers[id])
139 {
140 od->active_engine_index_chained = ei;
141 cm->chained_ops_handlers[id] = ce->chained_ops_handlers[id];
142 }
143 }
144
145 if (oct == CRYPTO_OP_BOTH || oct == CRYPTO_OP_SIMPLE)
146 {
147 if (ce->ops_handlers[id])
148 {
149 od->active_engine_index_simple = ei;
150 cm->ops_handlers[id] = ce->ops_handlers[id];
151 }
152 }
153}
154
Filip Tehlar1469d542019-03-25 09:04:41 -0700155int
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000156vnet_crypto_set_handler2 (char *alg_name, char *engine,
157 crypto_op_class_type_t oct)
Filip Tehlar1469d542019-03-25 09:04:41 -0700158{
159 uword *p;
160 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion060bfb92019-03-29 13:47:54 +0100161 vnet_crypto_alg_data_t *ad;
Damjan Marion060bfb92019-03-29 13:47:54 +0100162 int i;
Filip Tehlar1469d542019-03-25 09:04:41 -0700163
Damjan Marion060bfb92019-03-29 13:47:54 +0100164 p = hash_get_mem (cm->alg_index_by_name, alg_name);
Filip Tehlar1469d542019-03-25 09:04:41 -0700165 if (!p)
166 return -1;
167
Damjan Marion060bfb92019-03-29 13:47:54 +0100168 ad = vec_elt_at_index (cm->algs, p[0]);
Filip Tehlar1469d542019-03-25 09:04:41 -0700169
170 p = hash_get_mem (cm->engine_index_by_name, engine);
171 if (!p)
172 return -1;
173
Filip Tehlard26b8602020-02-25 09:53:26 +0000174 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
Damjan Marion060bfb92019-03-29 13:47:54 +0100175 {
176 vnet_crypto_op_data_t *od;
177 vnet_crypto_op_id_t id = ad->op_by_type[i];
178 if (id == 0)
179 continue;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000180
Lijian Zhangb15d7962019-09-27 16:25:35 +0800181 od = cm->opt_data + id;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000182 crypto_set_active_engine (od, id, p[0], oct);
Damjan Marion060bfb92019-03-29 13:47:54 +0100183 }
Filip Tehlar1469d542019-03-25 09:04:41 -0700184
185 return 0;
186}
187
Neale Rannsece2ae02019-06-21 12:44:11 +0000188int
189vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
190{
191 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleskib2525922021-01-11 08:59:31 +0000192 vnet_crypto_op_id_t opt = 0;
193 int i;
Neale Rannsece2ae02019-06-21 12:44:11 +0000194
Dmitry Valtere8e4b5f2023-10-05 08:31:23 +0000195 if (alg >= vec_len (cm->algs))
PiotrX Kleskib2525922021-01-11 08:59:31 +0000196 return 0;
197
198 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
199 if ((opt = cm->algs[alg].op_by_type[i]) != 0)
200 break;
201
Dmitry Valtere8e4b5f2023-10-05 08:31:23 +0000202 if (opt >= vec_len (cm->ops_handlers))
203 return 0;
204
PiotrX Kleskib2525922021-01-11 08:59:31 +0000205 return NULL != cm->ops_handlers[opt];
Neale Rannsece2ae02019-06-21 12:44:11 +0000206}
207
Damjan Mariond1bed682019-04-24 15:20:35 +0200208void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000209vnet_crypto_register_ops_handler_inline (vlib_main_t * vm, u32 engine_index,
210 vnet_crypto_op_id_t opt,
211 vnet_crypto_ops_handler_t * fn,
212 vnet_crypto_chained_ops_handler_t *
213 cfn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100214{
215 vnet_crypto_main_t *cm = &crypto_main;
216 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
Damjan Marion060bfb92019-03-29 13:47:54 +0100217 vnet_crypto_op_data_t *otd = cm->opt_data + opt;
218 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100219 CLIB_CACHE_LINE_BYTES);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000220 vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
221 CLIB_CACHE_LINE_BYTES);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100222
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000223 if (fn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100224 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000225 e->ops_handlers[opt] = fn;
226 if (otd->active_engine_index_simple == ~0)
227 {
228 otd->active_engine_index_simple = engine_index;
229 cm->ops_handlers[opt] = fn;
230 }
231
232 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_simple);
233 if (ae->priority < e->priority)
234 crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_SIMPLE);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100235 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000236
237 if (cfn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100238 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000239 e->chained_ops_handlers[opt] = cfn;
240 if (otd->active_engine_index_chained == ~0)
241 {
242 otd->active_engine_index_chained = engine_index;
243 cm->chained_ops_handlers[opt] = cfn;
244 }
245
246 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_chained);
247 if (ae->priority < e->priority)
248 crypto_set_active_engine (otd, opt, engine_index, CRYPTO_OP_CHAINED);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100249 }
250
Damjan Mariond1bed682019-04-24 15:20:35 +0200251 return;
252}
253
254void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000255vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
256 vnet_crypto_op_id_t opt,
257 vnet_crypto_ops_handler_t * fn)
258{
259 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, 0);
260}
261
262void
263vnet_crypto_register_chained_ops_handler (vlib_main_t * vm, u32 engine_index,
264 vnet_crypto_op_id_t opt,
265 vnet_crypto_chained_ops_handler_t *
266 fn)
267{
268 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, 0, fn);
269}
270
271void
272vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
273 vnet_crypto_op_id_t opt,
274 vnet_crypto_ops_handler_t * fn,
275 vnet_crypto_chained_ops_handler_t * cfn)
276{
277 vnet_crypto_register_ops_handler_inline (vm, engine_index, opt, fn, cfn);
278}
279
280void
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000281vnet_crypto_register_enqueue_handler (vlib_main_t *vm, u32 engine_index,
282 vnet_crypto_async_op_id_t opt,
283 vnet_crypto_frame_enqueue_t *enqueue_hdl)
Fan Zhangf5395782020-04-29 14:00:03 +0100284{
285 vnet_crypto_main_t *cm = &crypto_main;
286 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
287 vnet_crypto_async_op_data_t *otd = cm->async_opt_data + opt;
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500288 vec_validate_aligned (cm->enqueue_handlers, VNET_CRYPTO_ASYNC_OP_N_IDS,
Fan Zhangf5395782020-04-29 14:00:03 +0100289 CLIB_CACHE_LINE_BYTES);
Fan Zhangf5395782020-04-29 14:00:03 +0100290
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000291 if (!enqueue_hdl)
Fan Zhangf5395782020-04-29 14:00:03 +0100292 return;
293
294 e->enqueue_handlers[opt] = enqueue_hdl;
Fan Zhangf5395782020-04-29 14:00:03 +0100295 if (otd->active_engine_index_async == ~0)
296 {
297 otd->active_engine_index_async = engine_index;
298 cm->enqueue_handlers[opt] = enqueue_hdl;
Fan Zhangf5395782020-04-29 14:00:03 +0100299 }
300
301 ae = vec_elt_at_index (cm->engines, otd->active_engine_index_async);
Fan Zhange4db9452021-03-30 17:31:38 +0100302 if (ae->priority <= e->priority)
Fan Zhangf5395782020-04-29 14:00:03 +0100303 {
304 otd->active_engine_index_async = engine_index;
305 cm->enqueue_handlers[opt] = enqueue_hdl;
Fan Zhangf5395782020-04-29 14:00:03 +0100306 }
307
308 return;
309}
310
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000311static int
312engine_index_cmp (void *v1, void *v2)
313{
314 u32 *a1 = v1;
315 u32 *a2 = v2;
316
317 if (*a1 > *a2)
318 return 1;
319 if (*a1 < *a2)
320 return -1;
321 return 0;
322}
323
324static void
325vnet_crypto_update_cm_dequeue_handlers (void)
326{
327 vnet_crypto_main_t *cm = &crypto_main;
328 vnet_crypto_async_op_data_t *otd;
329 vnet_crypto_engine_t *e;
330 u32 *active_engines = 0, *ei, last_ei = ~0, i;
331
332 vec_reset_length (cm->dequeue_handlers);
333
334 for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_IDS; i++)
335 {
336 otd = cm->async_opt_data + i;
mgovind6a517752022-02-16 01:15:02 +0000337 if (otd->active_engine_index_async == ~0)
338 continue;
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000339 e = cm->engines + otd->active_engine_index_async;
340 if (!e->dequeue_handler)
341 continue;
342 vec_add1 (active_engines, otd->active_engine_index_async);
343 }
344
345 vec_sort_with_function (active_engines, engine_index_cmp);
346
347 vec_foreach (ei, active_engines)
348 {
349 if (ei[0] == last_ei)
350 continue;
mgovind6a517752022-02-16 01:15:02 +0000351 if (ei[0] == ~0)
352 continue;
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000353
354 e = cm->engines + ei[0];
355 vec_add1 (cm->dequeue_handlers, e->dequeue_handler);
356 last_ei = ei[0];
357 }
358
359 vec_free (active_engines);
360}
361
362void
363vnet_crypto_register_dequeue_handler (vlib_main_t *vm, u32 engine_index,
364 vnet_crypto_frame_dequeue_t *deq_fn)
365{
366 vnet_crypto_main_t *cm = &crypto_main;
367 vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
368
369 if (!deq_fn)
370 return;
371
372 e->dequeue_handler = deq_fn;
373
Xiaoming Jiang9a9604b2023-03-09 02:03:50 +0000374 vnet_crypto_update_cm_dequeue_handlers ();
375
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000376 return;
377}
378
Fan Zhangf5395782020-04-29 14:00:03 +0100379void
Damjan Mariond1bed682019-04-24 15:20:35 +0200380vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
381 vnet_crypto_key_handler_t * key_handler)
382{
383 vnet_crypto_main_t *cm = &crypto_main;
384 vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
385 e->key_op_handler = key_handler;
386 return;
387}
388
Benoît Gannebe954442019-04-29 16:05:46 +0200389static int
390vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
391{
392 switch (alg)
393 {
394 case VNET_CRYPTO_N_ALGS:
395 return 0;
396 case VNET_CRYPTO_ALG_NONE:
397 return 1;
398
399#define _(n, s, l) \
400 case VNET_CRYPTO_ALG_##n: \
401 if ((l) == length) \
Neale Rannse6be7022019-06-04 15:37:34 +0000402 return 1; \
403 break;
Benoît Gannebe954442019-04-29 16:05:46 +0200404 foreach_crypto_cipher_alg foreach_crypto_aead_alg
405#undef _
406 /* HMAC allows any key length */
407#define _(n, s) \
408 case VNET_CRYPTO_ALG_HMAC_##n: \
409 return 1;
Filip Tehlar06111a82021-05-03 15:29:56 +0000410 foreach_crypto_hmac_alg
411#undef _
412
413#define _(n, s) \
414 case VNET_CRYPTO_ALG_HASH_##n: \
415 return 1;
416 foreach_crypto_hash_alg
Benoît Gannebe954442019-04-29 16:05:46 +0200417#undef _
418 }
419
420 return 0;
421}
422
Damjan Mariond1bed682019-04-24 15:20:35 +0200423u32
424vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
425 u16 length)
426{
427 u32 index;
428 vnet_crypto_main_t *cm = &crypto_main;
429 vnet_crypto_engine_t *engine;
430 vnet_crypto_key_t *key;
Benoît Gannebe954442019-04-29 16:05:46 +0200431
Gabriel Oginskic12d48f2021-10-26 07:43:33 +0100432 u8 need_barrier_sync = 0;
433
Benoît Gannebe954442019-04-29 16:05:46 +0200434 if (!vnet_crypto_key_len_check (alg, length))
435 return ~0;
436
Damjan Marion66d4cb52022-03-17 18:59:46 +0100437 need_barrier_sync = pool_get_will_expand (cm->keys);
Gabriel Oginskic12d48f2021-10-26 07:43:33 +0100438 /* If the cm->keys will expand, stop the parade. */
439 if (need_barrier_sync)
440 vlib_worker_thread_barrier_sync (vm);
441
Damjan Mariond1bed682019-04-24 15:20:35 +0200442 pool_get_zero (cm->keys, key);
Gabriel Oginskic12d48f2021-10-26 07:43:33 +0100443
444 if (need_barrier_sync)
445 vlib_worker_thread_barrier_release (vm);
446
Damjan Mariond1bed682019-04-24 15:20:35 +0200447 index = key - cm->keys;
Fan Zhangf5395782020-04-29 14:00:03 +0100448 key->type = VNET_CRYPTO_KEY_TYPE_DATA;
Damjan Mariond1bed682019-04-24 15:20:35 +0200449 key->alg = alg;
450 vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
451 clib_memcpy (key->data, data, length);
Damjan Mariond1bed682019-04-24 15:20:35 +0200452 vec_foreach (engine, cm->engines)
453 if (engine->key_op_handler)
454 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
Damjan Mariond1bed682019-04-24 15:20:35 +0200455 return index;
456}
457
458void
459vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
460{
461 vnet_crypto_main_t *cm = &crypto_main;
462 vnet_crypto_engine_t *engine;
463 vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
464
Damjan Mariond1bed682019-04-24 15:20:35 +0200465 vec_foreach (engine, cm->engines)
466 if (engine->key_op_handler)
467 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
Damjan Mariond1bed682019-04-24 15:20:35 +0200468
Fan Zhangf5395782020-04-29 14:00:03 +0100469 if (key->type == VNET_CRYPTO_KEY_TYPE_DATA)
470 {
Benoît Ganne1ee334c2023-01-05 10:56:26 +0100471 clib_memset (key->data, 0xfe, vec_len (key->data));
Fan Zhangf5395782020-04-29 14:00:03 +0100472 vec_free (key->data);
473 }
474 else if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
475 {
Benoît Ganne1ee334c2023-01-05 10:56:26 +0100476 key->index_crypto = key->index_integ = ~0;
Fan Zhangf5395782020-04-29 14:00:03 +0100477 }
478
Damjan Mariond1bed682019-04-24 15:20:35 +0200479 pool_put (cm->keys, key);
480}
481
Lijian Zhang6f8252e2023-11-08 09:09:10 +0000482void
483vnet_crypto_key_update (vlib_main_t *vm, vnet_crypto_key_index_t index)
484{
485 vnet_crypto_main_t *cm = &crypto_main;
486 vnet_crypto_engine_t *engine;
487
488 vec_foreach (engine, cm->engines)
489 if (engine->key_op_handler)
490 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_MODIFY, index);
491}
492
Fan Zhangf5395782020-04-29 14:00:03 +0100493vnet_crypto_async_alg_t
494vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
495 vnet_crypto_alg_t integ_alg)
496{
497#define _(c, h, s, k ,d) \
498 if (crypto_alg == VNET_CRYPTO_ALG_##c && \
499 integ_alg == VNET_CRYPTO_ALG_HMAC_##h) \
500 return VNET_CRYPTO_ALG_##c##_##h##_TAG##d;
501 foreach_crypto_link_async_alg
502#undef _
503 return ~0;
504}
505
506u32
507vnet_crypto_key_add_linked (vlib_main_t * vm,
508 vnet_crypto_key_index_t index_crypto,
509 vnet_crypto_key_index_t index_integ)
510{
511 u32 index;
512 vnet_crypto_main_t *cm = &crypto_main;
513 vnet_crypto_engine_t *engine;
514 vnet_crypto_key_t *key_crypto, *key_integ, *key;
515 vnet_crypto_async_alg_t linked_alg;
516
517 key_crypto = pool_elt_at_index (cm->keys, index_crypto);
518 key_integ = pool_elt_at_index (cm->keys, index_integ);
519
Fan Zhangf5395782020-04-29 14:00:03 +0100520 linked_alg = vnet_crypto_link_algs (key_crypto->alg, key_integ->alg);
521 if (linked_alg == ~0)
522 return ~0;
523
524 pool_get_zero (cm->keys, key);
525 index = key - cm->keys;
526 key->type = VNET_CRYPTO_KEY_TYPE_LINK;
527 key->index_crypto = index_crypto;
528 key->index_integ = index_integ;
529 key->async_alg = linked_alg;
530
Fan Zhangf5395782020-04-29 14:00:03 +0100531 vec_foreach (engine, cm->engines)
532 if (engine->key_op_handler)
533 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
Fan Zhangf5395782020-04-29 14:00:03 +0100534
535 return index;
536}
537
Fan Zhangf5395782020-04-29 14:00:03 +0100538static_always_inline void
539crypto_set_active_async_engine (vnet_crypto_async_op_data_t * od,
540 vnet_crypto_async_op_id_t id, u32 ei)
541{
542 vnet_crypto_main_t *cm = &crypto_main;
543 vnet_crypto_engine_t *ce = vec_elt_at_index (cm->engines, ei);
544
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000545 if (ce->enqueue_handlers[id] && ce->dequeue_handler)
Fan Zhangf5395782020-04-29 14:00:03 +0100546 {
547 od->active_engine_index_async = ei;
548 cm->enqueue_handlers[id] = ce->enqueue_handlers[id];
Fan Zhangf5395782020-04-29 14:00:03 +0100549 }
550}
551
552int
553vnet_crypto_set_async_handler2 (char *alg_name, char *engine)
554{
555 uword *p;
556 vnet_crypto_main_t *cm = &crypto_main;
557 vnet_crypto_async_alg_data_t *ad;
558 int i;
559
560 p = hash_get_mem (cm->async_alg_index_by_name, alg_name);
561 if (!p)
562 return -1;
563
564 ad = vec_elt_at_index (cm->async_algs, p[0]);
565
566 p = hash_get_mem (cm->engine_index_by_name, engine);
567 if (!p)
568 return -1;
569
570 for (i = 0; i < VNET_CRYPTO_ASYNC_OP_N_TYPES; i++)
571 {
572 vnet_crypto_async_op_data_t *od;
573 vnet_crypto_async_op_id_t id = ad->op_by_type[i];
574 if (id == 0)
575 continue;
576
577 od = cm->async_opt_data + id;
578 crypto_set_active_async_engine (od, id, p[0]);
579 }
580
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000581 vnet_crypto_update_cm_dequeue_handlers ();
582
Fan Zhangf5395782020-04-29 14:00:03 +0100583 return 0;
584}
585
586u32
587vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name)
588{
589 vnet_crypto_main_t *cm = &crypto_main;
590 vnet_crypto_async_next_node_t *nn = 0;
591 vlib_node_t *cc, *pn;
592 uword index = vec_len (cm->next_nodes);
593
594 pn = vlib_get_node_by_name (vm, (u8 *) post_node_name);
595 if (!pn)
596 return ~0;
597
Dmitry Valtercb3a6bd2022-10-17 14:04:35 +0000598 vec_foreach (nn, cm->next_nodes)
599 {
600 if (nn->node_idx == pn->index)
601 return nn->next_idx;
602 }
Fan Zhangf5395782020-04-29 14:00:03 +0100603
604 vec_validate (cm->next_nodes, index);
605 nn = vec_elt_at_index (cm->next_nodes, index);
606
607 cc = vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch");
608 nn->next_idx = vlib_node_add_named_next (vm, cc->index, post_node_name);
609 nn->node_idx = pn->index;
610
611 return nn->next_idx;
612}
613
Vratko Polak139aba22023-08-17 16:22:48 +0200614void
615vnet_crypto_set_async_dispatch (u8 mode, u8 adaptive)
616{
617 vlib_thread_main_t *tm = vlib_get_thread_main ();
618 u32 i, node_index = crypto_main.crypto_node_index;
619 vlib_node_state_t state =
620 mode ? VLIB_NODE_STATE_INTERRUPT : VLIB_NODE_STATE_POLLING;
621
622 for (i = vlib_num_workers () > 0; i < tm->n_vlib_mains; i++)
623 {
624 vlib_main_t *ovm = vlib_get_main_by_index (i);
625 vlib_node_set_state (ovm, node_index, state);
626 vlib_node_set_flag (ovm, node_index, VLIB_NODE_FLAG_ADAPTIVE_MODE,
627 adaptive);
628 }
629}
630
Fan Zhangf5395782020-04-29 14:00:03 +0100631int
632vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t op)
633{
634 vnet_crypto_main_t *cm = &crypto_main;
635
636 return (op < vec_len (cm->enqueue_handlers) &&
637 NULL != cm->enqueue_handlers[op]);
638}
639
Damjan Marion060bfb92019-03-29 13:47:54 +0100640static void
641vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
642 vnet_crypto_op_id_t did, char *name, u8 is_aead)
643{
644 vnet_crypto_op_type_t eopt, dopt;
645 vnet_crypto_main_t *cm = &crypto_main;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000646
Damjan Marion060bfb92019-03-29 13:47:54 +0100647 cm->algs[alg].name = name;
648 cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000649 cm->opt_data[eid].active_engine_index_simple = ~0;
650 cm->opt_data[did].active_engine_index_simple = ~0;
651 cm->opt_data[eid].active_engine_index_chained = ~0;
652 cm->opt_data[did].active_engine_index_chained = ~0;
Damjan Marion060bfb92019-03-29 13:47:54 +0100653 if (is_aead)
654 {
655 eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
656 dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
657 }
658 else
659 {
660 eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
661 dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
662 }
663 cm->opt_data[eid].type = eopt;
664 cm->opt_data[did].type = dopt;
665 cm->algs[alg].op_by_type[eopt] = eid;
666 cm->algs[alg].op_by_type[dopt] = did;
667 hash_set_mem (cm->alg_index_by_name, name, alg);
668}
669
670static void
Filip Tehlar06111a82021-05-03 15:29:56 +0000671vnet_crypto_init_hash_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t id,
672 char *name)
673{
674 vnet_crypto_main_t *cm = &crypto_main;
675 cm->algs[alg].name = name;
676 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HASH] = id;
677 cm->opt_data[id].alg = alg;
678 cm->opt_data[id].active_engine_index_simple = ~0;
679 cm->opt_data[id].active_engine_index_chained = ~0;
680 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HASH;
681 hash_set_mem (cm->alg_index_by_name, name, alg);
682}
683
684static void
Damjan Marion060bfb92019-03-29 13:47:54 +0100685vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
686 vnet_crypto_op_id_t id, char *name)
687{
688 vnet_crypto_main_t *cm = &crypto_main;
689 cm->algs[alg].name = name;
690 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
691 cm->opt_data[id].alg = alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000692 cm->opt_data[id].active_engine_index_simple = ~0;
693 cm->opt_data[id].active_engine_index_chained = ~0;
Damjan Marion060bfb92019-03-29 13:47:54 +0100694 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
695 hash_set_mem (cm->alg_index_by_name, name, alg);
696}
697
Fan Zhangf5395782020-04-29 14:00:03 +0100698static void
699vnet_crypto_init_async_data (vnet_crypto_async_alg_t alg,
700 vnet_crypto_async_op_id_t eid,
701 vnet_crypto_async_op_id_t did, char *name)
702{
703 vnet_crypto_main_t *cm = &crypto_main;
704
705 cm->async_algs[alg].name = name;
706 cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT] = eid;
707 cm->async_algs[alg].op_by_type[VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT] = did;
708 cm->async_opt_data[eid].type = VNET_CRYPTO_ASYNC_OP_TYPE_ENCRYPT;
709 cm->async_opt_data[eid].alg = alg;
710 cm->async_opt_data[eid].active_engine_index_async = ~0;
711 cm->async_opt_data[eid].active_engine_index_async = ~0;
712 cm->async_opt_data[did].type = VNET_CRYPTO_ASYNC_OP_TYPE_DECRYPT;
713 cm->async_opt_data[did].alg = alg;
714 cm->async_opt_data[did].active_engine_index_async = ~0;
715 cm->async_opt_data[did].active_engine_index_async = ~0;
716 hash_set_mem (cm->async_alg_index_by_name, name, alg);
717}
718
Damjan Marion91f17dc2019-03-18 18:59:25 +0100719clib_error_t *
720vnet_crypto_init (vlib_main_t * vm)
721{
722 vnet_crypto_main_t *cm = &crypto_main;
723 vlib_thread_main_t *tm = vlib_get_thread_main ();
Fan Zhangf5395782020-04-29 14:00:03 +0100724 vnet_crypto_thread_t *ct = 0;
PiotrX Kleski22848172020-07-08 14:36:34 +0200725
Filip Tehlar1469d542019-03-25 09:04:41 -0700726 cm->engine_index_by_name = hash_create_string ( /* size */ 0,
727 sizeof (uword));
Damjan Marion060bfb92019-03-29 13:47:54 +0100728 cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
Fan Zhangf5395782020-04-29 14:00:03 +0100729 cm->async_alg_index_by_name = hash_create_string (0, sizeof (uword));
Damjan Marion91f17dc2019-03-18 18:59:25 +0100730 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
Fan Zhangf5395782020-04-29 14:00:03 +0100731 vec_foreach (ct, cm->threads)
gaoginskxf441b5d2021-06-07 12:07:01 +0100732 pool_init_fixed (ct->frame_pool, VNET_CRYPTO_FRAME_POOL_SIZE);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100733 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
Fan Zhangf5395782020-04-29 14:00:03 +0100734 vec_validate (cm->async_algs, VNET_CRYPTO_N_ASYNC_ALGS);
Fan Zhangf5395782020-04-29 14:00:03 +0100735
Benoît Gannebe954442019-04-29 16:05:46 +0200736#define _(n, s, l) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100737 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
738 VNET_CRYPTO_OP_##n##_ENC, \
739 VNET_CRYPTO_OP_##n##_DEC, s, 0);
740 foreach_crypto_cipher_alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100741#undef _
Benoît Gannebe954442019-04-29 16:05:46 +0200742#define _(n, s, l) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100743 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
744 VNET_CRYPTO_OP_##n##_ENC, \
745 VNET_CRYPTO_OP_##n##_DEC, s, 1);
746 foreach_crypto_aead_alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100747#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100748#define _(n, s) \
749 vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
750 VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
751 foreach_crypto_hmac_alg;
752#undef _
Filip Tehlar06111a82021-05-03 15:29:56 +0000753#define _(n, s) \
754 vnet_crypto_init_hash_data (VNET_CRYPTO_ALG_HASH_##n, \
755 VNET_CRYPTO_OP_##n##_HASH, s);
756 foreach_crypto_hash_alg;
757#undef _
Fan Zhangf5395782020-04-29 14:00:03 +0100758#define _(n, s, k, t, a) \
759 vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, \
760 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
761 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, \
762 s);
763 foreach_crypto_aead_async_alg
764#undef _
765#define _(c, h, s, k ,d) \
766 vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##c##_##h##_TAG##d, \
767 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
768 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, \
769 s);
770 foreach_crypto_link_async_alg
771#undef _
PiotrX Kleski22848172020-07-08 14:36:34 +0200772 cm->crypto_node_index =
773 vlib_get_node_by_name (vm, (u8 *) "crypto-dispatch")->index;
774
775 return 0;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100776}
777
778VLIB_INIT_FUNCTION (vnet_crypto_init);
779
780/*
781 * fd.io coding-style-patch-verification: ON
782 *
783 * Local Variables:
784 * eval: (c-set-style "gnu")
785 * End:
786 */