Coverage Report

Created: 2026-08-30 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptsetup/lib/utils_benchmark.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * libcryptsetup - cryptsetup library, cipher benchmark
4
 *
5
 * Copyright (C) 2012-2026 Red Hat, Inc. All rights reserved.
6
 * Copyright (C) 2012-2026 Milan Broz
7
 */
8
9
#include <stdlib.h>
10
#include <errno.h>
11
12
#include "internal.h"
13
14
int crypt_benchmark(struct crypt_device *cd,
15
  const char *cipher,
16
  const char *cipher_mode,
17
  size_t volume_key_size,
18
  size_t iv_size,
19
  size_t buffer_size,
20
  double *encryption_mbs,
21
  double *decryption_mbs)
22
0
{
23
0
  void *buffer = NULL;
24
0
  char *iv = NULL, *key = NULL, mode[MAX_CIPHER_LEN], *c;
25
0
  int r;
26
27
0
  if (!cipher || !cipher_mode || !volume_key_size || !encryption_mbs || !decryption_mbs)
28
0
    return -EINVAL;
29
30
0
  r = init_crypto(cd);
31
0
  if (r < 0)
32
0
    return r;
33
34
0
  r = -ENOMEM;
35
0
  if (posix_memalign(&buffer, crypt_getpagesize(), buffer_size))
36
0
    goto out;
37
0
  memset(buffer, 0, buffer_size);
38
39
0
  r = crypt_cipher_ivsize(cipher, cipher_mode);
40
0
  if (r >= 0 && iv_size != (size_t)r) {
41
0
    log_dbg(cd, "IV length for benchmark adjusted to %i bytes (requested %zu).", r, iv_size);
42
0
    iv_size = r;
43
0
  }
44
45
0
  if (iv_size) {
46
0
    iv = malloc(iv_size);
47
0
    if (!iv)
48
0
      goto out;
49
0
    r = crypt_random_get(cd, iv, iv_size, CRYPT_RND_NORMAL);
50
0
    if (r < 0)
51
0
      goto out;
52
0
  }
53
54
0
  key = malloc(volume_key_size);
55
0
  if (!key)
56
0
    goto out;
57
58
0
  r = crypt_random_get(cd, key, volume_key_size, CRYPT_RND_NORMAL);
59
0
  if (r < 0)
60
0
    goto out;
61
62
0
  strncpy(mode, cipher_mode, sizeof(mode)-1);
63
  /* Ignore IV generator */
64
0
  if ((c  = strchr(mode, '-')))
65
0
    *c = '\0';
66
67
0
  r = crypt_cipher_perf_kernel(cipher, cipher_mode, buffer, buffer_size, key, volume_key_size,
68
0
             iv, iv_size, encryption_mbs, decryption_mbs);
69
70
0
  if (r == -ERANGE)
71
0
    log_dbg(cd, "Measured cipher runtime is too low.");
72
0
  else if (r)
73
0
    log_dbg(cd, "Cannot initialize cipher %s, mode %s, key size %zu, IV size %zu.",
74
0
      cipher, cipher_mode, volume_key_size, iv_size);
75
0
out:
76
0
  free(buffer);
77
0
  free(key);
78
0
  free(iv);
79
80
0
  return r;
81
0
}
82
83
int crypt_benchmark_pbkdf(struct crypt_device *cd,
84
  struct crypt_pbkdf_type *pbkdf,
85
  const char *password,
86
  size_t password_size,
87
  const char *salt,
88
  size_t salt_size,
89
  size_t volume_key_size,
90
  int (*progress)(uint32_t time_ms, void *usrptr),
91
  void *usrptr)
92
0
{
93
0
  int r, priority;
94
0
  const char *kdf_opt;
95
0
  uint32_t memory_kb;
96
97
0
  if (!pbkdf || (!password && password_size))
98
0
    return -EINVAL;
99
100
0
  r = init_crypto(cd);
101
0
  if (r < 0)
102
0
    return r;
103
104
0
  kdf_opt = !strcmp(pbkdf->type, CRYPT_KDF_PBKDF2) ? pbkdf->hash : "";
105
106
0
  log_dbg(cd, "Running %s(%s) benchmark.", pbkdf->type, kdf_opt);
107
108
0
  memory_kb = pbkdf_adjusted_phys_memory_kb();
109
0
  if (memory_kb < pbkdf->max_memory_kb) {
110
0
    log_dbg(cd, "Not enough physical memory detected, "
111
0
      "PBKDF max memory decreased from %dkB to %dkB.",
112
0
      pbkdf->max_memory_kb, memory_kb);
113
0
    pbkdf->max_memory_kb = memory_kb;
114
0
  }
115
116
0
  crypt_process_priority(cd, &priority, true);
117
0
  r = crypt_pbkdf_perf(pbkdf->type, pbkdf->hash, password, password_size,
118
0
           salt, salt_size, volume_key_size, pbkdf->time_ms,
119
0
           pbkdf->max_memory_kb, pbkdf->parallel_threads,
120
0
           &pbkdf->iterations, &pbkdf->max_memory_kb, progress, usrptr);
121
0
  crypt_process_priority(cd, &priority, false);
122
123
0
  if (!r)
124
0
    log_dbg(cd, "Benchmark returns %s(%s) %u iterations, %u memory, %u threads (for %zu-bits key).",
125
0
      pbkdf->type, kdf_opt, pbkdf->iterations, pbkdf->max_memory_kb,
126
0
      pbkdf->parallel_threads, volume_key_size * 8);
127
0
  return r;
128
0
}
129
130
struct benchmark_usrptr {
131
  struct crypt_device *cd;
132
  struct crypt_pbkdf_type *pbkdf;
133
};
134
135
static int benchmark_callback(uint32_t time_ms, void *usrptr)
136
0
{
137
0
  struct benchmark_usrptr *u = usrptr;
138
139
0
  log_dbg(u->cd, "PBKDF benchmark: memory cost = %u, iterations = %u, "
140
0
    "threads = %u (took %u ms)", u->pbkdf->max_memory_kb,
141
0
    u->pbkdf->iterations, u->pbkdf->parallel_threads, time_ms);
142
143
0
  return 0;
144
0
}
145
146
/*
147
 * Used in internal places to benchmark crypt_device context PBKDF.
148
 * Once requested parameters are benchmarked, iterations attribute is set,
149
 * and the benchmarked values can be reused.
150
 * Note that memory cost can be changed after benchmark (if used).
151
 * NOTE: You need to check that you are benchmarking for the same key size.
152
 */
153
int crypt_benchmark_pbkdf_internal(struct crypt_device *cd,
154
           struct crypt_pbkdf_type *pbkdf,
155
           size_t volume_key_size)
156
0
{
157
0
  struct crypt_pbkdf_limits pbkdf_limits;
158
0
  double PBKDF2_tmp;
159
0
  uint32_t ms_tmp;
160
0
  int r = -EINVAL;
161
0
  struct benchmark_usrptr u = {
162
0
    .cd = cd,
163
0
    .pbkdf = pbkdf
164
0
  };
165
166
0
  r = crypt_pbkdf_get_limits(pbkdf->type, &pbkdf_limits);
167
0
  if (r)
168
0
    return r;
169
170
0
  if (pbkdf->flags & CRYPT_PBKDF_NO_BENCHMARK) {
171
0
    if (pbkdf->iterations) {
172
0
      log_dbg(cd, "Reusing PBKDF values (no benchmark flag is set).");
173
0
      return 0;
174
0
    }
175
0
    log_err(cd, _("PBKDF benchmark disabled but iterations not set."));
176
0
    return -EINVAL;
177
0
  }
178
179
  /* For PBKDF2 run benchmark always. Also note it depends on volume_key_size! */
180
0
  if (!strcmp(pbkdf->type, CRYPT_KDF_PBKDF2)) {
181
    /*
182
     * For PBKDF2 it is enough to run benchmark for only 1 second
183
     * and interpolate final iterations value from it.
184
     */
185
0
    ms_tmp = pbkdf->time_ms;
186
0
    pbkdf->time_ms = 1000;
187
0
    pbkdf->parallel_threads = 0; /* N/A in PBKDF2 */
188
0
    pbkdf->max_memory_kb = 0; /* N/A in PBKDF2 */
189
190
0
    r = crypt_benchmark_pbkdf(cd, pbkdf, "foobarfo", 8, "01234567890abcdef", 16,
191
0
          volume_key_size, &benchmark_callback, &u);
192
0
    pbkdf->time_ms = ms_tmp;
193
0
    if (r < 0) {
194
0
      log_err(cd, _("Not compatible PBKDF2 options (using hash algorithm %s)."),
195
0
        pbkdf->hash);
196
0
      return r;
197
0
    }
198
199
0
    PBKDF2_tmp = ((double)pbkdf->iterations * pbkdf->time_ms / 1000.);
200
0
    if (PBKDF2_tmp > (double)UINT32_MAX)
201
0
      return -EINVAL;
202
0
    pbkdf->iterations = AT_LEAST((uint32_t)PBKDF2_tmp, pbkdf_limits.min_iterations);
203
0
  } else {
204
    /* Already benchmarked */
205
0
    if (pbkdf->iterations) {
206
0
      log_dbg(cd, "Reusing PBKDF values.");
207
0
      return 0;
208
0
    }
209
210
0
    r = crypt_benchmark_pbkdf(cd, pbkdf, "foobarfo", 8,
211
0
      "0123456789abcdef0123456789abcdef", 32,
212
0
      volume_key_size, &benchmark_callback, &u);
213
0
    if (r < 0)
214
0
      log_err(cd, _("Not compatible PBKDF options."));
215
0
  }
216
217
0
  return r;
218
0
}