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