blob: dbdb58b16f6b73fb19d2cf3619626088bcbf0229 [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
Damjan Marion085637f2019-04-03 18:39:27 +020022static_always_inline u32
23vnet_crypto_process_ops_call_handler (vlib_main_t * vm,
24 vnet_crypto_main_t * cm,
Damjan Marion060bfb92019-03-29 13:47:54 +010025 vnet_crypto_op_id_t opt,
Damjan Marion085637f2019-04-03 18:39:27 +020026 vnet_crypto_op_t * ops[], u32 n_ops)
27{
28 if (n_ops == 0)
29 return 0;
30
31 if (cm->ops_handlers[opt] == 0)
32 {
Damjan Marionba01f072019-04-05 11:11:04 +020033 while (n_ops--)
Damjan Marion085637f2019-04-03 18:39:27 +020034 {
35 ops[0]->status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER;
36 ops++;
37 }
38 return 0;
39 }
40
41 return (cm->ops_handlers[opt]) (vm, ops, n_ops);
42}
43
44
Damjan Marion91f17dc2019-03-18 18:59:25 +010045u32
46vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
47{
48 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion085637f2019-04-03 18:39:27 +020049 const int op_q_size = VLIB_FRAME_SIZE;
50 vnet_crypto_op_t *op_queue[op_q_size];
Damjan Marion060bfb92019-03-29 13:47:54 +010051 vnet_crypto_op_id_t opt, current_op_type = ~0;
Damjan Marion085637f2019-04-03 18:39:27 +020052 u32 n_op_queue = 0;
Damjan Marion91f17dc2019-03-18 18:59:25 +010053 u32 rv = 0, i;
54
Damjan Marion085637f2019-04-03 18:39:27 +020055 ASSERT (n_ops >= 1);
56
Damjan Marion91f17dc2019-03-18 18:59:25 +010057 for (i = 0; i < n_ops; i++)
58 {
Damjan Marion085637f2019-04-03 18:39:27 +020059 opt = ops[i].op;
Damjan Marion91f17dc2019-03-18 18:59:25 +010060
Damjan Marion085637f2019-04-03 18:39:27 +020061 if (current_op_type != opt || n_op_queue >= op_q_size)
62 {
63 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
64 op_queue, n_op_queue);
65 n_op_queue = 0;
66 current_op_type = opt;
67 }
68
69 op_queue[n_op_queue++] = &ops[i];
Damjan Marion91f17dc2019-03-18 18:59:25 +010070 }
71
Damjan Marion085637f2019-04-03 18:39:27 +020072 rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
73 op_queue, n_op_queue);
Damjan Marion91f17dc2019-03-18 18:59:25 +010074 return rv;
75}
76
77u32
78vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
79 char *desc)
80{
81 vnet_crypto_main_t *cm = &crypto_main;
82 vnet_crypto_engine_t *p;
83
84 vec_add2 (cm->engines, p, 1);
85 p->name = name;
86 p->desc = desc;
87 p->priority = prio;
88
Filip Tehlar1469d542019-03-25 09:04:41 -070089 hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
90
Damjan Marion91f17dc2019-03-18 18:59:25 +010091 return p - cm->engines;
92}
93
Filip Tehlar1469d542019-03-25 09:04:41 -070094int
Damjan Marion060bfb92019-03-29 13:47:54 +010095vnet_crypto_set_handler (char *alg_name, char *engine)
Filip Tehlar1469d542019-03-25 09:04:41 -070096{
97 uword *p;
98 vnet_crypto_main_t *cm = &crypto_main;
Damjan Marion060bfb92019-03-29 13:47:54 +010099 vnet_crypto_alg_data_t *ad;
Filip Tehlar1469d542019-03-25 09:04:41 -0700100 vnet_crypto_engine_t *ce;
Damjan Marion060bfb92019-03-29 13:47:54 +0100101 int i;
Filip Tehlar1469d542019-03-25 09:04:41 -0700102
Damjan Marion060bfb92019-03-29 13:47:54 +0100103 p = hash_get_mem (cm->alg_index_by_name, alg_name);
Filip Tehlar1469d542019-03-25 09:04:41 -0700104 if (!p)
105 return -1;
106
Damjan Marion060bfb92019-03-29 13:47:54 +0100107 ad = vec_elt_at_index (cm->algs, p[0]);
Filip Tehlar1469d542019-03-25 09:04:41 -0700108
109 p = hash_get_mem (cm->engine_index_by_name, engine);
110 if (!p)
111 return -1;
112
Damjan Marion060bfb92019-03-29 13:47:54 +0100113 ce = vec_elt_at_index (cm->engines, p[0]);
114
115 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
116 {
117 vnet_crypto_op_data_t *od;
118 vnet_crypto_op_id_t id = ad->op_by_type[i];
119 if (id == 0)
120 continue;
121 od = vec_elt_at_index (cm->opt_data, id);
Neale Ranns21ada3b2019-04-11 08:18:34 +0000122 if (ce->ops_handlers[id])
123 {
124 od->active_engine_index = p[0];
125 cm->ops_handlers[id] = ce->ops_handlers[id];
126 }
Damjan Marion060bfb92019-03-29 13:47:54 +0100127 }
Filip Tehlar1469d542019-03-25 09:04:41 -0700128
129 return 0;
130}
131
Damjan Marion91f17dc2019-03-18 18:59:25 +0100132vlib_error_t *
133vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
Damjan Marion060bfb92019-03-29 13:47:54 +0100134 vnet_crypto_op_id_t opt,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100135 vnet_crypto_ops_handler_t * fn)
136{
137 vnet_crypto_main_t *cm = &crypto_main;
138 vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
Damjan Marion060bfb92019-03-29 13:47:54 +0100139 vnet_crypto_op_data_t *otd = cm->opt_data + opt;
140 vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100141 CLIB_CACHE_LINE_BYTES);
142 e->ops_handlers[opt] = fn;
143
144 if (otd->active_engine_index == ~0)
145 {
146 otd->active_engine_index = engine_index;
147 cm->ops_handlers[opt] = fn;
148 return 0;
149 }
150 ae = vec_elt_at_index (cm->engines, otd->active_engine_index);
151 if (ae->priority < e->priority)
152 {
153 otd->active_engine_index = engine_index;
154 cm->ops_handlers[opt] = fn;
155 }
156
157 return 0;
158}
159
Damjan Marion060bfb92019-03-29 13:47:54 +0100160static void
161vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
162 vnet_crypto_op_id_t did, char *name, u8 is_aead)
163{
164 vnet_crypto_op_type_t eopt, dopt;
165 vnet_crypto_main_t *cm = &crypto_main;
166 cm->algs[alg].name = name;
167 cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
168 cm->opt_data[eid].active_engine_index = ~0;
169 cm->opt_data[did].active_engine_index = ~0;
170 if (is_aead)
171 {
172 eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
173 dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
174 }
175 else
176 {
177 eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
178 dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
179 }
180 cm->opt_data[eid].type = eopt;
181 cm->opt_data[did].type = dopt;
182 cm->algs[alg].op_by_type[eopt] = eid;
183 cm->algs[alg].op_by_type[dopt] = did;
184 hash_set_mem (cm->alg_index_by_name, name, alg);
185}
186
187static void
188vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
189 vnet_crypto_op_id_t id, char *name)
190{
191 vnet_crypto_main_t *cm = &crypto_main;
192 cm->algs[alg].name = name;
193 cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
194 cm->opt_data[id].alg = alg;
195 cm->opt_data[id].active_engine_index = ~0;
196 cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
197 hash_set_mem (cm->alg_index_by_name, name, alg);
198}
199
Damjan Marion91f17dc2019-03-18 18:59:25 +0100200clib_error_t *
201vnet_crypto_init (vlib_main_t * vm)
202{
203 vnet_crypto_main_t *cm = &crypto_main;
204 vlib_thread_main_t *tm = vlib_get_thread_main ();
Filip Tehlar1469d542019-03-25 09:04:41 -0700205 cm->engine_index_by_name = hash_create_string ( /* size */ 0,
206 sizeof (uword));
Damjan Marion060bfb92019-03-29 13:47:54 +0100207 cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
Damjan Marion91f17dc2019-03-18 18:59:25 +0100208 vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
209 vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100210#define _(n, s) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100211 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
212 VNET_CRYPTO_OP_##n##_ENC, \
213 VNET_CRYPTO_OP_##n##_DEC, s, 0);
214 foreach_crypto_cipher_alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100215#undef _
Damjan Marion91f17dc2019-03-18 18:59:25 +0100216#define _(n, s) \
Damjan Marion060bfb92019-03-29 13:47:54 +0100217 vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
218 VNET_CRYPTO_OP_##n##_ENC, \
219 VNET_CRYPTO_OP_##n##_DEC, s, 1);
220 foreach_crypto_aead_alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100221#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100222#define _(n, s) \
223 vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
224 VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
225 foreach_crypto_hmac_alg;
226#undef _
Damjan Marion91f17dc2019-03-18 18:59:25 +0100227 return 0;
228}
229
230VLIB_INIT_FUNCTION (vnet_crypto_init);
231
232/*
233 * fd.io coding-style-patch-verification: ON
234 *
235 * Local Variables:
236 * eval: (c-set-style "gnu")
237 * End:
238 */