1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  * All rights reserved.
5  */
6 
7 #include <fcntl.h>
8 #include <math.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <sys/ioctl.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <ta_aes_perf.h>
19 #include <tee_client_api.h>
20 #include <tee_client_api_extensions.h>
21 #include <time.h>
22 #include <unistd.h>
23 
24 #include "crypto_common.h"
25 #include "xtest_helpers.h"
26 
27 #ifdef CFG_SECURE_DATA_PATH
28 #include "sdp_basic.h"
29 
30 static int input_sdp_fd;
31 static int output_sdp_fd;
32 static int ion_heap = DEFAULT_ION_HEAP_TYPE;
33 
34 /*  re-use the allocate_ion_buffer() from sdp_basic.c */
35 int allocate_ion_buffer(size_t size, int heap_id, int verbosity);
36 #endif /* CFG_SECURE_DATA_PATH */
37 
38 /*
39  * Type of buffer used for the performance tests
40  *
41  * BUFFER_UNSPECIFIED		test did not specify target buffer to use
42  * BUFFER_SHM_ALLOCATED		buffer allocated in TEE SHM.
43  * BUFFER_SECURE_REGISTER	secure buffer, registered to TEE at TA invoc.
44  * BUFFER_SECURE_PREREGISTERED	secure buffer, registered once to TEE.
45  */
46 enum buffer_types {
47 	BUFFER_UNSPECIFIED = 0,
48 	BUFFER_SHM_ALLOCATED,
49 	BUFFER_SECURE_REGISTER,		/* requires SDP */
50 	BUFFER_SECURE_PREREGISTERED,	/* requires SDP */
51 };
52 
53 static enum buffer_types input_buffer = BUFFER_UNSPECIFIED;
54 static enum buffer_types output_buffer = BUFFER_UNSPECIFIED;
55 
buf_type_str(int buf_type)56 static const char *buf_type_str(int buf_type)
57  {
58 	static const char sec_prereg[] = "Secure memory, registered once to TEE";
59 	static const char sec_reg[] = "Secure memory, registered at each TEE invoke";
60 	static const char ns_alloc[] = "Non secure memory";
61 	static const char inval[] = "UNEXPECTED";
62 
63 	switch (buf_type) {
64 	case BUFFER_SECURE_PREREGISTERED:
65 		return sec_prereg;
66 	case BUFFER_SECURE_REGISTER:
67 		return sec_reg;
68 	case BUFFER_SHM_ALLOCATED:
69 		return ns_alloc;
70 	default:
71 		return inval;
72 	}
73 }
74 
75 /* Are we running a SDP test: default to NO (is_sdp_test == 0) */
76 static int is_sdp_test;
77 
78 /*
79  * TEE client stuff
80  */
81 
82 static TEEC_Context ctx;
83 static TEEC_Session sess;
84 /*
85  * in_shm and out_shm are both IN/OUT to support dynamically choosing
86  * in_place == 1 or in_place == 0.
87  */
88 static TEEC_SharedMemory in_shm = {
89 	.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
90 };
91 static TEEC_SharedMemory out_shm = {
92 	.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
93 };
94 
errx(const char * msg,TEEC_Result res,uint32_t * orig)95 static void errx(const char *msg, TEEC_Result res, uint32_t *orig)
96 {
97 	fprintf(stderr, "%s: 0x%08x", msg, res);
98 	if (orig)
99 		fprintf(stderr, " (orig=%d)", (int)*orig);
100 	fprintf(stderr, "\n");
101 	exit (1);
102 }
103 
check_res(TEEC_Result res,const char * errmsg,uint32_t * orig)104 static void check_res(TEEC_Result res, const char *errmsg, uint32_t *orig)
105 {
106 	if (res != TEEC_SUCCESS)
107 		errx(errmsg, res, orig);
108 }
109 
open_ta(void)110 static void open_ta(void)
111 {
112 	TEEC_Result res = TEEC_ERROR_GENERIC;
113 	TEEC_UUID uuid = TA_AES_PERF_UUID;
114 	uint32_t err_origin = 0;
115 
116 	res = TEEC_InitializeContext(NULL, &ctx);
117 	check_res(res, "TEEC_InitializeContext", NULL);
118 
119 	res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
120 			       NULL, &err_origin);
121 	check_res(res, "TEEC_OpenSession", &err_origin);
122 }
123 
124 /*
125  * Statistics
126  *
127  * We want to compute min, max, mean and standard deviation of processing time
128  */
129 
130 struct statistics {
131 	int n;
132 	double m;
133 	double M2;
134 	double min;
135 	double max;
136 	int initialized;
137 };
138 
139 /* Take new sample into account (Knuth/Welford algorithm) */
update_stats(struct statistics * s,uint64_t t)140 static void update_stats(struct statistics *s, uint64_t t)
141 {
142 	double x = (double)t;
143 	double delta = x - s->m;
144 
145 	s->n++;
146 	s->m += delta/s->n;
147 	s->M2 += delta*(x - s->m);
148 	if (!s->initialized) {
149 		s->min = s->max = x;
150 		s->initialized = 1;
151 	} else {
152 		if (s->min > x)
153 			s->min = x;
154 		if (s->max < x)
155 			s->max = x;
156 	}
157 }
158 
stddev(struct statistics * s)159 static double stddev(struct statistics *s)
160 {
161 	if (s->n < 2)
162 		return NAN;
163 	return sqrt(s->M2/s->n);
164 }
165 
mode_str(uint32_t mode)166 static const char *mode_str(uint32_t mode)
167 {
168 	switch (mode) {
169 	case TA_AES_ECB:
170 		return "ECB";
171 	case TA_AES_CBC:
172 		return "CBC";
173 	case TA_AES_CTR:
174 		return "CTR";
175 	case TA_AES_XTS:
176 		return "XTS";
177 	case TA_AES_GCM:
178 		return "GCM";
179 	default:
180 		return "???";
181 	}
182 }
183 
184 #define _TO_STR(x) #x
185 #define TO_STR(x) _TO_STR(x)
186 
usage(const char * progname,int keysize,int mode,size_t size,size_t unit,int warmup,unsigned int l,unsigned int n)187 static void usage(const char *progname, int keysize, int mode, size_t size,
188 		  size_t unit, int warmup, unsigned int l, unsigned int n)
189 {
190 	fprintf(stderr, "Usage: %s [-h]\n", progname);
191 	fprintf(stderr, "Usage: %s [-d] [-i] [-k SIZE]", progname);
192 	fprintf(stderr, " [-l LOOP] [-m MODE] [-n LOOP] [-r|--no-inited] [-s SIZE]");
193 	fprintf(stderr, " [-v [-v]] [-w SEC]");
194 #ifdef CFG_SECURE_DATA_PATH
195 	fprintf(stderr, " [--sdp [-Id|-Ir|-IR] [-Od|-Or|-OR] [--ion-heap ID]]");
196 #endif
197 	fprintf(stderr, "\n");
198 	fprintf(stderr, "AES performance testing tool for OP-TEE\n");
199 	fprintf(stderr, "\n");
200 	fprintf(stderr, "Options:\n");
201 	fprintf(stderr, "  -d            Test AES decryption instead of encryption\n");
202 	fprintf(stderr, "  -h|--help     Print this help and exit\n");
203 	fprintf(stderr, "  -i|--in-place Use same buffer for input and output (decrypt in place)\n");
204 	fprintf(stderr, "  -k SIZE       Key size in bits: 128, 192 or 256 [%u]\n", keysize);
205 	fprintf(stderr, "  -l LOOP       Inner loop iterations [%u]\n", l);
206 	fprintf(stderr, "  -m MODE       AES mode: ECB, CBC, CTR, XTS, GCM [%s]\n", mode_str(mode));
207 	fprintf(stderr, "  -n LOOP       Outer test loop iterations [%u]\n", n);
208 	fprintf(stderr, "  --not-inited  Do not initialize input buffer content.\n");
209 	fprintf(stderr, "  -r|--random   Get input data from /dev/urandom (default: all zeros)\n");
210 	fprintf(stderr, "  -s SIZE       Test buffer size in bytes [%zu]\n", size);
211 	fprintf(stderr, "  -u UNIT       Divide buffer in UNIT-byte increments (+ remainder)\n");
212 	fprintf(stderr, "                (0 to ignore) [%zu]\n", unit);
213 	fprintf(stderr, "  -v            Be verbose (use twice for greater effect)\n");
214 	fprintf(stderr, "  -w|--warmup SEC  Warm-up time in seconds: execute a busy loop before\n");
215 	fprintf(stderr, "                   the test to mitigate the effects of cpufreq etc. [%u]\n", warmup);
216 #ifdef CFG_SECURE_DATA_PATH
217 	fprintf(stderr, "Secure data path specific options:\n");
218 	fprintf(stderr, "  --sdp          Run the AES test in the scope fo a Secure Data Path test TA\n");
219 	fprintf(stderr, "  --ion-heap ID  Set ION heap ID where to allocate secure buffers [%d]\n", ion_heap);
220 	fprintf(stderr, "  -I...          AES input test buffer management:\n");
221 	fprintf(stderr, "      -Id         allocate a non secure buffer (default)\n");
222 	fprintf(stderr, "      -Ir         allocate a secure buffer, registered at each TA invocation\n");
223 	fprintf(stderr, "      -IR         allocate a secure buffer, registered once in TEE\n");
224 	fprintf(stderr, "  -O...          AES output test buffer management:\n");
225 	fprintf(stderr, "      -Od         allocate a non secure buffer (default if \"--sdp\" is not set)\n");
226 	fprintf(stderr, "      -Or         allocated a secure buffer, registered at each TA invocation\n");
227 	fprintf(stderr, "      -OR         allocated a secure buffer, registered once in TEE (default if \"--sdp\")\n");
228 #endif
229 }
230 
231 #ifdef CFG_SECURE_DATA_PATH
register_shm(TEEC_SharedMemory * shm,int fd)232 static void register_shm(TEEC_SharedMemory *shm, int fd)
233 {
234 	TEEC_Result res = TEEC_RegisterSharedMemoryFileDescriptor(&ctx, shm, fd);
235 
236 	check_res(res, "TEEC_RegisterSharedMemoryFileDescriptor", NULL);
237 }
238 #endif
239 
allocate_shm(TEEC_SharedMemory * shm,size_t sz)240 static void allocate_shm(TEEC_SharedMemory *shm, size_t sz)
241 {
242 	TEEC_Result res = TEEC_ERROR_GENERIC;
243 
244 	shm->buffer = NULL;
245 	shm->size = sz;
246 	res = TEEC_AllocateSharedMemory(&ctx, shm);
247 	check_res(res, "TEEC_AllocateSharedMemory", NULL);
248 }
249 
250 /* initial test buffer allocation (eventual registering to TEEC) */
alloc_buffers(size_t sz,int in_place,int verbosity)251 static void alloc_buffers(size_t sz, int in_place, int verbosity)
252 {
253 	(void)verbosity;
254 
255 	if (input_buffer == BUFFER_SHM_ALLOCATED)
256 		allocate_shm(&in_shm, sz);
257 #ifdef CFG_SECURE_DATA_PATH
258 	else {
259 		input_sdp_fd = allocate_ion_buffer(sz, ion_heap, verbosity);
260 		if (input_buffer == BUFFER_SECURE_PREREGISTERED) {
261 			register_shm(&in_shm, input_sdp_fd);
262 			close(input_sdp_fd);
263 		}
264 	}
265 #endif
266 
267 	if (in_place)
268 		return;
269 
270 	if (output_buffer == BUFFER_SHM_ALLOCATED)
271 		allocate_shm(&out_shm, sz);
272 #ifdef CFG_SECURE_DATA_PATH
273 	else {
274 		output_sdp_fd = allocate_ion_buffer(sz, ion_heap, verbosity);
275 		if (output_buffer == BUFFER_SECURE_PREREGISTERED) {
276 			register_shm(&out_shm, output_sdp_fd);
277 			close(output_sdp_fd);
278 		}
279 	}
280 #endif
281 }
282 
free_shm(int in_place)283 static void free_shm(int in_place)
284 {
285 	(void)in_place;
286 
287 	if (input_buffer == BUFFER_SHM_ALLOCATED &&
288 	    output_buffer == BUFFER_SHM_ALLOCATED) {
289 		TEEC_ReleaseSharedMemory(&in_shm);
290 		TEEC_ReleaseSharedMemory(&out_shm);
291 		return;
292 	}
293 
294 #ifdef CFG_SECURE_DATA_PATH
295 	if (input_buffer == BUFFER_SECURE_PREREGISTERED)
296 		close(input_sdp_fd);
297 	if (input_buffer != BUFFER_SECURE_REGISTER)
298 		TEEC_ReleaseSharedMemory(&in_shm);
299 
300 	if (in_place)
301 		return;
302 
303 	if (output_buffer == BUFFER_SECURE_PREREGISTERED)
304 		close(output_sdp_fd);
305 	if (output_buffer != BUFFER_SECURE_REGISTER)
306 		TEEC_ReleaseSharedMemory(&out_shm);
307 #endif /* CFG_SECURE_DATA_PATH */
308 }
309 
read_random(void * in,size_t rsize)310 static ssize_t read_random(void *in, size_t rsize)
311 {
312 	static int rnd;
313 	ssize_t s = 0;
314 
315 	if (!rnd) {
316 		rnd = open("/dev/urandom", O_RDONLY);
317 		if (rnd < 0) {
318 			perror("open");
319 			return 1;
320 		}
321 	}
322 	s = read(rnd, in, rsize);
323 	if (s < 0) {
324 		perror("read");
325 		return 1;
326 	}
327 	if ((size_t)s != rsize) {
328 		printf("read: requested %zu bytes, got %zd\n", rsize, s);
329 	}
330 
331 	return 0;
332 }
333 
get_current_time(struct timespec * ts)334 static void get_current_time(struct timespec *ts)
335 {
336 	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
337 		perror("clock_gettime");
338 		exit(1);
339 	}
340 }
341 
timespec_to_ns(struct timespec * ts)342 static uint64_t timespec_to_ns(struct timespec *ts)
343 {
344 	return ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec;
345 }
346 
timespec_diff_ns(struct timespec * start,struct timespec * end)347 static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
348 {
349 	return timespec_to_ns(end) - timespec_to_ns(start);
350 }
351 
prepare_key(int decrypt,int keysize,int mode)352 static void prepare_key(int decrypt, int keysize, int mode)
353 {
354 	TEEC_Result res = TEEC_ERROR_GENERIC;
355 	uint32_t ret_origin = 0;
356 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
357 	uint32_t cmd = TA_AES_PERF_CMD_PREPARE_KEY;
358 
359 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
360 					 TEEC_NONE, TEEC_NONE);
361 	op.params[0].value.a = decrypt;
362 	op.params[0].value.b = keysize;
363 	op.params[1].value.a = mode;
364 	res = TEEC_InvokeCommand(&sess, cmd, &op,
365 				 &ret_origin);
366 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
367 }
368 
do_warmup(int warmup)369 static void do_warmup(int warmup)
370 {
371 	struct timespec t0 = { };
372 	struct timespec t = { };
373 	int i = 0;
374 
375 	get_current_time(&t0);
376 	do {
377 		for (i = 0; i < 100000; i++)
378 			;
379 		get_current_time(&t);
380 	} while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
381 }
382 
yesno(int v)383 static const char *yesno(int v)
384 {
385 	return (v ? "yes" : "no");
386 }
387 
mb_per_sec(size_t size,double usec)388 static double mb_per_sec(size_t size, double usec)
389 {
390 	return (1000000000/usec)*((double)size/(1024*1024));
391 }
392 
feed_input(void * in,size_t size,int random)393 static void feed_input(void *in, size_t size, int random)
394 {
395 	if (random)
396 		read_random(in, size);
397 	else
398 		memset(in, 0, size);
399 }
400 
run_feed_input(void * in,size_t size,int random)401 static void run_feed_input(void *in, size_t size, int random)
402 {
403 	if (!is_sdp_test) {
404 		feed_input(in, size, random);
405 		return;
406 	}
407 
408 #ifdef CFG_SECURE_DATA_PATH
409 	if (input_buffer == BUFFER_SHM_ALLOCATED) {
410 		feed_input(in, size, random);
411 	} else {
412 		char *data = mmap(NULL, size, PROT_WRITE, MAP_SHARED,
413 						input_sdp_fd, 0);
414 
415 		if (data == MAP_FAILED) {
416 			perror("failed to map input buffer");
417 			exit(-1);
418 		}
419 		feed_input(data, size, random);
420 		munmap(data, size);
421 	}
422 #endif
423 }
424 
425 
aes_perf_run_test(int mode,int keysize,int decrypt,size_t size,size_t unit,unsigned int n,unsigned int l,int input_data_init,int in_place,int warmup,int verbosity)426 void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size, size_t unit,
427 				unsigned int n, unsigned int l, int input_data_init,
428 				int in_place, int warmup, int verbosity)
429 {
430 	struct statistics stats = { };
431 	struct timespec ts = { };
432 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
433 	int n0 = n;
434 	double sd = 0;
435 	uint32_t cmd = is_sdp_test ? TA_AES_PERF_CMD_PROCESS_SDP :
436 				     TA_AES_PERF_CMD_PROCESS;
437 
438 	if (input_buffer == BUFFER_UNSPECIFIED)
439 		input_buffer = BUFFER_SHM_ALLOCATED;
440 
441 	if (output_buffer == BUFFER_UNSPECIFIED) {
442 		if (is_sdp_test)
443 			output_buffer = BUFFER_SECURE_PREREGISTERED;
444 		else
445 			output_buffer = BUFFER_SHM_ALLOCATED;
446 	}
447 
448 	if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
449 		perror("clock_getres");
450 		return;
451 	}
452 	vverbose("Clock resolution is %jd ns\n",
453 		 (intmax_t)ts.tv_sec * 1000000000 + ts.tv_nsec);
454 
455 	vverbose("input test buffer:  %s\n", buf_type_str(input_buffer));
456 	vverbose("output test buffer: %s\n", buf_type_str(output_buffer));
457 
458 	open_ta();
459 	prepare_key(decrypt, keysize, mode);
460 
461 	alloc_buffers(size, in_place, verbosity);
462 	if (input_data_init == CRYPTO_USE_ZEROS)
463 		run_feed_input(in_shm.buffer, size, 0);
464 
465 	/* Using INOUT to handle the case in_place == 1 */
466 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
467 					 TEEC_MEMREF_PARTIAL_INOUT,
468 					 TEEC_VALUE_INPUT, TEEC_NONE);
469 	op.params[0].memref.parent = &in_shm;
470 	op.params[0].memref.size = size;
471 	op.params[1].memref.parent = in_place ? &in_shm : &out_shm;
472 	op.params[1].memref.size = size;
473 	op.params[2].value.a = l;
474 	op.params[2].value.b = unit;
475 
476 	verbose("Starting test: %s, %scrypt, keysize=%u bits, size=%zu bytes, ",
477 		mode_str(mode), (decrypt ? "de" : "en"), keysize, size);
478 	verbose("random=%s, ", yesno(input_data_init == CRYPTO_USE_RANDOM));
479 	verbose("in place=%s, ", yesno(in_place));
480 	verbose("inner loops=%u, loops=%u, warm-up=%u s, ", l, n, warmup);
481 	verbose("unit=%zu\n", unit);
482 
483 	if (warmup)
484 		do_warmup(warmup);
485 
486 	while (n-- > 0) {
487 		TEEC_Result res = TEEC_ERROR_GENERIC;
488 		uint32_t ret_origin = 0;
489 		struct timespec t0 = { };
490 		struct timespec t1 = { };
491 
492 		if (input_data_init == CRYPTO_USE_RANDOM)
493 			run_feed_input(in_shm.buffer, size, 1);
494 
495 		get_current_time(&t0);
496 
497 #ifdef CFG_SECURE_DATA_PATH
498 		if (input_buffer == BUFFER_SECURE_REGISTER)
499 			register_shm(&in_shm, input_sdp_fd);
500 		if (output_buffer == BUFFER_SECURE_REGISTER)
501 			register_shm(&out_shm, output_sdp_fd);
502 #endif
503 
504 		res = TEEC_InvokeCommand(&sess, cmd,
505 					 &op, &ret_origin);
506 		check_res(res, "TEEC_InvokeCommand", &ret_origin);
507 
508 #ifdef CFG_SECURE_DATA_PATH
509 		if (input_buffer == BUFFER_SECURE_REGISTER)
510 			TEEC_ReleaseSharedMemory(&in_shm);
511 		if (output_buffer == BUFFER_SECURE_REGISTER)
512 			TEEC_ReleaseSharedMemory(&out_shm);
513 #endif
514 
515 		get_current_time(&t1);
516 
517 		update_stats(&stats, timespec_diff_ns(&t0, &t1));
518 		if (n % (n0 / 10) == 0)
519 			vverbose("#");
520 	}
521 	vverbose("\n");
522 	sd = stddev(&stats);
523 	printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n",
524 	       stats.min / 1000, stats.max / 1000, stats.m / 1000,
525 	       sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m));
526 	verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n",
527 		(stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000,
528 		mb_per_sec(size, stats.m + 2 * sd),
529 		mb_per_sec(size, stats.m - 2 * sd));
530 	free_shm(in_place);
531 }
532 
533 #define NEXT_ARG(i) \
534 	do { \
535 		if (++i == argc) { \
536 			fprintf(stderr, "%s: %s: missing argument\n", \
537 				argv[0], argv[i - 1]); \
538 			return 1; \
539 		} \
540 	} while (0);
541 
542 #define USAGE() usage(argv[0], keysize, mode, size, unit, warmup, l, n)
543 
aes_perf_runner_cmd_parser(int argc,char * argv[])544 int aes_perf_runner_cmd_parser(int argc, char *argv[])
545 {
546 	int i = 0;
547 	/*
548 	* Command line parameters
549 	*/
550 	size_t size = 1024;	/* Buffer size (-s) */
551 	size_t unit = CRYPTO_DEF_UNIT_SIZE; /* Divide buffer (-u) */
552 	unsigned int n = CRYPTO_DEF_COUNT; /*Number of measurements (-n)*/
553 	unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */
554 	int verbosity = CRYPTO_DEF_VERBOSITY;	/* Verbosity (-v) */
555 	int decrypt = 0;		/* Encrypt by default, -d to decrypt */
556 	int keysize = AES_128;	/* AES key size (-k) */
557 	int mode = TA_AES_ECB;	/* AES mode (-m) */
558 	/* Get input data from /dev/urandom (-r) */
559 	int input_data_init = CRYPTO_USE_ZEROS;
560 	/* Use same buffer for in and out (-i) */
561 	int in_place = AES_PERF_INPLACE;
562 	int warmup = CRYPTO_DEF_WARMUP;	/* Start with a 2-second busy loop (-w) */
563 
564 	/* Parse command line */
565 	for (i = 1; i < argc; i++) {
566 		if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
567 			USAGE();
568 			return 0;
569 		}
570 	}
571 	for (i = 1; i < argc; i++) {
572 		if (!strcmp(argv[i], "-d")) {
573 			decrypt = 1;
574 		} else if (!strcmp(argv[i], "--in-place") ||
575 			   !strcmp(argv[i], "-i")) {
576 			in_place = 1;
577 		} else if (!strcmp(argv[i], "-k")) {
578 			NEXT_ARG(i);
579 			keysize = atoi(argv[i]);
580 			if (keysize != AES_128 && keysize != AES_192 &&
581 				keysize != AES_256) {
582 				fprintf(stderr, "%s: invalid key size\n",
583 					argv[0]);
584 				USAGE();
585 				return 1;
586 			}
587 		} else if (!strcmp(argv[i], "-l")) {
588 			NEXT_ARG(i);
589 			l = atoi(argv[i]);
590 		} else if (!strcmp(argv[i], "-m")) {
591 			NEXT_ARG(i);
592 			if (!strcasecmp(argv[i], "ECB"))
593 				mode = TA_AES_ECB;
594 			else if (!strcasecmp(argv[i], "CBC"))
595 				mode = TA_AES_CBC;
596 			else if (!strcasecmp(argv[i], "CTR"))
597 				mode = TA_AES_CTR;
598 			else if (!strcasecmp(argv[i], "XTS"))
599 				mode = TA_AES_XTS;
600 			else if (!strcasecmp(argv[i], "GCM"))
601 				mode = TA_AES_GCM;
602 			else {
603 				fprintf(stderr, "%s, invalid mode\n",
604 					argv[0]);
605 				USAGE();
606 				return 1;
607 			}
608 		} else if (!strcmp(argv[i], "-n")) {
609 			NEXT_ARG(i);
610 			n = atoi(argv[i]);
611 		} else if (!strcmp(argv[i], "--random") ||
612 			   !strcmp(argv[i], "-r")) {
613 			if (input_data_init == CRYPTO_NOT_INITED) {
614 				perror("--random is not compatible with --not-inited\n");
615 				USAGE();
616 				return 1;
617 			}
618 			input_data_init = CRYPTO_USE_RANDOM;
619 		} else if (!strcmp(argv[i], "--not-inited")) {
620 			if (input_data_init == CRYPTO_USE_RANDOM) {
621 				perror("--random is not compatible with --not-inited\n");
622 				USAGE();
623 				return 1;
624 			}
625 			input_data_init = CRYPTO_NOT_INITED;
626 		} else if (!strcmp(argv[i], "-s")) {
627 			NEXT_ARG(i);
628 			size = atoi(argv[i]);
629 #ifdef CFG_SECURE_DATA_PATH
630 		} else if (!strcmp(argv[i], "--sdp")) {
631 			is_sdp_test = 1;
632 		} else if (!strcmp(argv[i], "-IR")) {
633 			input_buffer = BUFFER_SECURE_PREREGISTERED;
634 		} else if (!strcmp(argv[i], "-OR")) {
635 			output_buffer = BUFFER_SECURE_PREREGISTERED;
636 		} else if (!strcmp(argv[i], "-Ir")) {
637 			input_buffer = BUFFER_SECURE_REGISTER;
638 		} else if (!strcmp(argv[i], "-Or")) {
639 			output_buffer = BUFFER_SECURE_REGISTER;
640 		} else if (!strcmp(argv[i], "-Id")) {
641 			input_buffer = BUFFER_SHM_ALLOCATED;
642 		} else if (!strcmp(argv[i], "-Od")) {
643 			output_buffer = BUFFER_SHM_ALLOCATED;
644 		} else if (!strcmp(argv[i], "--ion-heap")) {
645 			NEXT_ARG(i);
646 			ion_heap = atoi(argv[i]);
647 #endif
648 		} else if (!strcmp(argv[i], "-u")) {
649 			NEXT_ARG(i);
650 			unit = atoi(argv[i]);
651 		} else if (!strcmp(argv[i], "-v")) {
652 			verbosity++;
653 		} else if (!strcmp(argv[i], "--warmup") ||
654 			   !strcmp(argv[i], "-w")) {
655 			NEXT_ARG(i);
656 			warmup = atoi(argv[i]);
657 		} else {
658 			fprintf(stderr, "%s: invalid argument: %s\n",
659 				argv[0], argv[i]);
660 			USAGE();
661 			return 1;
662 		}
663 	}
664 
665 	if (size & (16 - 1)) {
666 		fprintf(stderr, "invalid buffer size argument, must be a multiple of 16\n\n");
667 		USAGE();
668 		return 1;
669 	}
670 
671 
672 	aes_perf_run_test(mode, keysize, decrypt, size, unit, n, l,
673 			  input_data_init, in_place, warmup, verbosity);
674 
675 	return 0;
676 }
677