blob: 81eb2dd127b25682bf7e8fc3a3c3d61200d9b6c4 [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
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030028#include <fcntl.h>
29#include <math.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <strings.h>
Etienne Carriered0b3ee22017-05-03 18:20:59 +020035#include <sys/ioctl.h>
36#include <sys/mman.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <ta_aes_perf.h>
40#include <tee_client_api.h>
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030041#include <time.h>
42#include <unistd.h>
43
Igor Opaniukf9b7fd22016-09-16 16:22:34 +030044#include "crypto_common.h"
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030045
46/*
47 * TEE client stuff
48 */
49
50static TEEC_Context ctx;
51static TEEC_Session sess;
52/*
53 * in_shm and out_shm are both IN/OUT to support dynamically choosing
54 * in_place == 1 or in_place == 0.
55 */
56static TEEC_SharedMemory in_shm = {
57 .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
58};
59static TEEC_SharedMemory out_shm = {
60 .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT
61};
62
Etienne Carriere2f18cc42017-04-26 15:01:26 +020063static void errx(const char *msg, TEEC_Result res, uint32_t *orig)
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030064{
65 fprintf(stderr, "%s: 0x%08x", msg, res);
Etienne Carriere2f18cc42017-04-26 15:01:26 +020066 if (orig)
67 fprintf(stderr, " (orig=%d)", (int)*orig);
68 fprintf(stderr, "\n");
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030069 exit (1);
70}
71
Etienne Carriere2f18cc42017-04-26 15:01:26 +020072static void check_res(TEEC_Result res, const char *errmsg, uint32_t *orig)
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030073{
74 if (res != TEEC_SUCCESS)
Etienne Carriere2f18cc42017-04-26 15:01:26 +020075 errx(errmsg, res, orig);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030076}
77
78static void open_ta(void)
79{
80 TEEC_Result res;
81 TEEC_UUID uuid = TA_AES_PERF_UUID;
82 uint32_t err_origin;
83
84 res = TEEC_InitializeContext(NULL, &ctx);
Etienne Carriere2f18cc42017-04-26 15:01:26 +020085 check_res(res, "TEEC_InitializeContext", NULL);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030086
87 res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL,
88 NULL, &err_origin);
Etienne Carriere2f18cc42017-04-26 15:01:26 +020089 check_res(res, "TEEC_OpenSession", &err_origin);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +030090}
91
92/*
93 * Statistics
94 *
95 * We want to compute min, max, mean and standard deviation of processing time
96 */
97
98struct statistics {
99 int n;
100 double m;
101 double M2;
102 double min;
103 double max;
104 int initialized;
105};
106
107/* Take new sample into account (Knuth/Welford algorithm) */
108static void update_stats(struct statistics *s, uint64_t t)
109{
110 double x = (double)t;
111 double delta = x - s->m;
112
113 s->n++;
114 s->m += delta/s->n;
115 s->M2 += delta*(x - s->m);
116 if (!s->initialized) {
117 s->min = s->max = x;
118 s->initialized = 1;
119 } else {
120 if (s->min > x)
121 s->min = x;
122 if (s->max < x)
123 s->max = x;
124 }
125}
126
127static double stddev(struct statistics *s)
128{
129 if (s->n < 2)
130 return NAN;
131 return sqrt(s->M2/s->n);
132}
133
134static const char *mode_str(uint32_t mode)
135{
136 switch (mode) {
137 case TA_AES_ECB:
138 return "ECB";
139 case TA_AES_CBC:
140 return "CBC";
141 case TA_AES_CTR:
142 return "CTR";
143 case TA_AES_XTS:
144 return "XTS";
145 default:
146 return "???";
147 }
148}
149
150#define _TO_STR(x) #x
151#define TO_STR(x) _TO_STR(x)
152
153static void usage(const char *progname, int keysize, int mode,
154 size_t size, int warmup, unsigned int l, unsigned int n)
155{
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200156 fprintf(stderr, "Usage: %s [-h]\n", progname);
157 fprintf(stderr, "Usage: %s [-d] [-i] [-k SIZE]", progname);
158 fprintf(stderr, " [-l LOOP] [-m MODE] [-n LOOP] [-r] [-s SIZE]");
159 fprintf(stderr, " [-v [-v]] [-w SEC]\n");
160 fprintf(stderr, "AES performance testing tool for OP-TEE\n");
161 fprintf(stderr, "\n");
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300162 fprintf(stderr, "Options:\n");
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200163 fprintf(stderr, " -d Test AES decryption instead of encryption\n");
164 fprintf(stderr, " -h|--help Print this help and exit\n");
165 fprintf(stderr, " -i|--in-place Use same buffer for input and output (decrypt in place)\n");
166 fprintf(stderr, " -k SIZE Key size in bits: 128, 192 or 256 [%u]\n", keysize);
167 fprintf(stderr, " -l LOOP Inner loop iterations (TA calls TEE_CipherUpdate() <x> times) [%u]\n", l);
168 fprintf(stderr, " -m MODE AES mode: ECB, CBC, CTR, XTS [%s]\n", mode_str(mode));
169 fprintf(stderr, " -n LOOP Outer test loop iterations [%u]\n", n);
170 fprintf(stderr, " -r|--random Get input data from /dev/urandom (default: all-zeros)\n");
171 fprintf(stderr, " -s SIZE Test buffer size in bytes [%zu]\n", size);
172 fprintf(stderr, " -v Be verbose (use twice for greater effect)\n");
173 fprintf(stderr, " -w|--warmup SEC Warm-up time in seconds: execute a busy loop before\n");
174 fprintf(stderr, " the test to mitigate the effects of cpufreq etc. [%u]\n", warmup);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300175}
176
177static void alloc_shm(size_t sz, int in_place)
178{
179 TEEC_Result res;
180
181 in_shm.buffer = NULL;
182 in_shm.size = sz;
183 res = TEEC_AllocateSharedMemory(&ctx, &in_shm);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200184 check_res(res, "TEEC_AllocateSharedMemory", NULL);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300185
186 if (!in_place) {
187 out_shm.buffer = NULL;
188 out_shm.size = sz;
189 res = TEEC_AllocateSharedMemory(&ctx, &out_shm);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200190 check_res(res, "TEEC_AllocateSharedMemory", NULL);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300191 }
192}
193
194static void free_shm(void)
195{
196 TEEC_ReleaseSharedMemory(&in_shm);
197 TEEC_ReleaseSharedMemory(&out_shm);
198}
199
200static ssize_t read_random(void *in, size_t rsize)
201{
202 static int rnd;
203 ssize_t s;
204
205 if (!rnd) {
206 rnd = open("/dev/urandom", O_RDONLY);
207 if (rnd < 0) {
208 perror("open");
209 return 1;
210 }
211 }
212 s = read(rnd, in, rsize);
213 if (s < 0) {
214 perror("read");
215 return 1;
216 }
217 if ((size_t)s != rsize) {
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200218 printf("read: requested %zu bytes, got %zd\n", rsize, s);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300219 }
220
221 return 0;
222}
223
224static void get_current_time(struct timespec *ts)
225{
226 if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) {
227 perror("clock_gettime");
228 exit(1);
229 }
230}
231
232static uint64_t timespec_to_ns(struct timespec *ts)
233{
234 return ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec;
235}
236
237static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end)
238{
239 return timespec_to_ns(end) - timespec_to_ns(start);
240}
241
242static uint64_t run_test_once(void *in, size_t size, TEEC_Operation *op,
243 int random_in)
244{
245 struct timespec t0, t1;
246 TEEC_Result res;
247 uint32_t ret_origin;
248
249 if (random_in)
250 read_random(in, size);
251 get_current_time(&t0);
252 res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PROCESS, op,
253 &ret_origin);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200254 check_res(res, "TEEC_InvokeCommand", &ret_origin);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300255 get_current_time(&t1);
256
257 return timespec_diff_ns(&t0, &t1);
258}
259
260static void prepare_key(int decrypt, int keysize, int mode)
261{
262 TEEC_Result res;
263 uint32_t ret_origin;
264 TEEC_Operation op;
265
266 memset(&op, 0, sizeof(op));
267 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT,
268 TEEC_NONE, TEEC_NONE);
269 op.params[0].value.a = decrypt;
270 op.params[0].value.b = keysize;
271 op.params[1].value.a = mode;
272 res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PREPARE_KEY, &op,
273 &ret_origin);
Etienne Carriere2f18cc42017-04-26 15:01:26 +0200274 check_res(res, "TEEC_InvokeCommand", &ret_origin);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300275}
276
277static void do_warmup(int warmup)
278{
279 struct timespec t0, t;
280 int i;
281
282 get_current_time(&t0);
283 do {
284 for (i = 0; i < 100000; i++)
285 ;
286 get_current_time(&t);
287 } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000);
288}
289
290static const char *yesno(int v)
291{
292 return (v ? "yes" : "no");
293}
294
295static double mb_per_sec(size_t size, double usec)
296{
297 return (1000000000/usec)*((double)size/(1024*1024));
298}
299
300/* Encryption test: buffer of tsize byte. Run test n times. */
301void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size,
302 unsigned int n, unsigned int l, int random_in,
303 int in_place, int warmup, int verbosity)
304{
305 uint64_t t;
306 struct statistics stats;
307 struct timespec ts;
308 TEEC_Operation op;
309 int n0 = n;
Jerome Forissierd99fe972017-02-21 15:30:57 +0100310 double sd;
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300311
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100312 vverbose("aes-perf\n");
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300313 if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
314 perror("clock_getres");
315 return;
316 }
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200317 vverbose("Clock resolution is %lu ns\n",
318 ts.tv_sec * 1000000000 + ts.tv_nsec);
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300319
320 open_ta();
321 prepare_key(decrypt, keysize, mode);
322
323 memset(&stats, 0, sizeof(stats));
324
325 alloc_shm(size, in_place);
326
327 if (!random_in)
328 memset(in_shm.buffer, 0, size);
329
330 memset(&op, 0, sizeof(op));
331 /* Using INOUT to handle the case in_place == 1 */
332 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
333 TEEC_MEMREF_PARTIAL_INOUT,
334 TEEC_VALUE_INPUT, TEEC_NONE);
335 op.params[0].memref.parent = &in_shm;
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300336 op.params[0].memref.size = size;
337 op.params[1].memref.parent = in_place ? &in_shm : &out_shm;
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300338 op.params[1].memref.size = size;
339 op.params[2].value.a = l;
340
341 verbose("Starting test: %s, %scrypt, keysize=%u bits, size=%zu bytes, ",
342 mode_str(mode), (decrypt ? "de" : "en"), keysize, size);
343 verbose("random=%s, ", yesno(random_in));
344 verbose("in place=%s, ", yesno(in_place));
345 verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup);
346
347 if (warmup)
348 do_warmup(warmup);
349
350 while (n-- > 0) {
351 t = run_test_once(in_shm.buffer, size, &op, random_in);
352 update_stats(&stats, t);
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100353 if (n % (n0 / 10) == 0)
Igor Opaniuk44aff4b2016-09-16 10:18:00 +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 Opaniuk44aff4b2016-09-16 10:18:00 +0300365 free_shm();
366}
367
368#define NEXT_ARG(i) \
369 do { \
370 if (++i == argc) { \
371 fprintf(stderr, "%s: %s: missing argument\n", \
Etienne Carrierec92b55f2017-03-24 10:28:09 +0100372 argv[0], argv[i - 1]); \
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300373 return 1; \
374 } \
375 } while (0);
376
377int aes_perf_runner_cmd_parser(int argc, char *argv[])
378{
379 int i;
380
381 /*
382 * Command line parameters
383 */
384
385 size_t size = 1024; /* Buffer size (-s) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300386 unsigned int n = CRYPTO_DEF_COUNT; /*Number of measurements (-n)*/
387 unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */
388 int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300389 int decrypt = 0; /* Encrypt by default, -d to decrypt */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300390 int keysize = AES_128; /* AES key size (-k) */
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300391 int mode = TA_AES_ECB; /* AES mode (-m) */
Igor Opaniukf9b7fd22016-09-16 16:22:34 +0300392 /* Get input data from /dev/urandom (-r) */
393 int random_in = CRYPTO_USE_RANDOM;
394 /* Use same buffer for in and out (-i) */
395 int in_place = AES_PERF_INPLACE;
396 int warmup = CRYPTO_DEF_WARMUP; /* Start with a 2-second busy loop (-w) */
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300397
398 /* Parse command line */
399 for (i = 1; i < argc; i++) {
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200400 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300401 usage(argv[0], keysize, mode, size, warmup, l, n);
402 return 0;
403 }
404 }
405 for (i = 1; i < argc; i++) {
406 if (!strcmp(argv[i], "-d")) {
407 decrypt = 1;
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200408 } else if (!strcmp(argv[i], "--in-place") ||
409 !strcmp(argv[i], "-i")) {
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300410 in_place = 1;
411 } else if (!strcmp(argv[i], "-k")) {
412 NEXT_ARG(i);
413 keysize = atoi(argv[i]);
414 if (keysize != AES_128 && keysize != AES_192 &&
415 keysize != AES_256) {
416 fprintf(stderr, "%s: invalid key size\n",
417 argv[0]);
418 usage(argv[0], keysize, mode, size, warmup, l, n);
419 return 1;
420 }
421 } else if (!strcmp(argv[i], "-l")) {
422 NEXT_ARG(i);
423 l = atoi(argv[i]);
424 } else if (!strcmp(argv[i], "-m")) {
425 NEXT_ARG(i);
426 if (!strcasecmp(argv[i], "ECB"))
427 mode = TA_AES_ECB;
428 else if (!strcasecmp(argv[i], "CBC"))
429 mode = TA_AES_CBC;
430 else if (!strcasecmp(argv[i], "CTR"))
431 mode = TA_AES_CTR;
432 else if (!strcasecmp(argv[i], "XTS"))
433 mode = TA_AES_XTS;
434 else {
435 fprintf(stderr, "%s, invalid mode\n",
436 argv[0]);
437 usage(argv[0], keysize, mode, size, warmup, l, n);
438 return 1;
439 }
440 } else if (!strcmp(argv[i], "-n")) {
441 NEXT_ARG(i);
442 n = atoi(argv[i]);
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200443 } else if (!strcmp(argv[i], "--random") ||
444 !strcmp(argv[i], "-r")) {
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300445 random_in = 1;
446 } else if (!strcmp(argv[i], "-s")) {
447 NEXT_ARG(i);
448 size = atoi(argv[i]);
449 } else if (!strcmp(argv[i], "-v")) {
450 verbosity++;
Etienne Carriered0b3ee22017-05-03 18:20:59 +0200451 } else if (!strcmp(argv[i], "--warmup") ||
452 !strcmp(argv[i], "-w")) {
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300453 NEXT_ARG(i);
454 warmup = atoi(argv[i]);
455 } else {
456 fprintf(stderr, "%s: invalid argument: %s\n",
457 argv[0], argv[i]);
458 usage(argv[0], keysize, mode, size, warmup, l, n);
459 return 1;
460 }
461 }
462
Etienne Carriere057129b2017-04-26 15:03:36 +0200463 if (size & (16 - 1)) {
464 fprintf(stderr, "invalid buffer size argument, must be a multiple of 16\n\n");
465 usage(argv[0], keysize, mode, size, warmup, l, n);
466 return 1;
467 }
468
Igor Opaniuk44aff4b2016-09-16 10:18:00 +0300469 aes_perf_run_test(mode, keysize, decrypt, size, n, l, random_in,
470 in_place, warmup, verbosity);
471
472 return 0;
473}