blob: 71978b64835453437ae31bdc9fa96a1b579cb077 [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 Zhange4db9452021-03-30 17:31:38 +010022#define VNET_CRYPTO_FRAME_POOL_SIZE 1024
Fan Zhangf5395782020-04-29 14:00:03 +010023
Benoît Gannebe954442019-04-29 16:05:46 +020024/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */
Damjan Marion060bfb92019-03-29 13:47:54 +010025#define foreach_crypto_cipher_alg \
Benoît Gannebe954442019-04-29 16:05:46 +020026 _(DES_CBC, "des-cbc", 7) \
Neale Rannse6be7022019-06-04 15:37:34 +000027 _(3DES_CBC, "3des-cbc", 24) \
Benoît Gannebe954442019-04-29 16:05:46 +020028 _(AES_128_CBC, "aes-128-cbc", 16) \
29 _(AES_192_CBC, "aes-192-cbc", 24) \
30 _(AES_256_CBC, "aes-256-cbc", 32) \
31 _(AES_128_CTR, "aes-128-ctr", 16) \
32 _(AES_192_CTR, "aes-192-ctr", 24) \
33 _(AES_256_CTR, "aes-256-ctr", 32)
Damjan Marion91f17dc2019-03-18 18:59:25 +010034
Benoît Gannebe954442019-04-29 16:05:46 +020035/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES */
Damjan Marion060bfb92019-03-29 13:47:54 +010036#define foreach_crypto_aead_alg \
Benoît Gannebe954442019-04-29 16:05:46 +020037 _(AES_128_GCM, "aes-128-gcm", 16) \
38 _(AES_192_GCM, "aes-192-gcm", 24) \
Artem Glazychev61f49aa2020-08-31 15:37:39 +070039 _(AES_256_GCM, "aes-256-gcm", 32) \
40 _(CHACHA20_POLY1305, "chacha20-poly1305", 32)
Damjan Marion060bfb92019-03-29 13:47:54 +010041
Filip Tehlar06111a82021-05-03 15:29:56 +000042#define foreach_crypto_hash_alg \
43 _ (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_hmac_alg \
Filip Tehlare225f712019-03-19 10:37:06 -070050 _(MD5, "md5") \
Damjan Marion91f17dc2019-03-18 18:59:25 +010051 _(SHA1, "sha-1") \
52 _(SHA224, "sha-224") \
53 _(SHA256, "sha-256") \
54 _(SHA384, "sha-384") \
55 _(SHA512, "sha-512")
56
Filip Tehlar06111a82021-05-03 15:29:56 +000057#define foreach_crypto_op_type \
58 _ (ENCRYPT, "encrypt") \
59 _ (DECRYPT, "decrypt") \
60 _ (AEAD_ENCRYPT, "aead-encrypt") \
61 _ (AEAD_DECRYPT, "aead-decrypt") \
62 _ (HMAC, "hmac") \
63 _ (HASH, "hash")
Damjan Marion060bfb92019-03-29 13:47:54 +010064
65typedef enum
66{
67#define _(n, s) VNET_CRYPTO_OP_TYPE_##n,
68 foreach_crypto_op_type
69#undef _
70 VNET_CRYPTO_OP_N_TYPES,
71} vnet_crypto_op_type_t;
72
73#define foreach_crypto_op_status \
Fan Zhangf5395782020-04-29 14:00:03 +010074 _(IDLE, "idle") \
Damjan Marion060bfb92019-03-29 13:47:54 +010075 _(PENDING, "pending") \
Fan Zhangf5395782020-04-29 14:00:03 +010076 _(WORK_IN_PROGRESS, "work-in-progress") \
Damjan Marion060bfb92019-03-29 13:47:54 +010077 _(COMPLETED, "completed") \
78 _(FAIL_NO_HANDLER, "no-handler") \
Fan Zhangf5395782020-04-29 14:00:03 +010079 _(FAIL_BAD_HMAC, "bad-hmac") \
80 _(FAIL_ENGINE_ERR, "engine-error")
81
82/** async crypto **/
83
84/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, TAG_LEN, AAD_LEN */
85#define foreach_crypto_aead_async_alg \
86 _(AES_128_GCM, "aes-128-gcm-aad8", 16, 16, 8) \
87 _(AES_128_GCM, "aes-128-gcm-aad12", 16, 16, 12) \
88 _(AES_192_GCM, "aes-192-gcm-aad8", 24, 16, 8) \
89 _(AES_192_GCM, "aes-192-gcm-aad12", 24, 16, 12) \
90 _(AES_256_GCM, "aes-256-gcm-aad8", 32, 16, 8) \
Artem Glazychev61f49aa2020-08-31 15:37:39 +070091 _(AES_256_GCM, "aes-256-gcm-aad12", 32, 16, 12) \
92 _(CHACHA20_POLY1305, "chacha20-poly1305-aad8", 32, 16, 8) \
93 _(CHACHA20_POLY1305, "chacha20-poly1305-aad12", 32, 16, 12)
Fan Zhangf5395782020-04-29 14:00:03 +010094
95/* CRYPTO_ID, INTEG_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, DIGEST_LEN */
Benoît Ganne40ee2002021-01-22 18:09:40 +010096#define foreach_crypto_link_async_alg \
Alexander Chernavin005d1e42021-02-01 05:17:24 -050097 _ (3DES_CBC, MD5, "3des-cbc-hmac-md5", 24, 12) \
98 _ (AES_128_CBC, MD5, "aes-128-cbc-hmac-md5", 16, 12) \
99 _ (AES_192_CBC, MD5, "aes-192-cbc-hmac-md5", 24, 12) \
100 _ (AES_256_CBC, MD5, "aes-256-cbc-hmac-md5", 32, 12) \
101 _ (3DES_CBC, SHA1, "3des-cbc-hmac-sha-1", 24, 12) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100102 _ (AES_128_CBC, SHA1, "aes-128-cbc-hmac-sha-1", 16, 12) \
103 _ (AES_192_CBC, SHA1, "aes-192-cbc-hmac-sha-1", 24, 12) \
104 _ (AES_256_CBC, SHA1, "aes-256-cbc-hmac-sha-1", 32, 12) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500105 _ (3DES_CBC, SHA224, "3des-cbc-hmac-sha-224", 24, 14) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100106 _ (AES_128_CBC, SHA224, "aes-128-cbc-hmac-sha-224", 16, 14) \
107 _ (AES_192_CBC, SHA224, "aes-192-cbc-hmac-sha-224", 24, 14) \
108 _ (AES_256_CBC, SHA224, "aes-256-cbc-hmac-sha-224", 32, 14) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500109 _ (3DES_CBC, SHA256, "3des-cbc-hmac-sha-256", 24, 16) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100110 _ (AES_128_CBC, SHA256, "aes-128-cbc-hmac-sha-256", 16, 16) \
111 _ (AES_192_CBC, SHA256, "aes-192-cbc-hmac-sha-256", 24, 16) \
112 _ (AES_256_CBC, SHA256, "aes-256-cbc-hmac-sha-256", 32, 16) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500113 _ (3DES_CBC, SHA384, "3des-cbc-hmac-sha-384", 24, 24) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100114 _ (AES_128_CBC, SHA384, "aes-128-cbc-hmac-sha-384", 16, 24) \
115 _ (AES_192_CBC, SHA384, "aes-192-cbc-hmac-sha-384", 24, 24) \
116 _ (AES_256_CBC, SHA384, "aes-256-cbc-hmac-sha-384", 32, 24) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500117 _ (3DES_CBC, SHA512, "3des-cbc-hmac-sha-512", 24, 32) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100118 _ (AES_128_CBC, SHA512, "aes-128-cbc-hmac-sha-512", 16, 32) \
119 _ (AES_192_CBC, SHA512, "aes-192-cbc-hmac-sha-512", 24, 32) \
120 _ (AES_256_CBC, SHA512, "aes-256-cbc-hmac-sha-512", 32, 32) \
121 _ (AES_128_CTR, SHA1, "aes-128-ctr-hmac-sha-1", 16, 12) \
122 _ (AES_192_CTR, SHA1, "aes-192-ctr-hmac-sha-1", 24, 12) \
123 _ (AES_256_CTR, SHA1, "aes-256-ctr-hmac-sha-1", 32, 12)
Fan Zhangf5395782020-04-29 14:00:03 +0100124
125#define foreach_crypto_async_op_type \
126 _(ENCRYPT, "async-encrypt") \
127 _(DECRYPT, "async-decrypt")
Damjan Marion060bfb92019-03-29 13:47:54 +0100128
129typedef enum
130{
Damjan Mariond1bed682019-04-24 15:20:35 +0200131 VNET_CRYPTO_KEY_OP_ADD,
132 VNET_CRYPTO_KEY_OP_DEL,
133 VNET_CRYPTO_KEY_OP_MODIFY,
134} vnet_crypto_key_op_t;
135
136typedef enum
137{
Damjan Marion060bfb92019-03-29 13:47:54 +0100138#define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
139 foreach_crypto_op_status
140#undef _
141 VNET_CRYPTO_OP_N_STATUS,
142} vnet_crypto_op_status_t;
143
Damjan Marion91f17dc2019-03-18 18:59:25 +0100144/* *INDENT-OFF* */
145typedef enum
146{
Damjan Mariond1bed682019-04-24 15:20:35 +0200147 VNET_CRYPTO_ALG_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200148#define _(n, s, l) VNET_CRYPTO_ALG_##n,
Filip Tehlar06111a82021-05-03 15:29:56 +0000149 foreach_crypto_cipher_alg foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100150#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100151#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
Filip Tehlar06111a82021-05-03 15:29:56 +0000152 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100153#undef _
Filip Tehlar06111a82021-05-03 15:29:56 +0000154#define _(n, s) VNET_CRYPTO_ALG_HASH_##n,
155 foreach_crypto_hash_alg
156#undef _
157 VNET_CRYPTO_N_ALGS,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100158} vnet_crypto_alg_t;
159
Fan Zhangf5395782020-04-29 14:00:03 +0100160typedef enum
161{
162#define _(n, s) VNET_CRYPTO_ASYNC_OP_TYPE_##n,
163 foreach_crypto_async_op_type
164#undef _
165 VNET_CRYPTO_ASYNC_OP_N_TYPES,
166} vnet_crypto_async_op_type_t;
167
168typedef enum
169{
170 VNET_CRYPTO_ASYNC_ALG_NONE = 0,
171#define _(n, s, k, t, a) \
172 VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a,
173 foreach_crypto_aead_async_alg
174#undef _
175#define _(c, h, s, k ,d) \
176 VNET_CRYPTO_ALG_##c##_##h##_TAG##d,
177 foreach_crypto_link_async_alg
178#undef _
179 VNET_CRYPTO_N_ASYNC_ALGS,
180} vnet_crypto_async_alg_t;
181
182typedef enum
183{
184 VNET_CRYPTO_ASYNC_OP_NONE = 0,
185#define _(n, s, k, t, a) \
186 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
187 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC,
188 foreach_crypto_aead_async_alg
189#undef _
190#define _(c, h, s, k ,d) \
191 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
192 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC,
193 foreach_crypto_link_async_alg
194#undef _
195 VNET_CRYPTO_ASYNC_OP_N_IDS,
196} vnet_crypto_async_op_id_t;
197
Damjan Mariond1bed682019-04-24 15:20:35 +0200198typedef struct
199{
Fan Zhangf5395782020-04-29 14:00:03 +0100200 union
201 {
202 struct
203 {
204 u8 *data;
205 vnet_crypto_alg_t alg:8;
206 };
207 struct
208 {
209 u32 index_crypto;
210 u32 index_integ;
211 vnet_crypto_async_alg_t async_alg:8;
212 };
213 };
214#define VNET_CRYPTO_KEY_TYPE_DATA 0
215#define VNET_CRYPTO_KEY_TYPE_LINK 1
216 u8 type;
Damjan Mariond1bed682019-04-24 15:20:35 +0200217} vnet_crypto_key_t;
218
Damjan Marion91f17dc2019-03-18 18:59:25 +0100219typedef enum
220{
221 VNET_CRYPTO_OP_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200222#define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
Filip Tehlar06111a82021-05-03 15:29:56 +0000223 foreach_crypto_cipher_alg foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100224#undef _
225#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
Filip Tehlar06111a82021-05-03 15:29:56 +0000226 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100227#undef _
Filip Tehlar06111a82021-05-03 15:29:56 +0000228#define _(n, s) VNET_CRYPTO_OP_##n##_HASH,
229 foreach_crypto_hash_alg
230#undef _
231 VNET_CRYPTO_N_OP_IDS,
Damjan Marion060bfb92019-03-29 13:47:54 +0100232} vnet_crypto_op_id_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100233/* *INDENT-ON* */
234
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000235typedef enum
236{
237 CRYPTO_OP_SIMPLE,
238 CRYPTO_OP_CHAINED,
239 CRYPTO_OP_BOTH,
240} crypto_op_class_type_t;
241
Damjan Marion91f17dc2019-03-18 18:59:25 +0100242typedef struct
243{
244 char *name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100245 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100246} vnet_crypto_alg_data_t;
247
Damjan Marion91f17dc2019-03-18 18:59:25 +0100248typedef struct
249{
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000250 u8 *src;
251 u8 *dst;
252 u32 len;
253} vnet_crypto_op_chunk_t;
254
255typedef struct
256{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100257 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000258 uword user_data;
Damjan Marion060bfb92019-03-29 13:47:54 +0100259 vnet_crypto_op_id_t op:16;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100260 vnet_crypto_op_status_t status:8;
Damjan Marion060bfb92019-03-29 13:47:54 +0100261 u8 flags;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100262#define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
263#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000264#define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS (1 << 2)
265
266 union
267 {
268 u8 digest_len;
269 u8 tag_len;
270 };
Damjan Marion060bfb92019-03-29 13:47:54 +0100271 u16 aad_len;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000272
273 union
274 {
275 struct
276 {
277 u8 *src;
278 u8 *dst;
279 };
280
281 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
282 u16 n_chunks;
283 };
284
285 union
286 {
287 u32 len;
288 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
289 u32 chunk_index;
290 };
291
292 u32 key_index;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100293 u8 *iv;
Damjan Marion060bfb92019-03-29 13:47:54 +0100294 u8 *aad;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000295
296 union
297 {
298 u8 *tag;
299 u8 *digest;
300 };
Damjan Marion91f17dc2019-03-18 18:59:25 +0100301} vnet_crypto_op_t;
302
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000303STATIC_ASSERT_SIZEOF (vnet_crypto_op_t, CLIB_CACHE_LINE_BYTES);
304
Damjan Marion91f17dc2019-03-18 18:59:25 +0100305typedef struct
306{
Damjan Marion060bfb92019-03-29 13:47:54 +0100307 vnet_crypto_op_type_t type;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100308 vnet_crypto_alg_t alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000309 u32 active_engine_index_simple;
310 u32 active_engine_index_chained;
Damjan Marion060bfb92019-03-29 13:47:54 +0100311} vnet_crypto_op_data_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100312
313typedef struct
314{
Fan Zhangf5395782020-04-29 14:00:03 +0100315 vnet_crypto_async_op_type_t type;
316 vnet_crypto_async_alg_t alg;
317 u32 active_engine_index_async;
318} vnet_crypto_async_op_data_t;
319
320typedef struct
321{
322 char *name;
323 vnet_crypto_async_op_id_t op_by_type[VNET_CRYPTO_ASYNC_OP_N_TYPES];
324} vnet_crypto_async_alg_data_t;
325
326typedef struct
327{
Fan Zhangf5395782020-04-29 14:00:03 +0100328 u8 *iv;
329 union
330 {
331 u8 *digest;
332 u8 *tag;
333 };
334 u8 *aad;
Neale Ranns63ab8512021-02-24 09:18:53 +0000335 u32 key_index;
336 u32 crypto_total_length;
337 i16 crypto_start_offset; /* first buffer offset */
338 i16 integ_start_offset;
339 /* adj total_length for integ, e.g.4 bytes for IPSec ESN */
340 u16 integ_length_adj;
341 vnet_crypto_op_status_t status : 8;
Fan Zhangf5395782020-04-29 14:00:03 +0100342 u8 flags; /**< share same VNET_CRYPTO_OP_FLAG_* values */
343} vnet_crypto_async_frame_elt_t;
344
Neale Ranns63ab8512021-02-24 09:18:53 +0000345/* Assert the size so the compiler will warn us when it changes */
346STATIC_ASSERT_SIZEOF (vnet_crypto_async_frame_elt_t, 5 * sizeof (u64));
347
348typedef enum vnet_crypto_async_frame_state_t_
349{
350 VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED,
351 /* frame waiting to be processed */
352 VNET_CRYPTO_FRAME_STATE_PENDING,
353 VNET_CRYPTO_FRAME_STATE_WORK_IN_PROGRESS,
354 VNET_CRYPTO_FRAME_STATE_SUCCESS,
355 VNET_CRYPTO_FRAME_STATE_ELT_ERROR
356} __clib_packed vnet_crypto_async_frame_state_t;
357
Fan Zhangf5395782020-04-29 14:00:03 +0100358typedef struct
359{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100360 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns63ab8512021-02-24 09:18:53 +0000361 vnet_crypto_async_frame_state_t state;
Fan Zhangf5395782020-04-29 14:00:03 +0100362 vnet_crypto_async_op_id_t op:8;
363 u16 n_elts;
364 vnet_crypto_async_frame_elt_t elts[VNET_CRYPTO_FRAME_SIZE];
365 u32 buffer_indices[VNET_CRYPTO_FRAME_SIZE];
366 u16 next_node_index[VNET_CRYPTO_FRAME_SIZE];
PiotrX Kleski22848172020-07-08 14:36:34 +0200367 u32 enqueue_thread_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100368} vnet_crypto_async_frame_t;
369
370typedef struct
371{
372 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Fan Zhangf5395782020-04-29 14:00:03 +0100373 vnet_crypto_async_frame_t *frame_pool;
Neale Ranns63ab8512021-02-24 09:18:53 +0000374 u32 *buffer_indices;
Fan Zhangf5395782020-04-29 14:00:03 +0100375 u16 *nexts;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100376} vnet_crypto_thread_t;
377
Damjan Mariond1bed682019-04-24 15:20:35 +0200378typedef u32 vnet_crypto_key_index_t;
379
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000380typedef u32 (vnet_crypto_chained_ops_handler_t) (vlib_main_t * vm,
381 vnet_crypto_op_t * ops[],
382 vnet_crypto_op_chunk_t *
383 chunks, u32 n_ops);
384
Damjan Marion91f17dc2019-03-18 18:59:25 +0100385typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
386 vnet_crypto_op_t * ops[], u32 n_ops);
387
Damjan Mariond1bed682019-04-24 15:20:35 +0200388typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
389 vnet_crypto_key_op_t kop,
390 vnet_crypto_key_index_t idx);
391
Fan Zhangf5395782020-04-29 14:00:03 +0100392/** async crypto function handlers **/
PiotrX Kleski22848172020-07-08 14:36:34 +0200393typedef int
394 (vnet_crypto_frame_enqueue_t) (vlib_main_t * vm,
395 vnet_crypto_async_frame_t * frame);
Fan Zhangf5395782020-04-29 14:00:03 +0100396typedef vnet_crypto_async_frame_t *
PiotrX Kleski22848172020-07-08 14:36:34 +0200397 (vnet_crypto_frame_dequeue_t) (vlib_main_t * vm, u32 * nb_elts_processed,
398 u32 * enqueue_thread_idx);
Fan Zhangf5395782020-04-29 14:00:03 +0100399
PiotrX Kleski22848172020-07-08 14:36:34 +0200400u32
401vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
402 char *desc);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100403
Damjan Mariond1bed682019-04-24 15:20:35 +0200404void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
405 vnet_crypto_op_id_t opt,
406 vnet_crypto_ops_handler_t * oph);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000407
408void vnet_crypto_register_chained_ops_handler (vlib_main_t * vm,
409 u32 engine_index,
410 vnet_crypto_op_id_t opt,
411 vnet_crypto_chained_ops_handler_t
412 * oph);
Fan Zhangf5395782020-04-29 14:00:03 +0100413
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000414void vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
415 vnet_crypto_op_id_t opt,
416 vnet_crypto_ops_handler_t * fn,
417 vnet_crypto_chained_ops_handler_t *
418 cfn);
419
Damjan Mariond1bed682019-04-24 15:20:35 +0200420void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
421 vnet_crypto_key_handler_t * keyh);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100422
Fan Zhangf5395782020-04-29 14:00:03 +0100423/** async crypto register functions */
424u32 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name);
425void vnet_crypto_register_async_handler (vlib_main_t * vm,
426 u32 engine_index,
427 vnet_crypto_async_op_id_t opt,
428 vnet_crypto_frame_enqueue_t * enq_fn,
429 vnet_crypto_frame_dequeue_t *
430 deq_fn);
431
Damjan Marion91f17dc2019-03-18 18:59:25 +0100432typedef struct
433{
434 char *name;
435 char *desc;
436 int priority;
Damjan Mariond1bed682019-04-24 15:20:35 +0200437 vnet_crypto_key_handler_t *key_op_handler;
Damjan Marion060bfb92019-03-29 13:47:54 +0100438 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000439 vnet_crypto_chained_ops_handler_t
440 * chained_ops_handlers[VNET_CRYPTO_N_OP_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100441 vnet_crypto_frame_enqueue_t *enqueue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
442 vnet_crypto_frame_dequeue_t *dequeue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100443} vnet_crypto_engine_t;
444
445typedef struct
446{
Fan Zhangf5395782020-04-29 14:00:03 +0100447 u32 node_idx;
448 u32 next_idx;
449} vnet_crypto_async_next_node_t;
450
451typedef struct
452{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100453 vnet_crypto_alg_data_t *algs;
454 vnet_crypto_thread_t *threads;
455 vnet_crypto_ops_handler_t **ops_handlers;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000456 vnet_crypto_chained_ops_handler_t **chained_ops_handlers;
Fan Zhangf5395782020-04-29 14:00:03 +0100457 vnet_crypto_frame_enqueue_t **enqueue_handlers;
458 vnet_crypto_frame_dequeue_t **dequeue_handlers;
459 clib_bitmap_t *async_active_ids;
Damjan Marion060bfb92019-03-29 13:47:54 +0100460 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100461 vnet_crypto_async_op_data_t async_opt_data[VNET_CRYPTO_ASYNC_OP_N_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100462 vnet_crypto_engine_t *engines;
Damjan Mariond1bed682019-04-24 15:20:35 +0200463 vnet_crypto_key_t *keys;
Filip Tehlar1469d542019-03-25 09:04:41 -0700464 uword *engine_index_by_name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100465 uword *alg_index_by_name;
Fan Zhangf5395782020-04-29 14:00:03 +0100466 uword *async_alg_index_by_name;
467 vnet_crypto_async_alg_data_t *async_algs;
468 u32 async_refcnt;
469 vnet_crypto_async_next_node_t *next_nodes;
PiotrX Kleski22848172020-07-08 14:36:34 +0200470 u32 crypto_node_index;
471#define VNET_CRYPTO_ASYNC_DISPATCH_POLLING 0
472#define VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT 1
473 u8 dispatch_mode;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100474} vnet_crypto_main_t;
475
476extern vnet_crypto_main_t crypto_main;
477
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000478u32 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
479 vnet_crypto_op_chunk_t * chunks,
480 u32 n_ops);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100481u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
482 u32 n_ops);
483
Fan Zhangf5395782020-04-29 14:00:03 +0100484
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000485int vnet_crypto_set_handler2 (char *ops_handler_name, char *engine,
486 crypto_op_class_type_t oct);
Neale Rannsece2ae02019-06-21 12:44:11 +0000487int vnet_crypto_is_set_handler (vnet_crypto_alg_t alg);
Filip Tehlar1469d542019-03-25 09:04:41 -0700488
Damjan Mariond1bed682019-04-24 15:20:35 +0200489u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
490 u8 * data, u16 length);
491void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
Damjan Mariond1bed682019-04-24 15:20:35 +0200492
Fan Zhangf5395782020-04-29 14:00:03 +0100493/**
494 * Use 2 created keys to generate new key for linked algs (cipher + integ)
495 * The returned key index is to be used for linked alg only.
496 **/
497u32 vnet_crypto_key_add_linked (vlib_main_t * vm,
498 vnet_crypto_key_index_t index_crypto,
499 vnet_crypto_key_index_t index_integ);
500
501clib_error_t *crypto_dispatch_enable_disable (int is_enable);
502
503int vnet_crypto_set_async_handler2 (char *alg_name, char *engine);
504
505int vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t opt);
506
507void vnet_crypto_request_async_mode (int is_enable);
508
PiotrX Kleski22848172020-07-08 14:36:34 +0200509void vnet_crypto_set_async_dispatch_mode (u8 mode);
510
Fan Zhangf5395782020-04-29 14:00:03 +0100511vnet_crypto_async_alg_t vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
512 vnet_crypto_alg_t integ_alg);
513
514clib_error_t *crypto_dispatch_enable_disable (int is_enable);
515
Damjan Marion91f17dc2019-03-18 18:59:25 +0100516format_function_t format_vnet_crypto_alg;
517format_function_t format_vnet_crypto_engine;
518format_function_t format_vnet_crypto_op;
Damjan Marion060bfb92019-03-29 13:47:54 +0100519format_function_t format_vnet_crypto_op_type;
520format_function_t format_vnet_crypto_op_status;
Damjan Marion61c0a3d2019-04-05 19:42:21 +0200521unformat_function_t unformat_vnet_crypto_alg;
Damjan Mariona03d1822019-03-28 21:40:48 +0100522
Fan Zhangf5395782020-04-29 14:00:03 +0100523format_function_t format_vnet_crypto_async_op;
524format_function_t format_vnet_crypto_async_alg;
525format_function_t format_vnet_crypto_async_op_type;
526
Damjan Mariona03d1822019-03-28 21:40:48 +0100527static_always_inline void
Damjan Marion060bfb92019-03-29 13:47:54 +0100528vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
Damjan Mariona03d1822019-03-28 21:40:48 +0100529{
530 if (CLIB_DEBUG > 0)
531 clib_memset (op, 0xfe, sizeof (*op));
532 op->op = type;
533 op->flags = 0;
Damjan Mariond1bed682019-04-24 15:20:35 +0200534 op->key_index = ~0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000535 op->n_chunks = 0;
Damjan Mariona03d1822019-03-28 21:40:48 +0100536}
537
Damjan Marion060bfb92019-03-29 13:47:54 +0100538static_always_inline vnet_crypto_op_type_t
539vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
540{
541 vnet_crypto_main_t *cm = &crypto_main;
Lijian Zhangb15d7962019-09-27 16:25:35 +0800542 ASSERT (id < VNET_CRYPTO_N_OP_IDS);
543 vnet_crypto_op_data_t *od = cm->opt_data + id;
Damjan Marion060bfb92019-03-29 13:47:54 +0100544 return od->type;
545}
546
Damjan Mariond1bed682019-04-24 15:20:35 +0200547static_always_inline vnet_crypto_key_t *
548vnet_crypto_get_key (vnet_crypto_key_index_t index)
549{
550 vnet_crypto_main_t *cm = &crypto_main;
551 return vec_elt_at_index (cm->keys, index);
552}
553
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000554static_always_inline int
555vnet_crypto_set_handler (char *alg_name, char *engine)
556{
557 return vnet_crypto_set_handler2 (alg_name, engine, CRYPTO_OP_BOTH);
558}
559
Fan Zhangf5395782020-04-29 14:00:03 +0100560/** async crypto inline functions **/
561
562static_always_inline vnet_crypto_async_frame_t *
563vnet_crypto_async_get_frame (vlib_main_t * vm, vnet_crypto_async_op_id_t opt)
564{
565 vnet_crypto_main_t *cm = &crypto_main;
566 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
Neale Rannsfc811342021-02-26 10:35:33 +0000567 vnet_crypto_async_frame_t *f = NULL;
Fan Zhangf5395782020-04-29 14:00:03 +0100568
Neale Rannsfc811342021-02-26 10:35:33 +0000569 pool_get_aligned (ct->frame_pool, f, CLIB_CACHE_LINE_BYTES);
570 if (CLIB_DEBUG > 0)
571 clib_memset (f, 0xfe, sizeof (*f));
572 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
573 f->op = opt;
574 f->n_elts = 0;
575
Fan Zhangf5395782020-04-29 14:00:03 +0100576 return f;
577}
578
579static_always_inline void
580vnet_crypto_async_free_frame (vlib_main_t * vm,
581 vnet_crypto_async_frame_t * frame)
582{
583 vnet_crypto_main_t *cm = &crypto_main;
584 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
585 pool_put (ct->frame_pool, frame);
586}
587
588static_always_inline int
589vnet_crypto_async_submit_open_frame (vlib_main_t * vm,
590 vnet_crypto_async_frame_t * frame)
591{
592 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleski22848172020-07-08 14:36:34 +0200593 vlib_thread_main_t *tm = vlib_get_thread_main ();
Fan Zhangf5395782020-04-29 14:00:03 +0100594 vnet_crypto_async_op_id_t opt = frame->op;
PiotrX Kleski22848172020-07-08 14:36:34 +0200595 u32 i = vlib_num_workers () > 0;
596
PiotrX Kleskief69b512020-11-24 08:26:26 +0000597 frame->state = VNET_CRYPTO_FRAME_STATE_PENDING;
PiotrX Kleski22848172020-07-08 14:36:34 +0200598 frame->enqueue_thread_index = vm->thread_index;
PiotrX Kleskief69b512020-11-24 08:26:26 +0000599
600 int ret = (cm->enqueue_handlers[frame->op]) (vm, frame);
601
Fan Zhangf5395782020-04-29 14:00:03 +0100602 clib_bitmap_set_no_check (cm->async_active_ids, opt, 1);
603 if (PREDICT_TRUE (ret == 0))
604 {
Neale Rannsfc811342021-02-26 10:35:33 +0000605 if (cm->dispatch_mode == VNET_CRYPTO_ASYNC_DISPATCH_INTERRUPT)
606 {
607 for (; i < tm->n_vlib_mains; i++)
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100608 vlib_node_set_interrupt_pending (vlib_get_main_by_index (i),
Neale Rannsfc811342021-02-26 10:35:33 +0000609 cm->crypto_node_index);
610 }
Fan Zhangf5395782020-04-29 14:00:03 +0100611 }
PiotrX Kleskief69b512020-11-24 08:26:26 +0000612 else
613 {
614 frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
615 }
PiotrX Kleski22848172020-07-08 14:36:34 +0200616
Fan Zhangf5395782020-04-29 14:00:03 +0100617 return ret;
618}
619
Neale Rannsfc811342021-02-26 10:35:33 +0000620static_always_inline void
621vnet_crypto_async_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
622 u32 key_index, u32 crypto_len,
623 i16 integ_len_adj, i16 crypto_start_offset,
624 u16 integ_start_offset, u32 buffer_index,
625 u16 next_node, u8 *iv, u8 *tag, u8 *aad,
626 u8 flags)
Fan Zhangf5395782020-04-29 14:00:03 +0100627{
Fan Zhangf5395782020-04-29 14:00:03 +0100628 vnet_crypto_async_frame_elt_t *fe;
629 u16 index;
630
Neale Rannsfc811342021-02-26 10:35:33 +0000631 ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
Fan Zhangf5395782020-04-29 14:00:03 +0100632
633 index = f->n_elts;
634 fe = &f->elts[index];
635 f->n_elts++;
636 fe->key_index = key_index;
637 fe->crypto_total_length = crypto_len;
638 fe->crypto_start_offset = crypto_start_offset;
639 fe->integ_start_offset = integ_start_offset;
640 fe->integ_length_adj = integ_len_adj;
641 fe->iv = iv;
642 fe->tag = tag;
643 fe->aad = aad;
644 fe->flags = flags;
645 f->buffer_indices[index] = buffer_index;
646 f->next_node_index[index] = next_node;
Fan Zhangf5395782020-04-29 14:00:03 +0100647}
648
649static_always_inline void
650vnet_crypto_async_reset_frame (vnet_crypto_async_frame_t * f)
651{
652 vnet_crypto_async_op_id_t opt;
653 ASSERT (f != 0);
PiotrX Kleski8ed06c02020-11-24 07:34:09 +0000654 ASSERT ((f->state == VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED
655 || f->state == VNET_CRYPTO_FRAME_STATE_ELT_ERROR));
Fan Zhangf5395782020-04-29 14:00:03 +0100656 opt = f->op;
657 if (CLIB_DEBUG > 0)
658 clib_memset (f, 0xfe, sizeof (*f));
659 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
660 f->op = opt;
661 f->n_elts = 0;
662}
663
Neale Rannsfc811342021-02-26 10:35:33 +0000664static_always_inline u8
665vnet_crypto_async_frame_is_full (const vnet_crypto_async_frame_t *f)
666{
667 return (f->n_elts == VNET_CRYPTO_FRAME_SIZE);
668}
669
Damjan Marion91f17dc2019-03-18 18:59:25 +0100670#endif /* included_vnet_crypto_crypto_h */
671
672/*
673 * fd.io coding-style-patch-verification: ON
674 *
675 * Local Variables:
676 * eval: (c-set-style "gnu")
677 * End:
678 */