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