blob: a34d3c668d88c8f1a06d47ea95ee9191f2e76dc1 [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 */
Benoît Ganne84e66582023-03-10 17:33:03 +010036#define foreach_crypto_aead_alg \
37 _ (AES_128_GCM, "aes-128-gcm", 16) \
38 _ (AES_192_GCM, "aes-192-gcm", 24) \
39 _ (AES_256_GCM, "aes-256-gcm", 32) \
40 _ (AES_128_NULL_GMAC, "aes-128-null-gmac", 16) \
41 _ (AES_192_NULL_GMAC, "aes-192-null-gmac", 24) \
42 _ (AES_256_NULL_GMAC, "aes-256-null-gmac", 32) \
43 _ (CHACHA20_POLY1305, "chacha20-poly1305", 32)
Damjan Marion060bfb92019-03-29 13:47:54 +010044
Filip Tehlar06111a82021-05-03 15:29:56 +000045#define foreach_crypto_hash_alg \
46 _ (SHA1, "sha-1") \
47 _ (SHA224, "sha-224") \
48 _ (SHA256, "sha-256") \
49 _ (SHA384, "sha-384") \
50 _ (SHA512, "sha-512")
51
Damjan Marion060bfb92019-03-29 13:47:54 +010052#define foreach_crypto_hmac_alg \
Filip Tehlare225f712019-03-19 10:37:06 -070053 _(MD5, "md5") \
Damjan Marion91f17dc2019-03-18 18:59:25 +010054 _(SHA1, "sha-1") \
55 _(SHA224, "sha-224") \
56 _(SHA256, "sha-256") \
57 _(SHA384, "sha-384") \
58 _(SHA512, "sha-512")
59
Filip Tehlar06111a82021-05-03 15:29:56 +000060#define foreach_crypto_op_type \
61 _ (ENCRYPT, "encrypt") \
62 _ (DECRYPT, "decrypt") \
63 _ (AEAD_ENCRYPT, "aead-encrypt") \
64 _ (AEAD_DECRYPT, "aead-decrypt") \
65 _ (HMAC, "hmac") \
66 _ (HASH, "hash")
Damjan Marion060bfb92019-03-29 13:47:54 +010067
68typedef enum
69{
70#define _(n, s) VNET_CRYPTO_OP_TYPE_##n,
71 foreach_crypto_op_type
72#undef _
73 VNET_CRYPTO_OP_N_TYPES,
74} vnet_crypto_op_type_t;
75
76#define foreach_crypto_op_status \
Fan Zhangf5395782020-04-29 14:00:03 +010077 _(IDLE, "idle") \
Damjan Marion060bfb92019-03-29 13:47:54 +010078 _(PENDING, "pending") \
Fan Zhangf5395782020-04-29 14:00:03 +010079 _(WORK_IN_PROGRESS, "work-in-progress") \
Damjan Marion060bfb92019-03-29 13:47:54 +010080 _(COMPLETED, "completed") \
81 _(FAIL_NO_HANDLER, "no-handler") \
Fan Zhangf5395782020-04-29 14:00:03 +010082 _(FAIL_BAD_HMAC, "bad-hmac") \
83 _(FAIL_ENGINE_ERR, "engine-error")
84
85/** async crypto **/
86
87/* CRYPTO_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, TAG_LEN, AAD_LEN */
Gabriel Oginski492d7792021-11-10 07:59:56 +000088#define foreach_crypto_aead_async_alg \
89 _ (AES_128_GCM, "aes-128-gcm-aad8", 16, 16, 8) \
90 _ (AES_128_GCM, "aes-128-gcm-aad12", 16, 16, 12) \
91 _ (AES_192_GCM, "aes-192-gcm-aad8", 24, 16, 8) \
92 _ (AES_192_GCM, "aes-192-gcm-aad12", 24, 16, 12) \
93 _ (AES_256_GCM, "aes-256-gcm-aad8", 32, 16, 8) \
94 _ (AES_256_GCM, "aes-256-gcm-aad12", 32, 16, 12) \
Benoît Ganne84e66582023-03-10 17:33:03 +010095 _ (AES_128_NULL_GMAC, "aes-128-null-gmac-aad8", 16, 16, 8) \
96 _ (AES_128_NULL_GMAC, "aes-128-null-gmac-aad12", 16, 16, 12) \
97 _ (AES_192_NULL_GMAC, "aes-192-null-gmac-aad8", 24, 16, 8) \
98 _ (AES_192_NULL_GMAC, "aes-192-null-gmac-aad12", 24, 16, 12) \
99 _ (AES_256_NULL_GMAC, "aes-256-null-gmac-aad8", 32, 16, 8) \
100 _ (AES_256_NULL_GMAC, "aes-256-null-gmac-aad12", 32, 16, 12) \
Gabriel Oginski492d7792021-11-10 07:59:56 +0000101 _ (CHACHA20_POLY1305, "chacha20-poly1305-aad8", 32, 16, 8) \
102 _ (CHACHA20_POLY1305, "chacha20-poly1305-aad12", 32, 16, 12) \
103 _ (CHACHA20_POLY1305, "chacha20-poly1305", 32, 16, 0)
Fan Zhangf5395782020-04-29 14:00:03 +0100104
105/* CRYPTO_ID, INTEG_ID, PRETTY_NAME, KEY_LENGTH_IN_BYTES, DIGEST_LEN */
Benoît Ganne40ee2002021-01-22 18:09:40 +0100106#define foreach_crypto_link_async_alg \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500107 _ (3DES_CBC, MD5, "3des-cbc-hmac-md5", 24, 12) \
108 _ (AES_128_CBC, MD5, "aes-128-cbc-hmac-md5", 16, 12) \
109 _ (AES_192_CBC, MD5, "aes-192-cbc-hmac-md5", 24, 12) \
110 _ (AES_256_CBC, MD5, "aes-256-cbc-hmac-md5", 32, 12) \
111 _ (3DES_CBC, SHA1, "3des-cbc-hmac-sha-1", 24, 12) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100112 _ (AES_128_CBC, SHA1, "aes-128-cbc-hmac-sha-1", 16, 12) \
113 _ (AES_192_CBC, SHA1, "aes-192-cbc-hmac-sha-1", 24, 12) \
114 _ (AES_256_CBC, SHA1, "aes-256-cbc-hmac-sha-1", 32, 12) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500115 _ (3DES_CBC, SHA224, "3des-cbc-hmac-sha-224", 24, 14) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100116 _ (AES_128_CBC, SHA224, "aes-128-cbc-hmac-sha-224", 16, 14) \
117 _ (AES_192_CBC, SHA224, "aes-192-cbc-hmac-sha-224", 24, 14) \
118 _ (AES_256_CBC, SHA224, "aes-256-cbc-hmac-sha-224", 32, 14) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500119 _ (3DES_CBC, SHA256, "3des-cbc-hmac-sha-256", 24, 16) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100120 _ (AES_128_CBC, SHA256, "aes-128-cbc-hmac-sha-256", 16, 16) \
121 _ (AES_192_CBC, SHA256, "aes-192-cbc-hmac-sha-256", 24, 16) \
122 _ (AES_256_CBC, SHA256, "aes-256-cbc-hmac-sha-256", 32, 16) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500123 _ (3DES_CBC, SHA384, "3des-cbc-hmac-sha-384", 24, 24) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100124 _ (AES_128_CBC, SHA384, "aes-128-cbc-hmac-sha-384", 16, 24) \
125 _ (AES_192_CBC, SHA384, "aes-192-cbc-hmac-sha-384", 24, 24) \
126 _ (AES_256_CBC, SHA384, "aes-256-cbc-hmac-sha-384", 32, 24) \
Alexander Chernavin005d1e42021-02-01 05:17:24 -0500127 _ (3DES_CBC, SHA512, "3des-cbc-hmac-sha-512", 24, 32) \
Benoît Ganne40ee2002021-01-22 18:09:40 +0100128 _ (AES_128_CBC, SHA512, "aes-128-cbc-hmac-sha-512", 16, 32) \
129 _ (AES_192_CBC, SHA512, "aes-192-cbc-hmac-sha-512", 24, 32) \
130 _ (AES_256_CBC, SHA512, "aes-256-cbc-hmac-sha-512", 32, 32) \
131 _ (AES_128_CTR, SHA1, "aes-128-ctr-hmac-sha-1", 16, 12) \
132 _ (AES_192_CTR, SHA1, "aes-192-ctr-hmac-sha-1", 24, 12) \
133 _ (AES_256_CTR, SHA1, "aes-256-ctr-hmac-sha-1", 32, 12)
Fan Zhangf5395782020-04-29 14:00:03 +0100134
135#define foreach_crypto_async_op_type \
136 _(ENCRYPT, "async-encrypt") \
137 _(DECRYPT, "async-decrypt")
Damjan Marion060bfb92019-03-29 13:47:54 +0100138
139typedef enum
140{
Damjan Mariond1bed682019-04-24 15:20:35 +0200141 VNET_CRYPTO_KEY_OP_ADD,
142 VNET_CRYPTO_KEY_OP_DEL,
143 VNET_CRYPTO_KEY_OP_MODIFY,
144} vnet_crypto_key_op_t;
145
146typedef enum
147{
Damjan Marion060bfb92019-03-29 13:47:54 +0100148#define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
149 foreach_crypto_op_status
150#undef _
151 VNET_CRYPTO_OP_N_STATUS,
152} vnet_crypto_op_status_t;
153
Damjan Marion91f17dc2019-03-18 18:59:25 +0100154/* *INDENT-OFF* */
155typedef enum
156{
Damjan Mariond1bed682019-04-24 15:20:35 +0200157 VNET_CRYPTO_ALG_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200158#define _(n, s, l) VNET_CRYPTO_ALG_##n,
Filip Tehlar06111a82021-05-03 15:29:56 +0000159 foreach_crypto_cipher_alg foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100160#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100161#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
Filip Tehlar06111a82021-05-03 15:29:56 +0000162 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100163#undef _
Filip Tehlar06111a82021-05-03 15:29:56 +0000164#define _(n, s) VNET_CRYPTO_ALG_HASH_##n,
165 foreach_crypto_hash_alg
166#undef _
167 VNET_CRYPTO_N_ALGS,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100168} vnet_crypto_alg_t;
169
Fan Zhangf5395782020-04-29 14:00:03 +0100170typedef enum
171{
172#define _(n, s) VNET_CRYPTO_ASYNC_OP_TYPE_##n,
173 foreach_crypto_async_op_type
174#undef _
175 VNET_CRYPTO_ASYNC_OP_N_TYPES,
176} vnet_crypto_async_op_type_t;
177
178typedef enum
179{
180 VNET_CRYPTO_ASYNC_ALG_NONE = 0,
181#define _(n, s, k, t, a) \
182 VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a,
183 foreach_crypto_aead_async_alg
184#undef _
185#define _(c, h, s, k ,d) \
186 VNET_CRYPTO_ALG_##c##_##h##_TAG##d,
187 foreach_crypto_link_async_alg
188#undef _
189 VNET_CRYPTO_N_ASYNC_ALGS,
190} vnet_crypto_async_alg_t;
191
192typedef enum
193{
194 VNET_CRYPTO_ASYNC_OP_NONE = 0,
195#define _(n, s, k, t, a) \
196 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
197 VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC,
198 foreach_crypto_aead_async_alg
199#undef _
200#define _(c, h, s, k ,d) \
201 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
202 VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC,
203 foreach_crypto_link_async_alg
204#undef _
205 VNET_CRYPTO_ASYNC_OP_N_IDS,
206} vnet_crypto_async_op_id_t;
207
Damjan Mariond1bed682019-04-24 15:20:35 +0200208typedef struct
209{
Fan Zhangf5395782020-04-29 14:00:03 +0100210 union
211 {
212 struct
213 {
214 u8 *data;
215 vnet_crypto_alg_t alg:8;
216 };
217 struct
218 {
219 u32 index_crypto;
220 u32 index_integ;
221 vnet_crypto_async_alg_t async_alg:8;
222 };
223 };
224#define VNET_CRYPTO_KEY_TYPE_DATA 0
225#define VNET_CRYPTO_KEY_TYPE_LINK 1
226 u8 type;
Damjan Mariond1bed682019-04-24 15:20:35 +0200227} vnet_crypto_key_t;
228
Damjan Marion91f17dc2019-03-18 18:59:25 +0100229typedef enum
230{
231 VNET_CRYPTO_OP_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200232#define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
Filip Tehlar06111a82021-05-03 15:29:56 +0000233 foreach_crypto_cipher_alg foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100234#undef _
235#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
Filip Tehlar06111a82021-05-03 15:29:56 +0000236 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100237#undef _
Filip Tehlar06111a82021-05-03 15:29:56 +0000238#define _(n, s) VNET_CRYPTO_OP_##n##_HASH,
239 foreach_crypto_hash_alg
240#undef _
241 VNET_CRYPTO_N_OP_IDS,
Damjan Marion060bfb92019-03-29 13:47:54 +0100242} vnet_crypto_op_id_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100243/* *INDENT-ON* */
244
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000245typedef enum
246{
247 CRYPTO_OP_SIMPLE,
248 CRYPTO_OP_CHAINED,
249 CRYPTO_OP_BOTH,
250} crypto_op_class_type_t;
251
Damjan Marion91f17dc2019-03-18 18:59:25 +0100252typedef struct
253{
254 char *name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100255 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100256} vnet_crypto_alg_data_t;
257
Damjan Marion91f17dc2019-03-18 18:59:25 +0100258typedef struct
259{
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000260 u8 *src;
261 u8 *dst;
262 u32 len;
263} vnet_crypto_op_chunk_t;
264
265typedef struct
266{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100267 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000268 uword user_data;
Damjan Marion060bfb92019-03-29 13:47:54 +0100269 vnet_crypto_op_id_t op:16;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100270 vnet_crypto_op_status_t status:8;
Damjan Marion060bfb92019-03-29 13:47:54 +0100271 u8 flags;
Benoît Ganne063549f2022-01-19 10:09:42 +0100272#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 0)
273#define VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS (1 << 1)
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000274
275 union
276 {
277 u8 digest_len;
278 u8 tag_len;
279 };
Damjan Marion060bfb92019-03-29 13:47:54 +0100280 u16 aad_len;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000281
282 union
283 {
284 struct
285 {
286 u8 *src;
287 u8 *dst;
288 };
289
290 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
291 u16 n_chunks;
292 };
293
294 union
295 {
296 u32 len;
297 /* valid if VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS is set */
298 u32 chunk_index;
299 };
300
301 u32 key_index;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100302 u8 *iv;
Damjan Marion060bfb92019-03-29 13:47:54 +0100303 u8 *aad;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000304
305 union
306 {
307 u8 *tag;
308 u8 *digest;
309 };
Damjan Marion91f17dc2019-03-18 18:59:25 +0100310} vnet_crypto_op_t;
311
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000312STATIC_ASSERT_SIZEOF (vnet_crypto_op_t, CLIB_CACHE_LINE_BYTES);
313
Damjan Marion91f17dc2019-03-18 18:59:25 +0100314typedef struct
315{
Damjan Marion060bfb92019-03-29 13:47:54 +0100316 vnet_crypto_op_type_t type;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100317 vnet_crypto_alg_t alg;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000318 u32 active_engine_index_simple;
319 u32 active_engine_index_chained;
Damjan Marion060bfb92019-03-29 13:47:54 +0100320} vnet_crypto_op_data_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100321
322typedef struct
323{
Fan Zhangf5395782020-04-29 14:00:03 +0100324 vnet_crypto_async_op_type_t type;
325 vnet_crypto_async_alg_t alg;
326 u32 active_engine_index_async;
327} vnet_crypto_async_op_data_t;
328
329typedef struct
330{
331 char *name;
332 vnet_crypto_async_op_id_t op_by_type[VNET_CRYPTO_ASYNC_OP_N_TYPES];
333} vnet_crypto_async_alg_data_t;
334
335typedef struct
336{
Fan Zhangf5395782020-04-29 14:00:03 +0100337 u8 *iv;
338 union
339 {
340 u8 *digest;
341 u8 *tag;
342 };
343 u8 *aad;
Neale Ranns63ab8512021-02-24 09:18:53 +0000344 u32 key_index;
345 u32 crypto_total_length;
346 i16 crypto_start_offset; /* first buffer offset */
347 i16 integ_start_offset;
348 /* adj total_length for integ, e.g.4 bytes for IPSec ESN */
Benoît Ganne02dfd292022-01-18 15:56:41 +0100349 i16 integ_length_adj;
Neale Ranns63ab8512021-02-24 09:18:53 +0000350 vnet_crypto_op_status_t status : 8;
Fan Zhangf5395782020-04-29 14:00:03 +0100351 u8 flags; /**< share same VNET_CRYPTO_OP_FLAG_* values */
352} vnet_crypto_async_frame_elt_t;
353
Neale Ranns63ab8512021-02-24 09:18:53 +0000354/* Assert the size so the compiler will warn us when it changes */
355STATIC_ASSERT_SIZEOF (vnet_crypto_async_frame_elt_t, 5 * sizeof (u64));
356
357typedef enum vnet_crypto_async_frame_state_t_
358{
359 VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED,
360 /* frame waiting to be processed */
361 VNET_CRYPTO_FRAME_STATE_PENDING,
362 VNET_CRYPTO_FRAME_STATE_WORK_IN_PROGRESS,
363 VNET_CRYPTO_FRAME_STATE_SUCCESS,
364 VNET_CRYPTO_FRAME_STATE_ELT_ERROR
365} __clib_packed vnet_crypto_async_frame_state_t;
366
Fan Zhangf5395782020-04-29 14:00:03 +0100367typedef struct
368{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100369 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Neale Ranns63ab8512021-02-24 09:18:53 +0000370 vnet_crypto_async_frame_state_t state;
Fan Zhangf5395782020-04-29 14:00:03 +0100371 vnet_crypto_async_op_id_t op:8;
372 u16 n_elts;
373 vnet_crypto_async_frame_elt_t elts[VNET_CRYPTO_FRAME_SIZE];
374 u32 buffer_indices[VNET_CRYPTO_FRAME_SIZE];
375 u16 next_node_index[VNET_CRYPTO_FRAME_SIZE];
PiotrX Kleski22848172020-07-08 14:36:34 +0200376 u32 enqueue_thread_index;
Fan Zhangf5395782020-04-29 14:00:03 +0100377} vnet_crypto_async_frame_t;
378
379typedef struct
380{
381 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Fan Zhangf5395782020-04-29 14:00:03 +0100382 vnet_crypto_async_frame_t *frame_pool;
Neale Ranns63ab8512021-02-24 09:18:53 +0000383 u32 *buffer_indices;
Fan Zhangf5395782020-04-29 14:00:03 +0100384 u16 *nexts;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100385} vnet_crypto_thread_t;
386
Damjan Mariond1bed682019-04-24 15:20:35 +0200387typedef u32 vnet_crypto_key_index_t;
388
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000389typedef u32 (vnet_crypto_chained_ops_handler_t) (vlib_main_t * vm,
390 vnet_crypto_op_t * ops[],
391 vnet_crypto_op_chunk_t *
392 chunks, u32 n_ops);
393
Damjan Marion91f17dc2019-03-18 18:59:25 +0100394typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
395 vnet_crypto_op_t * ops[], u32 n_ops);
396
Damjan Mariond1bed682019-04-24 15:20:35 +0200397typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
398 vnet_crypto_key_op_t kop,
399 vnet_crypto_key_index_t idx);
400
Fan Zhangf5395782020-04-29 14:00:03 +0100401/** async crypto function handlers **/
PiotrX Kleski22848172020-07-08 14:36:34 +0200402typedef int
403 (vnet_crypto_frame_enqueue_t) (vlib_main_t * vm,
404 vnet_crypto_async_frame_t * frame);
Fan Zhangf5395782020-04-29 14:00:03 +0100405typedef vnet_crypto_async_frame_t *
PiotrX Kleski22848172020-07-08 14:36:34 +0200406 (vnet_crypto_frame_dequeue_t) (vlib_main_t * vm, u32 * nb_elts_processed,
407 u32 * enqueue_thread_idx);
Fan Zhangf5395782020-04-29 14:00:03 +0100408
PiotrX Kleski22848172020-07-08 14:36:34 +0200409u32
410vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
411 char *desc);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100412
Damjan Mariond1bed682019-04-24 15:20:35 +0200413void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
414 vnet_crypto_op_id_t opt,
415 vnet_crypto_ops_handler_t * oph);
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000416
417void vnet_crypto_register_chained_ops_handler (vlib_main_t * vm,
418 u32 engine_index,
419 vnet_crypto_op_id_t opt,
420 vnet_crypto_chained_ops_handler_t
421 * oph);
Fan Zhangf5395782020-04-29 14:00:03 +0100422
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000423void vnet_crypto_register_ops_handlers (vlib_main_t * vm, u32 engine_index,
424 vnet_crypto_op_id_t opt,
425 vnet_crypto_ops_handler_t * fn,
426 vnet_crypto_chained_ops_handler_t *
427 cfn);
428
Damjan Mariond1bed682019-04-24 15:20:35 +0200429void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
430 vnet_crypto_key_handler_t * keyh);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100431
Fan Zhangf5395782020-04-29 14:00:03 +0100432/** async crypto register functions */
433u32 vnet_crypto_register_post_node (vlib_main_t * vm, char *post_node_name);
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000434
435void
436vnet_crypto_register_enqueue_handler (vlib_main_t *vm, u32 engine_index,
437 vnet_crypto_async_op_id_t opt,
438 vnet_crypto_frame_enqueue_t *enq_fn);
439
440void
441vnet_crypto_register_dequeue_handler (vlib_main_t *vm, u32 engine_index,
442 vnet_crypto_frame_dequeue_t *deq_fn);
Fan Zhangf5395782020-04-29 14:00:03 +0100443
Damjan Marion91f17dc2019-03-18 18:59:25 +0100444typedef struct
445{
446 char *name;
447 char *desc;
448 int priority;
Damjan Mariond1bed682019-04-24 15:20:35 +0200449 vnet_crypto_key_handler_t *key_op_handler;
Damjan Marion060bfb92019-03-29 13:47:54 +0100450 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000451 vnet_crypto_chained_ops_handler_t
452 * chained_ops_handlers[VNET_CRYPTO_N_OP_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100453 vnet_crypto_frame_enqueue_t *enqueue_handlers[VNET_CRYPTO_ASYNC_OP_N_IDS];
Jakub Wysocki83b2bb82021-11-30 10:53:03 +0000454 vnet_crypto_frame_dequeue_t *dequeue_handler;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100455} vnet_crypto_engine_t;
456
457typedef struct
458{
Fan Zhangf5395782020-04-29 14:00:03 +0100459 u32 node_idx;
460 u32 next_idx;
461} vnet_crypto_async_next_node_t;
462
463typedef struct
464{
Damjan Marion91f17dc2019-03-18 18:59:25 +0100465 vnet_crypto_alg_data_t *algs;
466 vnet_crypto_thread_t *threads;
467 vnet_crypto_ops_handler_t **ops_handlers;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000468 vnet_crypto_chained_ops_handler_t **chained_ops_handlers;
Fan Zhangf5395782020-04-29 14:00:03 +0100469 vnet_crypto_frame_enqueue_t **enqueue_handlers;
470 vnet_crypto_frame_dequeue_t **dequeue_handlers;
Damjan Marion060bfb92019-03-29 13:47:54 +0100471 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
Fan Zhangf5395782020-04-29 14:00:03 +0100472 vnet_crypto_async_op_data_t async_opt_data[VNET_CRYPTO_ASYNC_OP_N_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100473 vnet_crypto_engine_t *engines;
Damjan Mariond1bed682019-04-24 15:20:35 +0200474 vnet_crypto_key_t *keys;
Filip Tehlar1469d542019-03-25 09:04:41 -0700475 uword *engine_index_by_name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100476 uword *alg_index_by_name;
Fan Zhangf5395782020-04-29 14:00:03 +0100477 uword *async_alg_index_by_name;
478 vnet_crypto_async_alg_data_t *async_algs;
Fan Zhangf5395782020-04-29 14:00:03 +0100479 vnet_crypto_async_next_node_t *next_nodes;
PiotrX Kleski22848172020-07-08 14:36:34 +0200480 u32 crypto_node_index;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100481} vnet_crypto_main_t;
482
483extern vnet_crypto_main_t crypto_main;
484
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000485u32 vnet_crypto_process_chained_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
486 vnet_crypto_op_chunk_t * chunks,
487 u32 n_ops);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100488u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
489 u32 n_ops);
490
Fan Zhangf5395782020-04-29 14:00:03 +0100491
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000492int vnet_crypto_set_handler2 (char *ops_handler_name, char *engine,
493 crypto_op_class_type_t oct);
Neale Rannsece2ae02019-06-21 12:44:11 +0000494int vnet_crypto_is_set_handler (vnet_crypto_alg_t alg);
Filip Tehlar1469d542019-03-25 09:04:41 -0700495
Damjan Mariond1bed682019-04-24 15:20:35 +0200496u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
497 u8 * data, u16 length);
498void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
Damjan Mariond1bed682019-04-24 15:20:35 +0200499
Fan Zhangf5395782020-04-29 14:00:03 +0100500/**
501 * Use 2 created keys to generate new key for linked algs (cipher + integ)
502 * The returned key index is to be used for linked alg only.
503 **/
504u32 vnet_crypto_key_add_linked (vlib_main_t * vm,
505 vnet_crypto_key_index_t index_crypto,
506 vnet_crypto_key_index_t index_integ);
507
Fan Zhangf5395782020-04-29 14:00:03 +0100508int vnet_crypto_set_async_handler2 (char *alg_name, char *engine);
509
510int vnet_crypto_is_set_async_handler (vnet_crypto_async_op_id_t opt);
511
Fan Zhangf5395782020-04-29 14:00:03 +0100512vnet_crypto_async_alg_t vnet_crypto_link_algs (vnet_crypto_alg_t crypto_alg,
513 vnet_crypto_alg_t integ_alg);
514
Damjan Marion91f17dc2019-03-18 18:59:25 +0100515format_function_t format_vnet_crypto_alg;
516format_function_t format_vnet_crypto_engine;
517format_function_t format_vnet_crypto_op;
Damjan Marion060bfb92019-03-29 13:47:54 +0100518format_function_t format_vnet_crypto_op_type;
519format_function_t format_vnet_crypto_op_status;
Damjan Marion61c0a3d2019-04-05 19:42:21 +0200520unformat_function_t unformat_vnet_crypto_alg;
Damjan Mariona03d1822019-03-28 21:40:48 +0100521
Fan Zhangf5395782020-04-29 14:00:03 +0100522format_function_t format_vnet_crypto_async_op;
523format_function_t format_vnet_crypto_async_alg;
524format_function_t format_vnet_crypto_async_op_type;
525
Damjan Mariona03d1822019-03-28 21:40:48 +0100526static_always_inline void
Damjan Marion060bfb92019-03-29 13:47:54 +0100527vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
Damjan Mariona03d1822019-03-28 21:40:48 +0100528{
529 if (CLIB_DEBUG > 0)
530 clib_memset (op, 0xfe, sizeof (*op));
531 op->op = type;
532 op->flags = 0;
Damjan Mariond1bed682019-04-24 15:20:35 +0200533 op->key_index = ~0;
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000534 op->n_chunks = 0;
Damjan Mariona03d1822019-03-28 21:40:48 +0100535}
536
Damjan Marion060bfb92019-03-29 13:47:54 +0100537static_always_inline vnet_crypto_op_type_t
538vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
539{
540 vnet_crypto_main_t *cm = &crypto_main;
Lijian Zhangb15d7962019-09-27 16:25:35 +0800541 ASSERT (id < VNET_CRYPTO_N_OP_IDS);
542 vnet_crypto_op_data_t *od = cm->opt_data + id;
Damjan Marion060bfb92019-03-29 13:47:54 +0100543 return od->type;
544}
545
Damjan Mariond1bed682019-04-24 15:20:35 +0200546static_always_inline vnet_crypto_key_t *
547vnet_crypto_get_key (vnet_crypto_key_index_t index)
548{
549 vnet_crypto_main_t *cm = &crypto_main;
550 return vec_elt_at_index (cm->keys, index);
551}
552
Filip Tehlarefcad1a2020-02-04 09:36:04 +0000553static_always_inline int
554vnet_crypto_set_handler (char *alg_name, char *engine)
555{
556 return vnet_crypto_set_handler2 (alg_name, engine, CRYPTO_OP_BOTH);
557}
558
Fan Zhangf5395782020-04-29 14:00:03 +0100559/** async crypto inline functions **/
560
561static_always_inline vnet_crypto_async_frame_t *
562vnet_crypto_async_get_frame (vlib_main_t * vm, vnet_crypto_async_op_id_t opt)
563{
564 vnet_crypto_main_t *cm = &crypto_main;
565 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
Neale Rannsfc811342021-02-26 10:35:33 +0000566 vnet_crypto_async_frame_t *f = NULL;
Fan Zhangf5395782020-04-29 14:00:03 +0100567
gaoginskxf441b5d2021-06-07 12:07:01 +0100568 if (PREDICT_TRUE (pool_free_elts (ct->frame_pool)))
569 {
570 pool_get_aligned (ct->frame_pool, f, CLIB_CACHE_LINE_BYTES);
571#if CLIB_DEBUG > 0
572 clib_memset (f, 0xfe, sizeof (*f));
573#endif
574 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
575 f->op = opt;
576 f->n_elts = 0;
577 }
Neale Rannsfc811342021-02-26 10:35:33 +0000578
Fan Zhangf5395782020-04-29 14:00:03 +0100579 return f;
580}
581
582static_always_inline void
583vnet_crypto_async_free_frame (vlib_main_t * vm,
584 vnet_crypto_async_frame_t * frame)
585{
586 vnet_crypto_main_t *cm = &crypto_main;
587 vnet_crypto_thread_t *ct = cm->threads + vm->thread_index;
588 pool_put (ct->frame_pool, frame);
589}
590
591static_always_inline int
592vnet_crypto_async_submit_open_frame (vlib_main_t * vm,
593 vnet_crypto_async_frame_t * frame)
594{
595 vnet_crypto_main_t *cm = &crypto_main;
PiotrX Kleski22848172020-07-08 14:36:34 +0200596 vlib_thread_main_t *tm = vlib_get_thread_main ();
Xiaoming Jiang9a9604b2023-03-09 02:03:50 +0000597 u32 i;
598 vlib_node_t *n;
PiotrX Kleski22848172020-07-08 14:36:34 +0200599
PiotrX Kleskief69b512020-11-24 08:26:26 +0000600 frame->state = VNET_CRYPTO_FRAME_STATE_PENDING;
PiotrX Kleski22848172020-07-08 14:36:34 +0200601 frame->enqueue_thread_index = vm->thread_index;
PiotrX Kleskief69b512020-11-24 08:26:26 +0000602
Dastin Wilski8a4a7c22022-03-31 11:55:09 +0200603 if (PREDICT_FALSE (cm->enqueue_handlers == NULL))
604 {
605 frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
606 return -1;
607 }
608
PiotrX Kleskief69b512020-11-24 08:26:26 +0000609 int ret = (cm->enqueue_handlers[frame->op]) (vm, frame);
610
Fan Zhangf5395782020-04-29 14:00:03 +0100611 if (PREDICT_TRUE (ret == 0))
612 {
Xiaoming Jiang9a9604b2023-03-09 02:03:50 +0000613 n = vlib_get_node (vm, cm->crypto_node_index);
614 if (n->state == VLIB_NODE_STATE_INTERRUPT)
Neale Rannsfc811342021-02-26 10:35:33 +0000615 {
Xiaoming Jiang9a9604b2023-03-09 02:03:50 +0000616 for (i = 0; i < tm->n_vlib_mains; i++)
Damjan Marion6ffb7c62021-03-26 13:06:13 +0100617 vlib_node_set_interrupt_pending (vlib_get_main_by_index (i),
Neale Rannsfc811342021-02-26 10:35:33 +0000618 cm->crypto_node_index);
619 }
Fan Zhangf5395782020-04-29 14:00:03 +0100620 }
PiotrX Kleskief69b512020-11-24 08:26:26 +0000621 else
622 {
623 frame->state = VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
624 }
PiotrX Kleski22848172020-07-08 14:36:34 +0200625
Fan Zhangf5395782020-04-29 14:00:03 +0100626 return ret;
627}
628
Neale Rannsfc811342021-02-26 10:35:33 +0000629static_always_inline void
630vnet_crypto_async_add_to_frame (vlib_main_t *vm, vnet_crypto_async_frame_t *f,
631 u32 key_index, u32 crypto_len,
632 i16 integ_len_adj, i16 crypto_start_offset,
Benoît Ganne02dfd292022-01-18 15:56:41 +0100633 i16 integ_start_offset, u32 buffer_index,
Neale Rannsfc811342021-02-26 10:35:33 +0000634 u16 next_node, u8 *iv, u8 *tag, u8 *aad,
635 u8 flags)
Fan Zhangf5395782020-04-29 14:00:03 +0100636{
Fan Zhangf5395782020-04-29 14:00:03 +0100637 vnet_crypto_async_frame_elt_t *fe;
638 u16 index;
639
Neale Rannsfc811342021-02-26 10:35:33 +0000640 ASSERT (f->n_elts < VNET_CRYPTO_FRAME_SIZE);
Fan Zhangf5395782020-04-29 14:00:03 +0100641
642 index = f->n_elts;
643 fe = &f->elts[index];
644 f->n_elts++;
645 fe->key_index = key_index;
646 fe->crypto_total_length = crypto_len;
647 fe->crypto_start_offset = crypto_start_offset;
648 fe->integ_start_offset = integ_start_offset;
649 fe->integ_length_adj = integ_len_adj;
650 fe->iv = iv;
651 fe->tag = tag;
652 fe->aad = aad;
653 fe->flags = flags;
654 f->buffer_indices[index] = buffer_index;
655 f->next_node_index[index] = next_node;
Fan Zhangf5395782020-04-29 14:00:03 +0100656}
657
658static_always_inline void
659vnet_crypto_async_reset_frame (vnet_crypto_async_frame_t * f)
660{
661 vnet_crypto_async_op_id_t opt;
662 ASSERT (f != 0);
PiotrX Kleski8ed06c02020-11-24 07:34:09 +0000663 ASSERT ((f->state == VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED
664 || f->state == VNET_CRYPTO_FRAME_STATE_ELT_ERROR));
Fan Zhangf5395782020-04-29 14:00:03 +0100665 opt = f->op;
666 if (CLIB_DEBUG > 0)
667 clib_memset (f, 0xfe, sizeof (*f));
668 f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
669 f->op = opt;
670 f->n_elts = 0;
671}
672
Neale Rannsfc811342021-02-26 10:35:33 +0000673static_always_inline u8
674vnet_crypto_async_frame_is_full (const vnet_crypto_async_frame_t *f)
675{
676 return (f->n_elts == VNET_CRYPTO_FRAME_SIZE);
677}
678
Damjan Marion91f17dc2019-03-18 18:59:25 +0100679#endif /* included_vnet_crypto_crypto_h */
680
681/*
682 * fd.io coding-style-patch-verification: ON
683 *
684 * Local Variables:
685 * eval: (c-set-style "gnu")
686 * End:
687 */