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