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