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