crypto: add support for AEAD and AES-GCM
[vpp.git] / src / vnet / crypto / crypto.c
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
20 vnet_crypto_main_t crypto_main;
21
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)
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
46 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
47 {
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;
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
78 vnet_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
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;
98   vnet_crypto_main_t *cm = &crypto_main;
99   vnet_crypto_alg_data_t *ad;
100   vnet_crypto_engine_t *ce;
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     {
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);
122       od->active_engine_index = p[0];
123       cm->ops_handlers[id] = ce->ops_handlers[id];
124     }
125
126   return 0;
127 }
128
129 vlib_error_t *
130 vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
131                                   vnet_crypto_op_id_t opt,
132                                   vnet_crypto_ops_handler_t * fn)
133 {
134   vnet_crypto_main_t *cm = &crypto_main;
135   vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
136   vnet_crypto_op_data_t *otd = cm->opt_data + opt;
137   vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_IDS - 1,
138                         CLIB_CACHE_LINE_BYTES);
139   e->ops_handlers[opt] = fn;
140
141   if (otd->active_engine_index == ~0)
142     {
143       otd->active_engine_index = engine_index;
144       cm->ops_handlers[opt] = fn;
145       return 0;
146     }
147   ae = vec_elt_at_index (cm->engines, otd->active_engine_index);
148   if (ae->priority < e->priority)
149     {
150       otd->active_engine_index = engine_index;
151       cm->ops_handlers[opt] = fn;
152     }
153
154   return 0;
155 }
156
157 static void
158 vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
159                               vnet_crypto_op_id_t did, char *name, u8 is_aead)
160 {
161   vnet_crypto_op_type_t eopt, dopt;
162   vnet_crypto_main_t *cm = &crypto_main;
163   cm->algs[alg].name = name;
164   cm->opt_data[eid].alg = cm->opt_data[did].alg = alg;
165   cm->opt_data[eid].active_engine_index = ~0;
166   cm->opt_data[did].active_engine_index = ~0;
167   if (is_aead)
168     {
169       eopt = VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT;
170       dopt = VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT;
171     }
172   else
173     {
174       eopt = VNET_CRYPTO_OP_TYPE_ENCRYPT;
175       dopt = VNET_CRYPTO_OP_TYPE_DECRYPT;
176     }
177   cm->opt_data[eid].type = eopt;
178   cm->opt_data[did].type = dopt;
179   cm->algs[alg].op_by_type[eopt] = eid;
180   cm->algs[alg].op_by_type[dopt] = did;
181   hash_set_mem (cm->alg_index_by_name, name, alg);
182 }
183
184 static void
185 vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
186                             vnet_crypto_op_id_t id, char *name)
187 {
188   vnet_crypto_main_t *cm = &crypto_main;
189   cm->algs[alg].name = name;
190   cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HMAC] = id;
191   cm->opt_data[id].alg = alg;
192   cm->opt_data[id].active_engine_index = ~0;
193   cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HMAC;
194   hash_set_mem (cm->alg_index_by_name, name, alg);
195 }
196
197 clib_error_t *
198 vnet_crypto_init (vlib_main_t * vm)
199 {
200   vnet_crypto_main_t *cm = &crypto_main;
201   vlib_thread_main_t *tm = vlib_get_thread_main ();
202   cm->engine_index_by_name = hash_create_string ( /* size */ 0,
203                                                  sizeof (uword));
204   cm->alg_index_by_name = hash_create_string (0, sizeof (uword));
205   vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
206   vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
207 #define _(n, s) \
208   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
209                                 VNET_CRYPTO_OP_##n##_ENC, \
210                                 VNET_CRYPTO_OP_##n##_DEC, s, 0);
211   foreach_crypto_cipher_alg;
212 #undef _
213 #define _(n, s) \
214   vnet_crypto_init_cipher_data (VNET_CRYPTO_ALG_##n, \
215                                 VNET_CRYPTO_OP_##n##_ENC, \
216                                 VNET_CRYPTO_OP_##n##_DEC, s, 1);
217   foreach_crypto_aead_alg;
218 #undef _
219 #define _(n, s) \
220   vnet_crypto_init_hmac_data (VNET_CRYPTO_ALG_HMAC_##n, \
221                               VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
222   foreach_crypto_hmac_alg;
223 #undef _
224   return 0;
225 }
226
227 VLIB_INIT_FUNCTION (vnet_crypto_init);
228
229 /*
230  * fd.io coding-style-patch-verification: ON
231  *
232  * Local Variables:
233  * eval: (c-set-style "gnu")
234  * End:
235  */