Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 1 | /* |
| 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 Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame^] | 42 | #include "crypto_common.h" |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * TEE client stuff |
| 46 | */ |
| 47 | |
| 48 | static TEEC_Context ctx; |
| 49 | static 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 | */ |
| 54 | static TEEC_SharedMemory in_shm = { |
| 55 | .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT |
| 56 | }; |
| 57 | static TEEC_SharedMemory out_shm = { |
| 58 | .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT |
| 59 | }; |
| 60 | |
| 61 | static void errx(const char *msg, TEEC_Result res) |
| 62 | { |
| 63 | fprintf(stderr, "%s: 0x%08x", msg, res); |
| 64 | exit (1); |
| 65 | } |
| 66 | |
| 67 | static void check_res(TEEC_Result res, const char *errmsg) |
| 68 | { |
| 69 | if (res != TEEC_SUCCESS) |
| 70 | errx(errmsg, res); |
| 71 | } |
| 72 | |
| 73 | static 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 | |
| 93 | struct 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) */ |
| 103 | static 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 | |
| 122 | static double stddev(struct statistics *s) |
| 123 | { |
| 124 | if (s->n < 2) |
| 125 | return NAN; |
| 126 | return sqrt(s->M2/s->n); |
| 127 | } |
| 128 | |
| 129 | static 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 | |
| 148 | static void usage(const char *progname, int keysize, int mode, |
| 149 | size_t size, int warmup, unsigned int l, unsigned int n) |
| 150 | { |
| 151 | fprintf(stderr, "AES performance testing tool for OP-TEE (%s)\n\n", |
| 152 | TO_STR(VERSION)); |
| 153 | fprintf(stderr, "Usage:\n"); |
| 154 | fprintf(stderr, " %s -h\n", progname); |
| 155 | fprintf(stderr, " %s [-v] [-m mode] [-k keysize] ", progname); |
| 156 | fprintf(stderr, "[-s bufsize] [-r] [-i] [-n loops] [-l iloops] \n"); |
| 157 | fprintf(stderr, "[-w warmup_time]\n"); |
| 158 | fprintf(stderr, "Options:\n"); |
| 159 | fprintf(stderr, " -h Print this help and exit\n"); |
| 160 | fprintf(stderr, " -i Use same buffer for input and output (in "); |
| 161 | fprintf(stderr, "place)\n"); |
| 162 | fprintf(stderr, " -k Key size in bits: 128, 192 or 256 [%u]\n", |
| 163 | keysize); |
| 164 | fprintf(stderr, " -l Inner loop iterations (TA calls "); |
| 165 | fprintf(stderr, "TEE_CipherUpdate() <x> times) [%u]\n", l); |
| 166 | fprintf(stderr, " -m AES mode: ECB, CBC, CTR, XTS [%s]\n", |
| 167 | mode_str(mode)); |
| 168 | fprintf(stderr, " -n Outer loop iterations [%u]\n", n); |
| 169 | fprintf(stderr, " -r Get input data from /dev/urandom "); |
| 170 | fprintf(stderr, "(otherwise use zero-filled buffer)\n"); |
| 171 | fprintf(stderr, " -s Buffer size (process <x> bytes at a time) "); |
| 172 | fprintf(stderr, "[%zu]\n", size); |
| 173 | fprintf(stderr, " -v Be verbose (use twice for greater effect)\n"); |
| 174 | fprintf(stderr, " -w Warm-up time in seconds: execute a busy "); |
| 175 | fprintf(stderr, "loop before the test\n"); |
| 176 | fprintf(stderr, " to mitigate the effects of cpufreq etc. "); |
| 177 | fprintf(stderr, "[%u]\n", warmup); |
| 178 | } |
| 179 | |
| 180 | static void alloc_shm(size_t sz, int in_place) |
| 181 | { |
| 182 | TEEC_Result res; |
| 183 | |
| 184 | in_shm.buffer = NULL; |
| 185 | in_shm.size = sz; |
| 186 | res = TEEC_AllocateSharedMemory(&ctx, &in_shm); |
| 187 | check_res(res, "TEEC_AllocateSharedMemory"); |
| 188 | |
| 189 | if (!in_place) { |
| 190 | out_shm.buffer = NULL; |
| 191 | out_shm.size = sz; |
| 192 | res = TEEC_AllocateSharedMemory(&ctx, &out_shm); |
| 193 | check_res(res, "TEEC_AllocateSharedMemory"); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void free_shm(void) |
| 198 | { |
| 199 | TEEC_ReleaseSharedMemory(&in_shm); |
| 200 | TEEC_ReleaseSharedMemory(&out_shm); |
| 201 | } |
| 202 | |
| 203 | static ssize_t read_random(void *in, size_t rsize) |
| 204 | { |
| 205 | static int rnd; |
| 206 | ssize_t s; |
| 207 | |
| 208 | if (!rnd) { |
| 209 | rnd = open("/dev/urandom", O_RDONLY); |
| 210 | if (rnd < 0) { |
| 211 | perror("open"); |
| 212 | return 1; |
| 213 | } |
| 214 | } |
| 215 | s = read(rnd, in, rsize); |
| 216 | if (s < 0) { |
| 217 | perror("read"); |
| 218 | return 1; |
| 219 | } |
| 220 | if ((size_t)s != rsize) { |
| 221 | printf("read: requested %zu bytes, got %zd\n", |
| 222 | rsize, s); |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static void get_current_time(struct timespec *ts) |
| 229 | { |
| 230 | if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) { |
| 231 | perror("clock_gettime"); |
| 232 | exit(1); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static uint64_t timespec_to_ns(struct timespec *ts) |
| 237 | { |
| 238 | return ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec; |
| 239 | } |
| 240 | |
| 241 | static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end) |
| 242 | { |
| 243 | return timespec_to_ns(end) - timespec_to_ns(start); |
| 244 | } |
| 245 | |
| 246 | static uint64_t run_test_once(void *in, size_t size, TEEC_Operation *op, |
| 247 | int random_in) |
| 248 | { |
| 249 | struct timespec t0, t1; |
| 250 | TEEC_Result res; |
| 251 | uint32_t ret_origin; |
| 252 | |
| 253 | if (random_in) |
| 254 | read_random(in, size); |
| 255 | get_current_time(&t0); |
| 256 | res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PROCESS, op, |
| 257 | &ret_origin); |
| 258 | check_res(res, "TEEC_InvokeCommand"); |
| 259 | get_current_time(&t1); |
| 260 | |
| 261 | return timespec_diff_ns(&t0, &t1); |
| 262 | } |
| 263 | |
| 264 | static void prepare_key(int decrypt, int keysize, int mode) |
| 265 | { |
| 266 | TEEC_Result res; |
| 267 | uint32_t ret_origin; |
| 268 | TEEC_Operation op; |
| 269 | |
| 270 | memset(&op, 0, sizeof(op)); |
| 271 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT, |
| 272 | TEEC_NONE, TEEC_NONE); |
| 273 | op.params[0].value.a = decrypt; |
| 274 | op.params[0].value.b = keysize; |
| 275 | op.params[1].value.a = mode; |
| 276 | res = TEEC_InvokeCommand(&sess, TA_AES_PERF_CMD_PREPARE_KEY, &op, |
| 277 | &ret_origin); |
| 278 | check_res(res, "TEEC_InvokeCommand"); |
| 279 | } |
| 280 | |
| 281 | static void do_warmup(int warmup) |
| 282 | { |
| 283 | struct timespec t0, t; |
| 284 | int i; |
| 285 | |
| 286 | get_current_time(&t0); |
| 287 | do { |
| 288 | for (i = 0; i < 100000; i++) |
| 289 | ; |
| 290 | get_current_time(&t); |
| 291 | } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000); |
| 292 | } |
| 293 | |
| 294 | static const char *yesno(int v) |
| 295 | { |
| 296 | return (v ? "yes" : "no"); |
| 297 | } |
| 298 | |
| 299 | static double mb_per_sec(size_t size, double usec) |
| 300 | { |
| 301 | return (1000000000/usec)*((double)size/(1024*1024)); |
| 302 | } |
| 303 | |
| 304 | /* Encryption test: buffer of tsize byte. Run test n times. */ |
| 305 | void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size, |
| 306 | unsigned int n, unsigned int l, int random_in, |
| 307 | int in_place, int warmup, int verbosity) |
| 308 | { |
| 309 | uint64_t t; |
| 310 | struct statistics stats; |
| 311 | struct timespec ts; |
| 312 | TEEC_Operation op; |
| 313 | int n0 = n; |
| 314 | |
| 315 | vverbose("aes-perf version %s\n", TO_STR(VERSION)); |
| 316 | 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); |
| 358 | if (n % (n0/10) == 0) |
| 359 | vverbose("#"); |
| 360 | } |
| 361 | vverbose("\n"); |
| 362 | printf("min=%gμs max=%gμs mean=%gμs stddev=%gμs (%gMiB/s)\n", |
| 363 | stats.min/1000, stats.max/1000, stats.m/1000, |
| 364 | stddev(&stats)/1000, mb_per_sec(size, stats.m)); |
| 365 | free_shm(); |
| 366 | } |
| 367 | |
| 368 | #define NEXT_ARG(i) \ |
| 369 | do { \ |
| 370 | if (++i == argc) { \ |
| 371 | fprintf(stderr, "%s: %s: missing argument\n", \ |
| 372 | argv[0], argv[i-1]); \ |
| 373 | return 1; \ |
| 374 | } \ |
| 375 | } while (0); |
| 376 | |
| 377 | int 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 Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame^] | 386 | 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 Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 389 | int decrypt = 0; /* Encrypt by default, -d to decrypt */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame^] | 390 | int keysize = AES_128; /* AES key size (-k) */ |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 391 | int mode = TA_AES_ECB; /* AES mode (-m) */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame^] | 392 | /* 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 Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 397 | |
| 398 | /* Parse command line */ |
| 399 | for (i = 1; i < argc; i++) { |
| 400 | if (!strcmp(argv[i], "-h")) { |
| 401 | 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; |
| 408 | } else if (!strcmp(argv[i], "-i")) { |
| 409 | in_place = 1; |
| 410 | } else if (!strcmp(argv[i], "-k")) { |
| 411 | NEXT_ARG(i); |
| 412 | keysize = atoi(argv[i]); |
| 413 | if (keysize != AES_128 && keysize != AES_192 && |
| 414 | keysize != AES_256) { |
| 415 | fprintf(stderr, "%s: invalid key size\n", |
| 416 | argv[0]); |
| 417 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 418 | return 1; |
| 419 | } |
| 420 | } else if (!strcmp(argv[i], "-l")) { |
| 421 | NEXT_ARG(i); |
| 422 | l = atoi(argv[i]); |
| 423 | } else if (!strcmp(argv[i], "-m")) { |
| 424 | NEXT_ARG(i); |
| 425 | if (!strcasecmp(argv[i], "ECB")) |
| 426 | mode = TA_AES_ECB; |
| 427 | else if (!strcasecmp(argv[i], "CBC")) |
| 428 | mode = TA_AES_CBC; |
| 429 | else if (!strcasecmp(argv[i], "CTR")) |
| 430 | mode = TA_AES_CTR; |
| 431 | else if (!strcasecmp(argv[i], "XTS")) |
| 432 | mode = TA_AES_XTS; |
| 433 | else { |
| 434 | fprintf(stderr, "%s, invalid mode\n", |
| 435 | argv[0]); |
| 436 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 437 | return 1; |
| 438 | } |
| 439 | } else if (!strcmp(argv[i], "-n")) { |
| 440 | NEXT_ARG(i); |
| 441 | n = atoi(argv[i]); |
| 442 | } else if (!strcmp(argv[i], "-r")) { |
| 443 | random_in = 1; |
| 444 | } else if (!strcmp(argv[i], "-s")) { |
| 445 | NEXT_ARG(i); |
| 446 | size = atoi(argv[i]); |
| 447 | } else if (!strcmp(argv[i], "-v")) { |
| 448 | verbosity++; |
| 449 | } else if (!strcmp(argv[i], "-w")) { |
| 450 | NEXT_ARG(i); |
| 451 | warmup = atoi(argv[i]); |
| 452 | } else { |
| 453 | fprintf(stderr, "%s: invalid argument: %s\n", |
| 454 | argv[0], argv[i]); |
| 455 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 456 | return 1; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | |
| 461 | aes_perf_run_test(mode, keysize, decrypt, size, n, l, random_in, |
| 462 | in_place, warmup, verbosity); |
| 463 | |
| 464 | return 0; |
| 465 | } |