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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <vlib/vlib.h>
18 #include <vnet/crypto/crypto.h>
20 vnet_crypto_main_t crypto_main;
22 static_always_inline u32
23 vnet_crypto_process_ops_call_handler (vlib_main_t * vm,
24 vnet_crypto_main_t * cm,
25 vnet_crypto_op_id_t opt,
26 vnet_crypto_op_t * ops[], u32 n_ops)
31 if (cm->ops_handlers[opt] == 0)
35 ops[0]->status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER;
41 return (cm->ops_handlers[opt]) (vm, ops, n_ops);
46 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
48 vnet_crypto_main_t *cm = &crypto_main;
49 const int op_q_size = VLIB_FRAME_SIZE;
50 vnet_crypto_op_t *op_queue[op_q_size];
51 vnet_crypto_op_id_t opt, current_op_type = ~0;
57 for (i = 0; i < n_ops; i++)
61 if (current_op_type != opt || n_op_queue >= op_q_size)
63 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
64 op_queue, n_op_queue);
66 current_op_type = opt;
69 op_queue[n_op_queue++] = &ops[i];
72 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
73 op_queue, n_op_queue);
78 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
81 vnet_crypto_main_t *cm = &crypto_main;
82 vnet_crypto_engine_t *p;
84 vec_add2 (cm->engines, p, 1);
89 hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
91 return p - cm->engines;
95 vnet_crypto_set_handler (char *alg_name, char *engine)
98 vnet_crypto_main_t *cm = &crypto_main;
99 vnet_crypto_alg_data_t *ad;
100 vnet_crypto_engine_t *ce;
103 p = hash_get_mem (cm->alg_index_by_name, alg_name);
107 ad = vec_elt_at_index (cm->algs, p[0]);
109 p = hash_get_mem (cm->engine_index_by_name, engine);
113 ce = vec_elt_at_index (cm->engines, p[0]);
115 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
117 vnet_crypto_op_data_t *od;
118 vnet_crypto_op_id_t id = ad->op_by_type[i];
121 od = cm->opt_data + id;
122 if (ce->ops_handlers[id])
124 od->active_engine_index = p[0];
125 cm->ops_handlers[id] = ce->ops_handlers[id];
133 vnet_crypto_is_set_handler (vnet_crypto_alg_t alg)
135 vnet_crypto_main_t *cm = &crypto_main;
137 return (alg < vec_len (cm->ops_handlers) && NULL != cm->ops_handlers[alg]);
141 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
142 vnet_crypto_op_id_t opt,
143 vnet_crypto_ops_handler_t * fn)
145 vnet_crypto_main_t *cm = &crypto_main;
146 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
147 vnet_crypto_op_data_t *otd = cm->opt_data + opt;
148 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
149 CLIB_CACHE_LINE_BYTES);
150 e->ops_handlers[opt] = fn;
152 if (otd->active_engine_index == ~0)
154 otd->active_engine_index = engine_index;
155 cm->ops_handlers[opt] = fn;
158 ae = vec_elt_at_index (cm->engines, otd->active_engine_index);
159 if (ae->priority < e->priority)
161 otd->active_engine_index = engine_index;
162 cm->ops_handlers[opt] = fn;
169 vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
170 vnet_crypto_key_handler_t * key_handler)
172 vnet_crypto_main_t *cm = &crypto_main;
173 vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
174 e->key_op_handler = key_handler;
179 vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
183 case VNET_CRYPTO_N_ALGS:
185 case VNET_CRYPTO_ALG_NONE:
189 case VNET_CRYPTO_ALG_##n: \
193 foreach_crypto_cipher_alg foreach_crypto_aead_alg
195 /* HMAC allows any key length */
197 case VNET_CRYPTO_ALG_HMAC_##n: \
199 foreach_crypto_hmac_alg
207 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg, u8 * data,
211 vnet_crypto_main_t *cm = &crypto_main;
212 vnet_crypto_engine_t *engine;
213 vnet_crypto_key_t *key;
215 if (!vnet_crypto_key_len_check (alg, length))
218 pool_get_zero (cm->keys, key);
219 index = key - cm->keys;
221 vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
222 clib_memcpy (key->data, data, length);
225 vec_foreach (engine, cm->engines)
226 if (engine->key_op_handler)
227 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
233 vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index)
235 vnet_crypto_main_t *cm = &crypto_main;
236 vnet_crypto_engine_t *engine;
237 vnet_crypto_key_t *key = pool_elt_at_index (cm->keys, index);
240 vec_foreach (engine, cm->engines)
241 if (engine->key_op_handler)
242 engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
245 clib_memset (key->data, 0, vec_len (key->data));
246 vec_free (key->data);
247 pool_put (cm->keys, key);
251 vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
252 vnet_crypto_op_id_t did, char *name, u8 is_aead)
254 vnet_crypto_op_type_t eopt, dopt;
255 vnet_crypto_main_t *cm = &crypto_main;
256 cm->algs[alg].name = name;
257 cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
258 cm->opt_data[eid].active_engine_index = ~0;
259 cm->opt_data[did].active_engine_index = ~0;
262 eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
263 dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
267 eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
268 dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
270 cm->opt_data[eid].type = eopt;
271 cm->opt_data[did].type = dopt;
272 cm->algs[alg].op_by_type[eopt] = eid;
273 cm->algs[alg].op_by_type[dopt] = did;
274 hash_set_mem (cm->alg_index_by_name, name, alg);
278 vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
279 vnet_crypto_op_id_t id, char *name)
281 vnet_crypto_main_t *cm = &crypto_main;
282 cm->algs[alg].name = name;
283 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
284 cm->opt_data[id].alg = alg;
285 cm->opt_data[id].active_engine_index = ~0;
286 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
287 hash_set_mem (cm->alg_index_by_name, name, alg);
291 vnet_crypto_init (vlib_main_t * vm)
293 vnet_crypto_main_t *cm = &crypto_main;
294 vlib_thread_main_t *tm = vlib_get_thread_main ();
295 cm->engine_index_by_name = hash_create_string ( /* size */ 0,
297 cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
298 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
299 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
301 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
302 VNET_CRYPTO_OP_##n##_ENC, \
303 VNET_CRYPTO_OP_##n##_DEC, s, 0);
304 foreach_crypto_cipher_alg;
307 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
308 VNET_CRYPTO_OP_##n##_ENC, \
309 VNET_CRYPTO_OP_##n##_DEC, s, 1);
310 foreach_crypto_aead_alg;
313 vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
314 VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
315 foreach_crypto_hmac_alg;
320 VLIB_INIT_FUNCTION (vnet_crypto_init);
323 * fd.io coding-style-patch-verification: ON
326 * eval: (c-set-style "gnu")