xref: /openbmc/qemu/tests/unit/test-crypto-pbkdf.c (revision 44e21ef0)
1 /*
2  * QEMU Crypto cipher algorithms
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.1 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 "qapi/error.h"
23 #include "crypto/init.h"
24 #ifndef _WIN32
25 #include <sys/resource.h>
26 #endif
27 
28 #if defined(_WIN32) || defined(RUSAGE_THREAD) || defined(CONFIG_DARWIN)
29 #include "crypto/pbkdf.h"
30 
31 typedef struct QCryptoPbkdfTestData QCryptoPbkdfTestData;
32 struct QCryptoPbkdfTestData {
33     const char *path;
34     QCryptoHashAlgo hash;
35     unsigned int iterations;
36     const char *key;
37     size_t nkey;
38     const char *salt;
39     size_t nsalt;
40     const char *out;
41     size_t nout;
42     bool slow;
43 };
44 
45 /* This test data comes from cryptsetup package
46  *
47  *  $SRC/lib/crypto_backend/pbkdf2_generic.c
48  *
49  * under LGPLv2.1+ license
50  */
51 static QCryptoPbkdfTestData test_data[] = {
52     /* RFC 3962 test data */
53     {
54         .path = "/crypto/pbkdf/rfc3962/sha1/iter1",
55         .hash = QCRYPTO_HASH_ALGO_SHA1,
56         .iterations = 1,
57         .key = "password",
58         .nkey = 8,
59         .salt = "ATHENA.MIT.EDUraeburn",
60         .nsalt = 21,
61         .out = "\xcd\xed\xb5\x28\x1b\xb2\xf8\x01"
62                "\x56\x5a\x11\x22\xb2\x56\x35\x15"
63                "\x0a\xd1\xf7\xa0\x4b\xb9\xf3\xa3"
64                "\x33\xec\xc0\xe2\xe1\xf7\x08\x37",
65         .nout = 32
66     },
67     {
68         .path = "/crypto/pbkdf/rfc3962/sha1/iter2",
69         .hash = QCRYPTO_HASH_ALGO_SHA1,
70         .iterations = 2,
71         .key = "password",
72         .nkey = 8,
73         .salt = "ATHENA.MIT.EDUraeburn",
74         .nsalt = 21,
75         .out = "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e"
76                "\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
77                "\xa0\x53\x78\xb9\x32\x44\xec\x8f"
78                "\x48\xa9\x9e\x61\xad\x79\x9d\x86",
79         .nout = 32
80     },
81     {
82         .path = "/crypto/pbkdf/rfc3962/sha1/iter1200a",
83         .hash = QCRYPTO_HASH_ALGO_SHA1,
84         .iterations = 1200,
85         .key = "password",
86         .nkey = 8,
87         .salt = "ATHENA.MIT.EDUraeburn",
88         .nsalt = 21,
89         .out = "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e"
90                "\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
91                "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f"
92                "\x70\x8a\x31\xe2\xe6\x2b\x1e\x13",
93         .nout = 32
94     },
95     {
96         .path = "/crypto/pbkdf/rfc3962/sha1/iter5",
97         .hash = QCRYPTO_HASH_ALGO_SHA1,
98         .iterations = 5,
99         .key = "password",
100         .nkey = 8,
101         .salt = "\0224VxxV4\022", /* "\x1234567878563412 */
102         .nsalt = 8,
103         .out = "\xd1\xda\xa7\x86\x15\xf2\x87\xe6"
104                "\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
105                "\x3f\x98\xd2\x03\xe6\xbe\x49\xa6"
106                "\xad\xf4\xfa\x57\x4b\x6e\x64\xee",
107         .nout = 32
108     },
109     {
110         .path = "/crypto/pbkdf/rfc3962/sha1/iter1200b",
111         .hash = QCRYPTO_HASH_ALGO_SHA1,
112         .iterations = 1200,
113         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
114                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
115         .nkey = 64,
116         .salt = "pass phrase equals block size",
117         .nsalt = 29,
118         .out = "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b"
119                "\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
120                "\xc5\xec\x59\xf1\xa4\x52\xf5\xcc"
121                "\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1",
122         .nout = 32
123     },
124     {
125         .path = "/crypto/pbkdf/rfc3962/sha1/iter1200c",
126         .hash = QCRYPTO_HASH_ALGO_SHA1,
127         .iterations = 1200,
128         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
129                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
130         .nkey = 65,
131         .salt = "pass phrase exceeds block size",
132         .nsalt = 30,
133         .out = "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5"
134                "\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
135                "\x1a\x8b\x4d\x28\x26\x01\xdb\x3b"
136                "\x36\xbe\x92\x46\x91\x5e\xc8\x2a",
137         .nout = 32
138     },
139     {
140         .path = "/crypto/pbkdf/rfc3962/sha1/iter50",
141         .hash = QCRYPTO_HASH_ALGO_SHA1,
142         .iterations = 50,
143         .key = "\360\235\204\236", /* g-clef ("\xf09d849e) */
144         .nkey = 4,
145         .salt = "EXAMPLE.COMpianist",
146         .nsalt = 18,
147         .out = "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43"
148                "\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
149                "\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2"
150                "\x81\xff\x30\x69\xe1\xe9\x4f\x52",
151         .nout = 32
152     },
153 
154     /* RFC-6070 test data */
155     {
156         .path = "/crypto/pbkdf/rfc6070/sha1/iter1",
157         .hash = QCRYPTO_HASH_ALGO_SHA1,
158         .iterations = 1,
159         .key = "password",
160         .nkey = 8,
161         .salt = "salt",
162         .nsalt = 4,
163         .out = "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9"
164                "\xb5\x24\xaf\x60\x12\x06\x2f\xe0\x37\xa6",
165         .nout = 20
166     },
167     {
168         .path = "/crypto/pbkdf/rfc6070/sha1/iter2",
169         .hash = QCRYPTO_HASH_ALGO_SHA1,
170         .iterations = 2,
171         .key = "password",
172         .nkey = 8,
173         .salt = "salt",
174         .nsalt = 4,
175         .out = "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c\xcd\x1e"
176                "\xd9\x2a\xce\x1d\x41\xf0\xd8\xde\x89\x57",
177         .nout = 20
178     },
179     {
180         .path = "/crypto/pbkdf/rfc6070/sha1/iter4096",
181         .hash = QCRYPTO_HASH_ALGO_SHA1,
182         .iterations = 4096,
183         .key = "password",
184         .nkey = 8,
185         .salt = "salt",
186         .nsalt = 4,
187         .out = "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad"
188                "\x49\xd9\x26\xf7\x21\xd0\x65\xa4\x29\xc1",
189         .nout = 20
190     },
191     {
192         .path = "/crypto/pbkdf/rfc6070/sha1/iter16777216",
193         .hash = QCRYPTO_HASH_ALGO_SHA1,
194         .iterations = 16777216,
195         .key = "password",
196         .nkey = 8,
197         .salt = "salt",
198         .nsalt = 4,
199         .out = "\xee\xfe\x3d\x61\xcd\x4d\xa4\xe4\xe9\x94"
200                "\x5b\x3d\x6b\xa2\x15\x8c\x26\x34\xe9\x84",
201         .nout = 20,
202         .slow = true,
203     },
204     {
205         .path = "/crypto/pbkdf/rfc6070/sha1/iter4096a",
206         .hash = QCRYPTO_HASH_ALGO_SHA1,
207         .iterations = 4096,
208         .key = "passwordPASSWORDpassword",
209         .nkey = 24,
210         .salt = "saltSALTsaltSALTsaltSALTsaltSALTsalt",
211         .nsalt = 36,
212         .out = "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8"
213                "\xd8\x36\x62\xc0\xe4\x4a\x8b\x29\x1a\x96"
214                "\x4c\xf2\xf0\x70\x38",
215         .nout = 25
216     },
217     {
218         .path = "/crypto/pbkdf/rfc6070/sha1/iter4096b",
219         .hash = QCRYPTO_HASH_ALGO_SHA1,
220         .iterations = 4096,
221         .key = "pass\0word",
222         .nkey = 9,
223         .salt = "sa\0lt",
224         .nsalt = 5,
225         .out = "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37"
226                "\xd7\xf0\x34\x25\xe0\xc3",
227         .nout = 16
228     },
229 
230     /* non-RFC misc test data */
231     {
232         /* empty password test. */
233         .path = "/crypto/pbkdf/nonrfc/sha1/iter2",
234         .hash = QCRYPTO_HASH_ALGO_SHA1,
235         .iterations = 2,
236         .key = "",
237         .nkey = 0,
238         .salt = "salt",
239         .nsalt = 4,
240         .out = "\x13\x3a\x4c\xe8\x37\xb4\xd2\x52\x1e\xe2"
241                "\xbf\x03\xe1\x1c\x71\xca\x79\x4e\x07\x97",
242         .nout = 20
243     },
244     {
245         /* Password exceeds block size test */
246         .path = "/crypto/pbkdf/nonrfc/sha256/iter1200",
247         .hash = QCRYPTO_HASH_ALGO_SHA256,
248         .iterations = 1200,
249         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
250                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
251         .nkey = 65,
252         .salt = "pass phrase exceeds block size",
253         .nsalt = 30,
254         .out = "\x22\x34\x4b\xc4\xb6\xe3\x26\x75"
255                "\xa8\x09\x0f\x3e\xa8\x0b\xe0\x1d"
256                "\x5f\x95\x12\x6a\x2c\xdd\xc3\xfa"
257                "\xcc\x4a\x5e\x6d\xca\x04\xec\x58",
258         .nout = 32
259     },
260     {
261         .path = "/crypto/pbkdf/nonrfc/sha512/iter1200",
262         .hash = QCRYPTO_HASH_ALGO_SHA512,
263         .iterations = 1200,
264         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
265                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
266                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
267                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
268         .nkey = 129,
269         .salt = "pass phrase exceeds block size",
270         .nsalt = 30,
271         .out = "\x0f\xb2\xed\x2c\x0e\x6e\xfb\x7d"
272                "\x7d\x8e\xdd\x58\x01\xb4\x59\x72"
273                "\x99\x92\x16\x30\x5e\xa4\x36\x8d"
274                "\x76\x14\x80\xf3\xe3\x7a\x22\xb9",
275         .nout = 32
276     },
277     {
278         .path = "/crypto/pbkdf/nonrfc/sha224/iter1200",
279         .hash = QCRYPTO_HASH_ALGO_SHA224,
280         .iterations = 1200,
281         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
282                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
283                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
284                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
285         .nkey = 129,
286         .salt = "pass phrase exceeds block size",
287         .nsalt = 30,
288         .out = "\x13\x3b\x88\x0c\x0e\x52\xa2\x41"
289                "\x49\x33\x35\xa6\xc3\x83\xae\x23"
290                "\xf6\x77\x43\x9e\x5b\x30\x92\x3e"
291                "\x4a\x3a\xaa\x24\x69\x3c\xed\x20",
292         .nout = 32
293     },
294     {
295         .path = "/crypto/pbkdf/nonrfc/sha384/iter1200",
296         .hash = QCRYPTO_HASH_ALGO_SHA384,
297         .iterations = 1200,
298         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
299                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
300                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
301                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
302         .nkey = 129,
303         .salt = "pass phrase exceeds block size",
304         .nsalt = 30,
305         .out = "\xfe\xe3\xe1\x84\xc9\x25\x3e\x10"
306                "\x47\xc8\x7d\x53\xc6\xa5\xe3\x77"
307                "\x29\x41\x76\xbd\x4b\xe3\x9b\xac"
308                "\x05\x6c\x11\xdd\x17\xc5\x93\x80",
309         .nout = 32
310     },
311     {
312         .path = "/crypto/pbkdf/nonrfc/ripemd160/iter1200",
313         .hash = QCRYPTO_HASH_ALGO_RIPEMD160,
314         .iterations = 1200,
315         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
316                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
317                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
318                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
319         .nkey = 129,
320         .salt = "pass phrase exceeds block size",
321         .nsalt = 30,
322         .out = "\xd6\xcb\xd8\xa7\xdb\x0c\xa2\x2a"
323                "\x23\x5e\x47\xaf\xdb\xda\xa8\xef"
324                "\xe4\x01\x0d\x6f\xb5\x33\xc8\xbd"
325                "\xce\xbf\x91\x14\x8b\x5c\x48\x41",
326         .nout = 32
327     },
328 #ifdef CONFIG_CRYPTO_SM3
329     {
330         .path = "/crypto/pbkdf/nonrfc/sm3/iter2",
331         .hash = QCRYPTO_HASH_ALGO_SM3,
332         .iterations = 2,
333         .key = "password",
334         .nkey = 8,
335         .salt = "ATHENA.MIT.EDUraeburn",
336         .nsalt = 21,
337         .out = "\x48\x71\x1b\x58\xa3\xcb\xce\x06"
338  "\xba\xad\x77\xa8\xb5\xb9\xd8\x07"
339  "\x6a\xe2\xb3\x5b\x95\xce\xc8\xce"
340  "\xe7\xb1\xcb\xee\x61\xdf\x04\xea",
341         .nout = 32
342     },
343 #endif
344 #if 0
345     {
346         .path = "/crypto/pbkdf/nonrfc/whirlpool/iter1200",
347         .hash = QCRYPTO_HASH_ALGO_WHIRLPOOL,
348         .iterations = 1200,
349         .key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
350                "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
351         .nkey = 65,
352         .salt = "pass phrase exceeds block size",
353         .nsalt = 30,
354         .out = "\x9c\x1c\x74\xf5\x88\x26\xe7\x6a"
355                "\x53\x58\xf4\x0c\x39\xe7\x80\x89"
356                "\x07\xc0\x31\x19\x9a\x50\xa2\x48"
357                "\xf1\xd9\xfe\x78\x64\xe5\x84\x50",
358         .nout = 32
359     }
360 #endif
361 };
362 
363 
364 static inline char hex(int i)
365 {
366     if (i < 10) {
367         return '0' + i;
368     }
369     return 'a' + (i - 10);
370 }
371 
372 static char *hex_string(const uint8_t *bytes,
373                         size_t len)
374 {
375     char *hexstr = g_new0(char, len * 2 + 1);
376     size_t i;
377 
378     for (i = 0; i < len; i++) {
379         hexstr[i * 2] = hex((bytes[i] >> 4) & 0xf);
380         hexstr[i * 2 + 1] = hex(bytes[i] & 0xf);
381     }
382     hexstr[len * 2] = '\0';
383 
384     return hexstr;
385 }
386 
387 static void test_pbkdf(const void *opaque)
388 {
389     const QCryptoPbkdfTestData *data = opaque;
390     size_t nout = data->nout;
391     uint8_t *out = g_new0(uint8_t, nout);
392     gchar *expect, *actual;
393 
394     qcrypto_pbkdf2(data->hash,
395                    (uint8_t *)data->key, data->nkey,
396                    (uint8_t *)data->salt, data->nsalt,
397                    data->iterations,
398                    (uint8_t *)out, nout,
399                    &error_abort);
400 
401     expect = hex_string((const uint8_t *)data->out, data->nout);
402     actual = hex_string(out, nout);
403 
404     g_assert_cmpstr(actual, ==, expect);
405 
406     g_free(actual);
407     g_free(expect);
408     g_free(out);
409 }
410 
411 
412 static void test_pbkdf_timing_sha256(void)
413 {
414     uint8_t key[32];
415     uint8_t salt[32];
416     int iters;
417 
418     memset(key, 0x5d, sizeof(key));
419     memset(salt, 0x7c, sizeof(salt));
420 
421     iters = qcrypto_pbkdf2_count_iters(QCRYPTO_HASH_ALGO_SHA256,
422                                        key, sizeof(key),
423                                        salt, sizeof(salt),
424                                        32,
425                                        &error_abort);
426 
427     g_assert(iters >= (1 << 15));
428 }
429 
430 
431 int main(int argc, char **argv)
432 {
433     size_t i;
434 
435     g_test_init(&argc, &argv, NULL);
436 
437     g_assert(qcrypto_init(NULL) == 0);
438 
439     for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
440         if (!qcrypto_pbkdf2_supports(test_data[i].hash)) {
441             continue;
442         }
443 
444         if (!test_data[i].slow ||
445             g_test_slow()) {
446             g_test_add_data_func(test_data[i].path, &test_data[i], test_pbkdf);
447         }
448     }
449 
450     if (g_test_slow() && qcrypto_pbkdf2_supports(QCRYPTO_HASH_ALGO_SHA256)) {
451         g_test_add_func("/crypt0/pbkdf/timing/sha256", test_pbkdf_timing_sha256);
452     }
453 
454     return g_test_run();
455 }
456 #else
457 int main(int argc, char **argv)
458 {
459     g_test_init(&argc, &argv, NULL);
460     return g_test_run();
461 }
462 #endif
463