crypto: introduce crypto infra
[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   _(SHA1, "sha-1") \
32   _(SHA224, "sha-224")  \
33   _(SHA256, "sha-256")  \
34   _(SHA384, "sha-384")  \
35   _(SHA512, "sha-512")
36
37 /* *INDENT-OFF* */
38 typedef enum
39 {
40 #define _(n, s) VNET_CRYPTO_ALG_##n,
41   foreach_crypto_alg
42 #undef _
43 #define _(n, s) VNET_CRYPTO_ALG_##n,
44   foreach_hmac_alg
45 #undef _
46   VNET_CRYPTO_N_ALGS,
47 } vnet_crypto_alg_t;
48
49 typedef enum
50 {
51   VNET_CRYPTO_OP_NONE = 0,
52 #define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
53   foreach_crypto_alg
54 #undef _
55 #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
56   foreach_hmac_alg
57 #undef _
58     VNET_CRYPTO_N_OP_TYPES,
59 } vnet_crypto_op_type_t;
60 /* *INDENT-ON* */
61
62 typedef struct
63 {
64   char *name;
65 } vnet_crypto_alg_data_t;
66
67 typedef enum
68 {
69   VNET_CRYPTO_OP_STATUS_PENDING,
70   VNET_CRYPTO_OP_STATUS_COMPLETED,
71   VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER,
72 } vnet_crypto_op_status_t;
73
74 typedef struct
75 {
76   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
77   vnet_crypto_op_type_t op:16;
78   vnet_crypto_op_status_t status:8;
79   u8 key_len;
80   u16 flags;
81 #define VNET_CRYPTO_OP_FLAG_INIT_IV 1
82   u32 len;
83   u8 *key;
84   u8 *iv;
85   u8 *src;
86   u8 *dst;
87 } vnet_crypto_op_t;
88
89 typedef struct
90 {
91   vnet_crypto_alg_t alg;
92   const char *desc;
93   u32 active_engine_index;
94 } vnet_crypto_op_type_data_t;
95
96 typedef struct
97 {
98   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
99   u32 head;
100   u32 tail;
101   u32 size;
102   vnet_crypto_alg_t alg:8;
103   vnet_crypto_op_type_t op:8;
104   vnet_crypto_op_t *jobs[0];
105 } vnet_crypto_queue_t;
106
107 typedef struct
108 {
109   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
110   clib_bitmap_t *act_queues;
111   vnet_crypto_queue_t *queues[VNET_CRYPTO_N_OP_TYPES];
112 } vnet_crypto_thread_t;
113
114 typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
115                                          vnet_crypto_op_t * ops[], u32 n_ops);
116
117 u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
118                                  char *desc);
119
120 vlib_error_t *vnet_crypto_register_ops_handler (vlib_main_t * vm,
121                                                 u32 provider_index,
122                                                 vnet_crypto_op_type_t opt,
123                                                 vnet_crypto_ops_handler_t *
124                                                 f);
125
126 typedef struct
127 {
128   char *name;
129   char *desc;
130   int priority;
131   vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_TYPES];
132 } vnet_crypto_engine_t;
133
134 typedef struct
135 {
136   vnet_crypto_alg_data_t *algs;
137   vnet_crypto_thread_t *threads;
138   vnet_crypto_ops_handler_t **ops_handlers;
139   vnet_crypto_op_type_data_t opt_data[VNET_CRYPTO_N_OP_TYPES];
140   vnet_crypto_engine_t *engines;
141 } vnet_crypto_main_t;
142
143 extern vnet_crypto_main_t crypto_main;
144
145 u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
146                             u32 n_jobs);
147
148 u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
149                              u32 n_ops);
150
151 format_function_t format_vnet_crypto_alg;
152 format_function_t format_vnet_crypto_engine;
153 format_function_t format_vnet_crypto_op;
154
155 #endif /* included_vnet_crypto_crypto_h */
156
157 /*
158  * fd.io coding-style-patch-verification: ON
159  *
160  * Local Variables:
161  * eval: (c-set-style "gnu")
162  * End:
163  */