blob: 5af0822812faf3dd3fc16f6558e1352e3e646ed6 [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
19#define VNET_CRYPTO_RING_SIZE 512
20
21#include <vlib/vlib.h>
22
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) \
26 _(3DES_CBC, "3des-cbc", 14) \
27 _(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) \
38 _(AES_256_GCM, "aes-256-gcm", 32)
Damjan Marion060bfb92019-03-29 13:47:54 +010039
40#define foreach_crypto_hmac_alg \
Filip Tehlare225f712019-03-19 10:37:06 -070041 _(MD5, "md5") \
Damjan Marion91f17dc2019-03-18 18:59:25 +010042 _(SHA1, "sha-1") \
43 _(SHA224, "sha-224") \
44 _(SHA256, "sha-256") \
45 _(SHA384, "sha-384") \
46 _(SHA512, "sha-512")
47
Damjan Marion060bfb92019-03-29 13:47:54 +010048
49#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 \
65 _(PENDING, "pending") \
66 _(COMPLETED, "completed") \
67 _(FAIL_NO_HANDLER, "no-handler") \
68 _(FAIL_BAD_HMAC, "bad-hmac") \
69 _(FAIL_DECRYPT, "decrypt-fail")
70
71typedef enum
72{
Damjan Mariond1bed682019-04-24 15:20:35 +020073 VNET_CRYPTO_KEY_OP_ADD,
74 VNET_CRYPTO_KEY_OP_DEL,
75 VNET_CRYPTO_KEY_OP_MODIFY,
76} vnet_crypto_key_op_t;
77
78typedef enum
79{
Damjan Marion060bfb92019-03-29 13:47:54 +010080#define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
81 foreach_crypto_op_status
82#undef _
83 VNET_CRYPTO_OP_N_STATUS,
84} vnet_crypto_op_status_t;
85
Damjan Marion91f17dc2019-03-18 18:59:25 +010086/* *INDENT-OFF* */
87typedef enum
88{
Damjan Mariond1bed682019-04-24 15:20:35 +020089 VNET_CRYPTO_ALG_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +020090#define _(n, s, l) VNET_CRYPTO_ALG_##n,
Damjan Marion060bfb92019-03-29 13:47:54 +010091 foreach_crypto_cipher_alg
92 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010093#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +010094#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
95 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010096#undef _
97 VNET_CRYPTO_N_ALGS,
98} vnet_crypto_alg_t;
99
Damjan Mariond1bed682019-04-24 15:20:35 +0200100typedef struct
101{
102 u8 *data;
103 vnet_crypto_alg_t alg:8;
104} vnet_crypto_key_t;
105
Damjan Marion91f17dc2019-03-18 18:59:25 +0100106typedef enum
107{
108 VNET_CRYPTO_OP_NONE = 0,
Benoît Gannebe954442019-04-29 16:05:46 +0200109#define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
Damjan Marion060bfb92019-03-29 13:47:54 +0100110 foreach_crypto_cipher_alg
111 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100112#undef _
113#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
Damjan Marion060bfb92019-03-29 13:47:54 +0100114 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100115#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100116 VNET_CRYPTO_N_OP_IDS,
117} vnet_crypto_op_id_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100118/* *INDENT-ON* */
119
120typedef struct
121{
122 char *name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100123 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100124} vnet_crypto_alg_data_t;
125
Damjan Marion91f17dc2019-03-18 18:59:25 +0100126typedef struct
127{
128 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion060bfb92019-03-29 13:47:54 +0100129 vnet_crypto_op_id_t op:16;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100130 vnet_crypto_op_status_t status:8;
Damjan Marion060bfb92019-03-29 13:47:54 +0100131 u8 flags;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100132#define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
133#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
Damjan Mariond1bed682019-04-24 15:20:35 +0200134 u32 key_index;
Damjan Mariond97918e2019-04-25 18:28:31 +0200135 u32 len;
Damjan Marion060bfb92019-03-29 13:47:54 +0100136 u16 aad_len;
Damjan Marion82d81d42019-04-25 18:24:04 +0200137 u8 digest_len, tag_len;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100138 u8 *iv;
139 u8 *src;
140 u8 *dst;
Damjan Marion060bfb92019-03-29 13:47:54 +0100141 u8 *aad;
142 u8 *tag;
143 u8 *digest;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100144 uword user_data;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100145} vnet_crypto_op_t;
146
147typedef struct
148{
Damjan Marion060bfb92019-03-29 13:47:54 +0100149 vnet_crypto_op_type_t type;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100150 vnet_crypto_alg_t alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100151 u32 active_engine_index;
Damjan Marion060bfb92019-03-29 13:47:54 +0100152} vnet_crypto_op_data_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100153
154typedef struct
155{
156 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
157 clib_bitmap_t *act_queues;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100158} vnet_crypto_thread_t;
159
Damjan Mariond1bed682019-04-24 15:20:35 +0200160typedef u32 vnet_crypto_key_index_t;
161
Damjan Marion91f17dc2019-03-18 18:59:25 +0100162typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
163 vnet_crypto_op_t * ops[], u32 n_ops);
164
Damjan Mariond1bed682019-04-24 15:20:35 +0200165typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
166 vnet_crypto_key_op_t kop,
167 vnet_crypto_key_index_t idx);
168
Damjan Marion91f17dc2019-03-18 18:59:25 +0100169u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
170 char *desc);
171
Damjan Mariond1bed682019-04-24 15:20:35 +0200172void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
173 vnet_crypto_op_id_t opt,
174 vnet_crypto_ops_handler_t * oph);
175void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
176 vnet_crypto_key_handler_t * keyh);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100177
178typedef struct
179{
180 char *name;
181 char *desc;
182 int priority;
Damjan Mariond1bed682019-04-24 15:20:35 +0200183 vnet_crypto_key_handler_t *key_op_handler;
Damjan Marion060bfb92019-03-29 13:47:54 +0100184 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100185} vnet_crypto_engine_t;
186
187typedef struct
188{
189 vnet_crypto_alg_data_t *algs;
190 vnet_crypto_thread_t *threads;
191 vnet_crypto_ops_handler_t **ops_handlers;
Damjan Marion060bfb92019-03-29 13:47:54 +0100192 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100193 vnet_crypto_engine_t *engines;
Damjan Mariond1bed682019-04-24 15:20:35 +0200194 vnet_crypto_key_t *keys;
Filip Tehlar1469d542019-03-25 09:04:41 -0700195 uword *engine_index_by_name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100196 uword *alg_index_by_name;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100197} vnet_crypto_main_t;
198
199extern vnet_crypto_main_t crypto_main;
200
201u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
202 u32 n_jobs);
203
204u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
205 u32 n_ops);
206
Filip Tehlar1469d542019-03-25 09:04:41 -0700207int vnet_crypto_set_handler (char *ops_handler_name, char *engine);
208
Damjan Mariond1bed682019-04-24 15:20:35 +0200209u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
210 u8 * data, u16 length);
211void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
212void vnet_crypto_key_modify (vlib_main_t * vm, vnet_crypto_key_index_t index,
213 vnet_crypto_alg_t alg, u8 * data, u16 len);
214
Damjan Marion91f17dc2019-03-18 18:59:25 +0100215format_function_t format_vnet_crypto_alg;
216format_function_t format_vnet_crypto_engine;
217format_function_t format_vnet_crypto_op;
Damjan Marion060bfb92019-03-29 13:47:54 +0100218format_function_t format_vnet_crypto_op_type;
219format_function_t format_vnet_crypto_op_status;
Damjan Marion61c0a3d2019-04-05 19:42:21 +0200220unformat_function_t unformat_vnet_crypto_alg;
Damjan Mariona03d1822019-03-28 21:40:48 +0100221
222static_always_inline void
Damjan Marion060bfb92019-03-29 13:47:54 +0100223vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
Damjan Mariona03d1822019-03-28 21:40:48 +0100224{
225 if (CLIB_DEBUG > 0)
226 clib_memset (op, 0xfe, sizeof (*op));
227 op->op = type;
228 op->flags = 0;
Damjan Mariond1bed682019-04-24 15:20:35 +0200229 op->key_index = ~0;
Damjan Mariona03d1822019-03-28 21:40:48 +0100230}
231
Damjan Marion060bfb92019-03-29 13:47:54 +0100232static_always_inline vnet_crypto_op_type_t
233vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
234{
235 vnet_crypto_main_t *cm = &crypto_main;
236 vnet_crypto_op_data_t *od = vec_elt_at_index (cm->opt_data, id);
237 return od->type;
238}
239
Damjan Mariond1bed682019-04-24 15:20:35 +0200240static_always_inline vnet_crypto_key_t *
241vnet_crypto_get_key (vnet_crypto_key_index_t index)
242{
243 vnet_crypto_main_t *cm = &crypto_main;
244 return vec_elt_at_index (cm->keys, index);
245}
246
Damjan Marion91f17dc2019-03-18 18:59:25 +0100247#endif /* included_vnet_crypto_crypto_h */
248
249/*
250 * fd.io coding-style-patch-verification: ON
251 *
252 * Local Variables:
253 * eval: (c-set-style "gnu")
254 * End:
255 */