blob: fa757d930905ae6b39b210f92830094f425d0d3e [file] [log] [blame]
Igor Opaniuk136644a2016-09-13 13:40:56 +03001/*
2 * Copyright (c) 2015, Linaro Limited
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <fcntl.h>
31#include <math.h>
32#include <stdint.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <strings.h>
37#include <time.h>
38#include <unistd.h>
39
40#include <adbg.h>
41#include <tee_client_api.h>
Igor Opaniukf9b7fd22016-09-16 16:22:34 +030042#include "crypto_common.h"
Igor Opaniuk136644a2016-09-13 13:40:56 +030043
44/*
45 * TEE client stuff
46 */
47
48static TEEC_Context ctx;
49static TEEC_Session sess;
50static TEEC_SharedMemory in_shm = {
51 .flags = TEEC_MEM_INPUT
52};
53static TEEC_SharedMemory out_shm = {
54 .flags = TEEC_MEM_OUTPUT
55};
56
57static void errx(const char *msg, TEEC_Result res)
58{
59 fprintf(stderr, "%s: 0x%08x", msg, res);
60 exit (1);
61}
62
63static void check_res(TEEC_Result res, const char *errmsg)
64{
65 if (res != TEEC_SUCCESS)
66 errx(errmsg, res);
67}
68
69static void open_ta(void)
70{
71 TEEC_Result res;
72 TEEC_UUID uuid = TA_SHA_PERF_UUID;
73 uint32_t err_origin;
74
75 res = TEEC_InitializeContext(NULL, &ctx);
76 check_res(res,"TEEC_InitializeContext");
77
78 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
79 NULL, &err_origin);
80 check_res(res,"TEEC_OpenSession");
81}
82
83/*
84 * Statistics
85 *
86 * We want to compute min, max, mean and standard deviation of processing time
87 */
88
89struct statistics {
90 int n;
91 double m;
92 double M2;
93 double min;
94 double max;
95 int initialized;
96};
97
98/* Take new sample into account (Knuth/Welford algorithm) */
99static void update_stats(struct statistics *s, uint64_t t)
100{
101 double x = (double)t;
102 double delta = x - s->m;
103
104 s->n++;
105 s->m += delta/s->n;
106 s->M2 += delta*(x - s->m);
107 if (!s->initialized) {
108 s->min = s->max = x;
109 s->initialized = 1;
110 } else {
111 if (s->min > x)
112 s->min = x;
113 if (s->max < x)
114 s->max = x;
115 }
116}
117
118static double stddev(struct statistics *s)
119{
120 if (s->n < 2)
121 return NAN;
122 return sqrt(s->M2/s->n);
123}
124
125static const char *algo_str(uint32_t algo)
126{
127 switch (algo) {
128 case TA_SHA_SHA1:
129 return "SHA1";
130 case TA_SHA_SHA224:
131 return "SHA224";
132 case TA_SHA_SHA256:
133 return "SHA256";
134 case TA_SHA_SHA384:
135 return "SHA384";
136 case TA_SHA_SHA512:
137 return "SHA512";
138 default:
139 return "???";
140 }
141}
142
143static int hash_size(uint32_t algo)
144{
145 switch (algo) {
146 case TA_SHA_SHA1:
147 return 20;
148 case TA_SHA_SHA224:
149 return 28;
150 case TA_SHA_SHA256:
151 return 32;
152 case TA_SHA_SHA384:
153 return 48;
154 case TA_SHA_SHA512:
155 return 64;
156 default:
157 return 0;
158 }
159}
160
161#define _TO_STR(x) #x
162#define TO_STR(x) _TO_STR(x)
163
164
165static void alloc_shm(size_t sz, uint32_t algo, int offset)
166{
167 TEEC_Result res;
168
169 in_shm.buffer = NULL;
170 in_shm.size = sz + offset;
171 res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
172 check_res(res, "TEEC_AllocateSharedMemory");
173
174 out_shm.buffer = NULL;
175 out_shm.size = hash_size(algo);
176 res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
177 check_res(res, "TEEC_AllocateSharedMemory");
178}
179
180static void free_shm(void)
181{
182 TEEC_ReleaseSharedMemory(&in_shm);
183 TEEC_ReleaseSharedMemory(&out_shm);
184}
185
186static ssize_t read_random(void *in, size_t rsize)
187{
188 static int rnd;
189 ssize_t s;
190
191 if (!rnd) {
192 rnd = open("/dev/urandom", O_RDONLY);
193 if (rnd < 0) {
194 perror("open");
195 return 1;
196 }
197 }
198 s = read(rnd, in, rsize);
199 if (s < 0) {
200 perror("read");
201 return 1;
202 }
203 if ((size_t)s != rsize) {
204 printf("read: requested %zu bytes, got %zd\n",
205 rsize, s);
206 }
207 return 0;
208}
209
210static long get_current_time(struct timespec *ts)
211{
212 if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
213 perror("clock_gettime");
214 exit(1);
215 }
216 return 0;
217}
218
219static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
220{
221 uint64_t ns = 0;
222
223 if (end->tv_nsec < start->tv_nsec) {
224 ns += 1000000000 * (end->tv_sec - start->tv_sec - 1);
225 ns += 1000000000 - start->tv_nsec + end->tv_nsec;
226 } else {
227 ns += 1000000000 * (end->tv_sec - start->tv_sec);
228 ns += end->tv_nsec - start->tv_nsec;
229 }
230 return ns;
231}
232
233static uint64_t run_test_once(void *in, size_t size, int random_in, TEEC_Operation *op)
234{
235 struct timespec t0, t1;
236 TEEC_Result res;
237 uint32_t ret_origin;
238
239 if (random_in)
240 read_random(in, size);
241 get_current_time(&t0);
242 res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op,
243 &ret_origin);
244 check_res(res, "TEEC_InvokeCommand");
245 get_current_time(&t1);
246
247 return timespec_diff_ns(&t0, &t1);
248}
249
250static void prepare_op(int algo)
251{
252 TEEC_Result res;
253 uint32_t ret_origin;
254 TEEC_Operation op;
255
256 memset(&op, 0, sizeof(op));
257 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
258 TEEC_NONE, TEEC_NONE);
259 op.params[0].value.a = algo;
260 res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PREPARE_OP, &op,
261 &ret_origin);
262 check_res(res, "TEEC_InvokeCommand");
263}
264
265static void do_warmup(int warmup)
266{
267 struct timespec t0, t;
268 int i;
269
270 get_current_time(&t0);
271 do {
272 for (i = 0; i < 100000; i++)
273 ;
274 get_current_time(&t);
275 } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
276}
277
278static const char *yesno(int v)
279{
280 return (v ? "yes" : "no");
281}
282
283static double mb_per_sec(size_t size, double usec)
284{
285 return (1000000000/usec)*((double)size/(1024*1024));
286}
287
288/* Hash test: buffer of size byte. Run test n times.
289 * Entry point for running SHA benchmark
290 * Params:
291 * algo - Algorithm
292 * size - Buffer size
293 * n - Number of measurements
294 * l - Amount of inner loops
295 * random_in - Get input from /dev/urandom
296 * offset - Buffer offset wrt. alloc-ed address
297 * warmup - Start with a-second busy loop
298 * verbosity - Verbosity level
299 * */
300extern void sha_perf_run_test(int algo, size_t size, unsigned int n,
301 unsigned int l, int random_in, int offset,
302 int warmup, int verbosity)
303{
304 uint64_t t;
305 struct statistics stats;
306 TEEC_Operation op;
307 int n0 = n;
308 struct timespec ts;
309
310 vverbose("sha-perf version %s\n", TO_STR(VERSION));
311 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
312 perror("clock_getres");
313 return;
314 }
315 vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 +
316 ts.tv_nsec);
317
318 open_ta();
319 prepare_op(algo);
320
321
322 alloc_shm(size, algo, offset);
323
324 if (!random_in)
325 memset((uint8_t *)in_shm.buffer + offset, 0, size);
326
327 memset(&op, 0, sizeof(op));
328 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
329 TEEC_MEMREF_PARTIAL_OUTPUT,
330 TEEC_VALUE_INPUT, TEEC_NONE);
331 op.params[0].memref.parent = &in_shm;
332 op.params[0].memref.offset = 0;
333 op.params[0].memref.size = size + offset;
334 op.params[1].memref.parent = &out_shm;
335 op.params[1].memref.offset = 0;
336 op.params[1].memref.size = hash_size(algo);
337 op.params[2].value.a = l;
338 op.params[2].value.b = offset;
339
340 verbose("Starting test: %s, size=%zu bytes, ",
341 algo_str(algo), size);
342 verbose("random=%s, ", yesno(random_in));
343 verbose("unaligned=%s, ", yesno(offset));
344 verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
345
346 if (warmup)
347 do_warmup(warmup);
348
349 memset(&stats, 0, sizeof(stats));
350 while (n-- > 0) {
351 t = run_test_once((uint8_t *)in_shm.buffer + offset, size, random_in, &op);
352 update_stats(&stats, t);
353 if (n % (n0/10) == 0)
354 vverbose("#");
355 }
356 vverbose("\n");
357 printf("min=%gμs max=%gμs mean=%gμs stddev=%gμs (%gMiB/s)\n",
358 stats.min/1000, stats.max/1000, stats.m/1000,
359 stddev(&stats)/1000, mb_per_sec(size, stats.m));
360 free_shm();
361}
362
363static void usage(const char *progname,
364 /* Default params */
365 int algo, size_t size, int warmup, int l, int n)
366{
367 fprintf(stderr, "SHA performance testing tool for OP-TEE (%s)\n\n",
368 TO_STR(VERSION));
369 fprintf(stderr, "Usage:\n");
370 fprintf(stderr, " %s -h\n", progname);
371 fprintf(stderr, " %s [-v] [-a algo] ", progname);
372 fprintf(stderr, "[-s bufsize] [-r] [-n loops] [-l iloops] ");
373 fprintf(stderr, "[-w warmup_time]\n");
374 fprintf(stderr, "Options:\n");
375 fprintf(stderr, " -h Print this help and exit\n");
376 fprintf(stderr, " -l Inner loop iterations (TA hashes ");
377 fprintf(stderr, "the buffer <x> times) [%u]\n", l);
378 fprintf(stderr, " -a Algorithm (SHA1, SHA224, SHA256, SHA384, ");
379 fprintf(stderr, "SHA512) [%s]\n", algo_str(algo));
380 fprintf(stderr, " -n Outer loop iterations [%u]\n", n);
381 fprintf(stderr, " -r Get input data from /dev/urandom ");
382 fprintf(stderr, "(otherwise use zero-filled buffer)\n");
383 fprintf(stderr, " -s Buffer size (process <x> bytes at a time) ");
384 fprintf(stderr, "[%zu]\n", size);
385 fprintf(stderr, " -u Use unaligned buffer (odd address)\n");
386 fprintf(stderr, " -v Be verbose (use twice for greater effect)\n");
387 fprintf(stderr, " -w Warm-up time in seconds: execute a busy ");
388 fprintf(stderr, "loop before the test\n");
389 fprintf(stderr, " to mitigate the effects of cpufreq etc. ");
390 fprintf(stderr, "[%u]\n", warmup);
391}
392
393#define NEXT_ARG(i) \
394 do { \
395 if (++i == argc) { \
396 fprintf(stderr, "%s: %s: missing argument\n", \
397 argv[0], argv[i-1]); \
398 return 1; \
399 } \
400 } while (0);
401
402
403
404extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
405{
406 int i;
407
408 /* Command line params */
409 size_t size = 1024; /* Buffer size (-s) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300410 unsigned int n = CRYPTO_DEF_COUNT;/* Number of measurements (-n)*/
411 unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */
412 int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */
Igor Opaniuk136644a2016-09-13 13:40:56 +0300413 int algo = TA_SHA_SHA1; /* Algorithm (-a) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300414 /* Get input data from /dev/urandom (-r) */
415 int random_in = CRYPTO_USE_RANDOM;
416 /* Start with a 2-second busy loop (-w) */
417 int warmup = CRYPTO_DEF_WARMUP;
418 int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */
Igor Opaniuk136644a2016-09-13 13:40:56 +0300419
420
421 /* Parse command line */
422 for (i = 1; i < argc; i++) {
423 if (!strcmp(argv[i], "-h")) {
424 usage(argv[0], algo, size, warmup, l, n);
425 return 0;
426 }
427 }
428 for (i = 1; i < argc; i++) {
429 if (!strcmp(argv[i], "-l")) {
430 NEXT_ARG(i);
431 l = atoi(argv[i]);
432 } else if (!strcmp(argv[i], "-a")) {
433 NEXT_ARG(i);
434 if (!strcasecmp(argv[i], "SHA1"))
435 algo = TA_SHA_SHA1;
436 else if (!strcasecmp(argv[i], "SHA224"))
437 algo = TA_SHA_SHA224;
438 else if (!strcasecmp(argv[i], "SHA256"))
439 algo = TA_SHA_SHA256;
440 else if (!strcasecmp(argv[i], "SHA384"))
441 algo = TA_SHA_SHA384;
442 else if (!strcasecmp(argv[i], "SHA512"))
443 algo = TA_SHA_SHA512;
444 else {
445 fprintf(stderr, "%s, invalid algorithm\n",
446 argv[0]);
447 usage(argv[0], algo, size, warmup, l, n);
448 return 1;
449 }
450 } else if (!strcmp(argv[i], "-n")) {
451 NEXT_ARG(i);
452 n = atoi(argv[i]);
453 } else if (!strcmp(argv[i], "-r")) {
454 random_in = 1;
455 } else if (!strcmp(argv[i], "-s")) {
456 NEXT_ARG(i);
457 size = atoi(argv[i]);
458 } else if (!strcmp(argv[i], "-u")) {
459 offset = 1;
460 } else if (!strcmp(argv[i], "-v")) {
461 verbosity++;
462 } else if (!strcmp(argv[i], "-w")) {
463 NEXT_ARG(i);
464 warmup = atoi(argv[i]);
465 } else {
466 fprintf(stderr, "%s: invalid argument: %s\n",
467 argv[0], argv[i]);
468 usage(argv[0], algo, size, warmup, l, n);
469 return 1;
470 }
471 }
472
473 sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
474
475 return 0;
476}