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 test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
36 vnet_crypto_main_t *cm = &crypto_main;
37 unittest_crypto_test_registration_t *r = tm->test_registrations;
38 unittest_crypto_test_registration_t **rv = 0;
39 vnet_crypto_alg_data_t *ad;
40 vnet_crypto_op_t *ops = 0, *op;
41 vnet_crypto_key_index_t *key_indices = 0;
42 u8 *computed_data = 0, *s = 0, *err = 0;
43 u32 computed_data_total_len = 0, n_ops = 0;
46 /* construct registration vector */
50 ad = vec_elt_at_index (cm->algs, r->alg);
52 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
54 vnet_crypto_op_id_t id = ad->op_by_type[i];
61 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
62 case VNET_CRYPTO_OP_TYPE_DECRYPT:
63 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
64 computed_data_total_len += r->ciphertext.length;
67 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
68 computed_data_total_len += r->ciphertext.length;
69 computed_data_total_len += r->tag.length;
72 case VNET_CRYPTO_OP_TYPE_HMAC:
73 computed_data_total_len += r->digest.length;
85 /* no tests registered */
89 vec_sort_with_function (rv, sort_registrations);
91 vec_validate_aligned (computed_data, computed_data_total_len - 1,
92 CLIB_CACHE_LINE_BYTES);
93 vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
94 computed_data_total_len = 0;
98 vec_foreach_index (i, rv)
102 ad = vec_elt_at_index (cm->algs, r->alg);
103 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
105 vnet_crypto_op_id_t id = ad->op_by_type[t];
110 vnet_crypto_op_init (op, id);
114 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
115 case VNET_CRYPTO_OP_TYPE_DECRYPT:
117 op->key_index = vnet_crypto_key_add (vm, r->alg,
120 vec_add1 (key_indices, op->key_index);
121 op->len = r->plaintext.length;
122 op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
123 r->plaintext.data : r->ciphertext.data;
124 op->dst = computed_data + computed_data_total_len;
125 computed_data_total_len += r->ciphertext.length;
127 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
128 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
130 op->key_index = vnet_crypto_key_add (vm, r->alg,
133 vec_add1 (key_indices, op->key_index);
134 op->aad = r->aad.data;
135 op->aad_len = r->aad.length;
136 op->len = r->plaintext.length;
137 op->dst = computed_data + computed_data_total_len;
138 computed_data_total_len += r->ciphertext.length;
139 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
141 op->src = r->plaintext.data;
142 op->tag = computed_data + computed_data_total_len;
143 computed_data_total_len += r->tag.length;
147 op->src = r->ciphertext.data;
148 op->tag = r->tag.data;
150 op->tag_len = r->tag.length;
152 case VNET_CRYPTO_OP_TYPE_HMAC:
153 op->key_index = vnet_crypto_key_add (vm, r->alg,
156 vec_add1 (key_indices, op->key_index);
157 op->src = r->plaintext.data;
158 op->len = r->plaintext.length;
159 op->digest_len = r->digest.length;
160 op->digest = computed_data + computed_data_total_len;
161 computed_data_total_len += r->digest.length;
173 vnet_crypto_process_ops (vm, ops, vec_len (ops));
176 vec_foreach (op, ops)
179 r = rv[op->user_data];
180 unittest_crypto_test_data_t *exp_pt = 0, *exp_ct = 0;
181 unittest_crypto_test_data_t *exp_digest = 0, *exp_tag = 0;
183 switch (vnet_crypto_get_op_type (op->op))
185 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
188 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
189 exp_ct = &r->ciphertext;
191 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
192 case VNET_CRYPTO_OP_TYPE_DECRYPT:
193 exp_pt = &r->plaintext;
195 case VNET_CRYPTO_OP_TYPE_HMAC:
196 exp_digest = &r->digest;
202 vec_reset_length (err);
204 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
205 err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
206 format_vnet_crypto_op_status, op->status);
208 if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
209 err = format (err, "%sciphertext mismatch",
210 vec_len (err) ? ", " : "");
212 if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
213 err = format (err, "%splaintext mismatch", vec_len (err) ? ", " : "");
215 if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
216 err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
219 memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
220 err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
222 vec_reset_length (s);
223 s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op);
228 vlib_cli_output (vm, "%-60v%s%v", s, vec_len (err) ? "FAIL: " : "OK",
232 if (tm->verbose == 2)
236 vlib_cli_output (vm, "Expected ciphertext:\n%U"
237 "\nCalculated ciphertext:\n%U",
238 format_hexdump, exp_ct->data, exp_ct->length,
239 format_hexdump, op->dst, exp_ct->length);
241 vlib_cli_output (vm, "Expected plaintext:\n%U"
242 "\nCalculated plaintext:\n%U",
243 format_hexdump, exp_pt->data, exp_pt->length,
244 format_hexdump, op->dst, exp_pt->length);
245 if (r->tag.length && fail)
246 vlib_cli_output (vm, "Expected tag:\n%U"
247 "\nCalculated tag:\n%U",
248 format_hexdump, r->tag.data, r->tag.length,
249 format_hexdump, op->tag, op->tag_len);
250 if (exp_digest && fail)
251 vlib_cli_output (vm, "Expected digest:\n%U"
252 "\nCalculated Digest:\n%U",
253 format_hexdump, exp_digest->data,
254 exp_digest->length, format_hexdump, op->digest,
259 vec_foreach_index (i, key_indices)
260 vnet_crypto_key_del (vm, key_indices[i]);
263 vec_free (computed_data);
271 static clib_error_t *
272 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
274 vnet_crypto_main_t *cm = &crypto_main;
275 clib_error_t *err = 0;
276 u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
277 u32 *buffer_indices = 0;
278 vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
279 vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
280 vnet_crypto_key_index_t key_index = ~0;
282 int buffer_size = vlib_buffer_get_default_data_size (vm);
283 u64 seed = clib_cpu_time_now ();
284 u64 t0[5], t1[5], t2[5], n_bytes = 0;
287 if (tm->buffer_size > buffer_size)
288 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
290 rounds = tm->rounds ? tm->rounds : 100;
291 n_buffers = tm->n_buffers ? tm->n_buffers : 256;
292 buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
293 warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
295 if (buffer_size > vlib_buffer_get_default_data_size (vm))
296 return clib_error_return (0, "buffer size too big");
298 vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
299 vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
300 vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
302 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
303 if (n_alloc != n_buffers)
306 vlib_buffer_free (vm, buffer_indices, n_alloc);
307 err = clib_error_return (0, "buffer alloc failure");
311 vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
313 format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
314 rounds, warmup_rounds);
315 vlib_cli_output (vm, " cpu-freq %.2f GHz",
316 (f64) vm->clib_time.clocks_per_second * 1e-9);
318 vnet_crypto_op_type_t ot = 0;
320 for (i = 0; i < sizeof (key); i++)
323 key_index = vnet_crypto_key_add (vm, tm->alg, key, sizeof (key));
325 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
327 vnet_crypto_op_id_t id = ad->op_by_type[i];
334 for (i = 0; i < n_buffers; i++)
336 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
342 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
343 case VNET_CRYPTO_OP_TYPE_DECRYPT:
344 vnet_crypto_op_init (op1,
345 ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
346 vnet_crypto_op_init (op2,
347 ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
348 op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
349 op1->src = op2->src = op1->dst = op2->dst = b->data;
350 op1->key_index = op2->key_index = key_index;
351 op1->iv = op2->iv = b->data - 64;
352 n_bytes += op1->len = op2->len = buffer_size;
354 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
355 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
356 vnet_crypto_op_init (op1,
358 [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
359 vnet_crypto_op_init (op2,
361 [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
362 op1->src = op2->src = op1->dst = op2->dst = b->data;
363 op1->key_index = op2->key_index = key_index;
364 op1->tag = op2->tag = b->data - 32;
365 op1->iv = op2->iv = b->data - 64;
366 op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
367 op1->aad_len = op2->aad_len = 64;
368 op1->tag_len = op2->tag_len = 16;
369 n_bytes += op1->len = op2->len = buffer_size;
371 case VNET_CRYPTO_OP_TYPE_HMAC:
372 vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
374 op1->key_index = key_index;
376 op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
378 n_bytes += op1->len = buffer_size;
384 for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
385 *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
388 for (i = 0; i < 5; i++)
390 for (j = 0; j < warmup_rounds; j++)
392 vnet_crypto_process_ops (vm, ops1, n_buffers);
393 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
394 vnet_crypto_process_ops (vm, ops2, n_buffers);
397 t0[i] = clib_cpu_time_now ();
398 for (j = 0; j < rounds; j++)
399 vnet_crypto_process_ops (vm, ops1, n_buffers);
400 t1[i] = clib_cpu_time_now ();
402 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
404 for (j = 0; j < rounds; j++)
405 vnet_crypto_process_ops (vm, ops2, n_buffers);
406 t2[i] = clib_cpu_time_now ();
410 for (i = 0; i < 5; i++)
412 f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
413 f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
416 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
418 tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
419 gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
420 vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
421 "decrypt %.03f ticks/byte, %.02f Gbps",
422 i + 1, tpb1, gbps1, tpb2, gbps2);
426 vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
433 vlib_buffer_free (vm, buffer_indices, n_alloc);
436 vnet_crypto_key_del (vm, key_index);
438 vec_free (buffer_indices);
444 static clib_error_t *
445 test_crypto_command_fn (vlib_main_t * vm,
446 unformat_input_t * input, vlib_cli_command_t * cmd)
448 crypto_test_main_t *tm = &crypto_test_main;
449 unittest_crypto_test_registration_t *tr;
452 tr = tm->test_registrations;
453 memset (tm, 0, sizeof (crypto_test_main_t));
454 tm->test_registrations = tr;
457 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
459 if (unformat (input, "verbose"))
461 else if (unformat (input, "detail"))
464 if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
466 else if (unformat (input, "buffers %u", &tm->n_buffers))
468 else if (unformat (input, "rounds %u", &tm->rounds))
470 else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
472 else if (unformat (input, "buffer-size %u", &tm->buffer_size))
475 return clib_error_return (0, "unknown input '%U'",
476 format_unformat_error, input);
480 return test_crypto_perf (vm, tm);
482 return test_crypto (vm, tm);
486 VLIB_CLI_COMMAND (test_crypto_command, static) =
488 .path = "test crypto",
489 .short_help = "test crypto",
490 .function = test_crypto_command_fn,
494 static clib_error_t *
495 crypto_test_init (vlib_main_t * vm)
500 VLIB_INIT_FUNCTION (crypto_test_init);
503 * fd.io coding-style-patch-verification: ON
506 * eval: (c-set-style "gnu")