f08b253dd1923a36548a4dcd72d630a18ac9f51b
[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   vnet_crypto_main_t *cm = &crypto_main;
211   u32 i;
212   vnet_crypto_engine_t *ce;
213
214   for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
215     {
216       vnet_crypto_op_data_t *od = &cm->opt_data[i];
217
218       if (engs[i] != ~0)
219         {
220           ce = vec_elt_at_index (cm->engines, engs[i]);
221           od->active_engine_index_simple = engs[i];
222           cm->ops_handlers[i] = ce->ops_handlers[i];
223         }
224     }
225
226   return 0;
227 }
228
229 static int
230 save_current_engines (u32 * engs)
231 {
232   vnet_crypto_main_t *cm = &crypto_main;
233   uword *p;
234   u32 i;
235   vnet_crypto_engine_t *ce;
236
237   p = hash_get_mem (cm->engine_index_by_name, "openssl");
238   if (!p)
239     return -1;
240
241   ce = vec_elt_at_index (cm->engines, p[0]);
242
243   /* set openssl for all crypto algs to generate expected data */
244   for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
245     {
246       vnet_crypto_op_data_t *od = &cm->opt_data[i];
247       if (od->active_engine_index_simple != ~0)
248         {
249           /* save engine index */
250           engs[i] = od->active_engine_index_simple;
251           od->active_engine_index_simple = ce - cm->engines;
252           cm->ops_handlers[i] = ce->ops_handlers[i];
253         }
254     }
255
256   return 0;
257 }
258
259 static clib_error_t *
260 test_crypto_incremental (vlib_main_t * vm, crypto_test_main_t * tm,
261                          unittest_crypto_test_registration_t ** rv, u32 n_ops,
262                          u32 computed_data_total_len)
263 {
264   vnet_crypto_main_t *cm = &crypto_main;
265   vnet_crypto_alg_data_t *ad;
266   vnet_crypto_key_index_t *key_indices = 0;
267   u32 i;
268   unittest_crypto_test_registration_t *r;
269   vnet_crypto_op_t *ops = 0, *op;
270   u8 *encrypted_data = 0, *decrypted_data = 0, *s = 0, *err = 0;
271
272   if (n_ops == 0)
273     return 0;
274
275   vec_validate_aligned (encrypted_data, computed_data_total_len - 1,
276                         CLIB_CACHE_LINE_BYTES);
277   vec_validate_aligned (decrypted_data, computed_data_total_len - 1,
278                         CLIB_CACHE_LINE_BYTES);
279   vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
280   computed_data_total_len = 0;
281
282   op = ops;
283   /* first stage: encrypt only */
284
285   vec_foreach_index (i, rv)
286   {
287     r = rv[i];
288     int t;
289     ad = vec_elt_at_index (cm->algs, r->alg);
290     for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
291       {
292         vnet_crypto_op_id_t id = ad->op_by_type[t];
293
294         if (id == 0)
295           continue;
296
297         switch (t)
298           {
299           case VNET_CRYPTO_OP_TYPE_ENCRYPT:
300             vnet_crypto_op_init (op, id);
301             op->iv = tm->inc_data;
302             op->key_index = vnet_crypto_key_add (vm, r->alg,
303                                                  tm->inc_data, r->key.length);
304             vec_add1 (key_indices, op->key_index);
305             op->len = r->plaintext_incremental;
306             op->src = tm->inc_data;
307             op->dst = encrypted_data + computed_data_total_len;
308             computed_data_total_len += r->plaintext_incremental;
309             op->user_data = i;
310             op++;
311             break;
312           case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
313             vnet_crypto_op_init (op, id);
314             op->iv = tm->inc_data;
315             op->key_index = vnet_crypto_key_add (vm, r->alg,
316                                                  tm->inc_data, r->key.length);
317             vec_add1 (key_indices, op->key_index);
318             op->aad = tm->inc_data;
319             op->aad_len = r->aad.length;
320             op->len = r->plaintext_incremental;
321             op->dst = encrypted_data + computed_data_total_len;
322             computed_data_total_len += r->plaintext_incremental;
323             op->src = tm->inc_data;
324             op->tag = encrypted_data + computed_data_total_len;
325             computed_data_total_len += r->tag.length;
326             op->tag_len = r->tag.length;
327             op->user_data = i;
328             op++;
329             break;
330           case VNET_CRYPTO_OP_TYPE_HMAC:
331             /* compute hmac in the next stage */
332             op->op = VNET_CRYPTO_OP_NONE;
333             computed_data_total_len += r->digest.length;
334             op->user_data = i;
335             op++;
336             break;
337           default:
338             break;
339           };
340       }
341   }
342
343   vnet_crypto_process_ops (vm, ops, n_ops);
344   computed_data_total_len = 0;
345
346   /* second stage: hash/decrypt previously encrypted data */
347   op = ops;
348
349   vec_foreach_index (i, rv)
350   {
351     r = rv[i];
352     int t;
353     ad = vec_elt_at_index (cm->algs, r->alg);
354     for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
355       {
356         vnet_crypto_op_id_t id = ad->op_by_type[t];
357
358         if (id == 0)
359           continue;
360
361         switch (t)
362           {
363           case VNET_CRYPTO_OP_TYPE_DECRYPT:
364             vnet_crypto_op_init (op, id);
365             op->iv = tm->inc_data;
366             op->key_index = vnet_crypto_key_add (vm, r->alg,
367                                                  tm->inc_data, r->key.length);
368             vec_add1 (key_indices, op->key_index);
369             op->len = r->plaintext_incremental;
370             op->src = encrypted_data + computed_data_total_len;
371             op->dst = decrypted_data + computed_data_total_len;
372             computed_data_total_len += r->plaintext_incremental;
373             op->user_data = i;
374             op++;
375             break;
376           case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
377             vnet_crypto_op_init (op, id);
378             op->iv = tm->inc_data;
379             op->key_index = vnet_crypto_key_add (vm, r->alg,
380                                                  tm->inc_data, r->key.length);
381             vec_add1 (key_indices, op->key_index);
382             op->aad = tm->inc_data;
383             op->aad_len = r->aad.length;
384             op->len = r->plaintext_incremental;
385             op->dst = decrypted_data + computed_data_total_len;
386             op->src = encrypted_data + computed_data_total_len;
387             computed_data_total_len += r->plaintext_incremental;
388
389             op->tag = encrypted_data + computed_data_total_len;
390             computed_data_total_len += r->tag.length;
391             op->tag_len = r->tag.length;
392             op->user_data = i;
393             op++;
394             break;
395           case VNET_CRYPTO_OP_TYPE_HMAC:
396             vnet_crypto_op_init (op, id);
397             op->key_index = vnet_crypto_key_add (vm, r->alg,
398                                                  tm->inc_data, r->key.length);
399             vec_add1 (key_indices, op->key_index);
400             op->src = tm->inc_data;
401             op->len = r->plaintext_incremental;
402             op->digest_len = r->digest.length;
403             op->digest = encrypted_data + computed_data_total_len;
404             computed_data_total_len += r->digest.length;
405             op->user_data = i;
406             op++;
407             break;
408           default:
409             break;
410           };
411
412       }
413   }
414
415   vnet_crypto_process_ops (vm, ops, n_ops);
416   print_results (vm, rv, ops, 0, n_ops, tm);
417
418   vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
419   vec_free (tm->inc_data);
420   vec_free (ops);
421   vec_free (encrypted_data);
422   vec_free (decrypted_data);
423   vec_free (err);
424   vec_free (s);
425   return 0;
426 }
427
428 static clib_error_t *
429 test_crypto_static (vlib_main_t * vm, crypto_test_main_t * tm,
430                     unittest_crypto_test_registration_t ** rv, u32 n_ops,
431                     u32 n_chained_ops, u32 computed_data_total_len)
432 {
433   unittest_crypto_test_data_t *pt, *ct;
434   vnet_crypto_op_chunk_t *chunks = 0, ch;
435   unittest_crypto_test_registration_t *r;
436   vnet_crypto_op_t *ops = 0, *op, *chained_ops = 0;
437   vnet_crypto_op_t *current_chained_op = 0, *current_op = 0;
438   vnet_crypto_main_t *cm = &crypto_main;
439   vnet_crypto_alg_data_t *ad;
440   vnet_crypto_key_index_t *key_indices = 0;
441   u8 *computed_data = 0;
442   u32 i;
443
444   vec_sort_with_function (rv, sort_registrations);
445
446   vec_validate_aligned (computed_data, computed_data_total_len - 1,
447                         CLIB_CACHE_LINE_BYTES);
448   vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
449   vec_validate_aligned (chained_ops, n_chained_ops - 1,
450                         CLIB_CACHE_LINE_BYTES);
451   computed_data_total_len = 0;
452
453   current_op = ops;
454   current_chained_op = chained_ops;
455   /* *INDENT-OFF* */
456   vec_foreach_index (i, rv)
457     {
458       r = rv[i];
459       int t;
460       ad = vec_elt_at_index (cm->algs, r->alg);
461       for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
462         {
463           vnet_crypto_op_id_t id = ad->op_by_type[t];
464
465           if (id == 0)
466             continue;
467
468           if (r->is_chained)
469           {
470             op = current_chained_op;
471             current_chained_op += 1;
472           }
473           else
474           {
475             op = current_op;
476             current_op += 1;
477           }
478
479           vnet_crypto_op_init (op, id);
480
481           switch (t)
482             {
483             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
484             case VNET_CRYPTO_OP_TYPE_DECRYPT:
485               op->iv = r->iv.data;
486               op->key_index = vnet_crypto_key_add (vm, r->alg,
487                                                    r->key.data,
488                                                    r->key.length);
489               vec_add1 (key_indices, op->key_index);
490
491               if (r->is_chained)
492               {
493               pt = r->pt_chunks;
494               ct = r->ct_chunks;
495               op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
496               op->chunk_index = vec_len (chunks);
497               while (pt->data)
498                 {
499                   ch.src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
500                     pt->data : ct->data;
501                   ch.len = pt->length;
502                   ch.dst = computed_data + computed_data_total_len;
503                   computed_data_total_len += pt->length;
504                   vec_add1 (chunks, ch);
505                   op->n_chunks++;
506                   pt++;
507                   ct++;
508                 }
509               }
510               else
511               {
512               op->len = r->plaintext.length;
513               op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
514                 r->plaintext.data : r->ciphertext.data;
515               op->dst = computed_data + computed_data_total_len;
516               computed_data_total_len += r->ciphertext.length;
517               }
518               break;
519             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
520             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
521               if (r->is_chained)
522               {
523               op->iv = r->iv.data;
524               op->key_index = vnet_crypto_key_add (vm, r->alg,
525                                                    r->key.data,
526                                                    r->key.length);
527               vec_add1 (key_indices, op->key_index);
528               op->aad = r->aad.data;
529               op->aad_len = r->aad.length;
530               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
531                 {
532                   pt = r->pt_chunks;
533                   op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
534                   op->chunk_index = vec_len (chunks);
535                   while (pt->data)
536                     {
537                       clib_memset (&ch, 0, sizeof (ch));
538                       ch.src = pt->data;
539                       ch.len = pt->length;
540                       ch.dst = computed_data + computed_data_total_len;
541                       computed_data_total_len += pt->length;
542                       vec_add1 (chunks, ch);
543                       op->n_chunks++;
544                       pt++;
545                     }
546                   op->tag = computed_data + computed_data_total_len;
547                   computed_data_total_len += r->tag.length;
548                 }
549               else
550                 {
551                   ct = r->ct_chunks;
552                   op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
553                   op->chunk_index = vec_len (chunks);
554                   while (ct->data)
555                     {
556                       clib_memset (&ch, 0, sizeof (ch));
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                   clib_memset (&ch, 0, sizeof (ch));
612                   ch.src = pt->data;
613                   ch.len = pt->length;
614                   vec_add1 (chunks, ch);
615                   op->n_chunks++;
616                   pt++;
617                 }
618               }
619               else
620               {
621               op->key_index = vnet_crypto_key_add (vm, r->alg,
622                                                    r->key.data,
623                                                    r->key.length);
624               vec_add1 (key_indices, op->key_index);
625               op->digest_len = r->digest.length;
626               op->digest = computed_data + computed_data_total_len;
627               computed_data_total_len += r->digest.length;
628               op->src = r->plaintext.data;
629               op->len = r->plaintext.length;
630               }
631               break;
632             default:
633               break;
634             };
635
636           op->user_data = i;
637         }
638     }
639   /* *INDENT-ON* */
640
641   vnet_crypto_process_ops (vm, ops, vec_len (ops));
642   vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
643                                    vec_len (chained_ops));
644
645   print_results (vm, rv, ops, chunks, vec_len (ops), tm);
646   print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
647
648   vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
649
650   vec_free (computed_data);
651   vec_free (ops);
652   vec_free (chained_ops);
653   vec_free (chunks);
654   return 0;
655 }
656
657 static u32
658 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
659 {
660   switch (alg)
661     {
662 #define _(n, s, l) \
663   case VNET_CRYPTO_ALG_##n: \
664     return l;
665   /* *INDENT-OFF* */
666   foreach_crypto_cipher_alg
667   foreach_crypto_aead_alg
668   /* *INDENT-ON* */
669 #undef _
670     case VNET_CRYPTO_ALG_HMAC_MD5:
671     case VNET_CRYPTO_ALG_HMAC_SHA1:
672       return 20;
673     case VNET_CRYPTO_ALG_HMAC_SHA224:
674       return 28;
675     case VNET_CRYPTO_ALG_HMAC_SHA256:
676       return 32;
677     case VNET_CRYPTO_ALG_HMAC_SHA384:
678       return 48;
679     case VNET_CRYPTO_ALG_HMAC_SHA512:
680       return 64;
681     default:
682       return 0;
683     }
684   return 0;
685 }
686
687 static clib_error_t *
688 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
689 {
690   clib_error_t *err = 0;
691   vnet_crypto_main_t *cm = &crypto_main;
692   unittest_crypto_test_registration_t *r = tm->test_registrations;
693   unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
694   u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
695   vnet_crypto_alg_data_t *ad;
696   u32 computed_data_total_len = 0;
697   u32 computed_data_total_incr_len = 0;
698   u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
699   unittest_crypto_test_data_t *ct;
700
701   /* pre-allocate plaintext data with reasonable length */
702   validate_data (&tm->inc_data, 2048);
703
704   int rc = save_current_engines (saved_engs);
705   if (rc)
706     return clib_error_return (0, "failed to set default crypto engine!");
707
708   /* construct registration vector */
709   while (r)
710     {
711       if (r->plaintext_incremental)
712         vec_add1 (inc_tests, r);
713       else
714         vec_add1 (static_tests, r);
715
716       ad = vec_elt_at_index (cm->algs, r->alg);
717
718       for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
719         {
720           vnet_crypto_op_id_t id = ad->op_by_type[i];
721
722           if (id == 0)
723             continue;
724
725           switch (i)
726             {
727             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
728               if (r->plaintext_incremental)
729                 {
730                   computed_data_total_incr_len += r->plaintext_incremental;
731                   n_ops_incr += 1;
732                 }
733               /* fall though */
734             case VNET_CRYPTO_OP_TYPE_DECRYPT:
735             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
736               if (r->is_chained)
737                 {
738                   ct = r->ct_chunks;
739                   j = 0;
740                   while (ct->data)
741                     {
742                       if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
743                         return clib_error_return (0,
744                                                   "test case '%s' exceeds extra data!",
745                                                   r->name);
746                       computed_data_total_len += ct->length;
747                       ct++;
748                       j++;
749                     }
750                   n_chained_ops += 1;
751                 }
752               else if (!r->plaintext_incremental)
753                 {
754                   computed_data_total_len += r->ciphertext.length;
755                   n_ops_static += 1;
756                 }
757               break;
758             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
759               if (r->plaintext_incremental)
760                 {
761                   computed_data_total_incr_len += r->plaintext_incremental;
762                   computed_data_total_incr_len += r->tag.length;
763                   n_ops_incr += 1;
764                 }
765               else
766                 {
767                   computed_data_total_len += r->ciphertext.length;
768                   computed_data_total_len += r->tag.length;
769                   if (r->is_chained)
770                     {
771                       ct = r->ct_chunks;
772                       j = 0;
773                       while (ct->data)
774                         {
775                           if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
776                             return clib_error_return (0,
777                                                       "test case '%s' exceeds extra data!",
778                                                       r->name);
779                           computed_data_total_len += ct->length;
780                           ct++;
781                           j++;
782                         }
783                       n_chained_ops += 1;
784                     }
785                   else
786                     n_ops_static += 1;
787                 }
788               break;
789             case VNET_CRYPTO_OP_TYPE_HMAC:
790               if (r->plaintext_incremental)
791                 {
792                   computed_data_total_incr_len += r->digest.length;
793                   n_ops_incr += 1;
794                   generate_digest (vm, r, id);
795                 }
796               else
797                 {
798                   computed_data_total_len += r->digest.length;
799                   if (r->is_chained)
800                     n_chained_ops += 1;
801                   else
802                     n_ops_static += 1;
803                 }
804               break;
805             default:
806               break;
807             };
808         }
809
810       /* next: */
811       r = r->next;
812     }
813   restore_engines (saved_engs);
814
815   err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
816                             computed_data_total_len);
817   if (err)
818     goto done;
819
820   err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
821                                  computed_data_total_incr_len);
822
823   r = tm->test_registrations;
824   while (r)
825     {
826       if (r->plaintext_incremental)
827         vec_free (r->digest.data);
828       r = r->next;
829     }
830
831 done:
832   vec_free (inc_tests);
833   vec_free (static_tests);
834   return err;
835 }
836
837 static clib_error_t *
838 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
839 {
840   vnet_crypto_main_t *cm = &crypto_main;
841   clib_error_t *err = 0;
842   u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
843   u32 *buffer_indices = 0;
844   vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
845   vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
846   vnet_crypto_key_index_t key_index = ~0;
847   u8 key[32];
848   int buffer_size = vlib_buffer_get_default_data_size (vm);
849   u64 seed = clib_cpu_time_now ();
850   u64 t0[5], t1[5], t2[5], n_bytes = 0;
851   int i, j;
852
853   if (tm->buffer_size > buffer_size)
854     return clib_error_return (0, "buffer size must be <= %u", buffer_size);
855
856   rounds = tm->rounds ? tm->rounds : 100;
857   n_buffers = tm->n_buffers ? tm->n_buffers : 256;
858   buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
859   warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
860
861   if (buffer_size > vlib_buffer_get_default_data_size (vm))
862     return clib_error_return (0, "buffer size too big");
863
864   vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
865   vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
866   vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
867
868   n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
869   if (n_alloc != n_buffers)
870     {
871       if (n_alloc)
872         vlib_buffer_free (vm, buffer_indices, n_alloc);
873       err = clib_error_return (0, "buffer alloc failure");
874       goto done;
875     }
876
877   vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
878                    "warmup-rounds %u",
879                    format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
880                    rounds, warmup_rounds);
881   vlib_cli_output (vm, "   cpu-freq %.2f GHz",
882                    (f64) vm->clib_time.clocks_per_second * 1e-9);
883
884   vnet_crypto_op_type_t ot = 0;
885
886   for (i = 0; i < sizeof (key); i++)
887     key[i] = i;
888
889   key_index = vnet_crypto_key_add (vm, tm->alg, key,
890                                    test_crypto_get_key_sz (tm->alg));
891
892   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
893     {
894       vnet_crypto_op_id_t id = ad->op_by_type[i];
895       if (id == 0)
896         continue;
897       ot = i;
898       break;
899     }
900
901   for (i = 0; i < n_buffers; i++)
902     {
903       vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
904       op1 = ops1 + i;
905       op2 = ops2 + i;
906
907       switch (ot)
908         {
909         case VNET_CRYPTO_OP_TYPE_ENCRYPT:
910         case VNET_CRYPTO_OP_TYPE_DECRYPT:
911           vnet_crypto_op_init (op1,
912                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
913           vnet_crypto_op_init (op2,
914                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
915           op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
916           op1->src = op2->src = op1->dst = op2->dst = b->data;
917           op1->key_index = op2->key_index = key_index;
918           op1->iv = op2->iv = b->data - 64;
919           n_bytes += op1->len = op2->len = buffer_size;
920           break;
921         case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
922         case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
923           vnet_crypto_op_init (op1,
924                                ad->op_by_type
925                                [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
926           vnet_crypto_op_init (op2,
927                                ad->op_by_type
928                                [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
929           op1->src = op2->src = op1->dst = op2->dst = b->data;
930           op1->key_index = op2->key_index = key_index;
931           op1->tag = op2->tag = b->data - 32;
932           op1->iv = op2->iv = b->data - 64;
933           op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
934           op1->aad_len = op2->aad_len = 64;
935           op1->tag_len = op2->tag_len = 16;
936           n_bytes += op1->len = op2->len = buffer_size;
937           break;
938         case VNET_CRYPTO_OP_TYPE_HMAC:
939           vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
940           op1->src = b->data;
941           op1->key_index = key_index;
942           op1->iv = 0;
943           op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
944           op1->digest_len = 0;
945           n_bytes += op1->len = buffer_size;
946           break;
947         default:
948           return 0;
949         }
950
951       for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
952         *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
953     }
954
955   for (i = 0; i < 5; i++)
956     {
957       for (j = 0; j < warmup_rounds; j++)
958         {
959           vnet_crypto_process_ops (vm, ops1, n_buffers);
960           if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
961             vnet_crypto_process_ops (vm, ops2, n_buffers);
962         }
963
964       t0[i] = clib_cpu_time_now ();
965       for (j = 0; j < rounds; j++)
966         vnet_crypto_process_ops (vm, ops1, n_buffers);
967       t1[i] = clib_cpu_time_now ();
968
969       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
970         {
971           for (j = 0; j < rounds; j++)
972             vnet_crypto_process_ops (vm, ops2, n_buffers);
973           t2[i] = clib_cpu_time_now ();
974         }
975     }
976
977   for (i = 0; i < 5; i++)
978     {
979       f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
980       f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
981       f64 tpb2, gbps2;
982
983       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
984         {
985           tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
986           gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
987           vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
988                            "decrypt %.03f ticks/byte, %.02f Gbps",
989                            i + 1, tpb1, gbps1, tpb2, gbps2);
990         }
991       else
992         {
993           vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
994                            i + 1, tpb1, gbps1);
995         }
996     }
997
998 done:
999   if (n_alloc)
1000     vlib_buffer_free (vm, buffer_indices, n_alloc);
1001
1002   if (key_index != ~0)
1003     vnet_crypto_key_del (vm, key_index);
1004
1005   vec_free (buffer_indices);
1006   vec_free (ops1);
1007   vec_free (ops2);
1008   return err;
1009 }
1010
1011 static clib_error_t *
1012 test_crypto_command_fn (vlib_main_t * vm,
1013                         unformat_input_t * input, vlib_cli_command_t * cmd)
1014 {
1015   crypto_test_main_t *tm = &crypto_test_main;
1016   unittest_crypto_test_registration_t *tr;
1017   int is_perf = 0;
1018
1019   tr = tm->test_registrations;
1020   memset (tm, 0, sizeof (crypto_test_main_t));
1021   tm->test_registrations = tr;
1022   tm->alg = ~0;
1023
1024   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1025     {
1026       if (unformat (input, "verbose"))
1027         tm->verbose = 1;
1028       else if (unformat (input, "detail"))
1029         tm->verbose = 2;
1030       else
1031         if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1032         is_perf = 1;
1033       else if (unformat (input, "buffers %u", &tm->n_buffers))
1034         ;
1035       else if (unformat (input, "rounds %u", &tm->rounds))
1036         ;
1037       else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1038         ;
1039       else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1040         ;
1041       else
1042         return clib_error_return (0, "unknown input '%U'",
1043                                   format_unformat_error, input);
1044     }
1045
1046   if (is_perf)
1047     return test_crypto_perf (vm, tm);
1048   else
1049     return test_crypto (vm, tm);
1050 }
1051
1052 /* *INDENT-OFF* */
1053 VLIB_CLI_COMMAND (test_crypto_command, static) =
1054 {
1055   .path = "test crypto",
1056   .short_help = "test crypto",
1057   .function = test_crypto_command_fn,
1058 };
1059 /* *INDENT-ON* */
1060
1061 static clib_error_t *
1062 crypto_test_init (vlib_main_t * vm)
1063 {
1064   return (0);
1065 }
1066
1067 VLIB_INIT_FUNCTION (crypto_test_init);
1068
1069 /*
1070  * fd.io coding-style-patch-verification: ON
1071  *
1072  * Local Variables:
1073  * eval: (c-set-style "gnu")
1074  * End:
1075  */