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 u8 *computed_data = 0, *s = 0, *err = 0;
42 u32 computed_data_total_len = 0, n_ops = 0;
45 /* construct registration vector */
49 ad = vec_elt_at_index (cm->algs, r->alg);
51 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
53 vnet_crypto_op_id_t id = ad->op_by_type[i];
60 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
61 case VNET_CRYPTO_OP_TYPE_DECRYPT:
62 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
63 computed_data_total_len += r->ciphertext.length;
66 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
67 computed_data_total_len += r->ciphertext.length;
68 computed_data_total_len += r->tag.length;
71 case VNET_CRYPTO_OP_TYPE_HMAC:
72 computed_data_total_len += r->digest.length;
84 vec_sort_with_function (rv, sort_registrations);
86 vec_validate_aligned (computed_data, computed_data_total_len - 1,
87 CLIB_CACHE_LINE_BYTES);
88 vec_validate_aligned (ops, n_ops - 1, CLIB_CACHE_LINE_BYTES);
89 computed_data_total_len = 0;
93 vec_foreach_index (i, rv)
97 ad = vec_elt_at_index (cm->algs, r->alg);
98 for (t = 0; t < VNET_CRYPTO_OP_N_TYPES; t++)
100 vnet_crypto_op_id_t id = ad->op_by_type[t];
105 vnet_crypto_op_init (op, id);
109 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
110 case VNET_CRYPTO_OP_TYPE_DECRYPT:
112 op->iv_len = r->iv.length;
113 op->key = r->key.data;
114 op->key_len = r->key.length;
115 op->len = r->plaintext.length;
116 op->src = t == VNET_CRYPTO_OP_TYPE_ENCRYPT ?
117 r->plaintext.data : r->ciphertext.data;
118 op->dst = computed_data + computed_data_total_len;
119 computed_data_total_len += r->ciphertext.length;
121 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
122 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
124 op->iv_len = r->iv.length;
125 op->key = r->key.data;
126 op->key_len = r->key.length;
127 op->aad = r->aad.data;
128 op->aad_len = r->aad.length;
129 op->len = r->plaintext.length;
130 op->dst = computed_data + computed_data_total_len;
131 computed_data_total_len += r->ciphertext.length;
132 if (t == VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT)
134 op->src = r->plaintext.data;
135 op->tag = computed_data + computed_data_total_len;
136 computed_data_total_len += r->tag.length;
140 op->src = r->ciphertext.data;
141 op->tag = r->tag.data;
143 op->tag_len = r->tag.length;
145 case VNET_CRYPTO_OP_TYPE_HMAC:
146 op->key = r->key.data;
147 op->key_len = r->key.length;
148 op->src = r->plaintext.data;
149 op->len = r->plaintext.length;
150 op->digest_len = r->digest.length;
151 op->digest = computed_data + computed_data_total_len;
152 computed_data_total_len += r->digest.length;
166 vnet_crypto_process_ops (vm, ops, vec_len (ops));
169 vec_foreach (op, ops)
172 r = rv[op->user_data];
173 unittest_crypto_test_data_t *exp_pt = 0, *exp_ct = 0;
174 unittest_crypto_test_data_t *exp_digest = 0, *exp_tag = 0;
176 switch (vnet_crypto_get_op_type (op->op))
178 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
180 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
181 exp_ct = &r->ciphertext;
183 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
184 case VNET_CRYPTO_OP_TYPE_DECRYPT:
185 exp_pt = &r->plaintext;
187 case VNET_CRYPTO_OP_TYPE_HMAC:
188 exp_digest = &r->digest;
194 vec_reset_length (err);
196 if (op->status != VNET_CRYPTO_OP_STATUS_COMPLETED)
197 err = format (err, "%sengine error: %U", vec_len (err) ? ", " : "",
198 format_vnet_crypto_op_status, op->status);
200 if (exp_ct && memcmp (op->dst, exp_ct->data, exp_ct->length) != 0)
201 err = format (err, "%sciphertext mismatch",
202 vec_len (err) ? ", " : "");
204 if (exp_pt && memcmp (op->dst, exp_pt->data, exp_pt->length) != 0)
205 err = format (err, "%splaintext mismatch", vec_len (err) ? ", " : "");
207 if (exp_tag && memcmp (op->tag, exp_tag->data, exp_tag->length) != 0)
208 err = format (err, "%stag mismatch", vec_len (err) ? ", " : "");
211 memcmp (op->digest, exp_digest->data, exp_digest->length) != 0)
212 err = format (err, "%sdigest mismatch", vec_len (err) ? ", " : "");
214 vec_reset_length (s);
215 s = format (s, "%s (%U)", r->name, format_vnet_crypto_op, op->op);
220 vlib_cli_output (vm, "%-60v%s%v", s, vec_len (err) ? "FAIL: " : "OK",
224 if (tm->verbose == 2)
228 vlib_cli_output (vm, "Expected ciphertext:\n%U"
229 "\nCalculated ciphertext:\n%U",
230 format_hexdump, exp_ct->data, exp_ct->length,
231 format_hexdump, op->dst, exp_ct->length);
233 vlib_cli_output (vm, "Expected plaintext:\n%U"
234 "\nCalculated plaintext:\n%U",
235 format_hexdump, exp_pt->data, exp_pt->length,
236 format_hexdump, op->dst, exp_pt->length);
237 if (r->tag.length && fail)
238 vlib_cli_output (vm, "Expected tag:\n%U"
239 "\nCalculated tag:\n%U",
240 format_hexdump, r->tag.data, r->tag.length,
241 format_hexdump, op->tag, op->tag_len);
242 if (exp_digest && fail)
243 vlib_cli_output (vm, "Expected digest:\n%U"
244 "\nCalculated Digest:\n%U",
245 format_hexdump, exp_digest->data,
246 exp_digest->length, format_hexdump, op->digest,
252 vec_free (computed_data);
260 static clib_error_t *
261 test_crypto_perf (vlib_main_t * vm, crypto_test_main_t * tm)
263 vnet_crypto_main_t *cm = &crypto_main;
264 clib_error_t *err = 0;
265 u32 n_buffers, n_alloc = 0, warmup_rounds, rounds;
266 u32 *buffer_indices = 0;
267 vnet_crypto_op_t *ops1 = 0, *ops2 = 0, *op1, *op2;
268 vnet_crypto_alg_data_t *ad = vec_elt_at_index (cm->algs, tm->alg);
269 int buffer_size = vlib_buffer_get_default_data_size (vm);
270 u64 seed = clib_cpu_time_now ();
271 u64 t0[5], t1[5], t2[5], n_bytes = 0;
275 if (tm->buffer_size > buffer_size)
276 return clib_error_return (0, "buffer size must be <= %u", buffer_size);
278 rounds = tm->rounds ? tm->rounds : 100;
279 n_buffers = tm->n_buffers ? tm->n_buffers : 256;
280 buffer_size = tm->buffer_size ? tm->buffer_size : 2048;
281 warmup_rounds = tm->warmup_rounds ? tm->warmup_rounds : 100;
283 if (buffer_size > vlib_buffer_get_default_data_size (vm))
284 return clib_error_return (0, "buffer size too big");
286 vec_validate_aligned (buffer_indices, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
287 vec_validate_aligned (ops1, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
288 vec_validate_aligned (ops2, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
290 n_alloc = vlib_buffer_alloc (vm, buffer_indices, n_buffers);
291 if (n_alloc != n_buffers)
294 vlib_buffer_free (vm, buffer_indices, n_alloc);
295 err = clib_error_return (0, "buffer alloc failure");
299 vlib_cli_output (vm, "%U: n_buffers %u buffer-size %u rounds %u "
300 "warmup-rounds %u one-key %s",
301 format_vnet_crypto_alg, tm->alg, n_buffers, buffer_size,
302 rounds, warmup_rounds, tm->one_key ? "yes" : "no");
303 vlib_cli_output (vm, " cpu-freq %.2f GHz",
304 (f64) vm->clib_time.clocks_per_second * 1e-9);
306 vnet_crypto_op_type_t ot = 0;
308 for (i = 0; i < VNET_CRYPTO_OP_N_TYPES; i++)
310 vnet_crypto_op_id_t id = ad->op_by_type[i];
317 for (i = 0; i < n_buffers; i++)
319 vlib_buffer_t *b = vlib_get_buffer (vm, buffer_indices[i]);
327 case VNET_CRYPTO_OP_TYPE_ENCRYPT:
328 case VNET_CRYPTO_OP_TYPE_DECRYPT:
329 vnet_crypto_op_init (op1,
330 ad->op_by_type[VNET_CRYPTO_OP_TYPE_ENCRYPT]);
331 vnet_crypto_op_init (op2,
332 ad->op_by_type[VNET_CRYPTO_OP_TYPE_DECRYPT]);
333 op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
334 op1->src = op2->src = op1->dst = op2->dst = b->data;
335 op1->key = op2->key = tm->one_key ? key : b->data - 32;
336 op1->iv = op2->iv = b->data - 64;
337 n_bytes += op1->len = op2->len = buffer_size;
339 case VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT:
340 case VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT:
341 vnet_crypto_op_init (op1,
343 [VNET_CRYPTO_OP_TYPE_AEAD_ENCRYPT]);
344 vnet_crypto_op_init (op2,
346 [VNET_CRYPTO_OP_TYPE_AEAD_DECRYPT]);
347 op1->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
348 op1->src = op2->src = op1->dst = op2->dst = b->data;
349 op1->key = op2->key = tm->one_key ? key : b->data - 32;
350 op1->iv = op2->iv = b->data - 64;
351 op1->aad = op2->aad = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
352 op1->aad_len = op2->aad_len = 0;
353 n_bytes += op1->len = op2->len = buffer_size;
355 case VNET_CRYPTO_OP_TYPE_HMAC:
356 vnet_crypto_op_init (op1, ad->op_by_type[VNET_CRYPTO_OP_TYPE_HMAC]);
358 op1->key = tm->one_key ? key : b->data - 32;
360 op1->digest = b->data - VLIB_BUFFER_PRE_DATA_SIZE;
362 n_bytes += op1->len = buffer_size;
368 for (j = -VLIB_BUFFER_PRE_DATA_SIZE; j < buffer_size; j += 8)
369 *(u64 *) (b->data + j) = 1 + random_u64 (&seed);
372 for (i = 0; i < 5; i++)
374 for (j = 0; j < warmup_rounds; j++)
376 vnet_crypto_process_ops (vm, ops1, n_buffers);
377 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
378 vnet_crypto_process_ops (vm, ops2, n_buffers);
381 t0[i] = clib_cpu_time_now ();
382 for (j = 0; j < rounds; j++)
383 vnet_crypto_process_ops (vm, ops1, n_buffers);
384 t1[i] = clib_cpu_time_now ();
386 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
388 for (j = 0; j < rounds; j++)
389 vnet_crypto_process_ops (vm, ops2, n_buffers);
390 t2[i] = clib_cpu_time_now ();
394 for (i = 0; i < 5; i++)
396 f64 tpb1 = (f64) (t1[i] - t0[i]) / (n_bytes * rounds);
397 f64 gbps1 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb1;
400 if (ot != VNET_CRYPTO_OP_TYPE_HMAC)
402 tpb2 = (f64) (t2[i] - t1[i]) / (n_bytes * rounds);
403 gbps2 = vm->clib_time.clocks_per_second * 1e-9 * 8 / tpb2;
404 vlib_cli_output (vm, "%-2u: encrypt %.03f ticks/byte, %.02f Gbps; "
405 "decrypt %.03f ticks/byte, %.02f Gbps",
406 i + 1, tpb1, gbps1, tpb2, gbps2);
410 vlib_cli_output (vm, "%-2u: hash %.03f ticks/byte, %.02f Gbps\n",
417 vlib_buffer_free (vm, buffer_indices, n_alloc);
418 vec_free (buffer_indices);
424 static clib_error_t *
425 test_crypto_command_fn (vlib_main_t * vm,
426 unformat_input_t * input, vlib_cli_command_t * cmd)
428 crypto_test_main_t *tm = &crypto_test_main;
429 unittest_crypto_test_registration_t *tr;
432 tr = tm->test_registrations;
433 memset (tm, 0, sizeof (crypto_test_main_t));
434 tm->test_registrations = tr;
437 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
439 if (unformat (input, "verbose"))
441 else if (unformat (input, "detail"))
444 if (unformat (input, "perf %U", unformat_vnet_crypto_alg, &tm->alg))
446 else if (unformat (input, "buffers %u", &tm->n_buffers))
448 else if (unformat (input, "rounds %u", &tm->rounds))
450 else if (unformat (input, "warmup-rounds %u", &tm->warmup_rounds))
452 else if (unformat (input, "buffer-size %u", &tm->buffer_size))
454 else if (unformat (input, "one-key"))
457 return clib_error_return (0, "unknown input '%U'",
458 format_unformat_error, input);
462 return test_crypto_perf (vm, tm);
464 return test_crypto (vm, tm);
468 VLIB_CLI_COMMAND (test_crypto_command, static) =
470 .path = "test crypto",
471 .short_help = "test crypto",
472 .function = test_crypto_command_fn,
476 static clib_error_t *
477 crypto_test_init (vlib_main_t * vm)
482 VLIB_INIT_FUNCTION (crypto_test_init);
485 * fd.io coding-style-patch-verification: ON
488 * eval: (c-set-style "gnu")