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)
210 vnet_crypto_main_t *cm = &crypto_main;
212 vnet_crypto_engine_t *ce;
214 for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
216 vnet_crypto_op_data_t *od = &cm->opt_data[i];
220 ce = vec_elt_at_index (cm->engines, engs[i]);
221 od->active_engine_index_simple = engs[i];
222 cm->ops_handlers[i] = ce->ops_handlers[i];
230 save_current_engines (u32 * engs)
232 vnet_crypto_main_t *cm = &crypto_main;
235 vnet_crypto_engine_t *ce;
237 p = hash_get_mem (cm->engine_index_by_name, "openssl");
241 ce = vec_elt_at_index (cm->engines, p[0]);
243 /* set openssl for all crypto algs to generate expected data */
244 for (i = 1; i < VNET_CRYPTO_N_OP_IDS; i++)
246 vnet_crypto_op_data_t *od = &cm->opt_data[i];
247 if (od->active_engine_index_simple != ~0)
249 /* save engine index */
250 engs[i] = od->active_engine_index_simple;
251 od->active_engine_index_simple = ce - cm->engines;
252 cm->ops_handlers[i] = ce->ops_handlers[i];
259 static clib_error_t *
260 test_crypto_incremental (vlib_main_t * vm, crypto_test_main_t * tm,
261 unittest_crypto_test_registration_t ** rv, u32 n_ops,
262 u32 computed_data_total_len)
264 vnet_crypto_main_t *cm = &crypto_main;
265 vnet_crypto_alg_data_t *ad;
266 vnet_crypto_key_index_t *key_indices = 0;
268 unittest_crypto_test_registration_t *r;
269 vnet_crypto_op_t *ops = 0, *op;
270 u8 *encrypted_data = 0, *decrypted_data = 0, *s = 0, *err = 0;
275 vec_validate_aligned (encrypted_data, computed_data_total_len - 1,
276 CLIB_CACHE_LINE_BYTES);
277 vec_validate_aligned (decrypted_data, computed_data_total_len - 1,
278 CLIB_CACHE_LINE_BYTES);
279 vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
280 computed_data_total_len = 0;
283 /* first stage: encrypt only */
285 vec_foreach_index (i, rv)
289 ad = vec_elt_at_index (cm->algs, r->alg);
290 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
292 vnet_crypto_op_id_t id = ad->op_by_type[t];
299 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
300 vnet_crypto_op_init (op, id);
301 op->iv = tm->inc_data;
302 op->key_index = vnet_crypto_key_add (vm, r->alg,
303 tm->inc_data, r->key.length);
304 vec_add1 (key_indices, op->key_index);
305 op->len = r->plaintext_incremental;
306 op->src = tm->inc_data;
307 op->dst = encrypted_data + computed_data_total_len;
308 computed_data_total_len += r->plaintext_incremental;
312 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
313 vnet_crypto_op_init (op, id);
314 op->iv = tm->inc_data;
315 op->key_index = vnet_crypto_key_add (vm, r->alg,
316 tm->inc_data, r->key.length);
317 vec_add1 (key_indices, op->key_index);
318 op->aad = tm->inc_data;
319 op->aad_len = r->aad.length;
320 op->len = r->plaintext_incremental;
321 op->dst = encrypted_data + computed_data_total_len;
322 computed_data_total_len += r->plaintext_incremental;
323 op->src = tm->inc_data;
324 op->tag = encrypted_data + computed_data_total_len;
325 computed_data_total_len += r->tag.length;
326 op->tag_len = r->tag.length;
330 case VNET_CRYPTO_OP_TYPE_HMAC:
331 /* compute hmac in the next stage */
332 op->op = VNET_CRYPTO_OP_NONE;
333 computed_data_total_len += r->digest.length;
343 vnet_crypto_process_ops (vm, ops, n_ops);
344 computed_data_total_len = 0;
346 /* second stage: hash/decrypt previously encrypted data */
349 vec_foreach_index (i, rv)
353 ad = vec_elt_at_index (cm->algs, r->alg);
354 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
356 vnet_crypto_op_id_t id = ad->op_by_type[t];
363 case VNET_CRYPTO_OP_TYPE_DECRYPT:
364 vnet_crypto_op_init (op, id);
365 op->iv = tm->inc_data;
366 op->key_index = vnet_crypto_key_add (vm, r->alg,
367 tm->inc_data, r->key.length);
368 vec_add1 (key_indices, op->key_index);
369 op->len = r->plaintext_incremental;
370 op->src = encrypted_data + computed_data_total_len;
371 op->dst = decrypted_data + computed_data_total_len;
372 computed_data_total_len += r->plaintext_incremental;
376 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
377 vnet_crypto_op_init (op, id);
378 op->iv = tm->inc_data;
379 op->key_index = vnet_crypto_key_add (vm, r->alg,
380 tm->inc_data, r->key.length);
381 vec_add1 (key_indices, op->key_index);
382 op->aad = tm->inc_data;
383 op->aad_len = r->aad.length;
384 op->len = r->plaintext_incremental;
385 op->dst = decrypted_data + computed_data_total_len;
386 op->src = encrypted_data + computed_data_total_len;
387 computed_data_total_len += r->plaintext_incremental;
389 op->tag = encrypted_data + computed_data_total_len;
390 computed_data_total_len += r->tag.length;
391 op->tag_len = r->tag.length;
395 case VNET_CRYPTO_OP_TYPE_HMAC:
396 vnet_crypto_op_init (op, id);
397 op->key_index = vnet_crypto_key_add (vm, r->alg,
398 tm->inc_data, r->key.length);
399 vec_add1 (key_indices, op->key_index);
400 op->src = tm->inc_data;
401 op->len = r->plaintext_incremental;
402 op->digest_len = r->digest.length;
403 op->digest = encrypted_data + computed_data_total_len;
404 computed_data_total_len += r->digest.length;
415 vnet_crypto_process_ops (vm, ops, n_ops);
416 print_results (vm, rv, ops, 0, n_ops, tm);
418 vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
419 vec_free (tm->inc_data);
421 vec_free (encrypted_data);
422 vec_free (decrypted_data);
428 static clib_error_t *
429 test_crypto_static (vlib_main_t * vm, crypto_test_main_t * tm,
430 unittest_crypto_test_registration_t ** rv, u32 n_ops,
431 u32 n_chained_ops, u32 computed_data_total_len)
433 unittest_crypto_test_data_t *pt, *ct;
434 vnet_crypto_op_chunk_t *chunks = 0, ch;
435 unittest_crypto_test_registration_t *r;
436 vnet_crypto_op_t *ops = 0, *op, *chained_ops = 0;
437 vnet_crypto_op_t *current_chained_op = 0, *current_op = 0;
438 vnet_crypto_main_t *cm = &crypto_main;
439 vnet_crypto_alg_data_t *ad;
440 vnet_crypto_key_index_t *key_indices = 0;
441 u8 *computed_data = 0;
444 vec_sort_with_function (rv, sort_registrations);
446 vec_validate_aligned (computed_data, computed_data_total_len - 1,
447 CLIB_CACHE_LINE_BYTES);
448 vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
449 vec_validate_aligned (chained_ops, n_chained_ops - 1,
450 CLIB_CACHE_LINE_BYTES);
451 computed_data_total_len = 0;
454 current_chained_op = chained_ops;
456 vec_foreach_index (i, rv)
460 ad = vec_elt_at_index (cm->algs, r->alg);
461 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
463 vnet_crypto_op_id_t id = ad->op_by_type[t];
470 op = current_chained_op;
471 current_chained_op += 1;
479 vnet_crypto_op_init (op, id);
483 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
484 case VNET_CRYPTO_OP_TYPE_DECRYPT:
486 op->key_index = vnet_crypto_key_add (vm, r->alg,
489 vec_add1 (key_indices, op->key_index);
495 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
496 op->chunk_index = vec_len (chunks);
499 ch.src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
502 ch.dst = computed_data + computed_data_total_len;
503 computed_data_total_len += pt->length;
504 vec_add1 (chunks, ch);
512 op->len = r->plaintext.length;
513 op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
514 r->plaintext.data : r->ciphertext.data;
515 op->dst = computed_data + computed_data_total_len;
516 computed_data_total_len += r->ciphertext.length;
519 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
520 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
524 op->key_index = vnet_crypto_key_add (vm, r->alg,
527 vec_add1 (key_indices, op->key_index);
528 op->aad = r->aad.data;
529 op->aad_len = r->aad.length;
530 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
533 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
534 op->chunk_index = vec_len (chunks);
537 clib_memset (&ch, 0, sizeof (ch));
540 ch.dst = computed_data + computed_data_total_len;
541 computed_data_total_len += pt->length;
542 vec_add1 (chunks, ch);
546 op->tag = computed_data + computed_data_total_len;
547 computed_data_total_len += r->tag.length;
552 op->flags |= VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS;
553 op->chunk_index = vec_len (chunks);
556 clib_memset (&ch, 0, sizeof (ch));
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);
611 clib_memset (&ch, 0, sizeof (ch));
614 vec_add1 (chunks, ch);
621 op->key_index = vnet_crypto_key_add (vm, r->alg,
624 vec_add1 (key_indices, op->key_index);
625 op->digest_len = r->digest.length;
626 op->digest = computed_data + computed_data_total_len;
627 computed_data_total_len += r->digest.length;
628 op->src = r->plaintext.data;
629 op->len = r->plaintext.length;
641 vnet_crypto_process_ops (vm, ops, vec_len (ops));
642 vnet_crypto_process_chained_ops (vm, chained_ops, chunks,
643 vec_len (chained_ops));
645 print_results (vm, rv, ops, chunks, vec_len (ops), tm);
646 print_results (vm, rv, chained_ops, chunks, vec_len (chained_ops), tm);
648 vec_foreach_index (i, key_indices) vnet_crypto_key_del (vm, key_indices[i]);
650 vec_free (computed_data);
652 vec_free (chained_ops);
658 test_crypto_get_key_sz (vnet_crypto_alg_t alg)
663 case VNET_CRYPTO_ALG_##n: \
666 foreach_crypto_cipher_alg
667 foreach_crypto_aead_alg
670 case VNET_CRYPTO_ALG_HMAC_MD5:
671 case VNET_CRYPTO_ALG_HMAC_SHA1:
673 case VNET_CRYPTO_ALG_HMAC_SHA224:
675 case VNET_CRYPTO_ALG_HMAC_SHA256:
677 case VNET_CRYPTO_ALG_HMAC_SHA384:
679 case VNET_CRYPTO_ALG_HMAC_SHA512:
687 static clib_error_t *
688 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
690 clib_error_t *err = 0;
691 vnet_crypto_main_t *cm = &crypto_main;
692 unittest_crypto_test_registration_t *r = tm->test_registrations;
693 unittest_crypto_test_registration_t **static_tests = 0, **inc_tests = 0;
694 u32 i, j, n_ops_static = 0, n_ops_incr = 0, n_chained_ops = 0;
695 vnet_crypto_alg_data_t *ad;
696 u32 computed_data_total_len = 0;
697 u32 computed_data_total_incr_len = 0;
698 u32 saved_engs[VNET_CRYPTO_N_OP_IDS] = { ~0, };
699 unittest_crypto_test_data_t *ct;
701 /* pre-allocate plaintext data with reasonable length */
702 validate_data (&tm->inc_data, 2048);
704 int rc = save_current_engines (saved_engs);
706 return clib_error_return (0, "failed to set default crypto engine!");
708 /* construct registration vector */
711 if (r->plaintext_incremental)
712 vec_add1 (inc_tests, r);
714 vec_add1 (static_tests, r);
716 ad = vec_elt_at_index (cm->algs, r->alg);
718 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
720 vnet_crypto_op_id_t id = ad->op_by_type[i];
727 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
728 if (r->plaintext_incremental)
730 computed_data_total_incr_len += r->plaintext_incremental;
734 case VNET_CRYPTO_OP_TYPE_DECRYPT:
735 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
742 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
743 return clib_error_return (0,
744 "test case '%s' exceeds extra data!",
746 computed_data_total_len += ct->length;
752 else if (!r->plaintext_incremental)
754 computed_data_total_len += r->ciphertext.length;
758 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
759 if (r->plaintext_incremental)
761 computed_data_total_incr_len += r->plaintext_incremental;
762 computed_data_total_incr_len += r->tag.length;
767 computed_data_total_len += r->ciphertext.length;
768 computed_data_total_len += r->tag.length;
775 if (j > CRYPTO_TEST_MAX_OP_CHUNKS)
776 return clib_error_return (0,
777 "test case '%s' exceeds extra data!",
779 computed_data_total_len += ct->length;
789 case VNET_CRYPTO_OP_TYPE_HMAC:
790 if (r->plaintext_incremental)
792 computed_data_total_incr_len += r->digest.length;
794 generate_digest (vm, r, id);
798 computed_data_total_len += r->digest.length;
813 restore_engines (saved_engs);
815 err = test_crypto_static (vm, tm, static_tests, n_ops_static, n_chained_ops,
816 computed_data_total_len);
820 err = test_crypto_incremental (vm, tm, inc_tests, n_ops_incr,
821 computed_data_total_incr_len);
823 r = tm->test_registrations;
826 if (r->plaintext_incremental)
827 vec_free (r->digest.data);
832 vec_free (inc_tests);
833 vec_free (static_tests);
837 static clib_error_t *
838 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
840 vnet_crypto_main_t *cm = &crypto_main;
841 clib_error_t *err = 0;
842 u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
843 u32 *buffer_indices = 0;
844 vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
845 vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
846 vnet_crypto_key_index_t key_index = ~0;
848 int buffer_size = vlib_buffer_get_default_data_size (vm);
849 u64 seed = clib_cpu_time_now ();
850 u64 t0[5], t1[5], t2[5], n_bytes = 0;
853 if (tm->buffer_size > buffer_size)
854 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
856 rounds = tm->rounds ? tm->rounds : 100;
857 n_buffers = tm->n_buffers ? tm->n_buffers : 256;
858 buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
859 warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
861 if (buffer_size > vlib_buffer_get_default_data_size (vm))
862 return clib_error_return (0, "buffer size too big");
864 vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
865 vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
866 vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
868 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
869 if (n_alloc != n_buffers)
872 vlib_buffer_free (vm, buffer_indices, n_alloc);
873 err = clib_error_return (0, "buffer alloc failure");
877 vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
879 format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
880 rounds, warmup_rounds);
881 vlib_cli_output (vm, " cpu-freq %.2f GHz",
882 (f64) vm->clib_time.clocks_per_second * 1e-9);
884 vnet_crypto_op_type_t ot = 0;
886 for (i = 0; i < sizeof (key); i++)
889 key_index = vnet_crypto_key_add (vm, tm->alg, key,
890 test_crypto_get_key_sz (tm->alg));
892 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
894 vnet_crypto_op_id_t id = ad->op_by_type[i];
901 for (i = 0; i < n_buffers; i++)
903 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
909 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
910 case VNET_CRYPTO_OP_TYPE_DECRYPT:
911 vnet_crypto_op_init (op1,
912 ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
913 vnet_crypto_op_init (op2,
914 ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
915 op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
916 op1->src = op2->src = op1->dst = op2->dst = b->data;
917 op1->key_index = op2->key_index = key_index;
918 op1->iv = op2->iv = b->data - 64;
919 n_bytes += op1->len = op2->len = buffer_size;
921 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
922 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
923 vnet_crypto_op_init (op1,
925 [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
926 vnet_crypto_op_init (op2,
928 [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
929 op1->src = op2->src = op1->dst = op2->dst = b->data;
930 op1->key_index = op2->key_index = key_index;
931 op1->tag = op2->tag = b->data - 32;
932 op1->iv = op2->iv = b->data - 64;
933 op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
934 op1->aad_len = op2->aad_len = 64;
935 op1->tag_len = op2->tag_len = 16;
936 n_bytes += op1->len = op2->len = buffer_size;
938 case VNET_CRYPTO_OP_TYPE_HMAC:
939 vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
941 op1->key_index = key_index;
943 op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
945 n_bytes += op1->len = buffer_size;
951 for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
952 *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
955 for (i = 0; i < 5; i++)
957 for (j = 0; j < warmup_rounds; j++)
959 vnet_crypto_process_ops (vm, ops1, n_buffers);
960 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
961 vnet_crypto_process_ops (vm, ops2, n_buffers);
964 t0[i] = clib_cpu_time_now ();
965 for (j = 0; j < rounds; j++)
966 vnet_crypto_process_ops (vm, ops1, n_buffers);
967 t1[i] = clib_cpu_time_now ();
969 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
971 for (j = 0; j < rounds; j++)
972 vnet_crypto_process_ops (vm, ops2, n_buffers);
973 t2[i] = clib_cpu_time_now ();
977 for (i = 0; i < 5; i++)
979 f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
980 f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
983 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
985 tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
986 gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
987 vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
988 "decrypt %.03f ticks/byte, %.02f Gbps",
989 i + 1, tpb1, gbps1, tpb2, gbps2);
993 vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
1000 vlib_buffer_free (vm, buffer_indices, n_alloc);
1002 if (key_index != ~0)
1003 vnet_crypto_key_del (vm, key_index);
1005 vec_free (buffer_indices);
1011 static clib_error_t *
1012 test_crypto_command_fn (vlib_main_t * vm,
1013 unformat_input_t * input, vlib_cli_command_t * cmd)
1015 crypto_test_main_t *tm = &crypto_test_main;
1016 unittest_crypto_test_registration_t *tr;
1019 tr = tm->test_registrations;
1020 memset (tm, 0, sizeof (crypto_test_main_t));
1021 tm->test_registrations = tr;
1024 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1026 if (unformat (input, "verbose"))
1028 else if (unformat (input, "detail"))
1031 if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
1033 else if (unformat (input, "buffers %u", &tm->n_buffers))
1035 else if (unformat (input, "rounds %u", &tm->rounds))
1037 else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
1039 else if (unformat (input, "buffer-size %u", &tm->buffer_size))
1042 return clib_error_return (0, "unknown input '%U'",
1043 format_unformat_error, input);
1047 return test_crypto_perf (vm, tm);
1049 return test_crypto (vm, tm);
1053 VLIB_CLI_COMMAND (test_crypto_command, static) =
1055 .path = "test crypto",
1056 .short_help = "test crypto",
1057 .function = test_crypto_command_fn,
1061 static clib_error_t *
1062 crypto_test_init (vlib_main_t * vm)
1067 VLIB_INIT_FUNCTION (crypto_test_init);
1070 * fd.io coding-style-patch-verification: ON
1073 * eval: (c-set-style "gnu")