crypto-ipsecmb: more explicit errors reporting
[vpp.git] / src / plugins / crypto_ipsecmb / ipsecmb.c
1 /*
2  * ipsecmb.c - Intel IPSec Multi-buffer library Crypto Engine
3  *
4  * Copyright (c) 2019 Cisco Systemss
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <fcntl.h>
19
20 #include <intel-ipsec-mb.h>
21
22 #include <vnet/vnet.h>
23 #include <vnet/plugin/plugin.h>
24 #include <vpp/app/version.h>
25 #include <vnet/crypto/crypto.h>
26 #include <vppinfra/cpu.h>
27
28 #define HMAC_MAX_BLOCK_SIZE SHA_512_BLOCK_SIZE
29 #define EXPANDED_KEY_N_BYTES (16 * 15)
30
31 typedef struct
32 {
33   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
34   MB_MGR *mgr;
35   __m128i cbc_iv;
36 } ipsecmb_per_thread_data_t;
37
38 typedef struct
39 {
40   u16 data_size;
41   u8 block_size;
42   aes_gcm_pre_t aes_gcm_pre;
43   keyexp_t keyexp;
44   hash_one_block_t hash_one_block;
45   hash_fn_t hash_fn;
46 } ipsecmb_alg_data_t;
47
48 typedef struct ipsecmb_main_t_
49 {
50   ipsecmb_per_thread_data_t *per_thread_data;
51   ipsecmb_alg_data_t alg_data[VNET_CRYPTO_N_ALGS];
52   void **key_data;
53 } ipsecmb_main_t;
54
55 typedef struct
56 {
57   u8 enc_key_exp[EXPANDED_KEY_N_BYTES];
58   u8 dec_key_exp[EXPANDED_KEY_N_BYTES];
59 } ipsecmb_aes_cbc_key_data_t;
60
61 static ipsecmb_main_t ipsecmb_main = { };
62
63 /*
64  * (Alg, JOB_HASH_ALG, fn, block-size-bytes, hash-size-bytes, digest-size-bytes)
65  */
66 #define foreach_ipsecmb_hmac_op                                \
67   _(SHA1,   SHA1,    sha1,   64,  20, 20)                      \
68   _(SHA224, SHA_224, sha224, 64,  32, 28)                      \
69   _(SHA256, SHA_256, sha256, 64,  32, 32)                      \
70   _(SHA384, SHA_384, sha384, 128, 64, 48)                      \
71   _(SHA512, SHA_512, sha512, 128, 64, 64)
72
73 /*
74  * (Alg, key-len-bits)
75  */
76 #define foreach_ipsecmb_cbc_cipher_op                          \
77   _(AES_128_CBC, 128)                                          \
78   _(AES_192_CBC, 192)                                          \
79   _(AES_256_CBC, 256)
80
81 /*
82  * (Alg, key-len-bytes, iv-len-bytes)
83  */
84 #define foreach_ipsecmb_gcm_cipher_op                          \
85   _(AES_128_GCM, 128)                                          \
86   _(AES_192_GCM, 192)                                          \
87   _(AES_256_GCM, 256)
88
89 static_always_inline vnet_crypto_op_status_t
90 ipsecmb_status_job (JOB_STS status)
91 {
92   switch (status)
93     {
94     case STS_COMPLETED:
95       return VNET_CRYPTO_OP_STATUS_COMPLETED;
96     case STS_BEING_PROCESSED:
97     case STS_COMPLETED_AES:
98     case STS_COMPLETED_HMAC:
99       return VNET_CRYPTO_OP_STATUS_WORK_IN_PROGRESS;
100     case STS_INVALID_ARGS:
101     case STS_INTERNAL_ERROR:
102     case STS_ERROR:
103       return VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR;
104     }
105   ASSERT (0);
106   return VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR;
107 }
108
109 always_inline void
110 ipsecmb_retire_hmac_job (JOB_AES_HMAC * job, u32 * n_fail, u32 digest_size)
111 {
112   vnet_crypto_op_t *op = job->user_data;
113   u32 len = op->digest_len ? op->digest_len : digest_size;
114
115   if (PREDICT_FALSE (STS_COMPLETED != job->status))
116     {
117       op->status = ipsecmb_status_job (job->status);
118       *n_fail = *n_fail + 1;
119       return;
120     }
121
122   if (op->flags & VNET_CRYPTO_OP_FLAG_HMAC_CHECK)
123     {
124       if ((memcmp (op->digest, job->auth_tag_output, len)))
125         {
126           *n_fail = *n_fail + 1;
127           op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
128           return;
129         }
130     }
131   else if (len == digest_size)
132     clib_memcpy_fast (op->digest, job->auth_tag_output, digest_size);
133   else
134     clib_memcpy_fast (op->digest, job->auth_tag_output, len);
135
136   op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
137 }
138
139 static_always_inline u32
140 ipsecmb_ops_hmac_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[],
141                          u32 n_ops, u32 block_size, u32 hash_size,
142                          u32 digest_size, JOB_HASH_ALG alg)
143 {
144   ipsecmb_main_t *imbm = &ipsecmb_main;
145   ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,
146                                                      vm->thread_index);
147   JOB_AES_HMAC *job;
148   u32 i, n_fail = 0;
149   u8 scratch[n_ops][digest_size];
150
151   /*
152    * queue all the jobs first ...
153    */
154   for (i = 0; i < n_ops; i++)
155     {
156       vnet_crypto_op_t *op = ops[i];
157       u8 *kd = (u8 *) imbm->key_data[op->key_index];
158
159       job = IMB_GET_NEXT_JOB (ptd->mgr);
160
161       job->src = op->src;
162       job->hash_start_src_offset_in_bytes = 0;
163       job->msg_len_to_hash_in_bytes = op->len;
164       job->hash_alg = alg;
165       job->auth_tag_output_len_in_bytes = digest_size;
166       job->auth_tag_output = scratch[i];
167
168       job->cipher_mode = NULL_CIPHER;
169       job->cipher_direction = DECRYPT;
170       job->chain_order = HASH_CIPHER;
171
172       job->u.HMAC._hashed_auth_key_xor_ipad = kd;
173       job->u.HMAC._hashed_auth_key_xor_opad = kd + hash_size;
174       job->user_data = op;
175
176       job = IMB_SUBMIT_JOB (ptd->mgr);
177
178       if (job)
179         ipsecmb_retire_hmac_job (job, &n_fail, digest_size);
180     }
181
182   while ((job = IMB_FLUSH_JOB (ptd->mgr)))
183     ipsecmb_retire_hmac_job (job, &n_fail, digest_size);
184
185   return n_ops - n_fail;
186 }
187
188 #define _(a, b, c, d, e, f)                                             \
189 static_always_inline u32                                                \
190 ipsecmb_ops_hmac_##a (vlib_main_t * vm,                                 \
191                       vnet_crypto_op_t * ops[],                         \
192                       u32 n_ops)                                        \
193 { return ipsecmb_ops_hmac_inline (vm, ops, n_ops, d, e, f, b); }        \
194
195 foreach_ipsecmb_hmac_op;
196 #undef _
197
198 always_inline void
199 ipsecmb_retire_cipher_job (JOB_AES_HMAC * job, u32 * n_fail)
200 {
201   vnet_crypto_op_t *op = job->user_data;
202
203   if (PREDICT_FALSE (STS_COMPLETED != job->status))
204     {
205       op->status = ipsecmb_status_job (job->status);
206       *n_fail = *n_fail + 1;
207     }
208   else
209     op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
210 }
211
212 static_always_inline u32
213 ipsecmb_ops_cbc_cipher_inline (vlib_main_t * vm, vnet_crypto_op_t * ops[],
214                                u32 n_ops, u32 key_len,
215                                JOB_CIPHER_DIRECTION direction)
216 {
217   ipsecmb_main_t *imbm = &ipsecmb_main;
218   ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,
219                                                      vm->thread_index);
220   JOB_AES_HMAC *job;
221   u32 i, n_fail = 0;
222
223   for (i = 0; i < n_ops; i++)
224     {
225       ipsecmb_aes_cbc_key_data_t *kd;
226       vnet_crypto_op_t *op = ops[i];
227       kd = (ipsecmb_aes_cbc_key_data_t *) imbm->key_data[op->key_index];
228       __m128i iv;
229
230       job = IMB_GET_NEXT_JOB (ptd->mgr);
231
232       job->src = op->src;
233       job->dst = op->dst;
234       job->msg_len_to_cipher_in_bytes = op->len;
235       job->cipher_start_src_offset_in_bytes = 0;
236
237       job->hash_alg = NULL_HASH;
238       job->cipher_mode = CBC;
239       job->cipher_direction = direction;
240       job->chain_order = (direction == ENCRYPT ? CIPHER_HASH : HASH_CIPHER);
241
242       if ((direction == ENCRYPT) && (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV))
243         {
244           iv = ptd->cbc_iv;
245           _mm_storeu_si128 ((__m128i *) op->iv, iv);
246           ptd->cbc_iv = _mm_aesenc_si128 (iv, iv);
247         }
248
249       job->aes_key_len_in_bytes = key_len / 8;
250       job->aes_enc_key_expanded = kd->enc_key_exp;
251       job->aes_dec_key_expanded = kd->dec_key_exp;
252       job->iv = op->iv;
253       job->iv_len_in_bytes = AES_BLOCK_SIZE;
254
255       job->user_data = op;
256
257       job = IMB_SUBMIT_JOB (ptd->mgr);
258
259       if (job)
260         ipsecmb_retire_cipher_job (job, &n_fail);
261     }
262
263   while ((job = IMB_FLUSH_JOB (ptd->mgr)))
264     ipsecmb_retire_cipher_job (job, &n_fail);
265
266   return n_ops - n_fail;
267 }
268
269 #define _(a, b)                                                              \
270 static_always_inline u32                                                     \
271 ipsecmb_ops_cbc_cipher_enc_##a (vlib_main_t * vm,                            \
272                                 vnet_crypto_op_t * ops[],                    \
273                                 u32 n_ops)                                   \
274 { return ipsecmb_ops_cbc_cipher_inline (vm, ops, n_ops, b, ENCRYPT); }       \
275                                                                              \
276 static_always_inline u32                                                     \
277 ipsecmb_ops_cbc_cipher_dec_##a (vlib_main_t * vm,                            \
278                                 vnet_crypto_op_t * ops[],                    \
279                                 u32 n_ops)                                   \
280 { return ipsecmb_ops_cbc_cipher_inline (vm, ops, n_ops, b, DECRYPT); }       \
281
282 foreach_ipsecmb_cbc_cipher_op;
283 #undef _
284
285 #define _(a, b)                                                              \
286 static_always_inline u32                                                     \
287 ipsecmb_ops_gcm_cipher_enc_##a##_chained (vlib_main_t * vm,                  \
288     vnet_crypto_op_t * ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops)     \
289 {                                                                            \
290   ipsecmb_main_t *imbm = &ipsecmb_main;                                      \
291   ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,  \
292                                                      vm->thread_index);      \
293   MB_MGR *m = ptd->mgr;                                                      \
294   vnet_crypto_op_chunk_t *chp;                                               \
295   u32 i, j;                                                                  \
296                                                                              \
297   for (i = 0; i < n_ops; i++)                                                \
298     {                                                                        \
299       struct gcm_key_data *kd;                                               \
300       struct gcm_context_data ctx;                                           \
301       vnet_crypto_op_t *op = ops[i];                                         \
302                                                                              \
303       kd = (struct gcm_key_data *) imbm->key_data[op->key_index];            \
304       ASSERT (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS);              \
305       IMB_AES##b##_GCM_INIT(m, kd, &ctx, op->iv, op->aad, op->aad_len);      \
306       chp = chunks + op->chunk_index;                                        \
307       for (j = 0; j < op->n_chunks; j++)                                     \
308         {                                                                    \
309           IMB_AES##b##_GCM_ENC_UPDATE (m, kd, &ctx, chp->dst, chp->src,      \
310                                        chp->len);                            \
311           chp += 1;                                                          \
312         }                                                                    \
313       IMB_AES##b##_GCM_ENC_FINALIZE(m, kd, &ctx, op->tag, op->tag_len);      \
314                                                                              \
315       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;                          \
316     }                                                                        \
317                                                                              \
318   return n_ops;                                                              \
319 }                                                                            \
320                                                                              \
321 static_always_inline u32                                                     \
322 ipsecmb_ops_gcm_cipher_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[],  \
323                                 u32 n_ops)                                   \
324 {                                                                            \
325   ipsecmb_main_t *imbm = &ipsecmb_main;                                      \
326   ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,  \
327                                                      vm->thread_index);      \
328   MB_MGR *m = ptd->mgr;                                                      \
329   u32 i;                                                                     \
330                                                                              \
331   for (i = 0; i < n_ops; i++)                                                \
332     {                                                                        \
333       struct gcm_key_data *kd;                                               \
334       struct gcm_context_data ctx;                                           \
335       vnet_crypto_op_t *op = ops[i];                                         \
336                                                                              \
337       kd = (struct gcm_key_data *) imbm->key_data[op->key_index];            \
338       IMB_AES##b##_GCM_ENC (m, kd, &ctx, op->dst, op->src, op->len, op->iv,  \
339                             op->aad, op->aad_len, op->tag, op->tag_len);     \
340                                                                              \
341       op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;                          \
342     }                                                                        \
343                                                                              \
344   return n_ops;                                                              \
345 }                                                                            \
346                                                                              \
347 static_always_inline u32                                                     \
348 ipsecmb_ops_gcm_cipher_dec_##a##_chained (vlib_main_t * vm,                  \
349     vnet_crypto_op_t * ops[], vnet_crypto_op_chunk_t *chunks, u32 n_ops)     \
350 {                                                                            \
351   ipsecmb_main_t *imbm = &ipsecmb_main;                                      \
352   ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,  \
353                                                      vm->thread_index);      \
354   MB_MGR *m = ptd->mgr;                                                      \
355   vnet_crypto_op_chunk_t *chp;                                               \
356   u32 i, j, n_failed = 0;                                                    \
357                                                                              \
358   for (i = 0; i < n_ops; i++)                                                \
359     {                                                                        \
360       struct gcm_key_data *kd;                                               \
361       struct gcm_context_data ctx;                                           \
362       vnet_crypto_op_t *op = ops[i];                                         \
363       u8 scratch[64];                                                        \
364                                                                              \
365       kd = (struct gcm_key_data *) imbm->key_data[op->key_index];            \
366       ASSERT (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS);              \
367       IMB_AES##b##_GCM_INIT(m, kd, &ctx, op->iv, op->aad, op->aad_len);      \
368       chp = chunks + op->chunk_index;                                        \
369       for (j = 0; j < op->n_chunks; j++)                                     \
370         {                                                                    \
371           IMB_AES##b##_GCM_DEC_UPDATE (m, kd, &ctx, chp->dst, chp->src,      \
372                                        chp->len);                            \
373           chp += 1;                                                          \
374         }                                                                    \
375       IMB_AES##b##_GCM_DEC_FINALIZE(m, kd, &ctx, scratch, op->tag_len);      \
376                                                                              \
377       if ((memcmp (op->tag, scratch, op->tag_len)))                          \
378         {                                                                    \
379           op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;                  \
380           n_failed++;                                                        \
381         }                                                                    \
382       else                                                                   \
383         op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;                        \
384     }                                                                        \
385                                                                              \
386   return n_ops - n_failed;                                                   \
387 }                                                                            \
388                                                                              \
389 static_always_inline u32                                                     \
390 ipsecmb_ops_gcm_cipher_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[],  \
391                                  u32 n_ops)                                  \
392 {                                                                            \
393   ipsecmb_main_t *imbm = &ipsecmb_main;                                      \
394   ipsecmb_per_thread_data_t *ptd = vec_elt_at_index (imbm->per_thread_data,  \
395                                                      vm->thread_index);      \
396   MB_MGR *m = ptd->mgr;                                                      \
397   u32 i, n_failed = 0;                                                       \
398                                                                              \
399   for (i = 0; i < n_ops; i++)                                                \
400     {                                                                        \
401       struct gcm_key_data *kd;                                               \
402       struct gcm_context_data ctx;                                           \
403       vnet_crypto_op_t *op = ops[i];                                         \
404       u8 scratch[64];                                                        \
405                                                                              \
406       kd = (struct gcm_key_data *) imbm->key_data[op->key_index];            \
407       IMB_AES##b##_GCM_DEC (m, kd, &ctx, op->dst, op->src, op->len, op->iv,  \
408                             op->aad, op->aad_len, scratch, op->tag_len);     \
409                                                                              \
410       if ((memcmp (op->tag, scratch, op->tag_len)))                          \
411         {                                                                    \
412           op->status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;                  \
413           n_failed++;                                                        \
414         }                                                                    \
415       else                                                                   \
416         op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;                        \
417     }                                                                        \
418                                                                              \
419   return n_ops - n_failed;                                                   \
420 }
421
422 foreach_ipsecmb_gcm_cipher_op;
423 #undef _
424
425 clib_error_t *
426 crypto_ipsecmb_iv_init (ipsecmb_main_t * imbm)
427 {
428   ipsecmb_per_thread_data_t *ptd;
429   clib_error_t *err = 0;
430   int fd;
431
432   if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
433     return clib_error_return_unix (0, "failed to open '/dev/urandom'");
434
435   vec_foreach (ptd, imbm->per_thread_data)
436   {
437     if (read (fd, &ptd->cbc_iv, sizeof (ptd->cbc_iv)) != sizeof (ptd->cbc_iv))
438       {
439         err = clib_error_return_unix (0, "'/dev/urandom' read failure");
440         close (fd);
441         return (err);
442       }
443   }
444
445   close (fd);
446   return (NULL);
447 }
448
449 static void
450 crypto_ipsecmb_key_handler (vlib_main_t * vm, vnet_crypto_key_op_t kop,
451                             vnet_crypto_key_index_t idx)
452 {
453   ipsecmb_main_t *imbm = &ipsecmb_main;
454   vnet_crypto_key_t *key = vnet_crypto_get_key (idx);
455   ipsecmb_alg_data_t *ad = imbm->alg_data + key->alg;
456   u32 i;
457   void *kd;
458
459   /** TODO: add linked alg support **/
460   if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
461     return;
462
463   if (kop == VNET_CRYPTO_KEY_OP_DEL)
464     {
465       if (idx >= vec_len (imbm->key_data))
466         return;
467
468       if (imbm->key_data[idx] == 0)
469         return;
470
471       clib_mem_free_s (imbm->key_data[idx]);
472       imbm->key_data[idx] = 0;
473       return;
474     }
475
476   if (ad->data_size == 0)
477     return;
478
479   vec_validate_aligned (imbm->key_data, idx, CLIB_CACHE_LINE_BYTES);
480
481   if (kop == VNET_CRYPTO_KEY_OP_MODIFY && imbm->key_data[idx])
482     {
483       clib_mem_free_s (imbm->key_data[idx]);
484     }
485
486   kd = imbm->key_data[idx] = clib_mem_alloc_aligned (ad->data_size,
487                                                      CLIB_CACHE_LINE_BYTES);
488
489   /* AES CBC key expansion */
490   if (ad->keyexp)
491     {
492       ad->keyexp (key->data, ((ipsecmb_aes_cbc_key_data_t *) kd)->enc_key_exp,
493                   ((ipsecmb_aes_cbc_key_data_t *) kd)->dec_key_exp);
494       return;
495     }
496
497   /* AES GCM */
498   if (ad->aes_gcm_pre)
499     {
500       ad->aes_gcm_pre (key->data, (struct gcm_key_data *) kd);
501       return;
502     }
503
504   /* HMAC */
505   if (ad->hash_one_block)
506     {
507       const int block_qw = HMAC_MAX_BLOCK_SIZE / sizeof (u64);
508       u64 pad[block_qw], key_hash[block_qw];
509
510       clib_memset_u8 (key_hash, 0, HMAC_MAX_BLOCK_SIZE);
511       if (vec_len (key->data) <= ad->block_size)
512         clib_memcpy_fast (key_hash, key->data, vec_len (key->data));
513       else
514         ad->hash_fn (key->data, vec_len (key->data), key_hash);
515
516       for (i = 0; i < block_qw; i++)
517         pad[i] = key_hash[i] ^ 0x3636363636363636;
518       ad->hash_one_block (pad, kd);
519
520       for (i = 0; i < block_qw; i++)
521         pad[i] = key_hash[i] ^ 0x5c5c5c5c5c5c5c5c;
522       ad->hash_one_block (pad, ((u8 *) kd) + (ad->data_size / 2));
523
524       return;
525     }
526 }
527
528 static clib_error_t *
529 crypto_ipsecmb_init (vlib_main_t * vm)
530 {
531   ipsecmb_main_t *imbm = &ipsecmb_main;
532   ipsecmb_alg_data_t *ad;
533   ipsecmb_per_thread_data_t *ptd;
534   vlib_thread_main_t *tm = vlib_get_thread_main ();
535   clib_error_t *error;
536   MB_MGR *m = 0;
537   u32 eidx;
538   u8 *name;
539
540   if (!clib_cpu_supports_aes ())
541     return 0;
542
543   /*
544    * A priority that is better than OpenSSL but worse than VPP natvie
545    */
546   name = format (0, "Intel(R) Multi-Buffer Crypto for IPsec Library %s%c",
547                  IMB_VERSION_STR, 0);
548   eidx = vnet_crypto_register_engine (vm, "ipsecmb", 80, (char *) name);
549
550   vec_validate_aligned (imbm->per_thread_data, tm->n_vlib_mains - 1,
551                         CLIB_CACHE_LINE_BYTES);
552
553   /* *INDENT-OFF* */
554   vec_foreach (ptd, imbm->per_thread_data)
555     {
556         ptd->mgr = alloc_mb_mgr (0);
557         if (clib_cpu_supports_avx512f ())
558           init_mb_mgr_avx512 (ptd->mgr);
559         else if (clib_cpu_supports_avx2 ())
560           init_mb_mgr_avx2 (ptd->mgr);
561         else
562           init_mb_mgr_sse (ptd->mgr);
563
564         if (ptd == imbm->per_thread_data)
565           m = ptd->mgr;
566     }
567   /* *INDENT-ON* */
568
569   if (clib_cpu_supports_x86_aes () && (error = crypto_ipsecmb_iv_init (imbm)))
570     return (error);
571
572 #define _(a, b, c, d, e, f)                                              \
573   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
574                                     ipsecmb_ops_hmac_##a);               \
575   ad = imbm->alg_data + VNET_CRYPTO_ALG_HMAC_##a;                        \
576   ad->block_size = d;                                                    \
577   ad->data_size = e * 2;                                                 \
578   ad->hash_one_block = m-> c##_one_block;                                \
579   ad->hash_fn = m-> c;                                                   \
580
581   foreach_ipsecmb_hmac_op;
582 #undef _
583 #define _(a, b)                                                         \
584   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
585                                     ipsecmb_ops_cbc_cipher_enc_##a);    \
586   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
587                                     ipsecmb_ops_cbc_cipher_dec_##a);    \
588   ad = imbm->alg_data + VNET_CRYPTO_ALG_##a;                            \
589   ad->data_size = sizeof (ipsecmb_aes_cbc_key_data_t);                  \
590   ad->keyexp = m->keyexp_##b;                                           \
591
592   foreach_ipsecmb_cbc_cipher_op;
593 #undef _
594 #define _(a, b)                                                         \
595   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
596                                     ipsecmb_ops_gcm_cipher_enc_##a);    \
597   vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
598                                     ipsecmb_ops_gcm_cipher_dec_##a);    \
599   vnet_crypto_register_chained_ops_handler                              \
600       (vm, eidx, VNET_CRYPTO_OP_##a##_ENC,                              \
601        ipsecmb_ops_gcm_cipher_enc_##a##_chained);                       \
602   vnet_crypto_register_chained_ops_handler                              \
603       (vm, eidx, VNET_CRYPTO_OP_##a##_DEC,                              \
604        ipsecmb_ops_gcm_cipher_dec_##a##_chained);                       \
605   ad = imbm->alg_data + VNET_CRYPTO_ALG_##a;                            \
606   ad->data_size = sizeof (struct gcm_key_data);                         \
607   ad->aes_gcm_pre = m->gcm##b##_pre;                                    \
608
609   foreach_ipsecmb_gcm_cipher_op;
610 #undef _
611
612   vnet_crypto_register_key_handler (vm, eidx, crypto_ipsecmb_key_handler);
613   return (NULL);
614 }
615
616 /* *INDENT-OFF* */
617 VLIB_INIT_FUNCTION (crypto_ipsecmb_init) =
618 {
619   .runs_after = VLIB_INITS ("vnet_crypto_init"),
620 };
621 /* *INDENT-ON* */
622
623 /* *INDENT-OFF* */
624 VLIB_PLUGIN_REGISTER () =
625 {
626   .version = VPP_BUILD_VER,
627   .description = "Intel IPSEC Multi-buffer Crypto Engine",
628 };
629 /* *INDENT-ON* */
630
631 /*
632  * fd.io coding-style-patch-verification: ON
633  *
634  * Local Variables:
635  * eval: (c-set-style "gnu")
636  * End:
637  */