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 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 28 | #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 Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 35 | #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> |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 41 | #include <tee_client_api_extensions.h> |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 42 | #include <time.h> |
| 43 | #include <unistd.h> |
| 44 | |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 45 | #include "crypto_common.h" |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 46 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 47 | #ifdef CFG_SECURE_DATA_PATH |
| 48 | #include "sdp_basic.h" |
| 49 | |
| 50 | static int input_sdp_fd; |
| 51 | static int output_sdp_fd; |
| 52 | static int ion_heap = DEFAULT_ION_HEAP_TYPE; |
| 53 | |
| 54 | /* re-use the allocate_ion_buffer() from sdp_basic.c */ |
| 55 | int allocate_ion_buffer(size_t size, int heap_id, int verbosity); |
| 56 | #endif /* CFG_SECURE_DATA_PATH */ |
| 57 | |
| 58 | /* |
| 59 | * Type of buffer used for the performance tests |
| 60 | * |
| 61 | * BUFFER_UNSPECIFIED test did not specify target buffer to use |
| 62 | * BUFFER_SHM_ALLOCATED buffer allocated in TEE SHM. |
| 63 | * BUFFER_SECURE_REGISTER secure buffer, registered to TEE at TA invoc. |
| 64 | * BUFFER_SECURE_PREREGISTERED secure buffer, registered once to TEE. |
| 65 | */ |
| 66 | enum buffer_types { |
| 67 | BUFFER_UNSPECIFIED = 0, |
| 68 | BUFFER_SHM_ALLOCATED, |
| 69 | BUFFER_SECURE_REGISTER, /* requires SDP */ |
| 70 | BUFFER_SECURE_PREREGISTERED, /* requires SDP */ |
| 71 | }; |
| 72 | |
| 73 | static enum buffer_types input_buffer = BUFFER_UNSPECIFIED; |
| 74 | static enum buffer_types output_buffer = BUFFER_UNSPECIFIED; |
| 75 | |
| 76 | static const char *buf_type_str(int buf_type) |
| 77 | { |
| 78 | static const char sec_prereg[] = "Secure memory, registered once to TEE"; |
| 79 | static const char sec_reg[] = "Secure memory, registered at each TEE invoke"; |
| 80 | static const char ns_alloc[] = "Non secure memory"; |
| 81 | static const char inval[] = "UNEXPECTED"; |
| 82 | |
| 83 | switch (buf_type) { |
| 84 | case BUFFER_SECURE_PREREGISTERED: |
| 85 | return sec_prereg; |
| 86 | case BUFFER_SECURE_REGISTER: |
| 87 | return sec_reg; |
| 88 | case BUFFER_SHM_ALLOCATED: |
| 89 | return ns_alloc; |
| 90 | default: |
| 91 | return inval; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /* Are we running a SDP test: default to NO (is_sdp_test == 0) */ |
| 96 | static int is_sdp_test; |
| 97 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 98 | /* |
| 99 | * TEE client stuff |
| 100 | */ |
| 101 | |
| 102 | static TEEC_Context ctx; |
| 103 | static TEEC_Session sess; |
| 104 | /* |
| 105 | * in_shm and out_shm are both IN/OUT to support dynamically choosing |
| 106 | * in_place == 1 or in_place == 0. |
| 107 | */ |
| 108 | static TEEC_SharedMemory in_shm = { |
| 109 | .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT |
| 110 | }; |
| 111 | static TEEC_SharedMemory out_shm = { |
| 112 | .flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT |
| 113 | }; |
| 114 | |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 115 | static void errx(const char *msg, TEEC_Result res, uint32_t *orig) |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 116 | { |
| 117 | fprintf(stderr, "%s: 0x%08x", msg, res); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 118 | if (orig) |
| 119 | fprintf(stderr, " (orig=%d)", (int)*orig); |
| 120 | fprintf(stderr, "\n"); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 121 | exit (1); |
| 122 | } |
| 123 | |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 124 | static void check_res(TEEC_Result res, const char *errmsg, uint32_t *orig) |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 125 | { |
| 126 | if (res != TEEC_SUCCESS) |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 127 | errx(errmsg, res, orig); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void open_ta(void) |
| 131 | { |
| 132 | TEEC_Result res; |
| 133 | TEEC_UUID uuid = TA_AES_PERF_UUID; |
| 134 | uint32_t err_origin; |
| 135 | |
| 136 | res = TEEC_InitializeContext(NULL, &ctx); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 137 | check_res(res, "TEEC_InitializeContext", NULL); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 138 | |
| 139 | res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 140 | NULL, &err_origin); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 141 | check_res(res, "TEEC_OpenSession", &err_origin); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Statistics |
| 146 | * |
| 147 | * We want to compute min, max, mean and standard deviation of processing time |
| 148 | */ |
| 149 | |
| 150 | struct statistics { |
| 151 | int n; |
| 152 | double m; |
| 153 | double M2; |
| 154 | double min; |
| 155 | double max; |
| 156 | int initialized; |
| 157 | }; |
| 158 | |
| 159 | /* Take new sample into account (Knuth/Welford algorithm) */ |
| 160 | static void update_stats(struct statistics *s, uint64_t t) |
| 161 | { |
| 162 | double x = (double)t; |
| 163 | double delta = x - s->m; |
| 164 | |
| 165 | s->n++; |
| 166 | s->m += delta/s->n; |
| 167 | s->M2 += delta*(x - s->m); |
| 168 | if (!s->initialized) { |
| 169 | s->min = s->max = x; |
| 170 | s->initialized = 1; |
| 171 | } else { |
| 172 | if (s->min > x) |
| 173 | s->min = x; |
| 174 | if (s->max < x) |
| 175 | s->max = x; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | static double stddev(struct statistics *s) |
| 180 | { |
| 181 | if (s->n < 2) |
| 182 | return NAN; |
| 183 | return sqrt(s->M2/s->n); |
| 184 | } |
| 185 | |
| 186 | static const char *mode_str(uint32_t mode) |
| 187 | { |
| 188 | switch (mode) { |
| 189 | case TA_AES_ECB: |
| 190 | return "ECB"; |
| 191 | case TA_AES_CBC: |
| 192 | return "CBC"; |
| 193 | case TA_AES_CTR: |
| 194 | return "CTR"; |
| 195 | case TA_AES_XTS: |
| 196 | return "XTS"; |
| 197 | default: |
| 198 | return "???"; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | #define _TO_STR(x) #x |
| 203 | #define TO_STR(x) _TO_STR(x) |
| 204 | |
| 205 | static void usage(const char *progname, int keysize, int mode, |
| 206 | size_t size, int warmup, unsigned int l, unsigned int n) |
| 207 | { |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 208 | fprintf(stderr, "Usage: %s [-h]\n", progname); |
| 209 | fprintf(stderr, "Usage: %s [-d] [-i] [-k SIZE]", progname); |
| 210 | fprintf(stderr, " [-l LOOP] [-m MODE] [-n LOOP] [-r] [-s SIZE]"); |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 211 | fprintf(stderr, " [-v [-v]] [-w SEC]"); |
| 212 | #ifdef CFG_SECURE_DATA_PATH |
| 213 | fprintf(stderr, " [--sdp [-Id|-Ir|-IR] [-Od|-Or|-OR] [--ion-heap ID]]"); |
| 214 | #endif |
| 215 | fprintf(stderr, "\n"); |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 216 | fprintf(stderr, "AES performance testing tool for OP-TEE\n"); |
| 217 | fprintf(stderr, "\n"); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 218 | fprintf(stderr, "Options:\n"); |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 219 | fprintf(stderr, " -d Test AES decryption instead of encryption\n"); |
| 220 | fprintf(stderr, " -h|--help Print this help and exit\n"); |
| 221 | fprintf(stderr, " -i|--in-place Use same buffer for input and output (decrypt in place)\n"); |
| 222 | fprintf(stderr, " -k SIZE Key size in bits: 128, 192 or 256 [%u]\n", keysize); |
| 223 | fprintf(stderr, " -l LOOP Inner loop iterations (TA calls TEE_CipherUpdate() <x> times) [%u]\n", l); |
| 224 | fprintf(stderr, " -m MODE AES mode: ECB, CBC, CTR, XTS [%s]\n", mode_str(mode)); |
| 225 | fprintf(stderr, " -n LOOP Outer test loop iterations [%u]\n", n); |
| 226 | fprintf(stderr, " -r|--random Get input data from /dev/urandom (default: all-zeros)\n"); |
| 227 | fprintf(stderr, " -s SIZE Test buffer size in bytes [%zu]\n", size); |
| 228 | fprintf(stderr, " -v Be verbose (use twice for greater effect)\n"); |
| 229 | fprintf(stderr, " -w|--warmup SEC Warm-up time in seconds: execute a busy loop before\n"); |
| 230 | fprintf(stderr, " the test to mitigate the effects of cpufreq etc. [%u]\n", warmup); |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 231 | #ifdef CFG_SECURE_DATA_PATH |
| 232 | fprintf(stderr, "Secure data path specific options:\n"); |
| 233 | fprintf(stderr, " --sdp Run the AES test in the scope fo a Secure Data Path test TA\n"); |
| 234 | fprintf(stderr, " --ion-heap ID Set ION heap ID where to allocate secure buffers [%d]\n", ion_heap); |
| 235 | fprintf(stderr, " -I... AES input test buffer management:\n"); |
| 236 | fprintf(stderr, " -Id allocate a non secure buffer (default)\n"); |
| 237 | fprintf(stderr, " -Ir allocate a secure buffer, registered at each TA invocation\n"); |
| 238 | fprintf(stderr, " -IR allocate a secure buffer, registered once in TEE\n"); |
| 239 | fprintf(stderr, " -O... AES output test buffer management:\n"); |
| 240 | fprintf(stderr, " -Od allocate a non secure buffer (default if \"--sdp\" is not set)\n"); |
| 241 | fprintf(stderr, " -Or allocated a secure buffer, registered at each TA invocation\n"); |
| 242 | fprintf(stderr, " -OR allocated a secure buffer, registered once in TEE (default if \"--sdp\")\n"); |
| 243 | #endif |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 244 | } |
| 245 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 246 | #ifdef CFG_SECURE_DATA_PATH |
| 247 | static void register_shm(TEEC_SharedMemory *shm, int fd) |
| 248 | { |
| 249 | TEEC_Result res = TEEC_RegisterSharedMemoryFileDescriptor(&ctx, shm, fd); |
| 250 | |
| 251 | check_res(res, "TEEC_RegisterSharedMemoryFileDescriptor", NULL); |
| 252 | } |
| 253 | #endif |
| 254 | |
| 255 | static void allocate_shm(TEEC_SharedMemory *shm, size_t sz) |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 256 | { |
| 257 | TEEC_Result res; |
| 258 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 259 | shm->buffer = NULL; |
| 260 | shm->size = sz; |
| 261 | res = TEEC_AllocateSharedMemory(&ctx, shm); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 262 | check_res(res, "TEEC_AllocateSharedMemory", NULL); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 263 | } |
| 264 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 265 | /* initial test buffer allocation (eventual registering to TEEC) */ |
| 266 | static void alloc_buffers(size_t sz, int in_place, int verbosity) |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 267 | { |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 268 | (void)verbosity; |
| 269 | |
| 270 | if (input_buffer == BUFFER_SHM_ALLOCATED) |
| 271 | allocate_shm(&in_shm, sz); |
| 272 | #ifdef CFG_SECURE_DATA_PATH |
| 273 | else { |
| 274 | input_sdp_fd = allocate_ion_buffer(sz, ion_heap, verbosity); |
| 275 | if (input_buffer == BUFFER_SECURE_PREREGISTERED) { |
| 276 | register_shm(&in_shm, input_sdp_fd); |
| 277 | close(input_sdp_fd); |
| 278 | } |
| 279 | } |
| 280 | #endif |
| 281 | |
| 282 | if (in_place) |
| 283 | return; |
| 284 | |
| 285 | if (output_buffer == BUFFER_SHM_ALLOCATED) |
| 286 | allocate_shm(&out_shm, sz); |
| 287 | #ifdef CFG_SECURE_DATA_PATH |
| 288 | else { |
| 289 | output_sdp_fd = allocate_ion_buffer(sz, ion_heap, verbosity); |
| 290 | if (output_buffer == BUFFER_SECURE_PREREGISTERED) { |
| 291 | register_shm(&out_shm, output_sdp_fd); |
| 292 | close(output_sdp_fd); |
| 293 | } |
| 294 | } |
| 295 | #endif |
| 296 | } |
| 297 | |
| 298 | static void free_shm(int in_place) |
| 299 | { |
| 300 | (void)in_place; |
| 301 | |
| 302 | if (input_buffer == BUFFER_SHM_ALLOCATED && |
| 303 | output_buffer == BUFFER_SHM_ALLOCATED) { |
| 304 | TEEC_ReleaseSharedMemory(&in_shm); |
| 305 | TEEC_ReleaseSharedMemory(&out_shm); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | #ifdef CFG_SECURE_DATA_PATH |
| 310 | if (input_buffer == BUFFER_SECURE_PREREGISTERED) |
| 311 | close(input_sdp_fd); |
| 312 | if (input_buffer != BUFFER_SECURE_REGISTER) |
| 313 | TEEC_ReleaseSharedMemory(&in_shm); |
| 314 | |
| 315 | if (in_place) |
| 316 | return; |
| 317 | |
| 318 | if (output_buffer == BUFFER_SECURE_PREREGISTERED) |
| 319 | close(output_sdp_fd); |
| 320 | if (output_buffer != BUFFER_SECURE_REGISTER) |
| 321 | TEEC_ReleaseSharedMemory(&out_shm); |
| 322 | #endif /* CFG_SECURE_DATA_PATH */ |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static ssize_t read_random(void *in, size_t rsize) |
| 326 | { |
| 327 | static int rnd; |
| 328 | ssize_t s; |
| 329 | |
| 330 | if (!rnd) { |
| 331 | rnd = open("/dev/urandom", O_RDONLY); |
| 332 | if (rnd < 0) { |
| 333 | perror("open"); |
| 334 | return 1; |
| 335 | } |
| 336 | } |
| 337 | s = read(rnd, in, rsize); |
| 338 | if (s < 0) { |
| 339 | perror("read"); |
| 340 | return 1; |
| 341 | } |
| 342 | if ((size_t)s != rsize) { |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 343 | printf("read: requested %zu bytes, got %zd\n", rsize, s); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static void get_current_time(struct timespec *ts) |
| 350 | { |
| 351 | if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) { |
| 352 | perror("clock_gettime"); |
| 353 | exit(1); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | static uint64_t timespec_to_ns(struct timespec *ts) |
| 358 | { |
| 359 | return ((uint64_t)ts->tv_sec * 1000000000) + ts->tv_nsec; |
| 360 | } |
| 361 | |
| 362 | static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end) |
| 363 | { |
| 364 | return timespec_to_ns(end) - timespec_to_ns(start); |
| 365 | } |
| 366 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 367 | static void prepare_key(int decrypt, int keysize, int mode) |
| 368 | { |
| 369 | TEEC_Result res; |
| 370 | uint32_t ret_origin; |
| 371 | TEEC_Operation op; |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 372 | uint32_t cmd = TA_AES_PERF_CMD_PREPARE_KEY; |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 373 | |
| 374 | memset(&op, 0, sizeof(op)); |
| 375 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_VALUE_INPUT, |
| 376 | TEEC_NONE, TEEC_NONE); |
| 377 | op.params[0].value.a = decrypt; |
| 378 | op.params[0].value.b = keysize; |
| 379 | op.params[1].value.a = mode; |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 380 | res = TEEC_InvokeCommand(&sess, cmd, &op, |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 381 | &ret_origin); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 382 | check_res(res, "TEEC_InvokeCommand", &ret_origin); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void do_warmup(int warmup) |
| 386 | { |
| 387 | struct timespec t0, t; |
| 388 | int i; |
| 389 | |
| 390 | get_current_time(&t0); |
| 391 | do { |
| 392 | for (i = 0; i < 100000; i++) |
| 393 | ; |
| 394 | get_current_time(&t); |
| 395 | } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000); |
| 396 | } |
| 397 | |
| 398 | static const char *yesno(int v) |
| 399 | { |
| 400 | return (v ? "yes" : "no"); |
| 401 | } |
| 402 | |
| 403 | static double mb_per_sec(size_t size, double usec) |
| 404 | { |
| 405 | return (1000000000/usec)*((double)size/(1024*1024)); |
| 406 | } |
| 407 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 408 | |
| 409 | static void run_feed_input(void *in, size_t size, int random_in) |
| 410 | { |
| 411 | if (!random_in) |
| 412 | return; |
| 413 | |
| 414 | if (!is_sdp_test) |
| 415 | read_random(in, size); |
| 416 | |
| 417 | #ifdef CFG_SECURE_DATA_PATH |
| 418 | if (input_buffer != BUFFER_SHM_ALLOCATED) { |
| 419 | char *data = mmap(NULL, size, PROT_WRITE, MAP_SHARED, |
| 420 | input_sdp_fd, 0); |
| 421 | |
| 422 | if (data == MAP_FAILED) { |
| 423 | perror("failed to map input buffer"); |
| 424 | exit(-1); |
| 425 | } |
| 426 | read_random(data, size); |
| 427 | munmap(data, size); |
| 428 | } |
| 429 | #endif |
| 430 | } |
| 431 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 432 | /* Encryption test: buffer of tsize byte. Run test n times. */ |
| 433 | void aes_perf_run_test(int mode, int keysize, int decrypt, size_t size, |
| 434 | unsigned int n, unsigned int l, int random_in, |
| 435 | int in_place, int warmup, int verbosity) |
| 436 | { |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 437 | struct statistics stats; |
| 438 | struct timespec ts; |
| 439 | TEEC_Operation op; |
| 440 | int n0 = n; |
Jerome Forissier | d99fe97 | 2017-02-21 15:30:57 +0100 | [diff] [blame] | 441 | double sd; |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 442 | uint32_t cmd = is_sdp_test ? TA_AES_PERF_CMD_PROCESS_SDP : |
| 443 | TA_AES_PERF_CMD_PROCESS; |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 444 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 445 | if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) { |
| 446 | perror("clock_getres"); |
| 447 | return; |
| 448 | } |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 449 | vverbose("Clock resolution is %lu ns\n", |
| 450 | ts.tv_sec * 1000000000 + ts.tv_nsec); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 451 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 452 | vverbose("input test buffer: %s\n", buf_type_str(input_buffer)); |
| 453 | vverbose("output test buffer: %s\n", buf_type_str(output_buffer)); |
| 454 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 455 | open_ta(); |
| 456 | prepare_key(decrypt, keysize, mode); |
| 457 | |
| 458 | memset(&stats, 0, sizeof(stats)); |
| 459 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 460 | alloc_buffers(size, in_place, verbosity); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 461 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 462 | if (input_buffer == BUFFER_SHM_ALLOCATED && !random_in) |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 463 | memset(in_shm.buffer, 0, size); |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 464 | #ifdef CFG_SECURE_DATA_PATH |
| 465 | else { |
| 466 | char *data = mmap(NULL, size, PROT_WRITE, MAP_SHARED, |
| 467 | input_sdp_fd, 0); |
| 468 | |
| 469 | if (data == MAP_FAILED) { |
| 470 | perror("failed to map input buffer"); |
| 471 | exit(-1); |
| 472 | } |
| 473 | memset(in_shm.buffer, 0, size); |
| 474 | munmap(data, size); |
| 475 | } |
| 476 | #endif |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 477 | |
| 478 | memset(&op, 0, sizeof(op)); |
| 479 | /* Using INOUT to handle the case in_place == 1 */ |
| 480 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT, |
| 481 | TEEC_MEMREF_PARTIAL_INOUT, |
| 482 | TEEC_VALUE_INPUT, TEEC_NONE); |
| 483 | op.params[0].memref.parent = &in_shm; |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 484 | op.params[0].memref.size = size; |
| 485 | op.params[1].memref.parent = in_place ? &in_shm : &out_shm; |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 486 | op.params[1].memref.size = size; |
| 487 | op.params[2].value.a = l; |
| 488 | |
| 489 | verbose("Starting test: %s, %scrypt, keysize=%u bits, size=%zu bytes, ", |
| 490 | mode_str(mode), (decrypt ? "de" : "en"), keysize, size); |
| 491 | verbose("random=%s, ", yesno(random_in)); |
| 492 | verbose("in place=%s, ", yesno(in_place)); |
| 493 | verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup); |
| 494 | |
| 495 | if (warmup) |
| 496 | do_warmup(warmup); |
| 497 | |
| 498 | while (n-- > 0) { |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 499 | TEEC_Result res; |
| 500 | uint32_t ret_origin; |
| 501 | struct timespec t0, t1; |
| 502 | |
| 503 | run_feed_input(in_shm.buffer, size, random_in); |
| 504 | |
| 505 | get_current_time(&t0); |
| 506 | |
| 507 | #ifdef CFG_SECURE_DATA_PATH |
| 508 | if (input_buffer == BUFFER_SECURE_REGISTER) |
| 509 | register_shm(&in_shm, input_sdp_fd); |
| 510 | if (output_buffer == BUFFER_SECURE_REGISTER) |
| 511 | register_shm(&out_shm, output_sdp_fd); |
| 512 | #endif |
| 513 | |
| 514 | res = TEEC_InvokeCommand(&sess, cmd, |
| 515 | &op, &ret_origin); |
| 516 | check_res(res, "TEEC_InvokeCommand", &ret_origin); |
| 517 | |
| 518 | #ifdef CFG_SECURE_DATA_PATH |
| 519 | if (input_buffer == BUFFER_SECURE_REGISTER) |
| 520 | TEEC_ReleaseSharedMemory(&in_shm); |
| 521 | if (output_buffer == BUFFER_SECURE_REGISTER) |
| 522 | TEEC_ReleaseSharedMemory(&out_shm); |
| 523 | #endif |
| 524 | |
| 525 | get_current_time(&t1); |
| 526 | |
| 527 | update_stats(&stats, timespec_diff_ns(&t0, &t1)); |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 528 | if (n % (n0 / 10) == 0) |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 529 | vverbose("#"); |
| 530 | } |
| 531 | vverbose("\n"); |
Jerome Forissier | d99fe97 | 2017-02-21 15:30:57 +0100 | [diff] [blame] | 532 | sd = stddev(&stats); |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 533 | printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n", |
| 534 | stats.min / 1000, stats.max / 1000, stats.m / 1000, |
| 535 | sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m)); |
| 536 | verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n", |
| 537 | (stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000, |
| 538 | mb_per_sec(size, stats.m + 2 * sd), |
| 539 | mb_per_sec(size, stats.m - 2 * sd)); |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 540 | free_shm(in_place); |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | #define NEXT_ARG(i) \ |
| 544 | do { \ |
| 545 | if (++i == argc) { \ |
| 546 | fprintf(stderr, "%s: %s: missing argument\n", \ |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 547 | argv[0], argv[i - 1]); \ |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 548 | return 1; \ |
| 549 | } \ |
| 550 | } while (0); |
| 551 | |
| 552 | int aes_perf_runner_cmd_parser(int argc, char *argv[]) |
| 553 | { |
| 554 | int i; |
| 555 | |
| 556 | /* |
| 557 | * Command line parameters |
| 558 | */ |
| 559 | |
| 560 | size_t size = 1024; /* Buffer size (-s) */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 561 | unsigned int n = CRYPTO_DEF_COUNT; /*Number of measurements (-n)*/ |
| 562 | unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */ |
| 563 | int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */ |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 564 | int decrypt = 0; /* Encrypt by default, -d to decrypt */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 565 | int keysize = AES_128; /* AES key size (-k) */ |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 566 | int mode = TA_AES_ECB; /* AES mode (-m) */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 567 | /* Get input data from /dev/urandom (-r) */ |
| 568 | int random_in = CRYPTO_USE_RANDOM; |
| 569 | /* Use same buffer for in and out (-i) */ |
| 570 | int in_place = AES_PERF_INPLACE; |
| 571 | 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] | 572 | |
| 573 | /* Parse command line */ |
| 574 | for (i = 1; i < argc; i++) { |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 575 | if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 576 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 577 | return 0; |
| 578 | } |
| 579 | } |
| 580 | for (i = 1; i < argc; i++) { |
| 581 | if (!strcmp(argv[i], "-d")) { |
| 582 | decrypt = 1; |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 583 | } else if (!strcmp(argv[i], "--in-place") || |
| 584 | !strcmp(argv[i], "-i")) { |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 585 | in_place = 1; |
| 586 | } else if (!strcmp(argv[i], "-k")) { |
| 587 | NEXT_ARG(i); |
| 588 | keysize = atoi(argv[i]); |
| 589 | if (keysize != AES_128 && keysize != AES_192 && |
| 590 | keysize != AES_256) { |
| 591 | fprintf(stderr, "%s: invalid key size\n", |
| 592 | argv[0]); |
| 593 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 594 | return 1; |
| 595 | } |
| 596 | } else if (!strcmp(argv[i], "-l")) { |
| 597 | NEXT_ARG(i); |
| 598 | l = atoi(argv[i]); |
| 599 | } else if (!strcmp(argv[i], "-m")) { |
| 600 | NEXT_ARG(i); |
| 601 | if (!strcasecmp(argv[i], "ECB")) |
| 602 | mode = TA_AES_ECB; |
| 603 | else if (!strcasecmp(argv[i], "CBC")) |
| 604 | mode = TA_AES_CBC; |
| 605 | else if (!strcasecmp(argv[i], "CTR")) |
| 606 | mode = TA_AES_CTR; |
| 607 | else if (!strcasecmp(argv[i], "XTS")) |
| 608 | mode = TA_AES_XTS; |
| 609 | else { |
| 610 | fprintf(stderr, "%s, invalid mode\n", |
| 611 | argv[0]); |
| 612 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 613 | return 1; |
| 614 | } |
| 615 | } else if (!strcmp(argv[i], "-n")) { |
| 616 | NEXT_ARG(i); |
| 617 | n = atoi(argv[i]); |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 618 | } else if (!strcmp(argv[i], "--random") || |
| 619 | !strcmp(argv[i], "-r")) { |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 620 | random_in = 1; |
| 621 | } else if (!strcmp(argv[i], "-s")) { |
| 622 | NEXT_ARG(i); |
| 623 | size = atoi(argv[i]); |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 624 | #ifdef CFG_SECURE_DATA_PATH |
| 625 | } else if (!strcmp(argv[i], "--sdp")) { |
| 626 | is_sdp_test = 1; |
| 627 | } else if (!strcmp(argv[i], "-IR")) { |
| 628 | input_buffer = BUFFER_SECURE_PREREGISTERED; |
| 629 | } else if (!strcmp(argv[i], "-OR")) { |
| 630 | output_buffer = BUFFER_SECURE_PREREGISTERED; |
| 631 | } else if (!strcmp(argv[i], "-Ir")) { |
| 632 | input_buffer = BUFFER_SECURE_REGISTER; |
| 633 | } else if (!strcmp(argv[i], "-Or")) { |
| 634 | output_buffer = BUFFER_SECURE_REGISTER; |
| 635 | } else if (!strcmp(argv[i], "-Id")) { |
| 636 | input_buffer = BUFFER_SHM_ALLOCATED; |
| 637 | } else if (!strcmp(argv[i], "-Od")) { |
| 638 | output_buffer = BUFFER_SHM_ALLOCATED; |
| 639 | } else if (!strcmp(argv[i], "--ion-heap")) { |
| 640 | NEXT_ARG(i); |
| 641 | ion_heap = atoi(argv[i]); |
| 642 | #endif |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 643 | } else if (!strcmp(argv[i], "-v")) { |
| 644 | verbosity++; |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame] | 645 | } else if (!strcmp(argv[i], "--warmup") || |
| 646 | !strcmp(argv[i], "-w")) { |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 647 | NEXT_ARG(i); |
| 648 | warmup = atoi(argv[i]); |
| 649 | } else { |
| 650 | fprintf(stderr, "%s: invalid argument: %s\n", |
| 651 | argv[0], argv[i]); |
| 652 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 653 | return 1; |
| 654 | } |
| 655 | } |
| 656 | |
Etienne Carriere | 057129b | 2017-04-26 15:03:36 +0200 | [diff] [blame] | 657 | if (size & (16 - 1)) { |
| 658 | fprintf(stderr, "invalid buffer size argument, must be a multiple of 16\n\n"); |
| 659 | usage(argv[0], keysize, mode, size, warmup, l, n); |
| 660 | return 1; |
| 661 | } |
| 662 | |
Etienne Carriere | b85a5f9 | 2017-05-03 18:21:56 +0200 | [diff] [blame^] | 663 | if (input_buffer == BUFFER_UNSPECIFIED) |
| 664 | input_buffer = BUFFER_SHM_ALLOCATED; |
| 665 | |
| 666 | if (output_buffer == BUFFER_UNSPECIFIED) { |
| 667 | if (is_sdp_test) |
| 668 | output_buffer = BUFFER_SECURE_PREREGISTERED; |
| 669 | else |
| 670 | output_buffer = BUFFER_SHM_ALLOCATED; |
| 671 | } |
| 672 | |
| 673 | |
Igor Opaniuk | 44aff4b | 2016-09-16 10:18:00 +0300 | [diff] [blame] | 674 | aes_perf_run_test(mode, keysize, decrypt, size, n, l, random_in, |
| 675 | in_place, warmup, verbosity); |
| 676 | |
| 677 | return 0; |
| 678 | } |