2 * PBKDF performance check
3 * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
4 * Copyright (C) 2012, Milan Broz
6 * This file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <sys/resource.h>
24 #include "crypto_backend.h"
26 static long time_ms(struct rusage *start, struct rusage *end)
30 ms = (end->ru_utime.tv_sec - start->ru_utime.tv_sec) * 1000;
31 ms += (end->ru_utime.tv_usec - start->ru_utime.tv_usec) / 1000;
33 if (crypt_backend_flags() & CRYPT_BACKEND_KERNEL) {
34 ms += (end->ru_stime.tv_sec - start->ru_stime.tv_sec) * 1000;
35 ms += (end->ru_stime.tv_usec - start->ru_stime.tv_usec) / 1000;
41 /* This code benchmarks PBKDF and returns iterations/second using specified hash */
42 int crypt_pbkdf_check(const char *kdf, const char *hash,
43 const char *password, size_t password_size,
44 const char *salt, size_t salt_size,
47 struct rusage rstart, rend;
51 unsigned int iterations;
58 if (getrusage(RUSAGE_SELF, &rstart) < 0)
61 r = crypt_pbkdf(kdf, hash, password, password_size, salt,
62 salt_size, &buf, 1, iterations);
66 if (getrusage(RUSAGE_SELF, &rend) < 0)
69 ms = time_ms(&rstart, &rend);
82 if (++step > 10 || !iterations)
87 *iter_secs = (iterations * 1000) / ms;