usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-gc-pbkdf2.c
blob8afb0efeeccd0055f14a3c9a726ed796705dd310
1 /*
2 * Copyright (C) 2002-2005, 2007, 2010-2024 Free Software Foundation, Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3, or (at your option)
7 * any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Simon Josefsson. */
19 #include <config.h>
21 #include "gc.h"
23 #include <stdio.h>
24 #include <string.h>
26 /* Test vectors from RFC 3962. */
28 #define G_CLEF "\xF0\x9D\x84\x9E"
30 struct pkcs5
32 Gc_hash hash;
33 int iterations;
34 const char *password;
35 const char *salt;
36 int dklen;
37 const char *expected;
39 const struct pkcs5 pkcs5[] = {
40 #if GNULIB_GC_HMAC_SHA1
41 {GC_SHA1, 1, "password", "ATHENA.MIT.EDUraeburn", 16,
42 "\xCD\xED\xB5\x28\x1B\xB2\xF8\x01\x56\x5A\x11\x22\xB2\x56\x35\x15"},
43 {GC_SHA1, 2, "password", "ATHENA.MIT.EDUraeburn", 16,
44 "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d"},
45 {GC_SHA1, 2, "password", "ATHENA.MIT.EDUraeburn", 32,
46 "\x01\xdb\xee\x7f\x4a\x9e\x24\x3e\x98\x8b\x62\xc7\x3c\xda\x93\x5d"
47 "\xa0\x53\x78\xb9\x32\x44\xec\x8f\x48\xa9\x9e\x61\xad\x79\x9d\x86"},
48 {GC_SHA1, 1200, "password", "ATHENA.MIT.EDUraeburn", 16,
49 "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"},
50 {GC_SHA1, 1200, "password", "ATHENA.MIT.EDUraeburn", 32,
51 "\x5c\x08\xeb\x61\xfd\xf7\x1e\x4e\x4e\xc3\xcf\x6b\xa1\xf5\x51\x2b"
52 "\xa7\xe5\x2d\xdb\xc5\xe5\x14\x2f\x70\x8a\x31\xe2\xe6\x2b\x1e\x13"},
53 {GC_SHA1, 5, "password", "\x12\x34\x56\x78\x78\x56\x34\x12\x00", 16,
54 "\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"},
55 {GC_SHA1, 5, "password", "\x12\x34\x56\x78\x78\x56\x34\x12\x00", 32,
56 "\xd1\xda\xa7\x86\x15\xf2\x87\xe6\xa1\xc8\xb1\x20\xd7\x06\x2a\x49"
57 "\x3f\x98\xd2\x03\xe6\xbe\x49\xa6\xad\xf4\xfa\x57\x4b\x6e\x64\xee"},
58 {GC_SHA1, 1200,
59 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
60 "pass phrase equals block size", 16,
61 "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"},
62 {GC_SHA1, 1200,
63 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
64 "pass phrase equals block size", 32,
65 "\x13\x9c\x30\xc0\x96\x6b\xc3\x2b\xa5\x5f\xdb\xf2\x12\x53\x0a\xc9"
66 "\xc5\xec\x59\xf1\xa4\x52\xf5\xcc\x9a\xd9\x40\xfe\xa0\x59\x8e\xd1"},
67 {GC_SHA1, 1200,
68 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
69 "pass phrase exceeds block size", 16,
70 "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61"},
71 {GC_SHA1, 1200,
72 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
73 "pass phrase exceeds block size", 32,
74 "\x9c\xca\xd6\xd4\x68\x77\x0c\xd5\x1b\x10\xe6\xa6\x87\x21\xbe\x61"
75 "\x1a\x8b\x4d\x28\x26\x01\xdb\x3b\x36\xbe\x92\x46\x91\x5e\xc8\x2a"},
76 {GC_SHA1, 50, G_CLEF "\x00", "EXAMPLE.COMpianist", 16,
77 "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"},
78 {GC_SHA1, 50, G_CLEF "\x00", "EXAMPLE.COMpianist", 32,
79 "\x6b\x9c\xf2\x6d\x45\x45\x5a\x43\xa5\xb8\xbb\x27\x6a\x40\x3b\x39"
80 "\xe7\xfe\x37\xa0\xc4\x1e\x02\xc2\x81\xff\x30\x69\xe1\xe9\x4f\x52"},
81 {GC_SHA1, 500,
82 "All n-entities must communicate with other n-entities via n-1 "
83 "entiteeheehees", "\x12\x34\x56\x78\x78\x56\x34\x12\x00", 16,
84 "\x6A\x89\x70\xBF\x68\xC9\x2C\xAE\xA8\x4A\x8D\xF2\x85\x10\x85\x86"},
85 #endif
86 #if GNULIB_GC_HMAC_SHA256
87 {GC_SHA256, 4096, "pencil",
88 "\x5b\x6d\x99\x68\x9d\x12\x35\x8e\xec\xa0\x4b\x14\x12\x36\xfa\x81",
89 32,
90 "\xc4\xa4\x95\x10\x32\x3a\xb4\xf9\x52\xca\xc1\xfa\x99\x44\x19\x39"
91 "\xe7\x8e\xa7\x4d\x6b\xe8\x1d\xdf\x70\x96\xe8\x75\x13\xdc\x61\x5d"}
92 #endif
95 int
96 main (int argc, char *argv[])
98 size_t i;
99 int rc;
100 char out[BUFSIZ];
102 rc = gc_pbkdf2_hmac (GC_MD2, "p", 1, "s", 1, 42, out, 17);
104 if (rc != GC_INVALID_HASH)
106 printf ("PBKDF2 accepts invalid MD2 hash %d\n", rc);
107 return 1;
110 for (i = 0; i < sizeof (pkcs5) / sizeof (pkcs5[0]); i++)
112 rc = gc_pbkdf2_hmac (pkcs5[i].hash,
113 pkcs5[i].password, strlen (pkcs5[i].password),
114 pkcs5[i].salt, strlen (pkcs5[i].salt),
115 pkcs5[i].iterations, out, pkcs5[i].dklen);
116 if (rc != GC_OK)
118 printf ("PKCS5 entry %ld failed fatally: %d\n",
119 (unsigned long) i, rc);
120 return 1;
123 if (memcmp (pkcs5[i].expected, out, pkcs5[i].dklen) != 0)
125 printf ("PKCS5 entry %ld failed\n", (unsigned long) i);
126 return 1;
130 return 0;