1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
4 #ifdef RTE_EXEC_ENV_BSDAPP
9 #include <rte_malloc.h>
11 #include "cperf_options.h"
12 #include "cperf_test_vectors.h"
13 #include "cperf_test_vector_parsing.h"
16 free_test_vector(struct cperf_test_vector *vector, struct cperf_options *opts)
18 if (vector == NULL || opts == NULL)
21 rte_free(vector->cipher_iv.data);
22 rte_free(vector->auth_iv.data);
23 rte_free(vector->aad.data);
24 rte_free(vector->digest.data);
26 if (opts->test_file != NULL) {
27 rte_free(vector->plaintext.data);
28 rte_free(vector->cipher_key.data);
29 rte_free(vector->auth_key.data);
30 rte_free(vector->ciphertext.data);
39 show_test_vector(struct cperf_test_vector *test_vector)
41 const uint8_t wrap = 32;
44 if (test_vector == NULL)
47 if (test_vector->plaintext.data) {
48 printf("\nplaintext =\n");
49 for (i = 0; i < test_vector->plaintext.length; ++i) {
50 if ((i % wrap == 0) && (i != 0))
52 if (i == test_vector->plaintext.length - 1)
54 test_vector->plaintext.data[i]);
57 test_vector->plaintext.data[i]);
62 if (test_vector->cipher_key.data) {
63 printf("\ncipher_key =\n");
64 for (i = 0; i < test_vector->cipher_key.length; ++i) {
65 if ((i % wrap == 0) && (i != 0))
67 if (i == (uint32_t)(test_vector->cipher_key.length - 1))
69 test_vector->cipher_key.data[i]);
72 test_vector->cipher_key.data[i]);
77 if (test_vector->auth_key.data) {
78 printf("\nauth_key =\n");
79 for (i = 0; i < test_vector->auth_key.length; ++i) {
80 if ((i % wrap == 0) && (i != 0))
82 if (i == (uint32_t)(test_vector->auth_key.length - 1))
83 printf("0x%02x", test_vector->auth_key.data[i]);
86 test_vector->auth_key.data[i]);
91 if (test_vector->aead_key.data) {
92 printf("\naead_key =\n");
93 for (i = 0; i < test_vector->aead_key.length; ++i) {
94 if ((i % wrap == 0) && (i != 0))
96 if (i == (uint32_t)(test_vector->aead_key.length - 1))
97 printf("0x%02x", test_vector->aead_key.data[i]);
100 test_vector->aead_key.data[i]);
105 if (test_vector->cipher_iv.data) {
106 printf("\ncipher_iv =\n");
107 for (i = 0; i < test_vector->cipher_iv.length; ++i) {
108 if ((i % wrap == 0) && (i != 0))
110 if (i == (uint32_t)(test_vector->cipher_iv.length - 1))
111 printf("0x%02x", test_vector->cipher_iv.data[i]);
113 printf("0x%02x, ", test_vector->cipher_iv.data[i]);
118 if (test_vector->auth_iv.data) {
119 printf("\nauth_iv =\n");
120 for (i = 0; i < test_vector->auth_iv.length; ++i) {
121 if ((i % wrap == 0) && (i != 0))
123 if (i == (uint32_t)(test_vector->auth_iv.length - 1))
124 printf("0x%02x", test_vector->auth_iv.data[i]);
126 printf("0x%02x, ", test_vector->auth_iv.data[i]);
131 if (test_vector->aead_iv.data) {
132 printf("\naead_iv =\n");
133 for (i = 0; i < test_vector->aead_iv.length; ++i) {
134 if ((i % wrap == 0) && (i != 0))
136 if (i == (uint32_t)(test_vector->aead_iv.length - 1))
137 printf("0x%02x", test_vector->aead_iv.data[i]);
139 printf("0x%02x, ", test_vector->aead_iv.data[i]);
144 if (test_vector->ciphertext.data) {
145 printf("\nciphertext =\n");
146 for (i = 0; i < test_vector->ciphertext.length; ++i) {
147 if ((i % wrap == 0) && (i != 0))
149 if (i == test_vector->ciphertext.length - 1)
151 test_vector->ciphertext.data[i]);
154 test_vector->ciphertext.data[i]);
159 if (test_vector->aad.data) {
161 for (i = 0; i < test_vector->aad.length; ++i) {
162 if ((i % wrap == 0) && (i != 0))
164 if (i == (uint32_t)(test_vector->aad.length - 1))
165 printf("0x%02x", test_vector->aad.data[i]);
167 printf("0x%02x, ", test_vector->aad.data[i]);
172 if (test_vector->digest.data) {
173 printf("\ndigest =\n");
174 for (i = 0; i < test_vector->digest.length; ++i) {
175 if ((i % wrap == 0) && (i != 0))
177 if (i == (uint32_t)(test_vector->digest.length - 1))
178 printf("0x%02x", test_vector->digest.data[i]);
180 printf("0x%02x, ", test_vector->digest.data[i]);
186 /* trim leading and trailing spaces */
188 trim_space(char *str)
192 for (start = str; *start; start++) {
193 if (!isspace((unsigned char) start[0]))
197 for (end = start + strlen(start); end > start + 1; end--) {
198 if (!isspace((unsigned char) end[-1]))
204 /* Shift from "start" to the beginning of the string */
206 memmove(str, start, (end - start) + 1);
211 /* tokenization test values separated by a comma */
213 parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
216 uint32_t data_size = 32;
218 uint8_t *values, *values_resized;
219 char *tok, *error = NULL;
221 tok = strtok(tokens, CPERF_VALUE_DELIMITER);
225 values = (uint8_t *) rte_zmalloc(NULL, sizeof(uint8_t) * data_size, 0);
230 while (tok != NULL) {
231 values_resized = NULL;
233 if (n_tokens >= data_size) {
236 values_resized = (uint8_t *) rte_realloc(values,
237 sizeof(uint8_t) * data_size, 0);
238 if (values_resized == NULL) {
242 values = values_resized;
245 values[n_tokens] = (uint8_t) strtoul(tok, &error, 0);
246 if ((error == NULL) || (*error != '\0')) {
247 printf("Failed with convert '%s'\n", tok);
252 tok = strtok(NULL, CPERF_VALUE_DELIMITER);
259 values_resized = (uint8_t *) rte_realloc(values,
260 sizeof(uint8_t) * (n_tokens + 1), 0);
262 if (values_resized == NULL) {
267 *data = values_resized;
268 *data_length = n_tokens + 1;
273 /* checks the type of key and assigns data */
275 parse_entry(char *entry, struct cperf_test_vector *vector,
276 struct cperf_options *opts, uint8_t tc_found)
279 uint32_t data_length;
281 uint8_t *data = NULL;
282 char *token, *key_token;
285 printf("Expected entry value\n");
290 token = strtok(entry, CPERF_ENTRY_DELIMITER);
292 /* get values for key */
293 token = strtok(NULL, CPERF_ENTRY_DELIMITER);
295 if (key_token == NULL || token == NULL) {
296 printf("Expected 'key = values' but was '%.40s'..\n", entry);
300 status = parse_values(token, &data, &data_length);
305 if (strstr(key_token, "plaintext")) {
306 rte_free(vector->plaintext.data);
307 vector->plaintext.data = data;
309 vector->plaintext.length = data_length;
311 if (opts->max_buffer_size > data_length) {
312 printf("Global plaintext shorter than "
316 vector->plaintext.length = opts->max_buffer_size;
319 } else if (strstr(key_token, "cipher_key")) {
320 rte_free(vector->cipher_key.data);
321 vector->cipher_key.data = data;
323 vector->cipher_key.length = data_length;
325 if (opts->cipher_key_sz > data_length) {
326 printf("Global cipher_key shorter than "
330 vector->cipher_key.length = opts->cipher_key_sz;
333 } else if (strstr(key_token, "auth_key")) {
334 rte_free(vector->auth_key.data);
335 vector->auth_key.data = data;
337 vector->auth_key.length = data_length;
339 if (opts->auth_key_sz > data_length) {
340 printf("Global auth_key shorter than "
344 vector->auth_key.length = opts->auth_key_sz;
347 } else if (strstr(key_token, "aead_key")) {
348 rte_free(vector->aead_key.data);
349 vector->aead_key.data = data;
351 vector->aead_key.length = data_length;
353 if (opts->aead_key_sz > data_length) {
354 printf("Global aead_key shorter than "
358 vector->aead_key.length = opts->aead_key_sz;
361 } else if (strstr(key_token, "cipher_iv")) {
362 rte_free(vector->cipher_iv.data);
363 vector->cipher_iv.data = data;
365 vector->cipher_iv.length = data_length;
367 if (opts->cipher_iv_sz > data_length) {
368 printf("Global cipher iv shorter than "
372 vector->cipher_iv.length = opts->cipher_iv_sz;
375 } else if (strstr(key_token, "auth_iv")) {
376 rte_free(vector->auth_iv.data);
377 vector->auth_iv.data = data;
379 vector->auth_iv.length = data_length;
381 if (opts->auth_iv_sz > data_length) {
382 printf("Global auth iv shorter than "
386 vector->auth_iv.length = opts->auth_iv_sz;
389 } else if (strstr(key_token, "aead_iv")) {
390 rte_free(vector->aead_iv.data);
391 vector->aead_iv.data = data;
393 vector->aead_iv.length = data_length;
395 if (opts->aead_iv_sz > data_length) {
396 printf("Global aead iv shorter than "
400 vector->aead_iv.length = opts->aead_iv_sz;
403 } else if (strstr(key_token, "ciphertext")) {
404 rte_free(vector->ciphertext.data);
405 vector->ciphertext.data = data;
407 vector->ciphertext.length = data_length;
409 if (opts->max_buffer_size > data_length) {
410 printf("Global ciphertext shorter than "
414 vector->ciphertext.length = opts->max_buffer_size;
417 } else if (strstr(key_token, "aad")) {
418 rte_free(vector->aad.data);
419 vector->aad.data = data;
420 vector->aad.phys_addr = rte_malloc_virt2iova(vector->aad.data);
422 vector->aad.length = data_length;
424 if (opts->aead_aad_sz > data_length) {
425 printf("Global aad shorter than "
429 vector->aad.length = opts->aead_aad_sz;
432 } else if (strstr(key_token, "digest")) {
433 rte_free(vector->digest.data);
434 vector->digest.data = data;
435 vector->digest.phys_addr = rte_malloc_virt2iova(
436 vector->digest.data);
438 vector->digest.length = data_length;
440 if (opts->digest_sz > data_length) {
441 printf("Global digest shorter than "
445 vector->digest.length = opts->digest_sz;
448 printf("Not valid key: '%s'\n", trim_space(key_token));
455 /* searches in the file for test keys and values */
457 parse_file(struct cperf_test_vector *vector, struct cperf_options *opts)
459 uint8_t tc_found = 0;
460 uint8_t tc_data_start = 0;
469 fp = fopen(opts->test_file, "r");
471 printf("File %s does not exists\n", opts->test_file);
475 while ((read = getline(&line, &len, fp)) != -1) {
477 /* ignore comments and new lines */
478 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
479 || line[0] == '\r' || line[0] == ' ')
484 /* next test case is started */
485 if (line[0] == '[' && line[strlen(line) - 1] == ']' && tc_found)
487 /* test case section started, end of global data */
488 else if (line[0] == '[' && line[strlen(line) - 1] == ']')
491 /* test name unspecified, end after global data */
492 if (tc_data_start && opts->test_name == NULL)
494 /* searching for a suitable test */
495 else if (tc_data_start && tc_found == 0) {
496 if (!strcmp(line, opts->test_name)) {
503 /* buffer for multiline */
504 entry = (char *) rte_realloc(entry,
505 sizeof(char) * strlen(line) + 1, 0);
509 memset(entry, 0, strlen(line) + 1);
510 strncpy(entry, line, strlen(line));
512 /* check if entry ends with , or = */
513 if (entry[strlen(entry) - 1] == ','
514 || entry[strlen(entry) - 1] == '=') {
515 while ((read = getline(&line, &len, fp)) != -1) {
518 /* extend entry about length of new line */
519 char *entry_extended = (char *) rte_realloc(
521 * (strlen(line) + strlen(entry))
524 if (entry_extended == NULL)
526 entry = entry_extended;
528 strncat(entry, line, strlen(line));
530 if (entry[strlen(entry) - 1] != ',')
534 status = parse_entry(entry, vector, opts, tc_found);
536 printf("An error occurred while parsing!\n");
541 if (tc_found == 0 && opts->test_name != NULL) {
542 printf("Not found '%s' case in test file\n", opts->test_name);
563 struct cperf_test_vector*
564 cperf_test_vector_get_from_file(struct cperf_options *opts)
567 struct cperf_test_vector *test_vector = NULL;
569 if (opts == NULL || opts->test_file == NULL)
572 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
573 sizeof(struct cperf_test_vector), 0);
574 if (test_vector == NULL)
577 /* filling the vector with data from a file */
578 status = parse_file(test_vector, opts);
580 free_test_vector(test_vector, opts);
584 /* other values not included in the file */
585 test_vector->data.cipher_offset = 0;
586 test_vector->data.cipher_length = opts->max_buffer_size;
588 test_vector->data.auth_offset = 0;
589 test_vector->data.auth_length = opts->max_buffer_size;