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;
78 case VNET_CRYPTO_OP_TYPE_HASH:
79 exp_digest = &r->digest;
85 vec_reset_length (err);
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);
91 if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
95 chp = vec_elt_at_index (chunks, op->chunk_index);
96 for (i = 0; i < op->n_chunks; i++)
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);
107 chp = vec_elt_at_index (chunks, op->chunk_index);
108 for (i = 0; i < op->n_chunks; i++)
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);
119 if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
120 err = format (err, "%sciphertext mismatch",
121 vec_len (err) ? ", " : "");
123 if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
124 err = format (err, "%splaintext mismatch",
125 vec_len (err) ? ", " : "");
128 if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
129 err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
132 memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
133 err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
135 vec_reset_length (s);
136 s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op,
142 vlib_cli_output (vm, "%-65v%s%v", s, vec_len (err) ? "FAIL: " : "OK", err);
145 if (tm->verbose == 2)
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);
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,
176 validate_data (u8 ** data, u32 len)
178 u32 i, diff, old_len;
179 if (vec_len (data[0]) >= len)
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++)
190 generate_digest (vlib_main_t * vm,
191 unittest_crypto_test_registration_t * r,
192 vnet_crypto_op_id_t id)
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);
205 /* at this point openssl is set for each algo */
206 vnet_crypto_process_ops (vm, op, 1);
210 restore_engines (u32 * engs)
212 vnet_crypto_main_t *cm = &crypto_main;
214 vnet_crypto_engine_t *ce;
216 for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
218 vnet_crypto_op_data_t *od = &cm->opt_data[i];
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];
232 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);
539 clib_memset (&ch, 0, sizeof (ch));
542 ch.dst = computed_data + computed_data_total_len;
543 computed_data_total_len += pt->length;
544 vec_add1 (chunks, ch);
548 op->tag = computed_data + computed_data_total_len;
549 computed_data_total_len += r->tag.length;
554 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
555 op->chunk_index = vec_len (chunks);
558 clib_memset (&ch, 0, sizeof (ch));
561 ch.dst = computed_data + computed_data_total_len;
562 computed_data_total_len += ct->length;
563 vec_add1 (chunks, ch);
567 op->tag = r->tag.data;
569 op->tag_len = r->tag.length;
574 op->key_index = vnet_crypto_key_add (vm, r->alg,
577 vec_add1 (key_indices, op->key_index);
578 op->aad = r->aad.data;
579 op->aad_len = r->aad.length;
580 op->len = r->plaintext.length;
581 op->dst = computed_data + computed_data_total_len;
582 computed_data_total_len += r->ciphertext.length;
584 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
586 op->src = r->plaintext.data;
587 op->tag = computed_data + computed_data_total_len;
588 computed_data_total_len += r->tag.length;
592 op->tag = r->tag.data;
593 op->src = r->ciphertext.data;
595 op->tag_len = r->tag.length;
598 case VNET_CRYPTO_OP_TYPE_HMAC:
601 op->key_index = vnet_crypto_key_add (vm, r->alg,
604 vec_add1 (key_indices, op->key_index);
605 op->digest_len = r->digest.length;
606 op->digest = computed_data + computed_data_total_len;
607 computed_data_total_len += r->digest.length;
609 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
610 op->chunk_index = vec_len (chunks);
613 clib_memset (&ch, 0, sizeof (ch));
616 vec_add1 (chunks, ch);
623 op->key_index = vnet_crypto_key_add (vm, r->alg,
626 vec_add1 (key_indices, op->key_index);
627 op->digest_len = r->digest.length;
628 op->digest = computed_data + computed_data_total_len;
629 computed_data_total_len += r->digest.length;
630 op->src = r->plaintext.data;
631 op->len = r->plaintext.length;
634 case VNET_CRYPTO_OP_TYPE_HASH:
635 op->digest = computed_data + computed_data_total_len;
636 computed_data_total_len += r->digest.length;
637 op->src = r->plaintext.data;
638 op->len = r->plaintext.length;
649 vnet_crypto_process_ops (vm, ops, vec_len (ops));
650 vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
651 vec_len (chained_ops));
653 print_results (vm, rv, ops, chunks, vec_len (ops), tm);
654 print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
656 vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
658 vec_free (computed_data);
660 vec_free (chained_ops);
666 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
671 case VNET_CRYPTO_ALG_##n: \
674 foreach_crypto_cipher_alg
675 foreach_crypto_aead_alg
678 case VNET_CRYPTO_ALG_HMAC_MD5:
679 case VNET_CRYPTO_ALG_HMAC_SHA1:
681 case VNET_CRYPTO_ALG_HMAC_SHA224:
683 case VNET_CRYPTO_ALG_HMAC_SHA256:
685 case VNET_CRYPTO_ALG_HMAC_SHA384:
687 case VNET_CRYPTO_ALG_HMAC_SHA512:
695 static clib_error_t *
696 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
698 clib_error_t *err = 0;
699 vnet_crypto_main_t *cm = &crypto_main;
700 unittest_crypto_test_registration_t *r = tm->test_registrations;
701 unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
702 u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
703 vnet_crypto_alg_data_t *ad;
704 u32 computed_data_total_len = 0;
705 u32 computed_data_total_incr_len = 0;
706 u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
707 unittest_crypto_test_data_t *ct;
709 /* pre-allocate plaintext data with reasonable length */
710 validate_data (&tm->inc_data, 2048);
712 int rc = save_current_engines (saved_engs);
714 return clib_error_return (0, "failed to set default crypto engine!");
716 /* construct registration vector */
719 if (r->plaintext_incremental)
720 vec_add1 (inc_tests, r);
722 vec_add1 (static_tests, r);
724 ad = vec_elt_at_index (cm->algs, r->alg);
726 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
728 vnet_crypto_op_id_t id = ad->op_by_type[i];
735 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
736 if (r->plaintext_incremental)
738 computed_data_total_incr_len += r->plaintext_incremental;
742 case VNET_CRYPTO_OP_TYPE_DECRYPT:
743 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
750 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
751 return clib_error_return (0,
752 "test case '%s' exceeds extra data!",
754 computed_data_total_len += ct->length;
760 else if (!r->plaintext_incremental)
762 computed_data_total_len += r->ciphertext.length;
766 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
767 if (r->plaintext_incremental)
769 computed_data_total_incr_len += r->plaintext_incremental;
770 computed_data_total_incr_len += r->tag.length;
775 computed_data_total_len += r->ciphertext.length;
776 computed_data_total_len += r->tag.length;
783 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
784 return clib_error_return (0,
785 "test case '%s' exceeds extra data!",
787 computed_data_total_len += ct->length;
797 case VNET_CRYPTO_OP_TYPE_HMAC:
798 if (r->plaintext_incremental)
800 computed_data_total_incr_len += r->digest.length;
802 generate_digest (vm, r, id);
806 computed_data_total_len += r->digest.length;
813 case VNET_CRYPTO_OP_TYPE_HASH:
814 computed_data_total_len += r->digest.length;
825 restore_engines (saved_engs);
827 err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
828 computed_data_total_len);
832 err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
833 computed_data_total_incr_len);
835 r = tm->test_registrations;
838 if (r->plaintext_incremental)
839 vec_free (r->digest.data);
844 vec_free (inc_tests);
845 vec_free (static_tests);
849 static clib_error_t *
850 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
852 vnet_crypto_main_t *cm = &crypto_main;
853 clib_error_t *err = 0;
854 u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
855 u32 *buffer_indices = 0;
856 vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
857 vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
858 vnet_crypto_key_index_t key_index = ~0;
860 int buffer_size = vlib_buffer_get_default_data_size (vm);
861 u64 seed = clib_cpu_time_now ();
862 u64 t0[5], t1[5], t2[5], n_bytes = 0;
865 if (tm->buffer_size > buffer_size)
866 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
868 rounds = tm->rounds ? tm->rounds : 100;
869 n_buffers = tm->n_buffers ? tm->n_buffers : 256;
870 buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
871 warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
873 if (buffer_size > vlib_buffer_get_default_data_size (vm))
874 return clib_error_return (0, "buffer size too big");
876 vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
877 vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
878 vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
880 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
881 if (n_alloc != n_buffers)
884 vlib_buffer_free (vm, buffer_indices, n_alloc);
885 err = clib_error_return (0, "buffer alloc failure");
889 vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
891 format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
892 rounds, warmup_rounds);
893 vlib_cli_output (vm, " cpu-freq %.2f GHz",
894 (f64) vm->clib_time.clocks_per_second * 1e-9);
896 vnet_crypto_op_type_t ot = 0;
898 for (i = 0; i < sizeof (key); i++)
901 key_index = vnet_crypto_key_add (vm, tm->alg, key,
902 test_crypto_get_key_sz (tm->alg));
904 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
906 vnet_crypto_op_id_t id = ad->op_by_type[i];
913 for (i = 0; i < n_buffers; i++)
915 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
921 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
922 case VNET_CRYPTO_OP_TYPE_DECRYPT:
923 vnet_crypto_op_init (op1,
924 ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
925 vnet_crypto_op_init (op2,
926 ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
927 op1->src = op2->src = op1->dst = op2->dst = b->data;
928 op1->key_index = op2->key_index = key_index;
929 op1->iv = op2->iv = b->data - 64;
930 n_bytes += op1->len = op2->len = buffer_size;
932 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
933 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
934 vnet_crypto_op_init (op1,
936 [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
937 vnet_crypto_op_init (op2,
939 [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
940 op1->src = op2->src = op1->dst = op2->dst = b->data;
941 op1->key_index = op2->key_index = key_index;
942 op1->tag = op2->tag = b->data - 32;
943 op1->iv = op2->iv = b->data - 64;
944 op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
945 op1->aad_len = op2->aad_len = 64;
946 op1->tag_len = op2->tag_len = 16;
947 n_bytes += op1->len = op2->len = buffer_size;
949 case VNET_CRYPTO_OP_TYPE_HMAC:
950 vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
952 op1->key_index = key_index;
954 op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
956 n_bytes += op1->len = buffer_size;
962 for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
963 *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
966 for (i = 0; i < 5; i++)
968 for (j = 0; j < warmup_rounds; j++)
970 vnet_crypto_process_ops (vm, ops1, n_buffers);
971 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
972 vnet_crypto_process_ops (vm, ops2, n_buffers);
975 t0[i] = clib_cpu_time_now ();
976 for (j = 0; j < rounds; j++)
977 vnet_crypto_process_ops (vm, ops1, n_buffers);
978 t1[i] = clib_cpu_time_now ();
980 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
982 for (j = 0; j < rounds; j++)
983 vnet_crypto_process_ops (vm, ops2, n_buffers);
984 t2[i] = clib_cpu_time_now ();
988 for (i = 0; i < 5; i++)
990 f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
991 f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
994 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
996 tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
997 gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
998 vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
999 "decrypt %.03f ticks/byte, %.02f Gbps",
1000 i + 1, tpb1, gbps1, tpb2, gbps2);
1004 vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
1005 i + 1, tpb1, gbps1);
1011 vlib_buffer_free (vm, buffer_indices, n_alloc);
1013 if (key_index != ~0)
1014 vnet_crypto_key_del (vm, key_index);
1016 vec_free (buffer_indices);
1022 static clib_error_t *
1023 test_crypto_command_fn (vlib_main_t * vm,
1024 unformat_input_t * input, vlib_cli_command_t * cmd)
1026 crypto_test_main_t *tm = &crypto_test_main;
1027 unittest_crypto_test_registration_t *tr;
1030 tr = tm->test_registrations;
1031 memset (tm, 0, sizeof (crypto_test_main_t));
1032 tm->test_registrations = tr;
1035 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1037 if (unformat (input, "verbose"))
1039 else if (unformat (input, "detail"))
1042 if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1044 else if (unformat (input, "buffers %u", &tm->n_buffers))
1046 else if (unformat (input, "rounds %u", &tm->rounds))
1048 else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1050 else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1053 return clib_error_return (0, "unknown input '%U'",
1054 format_unformat_error, input);
1058 return test_crypto_perf (vm, tm);
1060 return test_crypto (vm, tm);
1064 VLIB_CLI_COMMAND (test_crypto_command, static) =
1066 .path = "test crypto",
1067 .short_help = "test crypto",
1068 .function = test_crypto_command_fn,
1072 static clib_error_t *
1073 crypto_test_init (vlib_main_t * vm)
1078 VLIB_INIT_FUNCTION (crypto_test_init);
1081 * fd.io coding-style-patch-verification: ON
1084 * eval: (c-set-style "gnu")