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->iv.data);
50 rte_free(vector->aad.data);
51 rte_free(vector->digest.data);
53 if (opts->test_file != NULL) {
54 rte_free(vector->plaintext.data);
55 rte_free(vector->cipher_key.data);
56 rte_free(vector->auth_key.data);
57 rte_free(vector->ciphertext.data);
66 show_test_vector(struct cperf_test_vector *test_vector)
68 const uint8_t wrap = 32;
71 if (test_vector == NULL)
74 if (test_vector->plaintext.data) {
75 printf("\nplaintext =\n");
76 for (i = 0; i < test_vector->plaintext.length; ++i) {
77 if ((i % wrap == 0) && (i != 0))
79 if (i == test_vector->plaintext.length - 1)
81 test_vector->plaintext.data[i]);
84 test_vector->plaintext.data[i]);
89 if (test_vector->cipher_key.data) {
90 printf("\ncipher_key =\n");
91 for (i = 0; i < test_vector->cipher_key.length; ++i) {
92 if ((i % wrap == 0) && (i != 0))
94 if (i == (uint32_t)(test_vector->cipher_key.length - 1))
96 test_vector->cipher_key.data[i]);
99 test_vector->cipher_key.data[i]);
104 if (test_vector->auth_key.data) {
105 printf("\nauth_key =\n");
106 for (i = 0; i < test_vector->auth_key.length; ++i) {
107 if ((i % wrap == 0) && (i != 0))
109 if (i == (uint32_t)(test_vector->auth_key.length - 1))
110 printf("0x%02x", test_vector->auth_key.data[i]);
113 test_vector->auth_key.data[i]);
118 if (test_vector->iv.data) {
120 for (i = 0; i < test_vector->iv.length; ++i) {
121 if ((i % wrap == 0) && (i != 0))
123 if (i == (uint32_t)(test_vector->iv.length - 1))
124 printf("0x%02x", test_vector->iv.data[i]);
126 printf("0x%02x, ", test_vector->iv.data[i]);
131 if (test_vector->ciphertext.data) {
132 printf("\nciphertext =\n");
133 for (i = 0; i < test_vector->ciphertext.length; ++i) {
134 if ((i % wrap == 0) && (i != 0))
136 if (i == test_vector->ciphertext.length - 1)
138 test_vector->ciphertext.data[i]);
141 test_vector->ciphertext.data[i]);
146 if (test_vector->aad.data) {
148 for (i = 0; i < test_vector->aad.length; ++i) {
149 if ((i % wrap == 0) && (i != 0))
151 if (i == (uint32_t)(test_vector->aad.length - 1))
152 printf("0x%02x", test_vector->aad.data[i]);
154 printf("0x%02x, ", test_vector->aad.data[i]);
159 if (test_vector->digest.data) {
160 printf("\ndigest =\n");
161 for (i = 0; i < test_vector->digest.length; ++i) {
162 if ((i % wrap == 0) && (i != 0))
164 if (i == (uint32_t)(test_vector->digest.length - 1))
165 printf("0x%02x", test_vector->digest.data[i]);
167 printf("0x%02x, ", test_vector->digest.data[i]);
173 /* trim leading and trailing spaces */
175 trim_space(char *str)
179 for (start = str; *start; start++) {
180 if (!isspace((unsigned char) start[0]))
184 for (end = start + strlen(start); end > start + 1; end--) {
185 if (!isspace((unsigned char) end[-1]))
191 /* Shift from "start" to the beginning of the string */
193 memmove(str, start, (end - start) + 1);
198 /* tokenization test values separated by a comma */
200 parse_values(char *tokens, uint8_t **data, uint32_t *data_length)
203 uint32_t data_size = 32;
205 uint8_t *values, *values_resized;
206 char *tok, *error = NULL;
208 tok = strtok(tokens, CPERF_VALUE_DELIMITER);
212 values = (uint8_t *) rte_zmalloc(NULL, sizeof(uint8_t) * data_size, 0);
217 while (tok != NULL) {
218 values_resized = NULL;
220 if (n_tokens >= data_size) {
223 values_resized = (uint8_t *) rte_realloc(values,
224 sizeof(uint8_t) * data_size, 0);
225 if (values_resized == NULL) {
229 values = values_resized;
232 values[n_tokens] = (uint8_t) strtoul(tok, &error, 0);
233 if ((error == NULL) || (*error != '\0')) {
234 printf("Failed with convert '%s'\n", tok);
239 tok = strtok(NULL, CPERF_VALUE_DELIMITER);
246 values_resized = (uint8_t *) rte_realloc(values,
247 sizeof(uint8_t) * (n_tokens + 1), 0);
249 if (values_resized == NULL) {
254 *data = values_resized;
255 *data_length = n_tokens + 1;
260 /* checks the type of key and assigns data */
262 parse_entry(char *entry, struct cperf_test_vector *vector,
263 struct cperf_options *opts, uint8_t tc_found)
266 uint32_t data_length;
268 uint8_t *data = NULL;
269 char *token, *key_token;
272 printf("Expected entry value\n");
277 token = strtok(entry, CPERF_ENTRY_DELIMITER);
279 /* get values for key */
280 token = strtok(NULL, CPERF_ENTRY_DELIMITER);
282 if (key_token == NULL || token == NULL) {
283 printf("Expected 'key = values' but was '%.40s'..\n", entry);
287 status = parse_values(token, &data, &data_length);
292 if (strstr(key_token, "plaintext")) {
293 rte_free(vector->plaintext.data);
294 vector->plaintext.data = data;
296 vector->plaintext.length = data_length;
298 if (opts->max_buffer_size > data_length) {
299 printf("Global plaintext shorter than "
303 vector->plaintext.length = opts->max_buffer_size;
306 } else if (strstr(key_token, "cipher_key")) {
307 rte_free(vector->cipher_key.data);
308 vector->cipher_key.data = data;
310 vector->cipher_key.length = data_length;
312 if (opts->cipher_key_sz > data_length) {
313 printf("Global cipher_key shorter than "
317 vector->cipher_key.length = opts->cipher_key_sz;
320 } else if (strstr(key_token, "auth_key")) {
321 rte_free(vector->auth_key.data);
322 vector->auth_key.data = data;
324 vector->auth_key.length = data_length;
326 if (opts->auth_key_sz > data_length) {
327 printf("Global auth_key shorter than "
331 vector->auth_key.length = opts->auth_key_sz;
334 } else if (strstr(key_token, "iv")) {
335 rte_free(vector->iv.data);
336 vector->iv.data = data;
338 vector->iv.length = data_length;
340 if (opts->cipher_iv_sz > data_length) {
341 printf("Global iv shorter than "
345 vector->iv.length = opts->cipher_iv_sz;
348 } else if (strstr(key_token, "ciphertext")) {
349 rte_free(vector->ciphertext.data);
350 vector->ciphertext.data = data;
352 vector->ciphertext.length = data_length;
354 if (opts->max_buffer_size > data_length) {
355 printf("Global ciphertext shorter than "
359 vector->ciphertext.length = opts->max_buffer_size;
362 } else if (strstr(key_token, "aad")) {
363 rte_free(vector->aad.data);
364 vector->aad.data = data;
365 vector->aad.phys_addr = rte_malloc_virt2phy(vector->aad.data);
367 vector->aad.length = data_length;
369 if (opts->auth_aad_sz > data_length) {
370 printf("Global aad shorter than "
374 vector->aad.length = opts->auth_aad_sz;
377 } else if (strstr(key_token, "digest")) {
378 rte_free(vector->digest.data);
379 vector->digest.data = data;
380 vector->digest.phys_addr = rte_malloc_virt2phy(
381 vector->digest.data);
383 vector->digest.length = data_length;
385 if (opts->auth_digest_sz > data_length) {
386 printf("Global digest shorter than "
390 vector->digest.length = opts->auth_digest_sz;
393 printf("Not valid key: '%s'\n", trim_space(key_token));
400 /* searches in the file for test keys and values */
402 parse_file(struct cperf_test_vector *vector, struct cperf_options *opts)
404 uint8_t tc_found = 0;
405 uint8_t tc_data_start = 0;
414 fp = fopen(opts->test_file, "r");
416 printf("File %s does not exists\n", opts->test_file);
420 while ((read = getline(&line, &len, fp)) != -1) {
422 /* ignore comments and new lines */
423 if (line[0] == '#' || line[0] == '/' || line[0] == '\n'
424 || line[0] == '\r' || line[0] == ' ')
429 /* next test case is started */
430 if (line[0] == '[' && line[strlen(line) - 1] == ']' && tc_found)
432 /* test case section started, end of global data */
433 else if (line[0] == '[' && line[strlen(line) - 1] == ']')
436 /* test name unspecified, end after global data */
437 if (tc_data_start && opts->test_name == NULL)
439 /* searching for a suitable test */
440 else if (tc_data_start && tc_found == 0) {
441 if (!strcmp(line, opts->test_name)) {
448 /* buffer for multiline */
449 entry = (char *) rte_realloc(entry,
450 sizeof(char) * strlen(line) + 1, 0);
454 memset(entry, 0, strlen(line) + 1);
455 strncpy(entry, line, strlen(line));
457 /* check if entry ends with , or = */
458 if (entry[strlen(entry) - 1] == ','
459 || entry[strlen(entry) - 1] == '=') {
460 while ((read = getline(&line, &len, fp)) != -1) {
463 /* extend entry about length of new line */
464 char *entry_extended = (char *) rte_realloc(
466 * (strlen(line) + strlen(entry))
469 if (entry_extended == NULL)
471 entry = entry_extended;
473 strncat(entry, line, strlen(line));
475 if (entry[strlen(entry) - 1] != ',')
479 status = parse_entry(entry, vector, opts, tc_found);
481 printf("An error occurred while parsing!\n");
486 if (tc_found == 0 && opts->test_name != NULL) {
487 printf("Not found '%s' case in test file\n", opts->test_name);
508 struct cperf_test_vector*
509 cperf_test_vector_get_from_file(struct cperf_options *opts)
512 struct cperf_test_vector *test_vector = NULL;
514 if (opts == NULL || opts->test_file == NULL)
517 test_vector = (struct cperf_test_vector *) rte_zmalloc(NULL,
518 sizeof(struct cperf_test_vector), 0);
519 if (test_vector == NULL)
522 /* filling the vector with data from a file */
523 status = parse_file(test_vector, opts);
525 free_test_vector(test_vector, opts);
529 /* other values not included in the file */
530 test_vector->data.cipher_offset = 0;
531 test_vector->data.cipher_length = opts->max_buffer_size;
533 test_vector->data.auth_offset = 0;
534 test_vector->data.auth_length = opts->max_buffer_size;