xref: /openbsd-src/regress/lib/libutil/bcrypt_pbkdf/bcrypt_pbkdf_test.c (revision c4607e8b4231d314247f2ed23b5e951a0edecd80)
1 #include <inttypes.h>
2 #include <stdio.h>
3 
4 #include <util.h>
5 
6 struct test {
7 	int rounds;
8 	size_t passlen;
9 	const char *password;
10 	size_t saltlen;
11 	const uint8_t *salt;
12 	size_t keylen;
13 	const uint8_t *key;
14 };
15 
16 struct test tests[] = {
17 	/* basic */
18 	{ 4, 8, "password", 4, "salt", 32,
19 	"\xbe\x1e\xd7\x80\x31\xfc\x18\xd2\x4b\xea\x3b\xea\x9f\x80\x5e\x71"
20 	"\xc5\xb9\xd2\x02\x7b\x5f\x25\xea\x9c\x45\x3b\x75\xe7\x4b\x87\x9f"
21 	},
22 	{ 4, 8, "password", 1, "", 16,
23 	"\x97\xf7\xb6\x13\x7b\x02\xf7\x8f\x69\x39\x2e\x28\xff\x44\x72\xa6"
24 	},
25 	{ 4, 1, "", 4, "salt", 16,
26 	"\x81\xcf\xc7\x92\x3e\x71\xb2\x19\x08\x5e\x1f\xc9\x36\x23\x0e\x43"
27 	},
28 	/* nul bytes in password and string */
29 	{ 4, 9, "password", 5, "salt", 32,
30 	"\x20\xcc\x6c\xed\x16\x38\x76\xb6\x04\x98\x9e\x43\xa4\xff\x10\x3e"
31 	"\xbd\xa3\x9f\x30\x84\x0a\x66\x18\x3d\xa8\x82\xe8\x27\xa8\x01\xc1"
32 	},
33 	{ 4, 8, "pass\0word", 4, "sa\0lt", 16,
34 	"\xd6\xc3\xca\x46\x62\x45\x53\xaf\xfe\x75\xf1\xaf\x52\xdd\xeb\x39"
35 	},
36 	{ 4, 9, "pass\0word", 5, "sa\0lt", 16,
37 	"\x72\x65\x56\x6e\xd2\x40\x00\x4c\x6d\x86\x05\xd4\x94\x80\x0a\x42"
38 	},
39 	/* bigger key */
40 	{ 8, 8, "password", 4, "salt", 64,
41 	"\x89\x33\xf5\xab\x6b\x2a\xde\x1b\x8f\x09\x6a\x4e\x41\xbc\x40\x21"
42 	"\x70\x1a\xfb\xe3\xc3\x5e\x39\x93\x1c\xb1\x27\xd3\xb7\x68\xf4\x71"
43 	"\x69\x93\x6b\xd7\x3e\xdc\x00\x07\x20\xcf\x26\xa0\x71\x98\xac\x1c"
44 	"\xf0\x06\x26\x64\xf3\x2b\x10\x43\x29\xdb\x88\x0a\xb5\x93\x06\x6f"
45 	},
46 	/* more rounds */
47 	{ 42, 8, "password", 4, "salt", 16,
48 	"\x0d\xb3\xac\x94\xb3\xee\x53\x28\x4f\x4a\x22\x89\x3b\x3c\x24\xae"
49 	},
50 	/* longer password */
51 	{ 8, 446,
52 	"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do "
53 	"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
54 	"enim ad minim veniam, quis nostrud exercitation ullamco laboris "
55 	"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor "
56 	"in reprehenderit in voluptate velit esse cillum dolore eu fugiat "
57 	"nulla pariatur. Excepteur sint occaecat cupidatat non proident, "
58 	"sunt in culpa qui officia deserunt mollit anim id est laborum.",
59 	6, "salis", 16,
60 	"\xf5\xfc\xa8\x06\x4e\x92\xf5\xf0\x37\xd7\x7e\x05\xb7\x80\x2f\x96"
61 	},
62 	/* "unicode" */
63 	{ 8,
64 	16, "\x0d\xb3\xac\x94\xb3\xee\x53\x28\x4f\x4a\x22\x89\x3b\x3c\x24\xae",
65 	16, "\x3a\x62\xf0\xf0\xdb\xce\xf8\x23\xcf\xcc\x85\x48\x56\xea\x10\x28",
66 	16, "\x05\xbc\x80\xc5\x1f\x3c\x95\x77\x71\x72\xef\x43\x27\xef\x03\xd2"
67 	},
68 	/* very large key */
69 	{ 8,
70 	16, "\x0d\xb3\xac\x94\xb3\xee\x53\x28\x4f\x4a\x22\x89\x3b\x3c\x24\xae",
71 	16, "\x3a\x62\xf0\xf0\xdb\xce\xf8\x23\xcf\xcc\x85\x48\x56\xea\x10\x28",
72 	256,
73 	"\x05\xeb\x9b\x47\xbd\x01\x70\x21\xbc\x98\xfc\x99\x5b\x40\x57\x95"
74 	"\x80\xa5\xa5\x0b\x33\x76\xa0\x45\xc5\x83\xb9\xb8\x12\x6f\x8e\x15"
75 	"\x1f\xd6\xd0\x0f\x8f\x34\xa4\x1f\x3c\x3d\x68\x2c\xca\xeb\xa6\x48"
76 	"\x95\xba\x46\xb6\x62\xd1\xf5\xf2\x77\x97\x7e\xab\x2a\x53\xeb\x32"
77 	"\x71\x55\xf4\x6a\x77\x51\xa1\x94\x72\xdb\x98\x85\xb7\x6d\x48\x19"
78 	"\xef\x34\xaf\x62\xd3\x03\x14\xe9\x43\x59\xec\x6f\x91\xb0\x62\xda"
79 	"\x27\xa1\x12\xe1\xa1\xc4\x3d\x2d\xef\xeb\xe2\x5d\xb7\xf3\x2e\x74"
80 	"\x03\xce\x8f\xb6\xba\xec\x92\x83\xd2\x16\xfa\x2e\x36\x3b\x25\xc6"
81 	"\xc7\x93\x2f\xe3\xf4\x67\x2a\x06\x26\x74\xec\x4b\x09\x34\x7f\x5f"
82 	"\xff\xe4\xfe\xe0\x23\x42\xe0\x8d\xa7\x97\xca\xe2\xc6\xe8\x93\xd1"
83 	"\x66\x9d\x2d\x6c\xa1\x16\xb3\xf9\x2d\x89\x64\x71\x5d\x8a\xc4\x2f"
84 	"\xcc\x57\xfe\x0a\x0d\x56\x5e\xd0\x75\x0e\x91\x86\x27\xf7\xcb\xd6"
85 	"\x11\x87\xcf\xd8\x3d\xca\x4b\xc4\x7b\x9e\xf4\x3b\x61\xec\xa8\xb3"
86 	"\xbe\xaf\xd9\x82\xdf\x31\xe5\xba\xa7\x14\x5b\xd8\xf0\xda\x15\x13"
87 	"\xed\x0a\xe1\x69\xc8\x84\x59\x5e\x6a\x1d\x59\x13\x53\x5b\x03\xe2"
88 	"\xb7\x0c\xe1\xb3\xd9\x89\x9f\x57\x31\x2e\xd9\x52\x79\x1e\xb4\x58"
89 	},
90 };
91 
92 void
93 printkey(uint8_t *key, size_t keylen)
94 {
95 	int k;
96 
97 	for (k = 0; k < keylen; k++) {
98 		printf("\\x%.2x", key[k]);
99 		if (k % 16 == 15)
100 			printf("\n");
101 	}
102 	printf("\n");
103 }
104 
105 int
106 main()
107 {
108 	uint8_t key[1024];
109 	char *password = "password";
110 	char *salt = "salt";
111 	int i, fails;
112 	struct test *t;
113 
114 	fails = 0;
115 	for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
116 		t = &tests[i];
117 		bcrypt_pbkdf(t->password, t->passlen, t->salt, t->saltlen,
118 		    key, t->keylen, t->rounds);
119 		if (memcmp(key, t->key, t->keylen) != 0) {
120 			printf("test %d FAILED\n", i);
121 			printf("expected:\n");
122 			printkey(t->key, t->keylen);
123 			printf("result:\n");
124 			printkey(key, t->keylen);
125 			fails++;
126 		}
127 	}
128 	return fails;
129 }
130