blob: 85fa7cd3b16cf9647cdea216a5a3ab14ab35d137 [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
Etienne Carriered0b3ee22017-05-03 18:20:59 +020028#include <adbg.h>
Igor Opaniuk136644a2016-09-13 13:40:56 +030029#include <fcntl.h>
30#include <math.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <strings.h>
Etienne Carriered0b3ee22017-05-03 18:20:59 +020036#include <sys/types.h>
37#include <sys/stat.h>
38#include <tee_client_api.h>
Igor Opaniuk136644a2016-09-13 13:40:56 +030039#include <time.h>
40#include <unistd.h>
41
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
Etienne Carriere2f18cc42017-04-26 15:01:26 +020057static void errx(const char *msg, TEEC_Result res, uint32_t *orig)
Igor Opaniuk136644a2016-09-13 13:40:56 +030058{
59 fprintf(stderr, "%s: 0x%08x", msg, res);
Etienne Carriere2f18cc42017-04-26 15:01:26 +020060 if (orig)
61 fprintf(stderr, " (orig=%d)", (int)*orig);
62 fprintf(stderr, "\n");
Igor Opaniuk136644a2016-09-13 13:40:56 +030063 exit (1);
64}
65
Etienne Carriere2f18cc42017-04-26 15:01:26 +020066static void check_res(TEEC_Result res, const char *errmsg, uint32_t *orig)
Igor Opaniuk136644a2016-09-13 13:40:56 +030067{
68 if (res != TEEC_SUCCESS)
Etienne Carriere2f18cc42017-04-26 15:01:26 +020069 errx(errmsg, res, orig);
Igor Opaniuk136644a2016-09-13 13:40:56 +030070}
71
72static void open_ta(void)
73{
74 TEEC_Result res;
75 TEEC_UUID uuid = TA_SHA_PERF_UUID;
76 uint32_t err_origin;
77
78 res = TEEC_InitializeContext(NULL, &ctx);
Etienne Carriere2f18cc42017-04-26 15:01:26 +020079 check_res(res,"TEEC_InitializeContext", NULL);
Igor Opaniuk136644a2016-09-13 13:40:56 +030080
81 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
82 NULL, &err_origin);
Etienne Carriere2f18cc42017-04-26 15:01:26 +020083 check_res(res,"TEEC_OpenSession", &err_origin);
Igor Opaniuk136644a2016-09-13 13:40:56 +030084}
85
86/*
87 * Statistics
88 *
89 * We want to compute min, max, mean and standard deviation of processing time
90 */
91
92struct statistics {
93 int n;
94 double m;
95 double M2;
96 double min;
97 double max;
98 int initialized;
99};
100
101/* Take new sample into account (Knuth/Welford algorithm) */
102static void update_stats(struct statistics *s, uint64_t t)
103{
104 double x = (double)t;
105 double delta = x - s->m;
106
107 s->n++;
108 s->m += delta/s->n;
109 s->M2 += delta*(x - s->m);
110 if (!s->initialized) {
111 s->min = s->max = x;
112 s->initialized = 1;
113 } else {
114 if (s->min > x)
115 s->min = x;
116 if (s->max < x)
117 s->max = x;
118 }
119}
120
121static double stddev(struct statistics *s)
122{
123 if (s->n < 2)
124 return NAN;
125 return sqrt(s->M2/s->n);
126}
127
128static const char *algo_str(uint32_t algo)
129{
130 switch (algo) {
131 case TA_SHA_SHA1:
132 return "SHA1";
133 case TA_SHA_SHA224:
134 return "SHA224";
135 case TA_SHA_SHA256:
136 return "SHA256";
137 case TA_SHA_SHA384:
138 return "SHA384";
139 case TA_SHA_SHA512:
140 return "SHA512";
141 default:
142 return "???";
143 }
144}
145
146static int hash_size(uint32_t algo)
147{
148 switch (algo) {
149 case TA_SHA_SHA1:
150 return 20;
151 case TA_SHA_SHA224:
152 return 28;
153 case TA_SHA_SHA256:
154 return 32;
155 case TA_SHA_SHA384:
156 return 48;
157 case TA_SHA_SHA512:
158 return 64;
159 default:
160 return 0;
161 }
162}
163
164#define _TO_STR(x) #x
165#define TO_STR(x) _TO_STR(x)
166
167
168static void alloc_shm(size_t sz, uint32_t algo, int offset)
169{
170 TEEC_Result res;
171
172 in_shm.buffer = NULL;
173 in_shm.size = sz + offset;
174 res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200175 check_res(res, "TEEC_AllocateSharedMemory", NULL);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300176
177 out_shm.buffer = NULL;
178 out_shm.size = hash_size(algo);
179 res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200180 check_res(res, "TEEC_AllocateSharedMemory", NULL);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300181}
182
183static void free_shm(void)
184{
185 TEEC_ReleaseSharedMemory(&in_shm);
186 TEEC_ReleaseSharedMemory(&out_shm);
187}
188
189static ssize_t read_random(void *in, size_t rsize)
190{
191 static int rnd;
192 ssize_t s;
193
194 if (!rnd) {
195 rnd = open("/dev/urandom", O_RDONLY);
196 if (rnd < 0) {
197 perror("open");
198 return 1;
199 }
200 }
201 s = read(rnd, in, rsize);
202 if (s < 0) {
203 perror("read");
204 return 1;
205 }
206 if ((size_t)s != rsize) {
207 printf("read: requested %zu bytes, got %zd\n",
208 rsize, s);
209 }
210 return 0;
211}
212
213static long get_current_time(struct timespec *ts)
214{
215 if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
216 perror("clock_gettime");
217 exit(1);
218 }
219 return 0;
220}
221
222static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
223{
224 uint64_t ns = 0;
225
226 if (end->tv_nsec < start->tv_nsec) {
227 ns += 1000000000 * (end->tv_sec - start->tv_sec - 1);
228 ns += 1000000000 - start->tv_nsec + end->tv_nsec;
229 } else {
230 ns += 1000000000 * (end->tv_sec - start->tv_sec);
231 ns += end->tv_nsec - start->tv_nsec;
232 }
233 return ns;
234}
235
236static uint64_t run_test_once(void *in, size_t size, int random_in, TEEC_Operation *op)
237{
238 struct timespec t0, t1;
239 TEEC_Result res;
240 uint32_t ret_origin;
241
242 if (random_in)
243 read_random(in, size);
244 get_current_time(&t0);
245 res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op,
246 &ret_origin);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200247 check_res(res, "TEEC_InvokeCommand", &ret_origin);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300248 get_current_time(&t1);
249
250 return timespec_diff_ns(&t0, &t1);
251}
252
253static void prepare_op(int algo)
254{
255 TEEC_Result res;
256 uint32_t ret_origin;
257 TEEC_Operation op;
258
259 memset(&op, 0, sizeof(op));
260 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE,
261 TEEC_NONE, TEEC_NONE);
262 op.params[0].value.a = algo;
263 res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PREPARE_OP, &op,
264 &ret_origin);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200265 check_res(res, "TEEC_InvokeCommand", &ret_origin);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300266}
267
268static void do_warmup(int warmup)
269{
270 struct timespec t0, t;
271 int i;
272
273 get_current_time(&t0);
274 do {
275 for (i = 0; i < 100000; i++)
276 ;
277 get_current_time(&t);
278 } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
279}
280
281static const char *yesno(int v)
282{
283 return (v ? "yes" : "no");
284}
285
286static double mb_per_sec(size_t size, double usec)
287{
288 return (1000000000/usec)*((double)size/(1024*1024));
289}
290
291/* Hash test: buffer of size byte. Run test n times.
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100292 * Entry point for running SHA benchmark
293 * Params:
Igor Opaniuk136644a2016-09-13 13:40:56 +0300294 * algo - Algorithm
295 * size - Buffer size
296 * n - Number of measurements
297 * l - Amount of inner loops
298 * random_in - Get input from /dev/urandom
299 * offset - Buffer offset wrt. alloc-ed address
300 * warmup - Start with a-second busy loop
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100301 * verbosity - Verbosity level
Igor Opaniuk136644a2016-09-13 13:40:56 +0300302 * */
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100303extern void sha_perf_run_test(int algo, size_t size, unsigned int n,
304 unsigned int l, int random_in, int offset,
Igor Opaniuk136644a2016-09-13 13:40:56 +0300305 int warmup, int verbosity)
306{
307 uint64_t t;
308 struct statistics stats;
309 TEEC_Operation op;
310 int n0 = n;
311 struct timespec ts;
Jerome Forissierd99fe972017-02-21 15:30:57 +0100312 double sd;
Igor Opaniuk136644a2016-09-13 13:40:56 +0300313
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100314 vverbose("sha-perf\n");
Igor Opaniuk136644a2016-09-13 13:40:56 +0300315 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
316 perror("clock_getres");
317 return;
318 }
319 vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 +
320 ts.tv_nsec);
321
322 open_ta();
323 prepare_op(algo);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100324
Igor Opaniuk136644a2016-09-13 13:40:56 +0300325 alloc_shm(size, algo, offset);
326
327 if (!random_in)
328 memset((uint8_t *)in_shm.buffer + offset, 0, size);
329
330 memset(&op, 0, sizeof(op));
331 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
332 TEEC_MEMREF_PARTIAL_OUTPUT,
333 TEEC_VALUE_INPUT, TEEC_NONE);
334 op.params[0].memref.parent = &in_shm;
335 op.params[0].memref.offset = 0;
336 op.params[0].memref.size = size + offset;
337 op.params[1].memref.parent = &out_shm;
338 op.params[1].memref.offset = 0;
339 op.params[1].memref.size = hash_size(algo);
340 op.params[2].value.a = l;
341 op.params[2].value.b = offset;
342
343 verbose("Starting test: %s, size=%zu bytes, ",
344 algo_str(algo), size);
345 verbose("random=%s, ", yesno(random_in));
346 verbose("unaligned=%s, ", yesno(offset));
347 verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
348
349 if (warmup)
350 do_warmup(warmup);
351
352 memset(&stats, 0, sizeof(stats));
353 while (n-- > 0) {
354 t = run_test_once((uint8_t *)in_shm.buffer + offset, size, random_in, &op);
355 update_stats(&stats, t);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100356 if (n % (n0 / 10) == 0)
Igor Opaniuk136644a2016-09-13 13:40:56 +0300357 vverbose("#");
358 }
359 vverbose("\n");
Jerome Forissierd99fe972017-02-21 15:30:57 +0100360 sd = stddev(&stats);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100361 printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n",
362 stats.min / 1000, stats.max / 1000, stats.m / 1000,
363 sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m));
364 verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n",
365 (stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000,
366 mb_per_sec(size, stats.m + 2 * sd),
367 mb_per_sec(size, stats.m - 2 * sd));
Igor Opaniuk136644a2016-09-13 13:40:56 +0300368 free_shm();
369}
370
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100371static void usage(const char *progname,
Igor Opaniuk136644a2016-09-13 13:40:56 +0300372 /* Default params */
373 int algo, size_t size, int warmup, int l, int n)
374{
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200375 fprintf(stderr, "Usage: %s [-h]\n", progname);
376 fprintf(stderr, "Usage: %s [-a ALGO] [-l LOOP] [-n LOOP] [-r] [-s SIZE]", progname);
377 fprintf(stderr, " [-v [-v]] [-w SEC]\n");
378 fprintf(stderr, "SHA performance testing tool for OP-TEE\n");
379 fprintf(stderr, "\n");
Igor Opaniuk136644a2016-09-13 13:40:56 +0300380 fprintf(stderr, "Options:\n");
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200381 fprintf(stderr, " -a ALGO Algorithm (SHA1, SHA224, SHA256, SHA384, SHA512) [%s]\n", algo_str(algo));
382 fprintf(stderr, " -h|--help Print this help and exit\n");
383 fprintf(stderr, " -l LOOP Inner loop iterations (TA calls TEE_DigestDoFinal() <x> times) [%u]\n", l);
384 fprintf(stderr, " -n LOOP Outer test loop iterations [%u]\n", n);
385 fprintf(stderr, " -r|--random Get input data from /dev/urandom (default: all-zeros)\n");
386 fprintf(stderr, " -s SIZE Test buffer size in bytes [%zu]\n", size);
387 fprintf(stderr, " -u|--unalign Use unaligned buffer (odd address)\n");
388 fprintf(stderr, " -v Be verbose (use twice for greater effect)\n");
389 fprintf(stderr, " -w|--warmup SEC Warm-up time in seconds: execute a busy loop before\n");
390 fprintf(stderr, " the test to mitigate the effects of cpufreq etc. [%u]\n", warmup);
Igor Opaniuk136644a2016-09-13 13:40:56 +0300391}
392
393#define NEXT_ARG(i) \
394 do { \
395 if (++i == argc) { \
396 fprintf(stderr, "%s: %s: missing argument\n", \
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100397 argv[0], argv[i - 1]); \
Igor Opaniuk136644a2016-09-13 13:40:56 +0300398 return 1; \
399 } \
400 } while (0);
401
402
403
404extern int sha_perf_runner_cmd_parser(int argc, char *argv[])
405{
406 int i;
Igor Opaniuk136644a2016-09-13 13:40:56 +0300407 /* Command line params */
408 size_t size = 1024; /* Buffer size (-s) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300409 unsigned int n = CRYPTO_DEF_COUNT;/* Number of measurements (-n)*/
410 unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */
411 int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */
Igor Opaniuk136644a2016-09-13 13:40:56 +0300412 int algo = TA_SHA_SHA1; /* Algorithm (-a) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300413 /* Get input data from /dev/urandom (-r) */
414 int random_in = CRYPTO_USE_RANDOM;
415 /* Start with a 2-second busy loop (-w) */
416 int warmup = CRYPTO_DEF_WARMUP;
417 int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */
Igor Opaniuk136644a2016-09-13 13:40:56 +0300418
Igor Opaniuk136644a2016-09-13 13:40:56 +0300419 /* Parse command line */
420 for (i = 1; i < argc; i++) {
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200421 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
Igor Opaniuk136644a2016-09-13 13:40:56 +0300422 usage(argv[0], algo, size, warmup, l, n);
423 return 0;
424 }
425 }
426 for (i = 1; i < argc; i++) {
427 if (!strcmp(argv[i], "-l")) {
428 NEXT_ARG(i);
429 l = atoi(argv[i]);
430 } else if (!strcmp(argv[i], "-a")) {
431 NEXT_ARG(i);
432 if (!strcasecmp(argv[i], "SHA1"))
433 algo = TA_SHA_SHA1;
434 else if (!strcasecmp(argv[i], "SHA224"))
435 algo = TA_SHA_SHA224;
436 else if (!strcasecmp(argv[i], "SHA256"))
437 algo = TA_SHA_SHA256;
438 else if (!strcasecmp(argv[i], "SHA384"))
439 algo = TA_SHA_SHA384;
440 else if (!strcasecmp(argv[i], "SHA512"))
441 algo = TA_SHA_SHA512;
442 else {
443 fprintf(stderr, "%s, invalid algorithm\n",
444 argv[0]);
445 usage(argv[0], algo, size, warmup, l, n);
446 return 1;
447 }
448 } else if (!strcmp(argv[i], "-n")) {
449 NEXT_ARG(i);
450 n = atoi(argv[i]);
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200451 } else if (!strcmp(argv[i], "--random") ||
452 !strcmp(argv[i], "-r")) {
Igor Opaniuk136644a2016-09-13 13:40:56 +0300453 random_in = 1;
454 } else if (!strcmp(argv[i], "-s")) {
455 NEXT_ARG(i);
456 size = atoi(argv[i]);
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200457 } else if (!strcmp(argv[i], "--unalign") ||
458 !strcmp(argv[i], "-u")) {
Igor Opaniuk136644a2016-09-13 13:40:56 +0300459 offset = 1;
460 } else if (!strcmp(argv[i], "-v")) {
461 verbosity++;
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200462 } else if (!strcmp(argv[i], "--warmup") ||
463 !strcmp(argv[i], "-w")) {
Igor Opaniuk136644a2016-09-13 13:40:56 +0300464 NEXT_ARG(i);
465 warmup = atoi(argv[i]);
466 } else {
467 fprintf(stderr, "%s: invalid argument: %s\n",
468 argv[0], argv[i]);
469 usage(argv[0], algo, size, warmup, l, n);
470 return 1;
471 }
472 }
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100473
Igor Opaniuk136644a2016-09-13 13:40:56 +0300474 sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity);
475
476 return 0;
477}