blob: 06f7e534e11bbb61ad2b17fb635e4caf906e9b46 [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{
71#define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
72 foreach_crypto_op_status
73#undef _
74 VNET_CRYPTO_OP_N_STATUS,
75} vnet_crypto_op_status_t;
76
Damjan Marion91f17dc2019-03-18 18:59:25 +010077/* *INDENT-OFF* */
78typedef enum
79{
80#define _(n, s) VNET_CRYPTO_ALG_##n,
Damjan Marion060bfb92019-03-29 13:47:54 +010081 foreach_crypto_cipher_alg
82 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010083#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +010084#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
85 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010086#undef _
87 VNET_CRYPTO_N_ALGS,
88} vnet_crypto_alg_t;
89
90typedef enum
91{
92 VNET_CRYPTO_OP_NONE = 0,
93#define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
Damjan Marion060bfb92019-03-29 13:47:54 +010094 foreach_crypto_cipher_alg
95 foreach_crypto_aead_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010096#undef _
97#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
Damjan Marion060bfb92019-03-29 13:47:54 +010098 foreach_crypto_hmac_alg
Damjan Marion91f17dc2019-03-18 18:59:25 +010099#undef _
Damjan Marion060bfb92019-03-29 13:47:54 +0100100 VNET_CRYPTO_N_OP_IDS,
101} vnet_crypto_op_id_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100102/* *INDENT-ON* */
103
104typedef struct
105{
106 char *name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100107 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100108} vnet_crypto_alg_data_t;
109
Damjan Marion91f17dc2019-03-18 18:59:25 +0100110typedef struct
111{
112 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Damjan Marion060bfb92019-03-29 13:47:54 +0100113 vnet_crypto_op_id_t op:16;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100114 vnet_crypto_op_status_t status:8;
Damjan Marion060bfb92019-03-29 13:47:54 +0100115 u8 flags;
Damjan Marionb4fff3a2019-03-25 15:54:40 +0100116#define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
117#define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
Damjan Marion91f17dc2019-03-18 18:59:25 +0100118 u32 len;
Damjan Marion060bfb92019-03-29 13:47:54 +0100119 u16 aad_len;
120 u8 key_len, iv_len, digest_len, tag_len;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100121 u8 *key;
122 u8 *iv;
123 u8 *src;
124 u8 *dst;
Damjan Marion060bfb92019-03-29 13:47:54 +0100125 u8 *aad;
126 u8 *tag;
127 u8 *digest;
Damjan Marionc59b9a22019-03-19 15:38:40 +0100128 uword user_data;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100129} vnet_crypto_op_t;
130
131typedef struct
132{
Damjan Marion060bfb92019-03-29 13:47:54 +0100133 vnet_crypto_op_type_t type;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100134 vnet_crypto_alg_t alg;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100135 u32 active_engine_index;
Damjan Marion060bfb92019-03-29 13:47:54 +0100136} vnet_crypto_op_data_t;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100137
138typedef struct
139{
140 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
141 clib_bitmap_t *act_queues;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100142} vnet_crypto_thread_t;
143
144typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
145 vnet_crypto_op_t * ops[], u32 n_ops);
146
147u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
148 char *desc);
149
150vlib_error_t *vnet_crypto_register_ops_handler (vlib_main_t * vm,
151 u32 provider_index,
Damjan Marion060bfb92019-03-29 13:47:54 +0100152 vnet_crypto_op_id_t opt,
Damjan Marion91f17dc2019-03-18 18:59:25 +0100153 vnet_crypto_ops_handler_t *
154 f);
155
156typedef struct
157{
158 char *name;
159 char *desc;
160 int priority;
Damjan Marion060bfb92019-03-29 13:47:54 +0100161 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100162} vnet_crypto_engine_t;
163
164typedef struct
165{
166 vnet_crypto_alg_data_t *algs;
167 vnet_crypto_thread_t *threads;
168 vnet_crypto_ops_handler_t **ops_handlers;
Damjan Marion060bfb92019-03-29 13:47:54 +0100169 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
Damjan Marion91f17dc2019-03-18 18:59:25 +0100170 vnet_crypto_engine_t *engines;
Filip Tehlar1469d542019-03-25 09:04:41 -0700171 uword *engine_index_by_name;
Damjan Marion060bfb92019-03-29 13:47:54 +0100172 uword *alg_index_by_name;
Damjan Marion91f17dc2019-03-18 18:59:25 +0100173} vnet_crypto_main_t;
174
175extern vnet_crypto_main_t crypto_main;
176
177u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
178 u32 n_jobs);
179
180u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
181 u32 n_ops);
182
Filip Tehlar1469d542019-03-25 09:04:41 -0700183
184int vnet_crypto_set_handler (char *ops_handler_name, char *engine);
185
Damjan Marion91f17dc2019-03-18 18:59:25 +0100186format_function_t format_vnet_crypto_alg;
187format_function_t format_vnet_crypto_engine;
188format_function_t format_vnet_crypto_op;
Damjan Marion060bfb92019-03-29 13:47:54 +0100189format_function_t format_vnet_crypto_op_type;
190format_function_t format_vnet_crypto_op_status;
Damjan Marion61c0a3d2019-04-05 19:42:21 +0200191unformat_function_t unformat_vnet_crypto_alg;
Damjan Mariona03d1822019-03-28 21:40:48 +0100192
193static_always_inline void
Damjan Marion060bfb92019-03-29 13:47:54 +0100194vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
Damjan Mariona03d1822019-03-28 21:40:48 +0100195{
196 if (CLIB_DEBUG > 0)
197 clib_memset (op, 0xfe, sizeof (*op));
198 op->op = type;
199 op->flags = 0;
200}
201
Damjan Marion060bfb92019-03-29 13:47:54 +0100202static_always_inline vnet_crypto_op_type_t
203vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
204{
205 vnet_crypto_main_t *cm = &crypto_main;
206 vnet_crypto_op_data_t *od = vec_elt_at_index (cm->opt_data, id);
207 return od->type;
208}
209
Damjan Marion91f17dc2019-03-18 18:59:25 +0100210#endif /* included_vnet_crypto_crypto_h */
211
212/*
213 * fd.io coding-style-patch-verification: ON
214 *
215 * Local Variables:
216 * eval: (c-set-style "gnu")
217 * End:
218 */