FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
crypto.c
Go to the documentation of this file.
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 
21 
26  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  {
33  while (n_ops--)
34  {
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 
45 u32
47 {
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;
52  u32 n_op_queue = 0;
53  u32 rv = 0, i;
54 
55  ASSERT (n_ops >= 1);
56 
57  for (i = 0; i < n_ops; i++)
58  {
59  opt = ops[i].op;
60 
61  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];
70  }
71 
72  rv += vnet_crypto_process_ops_call_handler (vm, cm, current_op_type,
73  op_queue, n_op_queue);
74  return rv;
75 }
76 
77 u32
79  char *desc)
80 {
83 
84  vec_add2 (cm->engines, p, 1);
85  p->name = name;
86  p->desc = desc;
87  p->priority = prio;
88 
89  hash_set_mem (cm->engine_index_by_name, p->name, p - cm->engines);
90 
91  return p - cm->engines;
92 }
93 
94 int
95 vnet_crypto_set_handler (char *alg_name, char *engine)
96 {
97  uword *p;
101  int i;
102 
103  p = hash_get_mem (cm->alg_index_by_name, alg_name);
104  if (!p)
105  return -1;
106 
107  ad = vec_elt_at_index (cm->algs, p[0]);
108 
109  p = hash_get_mem (cm->engine_index_by_name, engine);
110  if (!p)
111  return -1;
112 
113  ce = vec_elt_at_index (cm->engines, p[0]);
114 
115  for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
116  {
118  vnet_crypto_op_id_t id = ad->op_by_type[i];
119  if (id == 0)
120  continue;
121  od = cm->opt_data + id;
122  if (ce->ops_handlers[id])
123  {
124  od->active_engine_index = p[0];
125  cm->ops_handlers[id] = ce->ops_handlers[id];
126  }
127  }
128 
129  return 0;
130 }
131 
132 int
134 {
136 
137  return (alg < vec_len (cm->ops_handlers) && NULL != cm->ops_handlers[alg]);
138 }
139 
140 void
144 {
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;
150  e->ops_handlers[opt] = fn;
151 
152  if (otd->active_engine_index == ~0)
153  {
154  otd->active_engine_index = engine_index;
155  cm->ops_handlers[opt] = fn;
156  return;
157  }
159  if (ae->priority < e->priority)
160  {
161  otd->active_engine_index = engine_index;
162  cm->ops_handlers[opt] = fn;
163  }
164 
165  return;
166 }
167 
168 void
170  vnet_crypto_key_handler_t * key_handler)
171 {
173  vnet_crypto_engine_t *e = vec_elt_at_index (cm->engines, engine_index);
174  e->key_op_handler = key_handler;
175  return;
176 }
177 
178 static int
180 {
181  switch (alg)
182  {
183  case VNET_CRYPTO_N_ALGS:
184  return 0;
186  return 1;
187 
188 #define _(n, s, l) \
189  case VNET_CRYPTO_ALG_##n: \
190  if ((l) == length) \
191  return 1; \
192  break;
194 #undef _
195  /* HMAC allows any key length */
196 #define _(n, s) \
197  case VNET_CRYPTO_ALG_HMAC_##n: \
198  return 1;
200 #undef _
201  }
202 
203  return 0;
204 }
205 
206 u32
208  u16 length)
209 {
210  u32 index;
212  vnet_crypto_engine_t *engine;
214 
215  if (!vnet_crypto_key_len_check (alg, length))
216  return ~0;
217 
218  pool_get_zero (cm->keys, key);
219  index = key - cm->keys;
220  key->alg = alg;
221  vec_validate_aligned (key->data, length - 1, CLIB_CACHE_LINE_BYTES);
222  clib_memcpy (key->data, data, length);
223 
224  /* *INDENT-OFF* */
225  vec_foreach (engine, cm->engines)
226  if (engine->key_op_handler)
227  engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_ADD, index);
228  /* *INDENT-ON* */
229  return index;
230 }
231 
232 void
234 {
236  vnet_crypto_engine_t *engine;
238 
239  /* *INDENT-OFF* */
240  vec_foreach (engine, cm->engines)
241  if (engine->key_op_handler)
242  engine->key_op_handler (vm, VNET_CRYPTO_KEY_OP_DEL, index);
243  /* *INDENT-ON* */
244 
245  clib_memset (key->data, 0, vec_len (key->data));
246  vec_free (key->data);
247  pool_put (cm->keys, key);
248 }
249 
250 static void
252  vnet_crypto_op_id_t did, char *name, u8 is_aead)
253 {
254  vnet_crypto_op_type_t eopt, dopt;
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;
260  if (is_aead)
261  {
262  eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
263  dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
264  }
265  else
266  {
267  eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
268  dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
269  }
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);
275 }
276 
277 static void
279  vnet_crypto_op_id_t id, char *name)
280 {
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);
288 }
289 
290 clib_error_t *
292 {
295  cm->engine_index_by_name = hash_create_string ( /* size */ 0,
296  sizeof (uword));
297  cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
300 #define _(n, s, l) \
301  vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
302  VNET_CRYPTO_OP_##n##_ENC, \
303  VNET_CRYPTO_OP_##n##_DEC, s, 0);
305 #undef _
306 #define _(n, s, l) \
307  vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
308  VNET_CRYPTO_OP_##n##_ENC, \
309  VNET_CRYPTO_OP_##n##_DEC, s, 1);
311 #undef _
312 #define _(n, s) \
313  vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
314  VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
316 #undef _
317  return 0;
318 }
319 
321 
322 /*
323  * fd.io coding-style-patch-verification: ON
324  *
325  * Local Variables:
326  * eval: (c-set-style "gnu")
327  * End:
328  */
u32 vnet_crypto_process_ops(vlib_main_t *vm, vnet_crypto_op_t ops[], u32 n_ops)
Definition: crypto.c:46
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
vnet_crypto_engine_t * engines
Definition: crypto.h:192
vnet_crypto_thread_t * threads
Definition: crypto.h:189
uword * alg_index_by_name
Definition: crypto.h:195
vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS]
Definition: crypto.h:191
#define pool_get_zero(P, E)
Allocate an object E from a pool P and zero it.
Definition: pool.h:239
vnet_crypto_alg_data_t * algs
Definition: crypto.h:188
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES]
Definition: crypto.h:122
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
int i
#define hash_set_mem(h, key, value)
Definition: hash.h:275
u8 data[128]
Definition: ipsec.api:251
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
static void vnet_crypto_init_hmac_data(vnet_crypto_alg_t alg, vnet_crypto_op_id_t id, char *name)
Definition: crypto.c:278
unsigned char u8
Definition: types.h:56
#define clib_memcpy(d, s, n)
Definition: string.h:180
int vnet_crypto_is_set_handler(vnet_crypto_alg_t alg)
Definition: crypto.c:133
vnet_crypto_op_type_t
Definition: crypto.h:56
void vnet_crypto_register_key_handler(vlib_main_t *vm, u32 engine_index, vnet_crypto_key_handler_t *key_handler)
Definition: crypto.c:169
#define static_always_inline
Definition: clib.h:100
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
#define foreach_crypto_aead_alg
Definition: crypto.h:35
vnet_crypto_alg_t alg
Definition: crypto.h:149
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define foreach_crypto_hmac_alg
Definition: crypto.h:40
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:378
u32 vnet_crypto_key_add(vlib_main_t *vm, vnet_crypto_alg_t alg, u8 *data, u16 length)
Definition: crypto.c:207
vnet_crypto_op_id_t op
Definition: crypto.h:128
vnet_crypto_alg_t
Definition: crypto.h:86
#define hash_create_string(elts, value_bytes)
Definition: hash.h:690
vnet_crypto_main_t * cm
Definition: quic_crypto.c:41
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
void() vnet_crypto_key_handler_t(vlib_main_t *vm, vnet_crypto_key_op_t kop, vnet_crypto_key_index_t idx)
Definition: crypto.h:164
int vnet_crypto_set_handler(char *alg_name, char *engine)
Definition: crypto.c:95
vnet_crypto_key_handler_t * key_op_handler
Definition: crypto.h:182
unsigned short u16
Definition: types.h:57
void vnet_crypto_key_del(vlib_main_t *vm, vnet_crypto_key_index_t index)
Definition: crypto.c:233
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
void vnet_crypto_register_ops_handler(vlib_main_t *vm, u32 engine_index, vnet_crypto_op_id_t opt, vnet_crypto_ops_handler_t *fn)
Definition: crypto.c:141
u8 name[64]
Definition: memclnt.api:152
vnet_crypto_alg_t alg
Definition: crypto.h:102
vlib_main_t * vm
Definition: buffer.c:323
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
static int vnet_crypto_key_len_check(vnet_crypto_alg_t alg, u16 length)
Definition: crypto.c:179
u32() vnet_crypto_ops_handler_t(vlib_main_t *vm, vnet_crypto_op_t *ops[], u32 n_ops)
Definition: crypto.h:161
#define ASSERT(truth)
uword * engine_index_by_name
Definition: crypto.h:194
u32 vnet_crypto_key_index_t
Definition: crypto.h:159
static void vnet_crypto_init_cipher_data(vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid, vnet_crypto_op_id_t did, char *name, u8 is_aead)
Definition: crypto.c:251
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
typedef key
Definition: ipsec.api:247
vnet_crypto_op_type_t type
Definition: crypto.h:148
static_always_inline u32 vnet_crypto_process_ops_call_handler(vlib_main_t *vm, vnet_crypto_main_t *cm, vnet_crypto_op_id_t opt, vnet_crypto_op_t *ops[], u32 n_ops)
Definition: crypto.c:23
#define hash_get_mem(h, key)
Definition: hash.h:269
vnet_crypto_ops_handler_t * ops_handlers[VNET_CRYPTO_N_OP_IDS]
Definition: crypto.h:183
vnet_crypto_op_status_t status
Definition: crypto.h:129
vnet_crypto_op_id_t
Definition: crypto.h:105
vnet_crypto_key_t * keys
Definition: crypto.h:193
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
#define vec_foreach(var, vec)
Vector iterator.
u32 id
Definition: udp.api:45
vnet_crypto_ops_handler_t ** ops_handlers
Definition: crypto.h:190
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 vnet_crypto_register_engine(vlib_main_t *vm, char *name, int prio, char *desc)
Definition: crypto.c:78
vnet_crypto_main_t crypto_main
Definition: crypto.c:20
clib_error_t * vnet_crypto_init(vlib_main_t *vm)
Definition: crypto.c:291
#define foreach_crypto_cipher_alg
Definition: crypto.h:24