blob: eeb120400a88666e25b5a0c8fe6d748fb198e310 [file] [log] [blame]
Damjan Marion91f17dc2019-03-18 18:59:25 +01001/*
2 * Copyright (c) 2019 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#ifndef included_vnet_crypto_crypto_h
17#define included_vnet_crypto_crypto_h
18
Damjan Marion91f17dc2019-03-18 18:59:25 +010019#include <vlib/vlib.h>
20
PiotrX Kleski22848172020-07-08 14:36:34 +020021#define VNET_CRYPTO_FRAME_SIZE 64
Fan Zhangf5395782020-04-29 14:00:03 +010022
Benoît Gannebe954442019-04-29 16:05:46 +020023/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */
Damjan Marion060bfb92019-03-29 13:47:54 +010024#define foreach_crypto_cipher_alg \
Benoît Gannebe954442019-04-29 16:05:46 +020025 _(DES_CBC, "des-cbc", 7) \
Neale Rannse6be7022019-06-04 15:37:34 +000026 _(3DES_CBC, "3des-cbc", 24) \
Benoît Gannebe954442019-04-29 16:05:46 +020027 _(AES_128_CBC, "aes-128-cbc", 16) \
28 _(AES_192_CBC, "aes-192-cbc", 24) \
29 _(AES_256_CBC, "aes-256-cbc", 32) \
30 _(AES_128_CTR, "aes-128-ctr", 16) \
31 _(AES_192_CTR, "aes-192-ctr", 24) \
32 _(AES_256_CTR, "aes-256-ctr", 32)
Damjan Marion91f17dc2019-03-18 18:59:25 +010033
Benoît Gannebe954442019-04-29 16:05:46 +020034/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */
Damjan Marion060bfb92019-03-29 13:47:54 +010035#define foreach_crypto_aead_alg \
Benoît Gannebe954442019-04-29 16:05:46 +020036 _(AES_128_GCM, "aes-128-gcm", 16) \
37 _(AES_192_GCM, "aes-192-gcm", 24) \
Artem Glazychev61f49aa2020-08-31 15:37:39 +070038 _(AES_256_GCM, "aes-256-gcm", 32) \
39 _(CHACHA20_POLY1305, "chacha20-poly1305", 32)
Damjan Marion060bfb92019-03-29 13:47:54 +010040
41#define foreach_crypto_hmac_alg \
Filip Tehlare225f712019-03-19 10:37:06 -070042 _(MD5, "md5") \
Damjan Marion91f17dc2019-03-18 18:59:25 +010043 _(SHA1, "sha-1") \
44 _(SHA224, "sha-224") \
45 _(SHA256, "sha-256") \
46 _(SHA384, "sha-384") \
47 _(SHA512, "sha-512")
48
Damjan Marion060bfb92019-03-29 13:47:54 +010049#define foreach_crypto_op_type \
50 _(ENCRYPT, "encrypt") \
51 _(DECRYPT, "decrypt") \
52 _(AEAD_ENCRYPT, "aead-encrypt") \
53 _(AEAD_DECRYPT, "aead-decrypt") \
54 _(HMAC, "hmac")
55
56typedef enum
57{
58#define _(n, s) VNET_CRYPTO_OP_TYPE_##n,
59 foreach_crypto_op_type
60#undef _
61 VNET_CRYPTO_OP_N_TYPES,
62} vnet_crypto_op_type_t;
63
64#define foreach_crypto_op_status \
Fan Zhangf5395782020-04-29 14:00:03 +010065 _(IDLE, "idle") \
Damjan Marion060bfb92019-03-29 13:47:54 +010066 _(PENDING, "pending") \
Fan Zhangf5395782020-04-29 14:00:03 +010067 _(WORK_IN_PROGRESS, "work-in-progress") \
Damjan Marion060bfb92019-03-29 13:47:54 +010068 _(COMPLETED, "completed") \
69 _(FAIL_NO_HANDLER, "no-handler") \
Fan Zhangf5395782020-04-29 14:00:03 +010070 _(FAIL_BAD_HMAC, "bad-hmac") \
71 _(FAIL_ENGINE_ERR, "engine-error")
72
73/** async crypto **/
74
75/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, TAG_LEN, AAD_LEN */
76#define foreach_crypto_aead_async_alg \
77 _(AES_128_GCM, "aes-128-gcm-aad8", 16, 16, 8) \
78 _(AES_128_GCM, "aes-128-gcm-aad12", 16, 16, 12) \
79 _(AES_192_GCM, "aes-192-gcm-aad8", 24, 16, 8) \
80 _(AES_192_GCM, "aes-192-gcm-aad12", 24, 16, 12) \
81 _(AES_256_GCM, "aes-256-gcm-aad8", 32, 16, 8) \
Artem Glazychev61f49aa2020-08-31 15:37:39 +070082 _(AES_256_GCM, "aes-256-gcm-aad12", 32, 16, 12) \
83 _(CHACHA20_POLY1305, "chacha20-poly1305-aad8", 32, 16, 8) \
84 _(CHACHA20_POLY1305, "chacha20-poly1305-aad12", 32, 16, 12)
Fan Zhangf5395782020-04-29 14:00:03 +010085
86/* CRYPTO_ID, INTEG_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, DIGEST_LEN */
87#define foreach_crypto_link_async_alg \
88 _ (AES_128_CBC, SHA1, "aes-128-cbc-hmac-sha-1", 16, 12) \
89 _ (AES_192_CBC, SHA1, "aes-192-cbc-hmac-sha-1", 24, 12) \
90 _ (AES_256_CBC, SHA1, "aes-256-cbc-hmac-sha-1", 32, 12) \
91 _ (AES_128_CBC, SHA224, "aes-128-cbc-hmac-sha-224", 16, 14) \
92 _ (AES_192_CBC, SHA224, "aes-192-cbc-hmac-sha-224", 24, 14) \
93 _ (AES_256_CBC, SHA224, "aes-256-cbc-hmac-sha-224", 32, 14) \
94 _ (AES_128_CBC, SHA256, "aes-128-cbc-hmac-sha-256", 16, 16) \
95 _ (AES_192_CBC, SHA256, "aes-192-cbc-hmac-sha-256", 24, 16) \
96 _ (AES_256_CBC, SHA256, "aes-256-cbc-hmac-sha-256", 32, 16) \
97 _ (AES_128_CBC, SHA384, "aes-128-cbc-hmac-sha-384", 16, 24) \
98 _ (AES_192_CBC, SHA384, "aes-192-cbc-hmac-sha-384", 24, 24) \
99 _ (AES_256_CBC, SHA384, "aes-256-cbc-hmac-sha-384", 32, 24) \
100 _ (AES_128_CBC, SHA512, "aes-128-cbc-hmac-sha-512", 16, 32) \
101 _ (AES_192_CBC, SHA512, "aes-192-cbc-hmac-sha-512", 24, 32) \
102 _ (AES_256_CBC, SHA512, "aes-256-cbc-hmac-sha-512", 32, 32)
103
104#define foreach_crypto_async_op_type \
105 _(ENCRYPT, "async-encrypt") \
106 _(DECRYPT, "async-decrypt")
Damjan Marion060bfb92019-03-29 13:47:54 +0100107
108typedef enum
109{
Damjan Mariond1bed682019-04-24 15:20:35 +0200110 VNET_CRYPTO_KEY_OP_ADD,
111 VNET_CRYPTO_KEY_OP_DEL,
112 VNET_CRYPTO_KEY_OP_MODIFY,
113} vnet_crypto_key_op_t;
114
115typedef enum
116{
Damjan Marion060bfb92019-03-29 13:47:54 +0100117#define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
118 foreach_crypto_op_status
119#undef _
120 VNET_CRYPTO_OP_N_STATUS,
121} vnet_crypto_op_status_t;
122
Damjan Marion91f17dc2019-03-18 18:59:25 +0100123/* *INDENT-OFF* */
124typedef enum
125{
Damjan Mariond1bed682019-04-24 15:20:35 +0200126 VNET_CRYPTO_ALG_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200127#define _(n, s, l) VNET_CRYPTO_ALG_##n,
Damjan Marion060bfb92019-03-29 13:47:54 +0100128 foreach_crypto_cipher_alg
129 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100130#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100131#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
132 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100133#undef _
134 VNET_CRYPTO_N_ALGS,
135} vnet_crypto_alg_t;
136
Fan Zhangf5395782020-04-29 14:00:03 +0100137typedef enum
138{
139#define _(n, s) VNET_CRYPTO_ASYNC_OP_TYPE_##n,
140 foreach_crypto_async_op_type
141#undef _
142 VNET_CRYPTO_ASYNC_OP_N_TYPES,
143} vnet_crypto_async_op_type_t;
144
145typedef enum
146{
147 VNET_CRYPTO_ASYNC_ALG_NONE = 0,
148#define _(n, s, k, t, a) \
149 VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a,
150 foreach_crypto_aead_async_alg
151#undef _
152#define _(c, h, s, k ,d) \
153 VNET_CRYPTO_ALG_##c##_##h##_TAG##d,
154 foreach_crypto_link_async_alg
155#undef _
156 VNET_CRYPTO_N_ASYNC_ALGS,
157} vnet_crypto_async_alg_t;
158
159typedef enum
160{
161 VNET_CRYPTO_ASYNC_OP_NONE = 0,
162#define _(n, s, k, t, a) \
163 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
164 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC,
165 foreach_crypto_aead_async_alg
166#undef _
167#define _(c, h, s, k ,d) \
168 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
169 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC,
170 foreach_crypto_link_async_alg
171#undef _
172 VNET_CRYPTO_ASYNC_OP_N_IDS,
173} vnet_crypto_async_op_id_t;
174
Damjan Mariond1bed682019-04-24 15:20:35 +0200175typedef struct
176{
Fan Zhangf5395782020-04-29 14:00:03 +0100177 union
178 {
179 struct
180 {
181 u8 *data;
182 vnet_crypto_alg_t alg:8;
183 };
184 struct
185 {
186 u32 index_crypto;
187 u32 index_integ;
188 vnet_crypto_async_alg_t async_alg:8;
189 };
190 };
191#define VNET_CRYPTO_KEY_TYPE_DATA 0
192#define VNET_CRYPTO_KEY_TYPE_LINK 1
193 u8 type;
Damjan Mariond1bed682019-04-24 15:20:35 +0200194} vnet_crypto_key_t;
195
Damjan Marion91f17dc2019-03-18 18:59:25 +0100196typedef enum
197{
198 VNET_CRYPTO_OP_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200199#define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
Damjan Marion060bfb92019-03-29 13:47:54 +0100200 foreach_crypto_cipher_alg
201 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100202#undef _
203#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
Damjan Marion060bfb92019-03-29 13:47:54 +0100204 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100205#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100206 VNET_CRYPTO_N_OP_IDS,
207} vnet_crypto_op_id_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100208/* *INDENT-ON* */
209
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000210typedef enum
211{
212 CRYPTO_OP_SIMPLE,
213 CRYPTO_OP_CHAINED,
214 CRYPTO_OP_BOTH,
215} crypto_op_class_type_t;
216
Damjan Marion91f17dc2019-03-18 18:59:25 +0100217typedef struct
218{
219 char *name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100220 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100221} vnet_crypto_alg_data_t;
222
Damjan Marion91f17dc2019-03-18 18:59:25 +0100223typedef struct
224{
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000225 u8 *src;
226 u8 *dst;
227 u32 len;
228} vnet_crypto_op_chunk_t;
229
230typedef struct
231{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100232 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000233 uword user_data;
Damjan Marion060bfb92019-03-29 13:47:54 +0100234 vnet_crypto_op_id_t op:16;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100235 vnet_crypto_op_status_t status:8;
Damjan Marion060bfb92019-03-29 13:47:54 +0100236 u8 flags;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100237#define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
238#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000239#define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS (1 << 2)
240
241 union
242 {
243 u8 digest_len;
244 u8 tag_len;
245 };
Damjan Marion060bfb92019-03-29 13:47:54 +0100246 u16 aad_len;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000247
248 union
249 {
250 struct
251 {
252 u8 *src;
253 u8 *dst;
254 };
255
256 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
257 u16 n_chunks;
258 };
259
260 union
261 {
262 u32 len;
263 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
264 u32 chunk_index;
265 };
266
267 u32 key_index;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100268 u8 *iv;
Damjan Marion060bfb92019-03-29 13:47:54 +0100269 u8 *aad;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000270
271 union
272 {
273 u8 *tag;
274 u8 *digest;
275 };
Damjan Marion91f17dc2019-03-18 18:59:25 +0100276} vnet_crypto_op_t;
277
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000278STATIC_ASSERT_SIZEOF (vnet_crypto_op_t, CLIB_CACHE_LINE_BYTES);
279
Damjan Marion91f17dc2019-03-18 18:59:25 +0100280typedef struct
281{
Damjan Marion060bfb92019-03-29 13:47:54 +0100282 vnet_crypto_op_type_t type;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100283 vnet_crypto_alg_t alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000284 u32 active_engine_index_simple;
285 u32 active_engine_index_chained;
Damjan Marion060bfb92019-03-29 13:47:54 +0100286} vnet_crypto_op_data_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100287
288typedef struct
289{
Fan Zhangf5395782020-04-29 14:00:03 +0100290 vnet_crypto_async_op_type_t type;
291 vnet_crypto_async_alg_t alg;
292 u32 active_engine_index_async;
293} vnet_crypto_async_op_data_t;
294
295typedef struct
296{
297 char *name;
298 vnet_crypto_async_op_id_t op_by_type[VNET_CRYPTO_ASYNC_OP_N_TYPES];
299} vnet_crypto_async_alg_data_t;
300
301typedef struct
302{
Fan Zhangf5395782020-04-29 14:00:03 +0100303 u8 *iv;
304 union
305 {
306 u8 *digest;
307 u8 *tag;
308 };
309 u8 *aad;
Neale Ranns63ab8512021-02-24 09:18:53 +0000310 u32 key_index;
311 u32 crypto_total_length;
312 i16 crypto_start_offset; /* first buffer offset */
313 i16 integ_start_offset;
314 /* adj total_length for integ, e.g.4 bytes for IPSec ESN */
315 u16 integ_length_adj;
316 vnet_crypto_op_status_t status : 8;
Fan Zhangf5395782020-04-29 14:00:03 +0100317 u8 flags; /**< share same VNET_CRYPTO_OP_FLAG_* values */
318} vnet_crypto_async_frame_elt_t;
319
Neale Ranns63ab8512021-02-24 09:18:53 +0000320/* Assert the size so the compiler will warn us when it changes */
321STATIC_ASSERT_SIZEOF (vnet_crypto_async_frame_elt_t, 5 * sizeof (u64));
322
323typedef enum vnet_crypto_async_frame_state_t_
324{
325 VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED,
326 /* frame waiting to be processed */
327 VNET_CRYPTO_FRAME_STATE_PENDING,
328 VNET_CRYPTO_FRAME_STATE_WORK_IN_PROGRESS,
329 VNET_CRYPTO_FRAME_STATE_SUCCESS,
330 VNET_CRYPTO_FRAME_STATE_ELT_ERROR
331} __clib_packed vnet_crypto_async_frame_state_t;
332
Fan Zhangf5395782020-04-29 14:00:03 +0100333typedef struct
334{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100335 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns63ab8512021-02-24 09:18:53 +0000336 vnet_crypto_async_frame_state_t state;
Fan Zhangf5395782020-04-29 14:00:03 +0100337 vnet_crypto_async_op_id_t op:8;
338 u16 n_elts;
339 vnet_crypto_async_frame_elt_t elts[VNET_CRYPTO_FRAME_SIZE];
340 u32 buffer_indices[VNET_CRYPTO_FRAME_SIZE];
341 u16 next_node_index[VNET_CRYPTO_FRAME_SIZE];
PiotrX Kleski22848172020-07-08 14:36:34 +0200342 u32 enqueue_thread_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100343} vnet_crypto_async_frame_t;
344
345typedef struct
346{
347 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
348 vnet_crypto_async_frame_t *frames[VNET_CRYPTO_ASYNC_OP_N_IDS];
349 vnet_crypto_async_frame_t *frame_pool;
Neale Ranns63ab8512021-02-24 09:18:53 +0000350 u32 *buffer_indices;
Fan Zhangf5395782020-04-29 14:00:03 +0100351 u16 *nexts;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100352} vnet_crypto_thread_t;
353
Damjan Mariond1bed682019-04-24 15:20:35 +0200354typedef u32 vnet_crypto_key_index_t;
355
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000356typedef u32 (vnet_crypto_chained_ops_handler_t) (vlib_main_t * vm,
357 vnet_crypto_op_t * ops[],
358 vnet_crypto_op_chunk_t *
359 chunks, u32 n_ops);
360
Damjan Marion91f17dc2019-03-18 18:59:25 +0100361typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
362 vnet_crypto_op_t * ops[], u32 n_ops);
363
Damjan Mariond1bed682019-04-24 15:20:35 +0200364typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
365 vnet_crypto_key_op_t kop,
366 vnet_crypto_key_index_t idx);
367
Fan Zhangf5395782020-04-29 14:00:03 +0100368/** async crypto function handlers **/
PiotrX Kleski22848172020-07-08 14:36:34 +0200369typedef int
370 (vnet_crypto_frame_enqueue_t) (vlib_main_t * vm,
371 vnet_crypto_async_frame_t * frame);
Fan Zhangf5395782020-04-29 14:00:03 +0100372typedef vnet_crypto_async_frame_t *
PiotrX Kleski22848172020-07-08 14:36:34 +0200373 (vnet_crypto_frame_dequeue_t) (vlib_main_t * vm, u32 * nb_elts_processed,
374 u32 * enqueue_thread_idx);
Fan Zhangf5395782020-04-29 14:00:03 +0100375
PiotrX Kleski22848172020-07-08 14:36:34 +0200376u32
377vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
378 char *desc);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100379
Damjan Mariond1bed682019-04-24 15:20:35 +0200380void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
381 vnet_crypto_op_id_t opt,
382 vnet_crypto_ops_handler_t * oph);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000383
384void vnet_crypto_register_chained_ops_handler (vlib_main_t * vm,
385 u32 engine_index,
386 vnet_crypto_op_id_t opt,
387 vnet_crypto_chained_ops_handler_t
388 * oph);
Fan Zhangf5395782020-04-29 14:00:03 +0100389
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000390void vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
391 vnet_crypto_op_id_t opt,
392 vnet_crypto_ops_handler_t * fn,
393 vnet_crypto_chained_ops_handler_t *
394 cfn);
395
Damjan Mariond1bed682019-04-24 15:20:35 +0200396void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
397 vnet_crypto_key_handler_t * keyh);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100398
Fan Zhangf5395782020-04-29 14:00:03 +0100399/** async crypto register functions */
400u32 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name);
401void vnet_crypto_register_async_handler (vlib_main_t * vm,
402 u32 engine_index,
403 vnet_crypto_async_op_id_t opt,
404 vnet_crypto_frame_enqueue_t * enq_fn,
405 vnet_crypto_frame_dequeue_t *
406 deq_fn);
407
Damjan Marion91f17dc2019-03-18 18:59:25 +0100408typedef struct
409{
410 char *name;
411 char *desc;
412 int priority;
Damjan Mariond1bed682019-04-24 15:20:35 +0200413 vnet_crypto_key_handler_t *key_op_handler;
Damjan Marion060bfb92019-03-29 13:47:54 +0100414 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000415 vnet_crypto_chained_ops_handler_t
416 * chained_ops_handlers[VNET_CRYPTO_N_OP_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100417 vnet_crypto_frame_enqueue_t *enqueue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
418 vnet_crypto_frame_dequeue_t *dequeue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100419} vnet_crypto_engine_t;
420
421typedef struct
422{
Fan Zhangf5395782020-04-29 14:00:03 +0100423 u32 node_idx;
424 u32 next_idx;
425} vnet_crypto_async_next_node_t;
426
427typedef struct
428{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100429 vnet_crypto_alg_data_t *algs;
430 vnet_crypto_thread_t *threads;
431 vnet_crypto_ops_handler_t **ops_handlers;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000432 vnet_crypto_chained_ops_handler_t **chained_ops_handlers;
Fan Zhangf5395782020-04-29 14:00:03 +0100433 vnet_crypto_frame_enqueue_t **enqueue_handlers;
434 vnet_crypto_frame_dequeue_t **dequeue_handlers;
435 clib_bitmap_t *async_active_ids;
Damjan Marion060bfb92019-03-29 13:47:54 +0100436 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100437 vnet_crypto_async_op_data_t async_opt_data[VNET_CRYPTO_ASYNC_OP_N_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100438 vnet_crypto_engine_t *engines;
Damjan Mariond1bed682019-04-24 15:20:35 +0200439 vnet_crypto_key_t *keys;
Filip Tehlar1469d542019-03-25 09:04:41 -0700440 uword *engine_index_by_name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100441 uword *alg_index_by_name;
Fan Zhangf5395782020-04-29 14:00:03 +0100442 uword *async_alg_index_by_name;
443 vnet_crypto_async_alg_data_t *async_algs;
444 u32 async_refcnt;
445 vnet_crypto_async_next_node_t *next_nodes;
PiotrX Kleski22848172020-07-08 14:36:34 +0200446 u32 crypto_node_index;
447#define VNET_CRYPTO_ASYNC_DISPATCH_POLLING 0
448#define VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT 1
449 u8 dispatch_mode;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100450} vnet_crypto_main_t;
451
452extern vnet_crypto_main_t crypto_main;
453
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000454u32 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
455 vnet_crypto_op_chunk_t * chunks,
456 u32 n_ops);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100457u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
458 u32 n_ops);
459
Fan Zhangf5395782020-04-29 14:00:03 +0100460
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000461int vnet_crypto_set_handler2 (char *ops_handler_name, char *engine,
462 crypto_op_class_type_t oct);
Neale Rannsece2ae02019-06-21 12:44:11 +0000463int vnet_crypto_is_set_handler (vnet_crypto_alg_t alg);
Filip Tehlar1469d542019-03-25 09:04:41 -0700464
Damjan Mariond1bed682019-04-24 15:20:35 +0200465u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
466 u8 * data, u16 length);
467void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
Damjan Mariond1bed682019-04-24 15:20:35 +0200468
Fan Zhangf5395782020-04-29 14:00:03 +0100469/**
470 * Use 2 created keys to generate new key for linked algs (cipher + integ)
471 * The returned key index is to be used for linked alg only.
472 **/
473u32 vnet_crypto_key_add_linked (vlib_main_t * vm,
474 vnet_crypto_key_index_t index_crypto,
475 vnet_crypto_key_index_t index_integ);
476
477clib_error_t *crypto_dispatch_enable_disable (int is_enable);
478
479int vnet_crypto_set_async_handler2 (char *alg_name, char *engine);
480
481int vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t opt);
482
483void vnet_crypto_request_async_mode (int is_enable);
484
PiotrX Kleski22848172020-07-08 14:36:34 +0200485void vnet_crypto_set_async_dispatch_mode (u8 mode);
486
Fan Zhangf5395782020-04-29 14:00:03 +0100487vnet_crypto_async_alg_t vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
488 vnet_crypto_alg_t integ_alg);
489
490clib_error_t *crypto_dispatch_enable_disable (int is_enable);
491
Damjan Marion91f17dc2019-03-18 18:59:25 +0100492format_function_t format_vnet_crypto_alg;
493format_function_t format_vnet_crypto_engine;
494format_function_t format_vnet_crypto_op;
Damjan Marion060bfb92019-03-29 13:47:54 +0100495format_function_t format_vnet_crypto_op_type;
496format_function_t format_vnet_crypto_op_status;
Damjan Marion61c0a3d2019-04-05 19:42:21 +0200497unformat_function_t unformat_vnet_crypto_alg;
Damjan Mariona03d1822019-03-28 21:40:48 +0100498
Fan Zhangf5395782020-04-29 14:00:03 +0100499format_function_t format_vnet_crypto_async_op;
500format_function_t format_vnet_crypto_async_alg;
501format_function_t format_vnet_crypto_async_op_type;
502
Damjan Mariona03d1822019-03-28 21:40:48 +0100503static_always_inline void
Damjan Marion060bfb92019-03-29 13:47:54 +0100504vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
Damjan Mariona03d1822019-03-28 21:40:48 +0100505{
506 if (CLIB_DEBUG > 0)
507 clib_memset (op, 0xfe, sizeof (*op));
508 op->op = type;
509 op->flags = 0;
Damjan Mariond1bed682019-04-24 15:20:35 +0200510 op->key_index = ~0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000511 op->n_chunks = 0;
Damjan Mariona03d1822019-03-28 21:40:48 +0100512}
513
Damjan Marion060bfb92019-03-29 13:47:54 +0100514static_always_inline vnet_crypto_op_type_t
515vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
516{
517 vnet_crypto_main_t *cm = &crypto_main;
Lijian Zhangb15d7962019-09-27 16:25:35 +0800518 ASSERT (id < VNET_CRYPTO_N_OP_IDS);
519 vnet_crypto_op_data_t *od = cm->opt_data + id;
Damjan Marion060bfb92019-03-29 13:47:54 +0100520 return od->type;
521}
522
Damjan Mariond1bed682019-04-24 15:20:35 +0200523static_always_inline vnet_crypto_key_t *
524vnet_crypto_get_key (vnet_crypto_key_index_t index)
525{
526 vnet_crypto_main_t *cm = &crypto_main;
527 return vec_elt_at_index (cm->keys, index);
528}
529
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000530static_always_inline int
531vnet_crypto_set_handler (char *alg_name, char *engine)
532{
533 return vnet_crypto_set_handler2 (alg_name, engine, CRYPTO_OP_BOTH);
534}
535
Fan Zhangf5395782020-04-29 14:00:03 +0100536/** async crypto inline functions **/
537
538static_always_inline vnet_crypto_async_frame_t *
539vnet_crypto_async_get_frame (vlib_main_t * vm, vnet_crypto_async_op_id_t opt)
540{
541 vnet_crypto_main_t *cm = &crypto_main;
542 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
543 vnet_crypto_async_frame_t *f = ct->frames[opt];
544
545 if (!f)
546 {
547 pool_get_aligned (ct->frame_pool, f, CLIB_CACHE_LINE_BYTES);
548 if (CLIB_DEBUG > 0)
549 clib_memset (f, 0xfe, sizeof (*f));
550 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
551 f->op = opt;
552 f->n_elts = 0;
553 ct->frames[opt] = f;
554 }
555 return f;
556}
557
558static_always_inline void
559vnet_crypto_async_free_frame (vlib_main_t * vm,
560 vnet_crypto_async_frame_t * frame)
561{
562 vnet_crypto_main_t *cm = &crypto_main;
563 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
564 pool_put (ct->frame_pool, frame);
565}
566
567static_always_inline int
568vnet_crypto_async_submit_open_frame (vlib_main_t * vm,
569 vnet_crypto_async_frame_t * frame)
570{
571 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleski22848172020-07-08 14:36:34 +0200572 vlib_thread_main_t *tm = vlib_get_thread_main ();
Fan Zhangf5395782020-04-29 14:00:03 +0100573 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
574 vnet_crypto_async_op_id_t opt = frame->op;
PiotrX Kleski22848172020-07-08 14:36:34 +0200575 u32 i = vlib_num_workers () > 0;
576
PiotrX Kleskief69b512020-11-24 08:26:26 +0000577 frame->state = VNET_CRYPTO_FRAME_STATE_PENDING;
PiotrX Kleski22848172020-07-08 14:36:34 +0200578 frame->enqueue_thread_index = vm->thread_index;
PiotrX Kleskief69b512020-11-24 08:26:26 +0000579
580 int ret = (cm->enqueue_handlers[frame->op]) (vm, frame);
581
Fan Zhangf5395782020-04-29 14:00:03 +0100582 clib_bitmap_set_no_check (cm->async_active_ids, opt, 1);
583 if (PREDICT_TRUE (ret == 0))
584 {
585 vnet_crypto_async_frame_t *nf = 0;
Fan Zhangf5395782020-04-29 14:00:03 +0100586 pool_get_aligned (ct->frame_pool, nf, CLIB_CACHE_LINE_BYTES);
587 if (CLIB_DEBUG > 0)
588 clib_memset (nf, 0xfe, sizeof (*nf));
589 nf->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
590 nf->op = opt;
591 nf->n_elts = 0;
592 ct->frames[opt] = nf;
593 }
PiotrX Kleskief69b512020-11-24 08:26:26 +0000594 else
595 {
596 frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
597 }
PiotrX Kleski22848172020-07-08 14:36:34 +0200598
599 if (cm->dispatch_mode == VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT)
600 {
601 for (; i < tm->n_vlib_mains; i++)
602 {
603 vlib_node_set_interrupt_pending (vlib_mains[i],
604 cm->crypto_node_index);
605 }
606 }
Fan Zhangf5395782020-04-29 14:00:03 +0100607 return ret;
608}
609
610static_always_inline int
611vnet_crypto_async_add_to_frame (vlib_main_t * vm,
612 vnet_crypto_async_frame_t ** frame,
613 u32 key_index,
614 u32 crypto_len, i16 integ_len_adj,
615 i16 crypto_start_offset,
616 u16 integ_start_offset,
617 u32 buffer_index,
618 u16 next_node,
619 u8 * iv, u8 * tag, u8 * aad, u8 flags)
620{
621 vnet_crypto_async_frame_t *f = *frame;
622 vnet_crypto_async_frame_elt_t *fe;
623 u16 index;
624
625 if (PREDICT_FALSE (f->n_elts == VNET_CRYPTO_FRAME_SIZE))
626 {
627 vnet_crypto_async_op_id_t opt = f->op;
628 int ret;
629 ret = vnet_crypto_async_submit_open_frame (vm, f);
630 if (PREDICT_FALSE (ret < 0))
631 return -1;
632 f = vnet_crypto_async_get_frame (vm, opt);
633 *frame = f;
634 }
635
636 index = f->n_elts;
637 fe = &f->elts[index];
638 f->n_elts++;
639 fe->key_index = key_index;
640 fe->crypto_total_length = crypto_len;
641 fe->crypto_start_offset = crypto_start_offset;
642 fe->integ_start_offset = integ_start_offset;
643 fe->integ_length_adj = integ_len_adj;
644 fe->iv = iv;
645 fe->tag = tag;
646 fe->aad = aad;
647 fe->flags = flags;
648 f->buffer_indices[index] = buffer_index;
649 f->next_node_index[index] = next_node;
650
651 return 0;
652}
653
654static_always_inline void
655vnet_crypto_async_reset_frame (vnet_crypto_async_frame_t * f)
656{
657 vnet_crypto_async_op_id_t opt;
658 ASSERT (f != 0);
PiotrX Kleski8ed06c02020-11-24 07:34:09 +0000659 ASSERT ((f->state == VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED
660 || f->state == VNET_CRYPTO_FRAME_STATE_ELT_ERROR));
Fan Zhangf5395782020-04-29 14:00:03 +0100661 opt = f->op;
662 if (CLIB_DEBUG > 0)
663 clib_memset (f, 0xfe, sizeof (*f));
664 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
665 f->op = opt;
666 f->n_elts = 0;
667}
668
Damjan Marion91f17dc2019-03-18 18:59:25 +0100669#endif /* included_vnet_crypto_crypto_h */
670
671/*
672 * fd.io coding-style-patch-verification: ON
673 *
674 * Local Variables:
675 * eval: (c-set-style "gnu")
676 * End:
677 */