4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of Intel Corporation nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifdef RTE_EXEC_ENV_BSDAPP
37 #include <rte_malloc.h>
39 #include "cperf_options.h"
40 #include "cperf_test_vectors.h"
41 #include "cperf_test_vector_parsing.h"
44 free_test_vector(struct cperf_test_vector *vector, struct cperf_options *opts)
46 if (vector == NULL || opts == NULL)
49 rte_free(vector->cipher_iv.data);
50 rte_free(vector->auth_iv.data);
51 rte_free(vector->aad.data);
52 rte_free(vector->digest.data);
54 if (opts->test_file != NULL) {
55 rte_free(vector->plaintext.data);
56 rte_free(vector->cipher_key.data);
57 rte_free(vector->auth_key.data);
58 rte_free(vector->ciphertext.data);
67 show_test_vector(struct cperf_test_vector *test_vector)
69 const uint8_t wrap = 32;
72 if (test_vector == NULL)
75 if (test_vector->plaintext.data) {
76 printf("\nplaintext =\n");
77 for (i = 0; i < test_vector->plaintext.length; ++i) {
78 if ((i % wrap == 0) && (i != 0))
80 if (i == test_vector->plaintext.length - 1)
82 test_vector->plaintext.data[i]);
85 test_vector->plaintext.data[i]);
90 if (test_vector->cipher_key.data) {
91 printf("\ncipher_key =\n");
92 for (i = 0; i < test_vector->cipher_key.length; ++i) {
93 if ((i % wrap == 0) && (i != 0))
95 if (i == (uint32_t)(test_vector->cipher_key.length - 1))
97 test_vector->cipher_key.data[i]);
100 test_vector->cipher_key.data[i]);
105 if (test_vector->auth_key.data) {
106 printf("\nauth_key =\n");
107 for (i = 0; i < test_vector->auth_key.length; ++i) {
108 if ((i % wrap == 0) && (i != 0))
110 if (i == (uint32_t)(test_vector->auth_key.length - 1))
111 printf("0x%02x", test_vector->auth_key.data[i]);
114 test_vector->auth_key.data[i]);
119 if (test_vector->aead_key.data) {
120 printf("\naead_key =\n");
121 for (i = 0; i < test_vector->aead_key.length; ++i) {
122 if ((i % wrap == 0) && (i != 0))
124 if (i == (uint32_t)(test_vector->aead_key.length - 1))
125 printf("0x%02x", test_vector->aead_key.data[i]);
128 test_vector->aead_key.data[i]);
133 if (test_vector->cipher_iv.data) {
134 printf("\ncipher_iv =\n");
135 for (i = 0; i < test_vector->cipher_iv.length; ++i) {
136 if ((i % wrap == 0) && (i != 0))
138 if (i == (uint32_t)(test_vector->cipher_iv.length - 1))
139 printf("0x%02x", test_vector->cipher_iv.data[i]);
141 printf("0x%02x, ", test_vector->cipher_iv.data[i]);
146 if (test_vector->auth_iv.data) {
147 printf("\nauth_iv =\n");
148 for (i = 0; i < test_vector->auth_iv.length; ++i) {
149 if ((i % wrap == 0) && (i != 0))
151 if (i == (uint32_t)(test_vector->auth_iv.length - 1))
152 printf("0x%02x", test_vector->auth_iv.data[i]);
154 printf("0x%02x, ", test_vector->auth_iv.data[i]);
159 if (test_vector->aead_iv.data) {
160 printf("\naead_iv =\n");
161 for (i = 0; i < test_vector->aead_iv.length; ++i) {
162 if ((i % wrap == 0) && (i != 0))
164 if (i == (uint32_t)(test_vector->aead_iv.length - 1))
165 printf("0x%02x", test_vector->aead_iv.data[i]);
167 printf("0x%02x, ", test_vector->aead_iv.data[i]);
172 if (test_vector->ciphertext.data) {
173 printf("\nciphertext =\n");
174 for (i = 0; i < test_vector->ciphertext.length; ++i) {
175 if ((i % wrap == 0) && (i != 0))
177 if (i == test_vector->ciphertext.length - 1)
179 test_vector->ciphertext.data[i]);
182 test_vector->ciphertext.data[i]);
187 if (test_vector->aad.data) {
189 for (i = 0; i < test_vector->aad.length; ++i) {
190 if ((i % wrap == 0) && (i != 0))
192 if (i == (uint32_t)(test_vector->aad.length - 1))
193 printf("0x%02x", test_vector->aad.data[i]);
195 printf("0x%02x, ", test_vector->aad.data[i]);
200 if (test_vector->digest.data) {
201 printf("\ndigest =\n");
202 for (i = 0; i < test_vector->digest.length; ++i) {
203 if ((i % wrap == 0) && (i != 0))
205 if (i == (uint32_t)(test_vector->digest.length - 1))
206 printf("0x%02x", test_vector->digest.data[i]);
208 printf("0x%02x, ", test_vector->digest.data[i]);
214 /* trim leading and trailing spaces */
216 trim_space(char *str)
220 for (start = str; *start; start++) {
221 if (!isspace((unsigned char) start[0]))
225 for (end = start + strlen(start); end > start + 1; end--) {
226 if (!isspace((unsigned char) end[-1]))
232 /* Shift from "start" to the beginning of the string */
234 memmove(str, start, (end - start) + 1);
239 /* tokenization test values separated by a comma */
241 parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
244 uint32_t data_size = 32;
246 uint8_t *values, *values_resized;
247 char *tok, *error = NULL;
249 tok = strtok(tokens, CPERF_VALUE_DELIMITER);
253 values = (uint8_t *) rte_zmalloc(NULL, sizeof(uint8_t) * data_size, 0);
258 while (tok != NULL) {
259 values_resized = NULL;
261 if (n_tokens >= data_size) {
264 values_resized = (uint8_t *) rte_realloc(values,
265 sizeof(uint8_t) * data_size, 0);
266 if (values_resized == NULL) {
270 values = values_resized;
273 values[n_tokens] = (uint8_t) strtoul(tok, &error, 0);
274 if ((error == NULL) || (*error != '\0')) {
275 printf("Failed with convert '%s'\n", tok);
280 tok = strtok(NULL, CPERF_VALUE_DELIMITER);
287 values_resized = (uint8_t *) rte_realloc(values,
288 sizeof(uint8_t) * (n_tokens + 1), 0);
290 if (values_resized == NULL) {
295 *data = values_resized;
296 *data_length = n_tokens + 1;
301 /* checks the type of key and assigns data */
303 parse_entry(char *entry, struct cperf_test_vector *vector,
304 struct cperf_options *opts, uint8_t tc_found)
307 uint32_t data_length;
309 uint8_t *data = NULL;
310 char *token, *key_token;
313 printf("Expected entry value\n");
318 token = strtok(entry, CPERF_ENTRY_DELIMITER);
320 /* get values for key */
321 token = strtok(NULL, CPERF_ENTRY_DELIMITER);
323 if (key_token == NULL || token == NULL) {
324 printf("Expected 'key = values' but was '%.40s'..\n", entry);
328 status = parse_values(token, &data, &data_length);
333 if (strstr(key_token, "plaintext")) {
334 rte_free(vector->plaintext.data);
335 vector->plaintext.data = data;
337 vector->plaintext.length = data_length;
339 if (opts->max_buffer_size > data_length) {
340 printf("Global plaintext shorter than "
344 vector->plaintext.length = opts->max_buffer_size;
347 } else if (strstr(key_token, "cipher_key")) {
348 rte_free(vector->cipher_key.data);
349 vector->cipher_key.data = data;
351 vector->cipher_key.length = data_length;
353 if (opts->cipher_key_sz > data_length) {
354 printf("Global cipher_key shorter than "
358 vector->cipher_key.length = opts->cipher_key_sz;
361 } else if (strstr(key_token, "auth_key")) {
362 rte_free(vector->auth_key.data);
363 vector->auth_key.data = data;
365 vector->auth_key.length = data_length;
367 if (opts->auth_key_sz > data_length) {
368 printf("Global auth_key shorter than "
372 vector->auth_key.length = opts->auth_key_sz;
375 } else if (strstr(key_token, "aead_key")) {
376 rte_free(vector->aead_key.data);
377 vector->aead_key.data = data;
379 vector->aead_key.length = data_length;
381 if (opts->aead_key_sz > data_length) {
382 printf("Global aead_key shorter than "
386 vector->aead_key.length = opts->aead_key_sz;
389 } else if (strstr(key_token, "cipher_iv")) {
390 rte_free(vector->cipher_iv.data);
391 vector->cipher_iv.data = data;
393 vector->cipher_iv.length = data_length;
395 if (opts->cipher_iv_sz > data_length) {
396 printf("Global cipher iv shorter than "
400 vector->cipher_iv.length = opts->cipher_iv_sz;
403 } else if (strstr(key_token, "auth_iv")) {
404 rte_free(vector->auth_iv.data);
405 vector->auth_iv.data = data;
407 vector->auth_iv.length = data_length;
409 if (opts->auth_iv_sz > data_length) {
410 printf("Global auth iv shorter than "
414 vector->auth_iv.length = opts->auth_iv_sz;
417 } else if (strstr(key_token, "aead_iv")) {
418 rte_free(vector->aead_iv.data);
419 vector->aead_iv.data = data;
421 vector->aead_iv.length = data_length;
423 if (opts->aead_iv_sz > data_length) {
424 printf("Global aead iv shorter than "
428 vector->aead_iv.length = opts->aead_iv_sz;
431 } else if (strstr(key_token, "ciphertext")) {
432 rte_free(vector->ciphertext.data);
433 vector->ciphertext.data = data;
435 vector->ciphertext.length = data_length;
437 if (opts->max_buffer_size > data_length) {
438 printf("Global ciphertext shorter than "
442 vector->ciphertext.length = opts->max_buffer_size;
445 } else if (strstr(key_token, "aad")) {
446 rte_free(vector->aad.data);
447 vector->aad.data = data;
448 vector->aad.phys_addr = rte_malloc_virt2iova(vector->aad.data);
450 vector->aad.length = data_length;
452 if (opts->aead_aad_sz > data_length) {
453 printf("Global aad shorter than "
457 vector->aad.length = opts->aead_aad_sz;
460 } else if (strstr(key_token, "digest")) {
461 rte_free(vector->digest.data);
462 vector->digest.data = data;
463 vector->digest.phys_addr = rte_malloc_virt2iova(
464 vector->digest.data);
466 vector->digest.length = data_length;
468 if (opts->digest_sz > data_length) {
469 printf("Global digest shorter than "
473 vector->digest.length = opts->digest_sz;
476 printf("Not valid key: '%s'\n", trim_space(key_token));
483 /* searches in the file for test keys and values */
485 parse_file(struct cperf_test_vector *vector, struct cperf_options *opts)
487 uint8_t tc_found = 0;
488 uint8_t tc_data_start = 0;
497 fp = fopen(opts->test_file, "r");
499 printf("File %s does not exists\n", opts->test_file);
503 while ((read = getline(&line, &len, fp)) != -1) {
505 /* ignore comments and new lines */
506 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
507 || line[0] == '\r' || line[0] == ' ')
512 /* next test case is started */
513 if (line[0] == '[' && line[strlen(line) - 1] == ']' && tc_found)
515 /* test case section started, end of global data */
516 else if (line[0] == '[' && line[strlen(line) - 1] == ']')
519 /* test name unspecified, end after global data */
520 if (tc_data_start && opts->test_name == NULL)
522 /* searching for a suitable test */
523 else if (tc_data_start && tc_found == 0) {
524 if (!strcmp(line, opts->test_name)) {
531 /* buffer for multiline */
532 entry = (char *) rte_realloc(entry,
533 sizeof(char) * strlen(line) + 1, 0);
537 memset(entry, 0, strlen(line) + 1);
538 strncpy(entry, line, strlen(line));
540 /* check if entry ends with , or = */
541 if (entry[strlen(entry) - 1] == ','
542 || entry[strlen(entry) - 1] == '=') {
543 while ((read = getline(&line, &len, fp)) != -1) {
546 /* extend entry about length of new line */
547 char *entry_extended = (char *) rte_realloc(
549 * (strlen(line) + strlen(entry))
552 if (entry_extended == NULL)
554 entry = entry_extended;
556 strncat(entry, line, strlen(line));
558 if (entry[strlen(entry) - 1] != ',')
562 status = parse_entry(entry, vector, opts, tc_found);
564 printf("An error occurred while parsing!\n");
569 if (tc_found == 0 && opts->test_name != NULL) {
570 printf("Not found '%s' case in test file\n", opts->test_name);
591 struct cperf_test_vector*
592 cperf_test_vector_get_from_file(struct cperf_options *opts)
595 struct cperf_test_vector *test_vector = NULL;
597 if (opts == NULL || opts->test_file == NULL)
600 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
601 sizeof(struct cperf_test_vector), 0);
602 if (test_vector == NULL)
605 /* filling the vector with data from a file */
606 status = parse_file(test_vector, opts);
608 free_test_vector(test_vector, opts);
612 /* other values not included in the file */
613 test_vector->data.cipher_offset = 0;
614 test_vector->data.cipher_length = opts->max_buffer_size;
616 test_vector->data.auth_offset = 0;
617 test_vector->data.auth_length = opts->max_buffer_size;