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