xref: /openbmc/qemu/crypto/pbkdf.c (revision 37788f25)
1 /*
2  * QEMU Crypto PBKDF support (Password-Based Key Derivation Function)
3  *
4  * Copyright (c) 2015-2016 Red Hat, Inc.
5  *
6  * This library 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 of the License, or (at your option) any later version.
10  *
11  * This library 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 library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #include "qemu/osdep.h"
22 #include "crypto/pbkdf.h"
23 #ifndef _WIN32
24 #include <sys/resource.h>
25 #endif
26 
27 
28 static int qcrypto_pbkdf2_get_thread_cpu(unsigned long long *val_ms,
29                                          Error **errp)
30 {
31 #ifdef _WIN32
32     FILETIME creation_time, exit_time, kernel_time, user_time;
33     ULARGE_INTEGER thread_time;
34 
35     if (!GetThreadTimes(GetCurrentThread(), &creation_time, &exit_time,
36                         &kernel_time, &user_time)) {
37         error_setg(errp, "Unable to get thread CPU usage");
38         return -1;
39     }
40 
41     thread_time.LowPart = user_time.dwLowDateTime;
42     thread_time.HighPart = user_time.dwHighDateTime;
43 
44     /* QuadPart is units of 100ns and we want ms as unit */
45     *val_ms = thread_time.QuadPart / 10000ll;
46     return 0;
47 #elif defined(RUSAGE_THREAD)
48     struct rusage ru;
49     if (getrusage(RUSAGE_THREAD, &ru) < 0) {
50         error_setg_errno(errp, errno, "Unable to get thread CPU usage");
51         return -1;
52     }
53 
54     *val_ms = ((ru.ru_utime.tv_sec * 1000ll) +
55                (ru.ru_utime.tv_usec / 1000));
56     return 0;
57 #else
58     *val_ms = 0;
59     error_setg(errp, "Unable to calculate thread CPU usage on this platform");
60     return -1;
61 #endif
62 }
63 
64 int qcrypto_pbkdf2_count_iters(QCryptoHashAlgorithm hash,
65                                const uint8_t *key, size_t nkey,
66                                const uint8_t *salt, size_t nsalt,
67                                Error **errp)
68 {
69     uint8_t out[32];
70     long long int iterations = (1 << 15);
71     unsigned long long delta_ms, start_ms, end_ms;
72 
73     while (1) {
74         if (qcrypto_pbkdf2_get_thread_cpu(&start_ms, errp) < 0) {
75             return -1;
76         }
77         if (qcrypto_pbkdf2(hash,
78                            key, nkey,
79                            salt, nsalt,
80                            iterations,
81                            out, sizeof(out),
82                            errp) < 0) {
83             return -1;
84         }
85         if (qcrypto_pbkdf2_get_thread_cpu(&end_ms, errp) < 0) {
86             return -1;
87         }
88 
89         delta_ms = end_ms - start_ms;
90 
91         if (delta_ms > 500) {
92             break;
93         } else if (delta_ms < 100) {
94             iterations = iterations * 10;
95         } else {
96             iterations = (iterations * 1000 / delta_ms);
97         }
98     }
99 
100     iterations = iterations * 1000 / delta_ms;
101 
102     if (iterations > INT32_MAX) {
103         error_setg(errp, "Iterations %lld too large for a 32-bit int",
104                    iterations);
105         return -1;
106     }
107 
108     return iterations;
109 }
110