blob: 95bc72b2dc01f1002dc55e7791354a52d968d497 [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
Damjan Marion060bfb92019-03-29 13:47:54 +010023#define foreach_crypto_cipher_alg \
24 _(DES_CBC, "des-cbc") \
25 _(3DES_CBC, "3des-cbc") \
Damjan Marion91f17dc2019-03-18 18:59:25 +010026 _(AES_128_CBC, "aes-128-cbc") \
27 _(AES_192_CBC, "aes-192-cbc") \
fituldo89e66432019-04-12 12:26:43 +000028 _(AES_256_CBC, "aes-256-cbc") \
29 _(AES_128_CTR, "aes-128-ctr") \
30 _(AES_192_CTR, "aes-192-ctr") \
31 _(AES_256_CTR, "aes-256-ctr")
Damjan Marion91f17dc2019-03-18 18:59:25 +010032
Damjan Marion060bfb92019-03-29 13:47:54 +010033#define foreach_crypto_aead_alg \
34 _(AES_128_GCM, "aes-128-gcm") \
35 _(AES_192_GCM, "aes-192-gcm") \
36 _(AES_256_GCM, "aes-256-gcm")
37
38#define foreach_crypto_hmac_alg \
Filip Tehlare225f712019-03-19 10:37:06 -070039 _(MD5, "md5") \
Damjan Marion91f17dc2019-03-18 18:59:25 +010040 _(SHA1, "sha-1") \
41 _(SHA224, "sha-224") \
42 _(SHA256, "sha-256") \
43 _(SHA384, "sha-384") \
44 _(SHA512, "sha-512")
45
Damjan Marion060bfb92019-03-29 13:47:54 +010046
47#define foreach_crypto_op_type \
48 _(ENCRYPT, "encrypt") \
49 _(DECRYPT, "decrypt") \
50 _(AEAD_ENCRYPT, "aead-encrypt") \
51 _(AEAD_DECRYPT, "aead-decrypt") \
52 _(HMAC, "hmac")
53
54typedef enum
55{
56#define _(n, s) VNET_CRYPTO_OP_TYPE_##n,
57 foreach_crypto_op_type
58#undef _
59 VNET_CRYPTO_OP_N_TYPES,
60} vnet_crypto_op_type_t;
61
62#define foreach_crypto_op_status \
63 _(PENDING, "pending") \
64 _(COMPLETED, "completed") \
65 _(FAIL_NO_HANDLER, "no-handler") \
66 _(FAIL_BAD_HMAC, "bad-hmac") \
67 _(FAIL_DECRYPT, "decrypt-fail")
68
69typedef enum
70{
Damjan Mariond1bed682019-04-24 15:20:35 +020071 VNET_CRYPTO_KEY_OP_ADD,
72 VNET_CRYPTO_KEY_OP_DEL,
73 VNET_CRYPTO_KEY_OP_MODIFY,
74} vnet_crypto_key_op_t;
75
76typedef enum
77{
Damjan Marion060bfb92019-03-29 13:47:54 +010078#define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
79 foreach_crypto_op_status
80#undef _
81 VNET_CRYPTO_OP_N_STATUS,
82} vnet_crypto_op_status_t;
83
Damjan Marion91f17dc2019-03-18 18:59:25 +010084/* *INDENT-OFF* */
85typedef enum
86{
Damjan Mariond1bed682019-04-24 15:20:35 +020087 VNET_CRYPTO_ALG_NONE = 0,
Damjan Marion91f17dc2019-03-18 18:59:25 +010088#define _(n, s) VNET_CRYPTO_ALG_##n,
Damjan Marion060bfb92019-03-29 13:47:54 +010089 foreach_crypto_cipher_alg
90 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010091#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +010092#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
93 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010094#undef _
95 VNET_CRYPTO_N_ALGS,
96} vnet_crypto_alg_t;
97
Damjan Mariond1bed682019-04-24 15:20:35 +020098typedef struct
99{
100 u8 *data;
101 vnet_crypto_alg_t alg:8;
102} vnet_crypto_key_t;
103
Damjan Marion91f17dc2019-03-18 18:59:25 +0100104typedef enum
105{
106 VNET_CRYPTO_OP_NONE = 0,
107#define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
Damjan Marion060bfb92019-03-29 13:47:54 +0100108 foreach_crypto_cipher_alg
109 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100110#undef _
111#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
Damjan Marion060bfb92019-03-29 13:47:54 +0100112 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +0100113#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100114 VNET_CRYPTO_N_OP_IDS,
115} vnet_crypto_op_id_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100116/* *INDENT-ON* */
117
118typedef struct
119{
120 char *name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100121 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100122} vnet_crypto_alg_data_t;
123
Damjan Marion91f17dc2019-03-18 18:59:25 +0100124typedef struct
125{
126 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion060bfb92019-03-29 13:47:54 +0100127 vnet_crypto_op_id_t op:16;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100128 vnet_crypto_op_status_t status:8;
Damjan Marion060bfb92019-03-29 13:47:54 +0100129 u8 flags;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100130#define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
131#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
Damjan Mariond1bed682019-04-24 15:20:35 +0200132 u32 key_index;
Damjan Mariond97918e2019-04-25 18:28:31 +0200133 u32 len;
Damjan Marion060bfb92019-03-29 13:47:54 +0100134 u16 aad_len;
Damjan Marion82d81d42019-04-25 18:24:04 +0200135 u8 digest_len, tag_len;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100136 u8 *iv;
137 u8 *src;
138 u8 *dst;
Damjan Marion060bfb92019-03-29 13:47:54 +0100139 u8 *aad;
140 u8 *tag;
141 u8 *digest;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100142 uword user_data;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100143} vnet_crypto_op_t;
144
145typedef struct
146{
Damjan Marion060bfb92019-03-29 13:47:54 +0100147 vnet_crypto_op_type_t type;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100148 vnet_crypto_alg_t alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100149 u32 active_engine_index;
Damjan Marion060bfb92019-03-29 13:47:54 +0100150} vnet_crypto_op_data_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100151
152typedef struct
153{
154 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
155 clib_bitmap_t *act_queues;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100156} vnet_crypto_thread_t;
157
Damjan Mariond1bed682019-04-24 15:20:35 +0200158typedef u32 vnet_crypto_key_index_t;
159
Damjan Marion91f17dc2019-03-18 18:59:25 +0100160typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
161 vnet_crypto_op_t * ops[], u32 n_ops);
162
Damjan Mariond1bed682019-04-24 15:20:35 +0200163typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
164 vnet_crypto_key_op_t kop,
165 vnet_crypto_key_index_t idx);
166
Damjan Marion91f17dc2019-03-18 18:59:25 +0100167u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
168 char *desc);
169
Damjan Mariond1bed682019-04-24 15:20:35 +0200170void vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
171 vnet_crypto_op_id_t opt,
172 vnet_crypto_ops_handler_t * oph);
173void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
174 vnet_crypto_key_handler_t * keyh);
Damjan Marion91f17dc2019-03-18 18:59:25 +0100175
176typedef struct
177{
178 char *name;
179 char *desc;
180 int priority;
Damjan Mariond1bed682019-04-24 15:20:35 +0200181 vnet_crypto_key_handler_t *key_op_handler;
Damjan Marion060bfb92019-03-29 13:47:54 +0100182 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100183} vnet_crypto_engine_t;
184
185typedef struct
186{
187 vnet_crypto_alg_data_t *algs;
188 vnet_crypto_thread_t *threads;
189 vnet_crypto_ops_handler_t **ops_handlers;
Damjan Marion060bfb92019-03-29 13:47:54 +0100190 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100191 vnet_crypto_engine_t *engines;
Damjan Mariond1bed682019-04-24 15:20:35 +0200192 vnet_crypto_key_t *keys;
Filip Tehlar1469d542019-03-25 09:04:41 -0700193 uword *engine_index_by_name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100194 uword *alg_index_by_name;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100195} vnet_crypto_main_t;
196
197extern vnet_crypto_main_t crypto_main;
198
199u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
200 u32 n_jobs);
201
202u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
203 u32 n_ops);
204
Filip Tehlar1469d542019-03-25 09:04:41 -0700205int vnet_crypto_set_handler (char *ops_handler_name, char *engine);
206
Damjan Mariond1bed682019-04-24 15:20:35 +0200207u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
208 u8 * data, u16 length);
209void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
210void vnet_crypto_key_modify (vlib_main_t * vm, vnet_crypto_key_index_t index,
211 vnet_crypto_alg_t alg, u8 * data, u16 len);
212
Damjan Marion91f17dc2019-03-18 18:59:25 +0100213format_function_t format_vnet_crypto_alg;
214format_function_t format_vnet_crypto_engine;
215format_function_t format_vnet_crypto_op;
Damjan Marion060bfb92019-03-29 13:47:54 +0100216format_function_t format_vnet_crypto_op_type;
217format_function_t format_vnet_crypto_op_status;
Damjan Marion61c0a3d2019-04-05 19:42:21 +0200218unformat_function_t unformat_vnet_crypto_alg;
Damjan Mariona03d1822019-03-28 21:40:48 +0100219
220static_always_inline void
Damjan Marion060bfb92019-03-29 13:47:54 +0100221vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
Damjan Mariona03d1822019-03-28 21:40:48 +0100222{
223 if (CLIB_DEBUG > 0)
224 clib_memset (op, 0xfe, sizeof (*op));
225 op->op = type;
226 op->flags = 0;
Damjan Mariond1bed682019-04-24 15:20:35 +0200227 op->key_index = ~0;
Damjan Mariona03d1822019-03-28 21:40:48 +0100228}
229
Damjan Marion060bfb92019-03-29 13:47:54 +0100230static_always_inline vnet_crypto_op_type_t
231vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
232{
233 vnet_crypto_main_t *cm = &crypto_main;
234 vnet_crypto_op_data_t *od = vec_elt_at_index (cm->opt_data, id);
235 return od->type;
236}
237
Damjan Mariond1bed682019-04-24 15:20:35 +0200238static_always_inline vnet_crypto_key_t *
239vnet_crypto_get_key (vnet_crypto_key_index_t index)
240{
241 vnet_crypto_main_t *cm = &crypto_main;
242 return vec_elt_at_index (cm->keys, index);
243}
244
Damjan Marion91f17dc2019-03-18 18:59:25 +0100245#endif /* included_vnet_crypto_crypto_h */
246
247/*
248 * fd.io coding-style-patch-verification: ON
249 *
250 * Local Variables:
251 * eval: (c-set-style "gnu")
252 * End:
253 */