9030415aec8ea05bf4fed661a9cf8ab8972ce022
[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 clib_error_t *
34 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
35 {
36   vnet_crypto_main_t *cm = &crypto_main;
37   unittest_crypto_test_registration_t *r = tm->test_registrations;
38   unittest_crypto_test_registration_t **rv = 0;
39   vnet_crypto_alg_data_t *ad;
40   vnet_crypto_op_t *ops = 0, *op;
41   vnet_crypto_key_index_t *key_indices = 0;
42   u8 *computed_data = 0, *s = 0, *err = 0;
43   u32 computed_data_total_len = 0, n_ops = 0;
44   u32 i;
45
46   /* construct registration vector */
47   while (r)
48     {
49       vec_add1 (rv, r);
50       ad = vec_elt_at_index (cm->algs, r->alg);
51
52       for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
53         {
54           vnet_crypto_op_id_t id = ad->op_by_type[i];
55
56           if (id == 0)
57             continue;
58
59           switch (i)
60             {
61             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
62             case VNET_CRYPTO_OP_TYPE_DECRYPT:
63             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
64               computed_data_total_len += r->ciphertext.length;
65               n_ops += 1;
66               break;
67             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
68               computed_data_total_len += r->ciphertext.length;
69               computed_data_total_len += r->tag.length;
70               n_ops += 1;
71               break;
72             case VNET_CRYPTO_OP_TYPE_HMAC:
73               computed_data_total_len += r->digest.length;
74               n_ops += 1;
75               break;
76             default:
77               break;
78             };
79         }
80
81       /* next */
82       r = r->next;
83     }
84
85   /* no tests registered */
86   if (n_ops == 0)
87     return 0;
88
89   vec_sort_with_function (rv, sort_registrations);
90
91   vec_validate_aligned (computed_data, computed_data_total_len - 1,
92                         CLIB_CACHE_LINE_BYTES);
93   vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
94   computed_data_total_len = 0;
95
96   op = ops;
97   /* *INDENT-OFF* */
98   vec_foreach_index (i, rv)
99     {
100       r = rv[i];
101       int t;
102       ad = vec_elt_at_index (cm->algs, r->alg);
103       for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
104         {
105           vnet_crypto_op_id_t id = ad->op_by_type[t];
106
107           if (id == 0)
108             continue;
109
110           vnet_crypto_op_init (op, id);
111
112           switch (t)
113             {
114             case VNET_CRYPTO_OP_TYPE_ENCRYPT:
115             case VNET_CRYPTO_OP_TYPE_DECRYPT:
116               op->iv = r->iv.data;
117               op->key_index = vnet_crypto_key_add (vm, r->alg,
118                                                    r->key.data,
119                                                    r->key.length);
120               vec_add1 (key_indices, op->key_index);
121               op->len = r->plaintext.length;
122               op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
123                 r->plaintext.data : r->ciphertext.data;
124               op->dst = computed_data + computed_data_total_len;
125               computed_data_total_len += r->ciphertext.length;
126               break;
127             case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
128             case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
129               op->iv = r->iv.data;
130               op->key_index = vnet_crypto_key_add (vm, r->alg,
131                                                    r->key.data,
132                                                    r->key.length);
133               vec_add1 (key_indices, op->key_index);
134               op->aad = r->aad.data;
135               op->aad_len = r->aad.length;
136               op->len = r->plaintext.length;
137               op->dst = computed_data + computed_data_total_len;
138               computed_data_total_len += r->ciphertext.length;
139               if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
140                 {
141                   op->src = r->plaintext.data;
142                   op->tag = computed_data + computed_data_total_len;
143                   computed_data_total_len += r->tag.length;
144                 }
145               else
146                 {
147                   op->src = r->ciphertext.data;
148                   op->tag = r->tag.data;
149                 }
150               op->tag_len = r->tag.length;
151               break;
152             case VNET_CRYPTO_OP_TYPE_HMAC:
153               op->key_index = vnet_crypto_key_add (vm, r->alg,
154                                                    r->key.data,
155                                                    r->key.length);
156               vec_add1 (key_indices, op->key_index);
157               op->src = r->plaintext.data;
158               op->len = r->plaintext.length;
159               op->digest_len = r->digest.length;
160               op->digest = computed_data + computed_data_total_len;
161               computed_data_total_len += r->digest.length;
162               break;
163             default:
164               break;
165             };
166
167           op->user_data = i;
168           op++;
169         }
170     }
171   /* *INDENT-ON* */
172
173   vnet_crypto_process_ops (vm, ops, vec_len (ops));
174
175   /* *INDENT-OFF* */
176   vec_foreach (op, ops)
177     {
178       int fail = 0;
179       r = rv[op->user_data];
180       unittest_crypto_test_data_t *exp_pt = 0, *exp_ct = 0;
181       unittest_crypto_test_data_t *exp_digest = 0, *exp_tag = 0;
182
183       switch (vnet_crypto_get_op_type (op->op))
184         {
185         case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
186           exp_tag = &r->tag;
187           /* fall through */
188         case VNET_CRYPTO_OP_TYPE_ENCRYPT:
189           exp_ct = &r->ciphertext;
190           break;
191         case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
192         case VNET_CRYPTO_OP_TYPE_DECRYPT:
193           exp_pt = &r->plaintext;
194           break;
195         case VNET_CRYPTO_OP_TYPE_HMAC:
196           exp_digest = &r->digest;
197           break;
198         default:
199           break;
200         }
201
202       vec_reset_length (err);
203
204       if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
205         err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
206                       format_vnet_crypto_op_status, op->status);
207
208       if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
209         err = format (err, "%sciphertext mismatch",
210                       vec_len (err) ? ", " : "");
211
212       if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
213         err = format (err, "%splaintext mismatch", vec_len (err) ? ", " : "");
214
215       if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
216         err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
217
218       if (exp_digest &&
219           memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
220         err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
221
222       vec_reset_length (s);
223       s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op);
224
225       if (vec_len (err))
226         fail = 1;
227
228       vlib_cli_output (vm, "%-60v%s%v", s, vec_len (err) ? "FAIL: " : "OK",
229                        err);
230       if (tm->verbose)
231         {
232           if (tm->verbose == 2)
233             fail = 1;
234
235           if (exp_ct && fail)
236             vlib_cli_output (vm, "Expected ciphertext:\n%U"
237                              "\nCalculated ciphertext:\n%U",
238                              format_hexdump, exp_ct->data, exp_ct->length,
239                              format_hexdump, op->dst, exp_ct->length);
240           if (exp_pt && fail)
241             vlib_cli_output (vm, "Expected plaintext:\n%U"
242                              "\nCalculated plaintext:\n%U",
243                              format_hexdump, exp_pt->data, exp_pt->length,
244                              format_hexdump, op->dst, exp_pt->length);
245           if (r->tag.length && fail)
246             vlib_cli_output (vm, "Expected tag:\n%U"
247                              "\nCalculated tag:\n%U",
248                              format_hexdump, r->tag.data, r->tag.length,
249                              format_hexdump, op->tag, op->tag_len);
250           if (exp_digest && fail)
251             vlib_cli_output (vm, "Expected digest:\n%U"
252                              "\nCalculated Digest:\n%U",
253                              format_hexdump, exp_digest->data,
254                              exp_digest->length, format_hexdump, op->digest,
255                              op->digest_len);
256         }
257     }
258
259   vec_foreach_index (i, key_indices)
260     vnet_crypto_key_del (vm, key_indices[i]);
261   /* *INDENT-ON* */
262
263   vec_free (computed_data);
264   vec_free (ops);
265   vec_free (err);
266   vec_free (rv);
267   vec_free (s);
268   return 0;
269 }
270
271 static u32
272 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
273 {
274   switch (alg)
275     {
276 #define _(n, s, l) \
277   case VNET_CRYPTO_ALG_##n: \
278     return l;
279   /* *INDENT-OFF* */
280   foreach_crypto_cipher_alg
281   foreach_crypto_aead_alg
282   /* *INDENT-ON* */
283 #undef _
284     case VNET_CRYPTO_ALG_HMAC_MD5:
285     case VNET_CRYPTO_ALG_HMAC_SHA1:
286       return 20;
287     case VNET_CRYPTO_ALG_HMAC_SHA224:
288       return 28;
289     case VNET_CRYPTO_ALG_HMAC_SHA256:
290       return 32;
291     case VNET_CRYPTO_ALG_HMAC_SHA384:
292       return 48;
293     case VNET_CRYPTO_ALG_HMAC_SHA512:
294       return 64;
295     default:
296       return 0;
297     }
298
299   return 0;
300 }
301
302 static clib_error_t *
303 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
304 {
305   vnet_crypto_main_t *cm = &crypto_main;
306   clib_error_t *err = 0;
307   u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
308   u32 *buffer_indices = 0;
309   vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
310   vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
311   vnet_crypto_key_index_t key_index = ~0;
312   u8 key[32];
313   int buffer_size = vlib_buffer_get_default_data_size (vm);
314   u64 seed = clib_cpu_time_now ();
315   u64 t0[5], t1[5], t2[5], n_bytes = 0;
316   int i, j;
317
318   if (tm->buffer_size > buffer_size)
319     return clib_error_return (0, "buffer size must be <= %u", buffer_size);
320
321   rounds = tm->rounds ? tm->rounds : 100;
322   n_buffers = tm->n_buffers ? tm->n_buffers : 256;
323   buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
324   warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
325
326   if (buffer_size > vlib_buffer_get_default_data_size (vm))
327     return clib_error_return (0, "buffer size too big");
328
329   vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
330   vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
331   vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
332
333   n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
334   if (n_alloc != n_buffers)
335     {
336       if (n_alloc)
337         vlib_buffer_free (vm, buffer_indices, n_alloc);
338       err = clib_error_return (0, "buffer alloc failure");
339       goto done;
340     }
341
342   vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
343                    "warmup-rounds %u",
344                    format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
345                    rounds, warmup_rounds);
346   vlib_cli_output (vm, "   cpu-freq %.2f GHz",
347                    (f64) vm->clib_time.clocks_per_second * 1e-9);
348
349   vnet_crypto_op_type_t ot = 0;
350
351   for (i = 0; i < sizeof (key); i++)
352     key[i] = i;
353
354   key_index = vnet_crypto_key_add (vm, tm->alg, key,
355                                    test_crypto_get_key_sz (tm->alg));
356
357   for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
358     {
359       vnet_crypto_op_id_t id = ad->op_by_type[i];
360       if (id == 0)
361         continue;
362       ot = i;
363       break;
364     }
365
366   for (i = 0; i < n_buffers; i++)
367     {
368       vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
369       op1 = ops1 + i;
370       op2 = ops2 + i;
371
372       switch (ot)
373         {
374         case VNET_CRYPTO_OP_TYPE_ENCRYPT:
375         case VNET_CRYPTO_OP_TYPE_DECRYPT:
376           vnet_crypto_op_init (op1,
377                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
378           vnet_crypto_op_init (op2,
379                                ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
380           op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
381           op1->src = op2->src = op1->dst = op2->dst = b->data;
382           op1->key_index = op2->key_index = key_index;
383           op1->iv = op2->iv = b->data - 64;
384           n_bytes += op1->len = op2->len = buffer_size;
385           break;
386         case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
387         case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
388           vnet_crypto_op_init (op1,
389                                ad->op_by_type
390                                [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
391           vnet_crypto_op_init (op2,
392                                ad->op_by_type
393                                [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
394           op1->src = op2->src = op1->dst = op2->dst = b->data;
395           op1->key_index = op2->key_index = key_index;
396           op1->tag = op2->tag = b->data - 32;
397           op1->iv = op2->iv = b->data - 64;
398           op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
399           op1->aad_len = op2->aad_len = 64;
400           op1->tag_len = op2->tag_len = 16;
401           n_bytes += op1->len = op2->len = buffer_size;
402           break;
403         case VNET_CRYPTO_OP_TYPE_HMAC:
404           vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
405           op1->src = b->data;
406           op1->key_index = key_index;
407           op1->iv = 0;
408           op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
409           op1->digest_len = 0;
410           n_bytes += op1->len = buffer_size;
411           break;
412         default:
413           return 0;
414         }
415
416       for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
417         *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
418     }
419
420   for (i = 0; i < 5; i++)
421     {
422       for (j = 0; j < warmup_rounds; j++)
423         {
424           vnet_crypto_process_ops (vm, ops1, n_buffers);
425           if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
426             vnet_crypto_process_ops (vm, ops2, n_buffers);
427         }
428
429       t0[i] = clib_cpu_time_now ();
430       for (j = 0; j < rounds; j++)
431         vnet_crypto_process_ops (vm, ops1, n_buffers);
432       t1[i] = clib_cpu_time_now ();
433
434       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
435         {
436           for (j = 0; j < rounds; j++)
437             vnet_crypto_process_ops (vm, ops2, n_buffers);
438           t2[i] = clib_cpu_time_now ();
439         }
440     }
441
442   for (i = 0; i < 5; i++)
443     {
444       f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
445       f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
446       f64 tpb2, gbps2;
447
448       if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
449         {
450           tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
451           gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
452           vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
453                            "decrypt %.03f ticks/byte, %.02f Gbps",
454                            i + 1, tpb1, gbps1, tpb2, gbps2);
455         }
456       else
457         {
458           vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
459                            i + 1, tpb1, gbps1);
460         }
461     }
462
463 done:
464   if (n_alloc)
465     vlib_buffer_free (vm, buffer_indices, n_alloc);
466
467   if (key_index != ~0)
468     vnet_crypto_key_del (vm, key_index);
469
470   vec_free (buffer_indices);
471   vec_free (ops1);
472   vec_free (ops2);
473   return err;
474 }
475
476 static clib_error_t *
477 test_crypto_command_fn (vlib_main_t * vm,
478                         unformat_input_t * input, vlib_cli_command_t * cmd)
479 {
480   crypto_test_main_t *tm = &crypto_test_main;
481   unittest_crypto_test_registration_t *tr;
482   int is_perf = 0;
483
484   tr = tm->test_registrations;
485   memset (tm, 0, sizeof (crypto_test_main_t));
486   tm->test_registrations = tr;
487   tm->alg = ~0;
488
489   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
490     {
491       if (unformat (input, "verbose"))
492         tm->verbose = 1;
493       else if (unformat (input, "detail"))
494         tm->verbose = 2;
495       else
496         if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
497         is_perf = 1;
498       else if (unformat (input, "buffers %u", &tm->n_buffers))
499         ;
500       else if (unformat (input, "rounds %u", &tm->rounds))
501         ;
502       else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
503         ;
504       else if (unformat (input, "buffer-size %u", &tm->buffer_size))
505         ;
506       else
507         return clib_error_return (0, "unknown input '%U'",
508                                   format_unformat_error, input);
509     }
510
511   if (is_perf)
512     return test_crypto_perf (vm, tm);
513   else
514     return test_crypto (vm, tm);
515 }
516
517 /* *INDENT-OFF* */
518 VLIB_CLI_COMMAND (test_crypto_command, static) =
519 {
520   .path = "test crypto",
521   .short_help = "test crypto",
522   .function = test_crypto_command_fn,
523 };
524 /* *INDENT-ON* */
525
526 static clib_error_t *
527 crypto_test_init (vlib_main_t * vm)
528 {
529   return (0);
530 }
531
532 VLIB_INIT_FUNCTION (crypto_test_init);
533
534 /*
535  * fd.io coding-style-patch-verification: ON
536  *
537  * Local Variables:
538  * eval: (c-set-style "gnu")
539  * End:
540  */