2 * Cipher performance check
4 * Copyright (C) 2018-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2018-2021 Milan Broz
7 * This file is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This file is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this file; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "crypto_backend_internal.h"
26 #ifndef CLOCK_MONOTONIC_RAW
27 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
31 * This is not simulating storage, so using disk block causes extreme overhead.
32 * Let's use some fixed block size where results are more reliable...
34 #define CIPHER_BLOCK_BYTES 65536
37 * If the measured value is lower, encrypted buffer is probably too small
38 * and calculated values are not reliable.
40 #define CIPHER_TIME_MIN_MS 0.001
43 * The whole test depends on Linux kernel usermode crypto API for now.
44 * (The same implementations are used in dm-crypt though.)
47 static int time_ms(struct timespec *start, struct timespec *end, double *ms)
49 double start_ms, end_ms;
51 start_ms = start->tv_sec * 1000.0 + start->tv_nsec / (1000.0 * 1000);
52 end_ms = end->tv_sec * 1000.0 + end->tv_nsec / (1000.0 * 1000);
54 *ms = end_ms - start_ms;
58 static int cipher_perf_one(const char *name, const char *mode, char *buffer, size_t buffer_size,
59 const char *key, size_t key_size, const char *iv, size_t iv_size, int enc)
61 struct crypt_cipher_kernel cipher;
62 size_t done = 0, block = CIPHER_BLOCK_BYTES;
65 if (buffer_size < block)
68 r = crypt_cipher_init_kernel(&cipher, name, mode, key, key_size);
72 while (done < buffer_size) {
73 if ((done + block) > buffer_size)
74 block = buffer_size - done;
77 r = crypt_cipher_encrypt_kernel(&cipher, &buffer[done], &buffer[done],
80 r = crypt_cipher_decrypt_kernel(&cipher, &buffer[done], &buffer[done],
88 crypt_cipher_destroy_kernel(&cipher);
92 static int cipher_measure(const char *name, const char *mode, char *buffer, size_t buffer_size,
93 const char *key, size_t key_size, const char *iv, size_t iv_size,
94 int encrypt, double *ms)
96 struct timespec start, end;
100 * Using getrusage would be better here but the precision
101 * is not adequate, so better stick with CLOCK_MONOTONIC
103 if (clock_gettime(CLOCK_MONOTONIC_RAW, &start) < 0)
106 r = cipher_perf_one(name, mode, buffer, buffer_size, key, key_size, iv, iv_size, encrypt);
110 if (clock_gettime(CLOCK_MONOTONIC_RAW, &end) < 0)
113 r = time_ms(&start, &end, ms);
117 if (*ms < CIPHER_TIME_MIN_MS)
123 static double speed_mbs(unsigned long bytes, double ms)
125 double speed = bytes, s = ms / 1000.;
127 return speed / (1024 * 1024) / s;
130 int crypt_cipher_perf_kernel(const char *name, const char *mode, char *buffer, size_t buffer_size,
131 const char *key, size_t key_size, const char *iv, size_t iv_size,
132 double *encryption_mbs, double *decryption_mbs)
134 double ms_enc, ms_dec, ms;
135 int r, repeat_enc, repeat_dec;
139 while (ms_enc < 1000.0) {
140 r = cipher_measure(name, mode, buffer, buffer_size, key, key_size, iv, iv_size, 1, &ms);
149 while (ms_dec < 1000.0) {
150 r = cipher_measure(name, mode, buffer, buffer_size, key, key_size, iv, iv_size, 0, &ms);
157 *encryption_mbs = speed_mbs(buffer_size * repeat_enc, ms_enc);
158 *decryption_mbs = speed_mbs(buffer_size * repeat_dec, ms_dec);