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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef included_vnet_crypto_crypto_h
17 #define included_vnet_crypto_crypto_h
19 #define VNET_CRYPTO_RING_SIZE 512
21 #include <vlib/vlib.h>
23 #define foreach_crypto_cipher_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 _(AES_128_CTR, "aes-128-ctr") \
30 _(AES_192_CTR, "aes-192-ctr") \
31 _(AES_256_CTR, "aes-256-ctr")
33 #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")
38 #define foreach_crypto_hmac_alg \
41 _(SHA224, "sha-224") \
42 _(SHA256, "sha-256") \
43 _(SHA384, "sha-384") \
47 #define foreach_crypto_op_type \
48 _(ENCRYPT, "encrypt") \
49 _(DECRYPT, "decrypt") \
50 _(AEAD_ENCRYPT, "aead-encrypt") \
51 _(AEAD_DECRYPT, "aead-decrypt") \
56 #define _(n, s) VNET_CRYPTO_OP_TYPE_##n,
57 foreach_crypto_op_type
59 VNET_CRYPTO_OP_N_TYPES,
60 } vnet_crypto_op_type_t;
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")
71 VNET_CRYPTO_KEY_OP_ADD,
72 VNET_CRYPTO_KEY_OP_DEL,
73 VNET_CRYPTO_KEY_OP_MODIFY,
74 } vnet_crypto_key_op_t;
78 #define _(n, s) VNET_CRYPTO_OP_STATUS_##n,
79 foreach_crypto_op_status
81 VNET_CRYPTO_OP_N_STATUS,
82 } vnet_crypto_op_status_t;
87 VNET_CRYPTO_ALG_NONE = 0,
88 #define _(n, s) VNET_CRYPTO_ALG_##n,
89 foreach_crypto_cipher_alg
90 foreach_crypto_aead_alg
92 #define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
93 foreach_crypto_hmac_alg
101 vnet_crypto_alg_t alg:8;
106 VNET_CRYPTO_OP_NONE = 0,
107 #define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
108 foreach_crypto_cipher_alg
109 foreach_crypto_aead_alg
111 #define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
112 foreach_crypto_hmac_alg
114 VNET_CRYPTO_N_OP_IDS,
115 } vnet_crypto_op_id_t;
121 vnet_crypto_op_id_t op_by_type[VNET_CRYPTO_OP_N_TYPES];
122 } vnet_crypto_alg_data_t;
126 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
127 vnet_crypto_op_id_t op:16;
128 vnet_crypto_op_status_t status:8;
130 #define VNET_CRYPTO_OP_FLAG_INIT_IV (1 << 0)
131 #define VNET_CRYPTO_OP_FLAG_HMAC_CHECK (1 << 1)
135 u8 digest_len, tag_len;
147 vnet_crypto_op_type_t type;
148 vnet_crypto_alg_t alg;
149 u32 active_engine_index;
150 } vnet_crypto_op_data_t;
154 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
155 clib_bitmap_t *act_queues;
156 } vnet_crypto_thread_t;
158 typedef u32 vnet_crypto_key_index_t;
160 typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
161 vnet_crypto_op_t * ops[], u32 n_ops);
163 typedef void (vnet_crypto_key_handler_t) (vlib_main_t * vm,
164 vnet_crypto_key_op_t kop,
165 vnet_crypto_key_index_t idx);
167 u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
170 void 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);
173 void vnet_crypto_register_key_handler (vlib_main_t * vm, u32 engine_index,
174 vnet_crypto_key_handler_t * keyh);
181 vnet_crypto_key_handler_t *key_op_handler;
182 vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_IDS];
183 } vnet_crypto_engine_t;
187 vnet_crypto_alg_data_t *algs;
188 vnet_crypto_thread_t *threads;
189 vnet_crypto_ops_handler_t **ops_handlers;
190 vnet_crypto_op_data_t opt_data[VNET_CRYPTO_N_OP_IDS];
191 vnet_crypto_engine_t *engines;
192 vnet_crypto_key_t *keys;
193 uword *engine_index_by_name;
194 uword *alg_index_by_name;
195 } vnet_crypto_main_t;
197 extern vnet_crypto_main_t crypto_main;
199 u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
202 u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
205 int vnet_crypto_set_handler (char *ops_handler_name, char *engine);
207 u32 vnet_crypto_key_add (vlib_main_t * vm, vnet_crypto_alg_t alg,
208 u8 * data, u16 length);
209 void vnet_crypto_key_del (vlib_main_t * vm, vnet_crypto_key_index_t index);
210 void vnet_crypto_key_modify (vlib_main_t * vm, vnet_crypto_key_index_t index,
211 vnet_crypto_alg_t alg, u8 * data, u16 len);
213 format_function_t format_vnet_crypto_alg;
214 format_function_t format_vnet_crypto_engine;
215 format_function_t format_vnet_crypto_op;
216 format_function_t format_vnet_crypto_op_type;
217 format_function_t format_vnet_crypto_op_status;
218 unformat_function_t unformat_vnet_crypto_alg;
220 static_always_inline void
221 vnet_crypto_op_init (vnet_crypto_op_t * op, vnet_crypto_op_id_t type)
224 clib_memset (op, 0xfe, sizeof (*op));
230 static_always_inline vnet_crypto_op_type_t
231 vnet_crypto_get_op_type (vnet_crypto_op_id_t id)
233 vnet_crypto_main_t *cm = &crypto_main;
234 vnet_crypto_op_data_t *od = vec_elt_at_index (cm->opt_data, id);
238 static_always_inline vnet_crypto_key_t *
239 vnet_crypto_get_key (vnet_crypto_key_index_t index)
241 vnet_crypto_main_t *cm = &crypto_main;
242 return vec_elt_at_index (cm->keys, index);
245 #endif /* included_vnet_crypto_crypto_h */
248 * fd.io coding-style-patch-verification: ON
251 * eval: (c-set-style "gnu")