docs: Use newer Ubuntu LTS in tutorial
[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   vec_foreach_index (i, rv)
458     {
459       r = rv[i];
460       int t;
461       ad = vec_elt_at_index (cm->algs, r->alg);
462       for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
463         {
464           vnet_crypto_op_id_t id = ad->op_by_type[t];
465
466           if (id == 0)
467             continue;
468
469           if (r->is_chained)
470           {
471             op = current_chained_op;
472             current_chained_op += 1;
473           }
474           else
475           {
476             op = current_op;
477             current_op += 1;
478           }
479
480           vnet_crypto_op_init (op, id);
481
482           switch (t)
483             {
484             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
485             case VNET_CRYPTO_OP_TYPE_DECRYPT:
486               op->iv = r->iv.data;
487               op->key_index = vnet_crypto_key_add (vm, r->alg,
488                                                    r->key.data,
489                                                    r->key.length);
490               vec_add1 (key_indices, op->key_index);
491
492               if (r->is_chained)
493               {
494               pt = r->pt_chunks;
495               ct = r->ct_chunks;
496               op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
497               op->chunk_index = vec_len (chunks);
498               while (pt->data)
499                 {
500                   ch.src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
501                     pt->data : ct->data;
502                   ch.len = pt->length;
503                   ch.dst = computed_data + computed_data_total_len;
504                   computed_data_total_len += pt->length;
505                   vec_add1 (chunks, ch);
506                   op->n_chunks++;
507                   pt++;
508                   ct++;
509                 }
510               }
511               else
512               {
513               op->len = r->plaintext.length;
514               op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
515                 r->plaintext.data : r->ciphertext.data;
516               op->dst = computed_data + computed_data_total_len;
517               computed_data_total_len += r->ciphertext.length;
518               }
519               break;
520             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
521             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
522               if (r->is_chained)
523               {
524               op->iv = r->iv.data;
525               op->key_index = vnet_crypto_key_add (vm, r->alg,
526                                                    r->key.data,
527                                                    r->key.length);
528               vec_add1 (key_indices, op->key_index);
529               op->aad = r->aad.data;
530               op->aad_len = r->aad.length;
531               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
532                 {
533                   pt = r->pt_chunks;
534                   op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
535                   op->chunk_index = vec_len (chunks);
536                   while (pt->data)
537                     {
538                       clib_memset (&ch, 0, sizeof (ch));
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                       clib_memset (&ch, 0, sizeof (ch));
558                       ch.src = ct->data;
559                       ch.len = ct->length;
560                       ch.dst = computed_data + computed_data_total_len;
561                       computed_data_total_len += ct->length;
562                       vec_add1 (chunks, ch);
563                       op->n_chunks++;
564                       ct++;
565                     }
566                   op->tag = r->tag.data;
567                 }
568               op->tag_len = r->tag.length;
569               }
570               else
571               {
572               op->iv = r->iv.data;
573               op->key_index = vnet_crypto_key_add (vm, r->alg,
574                                                    r->key.data,
575                                                    r->key.length);
576               vec_add1 (key_indices, op->key_index);
577               op->aad = r->aad.data;
578               op->aad_len = r->aad.length;
579               op->len = r->plaintext.length;
580               op->dst = computed_data + computed_data_total_len;
581               computed_data_total_len += r->ciphertext.length;
582
583               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
584                 {
585                   op->src = r->plaintext.data;
586                   op->tag = computed_data + computed_data_total_len;
587                   computed_data_total_len += r->tag.length;
588                 }
589               else
590                 {
591                   op->tag = r->tag.data;
592                   op->src = r->ciphertext.data;
593                 }
594               op->tag_len = r->tag.length;
595               }
596               break;
597             case VNET_CRYPTO_OP_TYPE_HMAC:
598               if (r->is_chained)
599               {
600               op->key_index = vnet_crypto_key_add (vm, r->alg,
601                                                    r->key.data,
602                                                    r->key.length);
603               vec_add1 (key_indices, op->key_index);
604               op->digest_len = r->digest.length;
605               op->digest = computed_data + computed_data_total_len;
606               computed_data_total_len += r->digest.length;
607               pt = r->pt_chunks;
608               op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
609               op->chunk_index = vec_len (chunks);
610               while (pt->data)
611                 {
612                   clib_memset (&ch, 0, sizeof (ch));
613                   ch.src = pt->data;
614                   ch.len = pt->length;
615                   vec_add1 (chunks, ch);
616                   op->n_chunks++;
617                   pt++;
618                 }
619               }
620               else
621               {
622               op->key_index = vnet_crypto_key_add (vm, r->alg,
623                                                    r->key.data,
624                                                    r->key.length);
625               vec_add1 (key_indices, op->key_index);
626               op->digest_len = r->digest.length;
627               op->digest = computed_data + computed_data_total_len;
628               computed_data_total_len += r->digest.length;
629               op->src = r->plaintext.data;
630               op->len = r->plaintext.length;
631               }
632               break;
633             case VNET_CRYPTO_OP_TYPE_HASH:
634               op->digest = computed_data + computed_data_total_len;
635               computed_data_total_len += r->digest.length;
636               op->src = r->plaintext.data;
637               op->len = r->plaintext.length;
638               break;
639             default:
640               break;
641             };
642
643           op->user_data = i;
644         }
645     }
646
647   vnet_crypto_process_ops (vm, ops, vec_len (ops));
648   vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
649                                    vec_len (chained_ops));
650
651   print_results (vm, rv, ops, chunks, vec_len (ops), tm);
652   print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
653
654   vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
655
656   vec_free (computed_data);
657   vec_free (ops);
658   vec_free (chained_ops);
659   vec_free (chunks);
660   return 0;
661 }
662
663 static u32
664 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
665 {
666   switch (alg)
667     {
668 #define _(n, s, l) \
669   case VNET_CRYPTO_ALG_##n: \
670     return l;
671   foreach_crypto_cipher_alg
672   foreach_crypto_aead_alg
673 #undef _
674     case VNET_CRYPTO_ALG_HMAC_MD5:
675     case VNET_CRYPTO_ALG_HMAC_SHA1:
676       return 20;
677     case VNET_CRYPTO_ALG_HMAC_SHA224:
678       return 28;
679     case VNET_CRYPTO_ALG_HMAC_SHA256:
680       return 32;
681     case VNET_CRYPTO_ALG_HMAC_SHA384:
682       return 48;
683     case VNET_CRYPTO_ALG_HMAC_SHA512:
684       return 64;
685     default:
686       return 0;
687     }
688   return 0;
689 }
690
691 static clib_error_t *
692 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
693 {
694   clib_error_t *err = 0;
695   vnet_crypto_main_t *cm = &crypto_main;
696   unittest_crypto_test_registration_t *r = tm->test_registrations;
697   unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
698   u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
699   vnet_crypto_alg_data_t *ad;
700   u32 computed_data_total_len = 0;
701   u32 computed_data_total_incr_len = 0;
702   u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
703   unittest_crypto_test_data_t *ct;
704
705   /* pre-allocate plaintext data with reasonable length */
706   validate_data (&tm->inc_data, 2048);
707
708   int rc = save_current_engines (saved_engs);
709   if (rc)
710     return clib_error_return (0, "failed to set default crypto engine!");
711
712   /* construct registration vector */
713   while (r)
714     {
715       if (r->plaintext_incremental)
716         vec_add1 (inc_tests, r);
717       else
718         vec_add1 (static_tests, r);
719
720       ad = vec_elt_at_index (cm->algs, r->alg);
721
722       for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
723         {
724           vnet_crypto_op_id_t id = ad->op_by_type[i];
725
726           if (id == 0)
727             continue;
728
729           switch (i)
730             {
731             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
732               if (r->plaintext_incremental)
733                 {
734                   computed_data_total_incr_len += r->plaintext_incremental;
735                   n_ops_incr += 1;
736                 }
737               /* fall though */
738             case VNET_CRYPTO_OP_TYPE_DECRYPT:
739             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
740               if (r->is_chained)
741                 {
742                   ct = r->ct_chunks;
743                   j = 0;
744                   while (ct->data)
745                     {
746                       if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
747                         return clib_error_return (0,
748                                                   "test case '%s' exceeds extra data!",
749                                                   r->name);
750                       computed_data_total_len += ct->length;
751                       ct++;
752                       j++;
753                     }
754                   n_chained_ops += 1;
755                 }
756               else if (!r->plaintext_incremental)
757                 {
758                   computed_data_total_len += r->ciphertext.length;
759                   n_ops_static += 1;
760                 }
761               break;
762             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
763               if (r->plaintext_incremental)
764                 {
765                   computed_data_total_incr_len += r->plaintext_incremental;
766                   computed_data_total_incr_len += r->tag.length;
767                   n_ops_incr += 1;
768                 }
769               else
770                 {
771                   computed_data_total_len += r->ciphertext.length;
772                   computed_data_total_len += r->tag.length;
773                   if (r->is_chained)
774                     {
775                       ct = r->ct_chunks;
776                       j = 0;
777                       while (ct->data)
778                         {
779                           if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
780                             return clib_error_return (0,
781                                                       "test case '%s' exceeds extra data!",
782                                                       r->name);
783                           computed_data_total_len += ct->length;
784                           ct++;
785                           j++;
786                         }
787                       n_chained_ops += 1;
788                     }
789                   else
790                     n_ops_static += 1;
791                 }
792               break;
793             case VNET_CRYPTO_OP_TYPE_HMAC:
794               if (r->plaintext_incremental)
795                 {
796                   computed_data_total_incr_len += r->digest.length;
797                   n_ops_incr += 1;
798                   generate_digest (vm, r, id);
799                 }
800               else
801                 {
802                   computed_data_total_len += r->digest.length;
803                   if (r->is_chained)
804                     n_chained_ops += 1;
805                   else
806                     n_ops_static += 1;
807                 }
808               break;
809             case VNET_CRYPTO_OP_TYPE_HASH:
810               computed_data_total_len += r->digest.length;
811               n_ops_static += 1;
812               break;
813             default:
814               break;
815             };
816         }
817
818       /* next: */
819       r = r->next;
820     }
821   restore_engines (saved_engs);
822
823   err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
824                             computed_data_total_len);
825   if (err)
826     goto done;
827
828   err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
829                                  computed_data_total_incr_len);
830
831   r = tm->test_registrations;
832   while (r)
833     {
834       if (r->plaintext_incremental)
835         vec_free (r->digest.data);
836       r = r->next;
837     }
838
839 done:
840   vec_free (inc_tests);
841   vec_free (static_tests);
842   return err;
843 }
844
845 static clib_error_t *
846 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
847 {
848   vnet_crypto_main_t *cm = &crypto_main;
849   clib_error_t *err = 0;
850   u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
851   u32 *buffer_indices = 0;
852   vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
853   vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
854   vnet_crypto_key_index_t key_index = ~0;
855   u8 key[64];
856   int buffer_size = vlib_buffer_get_default_data_size (vm);
857   u64 seed = clib_cpu_time_now ();
858   u64 t0[5], t1[5], t2[5], n_bytes = 0;
859   int i, j;
860
861   if (tm->buffer_size > buffer_size)
862     return clib_error_return (0, "buffer size must be <= %u", buffer_size);
863
864   rounds = tm->rounds ? tm->rounds : 100;
865   n_buffers = tm->n_buffers ? tm->n_buffers : 256;
866   buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
867   warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
868
869   if (buffer_size > vlib_buffer_get_default_data_size (vm))
870     return clib_error_return (0, "buffer size too big");
871
872   vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
873   vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
874   vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
875
876   n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
877   if (n_alloc != n_buffers)
878     {
879       if (n_alloc)
880         vlib_buffer_free (vm, buffer_indices, n_alloc);
881       err = clib_error_return (0, "buffer alloc failure");
882       goto done;
883     }
884
885   vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
886                    "warmup-rounds %u",
887                    format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
888                    rounds, warmup_rounds);
889   vlib_cli_output (vm, "   cpu-freq %.2f GHz",
890                    (f64) vm->clib_time.clocks_per_second * 1e-9);
891
892   vnet_crypto_op_type_t ot = 0;
893
894   for (i = 0; i < sizeof (key); i++)
895     key[i] = i;
896
897   key_index = vnet_crypto_key_add (vm, tm->alg, key,
898                                    test_crypto_get_key_sz (tm->alg));
899
900   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
901     {
902       vnet_crypto_op_id_t id = ad->op_by_type[i];
903       if (id == 0)
904         continue;
905       ot = i;
906       break;
907     }
908
909   for (i = 0; i < n_buffers; i++)
910     {
911       vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
912       op1 = ops1 + i;
913       op2 = ops2 + i;
914
915       switch (ot)
916         {
917         case VNET_CRYPTO_OP_TYPE_ENCRYPT:
918         case VNET_CRYPTO_OP_TYPE_DECRYPT:
919           vnet_crypto_op_init (op1,
920                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
921           vnet_crypto_op_init (op2,
922                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
923           op1->src = op2->src = op1->dst = op2->dst = b->data;
924           op1->key_index = op2->key_index = key_index;
925           op1->iv = op2->iv = b->data - 64;
926           n_bytes += op1->len = op2->len = buffer_size;
927           break;
928         case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
929         case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
930           vnet_crypto_op_init (op1,
931                                ad->op_by_type
932                                [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
933           vnet_crypto_op_init (op2,
934                                ad->op_by_type
935                                [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
936           op1->src = op2->src = op1->dst = op2->dst = b->data;
937           op1->key_index = op2->key_index = key_index;
938           op1->tag = op2->tag = b->data - 32;
939           op1->iv = op2->iv = b->data - 64;
940           op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
941           op1->aad_len = op2->aad_len = 64;
942           op1->tag_len = op2->tag_len = 16;
943           n_bytes += op1->len = op2->len = buffer_size;
944           break;
945         case VNET_CRYPTO_OP_TYPE_HMAC:
946           vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
947           op1->src = b->data;
948           op1->key_index = key_index;
949           op1->iv = 0;
950           op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
951           op1->digest_len = 0;
952           n_bytes += op1->len = buffer_size;
953           break;
954         default:
955           return 0;
956         }
957
958       for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
959         *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
960     }
961
962   for (i = 0; i < 5; i++)
963     {
964       for (j = 0; j < warmup_rounds; j++)
965         {
966           vnet_crypto_process_ops (vm, ops1, n_buffers);
967           if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
968             vnet_crypto_process_ops (vm, ops2, n_buffers);
969         }
970
971       t0[i] = clib_cpu_time_now ();
972       for (j = 0; j < rounds; j++)
973         vnet_crypto_process_ops (vm, ops1, n_buffers);
974       t1[i] = clib_cpu_time_now ();
975
976       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
977         {
978           for (j = 0; j < rounds; j++)
979             vnet_crypto_process_ops (vm, ops2, n_buffers);
980           t2[i] = clib_cpu_time_now ();
981         }
982     }
983
984   for (i = 0; i < 5; i++)
985     {
986       f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
987       f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
988       f64 tpb2, gbps2;
989
990       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
991         {
992           tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
993           gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
994           vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
995                            "decrypt %.03f ticks/byte, %.02f Gbps",
996                            i + 1, tpb1, gbps1, tpb2, gbps2);
997         }
998       else
999         {
1000           vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
1001                            i + 1, tpb1, gbps1);
1002         }
1003     }
1004
1005 done:
1006   if (n_alloc)
1007     vlib_buffer_free (vm, buffer_indices, n_alloc);
1008
1009   if (key_index != ~0)
1010     vnet_crypto_key_del (vm, key_index);
1011
1012   vec_free (buffer_indices);
1013   vec_free (ops1);
1014   vec_free (ops2);
1015   return err;
1016 }
1017
1018 static clib_error_t *
1019 test_crypto_command_fn (vlib_main_t * vm,
1020                         unformat_input_t * input, vlib_cli_command_t * cmd)
1021 {
1022   crypto_test_main_t *tm = &crypto_test_main;
1023   unittest_crypto_test_registration_t *tr;
1024   int is_perf = 0;
1025
1026   tr = tm->test_registrations;
1027   memset (tm, 0, sizeof (crypto_test_main_t));
1028   tm->test_registrations = tr;
1029   tm->alg = ~0;
1030
1031   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1032     {
1033       if (unformat (input, "verbose"))
1034         tm->verbose = 1;
1035       else if (unformat (input, "detail"))
1036         tm->verbose = 2;
1037       else
1038         if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1039         is_perf = 1;
1040       else if (unformat (input, "buffers %u", &tm->n_buffers))
1041         ;
1042       else if (unformat (input, "rounds %u", &tm->rounds))
1043         ;
1044       else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1045         ;
1046       else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1047         ;
1048       else
1049         return clib_error_return (0, "unknown input '%U'",
1050                                   format_unformat_error, input);
1051     }
1052
1053   if (is_perf)
1054     return test_crypto_perf (vm, tm);
1055   else
1056     return test_crypto (vm, tm);
1057 }
1058
1059 VLIB_CLI_COMMAND (test_crypto_command, static) =
1060 {
1061   .path = "test crypto",
1062   .short_help = "test crypto",
1063   .function = test_crypto_command_fn,
1064 };
1065
1066 static clib_error_t *
1067 crypto_test_init (vlib_main_t * vm)
1068 {
1069   return (0);
1070 }
1071
1072 VLIB_INIT_FUNCTION (crypto_test_init);
1073
1074 /*
1075  * fd.io coding-style-patch-verification: ON
1076  *
1077  * Local Variables:
1078  * eval: (c-set-style "gnu")
1079  * End:
1080  */