Imported Upstream version 1.6.7
[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  * Copyright (C) 2012-2014, Milan Broz
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <errno.h>
22 #include <sys/time.h>
23 #include <sys/resource.h>
24 #include "crypto_backend.h"
25
26 static long time_ms(struct rusage *start, struct rusage *end)
27 {
28         int count_kernel_time = 0;
29         long ms;
30
31         if (crypt_backend_flags() & CRYPT_BACKEND_KERNEL)
32                 count_kernel_time = 1;
33
34         /*
35          * FIXME: if there is no self usage info, count system time.
36          * This seem like getrusage() bug in some hypervisors...
37          */
38         if (!end->ru_utime.tv_sec && !start->ru_utime.tv_sec &&
39             !end->ru_utime.tv_usec && !start->ru_utime.tv_usec)
40                 count_kernel_time = 1;
41
42         ms = (end->ru_utime.tv_sec - start->ru_utime.tv_sec) * 1000;
43         ms += (end->ru_utime.tv_usec - start->ru_utime.tv_usec) / 1000;
44
45         if (count_kernel_time) {
46                 ms += (end->ru_stime.tv_sec - start->ru_stime.tv_sec) * 1000;
47                 ms += (end->ru_stime.tv_usec - start->ru_stime.tv_usec) / 1000;
48         }
49
50         return ms;
51 }
52
53 /* This code benchmarks PBKDF and returns iterations/second using specified hash */
54 int crypt_pbkdf_check(const char *kdf, const char *hash,
55                       const char *password, size_t password_size,
56                       const char *salt, size_t salt_size,
57                       uint64_t *iter_secs)
58 {
59         struct rusage rstart, rend;
60         int r = 0, step = 0;
61         long ms = 0;
62         char buf;
63         unsigned int iterations;
64
65         if (!kdf || !hash)
66                 return -EINVAL;
67
68         iterations = 1 << 15;
69         while (ms < 500) {
70                 if (getrusage(RUSAGE_SELF, &rstart) < 0)
71                         return -EINVAL;
72
73                 r = crypt_pbkdf(kdf, hash, password, password_size, salt,
74                                 salt_size, &buf, 1, iterations);
75                 if (r < 0)
76                         return r;
77
78                 if (getrusage(RUSAGE_SELF, &rend) < 0)
79                         return -EINVAL;
80
81                 ms = time_ms(&rstart, &rend);
82                 if (ms > 500)
83                         break;
84
85                 if (ms <= 62)
86                         iterations <<= 4;
87                 else if (ms <= 125)
88                         iterations <<= 3;
89                 else if (ms <= 250)
90                         iterations <<= 2;
91                 else
92                         iterations <<= 1;
93
94                 if (++step > 10 || !iterations)
95                         return -EINVAL;
96         }
97
98         if (iter_secs)
99                 *iter_secs = (iterations * 1000) / ms;
100         return r;
101 }