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;
457 vec_foreach_index (i, rv)
461 ad = vec_elt_at_index (cm->algs, r->alg);
462 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
464 vnet_crypto_op_id_t id = ad->op_by_type[t];
471 op = current_chained_op;
472 current_chained_op += 1;
480 vnet_crypto_op_init (op, id);
484 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
485 case VNET_CRYPTO_OP_TYPE_DECRYPT:
487 op->key_index = vnet_crypto_key_add (vm, r->alg,
490 vec_add1 (key_indices, op->key_index);
496 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
497 op->chunk_index = vec_len (chunks);
500 ch.src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
503 ch.dst = computed_data + computed_data_total_len;
504 computed_data_total_len += pt->length;
505 vec_add1 (chunks, ch);
513 op->len = r->plaintext.length;
514 op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
515 r->plaintext.data : r->ciphertext.data;
516 op->dst = computed_data + computed_data_total_len;
517 computed_data_total_len += r->ciphertext.length;
520 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
521 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
525 op->key_index = vnet_crypto_key_add (vm, r->alg,
528 vec_add1 (key_indices, op->key_index);
529 op->aad = r->aad.data;
530 op->aad_len = r->aad.length;
531 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
534 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
535 op->chunk_index = vec_len (chunks);
538 clib_memset (&ch, 0, sizeof (ch));
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);
557 clib_memset (&ch, 0, sizeof (ch));
560 ch.dst = computed_data + computed_data_total_len;
561 computed_data_total_len += ct->length;
562 vec_add1 (chunks, ch);
566 op->tag = r->tag.data;
568 op->tag_len = r->tag.length;
573 op->key_index = vnet_crypto_key_add (vm, r->alg,
576 vec_add1 (key_indices, op->key_index);
577 op->aad = r->aad.data;
578 op->aad_len = r->aad.length;
579 op->len = r->plaintext.length;
580 op->dst = computed_data + computed_data_total_len;
581 computed_data_total_len += r->ciphertext.length;
583 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
585 op->src = r->plaintext.data;
586 op->tag = computed_data + computed_data_total_len;
587 computed_data_total_len += r->tag.length;
591 op->tag = r->tag.data;
592 op->src = r->ciphertext.data;
594 op->tag_len = r->tag.length;
597 case VNET_CRYPTO_OP_TYPE_HMAC:
600 op->key_index = vnet_crypto_key_add (vm, r->alg,
603 vec_add1 (key_indices, op->key_index);
604 op->digest_len = r->digest.length;
605 op->digest = computed_data + computed_data_total_len;
606 computed_data_total_len += r->digest.length;
608 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
609 op->chunk_index = vec_len (chunks);
612 clib_memset (&ch, 0, sizeof (ch));
615 vec_add1 (chunks, ch);
622 op->key_index = vnet_crypto_key_add (vm, r->alg,
625 vec_add1 (key_indices, op->key_index);
626 op->digest_len = r->digest.length;
627 op->digest = computed_data + computed_data_total_len;
628 computed_data_total_len += r->digest.length;
629 op->src = r->plaintext.data;
630 op->len = r->plaintext.length;
633 case VNET_CRYPTO_OP_TYPE_HASH:
634 op->digest = computed_data + computed_data_total_len;
635 computed_data_total_len += r->digest.length;
636 op->src = r->plaintext.data;
637 op->len = r->plaintext.length;
647 vnet_crypto_process_ops (vm, ops, vec_len (ops));
648 vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
649 vec_len (chained_ops));
651 print_results (vm, rv, ops, chunks, vec_len (ops), tm);
652 print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
654 vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
656 vec_free (computed_data);
658 vec_free (chained_ops);
664 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
669 case VNET_CRYPTO_ALG_##n: \
671 foreach_crypto_cipher_alg
672 foreach_crypto_aead_alg
674 case VNET_CRYPTO_ALG_HMAC_MD5:
675 case VNET_CRYPTO_ALG_HMAC_SHA1:
677 case VNET_CRYPTO_ALG_HMAC_SHA224:
679 case VNET_CRYPTO_ALG_HMAC_SHA256:
681 case VNET_CRYPTO_ALG_HMAC_SHA384:
683 case VNET_CRYPTO_ALG_HMAC_SHA512:
691 static clib_error_t *
692 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
694 clib_error_t *err = 0;
695 vnet_crypto_main_t *cm = &crypto_main;
696 unittest_crypto_test_registration_t *r = tm->test_registrations;
697 unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
698 u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
699 vnet_crypto_alg_data_t *ad;
700 u32 computed_data_total_len = 0;
701 u32 computed_data_total_incr_len = 0;
702 u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
703 unittest_crypto_test_data_t *ct;
705 /* pre-allocate plaintext data with reasonable length */
706 validate_data (&tm->inc_data, 2048);
708 int rc = save_current_engines (saved_engs);
710 return clib_error_return (0, "failed to set default crypto engine!");
712 /* construct registration vector */
715 if (r->plaintext_incremental)
716 vec_add1 (inc_tests, r);
718 vec_add1 (static_tests, r);
720 ad = vec_elt_at_index (cm->algs, r->alg);
722 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
724 vnet_crypto_op_id_t id = ad->op_by_type[i];
731 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
732 if (r->plaintext_incremental)
734 computed_data_total_incr_len += r->plaintext_incremental;
738 case VNET_CRYPTO_OP_TYPE_DECRYPT:
739 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
746 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
747 return clib_error_return (0,
748 "test case '%s' exceeds extra data!",
750 computed_data_total_len += ct->length;
756 else if (!r->plaintext_incremental)
758 computed_data_total_len += r->ciphertext.length;
762 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
763 if (r->plaintext_incremental)
765 computed_data_total_incr_len += r->plaintext_incremental;
766 computed_data_total_incr_len += r->tag.length;
771 computed_data_total_len += r->ciphertext.length;
772 computed_data_total_len += r->tag.length;
779 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
780 return clib_error_return (0,
781 "test case '%s' exceeds extra data!",
783 computed_data_total_len += ct->length;
793 case VNET_CRYPTO_OP_TYPE_HMAC:
794 if (r->plaintext_incremental)
796 computed_data_total_incr_len += r->digest.length;
798 generate_digest (vm, r, id);
802 computed_data_total_len += r->digest.length;
809 case VNET_CRYPTO_OP_TYPE_HASH:
810 computed_data_total_len += r->digest.length;
821 restore_engines (saved_engs);
823 err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
824 computed_data_total_len);
828 err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
829 computed_data_total_incr_len);
831 r = tm->test_registrations;
834 if (r->plaintext_incremental)
835 vec_free (r->digest.data);
840 vec_free (inc_tests);
841 vec_free (static_tests);
845 static clib_error_t *
846 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
848 vnet_crypto_main_t *cm = &crypto_main;
849 clib_error_t *err = 0;
850 u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
851 u32 *buffer_indices = 0;
852 vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
853 vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
854 vnet_crypto_key_index_t key_index = ~0;
856 int buffer_size = vlib_buffer_get_default_data_size (vm);
857 u64 seed = clib_cpu_time_now ();
858 u64 t0[5], t1[5], t2[5], n_bytes = 0;
861 if (tm->buffer_size > buffer_size)
862 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
864 rounds = tm->rounds ? tm->rounds : 100;
865 n_buffers = tm->n_buffers ? tm->n_buffers : 256;
866 buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
867 warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
869 if (buffer_size > vlib_buffer_get_default_data_size (vm))
870 return clib_error_return (0, "buffer size too big");
872 vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
873 vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
874 vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
876 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
877 if (n_alloc != n_buffers)
880 vlib_buffer_free (vm, buffer_indices, n_alloc);
881 err = clib_error_return (0, "buffer alloc failure");
885 vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
887 format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
888 rounds, warmup_rounds);
889 vlib_cli_output (vm, " cpu-freq %.2f GHz",
890 (f64) vm->clib_time.clocks_per_second * 1e-9);
892 vnet_crypto_op_type_t ot = 0;
894 for (i = 0; i < sizeof (key); i++)
897 key_index = vnet_crypto_key_add (vm, tm->alg, key,
898 test_crypto_get_key_sz (tm->alg));
900 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
902 vnet_crypto_op_id_t id = ad->op_by_type[i];
909 for (i = 0; i < n_buffers; i++)
911 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
917 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
918 case VNET_CRYPTO_OP_TYPE_DECRYPT:
919 vnet_crypto_op_init (op1,
920 ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
921 vnet_crypto_op_init (op2,
922 ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
923 op1->src = op2->src = op1->dst = op2->dst = b->data;
924 op1->key_index = op2->key_index = key_index;
925 op1->iv = op2->iv = b->data - 64;
926 n_bytes += op1->len = op2->len = buffer_size;
928 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
929 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
930 vnet_crypto_op_init (op1,
932 [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
933 vnet_crypto_op_init (op2,
935 [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
936 op1->src = op2->src = op1->dst = op2->dst = b->data;
937 op1->key_index = op2->key_index = key_index;
938 op1->tag = op2->tag = b->data - 32;
939 op1->iv = op2->iv = b->data - 64;
940 op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
941 op1->aad_len = op2->aad_len = 64;
942 op1->tag_len = op2->tag_len = 16;
943 n_bytes += op1->len = op2->len = buffer_size;
945 case VNET_CRYPTO_OP_TYPE_HMAC:
946 vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
948 op1->key_index = key_index;
950 op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
952 n_bytes += op1->len = buffer_size;
958 for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
959 *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
962 for (i = 0; i < 5; i++)
964 for (j = 0; j < warmup_rounds; j++)
966 vnet_crypto_process_ops (vm, ops1, n_buffers);
967 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
968 vnet_crypto_process_ops (vm, ops2, n_buffers);
971 t0[i] = clib_cpu_time_now ();
972 for (j = 0; j < rounds; j++)
973 vnet_crypto_process_ops (vm, ops1, n_buffers);
974 t1[i] = clib_cpu_time_now ();
976 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
978 for (j = 0; j < rounds; j++)
979 vnet_crypto_process_ops (vm, ops2, n_buffers);
980 t2[i] = clib_cpu_time_now ();
984 for (i = 0; i < 5; i++)
986 f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
987 f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
990 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
992 tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
993 gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
994 vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
995 "decrypt %.03f ticks/byte, %.02f Gbps",
996 i + 1, tpb1, gbps1, tpb2, gbps2);
1000 vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
1001 i + 1, tpb1, gbps1);
1007 vlib_buffer_free (vm, buffer_indices, n_alloc);
1009 if (key_index != ~0)
1010 vnet_crypto_key_del (vm, key_index);
1012 vec_free (buffer_indices);
1018 static clib_error_t *
1019 test_crypto_command_fn (vlib_main_t * vm,
1020 unformat_input_t * input, vlib_cli_command_t * cmd)
1022 crypto_test_main_t *tm = &crypto_test_main;
1023 unittest_crypto_test_registration_t *tr;
1026 tr = tm->test_registrations;
1027 memset (tm, 0, sizeof (crypto_test_main_t));
1028 tm->test_registrations = tr;
1031 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1033 if (unformat (input, "verbose"))
1035 else if (unformat (input, "detail"))
1038 if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1040 else if (unformat (input, "buffers %u", &tm->n_buffers))
1042 else if (unformat (input, "rounds %u", &tm->rounds))
1044 else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1046 else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1049 return clib_error_return (0, "unknown input '%U'",
1050 format_unformat_error, input);
1054 return test_crypto_perf (vm, tm);
1056 return test_crypto (vm, tm);
1059 VLIB_CLI_COMMAND (test_crypto_command, static) =
1061 .path = "test crypto",
1062 .short_help = "test crypto",
1063 .function = test_crypto_command_fn,
1066 static clib_error_t *
1067 crypto_test_init (vlib_main_t * vm)
1072 VLIB_INIT_FUNCTION (crypto_test_init);
1075 * fd.io coding-style-patch-verification: ON
1078 * eval: (c-set-style "gnu")