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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
22 crypto_test_main_t crypto_test_main;
25 sort_registrations (void *a0, void *a1)
27 unittest_crypto_test_registration_t **r0 = a0;
28 unittest_crypto_test_registration_t **r1 = a1;
30 return (strncmp (r0[0]->name, r1[0]->name, 256));
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)
39 unittest_crypto_test_registration_t *r;
40 vnet_crypto_op_chunk_t *chp;
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;
52 switch (vnet_crypto_get_op_type (op->op))
54 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
57 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
58 exp_ct = &r->ciphertext;
59 exp_ct_chunks = r->ct_chunks;
61 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
62 case VNET_CRYPTO_OP_TYPE_DECRYPT:
63 if (r->plaintext_incremental)
65 exp_pt_data.length = r->plaintext_incremental;
66 exp_pt_data.data = tm->inc_data;
67 exp_pt = &exp_pt_data;
71 exp_pt = &r->plaintext;
72 exp_pt_chunks = r->pt_chunks;
75 case VNET_CRYPTO_OP_TYPE_HMAC:
76 exp_digest = &r->digest;
82 vec_reset_length (err);
84 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
85 err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
86 format_vnet_crypto_op_status, op->status);
88 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
92 chp = vec_elt_at_index (chunks, op->chunk_index);
93 for (i = 0; i < op->n_chunks; i++)
95 if (memcmp (chp->dst, exp_ct_chunks[i].data, chp->len))
96 err = format (err, "%sciphertext mismatch [chunk %d]",
97 vec_len (err) ? ", " : "", i);
104 chp = vec_elt_at_index (chunks, op->chunk_index);
105 for (i = 0; i < op->n_chunks; i++)
107 if (memcmp (chp->dst, exp_pt_chunks[i].data, chp->len))
108 err = format (err, "%splaintext mismatch [chunk %d]",
109 vec_len (err) ? ", " : "", i);
116 if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
117 err = format (err, "%sciphertext mismatch",
118 vec_len (err) ? ", " : "");
120 if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
121 err = format (err, "%splaintext mismatch",
122 vec_len (err) ? ", " : "");
125 if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
126 err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
129 memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
130 err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
132 vec_reset_length (s);
133 s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op,
139 vlib_cli_output (vm, "%-60v%s%v", s, vec_len (err) ? "FAIL: " : "OK",
143 if (tm->verbose == 2)
147 vlib_cli_output (vm, "Expected ciphertext:\n%U"
148 "\nCalculated ciphertext:\n%U",
149 format_hexdump, exp_ct->data, exp_ct->length,
150 format_hexdump, op->dst, exp_ct->length);
152 vlib_cli_output (vm, "Expected plaintext:\n%U"
153 "\nCalculated plaintext:\n%U",
154 format_hexdump, exp_pt->data, exp_pt->length,
155 format_hexdump, op->dst, exp_pt->length);
156 if (r->tag.length && fail)
157 vlib_cli_output (vm, "Expected tag:\n%U"
158 "\nCalculated tag:\n%U",
159 format_hexdump, r->tag.data, r->tag.length,
160 format_hexdump, op->tag, op->tag_len);
161 if (exp_digest && fail)
162 vlib_cli_output (vm, "Expected digest:\n%U"
163 "\nCalculated Digest:\n%U",
164 format_hexdump, exp_digest->data,
165 exp_digest->length, format_hexdump, op->digest,
174 validate_data (u8 ** data, u32 len)
176 u32 i, diff, old_len;
177 if (vec_len (data[0]) >= len)
180 old_len = vec_len (data[0]);
181 diff = len - vec_len (data[0]);
182 vec_validate (data[0], old_len + diff - 1);
183 for (i = old_len; i < len; i++)
188 generate_digest (vlib_main_t * vm,
189 unittest_crypto_test_registration_t * r,
190 vnet_crypto_op_id_t id)
192 crypto_test_main_t *cm = &crypto_test_main;
193 vnet_crypto_op_t op[1];
194 vnet_crypto_op_init (op, id);
195 vec_validate (r->digest.data, r->digest.length - 1);
196 op->src = cm->inc_data;
197 op->len = r->plaintext_incremental;
198 op->digest = r->digest.data;
199 op->digest_len = r->digest.length;
200 op->key_index = vnet_crypto_key_add (vm, r->alg,
201 cm->inc_data, r->key.length);
203 /* at this point openssl is set for each algo */
204 vnet_crypto_process_ops (vm, op, 1);
208 restore_engines (u32 * engs)
211 vnet_crypto_main_t *cm = &crypto_main;
213 vnet_crypto_engine_t *ce;
215 for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
217 vnet_crypto_op_data_t *od = &cm->opt_data[i];
221 ce = vec_elt_at_index (cm->engines, engs[i]);
222 od->active_engine_index_simple = engs[i];
223 cm->ops_handlers[i] = ce->ops_handlers[i];
231 save_current_engines (u32 * engs)
234 vnet_crypto_main_t *cm = &crypto_main;
237 vnet_crypto_engine_t *ce;
239 p = hash_get_mem (cm->engine_index_by_name, "openssl");
243 ce = vec_elt_at_index (cm->engines, p[0]);
245 /* set openssl for all crypto algs to generate expected data */
246 for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
248 vnet_crypto_op_data_t *od = &cm->opt_data[i];
249 if (od->active_engine_index_simple != ~0)
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];
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)
266 vnet_crypto_main_t *cm = &crypto_main;
267 vnet_crypto_alg_data_t *ad;
268 vnet_crypto_key_index_t *key_indices = 0;
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;
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;
285 /* first stage: encrypt only */
287 vec_foreach_index (i, rv)
291 ad = vec_elt_at_index (cm->algs, r->alg);
292 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
294 vnet_crypto_op_id_t id = ad->op_by_type[t];
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;
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;
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;
345 vnet_crypto_process_ops (vm, ops, n_ops);
346 computed_data_total_len = 0;
348 /* second stage: hash/decrypt previously encrypted data */
351 vec_foreach_index (i, rv)
355 ad = vec_elt_at_index (cm->algs, r->alg);
356 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
358 vnet_crypto_op_id_t id = ad->op_by_type[t];
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;
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;
391 op->tag = encrypted_data + computed_data_total_len;
392 computed_data_total_len += r->tag.length;
393 op->tag_len = r->tag.length;
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;
417 vnet_crypto_process_ops (vm, ops, n_ops);
418 print_results (vm, rv, ops, 0, n_ops, tm);
420 vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
421 vec_free (tm->inc_data);
423 vec_free (encrypted_data);
424 vec_free (decrypted_data);
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)
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;
446 vec_sort_with_function (rv, sort_registrations);
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;
456 current_chained_op = chained_ops;
458 vec_foreach_index (i, rv)
462 ad = vec_elt_at_index (cm->algs, r->alg);
463 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
465 vnet_crypto_op_id_t id = ad->op_by_type[t];
472 op = current_chained_op;
473 current_chained_op += 1;
481 vnet_crypto_op_init (op, id);
485 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
486 case VNET_CRYPTO_OP_TYPE_DECRYPT:
488 op->key_index = vnet_crypto_key_add (vm, r->alg,
491 vec_add1 (key_indices, op->key_index);
497 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
498 op->chunk_index = vec_len (chunks);
501 ch.src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
504 ch.dst = computed_data + computed_data_total_len;
505 computed_data_total_len += pt->length;
506 vec_add1 (chunks, ch);
514 op->len = r->plaintext.length;
515 op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
516 r->plaintext.data : r->ciphertext.data;
517 op->dst = computed_data + computed_data_total_len;
518 computed_data_total_len += r->ciphertext.length;
521 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
522 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
526 op->key_index = vnet_crypto_key_add (vm, r->alg,
529 vec_add1 (key_indices, op->key_index);
530 op->aad = r->aad.data;
531 op->aad_len = r->aad.length;
532 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
535 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
536 op->chunk_index = vec_len (chunks);
541 ch.dst = computed_data + computed_data_total_len;
542 computed_data_total_len += pt->length;
543 vec_add1 (chunks, ch);
547 op->tag = computed_data + computed_data_total_len;
548 computed_data_total_len += r->tag.length;
553 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
554 op->chunk_index = vec_len (chunks);
559 ch.dst = computed_data + computed_data_total_len;
560 computed_data_total_len += ct->length;
561 vec_add1 (chunks, ch);
565 op->tag = r->tag.data;
567 op->tag_len = r->tag.length;
572 op->key_index = vnet_crypto_key_add (vm, r->alg,
575 vec_add1 (key_indices, op->key_index);
576 op->aad = r->aad.data;
577 op->aad_len = r->aad.length;
578 op->len = r->plaintext.length;
579 op->dst = computed_data + computed_data_total_len;
580 computed_data_total_len += r->ciphertext.length;
582 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
584 op->src = r->plaintext.data;
585 op->tag = computed_data + computed_data_total_len;
586 computed_data_total_len += r->tag.length;
590 op->tag = r->tag.data;
591 op->src = r->ciphertext.data;
593 op->tag_len = r->tag.length;
596 case VNET_CRYPTO_OP_TYPE_HMAC:
599 op->key_index = vnet_crypto_key_add (vm, r->alg,
602 vec_add1 (key_indices, op->key_index);
603 op->digest_len = r->digest.length;
604 op->digest = computed_data + computed_data_total_len;
605 computed_data_total_len += r->digest.length;
607 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
608 op->chunk_index = vec_len (chunks);
613 vec_add1 (chunks, ch);
620 op->key_index = vnet_crypto_key_add (vm, r->alg,
623 vec_add1 (key_indices, op->key_index);
624 op->digest_len = r->digest.length;
625 op->digest = computed_data + computed_data_total_len;
626 computed_data_total_len += r->digest.length;
627 op->src = r->plaintext.data;
628 op->len = r->plaintext.length;
640 vnet_crypto_process_ops (vm, ops, vec_len (ops));
641 vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
642 vec_len (chained_ops));
644 print_results (vm, rv, ops, chunks, vec_len (ops), tm);
645 print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
647 vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
649 vec_free (computed_data);
651 vec_free (chained_ops);
657 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
662 case VNET_CRYPTO_ALG_##n: \
665 foreach_crypto_cipher_alg
666 foreach_crypto_aead_alg
669 case VNET_CRYPTO_ALG_HMAC_MD5:
670 case VNET_CRYPTO_ALG_HMAC_SHA1:
672 case VNET_CRYPTO_ALG_HMAC_SHA224:
674 case VNET_CRYPTO_ALG_HMAC_SHA256:
676 case VNET_CRYPTO_ALG_HMAC_SHA384:
678 case VNET_CRYPTO_ALG_HMAC_SHA512:
686 static clib_error_t *
687 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
689 clib_error_t *err = 0;
690 vnet_crypto_main_t *cm = &crypto_main;
691 unittest_crypto_test_registration_t *r = tm->test_registrations;
692 unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
693 u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
694 vnet_crypto_alg_data_t *ad;
695 u32 computed_data_total_len = 0;
696 u32 computed_data_total_incr_len = 0;
697 u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
698 unittest_crypto_test_data_t *ct;
700 /* pre-allocate plaintext data with reasonable length */
701 validate_data (&tm->inc_data, 2048);
703 int rc = save_current_engines (saved_engs);
705 return clib_error_return (0, "failed to set default crypto engine!");
707 /* construct registration vector */
710 if (r->plaintext_incremental)
711 vec_add1 (inc_tests, r);
713 vec_add1 (static_tests, r);
715 ad = vec_elt_at_index (cm->algs, r->alg);
717 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
719 vnet_crypto_op_id_t id = ad->op_by_type[i];
726 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
727 if (r->plaintext_incremental)
729 computed_data_total_incr_len += r->plaintext_incremental;
733 case VNET_CRYPTO_OP_TYPE_DECRYPT:
734 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
741 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
742 return clib_error_return (0,
743 "test case '%s' exceeds extra data!",
745 computed_data_total_len += ct->length;
751 else if (!r->plaintext_incremental)
753 computed_data_total_len += r->ciphertext.length;
757 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
758 if (r->plaintext_incremental)
760 computed_data_total_incr_len += r->plaintext_incremental;
761 computed_data_total_incr_len += r->tag.length;
766 computed_data_total_len += r->ciphertext.length;
767 computed_data_total_len += r->tag.length;
774 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
775 return clib_error_return (0,
776 "test case '%s' exceeds extra data!",
778 computed_data_total_len += ct->length;
788 case VNET_CRYPTO_OP_TYPE_HMAC:
789 if (r->plaintext_incremental)
791 computed_data_total_incr_len += r->digest.length;
793 generate_digest (vm, r, id);
797 computed_data_total_len += r->digest.length;
812 restore_engines (saved_engs);
814 err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
815 computed_data_total_len);
819 err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
820 computed_data_total_incr_len);
822 r = tm->test_registrations;
825 if (r->plaintext_incremental)
826 vec_free (r->digest.data);
831 vec_free (inc_tests);
832 vec_free (static_tests);
836 static clib_error_t *
837 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
839 vnet_crypto_main_t *cm = &crypto_main;
840 clib_error_t *err = 0;
841 u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
842 u32 *buffer_indices = 0;
843 vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
844 vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
845 vnet_crypto_key_index_t key_index = ~0;
847 int buffer_size = vlib_buffer_get_default_data_size (vm);
848 u64 seed = clib_cpu_time_now ();
849 u64 t0[5], t1[5], t2[5], n_bytes = 0;
852 if (tm->buffer_size > buffer_size)
853 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
855 rounds = tm->rounds ? tm->rounds : 100;
856 n_buffers = tm->n_buffers ? tm->n_buffers : 256;
857 buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
858 warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
860 if (buffer_size > vlib_buffer_get_default_data_size (vm))
861 return clib_error_return (0, "buffer size too big");
863 vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
864 vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
865 vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
867 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
868 if (n_alloc != n_buffers)
871 vlib_buffer_free (vm, buffer_indices, n_alloc);
872 err = clib_error_return (0, "buffer alloc failure");
876 vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
878 format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
879 rounds, warmup_rounds);
880 vlib_cli_output (vm, " cpu-freq %.2f GHz",
881 (f64) vm->clib_time.clocks_per_second * 1e-9);
883 vnet_crypto_op_type_t ot = 0;
885 for (i = 0; i < sizeof (key); i++)
888 key_index = vnet_crypto_key_add (vm, tm->alg, key,
889 test_crypto_get_key_sz (tm->alg));
891 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
893 vnet_crypto_op_id_t id = ad->op_by_type[i];
900 for (i = 0; i < n_buffers; i++)
902 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
908 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
909 case VNET_CRYPTO_OP_TYPE_DECRYPT:
910 vnet_crypto_op_init (op1,
911 ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
912 vnet_crypto_op_init (op2,
913 ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
914 op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
915 op1->src = op2->src = op1->dst = op2->dst = b->data;
916 op1->key_index = op2->key_index = key_index;
917 op1->iv = op2->iv = b->data - 64;
918 n_bytes += op1->len = op2->len = buffer_size;
920 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
921 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
922 vnet_crypto_op_init (op1,
924 [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
925 vnet_crypto_op_init (op2,
927 [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
928 op1->src = op2->src = op1->dst = op2->dst = b->data;
929 op1->key_index = op2->key_index = key_index;
930 op1->tag = op2->tag = b->data - 32;
931 op1->iv = op2->iv = b->data - 64;
932 op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
933 op1->aad_len = op2->aad_len = 64;
934 op1->tag_len = op2->tag_len = 16;
935 n_bytes += op1->len = op2->len = buffer_size;
937 case VNET_CRYPTO_OP_TYPE_HMAC:
938 vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
940 op1->key_index = key_index;
942 op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
944 n_bytes += op1->len = buffer_size;
950 for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
951 *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
954 for (i = 0; i < 5; i++)
956 for (j = 0; j < warmup_rounds; j++)
958 vnet_crypto_process_ops (vm, ops1, n_buffers);
959 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
960 vnet_crypto_process_ops (vm, ops2, n_buffers);
963 t0[i] = clib_cpu_time_now ();
964 for (j = 0; j < rounds; j++)
965 vnet_crypto_process_ops (vm, ops1, n_buffers);
966 t1[i] = clib_cpu_time_now ();
968 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
970 for (j = 0; j < rounds; j++)
971 vnet_crypto_process_ops (vm, ops2, n_buffers);
972 t2[i] = clib_cpu_time_now ();
976 for (i = 0; i < 5; i++)
978 f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
979 f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
982 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
984 tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
985 gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
986 vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
987 "decrypt %.03f ticks/byte, %.02f Gbps",
988 i + 1, tpb1, gbps1, tpb2, gbps2);
992 vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
999 vlib_buffer_free (vm, buffer_indices, n_alloc);
1001 if (key_index != ~0)
1002 vnet_crypto_key_del (vm, key_index);
1004 vec_free (buffer_indices);
1010 static clib_error_t *
1011 test_crypto_command_fn (vlib_main_t * vm,
1012 unformat_input_t * input, vlib_cli_command_t * cmd)
1014 crypto_test_main_t *tm = &crypto_test_main;
1015 unittest_crypto_test_registration_t *tr;
1018 tr = tm->test_registrations;
1019 memset (tm, 0, sizeof (crypto_test_main_t));
1020 tm->test_registrations = tr;
1023 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1025 if (unformat (input, "verbose"))
1027 else if (unformat (input, "detail"))
1030 if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1032 else if (unformat (input, "buffers %u", &tm->n_buffers))
1034 else if (unformat (input, "rounds %u", &tm->rounds))
1036 else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1038 else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1041 return clib_error_return (0, "unknown input '%U'",
1042 format_unformat_error, input);
1046 return test_crypto_perf (vm, tm);
1048 return test_crypto (vm, tm);
1052 VLIB_CLI_COMMAND (test_crypto_command, static) =
1054 .path = "test crypto",
1055 .short_help = "test crypto",
1056 .function = test_crypto_command_fn,
1060 static clib_error_t *
1061 crypto_test_init (vlib_main_t * vm)
1066 VLIB_INIT_FUNCTION (crypto_test_init);
1069 * fd.io coding-style-patch-verification: ON
1072 * eval: (c-set-style "gnu")