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