Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +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 | |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 28 | #include <adbg.h> |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 29 | #include <fcntl.h> |
| 30 | #include <math.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <strings.h> |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <tee_client_api.h> |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 39 | #include <time.h> |
| 40 | #include <unistd.h> |
| 41 | |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 42 | #include "crypto_common.h" |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 43 | |
| 44 | /* |
| 45 | * TEE client stuff |
| 46 | */ |
| 47 | |
| 48 | static TEEC_Context ctx; |
| 49 | static TEEC_Session sess; |
| 50 | static TEEC_SharedMemory in_shm = { |
| 51 | .flags = TEEC_MEM_INPUT |
| 52 | }; |
| 53 | static TEEC_SharedMemory out_shm = { |
| 54 | .flags = TEEC_MEM_OUTPUT |
| 55 | }; |
| 56 | |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 57 | static void errx(const char *msg, TEEC_Result res, uint32_t *orig) |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 58 | { |
| 59 | fprintf(stderr, "%s: 0x%08x", msg, res); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 60 | if (orig) |
| 61 | fprintf(stderr, " (orig=%d)", (int)*orig); |
| 62 | fprintf(stderr, "\n"); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 63 | exit (1); |
| 64 | } |
| 65 | |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 66 | static void check_res(TEEC_Result res, const char *errmsg, uint32_t *orig) |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 67 | { |
| 68 | if (res != TEEC_SUCCESS) |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 69 | errx(errmsg, res, orig); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void open_ta(void) |
| 73 | { |
| 74 | TEEC_Result res; |
| 75 | TEEC_UUID uuid = TA_SHA_PERF_UUID; |
| 76 | uint32_t err_origin; |
| 77 | |
| 78 | res = TEEC_InitializeContext(NULL, &ctx); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 79 | check_res(res,"TEEC_InitializeContext", NULL); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 80 | |
| 81 | res = TEEC_OpenSession(&ctx, &sess, &uuid, TEEC_LOGIN_PUBLIC, NULL, |
| 82 | NULL, &err_origin); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 83 | check_res(res,"TEEC_OpenSession", &err_origin); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Statistics |
| 88 | * |
| 89 | * We want to compute min, max, mean and standard deviation of processing time |
| 90 | */ |
| 91 | |
| 92 | struct statistics { |
| 93 | int n; |
| 94 | double m; |
| 95 | double M2; |
| 96 | double min; |
| 97 | double max; |
| 98 | int initialized; |
| 99 | }; |
| 100 | |
| 101 | /* Take new sample into account (Knuth/Welford algorithm) */ |
| 102 | static void update_stats(struct statistics *s, uint64_t t) |
| 103 | { |
| 104 | double x = (double)t; |
| 105 | double delta = x - s->m; |
| 106 | |
| 107 | s->n++; |
| 108 | s->m += delta/s->n; |
| 109 | s->M2 += delta*(x - s->m); |
| 110 | if (!s->initialized) { |
| 111 | s->min = s->max = x; |
| 112 | s->initialized = 1; |
| 113 | } else { |
| 114 | if (s->min > x) |
| 115 | s->min = x; |
| 116 | if (s->max < x) |
| 117 | s->max = x; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static double stddev(struct statistics *s) |
| 122 | { |
| 123 | if (s->n < 2) |
| 124 | return NAN; |
| 125 | return sqrt(s->M2/s->n); |
| 126 | } |
| 127 | |
| 128 | static const char *algo_str(uint32_t algo) |
| 129 | { |
| 130 | switch (algo) { |
| 131 | case TA_SHA_SHA1: |
| 132 | return "SHA1"; |
| 133 | case TA_SHA_SHA224: |
| 134 | return "SHA224"; |
| 135 | case TA_SHA_SHA256: |
| 136 | return "SHA256"; |
| 137 | case TA_SHA_SHA384: |
| 138 | return "SHA384"; |
| 139 | case TA_SHA_SHA512: |
| 140 | return "SHA512"; |
| 141 | default: |
| 142 | return "???"; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static int hash_size(uint32_t algo) |
| 147 | { |
| 148 | switch (algo) { |
| 149 | case TA_SHA_SHA1: |
| 150 | return 20; |
| 151 | case TA_SHA_SHA224: |
| 152 | return 28; |
| 153 | case TA_SHA_SHA256: |
| 154 | return 32; |
| 155 | case TA_SHA_SHA384: |
| 156 | return 48; |
| 157 | case TA_SHA_SHA512: |
| 158 | return 64; |
| 159 | default: |
| 160 | return 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | #define _TO_STR(x) #x |
| 165 | #define TO_STR(x) _TO_STR(x) |
| 166 | |
| 167 | |
| 168 | static void alloc_shm(size_t sz, uint32_t algo, int offset) |
| 169 | { |
| 170 | TEEC_Result res; |
| 171 | |
| 172 | in_shm.buffer = NULL; |
| 173 | in_shm.size = sz + offset; |
| 174 | res = TEEC_AllocateSharedMemory(&ctx, &in_shm); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 175 | check_res(res, "TEEC_AllocateSharedMemory", NULL); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 176 | |
| 177 | out_shm.buffer = NULL; |
| 178 | out_shm.size = hash_size(algo); |
| 179 | res = TEEC_AllocateSharedMemory(&ctx, &out_shm); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 180 | check_res(res, "TEEC_AllocateSharedMemory", NULL); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void free_shm(void) |
| 184 | { |
| 185 | TEEC_ReleaseSharedMemory(&in_shm); |
| 186 | TEEC_ReleaseSharedMemory(&out_shm); |
| 187 | } |
| 188 | |
| 189 | static ssize_t read_random(void *in, size_t rsize) |
| 190 | { |
| 191 | static int rnd; |
| 192 | ssize_t s; |
| 193 | |
| 194 | if (!rnd) { |
| 195 | rnd = open("/dev/urandom", O_RDONLY); |
| 196 | if (rnd < 0) { |
| 197 | perror("open"); |
| 198 | return 1; |
| 199 | } |
| 200 | } |
| 201 | s = read(rnd, in, rsize); |
| 202 | if (s < 0) { |
| 203 | perror("read"); |
| 204 | return 1; |
| 205 | } |
| 206 | if ((size_t)s != rsize) { |
| 207 | printf("read: requested %zu bytes, got %zd\n", |
| 208 | rsize, s); |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | static long get_current_time(struct timespec *ts) |
| 214 | { |
| 215 | if (clock_gettime(CLOCK_MONOTONIC, ts) < 0) { |
| 216 | perror("clock_gettime"); |
| 217 | exit(1); |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static uint64_t timespec_diff_ns(struct timespec *start, struct timespec *end) |
| 223 | { |
| 224 | uint64_t ns = 0; |
| 225 | |
| 226 | if (end->tv_nsec < start->tv_nsec) { |
| 227 | ns += 1000000000 * (end->tv_sec - start->tv_sec - 1); |
| 228 | ns += 1000000000 - start->tv_nsec + end->tv_nsec; |
| 229 | } else { |
| 230 | ns += 1000000000 * (end->tv_sec - start->tv_sec); |
| 231 | ns += end->tv_nsec - start->tv_nsec; |
| 232 | } |
| 233 | return ns; |
| 234 | } |
| 235 | |
| 236 | static uint64_t run_test_once(void *in, size_t size, int random_in, TEEC_Operation *op) |
| 237 | { |
| 238 | struct timespec t0, t1; |
| 239 | TEEC_Result res; |
| 240 | uint32_t ret_origin; |
| 241 | |
| 242 | if (random_in) |
| 243 | read_random(in, size); |
| 244 | get_current_time(&t0); |
| 245 | res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PROCESS, op, |
| 246 | &ret_origin); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 247 | check_res(res, "TEEC_InvokeCommand", &ret_origin); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 248 | get_current_time(&t1); |
| 249 | |
| 250 | return timespec_diff_ns(&t0, &t1); |
| 251 | } |
| 252 | |
| 253 | static void prepare_op(int algo) |
| 254 | { |
| 255 | TEEC_Result res; |
| 256 | uint32_t ret_origin; |
| 257 | TEEC_Operation op; |
| 258 | |
| 259 | memset(&op, 0, sizeof(op)); |
| 260 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, |
| 261 | TEEC_NONE, TEEC_NONE); |
| 262 | op.params[0].value.a = algo; |
| 263 | res = TEEC_InvokeCommand(&sess, TA_SHA_PERF_CMD_PREPARE_OP, &op, |
| 264 | &ret_origin); |
Etienne Carriere | 2f18cc4 | 2017-04-26 15:01:26 +0200 | [diff] [blame] | 265 | check_res(res, "TEEC_InvokeCommand", &ret_origin); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void do_warmup(int warmup) |
| 269 | { |
| 270 | struct timespec t0, t; |
| 271 | int i; |
| 272 | |
| 273 | get_current_time(&t0); |
| 274 | do { |
| 275 | for (i = 0; i < 100000; i++) |
| 276 | ; |
| 277 | get_current_time(&t); |
| 278 | } while (timespec_diff_ns(&t0, &t) < (uint64_t)warmup * 1000000000); |
| 279 | } |
| 280 | |
| 281 | static const char *yesno(int v) |
| 282 | { |
| 283 | return (v ? "yes" : "no"); |
| 284 | } |
| 285 | |
| 286 | static double mb_per_sec(size_t size, double usec) |
| 287 | { |
| 288 | return (1000000000/usec)*((double)size/(1024*1024)); |
| 289 | } |
| 290 | |
| 291 | /* Hash test: buffer of size byte. Run test n times. |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 292 | * Entry point for running SHA benchmark |
| 293 | * Params: |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 294 | * algo - Algorithm |
| 295 | * size - Buffer size |
| 296 | * n - Number of measurements |
| 297 | * l - Amount of inner loops |
| 298 | * random_in - Get input from /dev/urandom |
| 299 | * offset - Buffer offset wrt. alloc-ed address |
| 300 | * warmup - Start with a-second busy loop |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 301 | * verbosity - Verbosity level |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 302 | * */ |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 303 | extern void sha_perf_run_test(int algo, size_t size, unsigned int n, |
| 304 | unsigned int l, int random_in, int offset, |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 305 | int warmup, int verbosity) |
| 306 | { |
| 307 | uint64_t t; |
| 308 | struct statistics stats; |
| 309 | TEEC_Operation op; |
| 310 | int n0 = n; |
| 311 | struct timespec ts; |
Jerome Forissier | d99fe97 | 2017-02-21 15:30:57 +0100 | [diff] [blame] | 312 | double sd; |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 313 | |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 314 | vverbose("sha-perf\n"); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 315 | if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) { |
| 316 | perror("clock_getres"); |
| 317 | return; |
| 318 | } |
| 319 | vverbose("Clock resolution is %lu ns\n", ts.tv_sec*1000000000 + |
| 320 | ts.tv_nsec); |
| 321 | |
| 322 | open_ta(); |
| 323 | prepare_op(algo); |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 324 | |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 325 | alloc_shm(size, algo, offset); |
| 326 | |
| 327 | if (!random_in) |
| 328 | memset((uint8_t *)in_shm.buffer + offset, 0, size); |
| 329 | |
| 330 | memset(&op, 0, sizeof(op)); |
| 331 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT, |
| 332 | TEEC_MEMREF_PARTIAL_OUTPUT, |
| 333 | TEEC_VALUE_INPUT, TEEC_NONE); |
| 334 | op.params[0].memref.parent = &in_shm; |
| 335 | op.params[0].memref.offset = 0; |
| 336 | op.params[0].memref.size = size + offset; |
| 337 | op.params[1].memref.parent = &out_shm; |
| 338 | op.params[1].memref.offset = 0; |
| 339 | op.params[1].memref.size = hash_size(algo); |
| 340 | op.params[2].value.a = l; |
| 341 | op.params[2].value.b = offset; |
| 342 | |
| 343 | verbose("Starting test: %s, size=%zu bytes, ", |
| 344 | algo_str(algo), size); |
| 345 | verbose("random=%s, ", yesno(random_in)); |
| 346 | verbose("unaligned=%s, ", yesno(offset)); |
| 347 | verbose("inner loops=%u, loops=%u, warm-up=%u s\n", l, n, warmup); |
| 348 | |
| 349 | if (warmup) |
| 350 | do_warmup(warmup); |
| 351 | |
| 352 | memset(&stats, 0, sizeof(stats)); |
| 353 | while (n-- > 0) { |
| 354 | t = run_test_once((uint8_t *)in_shm.buffer + offset, size, random_in, &op); |
| 355 | update_stats(&stats, t); |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 356 | if (n % (n0 / 10) == 0) |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 357 | vverbose("#"); |
| 358 | } |
| 359 | vverbose("\n"); |
Jerome Forissier | d99fe97 | 2017-02-21 15:30:57 +0100 | [diff] [blame] | 360 | sd = stddev(&stats); |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 361 | printf("min=%gus max=%gus mean=%gus stddev=%gus (cv %g%%) (%gMiB/s)\n", |
| 362 | stats.min / 1000, stats.max / 1000, stats.m / 1000, |
| 363 | sd / 1000, 100 * sd / stats.m, mb_per_sec(size, stats.m)); |
| 364 | verbose("2-sigma interval: %g..%gus (%g..%gMiB/s)\n", |
| 365 | (stats.m - 2 * sd) / 1000, (stats.m + 2 * sd) / 1000, |
| 366 | mb_per_sec(size, stats.m + 2 * sd), |
| 367 | mb_per_sec(size, stats.m - 2 * sd)); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 368 | free_shm(); |
| 369 | } |
| 370 | |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 371 | static void usage(const char *progname, |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 372 | /* Default params */ |
| 373 | int algo, size_t size, int warmup, int l, int n) |
| 374 | { |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 375 | fprintf(stderr, "Usage: %s [-h]\n", progname); |
| 376 | fprintf(stderr, "Usage: %s [-a ALGO] [-l LOOP] [-n LOOP] [-r] [-s SIZE]", progname); |
| 377 | fprintf(stderr, " [-v [-v]] [-w SEC]\n"); |
| 378 | fprintf(stderr, "SHA performance testing tool for OP-TEE\n"); |
| 379 | fprintf(stderr, "\n"); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 380 | fprintf(stderr, "Options:\n"); |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 381 | fprintf(stderr, " -a ALGO Algorithm (SHA1, SHA224, SHA256, SHA384, SHA512) [%s]\n", algo_str(algo)); |
| 382 | fprintf(stderr, " -h|--help Print this help and exit\n"); |
| 383 | fprintf(stderr, " -l LOOP Inner loop iterations (TA calls TEE_DigestDoFinal() <x> times) [%u]\n", l); |
| 384 | fprintf(stderr, " -n LOOP Outer test loop iterations [%u]\n", n); |
| 385 | fprintf(stderr, " -r|--random Get input data from /dev/urandom (default: all-zeros)\n"); |
| 386 | fprintf(stderr, " -s SIZE Test buffer size in bytes [%zu]\n", size); |
| 387 | fprintf(stderr, " -u|--unalign Use unaligned buffer (odd address)\n"); |
| 388 | fprintf(stderr, " -v Be verbose (use twice for greater effect)\n"); |
| 389 | fprintf(stderr, " -w|--warmup SEC Warm-up time in seconds: execute a busy loop before\n"); |
| 390 | fprintf(stderr, " the test to mitigate the effects of cpufreq etc. [%u]\n", warmup); |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | #define NEXT_ARG(i) \ |
| 394 | do { \ |
| 395 | if (++i == argc) { \ |
| 396 | fprintf(stderr, "%s: %s: missing argument\n", \ |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 397 | argv[0], argv[i - 1]); \ |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 398 | return 1; \ |
| 399 | } \ |
| 400 | } while (0); |
| 401 | |
| 402 | |
| 403 | |
| 404 | extern int sha_perf_runner_cmd_parser(int argc, char *argv[]) |
| 405 | { |
| 406 | int i; |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 407 | /* Command line params */ |
| 408 | size_t size = 1024; /* Buffer size (-s) */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 409 | unsigned int n = CRYPTO_DEF_COUNT;/* Number of measurements (-n)*/ |
| 410 | unsigned int l = CRYPTO_DEF_LOOPS; /* Inner loops (-l) */ |
| 411 | int verbosity = CRYPTO_DEF_VERBOSITY; /* Verbosity (-v) */ |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 412 | int algo = TA_SHA_SHA1; /* Algorithm (-a) */ |
Igor Opaniuk | f9b7fd2 | 2016-09-16 16:22:34 +0300 | [diff] [blame] | 413 | /* Get input data from /dev/urandom (-r) */ |
| 414 | int random_in = CRYPTO_USE_RANDOM; |
| 415 | /* Start with a 2-second busy loop (-w) */ |
| 416 | int warmup = CRYPTO_DEF_WARMUP; |
| 417 | int offset = 0; /* Buffer offset wrt. alloc'ed address (-u) */ |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 418 | |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 419 | /* Parse command line */ |
| 420 | for (i = 1; i < argc; i++) { |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 421 | if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 422 | usage(argv[0], algo, size, warmup, l, n); |
| 423 | return 0; |
| 424 | } |
| 425 | } |
| 426 | for (i = 1; i < argc; i++) { |
| 427 | if (!strcmp(argv[i], "-l")) { |
| 428 | NEXT_ARG(i); |
| 429 | l = atoi(argv[i]); |
| 430 | } else if (!strcmp(argv[i], "-a")) { |
| 431 | NEXT_ARG(i); |
| 432 | if (!strcasecmp(argv[i], "SHA1")) |
| 433 | algo = TA_SHA_SHA1; |
| 434 | else if (!strcasecmp(argv[i], "SHA224")) |
| 435 | algo = TA_SHA_SHA224; |
| 436 | else if (!strcasecmp(argv[i], "SHA256")) |
| 437 | algo = TA_SHA_SHA256; |
| 438 | else if (!strcasecmp(argv[i], "SHA384")) |
| 439 | algo = TA_SHA_SHA384; |
| 440 | else if (!strcasecmp(argv[i], "SHA512")) |
| 441 | algo = TA_SHA_SHA512; |
| 442 | else { |
| 443 | fprintf(stderr, "%s, invalid algorithm\n", |
| 444 | argv[0]); |
| 445 | usage(argv[0], algo, size, warmup, l, n); |
| 446 | return 1; |
| 447 | } |
| 448 | } else if (!strcmp(argv[i], "-n")) { |
| 449 | NEXT_ARG(i); |
| 450 | n = atoi(argv[i]); |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 451 | } else if (!strcmp(argv[i], "--random") || |
| 452 | !strcmp(argv[i], "-r")) { |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 453 | random_in = 1; |
| 454 | } else if (!strcmp(argv[i], "-s")) { |
| 455 | NEXT_ARG(i); |
| 456 | size = atoi(argv[i]); |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 457 | } else if (!strcmp(argv[i], "--unalign") || |
| 458 | !strcmp(argv[i], "-u")) { |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 459 | offset = 1; |
| 460 | } else if (!strcmp(argv[i], "-v")) { |
| 461 | verbosity++; |
Etienne Carriere | d0b3ee2 | 2017-05-03 18:20:59 +0200 | [diff] [blame^] | 462 | } else if (!strcmp(argv[i], "--warmup") || |
| 463 | !strcmp(argv[i], "-w")) { |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 464 | NEXT_ARG(i); |
| 465 | warmup = atoi(argv[i]); |
| 466 | } else { |
| 467 | fprintf(stderr, "%s: invalid argument: %s\n", |
| 468 | argv[0], argv[i]); |
| 469 | usage(argv[0], algo, size, warmup, l, n); |
| 470 | return 1; |
| 471 | } |
| 472 | } |
Etienne Carriere | c92b55f | 2017-03-24 10:28:09 +0100 | [diff] [blame] | 473 | |
Igor Opaniuk | 136644a | 2016-09-13 13:40:56 +0300 | [diff] [blame] | 474 | sha_perf_run_test(algo, size, n, l, random_in, offset, warmup, verbosity); |
| 475 | |
| 476 | return 0; |
| 477 | } |