ipsec: add support for RFC-4543 ENCR_NULL_AUTH_AES_GMAC
[vpp.git] / src / plugins / unittest / crypto_test.c
1 /*
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 #include <vlib/vlib.h>
16 #include <vppinfra/time.h>
17 #include <vppinfra/cache.h>
18 #include <vppinfra/error.h>
19 #include <vnet/crypto/crypto.h>
20 #include <unittest/crypto/crypto.h>
21
22 crypto_test_main_t crypto_test_main;
23
24 static int
25 sort_registrations (void *a0, void *a1)
26 {
27   unittest_crypto_test_registration_t **r0 = a0;
28   unittest_crypto_test_registration_t **r1 = a1;
29
30   return (strncmp (r0[0]->name, r1[0]->name, 256));
31 }
32
33 static void
34 print_results (vlib_main_t * vm, unittest_crypto_test_registration_t ** rv,
35                vnet_crypto_op_t * ops, vnet_crypto_op_chunk_t * chunks,
36                u32 n_ops, crypto_test_main_t * tm)
37 {
38   int i;
39   unittest_crypto_test_registration_t *r;
40   vnet_crypto_op_chunk_t *chp;
41   u8 *s = 0, *err = 0;
42   vnet_crypto_op_t *op;
43
44   vec_foreach (op, ops)
45   {
46     int fail = 0;
47     r = rv[op->user_data];
48     unittest_crypto_test_data_t *exp_pt = 0, *exp_ct = 0, exp_pt_data;
49     unittest_crypto_test_data_t *exp_digest = 0, *exp_tag = 0;
50     unittest_crypto_test_data_t *exp_pt_chunks = 0, *exp_ct_chunks = 0;
51
52     switch (vnet_crypto_get_op_type (op->op))
53       {
54       case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
55         exp_tag = &r->tag;
56         /* fall through */
57       case VNET_CRYPTO_OP_TYPE_ENCRYPT:
58         exp_ct = &r->ciphertext;
59         exp_ct_chunks = r->ct_chunks;
60         break;
61       case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
62       case VNET_CRYPTO_OP_TYPE_DECRYPT:
63         if (r->plaintext_incremental)
64           {
65             exp_pt_data.length = r->plaintext_incremental;
66             exp_pt_data.data = tm->inc_data;
67             exp_pt = &exp_pt_data;
68           }
69         else
70           {
71             exp_pt = &r->plaintext;
72             exp_pt_chunks = r->pt_chunks;
73           }
74         break;
75       case VNET_CRYPTO_OP_TYPE_HMAC:
76         exp_digest = &r->digest;
77         break;
78       case VNET_CRYPTO_OP_TYPE_HASH:
79         exp_digest = &r->digest;
80         break;
81       default:
82         ASSERT (0);
83       }
84
85     vec_reset_length (err);
86
87     if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
88       err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
89                     format_vnet_crypto_op_status, op->status);
90
91     if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
92       {
93         if (exp_ct_chunks)
94           {
95             chp = vec_elt_at_index (chunks, op->chunk_index);
96             for (i = 0; i < op->n_chunks; i++)
97               {
98                 if (memcmp (chp->dst, exp_ct_chunks[i].data, chp->len))
99                   err = format (err, "%sciphertext mismatch [chunk %d]",
100                                 vec_len (err) ? ", " : "", i);
101                 chp += 1;
102               }
103           }
104
105         if (exp_pt_chunks)
106           {
107             chp = vec_elt_at_index (chunks, op->chunk_index);
108             for (i = 0; i < op->n_chunks; i++)
109               {
110                 if (memcmp (chp->dst, exp_pt_chunks[i].data, chp->len))
111                   err = format (err, "%splaintext mismatch [chunk %d]",
112                                 vec_len (err) ? ", " : "", i);
113                 chp += 1;
114               }
115           }
116       }
117     else
118       {
119         if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
120           err = format (err, "%sciphertext mismatch",
121                         vec_len (err) ? ", " : "");
122
123         if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
124           err = format (err, "%splaintext mismatch",
125                         vec_len (err) ? ", " : "");
126       }
127
128     if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
129       err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
130
131     if (exp_digest &&
132         memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
133       err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
134
135     vec_reset_length (s);
136     s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op,
137                 r->is_chained);
138
139     if (vec_len (err))
140       fail = 1;
141
142     vlib_cli_output (vm, "%-65v%s%v", s, vec_len (err) ? "FAIL: " : "OK", err);
143     if (tm->verbose)
144       {
145         if (tm->verbose == 2)
146           fail = 1;
147
148         if (exp_ct && fail)
149           vlib_cli_output (vm, "Expected ciphertext:\n%U"
150                            "\nCalculated ciphertext:\n%U",
151                            format_hexdump, exp_ct->data, exp_ct->length,
152                            format_hexdump, op->dst, exp_ct->length);
153         if (exp_pt && fail)
154           vlib_cli_output (vm, "Expected plaintext:\n%U"
155                            "\nCalculated plaintext:\n%U",
156                            format_hexdump, exp_pt->data, exp_pt->length,
157                            format_hexdump, op->dst, exp_pt->length);
158         if (r->tag.length && fail)
159           vlib_cli_output (vm, "Expected tag:\n%U"
160                            "\nCalculated tag:\n%U",
161                            format_hexdump, r->tag.data, r->tag.length,
162                            format_hexdump, op->tag, op->tag_len);
163         if (exp_digest && fail)
164           vlib_cli_output (vm, "Expected digest:\n%U"
165                            "\nCalculated Digest:\n%U",
166                            format_hexdump, exp_digest->data,
167                            exp_digest->length, format_hexdump, op->digest,
168                            op->digest_len);
169       }
170   }
171   vec_free (err);
172   vec_free (s);
173 }
174
175 static void
176 validate_data (u8 ** data, u32 len)
177 {
178   u32 i, diff, old_len;
179   if (vec_len (data[0]) >= len)
180     return;
181
182   old_len = vec_len (data[0]);
183   diff = len - vec_len (data[0]);
184   vec_validate (data[0], old_len + diff - 1);
185   for (i = old_len; i < len; i++)
186     data[0][i] = (u8) i;
187 }
188
189 static void
190 generate_digest (vlib_main_t * vm,
191                  unittest_crypto_test_registration_t * r,
192                  vnet_crypto_op_id_t id)
193 {
194   crypto_test_main_t *cm = &crypto_test_main;
195   vnet_crypto_op_t op[1];
196   vnet_crypto_op_init (op, id);
197   vec_validate (r->digest.data, r->digest.length - 1);
198   op->src = cm->inc_data;
199   op->len = r->plaintext_incremental;
200   op->digest = r->digest.data;
201   op->digest_len = r->digest.length;
202   op->key_index = vnet_crypto_key_add (vm, r->alg,
203                                        cm->inc_data, r->key.length);
204
205   /* at this point openssl is set for each algo */
206   vnet_crypto_process_ops (vm, op, 1);
207 }
208
209 static int
210 restore_engines (u32 * engs)
211 {
212   vnet_crypto_main_t *cm = &crypto_main;
213   u32 i;
214   vnet_crypto_engine_t *ce;
215
216   for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
217     {
218       vnet_crypto_op_data_t *od = &cm->opt_data[i];
219
220       if (engs[i] != ~0)
221         {
222           ce = vec_elt_at_index (cm->engines, engs[i]);
223           od->active_engine_index_simple = engs[i];
224           cm->ops_handlers[i] = ce->ops_handlers[i];
225         }
226     }
227
228   return 0;
229 }
230
231 static int
232 save_current_engines (u32 * engs)
233 {
234   vnet_crypto_main_t *cm = &crypto_main;
235   uword *p;
236   u32 i;
237   vnet_crypto_engine_t *ce;
238
239   p = hash_get_mem (cm->engine_index_by_name, "openssl");
240   if (!p)
241     return -1;
242
243   ce = vec_elt_at_index (cm->engines, p[0]);
244
245   /* set openssl for all crypto algs to generate expected data */
246   for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
247     {
248       vnet_crypto_op_data_t *od = &cm->opt_data[i];
249       if (od->active_engine_index_simple != ~0)
250         {
251           /* save engine index */
252           engs[i] = od->active_engine_index_simple;
253           od->active_engine_index_simple = ce - cm->engines;
254           cm->ops_handlers[i] = ce->ops_handlers[i];
255         }
256     }
257
258   return 0;
259 }
260
261 static clib_error_t *
262 test_crypto_incremental (vlib_main_t * vm, crypto_test_main_t * tm,
263                          unittest_crypto_test_registration_t ** rv, u32 n_ops,
264                          u32 computed_data_total_len)
265 {
266   vnet_crypto_main_t *cm = &crypto_main;
267   vnet_crypto_alg_data_t *ad;
268   vnet_crypto_key_index_t *key_indices = 0;
269   u32 i;
270   unittest_crypto_test_registration_t *r;
271   vnet_crypto_op_t *ops = 0, *op;
272   u8 *encrypted_data = 0, *decrypted_data = 0, *s = 0, *err = 0;
273
274   if (n_ops == 0)
275     return 0;
276
277   vec_validate_aligned (encrypted_data, computed_data_total_len - 1,
278                         CLIB_CACHE_LINE_BYTES);
279   vec_validate_aligned (decrypted_data, computed_data_total_len - 1,
280                         CLIB_CACHE_LINE_BYTES);
281   vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
282   computed_data_total_len = 0;
283
284   op = ops;
285   /* first stage: encrypt only */
286
287   vec_foreach_index (i, rv)
288   {
289     r = rv[i];
290     int t;
291     ad = vec_elt_at_index (cm->algs, r->alg);
292     for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
293       {
294         vnet_crypto_op_id_t id = ad->op_by_type[t];
295
296         if (id == 0)
297           continue;
298
299         switch (t)
300           {
301           case VNET_CRYPTO_OP_TYPE_ENCRYPT:
302             vnet_crypto_op_init (op, id);
303             op->iv = tm->inc_data;
304             op->key_index = vnet_crypto_key_add (vm, r->alg,
305                                                  tm->inc_data, r->key.length);
306             vec_add1 (key_indices, op->key_index);
307             op->len = r->plaintext_incremental;
308             op->src = tm->inc_data;
309             op->dst = encrypted_data + computed_data_total_len;
310             computed_data_total_len += r->plaintext_incremental;
311             op->user_data = i;
312             op++;
313             break;
314           case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
315             vnet_crypto_op_init (op, id);
316             op->iv = tm->inc_data;
317             op->key_index = vnet_crypto_key_add (vm, r->alg,
318                                                  tm->inc_data, r->key.length);
319             vec_add1 (key_indices, op->key_index);
320             op->aad = tm->inc_data;
321             op->aad_len = r->aad.length;
322             op->len = r->plaintext_incremental;
323             op->dst = encrypted_data + computed_data_total_len;
324             computed_data_total_len += r->plaintext_incremental;
325             op->src = tm->inc_data;
326             op->tag = encrypted_data + computed_data_total_len;
327             computed_data_total_len += r->tag.length;
328             op->tag_len = r->tag.length;
329             op->user_data = i;
330             op++;
331             break;
332           case VNET_CRYPTO_OP_TYPE_HMAC:
333             /* compute hmac in the next stage */
334             op->op = VNET_CRYPTO_OP_NONE;
335             computed_data_total_len += r->digest.length;
336             op->user_data = i;
337             op++;
338             break;
339           default:
340             break;
341           };
342       }
343   }
344
345   vnet_crypto_process_ops (vm, ops, n_ops);
346   computed_data_total_len = 0;
347
348   /* second stage: hash/decrypt previously encrypted data */
349   op = ops;
350
351   vec_foreach_index (i, rv)
352   {
353     r = rv[i];
354     int t;
355     ad = vec_elt_at_index (cm->algs, r->alg);
356     for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
357       {
358         vnet_crypto_op_id_t id = ad->op_by_type[t];
359
360         if (id == 0)
361           continue;
362
363         switch (t)
364           {
365           case VNET_CRYPTO_OP_TYPE_DECRYPT:
366             vnet_crypto_op_init (op, id);
367             op->iv = tm->inc_data;
368             op->key_index = vnet_crypto_key_add (vm, r->alg,
369                                                  tm->inc_data, r->key.length);
370             vec_add1 (key_indices, op->key_index);
371             op->len = r->plaintext_incremental;
372             op->src = encrypted_data + computed_data_total_len;
373             op->dst = decrypted_data + computed_data_total_len;
374             computed_data_total_len += r->plaintext_incremental;
375             op->user_data = i;
376             op++;
377             break;
378           case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
379             vnet_crypto_op_init (op, id);
380             op->iv = tm->inc_data;
381             op->key_index = vnet_crypto_key_add (vm, r->alg,
382                                                  tm->inc_data, r->key.length);
383             vec_add1 (key_indices, op->key_index);
384             op->aad = tm->inc_data;
385             op->aad_len = r->aad.length;
386             op->len = r->plaintext_incremental;
387             op->dst = decrypted_data + computed_data_total_len;
388             op->src = encrypted_data + computed_data_total_len;
389             computed_data_total_len += r->plaintext_incremental;
390
391             op->tag = encrypted_data + computed_data_total_len;
392             computed_data_total_len += r->tag.length;
393             op->tag_len = r->tag.length;
394             op->user_data = i;
395             op++;
396             break;
397           case VNET_CRYPTO_OP_TYPE_HMAC:
398             vnet_crypto_op_init (op, id);
399             op->key_index = vnet_crypto_key_add (vm, r->alg,
400                                                  tm->inc_data, r->key.length);
401             vec_add1 (key_indices, op->key_index);
402             op->src = tm->inc_data;
403             op->len = r->plaintext_incremental;
404             op->digest_len = r->digest.length;
405             op->digest = encrypted_data + computed_data_total_len;
406             computed_data_total_len += r->digest.length;
407             op->user_data = i;
408             op++;
409             break;
410           default:
411             break;
412           };
413
414       }
415   }
416
417   vnet_crypto_process_ops (vm, ops, n_ops);
418   print_results (vm, rv, ops, 0, n_ops, tm);
419
420   vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
421   vec_free (tm->inc_data);
422   vec_free (ops);
423   vec_free (encrypted_data);
424   vec_free (decrypted_data);
425   vec_free (err);
426   vec_free (s);
427   return 0;
428 }
429
430 static clib_error_t *
431 test_crypto_static (vlib_main_t * vm, crypto_test_main_t * tm,
432                     unittest_crypto_test_registration_t ** rv, u32 n_ops,
433                     u32 n_chained_ops, u32 computed_data_total_len)
434 {
435   unittest_crypto_test_data_t *pt, *ct;
436   vnet_crypto_op_chunk_t *chunks = 0, ch;
437   unittest_crypto_test_registration_t *r;
438   vnet_crypto_op_t *ops = 0, *op, *chained_ops = 0;
439   vnet_crypto_op_t *current_chained_op = 0, *current_op = 0;
440   vnet_crypto_main_t *cm = &crypto_main;
441   vnet_crypto_alg_data_t *ad;
442   vnet_crypto_key_index_t *key_indices = 0;
443   u8 *computed_data = 0;
444   u32 i;
445
446   vec_sort_with_function (rv, sort_registrations);
447
448   vec_validate_aligned (computed_data, computed_data_total_len - 1,
449                         CLIB_CACHE_LINE_BYTES);
450   vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
451   vec_validate_aligned (chained_ops, n_chained_ops - 1,
452                         CLIB_CACHE_LINE_BYTES);
453   computed_data_total_len = 0;
454
455   current_op = ops;
456   current_chained_op = chained_ops;
457   /* *INDENT-OFF* */
458   vec_foreach_index (i, rv)
459     {
460       r = rv[i];
461       int t;
462       ad = vec_elt_at_index (cm->algs, r->alg);
463       for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
464         {
465           vnet_crypto_op_id_t id = ad->op_by_type[t];
466
467           if (id == 0)
468             continue;
469
470           if (r->is_chained)
471           {
472             op = current_chained_op;
473             current_chained_op += 1;
474           }
475           else
476           {
477             op = current_op;
478             current_op += 1;
479           }
480
481           vnet_crypto_op_init (op, id);
482
483           switch (t)
484             {
485             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
486             case VNET_CRYPTO_OP_TYPE_DECRYPT:
487               op->iv = r->iv.data;
488               op->key_index = vnet_crypto_key_add (vm, r->alg,
489                                                    r->key.data,
490                                                    r->key.length);
491               vec_add1 (key_indices, op->key_index);
492
493               if (r->is_chained)
494               {
495               pt = r->pt_chunks;
496               ct = r->ct_chunks;
497               op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
498               op->chunk_index = vec_len (chunks);
499               while (pt->data)
500                 {
501                   ch.src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
502                     pt->data : ct->data;
503                   ch.len = pt->length;
504                   ch.dst = computed_data + computed_data_total_len;
505                   computed_data_total_len += pt->length;
506                   vec_add1 (chunks, ch);
507                   op->n_chunks++;
508                   pt++;
509                   ct++;
510                 }
511               }
512               else
513               {
514               op->len = r->plaintext.length;
515               op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
516                 r->plaintext.data : r->ciphertext.data;
517               op->dst = computed_data + computed_data_total_len;
518               computed_data_total_len += r->ciphertext.length;
519               }
520               break;
521             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
522             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
523               if (r->is_chained)
524               {
525               op->iv = r->iv.data;
526               op->key_index = vnet_crypto_key_add (vm, r->alg,
527                                                    r->key.data,
528                                                    r->key.length);
529               vec_add1 (key_indices, op->key_index);
530               op->aad = r->aad.data;
531               op->aad_len = r->aad.length;
532               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
533                 {
534                   pt = r->pt_chunks;
535                   op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
536                   op->chunk_index = vec_len (chunks);
537                   while (pt->data)
538                     {
539                       clib_memset (&ch, 0, sizeof (ch));
540                       ch.src = pt->data;
541                       ch.len = pt->length;
542                       ch.dst = computed_data + computed_data_total_len;
543                       computed_data_total_len += pt->length;
544                       vec_add1 (chunks, ch);
545                       op->n_chunks++;
546                       pt++;
547                     }
548                   op->tag = computed_data + computed_data_total_len;
549                   computed_data_total_len += r->tag.length;
550                 }
551               else
552                 {
553                   ct = r->ct_chunks;
554                   op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
555                   op->chunk_index = vec_len (chunks);
556                   while (ct->data)
557                     {
558                       clib_memset (&ch, 0, sizeof (ch));
559                       ch.src = ct->data;
560                       ch.len = ct->length;
561                       ch.dst = computed_data + computed_data_total_len;
562                       computed_data_total_len += ct->length;
563                       vec_add1 (chunks, ch);
564                       op->n_chunks++;
565                       ct++;
566                     }
567                   op->tag = r->tag.data;
568                 }
569               op->tag_len = r->tag.length;
570               }
571               else
572               {
573               op->iv = r->iv.data;
574               op->key_index = vnet_crypto_key_add (vm, r->alg,
575                                                    r->key.data,
576                                                    r->key.length);
577               vec_add1 (key_indices, op->key_index);
578               op->aad = r->aad.data;
579               op->aad_len = r->aad.length;
580               op->len = r->plaintext.length;
581               op->dst = computed_data + computed_data_total_len;
582               computed_data_total_len += r->ciphertext.length;
583
584               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
585                 {
586                   op->src = r->plaintext.data;
587                   op->tag = computed_data + computed_data_total_len;
588                   computed_data_total_len += r->tag.length;
589                 }
590               else
591                 {
592                   op->tag = r->tag.data;
593                   op->src = r->ciphertext.data;
594                 }
595               op->tag_len = r->tag.length;
596               }
597               break;
598             case VNET_CRYPTO_OP_TYPE_HMAC:
599               if (r->is_chained)
600               {
601               op->key_index = vnet_crypto_key_add (vm, r->alg,
602                                                    r->key.data,
603                                                    r->key.length);
604               vec_add1 (key_indices, op->key_index);
605               op->digest_len = r->digest.length;
606               op->digest = computed_data + computed_data_total_len;
607               computed_data_total_len += r->digest.length;
608               pt = r->pt_chunks;
609               op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
610               op->chunk_index = vec_len (chunks);
611               while (pt->data)
612                 {
613                   clib_memset (&ch, 0, sizeof (ch));
614                   ch.src = pt->data;
615                   ch.len = pt->length;
616                   vec_add1 (chunks, ch);
617                   op->n_chunks++;
618                   pt++;
619                 }
620               }
621               else
622               {
623               op->key_index = vnet_crypto_key_add (vm, r->alg,
624                                                    r->key.data,
625                                                    r->key.length);
626               vec_add1 (key_indices, op->key_index);
627               op->digest_len = r->digest.length;
628               op->digest = computed_data + computed_data_total_len;
629               computed_data_total_len += r->digest.length;
630               op->src = r->plaintext.data;
631               op->len = r->plaintext.length;
632               }
633               break;
634             case VNET_CRYPTO_OP_TYPE_HASH:
635               op->digest = computed_data + computed_data_total_len;
636               computed_data_total_len += r->digest.length;
637               op->src = r->plaintext.data;
638               op->len = r->plaintext.length;
639               break;
640             default:
641               break;
642             };
643
644           op->user_data = i;
645         }
646     }
647   /* *INDENT-ON* */
648
649   vnet_crypto_process_ops (vm, ops, vec_len (ops));
650   vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
651                                    vec_len (chained_ops));
652
653   print_results (vm, rv, ops, chunks, vec_len (ops), tm);
654   print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
655
656   vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
657
658   vec_free (computed_data);
659   vec_free (ops);
660   vec_free (chained_ops);
661   vec_free (chunks);
662   return 0;
663 }
664
665 static u32
666 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
667 {
668   switch (alg)
669     {
670 #define _(n, s, l) \
671   case VNET_CRYPTO_ALG_##n: \
672     return l;
673   /* *INDENT-OFF* */
674   foreach_crypto_cipher_alg
675   foreach_crypto_aead_alg
676   /* *INDENT-ON* */
677 #undef _
678     case VNET_CRYPTO_ALG_HMAC_MD5:
679     case VNET_CRYPTO_ALG_HMAC_SHA1:
680       return 20;
681     case VNET_CRYPTO_ALG_HMAC_SHA224:
682       return 28;
683     case VNET_CRYPTO_ALG_HMAC_SHA256:
684       return 32;
685     case VNET_CRYPTO_ALG_HMAC_SHA384:
686       return 48;
687     case VNET_CRYPTO_ALG_HMAC_SHA512:
688       return 64;
689     default:
690       return 0;
691     }
692   return 0;
693 }
694
695 static clib_error_t *
696 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
697 {
698   clib_error_t *err = 0;
699   vnet_crypto_main_t *cm = &crypto_main;
700   unittest_crypto_test_registration_t *r = tm->test_registrations;
701   unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
702   u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
703   vnet_crypto_alg_data_t *ad;
704   u32 computed_data_total_len = 0;
705   u32 computed_data_total_incr_len = 0;
706   u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
707   unittest_crypto_test_data_t *ct;
708
709   /* pre-allocate plaintext data with reasonable length */
710   validate_data (&tm->inc_data, 2048);
711
712   int rc = save_current_engines (saved_engs);
713   if (rc)
714     return clib_error_return (0, "failed to set default crypto engine!");
715
716   /* construct registration vector */
717   while (r)
718     {
719       if (r->plaintext_incremental)
720         vec_add1 (inc_tests, r);
721       else
722         vec_add1 (static_tests, r);
723
724       ad = vec_elt_at_index (cm->algs, r->alg);
725
726       for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
727         {
728           vnet_crypto_op_id_t id = ad->op_by_type[i];
729
730           if (id == 0)
731             continue;
732
733           switch (i)
734             {
735             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
736               if (r->plaintext_incremental)
737                 {
738                   computed_data_total_incr_len += r->plaintext_incremental;
739                   n_ops_incr += 1;
740                 }
741               /* fall though */
742             case VNET_CRYPTO_OP_TYPE_DECRYPT:
743             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
744               if (r->is_chained)
745                 {
746                   ct = r->ct_chunks;
747                   j = 0;
748                   while (ct->data)
749                     {
750                       if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
751                         return clib_error_return (0,
752                                                   "test case '%s' exceeds extra data!",
753                                                   r->name);
754                       computed_data_total_len += ct->length;
755                       ct++;
756                       j++;
757                     }
758                   n_chained_ops += 1;
759                 }
760               else if (!r->plaintext_incremental)
761                 {
762                   computed_data_total_len += r->ciphertext.length;
763                   n_ops_static += 1;
764                 }
765               break;
766             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
767               if (r->plaintext_incremental)
768                 {
769                   computed_data_total_incr_len += r->plaintext_incremental;
770                   computed_data_total_incr_len += r->tag.length;
771                   n_ops_incr += 1;
772                 }
773               else
774                 {
775                   computed_data_total_len += r->ciphertext.length;
776                   computed_data_total_len += r->tag.length;
777                   if (r->is_chained)
778                     {
779                       ct = r->ct_chunks;
780                       j = 0;
781                       while (ct->data)
782                         {
783                           if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
784                             return clib_error_return (0,
785                                                       "test case '%s' exceeds extra data!",
786                                                       r->name);
787                           computed_data_total_len += ct->length;
788                           ct++;
789                           j++;
790                         }
791                       n_chained_ops += 1;
792                     }
793                   else
794                     n_ops_static += 1;
795                 }
796               break;
797             case VNET_CRYPTO_OP_TYPE_HMAC:
798               if (r->plaintext_incremental)
799                 {
800                   computed_data_total_incr_len += r->digest.length;
801                   n_ops_incr += 1;
802                   generate_digest (vm, r, id);
803                 }
804               else
805                 {
806                   computed_data_total_len += r->digest.length;
807                   if (r->is_chained)
808                     n_chained_ops += 1;
809                   else
810                     n_ops_static += 1;
811                 }
812               break;
813             case VNET_CRYPTO_OP_TYPE_HASH:
814               computed_data_total_len += r->digest.length;
815               n_ops_static += 1;
816               break;
817             default:
818               break;
819             };
820         }
821
822       /* next: */
823       r = r->next;
824     }
825   restore_engines (saved_engs);
826
827   err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
828                             computed_data_total_len);
829   if (err)
830     goto done;
831
832   err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
833                                  computed_data_total_incr_len);
834
835   r = tm->test_registrations;
836   while (r)
837     {
838       if (r->plaintext_incremental)
839         vec_free (r->digest.data);
840       r = r->next;
841     }
842
843 done:
844   vec_free (inc_tests);
845   vec_free (static_tests);
846   return err;
847 }
848
849 static clib_error_t *
850 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
851 {
852   vnet_crypto_main_t *cm = &crypto_main;
853   clib_error_t *err = 0;
854   u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
855   u32 *buffer_indices = 0;
856   vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
857   vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
858   vnet_crypto_key_index_t key_index = ~0;
859   u8 key[64];
860   int buffer_size = vlib_buffer_get_default_data_size (vm);
861   u64 seed = clib_cpu_time_now ();
862   u64 t0[5], t1[5], t2[5], n_bytes = 0;
863   int i, j;
864
865   if (tm->buffer_size > buffer_size)
866     return clib_error_return (0, "buffer size must be <= %u", buffer_size);
867
868   rounds = tm->rounds ? tm->rounds : 100;
869   n_buffers = tm->n_buffers ? tm->n_buffers : 256;
870   buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
871   warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
872
873   if (buffer_size > vlib_buffer_get_default_data_size (vm))
874     return clib_error_return (0, "buffer size too big");
875
876   vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
877   vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
878   vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
879
880   n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
881   if (n_alloc != n_buffers)
882     {
883       if (n_alloc)
884         vlib_buffer_free (vm, buffer_indices, n_alloc);
885       err = clib_error_return (0, "buffer alloc failure");
886       goto done;
887     }
888
889   vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
890                    "warmup-rounds %u",
891                    format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
892                    rounds, warmup_rounds);
893   vlib_cli_output (vm, "   cpu-freq %.2f GHz",
894                    (f64) vm->clib_time.clocks_per_second * 1e-9);
895
896   vnet_crypto_op_type_t ot = 0;
897
898   for (i = 0; i < sizeof (key); i++)
899     key[i] = i;
900
901   key_index = vnet_crypto_key_add (vm, tm->alg, key,
902                                    test_crypto_get_key_sz (tm->alg));
903
904   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
905     {
906       vnet_crypto_op_id_t id = ad->op_by_type[i];
907       if (id == 0)
908         continue;
909       ot = i;
910       break;
911     }
912
913   for (i = 0; i < n_buffers; i++)
914     {
915       vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
916       op1 = ops1 + i;
917       op2 = ops2 + i;
918
919       switch (ot)
920         {
921         case VNET_CRYPTO_OP_TYPE_ENCRYPT:
922         case VNET_CRYPTO_OP_TYPE_DECRYPT:
923           vnet_crypto_op_init (op1,
924                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
925           vnet_crypto_op_init (op2,
926                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
927           op1->src = op2->src = op1->dst = op2->dst = b->data;
928           op1->key_index = op2->key_index = key_index;
929           op1->iv = op2->iv = b->data - 64;
930           n_bytes += op1->len = op2->len = buffer_size;
931           break;
932         case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
933         case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
934           vnet_crypto_op_init (op1,
935                                ad->op_by_type
936                                [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
937           vnet_crypto_op_init (op2,
938                                ad->op_by_type
939                                [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
940           op1->src = op2->src = op1->dst = op2->dst = b->data;
941           op1->key_index = op2->key_index = key_index;
942           op1->tag = op2->tag = b->data - 32;
943           op1->iv = op2->iv = b->data - 64;
944           op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
945           op1->aad_len = op2->aad_len = 64;
946           op1->tag_len = op2->tag_len = 16;
947           n_bytes += op1->len = op2->len = buffer_size;
948           break;
949         case VNET_CRYPTO_OP_TYPE_HMAC:
950           vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
951           op1->src = b->data;
952           op1->key_index = key_index;
953           op1->iv = 0;
954           op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
955           op1->digest_len = 0;
956           n_bytes += op1->len = buffer_size;
957           break;
958         default:
959           return 0;
960         }
961
962       for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
963         *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
964     }
965
966   for (i = 0; i < 5; i++)
967     {
968       for (j = 0; j < warmup_rounds; j++)
969         {
970           vnet_crypto_process_ops (vm, ops1, n_buffers);
971           if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
972             vnet_crypto_process_ops (vm, ops2, n_buffers);
973         }
974
975       t0[i] = clib_cpu_time_now ();
976       for (j = 0; j < rounds; j++)
977         vnet_crypto_process_ops (vm, ops1, n_buffers);
978       t1[i] = clib_cpu_time_now ();
979
980       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
981         {
982           for (j = 0; j < rounds; j++)
983             vnet_crypto_process_ops (vm, ops2, n_buffers);
984           t2[i] = clib_cpu_time_now ();
985         }
986     }
987
988   for (i = 0; i < 5; i++)
989     {
990       f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
991       f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
992       f64 tpb2, gbps2;
993
994       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
995         {
996           tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
997           gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
998           vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
999                            "decrypt %.03f ticks/byte, %.02f Gbps",
1000                            i + 1, tpb1, gbps1, tpb2, gbps2);
1001         }
1002       else
1003         {
1004           vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
1005                            i + 1, tpb1, gbps1);
1006         }
1007     }
1008
1009 done:
1010   if (n_alloc)
1011     vlib_buffer_free (vm, buffer_indices, n_alloc);
1012
1013   if (key_index != ~0)
1014     vnet_crypto_key_del (vm, key_index);
1015
1016   vec_free (buffer_indices);
1017   vec_free (ops1);
1018   vec_free (ops2);
1019   return err;
1020 }
1021
1022 static clib_error_t *
1023 test_crypto_command_fn (vlib_main_t * vm,
1024                         unformat_input_t * input, vlib_cli_command_t * cmd)
1025 {
1026   crypto_test_main_t *tm = &crypto_test_main;
1027   unittest_crypto_test_registration_t *tr;
1028   int is_perf = 0;
1029
1030   tr = tm->test_registrations;
1031   memset (tm, 0, sizeof (crypto_test_main_t));
1032   tm->test_registrations = tr;
1033   tm->alg = ~0;
1034
1035   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1036     {
1037       if (unformat (input, "verbose"))
1038         tm->verbose = 1;
1039       else if (unformat (input, "detail"))
1040         tm->verbose = 2;
1041       else
1042         if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1043         is_perf = 1;
1044       else if (unformat (input, "buffers %u", &tm->n_buffers))
1045         ;
1046       else if (unformat (input, "rounds %u", &tm->rounds))
1047         ;
1048       else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1049         ;
1050       else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1051         ;
1052       else
1053         return clib_error_return (0, "unknown input '%U'",
1054                                   format_unformat_error, input);
1055     }
1056
1057   if (is_perf)
1058     return test_crypto_perf (vm, tm);
1059   else
1060     return test_crypto (vm, tm);
1061 }
1062
1063 /* *INDENT-OFF* */
1064 VLIB_CLI_COMMAND (test_crypto_command, static) =
1065 {
1066   .path = "test crypto",
1067   .short_help = "test crypto",
1068   .function = test_crypto_command_fn,
1069 };
1070 /* *INDENT-ON* */
1071
1072 static clib_error_t *
1073 crypto_test_init (vlib_main_t * vm)
1074 {
1075   return (0);
1076 }
1077
1078 VLIB_INIT_FUNCTION (crypto_test_init);
1079
1080 /*
1081  * fd.io coding-style-patch-verification: ON
1082  *
1083  * Local Variables:
1084  * eval: (c-set-style "gnu")
1085  * End:
1086  */