blob: bb400384eb47424f308464239b406e2e1ca4be01 [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.
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100289 * Entry point for running SHA benchmark
290 * Params:
Igor Opaniuk136644a2016-09-13 13:40:56 +0300291 * 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
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100298 * verbosity - Verbosity level
Igor Opaniuk136644a2016-09-13 13:40:56 +0300299 * */
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100300extern void sha_perf_run_test(int algo, size_t size, unsigned int n,
301 unsigned int l, int random_in, int offset,
Igor Opaniuk136644a2016-09-13 13:40:56 +0300302 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;
Jerome Forissierd99fe972017-02-21 15:30:57 +0100309 double sd;
Igor Opaniuk136644a2016-09-13 13:40:56 +0300310
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100311 vverbose("sha-perf\n");
Igor Opaniuk136644a2016-09-13 13:40:56 +0300312 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
313 perror("clock_getres");
314 return;
315 }
316 vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 +
317 ts.tv_nsec);
318
319 open_ta();
320 prepare_op(algo);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100321
Igor Opaniuk136644a2016-09-13 13:40:56 +0300322 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);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100353 if (n % (n0 / 10) == 0)
Igor Opaniuk136644a2016-09-13 13:40:56 +0300354 vverbose("#");
355 }
356 vverbose("\n");
Jerome Forissierd99fe972017-02-21 15:30:57 +0100357 sd = stddev(&stats);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100358 printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n",
359 stats.min / 1000, stats.max / 1000, stats.m / 1000,
360 sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m));
361 verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n",
362 (stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000,
363 mb_per_sec(size, stats.m + 2 * sd),
364 mb_per_sec(size, stats.m - 2 * sd));
Igor Opaniuk136644a2016-09-13 13:40:56 +0300365 free_shm();
366}
367
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100368static void usage(const char *progname,
Igor Opaniuk136644a2016-09-13 13:40:56 +0300369 /* Default params */
370 int algo, size_t size, int warmup, int l, int n)
371{
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100372 fprintf(stderr, "SHA performance testing tool for OP-TEE\n\n");
Igor Opaniuk136644a2016-09-13 13:40:56 +0300373 fprintf(stderr, "Usage:\n");
374 fprintf(stderr, " %s -h\n", progname);
375 fprintf(stderr, " %s [-v] [-a algo] ", progname);
376 fprintf(stderr, "[-s bufsize] [-r] [-n loops] [-l iloops] ");
377 fprintf(stderr, "[-w warmup_time]\n");
378 fprintf(stderr, "Options:\n");
379 fprintf(stderr, " -h Print this help and exit\n");
380 fprintf(stderr, " -l Inner loop iterations (TA hashes ");
381 fprintf(stderr, "the buffer <x> times) [%u]\n", l);
382 fprintf(stderr, " -a Algorithm (SHA1, SHA224, SHA256, SHA384, ");
383 fprintf(stderr, "SHA512) [%s]\n", algo_str(algo));
384 fprintf(stderr, " -n Outer loop iterations [%u]\n", n);
385 fprintf(stderr, " -r Get input data from /dev/urandom ");
386 fprintf(stderr, "(otherwise use zero-filled buffer)\n");
387 fprintf(stderr, " -s Buffer size (process <x> bytes at a time) ");
388 fprintf(stderr, "[%zu]\n", size);
389 fprintf(stderr, " -u Use unaligned buffer (odd address)\n");
390 fprintf(stderr, " -v Be verbose (use twice for greater effect)\n");
391 fprintf(stderr, " -w Warm-up time in seconds: execute a busy ");
392 fprintf(stderr, "loop before the test\n");
393 fprintf(stderr, " to mitigate the effects of cpufreq etc. ");
394 fprintf(stderr, "[%u]\n", warmup);
395}
396
397#define NEXT_ARG(i) \
398 do { \
399 if (++i == argc) { \
400 fprintf(stderr, "%s: %s: missing argument\n", \
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100401 argv[0], argv[i - 1]); \
Igor Opaniuk136644a2016-09-13 13:40:56 +0300402 return 1; \
403 } \
404 } while (0);
405
406
407
408extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
409{
410 int i;
Igor Opaniuk136644a2016-09-13 13:40:56 +0300411 /* Command line params */
412 size_t size = 1024; /* Buffer size (-s) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300413 unsigned int n = CRYPTO_DEF_COUNT;/* Number of measurements (-n)*/
414 unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */
415 int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */
Igor Opaniuk136644a2016-09-13 13:40:56 +0300416 int algo = TA_SHA_SHA1; /* Algorithm (-a) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300417 /* Get input data from /dev/urandom (-r) */
418 int random_in = CRYPTO_USE_RANDOM;
419 /* Start with a 2-second busy loop (-w) */
420 int warmup = CRYPTO_DEF_WARMUP;
421 int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */
Igor Opaniuk136644a2016-09-13 13:40:56 +0300422
Igor Opaniuk136644a2016-09-13 13:40:56 +0300423 /* Parse command line */
424 for (i = 1; i < argc; i++) {
425 if (!strcmp(argv[i], "-h")) {
426 usage(argv[0], algo, size, warmup, l, n);
427 return 0;
428 }
429 }
430 for (i = 1; i < argc; i++) {
431 if (!strcmp(argv[i], "-l")) {
432 NEXT_ARG(i);
433 l = atoi(argv[i]);
434 } else if (!strcmp(argv[i], "-a")) {
435 NEXT_ARG(i);
436 if (!strcasecmp(argv[i], "SHA1"))
437 algo = TA_SHA_SHA1;
438 else if (!strcasecmp(argv[i], "SHA224"))
439 algo = TA_SHA_SHA224;
440 else if (!strcasecmp(argv[i], "SHA256"))
441 algo = TA_SHA_SHA256;
442 else if (!strcasecmp(argv[i], "SHA384"))
443 algo = TA_SHA_SHA384;
444 else if (!strcasecmp(argv[i], "SHA512"))
445 algo = TA_SHA_SHA512;
446 else {
447 fprintf(stderr, "%s, invalid algorithm\n",
448 argv[0]);
449 usage(argv[0], algo, size, warmup, l, n);
450 return 1;
451 }
452 } else if (!strcmp(argv[i], "-n")) {
453 NEXT_ARG(i);
454 n = atoi(argv[i]);
455 } else if (!strcmp(argv[i], "-r")) {
456 random_in = 1;
457 } else if (!strcmp(argv[i], "-s")) {
458 NEXT_ARG(i);
459 size = atoi(argv[i]);
460 } else if (!strcmp(argv[i], "-u")) {
461 offset = 1;
462 } else if (!strcmp(argv[i], "-v")) {
463 verbosity++;
464 } else if (!strcmp(argv[i], "-w")) {
465 NEXT_ARG(i);
466 warmup = atoi(argv[i]);
467 } else {
468 fprintf(stderr, "%s: invalid argument: %s\n",
469 argv[0], argv[i]);
470 usage(argv[0], algo, size, warmup, l, n);
471 return 1;
472 }
473 }
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100474
Igor Opaniuk136644a2016-09-13 13:40:56 +0300475 sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
476
477 return 0;
478}