ipsec: esp-decrypt rework
[vpp.git] / src / vnet / crypto / crypto.h
1 /*
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
19 #define VNET_CRYPTO_RING_SIZE 512
20
21 #include <vlib/vlib.h>
22
23 #define foreach_crypto_alg \
24   _(DES_CBC, "des-cbc") \
25   _(3DES_CBC, "3des-cbc") \
26   _(AES_128_CBC, "aes-128-cbc") \
27   _(AES_192_CBC, "aes-192-cbc") \
28   _(AES_256_CBC, "aes-256-cbc")
29
30 #define foreach_hmac_alg \
31   _(MD5, "md5") \
32   _(SHA1, "sha-1") \
33   _(SHA224, "sha-224")  \
34   _(SHA256, "sha-256")  \
35   _(SHA384, "sha-384")  \
36   _(SHA512, "sha-512")
37
38 /* *INDENT-OFF* */
39 typedef enum
40 {
41 #define _(n, s) VNET_CRYPTO_ALG_##n,
42   foreach_crypto_alg
43 #undef _
44 #define _(n, s) VNET_CRYPTO_ALG_##n,
45   foreach_hmac_alg
46 #undef _
47   VNET_CRYPTO_N_ALGS,
48 } vnet_crypto_alg_t;
49
50 typedef enum
51 {
52   VNET_CRYPTO_OP_NONE = 0,
53 #define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
54   foreach_crypto_alg
55 #undef _
56 #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
57   foreach_hmac_alg
58 #undef _
59     VNET_CRYPTO_N_OP_TYPES,
60 } vnet_crypto_op_type_t;
61 /* *INDENT-ON* */
62
63 typedef struct
64 {
65   char *name;
66 } vnet_crypto_alg_data_t;
67
68 typedef enum
69 {
70   VNET_CRYPTO_OP_STATUS_PENDING,
71   VNET_CRYPTO_OP_STATUS_COMPLETED,
72   VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER,
73   VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC,
74 } vnet_crypto_op_status_t;
75
76 typedef struct
77 {
78   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
79   vnet_crypto_op_type_t op:8;
80   vnet_crypto_op_status_t status:8;
81   u8 key_len, hmac_trunc_len;
82   u16 flags;
83 #define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
84 #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
85   u32 len;
86   u8 *key;
87   u8 *iv;
88   u8 *src;
89   u8 *dst;
90   uword user_data;
91 } vnet_crypto_op_t;
92
93 typedef struct
94 {
95   vnet_crypto_alg_t alg;
96   const char *desc;
97   u32 active_engine_index;
98 } vnet_crypto_op_type_data_t;
99
100 typedef struct
101 {
102   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
103   u32 head;
104   u32 tail;
105   u32 size;
106   vnet_crypto_alg_t alg:8;
107   vnet_crypto_op_type_t op:8;
108   vnet_crypto_op_t *jobs[0];
109 } vnet_crypto_queue_t;
110
111 typedef struct
112 {
113   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
114   clib_bitmap_t *act_queues;
115   vnet_crypto_queue_t *queues[VNET_CRYPTO_N_OP_TYPES];
116 } vnet_crypto_thread_t;
117
118 typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
119                                          vnet_crypto_op_t * ops[], u32 n_ops);
120
121 u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
122                                  char *desc);
123
124 vlib_error_t *vnet_crypto_register_ops_handler (vlib_main_t * vm,
125                                                 u32 provider_index,
126                                                 vnet_crypto_op_type_t opt,
127                                                 vnet_crypto_ops_handler_t *
128                                                 f);
129
130 typedef struct
131 {
132   char *name;
133   char *desc;
134   int priority;
135   vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_TYPES];
136 } vnet_crypto_engine_t;
137
138 typedef struct
139 {
140   vnet_crypto_alg_data_t *algs;
141   vnet_crypto_thread_t *threads;
142   vnet_crypto_ops_handler_t **ops_handlers;
143   vnet_crypto_op_type_data_t opt_data[VNET_CRYPTO_N_OP_TYPES];
144   vnet_crypto_engine_t *engines;
145   uword *engine_index_by_name;
146   uword *ops_handler_index_by_name;
147 } vnet_crypto_main_t;
148
149 extern vnet_crypto_main_t crypto_main;
150
151 u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
152                             u32 n_jobs);
153
154 u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
155                              u32 n_ops);
156
157
158 int vnet_crypto_set_handler (char *ops_handler_name, char *engine);
159
160 format_function_t format_vnet_crypto_alg;
161 format_function_t format_vnet_crypto_engine;
162 format_function_t format_vnet_crypto_op;
163
164
165 static_always_inline void
166 vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_type_t type)
167 {
168   if (CLIB_DEBUG > 0)
169     clib_memset (op, 0xfe, sizeof (*op));
170   op->op = type;
171   op->flags = 0;
172 }
173
174 #endif /* included_vnet_crypto_crypto_h */
175
176 /*
177  * fd.io coding-style-patch-verification: ON
178  *
179  * Local Variables:
180  * eval: (c-set-style "gnu")
181  * End:
182  */