crypto: add support for testing quad loops in crypto algos
[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       default:
79         ASSERT (0);
80       }
81
82     vec_reset_length (err);
83
84     if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
85       err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
86                     format_vnet_crypto_op_status, op->status);
87
88     if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
89       {
90         if (exp_ct_chunks)
91           {
92             chp = vec_elt_at_index (chunks, op->chunk_index);
93             for (i = 0; i < op->n_chunks; i++)
94               {
95                 if (memcmp (chp->dst, exp_ct_chunks[i].data, chp->len))
96                   err = format (err, "%sciphertext mismatch [chunk %d]",
97                                 vec_len (err) ? ", " : "", i);
98                 chp += 1;
99               }
100           }
101
102         if (exp_pt_chunks)
103           {
104             chp = vec_elt_at_index (chunks, op->chunk_index);
105             for (i = 0; i < op->n_chunks; i++)
106               {
107                 if (memcmp (chp->dst, exp_pt_chunks[i].data, chp->len))
108                   err = format (err, "%splaintext mismatch [chunk %d]",
109                                 vec_len (err) ? ", " : "", i);
110                 chp += 1;
111               }
112           }
113       }
114     else
115       {
116         if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
117           err = format (err, "%sciphertext mismatch",
118                         vec_len (err) ? ", " : "");
119
120         if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
121           err = format (err, "%splaintext mismatch",
122                         vec_len (err) ? ", " : "");
123       }
124
125     if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
126       err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
127
128     if (exp_digest &&
129         memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
130       err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
131
132     vec_reset_length (s);
133     s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op,
134                 r->is_chained);
135
136     if (vec_len (err))
137       fail = 1;
138
139     vlib_cli_output (vm, "%-60v%s%v", s, vec_len (err) ? "FAIL: " : "OK",
140                      err);
141     if (tm->verbose)
142       {
143         if (tm->verbose == 2)
144           fail = 1;
145
146         if (exp_ct && fail)
147           vlib_cli_output (vm, "Expected ciphertext:\n%U"
148                            "\nCalculated ciphertext:\n%U",
149                            format_hexdump, exp_ct->data, exp_ct->length,
150                            format_hexdump, op->dst, exp_ct->length);
151         if (exp_pt && fail)
152           vlib_cli_output (vm, "Expected plaintext:\n%U"
153                            "\nCalculated plaintext:\n%U",
154                            format_hexdump, exp_pt->data, exp_pt->length,
155                            format_hexdump, op->dst, exp_pt->length);
156         if (r->tag.length && fail)
157           vlib_cli_output (vm, "Expected tag:\n%U"
158                            "\nCalculated tag:\n%U",
159                            format_hexdump, r->tag.data, r->tag.length,
160                            format_hexdump, op->tag, op->tag_len);
161         if (exp_digest && fail)
162           vlib_cli_output (vm, "Expected digest:\n%U"
163                            "\nCalculated Digest:\n%U",
164                            format_hexdump, exp_digest->data,
165                            exp_digest->length, format_hexdump, op->digest,
166                            op->digest_len);
167       }
168   }
169   vec_free (err);
170   vec_free (s);
171 }
172
173 static void
174 validate_data (u8 ** data, u32 len)
175 {
176   u32 i, diff, old_len;
177   if (vec_len (data[0]) >= len)
178     return;
179
180   old_len = vec_len (data[0]);
181   diff = len - vec_len (data[0]);
182   vec_validate (data[0], old_len + diff - 1);
183   for (i = old_len; i < len; i++)
184     data[0][i] = (u8) i;
185 }
186
187 static void
188 generate_digest (vlib_main_t * vm,
189                  unittest_crypto_test_registration_t * r,
190                  vnet_crypto_op_id_t id)
191 {
192   crypto_test_main_t *cm = &crypto_test_main;
193   vnet_crypto_op_t op[1];
194   vnet_crypto_op_init (op, id);
195   vec_validate (r->digest.data, r->digest.length - 1);
196   op->src = cm->inc_data;
197   op->len = r->plaintext_incremental;
198   op->digest = r->digest.data;
199   op->digest_len = r->digest.length;
200   op->key_index = vnet_crypto_key_add (vm, r->alg,
201                                        cm->inc_data, r->key.length);
202
203   /* at this point openssl is set for each algo */
204   vnet_crypto_process_ops (vm, op, 1);
205 }
206
207 static int
208 restore_engines (u32 * engs)
209 {
210   return 0;
211   vnet_crypto_main_t *cm = &crypto_main;
212   u32 i;
213   vnet_crypto_engine_t *ce;
214
215   for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
216     {
217       vnet_crypto_op_data_t *od = &cm->opt_data[i];
218
219       if (engs[i] != ~0)
220         {
221           ce = vec_elt_at_index (cm->engines, engs[i]);
222           od->active_engine_index_simple = engs[i];
223           cm->ops_handlers[i] = ce->ops_handlers[i];
224         }
225     }
226
227   return 0;
228 }
229
230 static int
231 save_current_engines (u32 * engs)
232 {
233   return 0;
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                       ch.src = pt->data;
540                       ch.len = pt->length;
541                       ch.dst = computed_data + computed_data_total_len;
542                       computed_data_total_len += pt->length;
543                       vec_add1 (chunks, ch);
544                       op->n_chunks++;
545                       pt++;
546                     }
547                   op->tag = computed_data + computed_data_total_len;
548                   computed_data_total_len += r->tag.length;
549                 }
550               else
551                 {
552                   ct = r->ct_chunks;
553                   op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
554                   op->chunk_index = vec_len (chunks);
555                   while (ct->data)
556                     {
557                       ch.src = ct->data;
558                       ch.len = ct->length;
559                       ch.dst = computed_data + computed_data_total_len;
560                       computed_data_total_len += ct->length;
561                       vec_add1 (chunks, ch);
562                       op->n_chunks++;
563                       ct++;
564                     }
565                   op->tag = r->tag.data;
566                 }
567               op->tag_len = r->tag.length;
568               }
569               else
570               {
571               op->iv = r->iv.data;
572               op->key_index = vnet_crypto_key_add (vm, r->alg,
573                                                    r->key.data,
574                                                    r->key.length);
575               vec_add1 (key_indices, op->key_index);
576               op->aad = r->aad.data;
577               op->aad_len = r->aad.length;
578               op->len = r->plaintext.length;
579               op->dst = computed_data + computed_data_total_len;
580               computed_data_total_len += r->ciphertext.length;
581
582               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
583                 {
584                   op->src = r->plaintext.data;
585                   op->tag = computed_data + computed_data_total_len;
586                   computed_data_total_len += r->tag.length;
587                 }
588               else
589                 {
590                   op->tag = r->tag.data;
591                   op->src = r->ciphertext.data;
592                 }
593               op->tag_len = r->tag.length;
594               }
595               break;
596             case VNET_CRYPTO_OP_TYPE_HMAC:
597               if (r->is_chained)
598               {
599               op->key_index = vnet_crypto_key_add (vm, r->alg,
600                                                    r->key.data,
601                                                    r->key.length);
602               vec_add1 (key_indices, op->key_index);
603               op->digest_len = r->digest.length;
604               op->digest = computed_data + computed_data_total_len;
605               computed_data_total_len += r->digest.length;
606               pt = r->pt_chunks;
607               op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
608               op->chunk_index = vec_len (chunks);
609               while (pt->data)
610                 {
611                   ch.src = pt->data;
612                   ch.len = pt->length;
613                   vec_add1 (chunks, ch);
614                   op->n_chunks++;
615                   pt++;
616                 }
617               }
618               else
619               {
620               op->key_index = vnet_crypto_key_add (vm, r->alg,
621                                                    r->key.data,
622                                                    r->key.length);
623               vec_add1 (key_indices, op->key_index);
624               op->digest_len = r->digest.length;
625               op->digest = computed_data + computed_data_total_len;
626               computed_data_total_len += r->digest.length;
627               op->src = r->plaintext.data;
628               op->len = r->plaintext.length;
629               }
630               break;
631             default:
632               break;
633             };
634
635           op->user_data = i;
636         }
637     }
638   /* *INDENT-ON* */
639
640   vnet_crypto_process_ops (vm, ops, vec_len (ops));
641   vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
642                                    vec_len (chained_ops));
643
644   print_results (vm, rv, ops, chunks, vec_len (ops), tm);
645   print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
646
647   vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
648
649   vec_free (computed_data);
650   vec_free (ops);
651   vec_free (chained_ops);
652   vec_free (chunks);
653   return 0;
654 }
655
656 static u32
657 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
658 {
659   switch (alg)
660     {
661 #define _(n, s, l) \
662   case VNET_CRYPTO_ALG_##n: \
663     return l;
664   /* *INDENT-OFF* */
665   foreach_crypto_cipher_alg
666   foreach_crypto_aead_alg
667   /* *INDENT-ON* */
668 #undef _
669     case VNET_CRYPTO_ALG_HMAC_MD5:
670     case VNET_CRYPTO_ALG_HMAC_SHA1:
671       return 20;
672     case VNET_CRYPTO_ALG_HMAC_SHA224:
673       return 28;
674     case VNET_CRYPTO_ALG_HMAC_SHA256:
675       return 32;
676     case VNET_CRYPTO_ALG_HMAC_SHA384:
677       return 48;
678     case VNET_CRYPTO_ALG_HMAC_SHA512:
679       return 64;
680     default:
681       return 0;
682     }
683   return 0;
684 }
685
686 static clib_error_t *
687 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
688 {
689   clib_error_t *err = 0;
690   vnet_crypto_main_t *cm = &crypto_main;
691   unittest_crypto_test_registration_t *r = tm->test_registrations;
692   unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
693   u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
694   vnet_crypto_alg_data_t *ad;
695   u32 computed_data_total_len = 0;
696   u32 computed_data_total_incr_len = 0;
697   u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
698   unittest_crypto_test_data_t *ct;
699
700   /* pre-allocate plaintext data with reasonable length */
701   validate_data (&tm->inc_data, 2048);
702
703   int rc = save_current_engines (saved_engs);
704   if (rc)
705     return clib_error_return (0, "failed to set default crypto engine!");
706
707   /* construct registration vector */
708   while (r)
709     {
710       if (r->plaintext_incremental)
711         vec_add1 (inc_tests, r);
712       else
713         vec_add1 (static_tests, r);
714
715       ad = vec_elt_at_index (cm->algs, r->alg);
716
717       for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
718         {
719           vnet_crypto_op_id_t id = ad->op_by_type[i];
720
721           if (id == 0)
722             continue;
723
724           switch (i)
725             {
726             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
727               if (r->plaintext_incremental)
728                 {
729                   computed_data_total_incr_len += r->plaintext_incremental;
730                   n_ops_incr += 1;
731                 }
732               /* fall though */
733             case VNET_CRYPTO_OP_TYPE_DECRYPT:
734             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
735               if (r->is_chained)
736                 {
737                   ct = r->ct_chunks;
738                   j = 0;
739                   while (ct->data)
740                     {
741                       if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
742                         return clib_error_return (0,
743                                                   "test case '%s' exceeds extra data!",
744                                                   r->name);
745                       computed_data_total_len += ct->length;
746                       ct++;
747                       j++;
748                     }
749                   n_chained_ops += 1;
750                 }
751               else if (!r->plaintext_incremental)
752                 {
753                   computed_data_total_len += r->ciphertext.length;
754                   n_ops_static += 1;
755                 }
756               break;
757             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
758               if (r->plaintext_incremental)
759                 {
760                   computed_data_total_incr_len += r->plaintext_incremental;
761                   computed_data_total_incr_len += r->tag.length;
762                   n_ops_incr += 1;
763                 }
764               else
765                 {
766                   computed_data_total_len += r->ciphertext.length;
767                   computed_data_total_len += r->tag.length;
768                   if (r->is_chained)
769                     {
770                       ct = r->ct_chunks;
771                       j = 0;
772                       while (ct->data)
773                         {
774                           if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
775                             return clib_error_return (0,
776                                                       "test case '%s' exceeds extra data!",
777                                                       r->name);
778                           computed_data_total_len += ct->length;
779                           ct++;
780                           j++;
781                         }
782                       n_chained_ops += 1;
783                     }
784                   else
785                     n_ops_static += 1;
786                 }
787               break;
788             case VNET_CRYPTO_OP_TYPE_HMAC:
789               if (r->plaintext_incremental)
790                 {
791                   computed_data_total_incr_len += r->digest.length;
792                   n_ops_incr += 1;
793                   generate_digest (vm, r, id);
794                 }
795               else
796                 {
797                   computed_data_total_len += r->digest.length;
798                   if (r->is_chained)
799                     n_chained_ops += 1;
800                   else
801                     n_ops_static += 1;
802                 }
803               break;
804             default:
805               break;
806             };
807         }
808
809       /* next: */
810       r = r->next;
811     }
812   restore_engines (saved_engs);
813
814   err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
815                             computed_data_total_len);
816   if (err)
817     goto done;
818
819   err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
820                                  computed_data_total_incr_len);
821
822   r = tm->test_registrations;
823   while (r)
824     {
825       if (r->plaintext_incremental)
826         vec_free (r->digest.data);
827       r = r->next;
828     }
829
830 done:
831   vec_free (inc_tests);
832   vec_free (static_tests);
833   return err;
834 }
835
836 static clib_error_t *
837 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
838 {
839   vnet_crypto_main_t *cm = &crypto_main;
840   clib_error_t *err = 0;
841   u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
842   u32 *buffer_indices = 0;
843   vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
844   vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
845   vnet_crypto_key_index_t key_index = ~0;
846   u8 key[32];
847   int buffer_size = vlib_buffer_get_default_data_size (vm);
848   u64 seed = clib_cpu_time_now ();
849   u64 t0[5], t1[5], t2[5], n_bytes = 0;
850   int i, j;
851
852   if (tm->buffer_size > buffer_size)
853     return clib_error_return (0, "buffer size must be <= %u", buffer_size);
854
855   rounds = tm->rounds ? tm->rounds : 100;
856   n_buffers = tm->n_buffers ? tm->n_buffers : 256;
857   buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
858   warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
859
860   if (buffer_size > vlib_buffer_get_default_data_size (vm))
861     return clib_error_return (0, "buffer size too big");
862
863   vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
864   vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
865   vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
866
867   n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
868   if (n_alloc != n_buffers)
869     {
870       if (n_alloc)
871         vlib_buffer_free (vm, buffer_indices, n_alloc);
872       err = clib_error_return (0, "buffer alloc failure");
873       goto done;
874     }
875
876   vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
877                    "warmup-rounds %u",
878                    format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
879                    rounds, warmup_rounds);
880   vlib_cli_output (vm, "   cpu-freq %.2f GHz",
881                    (f64) vm->clib_time.clocks_per_second * 1e-9);
882
883   vnet_crypto_op_type_t ot = 0;
884
885   for (i = 0; i < sizeof (key); i++)
886     key[i] = i;
887
888   key_index = vnet_crypto_key_add (vm, tm->alg, key,
889                                    test_crypto_get_key_sz (tm->alg));
890
891   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
892     {
893       vnet_crypto_op_id_t id = ad->op_by_type[i];
894       if (id == 0)
895         continue;
896       ot = i;
897       break;
898     }
899
900   for (i = 0; i < n_buffers; i++)
901     {
902       vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
903       op1 = ops1 + i;
904       op2 = ops2 + i;
905
906       switch (ot)
907         {
908         case VNET_CRYPTO_OP_TYPE_ENCRYPT:
909         case VNET_CRYPTO_OP_TYPE_DECRYPT:
910           vnet_crypto_op_init (op1,
911                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
912           vnet_crypto_op_init (op2,
913                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
914           op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
915           op1->src = op2->src = op1->dst = op2->dst = b->data;
916           op1->key_index = op2->key_index = key_index;
917           op1->iv = op2->iv = b->data - 64;
918           n_bytes += op1->len = op2->len = buffer_size;
919           break;
920         case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
921         case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
922           vnet_crypto_op_init (op1,
923                                ad->op_by_type
924                                [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
925           vnet_crypto_op_init (op2,
926                                ad->op_by_type
927                                [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
928           op1->src = op2->src = op1->dst = op2->dst = b->data;
929           op1->key_index = op2->key_index = key_index;
930           op1->tag = op2->tag = b->data - 32;
931           op1->iv = op2->iv = b->data - 64;
932           op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
933           op1->aad_len = op2->aad_len = 64;
934           op1->tag_len = op2->tag_len = 16;
935           n_bytes += op1->len = op2->len = buffer_size;
936           break;
937         case VNET_CRYPTO_OP_TYPE_HMAC:
938           vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
939           op1->src = b->data;
940           op1->key_index = key_index;
941           op1->iv = 0;
942           op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
943           op1->digest_len = 0;
944           n_bytes += op1->len = buffer_size;
945           break;
946         default:
947           return 0;
948         }
949
950       for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
951         *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
952     }
953
954   for (i = 0; i < 5; i++)
955     {
956       for (j = 0; j < warmup_rounds; j++)
957         {
958           vnet_crypto_process_ops (vm, ops1, n_buffers);
959           if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
960             vnet_crypto_process_ops (vm, ops2, n_buffers);
961         }
962
963       t0[i] = clib_cpu_time_now ();
964       for (j = 0; j < rounds; j++)
965         vnet_crypto_process_ops (vm, ops1, n_buffers);
966       t1[i] = clib_cpu_time_now ();
967
968       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
969         {
970           for (j = 0; j < rounds; j++)
971             vnet_crypto_process_ops (vm, ops2, n_buffers);
972           t2[i] = clib_cpu_time_now ();
973         }
974     }
975
976   for (i = 0; i < 5; i++)
977     {
978       f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
979       f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
980       f64 tpb2, gbps2;
981
982       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
983         {
984           tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
985           gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
986           vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
987                            "decrypt %.03f ticks/byte, %.02f Gbps",
988                            i + 1, tpb1, gbps1, tpb2, gbps2);
989         }
990       else
991         {
992           vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
993                            i + 1, tpb1, gbps1);
994         }
995     }
996
997 done:
998   if (n_alloc)
999     vlib_buffer_free (vm, buffer_indices, n_alloc);
1000
1001   if (key_index != ~0)
1002     vnet_crypto_key_del (vm, key_index);
1003
1004   vec_free (buffer_indices);
1005   vec_free (ops1);
1006   vec_free (ops2);
1007   return err;
1008 }
1009
1010 static clib_error_t *
1011 test_crypto_command_fn (vlib_main_t * vm,
1012                         unformat_input_t * input, vlib_cli_command_t * cmd)
1013 {
1014   crypto_test_main_t *tm = &crypto_test_main;
1015   unittest_crypto_test_registration_t *tr;
1016   int is_perf = 0;
1017
1018   tr = tm->test_registrations;
1019   memset (tm, 0, sizeof (crypto_test_main_t));
1020   tm->test_registrations = tr;
1021   tm->alg = ~0;
1022
1023   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1024     {
1025       if (unformat (input, "verbose"))
1026         tm->verbose = 1;
1027       else if (unformat (input, "detail"))
1028         tm->verbose = 2;
1029       else
1030         if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1031         is_perf = 1;
1032       else if (unformat (input, "buffers %u", &tm->n_buffers))
1033         ;
1034       else if (unformat (input, "rounds %u", &tm->rounds))
1035         ;
1036       else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1037         ;
1038       else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1039         ;
1040       else
1041         return clib_error_return (0, "unknown input '%U'",
1042                                   format_unformat_error, input);
1043     }
1044
1045   if (is_perf)
1046     return test_crypto_perf (vm, tm);
1047   else
1048     return test_crypto (vm, tm);
1049 }
1050
1051 /* *INDENT-OFF* */
1052 VLIB_CLI_COMMAND (test_crypto_command, static) =
1053 {
1054   .path = "test crypto",
1055   .short_help = "test crypto",
1056   .function = test_crypto_command_fn,
1057 };
1058 /* *INDENT-ON* */
1059
1060 static clib_error_t *
1061 crypto_test_init (vlib_main_t * vm)
1062 {
1063   return (0);
1064 }
1065
1066 VLIB_INIT_FUNCTION (crypto_test_init);
1067
1068 /*
1069  * fd.io coding-style-patch-verification: ON
1070  *
1071  * Local Variables:
1072  * eval: (c-set-style "gnu")
1073  * End:
1074  */