1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015, Linaro Limited
4  * All rights reserved.
5  */
6 
7 #include <adbg.h>
8 #include <fcntl.h>
9 #include <math.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <strings.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <tee_client_api.h>
18 #include <time.h>
19 #include <unistd.h>
20 
21 #include "crypto_common.h"
22 #include "xtest_helpers.h"
23 #include "xtest_test.h"
24 
25 /*
26  * TEE client stuff
27  */
28 
29 static TEEC_Context ctx;
30 static TEEC_Session sess;
31 static TEEC_SharedMemory in_shm = {
32 	.flags = TEEC_MEM_INPUT
33 };
34 static TEEC_SharedMemory out_shm = {
35 	.flags = TEEC_MEM_OUTPUT
36 };
37 
errx(const char * msg,TEEC_Result res,uint32_t * orig)38 static void errx(const char *msg, TEEC_Result res, uint32_t *orig)
39 {
40 	fprintf(stderr, "%s: 0x%08x", msg, res);
41 	if (orig)
42 		fprintf(stderr, " (orig=%d)", (int)*orig);
43 	fprintf(stderr, "\n");
44 	exit (1);
45 }
46 
check_res(TEEC_Result res,const char * errmsg,uint32_t * orig)47 static void check_res(TEEC_Result res, const char *errmsg, uint32_t *orig)
48 {
49 	if (res != TEEC_SUCCESS)
50 		errx(errmsg, res, orig);
51 }
52 
open_ta(void)53 static void open_ta(void)
54 {
55 	TEEC_Result res = TEEC_ERROR_GENERIC;
56 	TEEC_UUID uuid = TA_CRYPTO_PERF_UUID;
57 	uint32_t err_origin = 0;
58 
59 	res = TEEC_InitializeContext(NULL, &ctx);
60 	check_res(res,"TEEC_InitializeContext", NULL);
61 
62 	res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
63 			       NULL, &err_origin);
64 	check_res(res,"TEEC_OpenSession", &err_origin);
65 }
66 
67 /*
68  * Statistics
69  *
70  * We want to compute min, max, mean and standard deviation of processing time
71  */
72 
73 struct statistics {
74 	int n;
75 	double m;
76 	double M2;
77 	double min;
78 	double max;
79 	int initialized;
80 };
81 
82 /* Take new sample into account (Knuth/Welford algorithm) */
update_stats(struct statistics * s,uint64_t t)83 static void update_stats(struct statistics *s, uint64_t t)
84 {
85 	double x = (double)t;
86 	double delta = x - s->m;
87 
88 	s->n++;
89 	s->m += delta/s->n;
90 	s->M2 += delta*(x - s->m);
91 	if (!s->initialized) {
92 		s->min = s->max = x;
93 		s->initialized = 1;
94 	} else {
95 		if (s->min > x)
96 			s->min = x;
97 		if (s->max < x)
98 			s->max = x;
99 	}
100 }
101 
stddev(struct statistics * s)102 static double stddev(struct statistics *s)
103 {
104 	if (s->n < 2)
105 		return NAN;
106 	return sqrt(s->M2/s->n);
107 }
108 
algo_str(uint32_t algo)109 static const char *algo_str(uint32_t algo)
110 {
111 	switch (algo) {
112 	case TA_SHA_SHA1:
113 		return "SHA1";
114 	case TA_SHA_SHA224:
115 		return "SHA224";
116 	case TA_SHA_SHA256:
117 		return "SHA256";
118 	case TA_SHA_SHA384:
119 		return "SHA384";
120 	case TA_SHA_SHA512:
121 		return "SHA512";
122 	case TA_SM3:
123 		return "SM3";
124 	case TA_HMAC_SHA1:
125 		return "HMAC_SHA1";
126 	case TA_HMAC_SHA224:
127 		return "HMAC_SHA224";
128 	case TA_HMAC_SHA256:
129 		return "HMAC_SHA256";
130 	case TA_HMAC_SHA384:
131 		return "HMAC_SHA384";
132 	case TA_HMAC_SHA512:
133 		return "HMAC_SHA512";
134 	case TA_HMAC_SM3:
135 		return "HMAC_SM3";
136 	default:
137 		return "???";
138 	}
139 }
140 
hash_size(uint32_t algo)141 static int hash_size(uint32_t algo)
142 {
143 	switch (algo) {
144 	case TA_SHA_SHA1:
145 	case TA_HMAC_SHA1:
146 		return 20;
147 	case TA_SHA_SHA224:
148 	case TA_HMAC_SHA224:
149 		return 28;
150 	case TA_SHA_SHA256:
151 	case TA_HMAC_SHA256:
152 		return 32;
153 	case TA_SHA_SHA384:
154 	case TA_HMAC_SHA384:
155 		return 48;
156 	case TA_SHA_SHA512:
157 	case TA_HMAC_SHA512:
158 		return 64;
159 	case TA_SM3:
160 	case TA_HMAC_SM3:
161 		return 32;
162 	default:
163 		return 0;
164 	}
165 }
166 
167 #define _TO_STR(x) #x
168 #define TO_STR(x) _TO_STR(x)
169 
alloc_shm(size_t sz,uint32_t algo,int offset)170 static void alloc_shm(size_t sz, uint32_t algo, int offset)
171 {
172 	TEEC_Result res = TEEC_ERROR_GENERIC;
173 
174 	in_shm.buffer = NULL;
175 	in_shm.size = sz + offset;
176 	res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
177 	check_res(res, "TEEC_AllocateSharedMemory", NULL);
178 
179 	out_shm.buffer = NULL;
180 	out_shm.size = hash_size(algo);
181 	res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
182 	check_res(res, "TEEC_AllocateSharedMemory", NULL);
183 }
184 
free_shm(void)185 static void free_shm(void)
186 {
187 	TEEC_ReleaseSharedMemory(&in_shm);
188 	TEEC_ReleaseSharedMemory(&out_shm);
189 }
190 
read_random(void * in,size_t rsize)191 static ssize_t read_random(void *in, size_t rsize)
192 {
193 	static int rnd;
194 	ssize_t s = 0;
195 
196 	if (!rnd) {
197 		rnd = open("/dev/urandom", O_RDONLY);
198 		if (rnd < 0) {
199 			perror("open");
200 			return 1;
201 		}
202 	}
203 	s = read(rnd, in, rsize);
204 	if (s < 0) {
205 		perror("read");
206 		return 1;
207 	}
208 	if ((size_t)s != rsize) {
209 		printf("read: requested %zu bytes, got %zd\n",
210 		       rsize, s);
211 	}
212 	return 0;
213 }
214 
get_current_time(struct timespec * ts)215 static long get_current_time(struct timespec *ts)
216 {
217 	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
218 		perror("clock_gettime");
219 		exit(1);
220 	}
221 	return 0;
222 }
223 
timespec_diff_ns(struct timespec * start,struct timespec * end)224 static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
225 {
226 	uint64_t ns = 0;
227 
228 	if (end->tv_nsec < start->tv_nsec) {
229 		ns += 1000000000 * (end->tv_sec - start->tv_sec - 1);
230 		ns += 1000000000 - start->tv_nsec + end->tv_nsec;
231 	} else {
232 		ns += 1000000000 * (end->tv_sec - start->tv_sec);
233 		ns += end->tv_nsec - start->tv_nsec;
234 	}
235 	return ns;
236 }
237 
run_test_once(void * in,size_t size,int random_in,TEEC_Operation * op)238 static uint64_t run_test_once(void *in, size_t size, int random_in,
239 			      TEEC_Operation *op)
240 {
241 	struct timespec t0 = { };
242 	struct timespec t1 = { };
243 	TEEC_Result res = TEEC_ERROR_GENERIC;
244 	uint32_t ret_origin = 0;
245 
246 	if (random_in == CRYPTO_USE_RANDOM)
247 		read_random(in, size);
248 
249 	get_current_time(&t0);
250 	res = TEEC_InvokeCommand(&sess, TA_CRYPTO_PERF_CMD_HASH_PROCESS, op,
251 				 &ret_origin);
252 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
253 	get_current_time(&t1);
254 
255 	return timespec_diff_ns(&t0, &t1);
256 }
257 
prepare_op(int algo)258 static void prepare_op(int algo)
259 {
260 	TEEC_Result res = TEEC_ERROR_GENERIC;
261 	uint32_t ret_origin = 0;
262 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
263 
264 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
265 					 TEEC_NONE, TEEC_NONE);
266 	op.params[0].value.a = algo;
267 	res = TEEC_InvokeCommand(&sess, TA_CRYPTO_PERF_CMD_HASH_PREPARE_OP, &op,
268 				 &ret_origin);
269 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
270 }
271 
do_warmup(int warmup)272 static void do_warmup(int warmup)
273 {
274 	struct timespec t0 = { };
275 	struct timespec t = { };
276 	int i = 0;
277 
278 	get_current_time(&t0);
279 	do {
280 		for (i = 0; i < 100000; i++)
281 			;
282 		get_current_time(&t);
283 	} while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
284 }
285 
yesno(int v)286 static const char *yesno(int v)
287 {
288 	return (v ? "yes" : "no");
289 }
290 
mb_per_sec(size_t size,double usec)291 static double mb_per_sec(size_t size, double usec)
292 {
293 	return (1000000000/usec)*((double)size/(1024*1024));
294 }
295 
296 /* Hash test: buffer of size byte. Run test n times.
297  * Entry point for running SHA benchmark
298  * Params:
299  * algo - Algorithm
300  * size - Buffer size
301  * n - Number of measurements
302  * l - Amount of inner loops
303  * random_in - Get input from /dev/urandom
304  * offset - Buffer offset wrt. alloc-ed address
305  * warmup - Start with a-second busy loop
306  * verbosity - Verbosity level
307  * */
hash_perf_run_test(int algo,size_t size,unsigned int n,unsigned int l,int random_in,int offset,int warmup,int verbosity)308 extern void hash_perf_run_test(int algo, size_t size, unsigned int n,
309 				unsigned int l, int random_in, int offset,
310 				int warmup, int verbosity)
311 {
312 	uint64_t t = 0;
313 	struct statistics stats = { };
314 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
315 	int n0 = n;
316 	struct timespec ts = { };
317 	double sd = 0;
318 
319 	vverbose("hash-perf\n");
320 	if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
321 		perror("clock_getres");
322 		return;
323 	}
324 	vverbose("Clock resolution is %jd ns\n",
325 		 (intmax_t)ts.tv_sec * 1000000000 + ts.tv_nsec);
326 
327 	open_ta();
328 	prepare_op(algo);
329 
330 	alloc_shm(size, algo, offset);
331 
332 	if (random_in == CRYPTO_USE_ZEROS)
333 		memset((uint8_t *)in_shm.buffer + offset, 0, size);
334 
335 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
336 					 TEEC_MEMREF_PARTIAL_OUTPUT,
337 					 TEEC_VALUE_INPUT, TEEC_NONE);
338 	op.params[0].memref.parent = &in_shm;
339 	op.params[0].memref.offset = 0;
340 	op.params[0].memref.size = size + offset;
341 	op.params[1].memref.parent = &out_shm;
342 	op.params[1].memref.offset = 0;
343 	op.params[1].memref.size = hash_size(algo);
344 	op.params[2].value.a = l;
345 	op.params[2].value.b = offset;
346 
347 	verbose("Starting test: %s, size=%zu bytes, ",
348 		algo_str(algo), size);
349 	verbose("random=%s, ", yesno(random_in == CRYPTO_USE_RANDOM));
350 	verbose("unaligned=%s, ", yesno(offset));
351 	verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
352 
353 	if (warmup)
354 		do_warmup(warmup);
355 
356 	while (n-- > 0) {
357 		t = run_test_once((uint8_t *)in_shm.buffer + offset, size,
358 				  random_in, &op);
359 		update_stats(&stats, t);
360 		if (n % (n0 / 10) == 0)
361 			vverbose("#");
362 	}
363 	vverbose("\n");
364 	sd = stddev(&stats);
365 	printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n",
366 	       stats.min / 1000, stats.max / 1000, stats.m / 1000,
367 	       sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m));
368 	verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n",
369 		(stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000,
370 		mb_per_sec(size, stats.m + 2 * sd),
371 		mb_per_sec(size, stats.m - 2 * sd));
372 	free_shm();
373 }
374 
usage(const char * applet_optname,int algo,size_t size,int warmup,int l,int n)375 static void usage(const char *applet_optname,
376 				/* Default params */
377 				int algo, size_t size, int warmup, int l, int n)
378 {
379 	fprintf(stderr, "Usage: %s %s [-h]\n", xtest_progname, applet_optname);
380 	fprintf(stderr, "Usage: %s %s [-a ALGO] [-l LOOP] [-n LOOP] [-r] [-s SIZE]",
381 		xtest_progname, applet_optname);
382 	fprintf(stderr, " [-v [-v]] [-w SEC]\n");
383 	fprintf(stderr, "Hash performance testing tool for OP-TEE\n");
384 	fprintf(stderr, "\n");
385 	fprintf(stderr, "Options:\n");
386 	fprintf(stderr, "  -a ALGO          Algorithm (SHA1, SHA224, SHA256, SHA384, SHA512, SM3,"
387 			"			       HMAC_SHA1, HMAC_SHA224, HMAC_SHA256,"
388 			"			       HMAC_SHA384, HMAC_SHA512, HMAC_SM3) [%s]\n", algo_str(algo));
389 	fprintf(stderr, "  -h|--help Print this help and exit\n");
390 	fprintf(stderr, "  -l LOOP          Inner loop iterations (TA calls TEE_DigestDoFinal() <x> times) [%u]\n", l);
391 	fprintf(stderr, "  -n LOOP          Outer test loop iterations [%u]\n", n);
392 	fprintf(stderr, "  -r|--random      Get input data from /dev/urandom (default:  all-zeros)\n");
393 	fprintf(stderr, "  -s SIZE          Test buffer size in bytes [%zu]\n", size);
394 	fprintf(stderr, "  -u|--unalign     Use unaligned buffer (odd address)\n");
395 	fprintf(stderr, "  -v               Be verbose (use twice for greater effect)\n");
396 	fprintf(stderr, "  -w|--warmup SEC  Warm-up time in seconds: execute a busy loop before\n");
397 	fprintf(stderr, "                   the test to mitigate the effects of cpufreq etc. [%u]\n", warmup);
398 }
399 
400 #define NEXT_ARG(i) \
401 	do { \
402 		if (++i == argc) { \
403 			fprintf(stderr, "%s %s: %s: missing argument\n", \
404 				xtest_progname, argv[0], argv[i - 1]); \
405 			return 1; \
406 		} \
407 	} while (0);
408 
hash_perf_runner_cmd_parser(int argc,char * argv[])409 extern int hash_perf_runner_cmd_parser(int argc, char *argv[])
410 {
411 	int i = 0;
412 	/* Command line params */
413 	size_t size = 1024;	/* Buffer size (-s) */
414 	unsigned int n = CRYPTO_DEF_COUNT;/* Number of measurements (-n)*/
415 	unsigned int l = CRYPTO_DEF_LOOPS;	/* Inner loops (-l) */
416 	int verbosity = CRYPTO_DEF_VERBOSITY;	/* Verbosity (-v) */
417 	int algo = TA_SHA_SHA1;	/* Algorithm (-a) */
418 	/* Get input data from /dev/urandom (-r) */
419 	int random_in = CRYPTO_USE_ZEROS;
420 	/* Start with a 2-second busy loop (-w) */
421 	int warmup = CRYPTO_DEF_WARMUP;
422 	int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */
423 
424 	/* Parse command line */
425 	for (i = 1; i < argc; i++) {
426 		if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
427 			usage(argv[0], algo, size, warmup, l, n);
428 			return 0;
429 		}
430 	}
431 	for (i = 1; i < argc; i++) {
432 		if (!strcmp(argv[i], "-l")) {
433 			NEXT_ARG(i);
434 			l = atoi(argv[i]);
435 		} else if (!strcmp(argv[i], "-a")) {
436 			NEXT_ARG(i);
437 			if (!strcasecmp(argv[i], "SHA1"))
438 				algo = TA_SHA_SHA1;
439 			else if (!strcasecmp(argv[i], "SHA224"))
440 				algo = TA_SHA_SHA224;
441 			else if (!strcasecmp(argv[i], "SHA256"))
442 				algo = TA_SHA_SHA256;
443 			else if (!strcasecmp(argv[i], "SHA384"))
444 				algo = TA_SHA_SHA384;
445 			else if (!strcasecmp(argv[i], "SHA512"))
446 				algo = TA_SHA_SHA512;
447 			else if (!strcasecmp(argv[i], "SM3"))
448 				algo = TA_SM3;
449 			else if (!strcasecmp(argv[i], "HMAC_SHA1"))
450 				algo = TA_HMAC_SHA1;
451 			else if (!strcasecmp(argv[i], "HMAC_SHA224"))
452 				algo = TA_HMAC_SHA224;
453 			else if (!strcasecmp(argv[i], "HMAC_SHA256"))
454 				algo = TA_HMAC_SHA256;
455 			else if (!strcasecmp(argv[i], "HMAC_SHA384"))
456 				algo = TA_HMAC_SHA384;
457 			else if (!strcasecmp(argv[i], "HMAC_SHA512"))
458 				algo = TA_HMAC_SHA512;
459 			else if (!strcasecmp(argv[i], "HMAC_SM3"))
460 				algo = TA_HMAC_SM3;
461 			else {
462 				fprintf(stderr, "%s %s, invalid algorithm\n",
463 					xtest_progname, argv[0]);
464 				usage(argv[0], algo, size, warmup, l, n);
465 				return 1;
466 			}
467 		} else if (!strcmp(argv[i], "-n")) {
468 			NEXT_ARG(i);
469 			n = atoi(argv[i]);
470 		} else if (!strcmp(argv[i], "--random") ||
471 			   !strcmp(argv[i], "-r")) {
472 			random_in = CRYPTO_USE_RANDOM;
473 		} else if (!strcmp(argv[i], "-s")) {
474 			NEXT_ARG(i);
475 			size = atoi(argv[i]);
476 		} else if (!strcmp(argv[i], "--unalign") ||
477 			   !strcmp(argv[i], "-u")) {
478 			offset = 1;
479 		} else if (!strcmp(argv[i], "-v")) {
480 			verbosity++;
481 		} else if (!strcmp(argv[i], "--warmup") ||
482 			   !strcmp(argv[i], "-w")) {
483 			NEXT_ARG(i);
484 			warmup = atoi(argv[i]);
485 		} else {
486 			fprintf(stderr, "%s %s: invalid argument: %s\n",
487 				xtest_progname, argv[0], argv[i]);
488 			usage(argv[0], algo, size, warmup, l, n);
489 			return 1;
490 		}
491 	}
492 
493 	hash_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
494 
495 	return 0;
496 }
497