Fix some problems found by Coverity scan.
[platform/upstream/cryptsetup.git] / lib / crypto_backend / pbkdf_check.c
1 /*
2  * PBKDF performance check
3  * Copyright (C) 2012, Red Hat, Inc. All rights reserved.
4  *
5  * This file is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This file is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this file; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <errno.h>
21 #include <sys/time.h>
22 #include <sys/resource.h>
23 #include "crypto_backend.h"
24
25 static long time_ms(struct rusage *start, struct rusage *end)
26 {
27         long ms;
28
29         ms = (end->ru_utime.tv_sec - start->ru_utime.tv_sec) * 1000;
30         ms += (end->ru_utime.tv_usec - start->ru_utime.tv_usec) / 1000;
31
32         if (crypt_backend_flags() & CRYPT_BACKEND_KERNEL) {
33                 ms += (end->ru_stime.tv_sec - start->ru_stime.tv_sec) * 1000;
34                 ms += (end->ru_stime.tv_usec - start->ru_stime.tv_usec) / 1000;
35         }
36
37         return ms;
38 }
39
40 /* This code benchmarks PBKDF and returns iterations/second using specified hash */
41 int crypt_pbkdf_check(const char *kdf, const char *hash,
42                       const char *password, size_t password_size,
43                       const char *salt, size_t salt_size,
44                       uint64_t *iter_secs)
45 {
46         struct rusage rstart, rend;
47         int r = 0, step = 0;
48         long ms = 0;
49         char buf;
50         unsigned int iterations;
51
52         if (!kdf || !hash)
53                 return -EINVAL;
54
55         iterations = 1 << 15;
56         while (ms < 500) {
57                 if (getrusage(RUSAGE_SELF, &rstart) < 0)
58                         return -EINVAL;
59
60                 r = crypt_pbkdf(kdf, hash, password, password_size, salt,
61                                 salt_size, &buf, 1, iterations);
62                 if (r < 0)
63                         return r;
64
65                 if (getrusage(RUSAGE_SELF, &rend) < 0)
66                         return -EINVAL;
67
68                 ms = time_ms(&rstart, &rend);
69                 if (ms > 500)
70                         break;
71
72                 if (ms <= 62)
73                         iterations <<= 4;
74                 else if (ms <= 125)
75                         iterations <<= 3;
76                 else if (ms <= 250)
77                         iterations <<= 2;
78                 else
79                         iterations <<= 1;
80
81                 if (++step > 10 || !iterations)
82                         return -EINVAL;
83         }
84
85         if (iter_secs)
86                 *iter_secs = (iterations * 1000) / ms;
87         return r;
88 }