blob: 4458bfcff4df899d0344ca61a8d6456d02306b38 [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
64
Filip Tehlarefcad1a2020-02-04 09:36:04 +000065static_always_inline u32
66vnet_crypto_process_ops_inline (vlib_main_t * vm, vnet_crypto_op_t ops[],
67 vnet_crypto_op_chunk_t * chunks, u32 n_ops)
Damjan Marion91f17dc2019-03-18 18:59:25 +010068{
69 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion085637f2019-04-03 18:39:27 +020070 const int op_q_size = VLIB_FRAME_SIZE;
71 vnet_crypto_op_t *op_queue[op_q_size];
Damjan Marion060bfb92019-03-29 13:47:54 +010072 vnet_crypto_op_id_t opt, current_op_type = ~0;
Damjan Marion085637f2019-04-03 18:39:27 +020073 u32 n_op_queue = 0;
Damjan Marion91f17dc2019-03-18 18:59:25 +010074 u32 rv = 0, i;
75
Damjan Marion085637f2019-04-03 18:39:27 +020076 ASSERT (n_ops >= 1);
77
Damjan Marion91f17dc2019-03-18 18:59:25 +010078 for (i = 0; i < n_ops; i++)
79 {
Damjan Marion085637f2019-04-03 18:39:27 +020080 opt = ops[i].op;
Damjan Marion91f17dc2019-03-18 18:59:25 +010081
Damjan Marion085637f2019-04-03 18:39:27 +020082 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 Tehlarefcad1a2020-02-04 09:36:04 +000085 op_queue, chunks,
86 n_op_queue);
Damjan Marion085637f2019-04-03 18:39:27 +020087 n_op_queue = 0;
88 current_op_type = opt;
89 }
90
91 op_queue[n_op_queue++] = &ops[i];
Damjan Marion91f17dc2019-03-18 18:59:25 +010092 }
93
Damjan Marion085637f2019-04-03 18:39:27 +020094 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
Filip Tehlarefcad1a2020-02-04 09:36:04 +000095 op_queue, chunks, n_op_queue);
Damjan Marion91f17dc2019-03-18 18:59:25 +010096 return rv;
97}
98
99u32
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000100vnet_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
105u32
106vnet_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
112u32
Damjan Marion91f17dc2019-03-18 18:59:25 +0100113vnet_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 Tehlar1469d542019-03-25 09:04:41 -0700124 hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
125
Damjan Marion91f17dc2019-03-18 18:59:25 +0100126 return p - cm->engines;
127}
128
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000129static_always_inline void
130crypto_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 Tehlar1469d542019-03-25 09:04:41 -0700156int
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000157vnet_crypto_set_handler2 (char *alg_name, char *engine,
158 crypto_op_class_type_t oct)
Filip Tehlar1469d542019-03-25 09:04:41 -0700159{
160 uword *p;
161 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion060bfb92019-03-29 13:47:54 +0100162 vnet_crypto_alg_data_t *ad;
Damjan Marion060bfb92019-03-29 13:47:54 +0100163 int i;
Filip Tehlar1469d542019-03-25 09:04:41 -0700164
Damjan Marion060bfb92019-03-29 13:47:54 +0100165 p = hash_get_mem (cm->alg_index_by_name, alg_name);
Filip Tehlar1469d542019-03-25 09:04:41 -0700166 if (!p)
167 return -1;
168
Damjan Marion060bfb92019-03-29 13:47:54 +0100169 ad = vec_elt_at_index (cm->algs, p[0]);
Filip Tehlar1469d542019-03-25 09:04:41 -0700170
171 p = hash_get_mem (cm->engine_index_by_name, engine);
172 if (!p)
173 return -1;
174
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000175 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i += 2)
Damjan Marion060bfb92019-03-29 13:47:54 +0100176 {
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 Tehlarefcad1a2020-02-04 09:36:04 +0000181
Lijian Zhangb15d7962019-09-27 16:25:35 +0800182 od = cm->opt_data + id;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000183 crypto_set_active_engine (od, id, p[0], oct);
Damjan Marion060bfb92019-03-29 13:47:54 +0100184 }
Filip Tehlar1469d542019-03-25 09:04:41 -0700185
186 return 0;
187}
188
Neale Rannsece2ae02019-06-21 12:44:11 +0000189int
190vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
191{
192 vnet_crypto_main_t *cm = &crypto_main;
193
Benoît Ganne6545df72019-11-06 14:21:07 +0100194 return (alg < vec_len (cm->ops_handlers) && NULL != cm->ops_handlers[alg]);
Neale Rannsece2ae02019-06-21 12:44:11 +0000195}
196
Damjan Mariond1bed682019-04-24 15:20:35 +0200197void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000198vnet_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 Marion91f17dc2019-03-18 18:59:25 +0100203{
204 vnet_crypto_main_t *cm = &crypto_main;
205 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
Damjan Marion060bfb92019-03-29 13:47:54 +0100206 vnet_crypto_op_data_t *otd = cm->opt_data + opt;
207 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100208 CLIB_CACHE_LINE_BYTES);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000209 vec_validate_aligned (cm->chained_ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
210 CLIB_CACHE_LINE_BYTES);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100211
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000212 if (fn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100213 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000214 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 Marion91f17dc2019-03-18 18:59:25 +0100224 }
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000225
226 if (cfn)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100227 {
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000228 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 Marion91f17dc2019-03-18 18:59:25 +0100238 }
239
Damjan Mariond1bed682019-04-24 15:20:35 +0200240 return;
241}
242
243void
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000244vnet_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
251void
252vnet_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
260void
261vnet_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
269void
Damjan Mariond1bed682019-04-24 15:20:35 +0200270vnet_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 Gannebe954442019-04-29 16:05:46 +0200279static int
280vnet_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 Rannse6be7022019-06-04 15:37:34 +0000292 return 1; \
293 break;
Benoît Gannebe954442019-04-29 16:05:46 +0200294 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 Mariond1bed682019-04-24 15:20:35 +0200307u32
308vnet_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 Gannebe954442019-04-29 16:05:46 +0200315
Benoît Gannebe954442019-04-29 16:05:46 +0200316 if (!vnet_crypto_key_len_check (alg, length))
317 return ~0;
318
Damjan Mariond1bed682019-04-24 15:20:35 +0200319 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
333void
334vnet_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 Marion060bfb92019-03-29 13:47:54 +0100351static void
352vnet_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 Tehlarefcad1a2020-02-04 09:36:04 +0000357
Damjan Marion060bfb92019-03-29 13:47:54 +0100358 cm->algs[alg].name = name;
359 cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000360 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 Marion060bfb92019-03-29 13:47:54 +0100364 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
381static void
382vnet_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 Tehlarefcad1a2020-02-04 09:36:04 +0000389 cm->opt_data[id].active_engine_index_simple = ~0;
390 cm->opt_data[id].active_engine_index_chained = ~0;
Damjan Marion060bfb92019-03-29 13:47:54 +0100391 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
392 hash_set_mem (cm->alg_index_by_name, name, alg);
393}
394
Damjan Marion91f17dc2019-03-18 18:59:25 +0100395clib_error_t *
396vnet_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 Tehlar1469d542019-03-25 09:04:41 -0700400 cm->engine_index_by_name = hash_create_string ( /* size */ 0,
401 sizeof (uword));
Damjan Marion060bfb92019-03-29 13:47:54 +0100402 cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
Damjan Marion91f17dc2019-03-18 18:59:25 +0100403 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
404 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
Benoît Gannebe954442019-04-29 16:05:46 +0200405#define _(n, s, l) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100406 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 Marion91f17dc2019-03-18 18:59:25 +0100410#undef _
Benoît Gannebe954442019-04-29 16:05:46 +0200411#define _(n, s, l) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100412 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 Marion91f17dc2019-03-18 18:59:25 +0100416#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100417#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 Marion91f17dc2019-03-18 18:59:25 +0100422 return 0;
423}
424
425VLIB_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 */