blob: 44bdbce58da290cca4155f8e94e8b88d13a83518 [file] [log] [blame]
Igor Opaniuk44aff4b2016-09-16 10:18:00 +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 <tee_client_api.h>
41#include "ta_aes_perf.h"
42#include "aes_perf.h"
43
44#define _verbose(lvl, ...) \
45 do { \
46 if (verbosity >= lvl) { \
47 printf(__VA_ARGS__); \
48 fflush(stdout); \
49 } \
50 } while (0)
51
52#define verbose(...) _verbose(1, __VA_ARGS__)
53#define vverbose(...) _verbose(2, __VA_ARGS__)
54
55/*
56 * TEE client stuff
57 */
58
59static TEEC_Context ctx;
60static TEEC_Session sess;
61/*
62 * in_shm and out_shm are both IN/OUT to support dynamically choosing
63 * in_place == 1 or in_place == 0.
64 */
65static TEEC_SharedMemory in_shm = {
66 .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
67};
68static TEEC_SharedMemory out_shm = {
69 .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
70};
71
72static void errx(const char *msg, TEEC_Result res)
73{
74 fprintf(stderr, "%s: 0x%08x", msg, res);
75 exit (1);
76}
77
78static void check_res(TEEC_Result res, const char *errmsg)
79{
80 if (res != TEEC_SUCCESS)
81 errx(errmsg, res);
82}
83
84static void open_ta(void)
85{
86 TEEC_Result res;
87 TEEC_UUID uuid = TA_AES_PERF_UUID;
88 uint32_t err_origin;
89
90 res = TEEC_InitializeContext(NULL, &ctx);
91 check_res(res,"TEEC_InitializeContext");
92
93 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
94 NULL, &err_origin);
95 check_res(res,"TEEC_OpenSession");
96}
97
98/*
99 * Statistics
100 *
101 * We want to compute min, max, mean and standard deviation of processing time
102 */
103
104struct statistics {
105 int n;
106 double m;
107 double M2;
108 double min;
109 double max;
110 int initialized;
111};
112
113/* Take new sample into account (Knuth/Welford algorithm) */
114static void update_stats(struct statistics *s, uint64_t t)
115{
116 double x = (double)t;
117 double delta = x - s->m;
118
119 s->n++;
120 s->m += delta/s->n;
121 s->M2 += delta*(x - s->m);
122 if (!s->initialized) {
123 s->min = s->max = x;
124 s->initialized = 1;
125 } else {
126 if (s->min > x)
127 s->min = x;
128 if (s->max < x)
129 s->max = x;
130 }
131}
132
133static double stddev(struct statistics *s)
134{
135 if (s->n < 2)
136 return NAN;
137 return sqrt(s->M2/s->n);
138}
139
140static const char *mode_str(uint32_t mode)
141{
142 switch (mode) {
143 case TA_AES_ECB:
144 return "ECB";
145 case TA_AES_CBC:
146 return "CBC";
147 case TA_AES_CTR:
148 return "CTR";
149 case TA_AES_XTS:
150 return "XTS";
151 default:
152 return "???";
153 }
154}
155
156#define _TO_STR(x) #x
157#define TO_STR(x) _TO_STR(x)
158
159static void usage(const char *progname, int keysize, int mode,
160 size_t size, int warmup, unsigned int l, unsigned int n)
161{
162 fprintf(stderr, "AES performance testing tool for OP-TEE (%s)\n\n",
163 TO_STR(VERSION));
164 fprintf(stderr, "Usage:\n");
165 fprintf(stderr, " %s -h\n", progname);
166 fprintf(stderr, " %s [-v] [-m mode] [-k keysize] ", progname);
167 fprintf(stderr, "[-s bufsize] [-r] [-i] [-n loops] [-l iloops] \n");
168 fprintf(stderr, "[-w warmup_time]\n");
169 fprintf(stderr, "Options:\n");
170 fprintf(stderr, " -h Print this help and exit\n");
171 fprintf(stderr, " -i Use same buffer for input and output (in ");
172 fprintf(stderr, "place)\n");
173 fprintf(stderr, " -k Key size in bits: 128, 192 or 256 [%u]\n",
174 keysize);
175 fprintf(stderr, " -l Inner loop iterations (TA calls ");
176 fprintf(stderr, "TEE_CipherUpdate() <x> times) [%u]\n", l);
177 fprintf(stderr, " -m AES mode: ECB, CBC, CTR, XTS [%s]\n",
178 mode_str(mode));
179 fprintf(stderr, " -n Outer loop iterations [%u]\n", n);
180 fprintf(stderr, " -r Get input data from /dev/urandom ");
181 fprintf(stderr, "(otherwise use zero-filled buffer)\n");
182 fprintf(stderr, " -s Buffer size (process <x> bytes at a time) ");
183 fprintf(stderr, "[%zu]\n", size);
184 fprintf(stderr, " -v Be verbose (use twice for greater effect)\n");
185 fprintf(stderr, " -w Warm-up time in seconds: execute a busy ");
186 fprintf(stderr, "loop before the test\n");
187 fprintf(stderr, " to mitigate the effects of cpufreq etc. ");
188 fprintf(stderr, "[%u]\n", warmup);
189}
190
191static void alloc_shm(size_t sz, int in_place)
192{
193 TEEC_Result res;
194
195 in_shm.buffer = NULL;
196 in_shm.size = sz;
197 res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
198 check_res(res, "TEEC_AllocateSharedMemory");
199
200 if (!in_place) {
201 out_shm.buffer = NULL;
202 out_shm.size = sz;
203 res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
204 check_res(res, "TEEC_AllocateSharedMemory");
205 }
206}
207
208static void free_shm(void)
209{
210 TEEC_ReleaseSharedMemory(&in_shm);
211 TEEC_ReleaseSharedMemory(&out_shm);
212}
213
214static ssize_t read_random(void *in, size_t rsize)
215{
216 static int rnd;
217 ssize_t s;
218
219 if (!rnd) {
220 rnd = open("/dev/urandom", O_RDONLY);
221 if (rnd < 0) {
222 perror("open");
223 return 1;
224 }
225 }
226 s = read(rnd, in, rsize);
227 if (s < 0) {
228 perror("read");
229 return 1;
230 }
231 if ((size_t)s != rsize) {
232 printf("read: requested %zu bytes, got %zd\n",
233 rsize, s);
234 }
235
236 return 0;
237}
238
239static void get_current_time(struct timespec *ts)
240{
241 if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
242 perror("clock_gettime");
243 exit(1);
244 }
245}
246
247static uint64_t timespec_to_ns(struct timespec *ts)
248{
249 return ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec;
250}
251
252static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
253{
254 return timespec_to_ns(end) - timespec_to_ns(start);
255}
256
257static uint64_t run_test_once(void *in, size_t size, TEEC_Operation *op,
258 int random_in)
259{
260 struct timespec t0, t1;
261 TEEC_Result res;
262 uint32_t ret_origin;
263
264 if (random_in)
265 read_random(in, size);
266 get_current_time(&t0);
267 res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PROCESS, op,
268 &ret_origin);
269 check_res(res, "TEEC_InvokeCommand");
270 get_current_time(&t1);
271
272 return timespec_diff_ns(&t0, &t1);
273}
274
275static void prepare_key(int decrypt, int keysize, int mode)
276{
277 TEEC_Result res;
278 uint32_t ret_origin;
279 TEEC_Operation op;
280
281 memset(&op, 0, sizeof(op));
282 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
283 TEEC_NONE, TEEC_NONE);
284 op.params[0].value.a = decrypt;
285 op.params[0].value.b = keysize;
286 op.params[1].value.a = mode;
287 res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PREPARE_KEY, &op,
288 &ret_origin);
289 check_res(res, "TEEC_InvokeCommand");
290}
291
292static void do_warmup(int warmup)
293{
294 struct timespec t0, t;
295 int i;
296
297 get_current_time(&t0);
298 do {
299 for (i = 0; i < 100000; i++)
300 ;
301 get_current_time(&t);
302 } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
303}
304
305static const char *yesno(int v)
306{
307 return (v ? "yes" : "no");
308}
309
310static double mb_per_sec(size_t size, double usec)
311{
312 return (1000000000/usec)*((double)size/(1024*1024));
313}
314
315/* Encryption test: buffer of tsize byte. Run test n times. */
316void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size,
317 unsigned int n, unsigned int l, int random_in,
318 int in_place, int warmup, int verbosity)
319{
320 uint64_t t;
321 struct statistics stats;
322 struct timespec ts;
323 TEEC_Operation op;
324 int n0 = n;
325
326 vverbose("aes-perf version %s\n", TO_STR(VERSION));
327 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
328 perror("clock_getres");
329 return;
330 }
331 vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 +
332 ts.tv_nsec);
333
334 open_ta();
335 prepare_key(decrypt, keysize, mode);
336
337 memset(&stats, 0, sizeof(stats));
338
339 alloc_shm(size, in_place);
340
341 if (!random_in)
342 memset(in_shm.buffer, 0, size);
343
344 memset(&op, 0, sizeof(op));
345 /* Using INOUT to handle the case in_place == 1 */
346 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
347 TEEC_MEMREF_PARTIAL_INOUT,
348 TEEC_VALUE_INPUT, TEEC_NONE);
349 op.params[0].memref.parent = &in_shm;
350 op.params[0].memref.offset = 0;
351 op.params[0].memref.size = size;
352 op.params[1].memref.parent = in_place ? &in_shm : &out_shm;
353 op.params[1].memref.offset = 0;
354 op.params[1].memref.size = size;
355 op.params[2].value.a = l;
356
357 verbose("Starting test: %s, %scrypt, keysize=%u bits, size=%zu bytes, ",
358 mode_str(mode), (decrypt ? "de" : "en"), keysize, size);
359 verbose("random=%s, ", yesno(random_in));
360 verbose("in place=%s, ", yesno(in_place));
361 verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
362
363 if (warmup)
364 do_warmup(warmup);
365
366 while (n-- > 0) {
367 t = run_test_once(in_shm.buffer, size, &op, random_in);
368 update_stats(&stats, t);
369 if (n % (n0/10) == 0)
370 vverbose("#");
371 }
372 vverbose("\n");
373 printf("min=%gμs max=%gμs mean=%gμs stddev=%gμs (%gMiB/s)\n",
374 stats.min/1000, stats.max/1000, stats.m/1000,
375 stddev(&stats)/1000, mb_per_sec(size, stats.m));
376 free_shm();
377}
378
379#define NEXT_ARG(i) \
380 do { \
381 if (++i == argc) { \
382 fprintf(stderr, "%s: %s: missing argument\n", \
383 argv[0], argv[i-1]); \
384 return 1; \
385 } \
386 } while (0);
387
388int aes_perf_runner_cmd_parser(int argc, char *argv[])
389{
390 int i;
391
392 /*
393 * Command line parameters
394 */
395
396 size_t size = 1024; /* Buffer size (-s) */
397 unsigned int n = 5000; /* Number of measurements (-n) */
398 unsigned int l = 1; /* Inner loops (-l) */
399 int verbosity = 0; /* Verbosity (-v) */
400 int decrypt = 0; /* Encrypt by default, -d to decrypt */
401 int keysize = 128; /* AES key size (-k) */
402 int mode = TA_AES_ECB; /* AES mode (-m) */
403 int random_in = 0; /* Get input data from /dev/urandom (-r) */
404 int in_place = 0; /* 1: use same buffer for in and out (-i) */
405 int warmup = 2; /* Start with a 2-second busy loop (-w) */
406
407 /* Parse command line */
408 for (i = 1; i < argc; i++) {
409 if (!strcmp(argv[i], "-h")) {
410 usage(argv[0], keysize, mode, size, warmup, l, n);
411 return 0;
412 }
413 }
414 for (i = 1; i < argc; i++) {
415 if (!strcmp(argv[i], "-d")) {
416 decrypt = 1;
417 } else if (!strcmp(argv[i], "-i")) {
418 in_place = 1;
419 } else if (!strcmp(argv[i], "-k")) {
420 NEXT_ARG(i);
421 keysize = atoi(argv[i]);
422 if (keysize != AES_128 && keysize != AES_192 &&
423 keysize != AES_256) {
424 fprintf(stderr, "%s: invalid key size\n",
425 argv[0]);
426 usage(argv[0], keysize, mode, size, warmup, l, n);
427 return 1;
428 }
429 } else if (!strcmp(argv[i], "-l")) {
430 NEXT_ARG(i);
431 l = atoi(argv[i]);
432 } else if (!strcmp(argv[i], "-m")) {
433 NEXT_ARG(i);
434 if (!strcasecmp(argv[i], "ECB"))
435 mode = TA_AES_ECB;
436 else if (!strcasecmp(argv[i], "CBC"))
437 mode = TA_AES_CBC;
438 else if (!strcasecmp(argv[i], "CTR"))
439 mode = TA_AES_CTR;
440 else if (!strcasecmp(argv[i], "XTS"))
441 mode = TA_AES_XTS;
442 else {
443 fprintf(stderr, "%s, invalid mode\n",
444 argv[0]);
445 usage(argv[0], keysize, mode, size, warmup, l, n);
446 return 1;
447 }
448 } else if (!strcmp(argv[i], "-n")) {
449 NEXT_ARG(i);
450 n = atoi(argv[i]);
451 } else if (!strcmp(argv[i], "-r")) {
452 random_in = 1;
453 } else if (!strcmp(argv[i], "-s")) {
454 NEXT_ARG(i);
455 size = atoi(argv[i]);
456 } else if (!strcmp(argv[i], "-v")) {
457 verbosity++;
458 } else if (!strcmp(argv[i], "-w")) {
459 NEXT_ARG(i);
460 warmup = atoi(argv[i]);
461 } else {
462 fprintf(stderr, "%s: invalid argument: %s\n",
463 argv[0], argv[i]);
464 usage(argv[0], keysize, mode, size, warmup, l, n);
465 return 1;
466 }
467 }
468
469
470 aes_perf_run_test(mode, keysize, decrypt, size, n, l, random_in,
471 in_place, warmup, verbosity);
472
473 return 0;
474}