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_SHA_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 	default:
122 		return "???";
123 	}
124 }
125 
hash_size(uint32_t algo)126 static int hash_size(uint32_t algo)
127 {
128 	switch (algo) {
129 	case TA_SHA_SHA1:
130 		return 20;
131 	case TA_SHA_SHA224:
132 		return 28;
133 	case TA_SHA_SHA256:
134 		return 32;
135 	case TA_SHA_SHA384:
136 		return 48;
137 	case TA_SHA_SHA512:
138 		return 64;
139 	default:
140 		return 0;
141 	}
142 }
143 
144 #define _TO_STR(x) #x
145 #define TO_STR(x) _TO_STR(x)
146 
147 
alloc_shm(size_t sz,uint32_t algo,int offset)148 static void alloc_shm(size_t sz, uint32_t algo, int offset)
149 {
150 	TEEC_Result res = TEEC_ERROR_GENERIC;
151 
152 	in_shm.buffer = NULL;
153 	in_shm.size = sz + offset;
154 	res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
155 	check_res(res, "TEEC_AllocateSharedMemory", NULL);
156 
157 	out_shm.buffer = NULL;
158 	out_shm.size = hash_size(algo);
159 	res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
160 	check_res(res, "TEEC_AllocateSharedMemory", NULL);
161 }
162 
free_shm(void)163 static void free_shm(void)
164 {
165 	TEEC_ReleaseSharedMemory(&in_shm);
166 	TEEC_ReleaseSharedMemory(&out_shm);
167 }
168 
read_random(void * in,size_t rsize)169 static ssize_t read_random(void *in, size_t rsize)
170 {
171 	static int rnd;
172 	ssize_t s = 0;
173 
174 	if (!rnd) {
175 		rnd = open("/dev/urandom", O_RDONLY);
176 		if (rnd < 0) {
177 			perror("open");
178 			return 1;
179 		}
180 	}
181 	s = read(rnd, in, rsize);
182 	if (s < 0) {
183 		perror("read");
184 		return 1;
185 	}
186 	if ((size_t)s != rsize) {
187 		printf("read: requested %zu bytes, got %zd\n",
188 		       rsize, s);
189 	}
190 	return 0;
191 }
192 
get_current_time(struct timespec * ts)193 static long get_current_time(struct timespec *ts)
194 {
195 	if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
196 		perror("clock_gettime");
197 		exit(1);
198 	}
199 	return 0;
200 }
201 
timespec_diff_ns(struct timespec * start,struct timespec * end)202 static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
203 {
204 	uint64_t ns = 0;
205 
206 	if (end->tv_nsec < start->tv_nsec) {
207 		ns += 1000000000 * (end->tv_sec - start->tv_sec - 1);
208 		ns += 1000000000 - start->tv_nsec + end->tv_nsec;
209 	} else {
210 		ns += 1000000000 * (end->tv_sec - start->tv_sec);
211 		ns += end->tv_nsec - start->tv_nsec;
212 	}
213 	return ns;
214 }
215 
run_test_once(void * in,size_t size,int random_in,TEEC_Operation * op)216 static uint64_t run_test_once(void *in, size_t size,  int random_in,
217 			      TEEC_Operation *op)
218 {
219 	struct timespec t0 = { };
220 	struct timespec t1 = { };
221 	TEEC_Result res = TEEC_ERROR_GENERIC;
222 	uint32_t ret_origin = 0;
223 
224 	if (random_in == CRYPTO_USE_RANDOM)
225 		read_random(in, size);
226 
227 	get_current_time(&t0);
228 	res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op,
229 				 &ret_origin);
230 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
231 	get_current_time(&t1);
232 
233 	return timespec_diff_ns(&t0, &t1);
234 }
235 
prepare_op(int algo)236 static void prepare_op(int algo)
237 {
238 	TEEC_Result res = TEEC_ERROR_GENERIC;
239 	uint32_t ret_origin = 0;
240 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
241 
242 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
243 					 TEEC_NONE, TEEC_NONE);
244 	op.params[0].value.a = algo;
245 	res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PREPARE_OP, &op,
246 				 &ret_origin);
247 	check_res(res, "TEEC_InvokeCommand", &ret_origin);
248 }
249 
do_warmup(int warmup)250 static void do_warmup(int warmup)
251 {
252 	struct timespec t0 = { };
253 	struct timespec t = { };
254 	int i = 0;
255 
256 	get_current_time(&t0);
257 	do {
258 		for (i = 0; i < 100000; i++)
259 			;
260 		get_current_time(&t);
261 	} while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
262 }
263 
yesno(int v)264 static const char *yesno(int v)
265 {
266 	return (v ? "yes" : "no");
267 }
268 
mb_per_sec(size_t size,double usec)269 static double mb_per_sec(size_t size, double usec)
270 {
271 	return (1000000000/usec)*((double)size/(1024*1024));
272 }
273 
274 /* Hash test: buffer of size byte. Run test n times.
275  * Entry point for running SHA benchmark
276  * Params:
277  * algo - Algorithm
278  * size - Buffer size
279  * n - Number of measurements
280  * l - Amount of inner loops
281  * random_in - Get input from /dev/urandom
282  * offset - Buffer offset wrt. alloc-ed address
283  * warmup - Start with a-second busy loop
284  * verbosity - Verbosity level
285  * */
sha_perf_run_test(int algo,size_t size,unsigned int n,unsigned int l,int random_in,int offset,int warmup,int verbosity)286 extern void sha_perf_run_test(int algo, size_t size, unsigned int n,
287 				unsigned int l, int random_in, int offset,
288 				int warmup, int verbosity)
289 {
290 	uint64_t t = 0;
291 	struct statistics stats = { };
292 	TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
293 	int n0 = n;
294 	struct timespec ts = { };
295 	double sd = 0;
296 
297 	vverbose("sha-perf\n");
298 	if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
299 		perror("clock_getres");
300 		return;
301 	}
302 	vverbose("Clock resolution is %jd ns\n",
303 		 (intmax_t)ts.tv_sec * 1000000000 + ts.tv_nsec);
304 
305 	open_ta();
306 	prepare_op(algo);
307 
308 	alloc_shm(size, algo, offset);
309 
310 	if (random_in == CRYPTO_USE_ZEROS)
311 		memset((uint8_t *)in_shm.buffer + offset, 0, size);
312 
313 	op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
314 					 TEEC_MEMREF_PARTIAL_OUTPUT,
315 					 TEEC_VALUE_INPUT, TEEC_NONE);
316 	op.params[0].memref.parent = &in_shm;
317 	op.params[0].memref.offset = 0;
318 	op.params[0].memref.size = size + offset;
319 	op.params[1].memref.parent = &out_shm;
320 	op.params[1].memref.offset = 0;
321 	op.params[1].memref.size = hash_size(algo);
322 	op.params[2].value.a = l;
323 	op.params[2].value.b = offset;
324 
325 	verbose("Starting test: %s, size=%zu bytes, ",
326 		algo_str(algo), size);
327 	verbose("random=%s, ", yesno(random_in == CRYPTO_USE_RANDOM));
328 	verbose("unaligned=%s, ", yesno(offset));
329 	verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
330 
331 	if (warmup)
332 		do_warmup(warmup);
333 
334 	while (n-- > 0) {
335 		t = run_test_once((uint8_t *)in_shm.buffer + offset, size,
336 				  random_in, &op);
337 		update_stats(&stats, t);
338 		if (n % (n0 / 10) == 0)
339 			vverbose("#");
340 	}
341 	vverbose("\n");
342 	sd = stddev(&stats);
343 	printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n",
344 	       stats.min / 1000, stats.max / 1000, stats.m / 1000,
345 	       sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m));
346 	verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n",
347 		(stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000,
348 		mb_per_sec(size, stats.m + 2 * sd),
349 		mb_per_sec(size, stats.m - 2 * sd));
350 	free_shm();
351 }
352 
usage(const char * progname,int algo,size_t size,int warmup,int l,int n)353 static void usage(const char *progname,
354 				/* Default params */
355 				int algo, size_t size, int warmup, int l, int n)
356 {
357 	fprintf(stderr, "Usage: %s [-h]\n", progname);
358 	fprintf(stderr, "Usage: %s [-a ALGO] [-l LOOP] [-n LOOP] [-r] [-s SIZE]", progname);
359 	fprintf(stderr, " [-v [-v]] [-w SEC]\n");
360 	fprintf(stderr, "SHA performance testing tool for OP-TEE\n");
361 	fprintf(stderr, "\n");
362 	fprintf(stderr, "Options:\n");
363 	fprintf(stderr, "  -a ALGO          Algorithm (SHA1, SHA224, SHA256, SHA384, SHA512) [%s]\n", algo_str(algo));
364 	fprintf(stderr, "  -h|--help Print this help and exit\n");
365 	fprintf(stderr, "  -l LOOP          Inner loop iterations (TA calls TEE_DigestDoFinal() <x> times) [%u]\n", l);
366 	fprintf(stderr, "  -n LOOP          Outer test loop iterations [%u]\n", n);
367 	fprintf(stderr, "  -r|--random      Get input data from /dev/urandom (default:  all-zeros)\n");
368 	fprintf(stderr, "  -s SIZE          Test buffer size in bytes [%zu]\n", size);
369 	fprintf(stderr, "  -u|--unalign     Use unaligned buffer (odd address)\n");
370 	fprintf(stderr, "  -v               Be verbose (use twice for greater effect)\n");
371 	fprintf(stderr, "  -w|--warmup SEC  Warm-up time in seconds: execute a busy loop before\n");
372 	fprintf(stderr, "                   the test to mitigate the effects of cpufreq etc. [%u]\n", warmup);
373 }
374 
375 #define NEXT_ARG(i) \
376 	do { \
377 		if (++i == argc) { \
378 			fprintf(stderr, "%s: %s: missing argument\n", \
379 				argv[0], argv[i - 1]); \
380 			return 1; \
381 		} \
382 	} while (0);
383 
384 
385 
sha_perf_runner_cmd_parser(int argc,char * argv[])386 extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
387 {
388 	int i = 0;
389 	/* Command line params */
390 	size_t size = 1024;	/* Buffer size (-s) */
391 	unsigned int n = CRYPTO_DEF_COUNT;/* Number of measurements (-n)*/
392 	unsigned int l = CRYPTO_DEF_LOOPS;	/* Inner loops (-l) */
393 	int verbosity = CRYPTO_DEF_VERBOSITY;	/* Verbosity (-v) */
394 	int algo = TA_SHA_SHA1;	/* Algorithm (-a) */
395 	/* Get input data from /dev/urandom (-r) */
396 	int random_in = CRYPTO_USE_ZEROS;
397 	/* Start with a 2-second busy loop (-w) */
398 	int warmup = CRYPTO_DEF_WARMUP;
399 	int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */
400 
401 	/* Parse command line */
402 	for (i = 1; i < argc; i++) {
403 		if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
404 			usage(argv[0], algo, size, warmup, l, n);
405 			return 0;
406 		}
407 	}
408 	for (i = 1; i < argc; i++) {
409 		if (!strcmp(argv[i], "-l")) {
410 			NEXT_ARG(i);
411 			l = atoi(argv[i]);
412 		} else if (!strcmp(argv[i], "-a")) {
413 			NEXT_ARG(i);
414 			if (!strcasecmp(argv[i], "SHA1"))
415 				algo = TA_SHA_SHA1;
416 			else if (!strcasecmp(argv[i], "SHA224"))
417 				algo = TA_SHA_SHA224;
418 			else if (!strcasecmp(argv[i], "SHA256"))
419 				algo = TA_SHA_SHA256;
420 			else if (!strcasecmp(argv[i], "SHA384"))
421 				algo = TA_SHA_SHA384;
422 			else if (!strcasecmp(argv[i], "SHA512"))
423 				algo = TA_SHA_SHA512;
424 			else {
425 				fprintf(stderr, "%s, invalid algorithm\n",
426 					argv[0]);
427 				usage(argv[0], algo, size, warmup, l, n);
428 				return 1;
429 			}
430 		} else if (!strcmp(argv[i], "-n")) {
431 			NEXT_ARG(i);
432 			n = atoi(argv[i]);
433 		} else if (!strcmp(argv[i], "--random") ||
434 			   !strcmp(argv[i], "-r")) {
435 			random_in = CRYPTO_USE_RANDOM;
436 		} else if (!strcmp(argv[i], "-s")) {
437 			NEXT_ARG(i);
438 			size = atoi(argv[i]);
439 		} else if (!strcmp(argv[i], "--unalign") ||
440 			   !strcmp(argv[i], "-u")) {
441 			offset = 1;
442 		} else if (!strcmp(argv[i], "-v")) {
443 			verbosity++;
444 		} else if (!strcmp(argv[i], "--warmup") ||
445 			   !strcmp(argv[i], "-w")) {
446 			NEXT_ARG(i);
447 			warmup = atoi(argv[i]);
448 		} else {
449 			fprintf(stderr, "%s: invalid argument: %s\n",
450 				argv[0], argv[i]);
451 			usage(argv[0], algo, size, warmup, l, n);
452 			return 1;
453 		}
454 	}
455 
456 	sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
457 
458 	return 0;
459 }
460