Debian Codegraph @

test/gensalt-extradata.c

raw
1 /* Test that the prefix argument to crypt_gensalt affects only the
2 choice of hashing method, not any of the parameters or the salt.
3
4 Written by Zack Weinberg <zackw at panix.com> in 2018.
5 To the extent possible under law, Zack Weinberg has waived all
6 copyright and related or neighboring rights to this work.
7
8 See https://creativecommons.org/publicdomain/zero/1.0/ for further
9 details. */
10
11 #include "crypt-port.h"
12
13 #include <errno.h>
14 #include <stdio.h>
15
16 /* Random bytes used when calling crypt_gensalt; for determinism, these
17 are fixed from run to run. */
18 #define N_RBYTES 64ul
19
20 static const char rbytes1[] =
21 "90idUkI2+mu2E/tMTViD418j2sPdEYq9LYq0yRW7RYhr4RqQ+oVzIIEcfJBqpf/D";
22
23 static const char rbytes2[] =
24 "sEwXQxrjBTEADauxCpvOQqq7iU9oq6uJ+Iux/fbhtLRj1MWgBFyo/t+nh/nzm0Kn";
25
26 static_assert(sizeof rbytes1 == N_RBYTES + 1, "rbytes1 is wrong length");
27 static_assert(sizeof rbytes2 == N_RBYTES + 1, "rbytes2 is wrong length");
28
29 struct testcase
30 {
31 const char *prefix;
32 unsigned long count1;
33 unsigned long count2;
34 };
35
36 /* This list should include one entry for each potentially-supported
37 hash prefix. If the hash method has tunable cost, set count1 and
38 count2 to two different nonzero values, within the supported cost
39 range. Neither value should equal the default cost. If the hash
40 method does not have tunable cost, set count1 and count2 to zero. */
41 static const struct testcase testcases[] =
42 {
43 #if INCLUDE_descrypt || INCLUDE_bigcrypt
44 { "", 0, 0 },
45 #endif
46 #if INCLUDE_bsdicrypt
47 { "_", 7019, 1120211 },
48 #endif
49 #if INCLUDE_nt
50 { "$3$", 0, 0 },
51 #endif
52 #if INCLUDE_md5crypt
53 { "$1$", 0, 0 },
54 #endif
55 #if INCLUDE_sunmd5
56 { "$md5", 7019, 1120211 },
57 #endif
58 #if INCLUDE_sm3crypt
59 { "$sm3$", 7019, 1120211 },
60 #endif
61 #if INCLUDE_sha1crypt
62 { "$sha1", 7019, 1120211 },
63 #endif
64 #if INCLUDE_sha256crypt
65 { "$5$", 7019, 1120211 },
66 #endif
67 #if INCLUDE_sha512crypt
68 { "$6$", 7019, 1120211 },
69 #endif
70 #if INCLUDE_bcrypt
71 { "$2b$", 7, 11 },
72 #endif
73 #if INCLUDE_bcrypt_y
74 { "$2y$", 7, 11 },
75 #endif
76 #if INCLUDE_bcrypt_a
77 { "$2a$", 7, 11 },
78 #endif
79 #if INCLUDE_scrypt
80 { "$7$", 7, 11, },
81 #endif
82 #if INCLUDE_yescrypt
83 { "$y$", 7, 11, },
84 #endif
85 #if INCLUDE_gost_yescrypt
86 { "$gy$", 7, 11, },
87 #endif
88 #if INCLUDE_sm3_yescrypt
89 { "$sm3y$", 7, 11, },
90 #endif
91 { 0, 0, 0, }
92 };
93
94 static int
95 do_crypt_gensalt(const char *prefix,
96 const char rbytes[MIN_SIZE(N_RBYTES)],
97 unsigned long count,
98 char outbuf[MIN_SIZE(CRYPT_GENSALT_OUTPUT_SIZE)])
99 {
100 /* Detect failure to NUL-terminate the output properly. */
101 static int ncalls = 0;
102 memset(outbuf, '!' + (ncalls % ('~' - '!' + 1)),
103 CRYPT_GENSALT_OUTPUT_SIZE - 1);
104 outbuf[CRYPT_GENSALT_OUTPUT_SIZE - 1] = 0;
105 ncalls++;
106
107 char *rv = crypt_gensalt_rn(prefix, count, rbytes, N_RBYTES,
108 outbuf, CRYPT_GENSALT_OUTPUT_SIZE);
109 if (rv == 0)
110 {
111 printf("ERROR: gensalt(%s, %lu, %c%c..., %lu, outbuf, %lu) = 0/%s\n",
112 prefix, count, rbytes[0], rbytes[1],
113 N_RBYTES, (unsigned long)CRYPT_GENSALT_OUTPUT_SIZE,
114 strerror(errno));
115 outbuf[0] = '*';
116 memset (outbuf+1, 0, CRYPT_GENSALT_OUTPUT_SIZE-1);
117 return 1;
118 }
119 else if (rv[0] == '*')
120 {
121 printf("ERROR: gensalt(%s, %lu, %c%c..., %lu, outbuf, %lu) = %s/%s\n",
122 prefix, count, rbytes[0], rbytes[1],
123 N_RBYTES, (unsigned long)CRYPT_GENSALT_OUTPUT_SIZE,
124 outbuf, strerror(errno));
125 outbuf[0] = '*';
126 memset (outbuf+1, 0, CRYPT_GENSALT_OUTPUT_SIZE-1);
127 return 1;
128 }
129 else
130 return 0;
131 }
132
133 static int
134 do_check_equal(const char *stst, const char *sref,
135 const char *prefix, const char rbytes[N_RBYTES],
136 unsigned long count, const char *setting)
137 {
138 if (!strcmp(stst, sref))
139 return 0;
140
141 printf("FAIL: expected %s\n"
142 " got %s\n"
143 " from %s, %lu, %c%c...\n"
144 " and %s\n",
145 sref, stst, prefix, count, rbytes[0], rbytes[1], setting);
146 return 1;
147 }
148
149 int
150 main(void)
151 {
152 int status = 0;
153 char sref[6][CRYPT_GENSALT_OUTPUT_SIZE];
154 char stst[CRYPT_GENSALT_OUTPUT_SIZE];
155
156 for (size_t i = 0; testcases[i].prefix; i++)
157 {
158 const char *prefix = testcases[i].prefix;
159 unsigned long count1 = testcases[i].count1;
160 unsigned long count2 = testcases[i].count2;
161 int ncases;
162
163 memset(sref, 0, sizeof sref);
164
165 /* If count1 and count2 are both nonzero, then they should also
166 be unequal, and we have six reference cases:
167 (0, count1, count2) x (rbytes1, rbytes2).
168 If count1 and count2 are both zero, then we only have two
169 reference cases: 0 x (rbytes1, rbytes2) (this happens when the
170 hash method doesn't have tunable cost).
171 It is incorrect for only one of count1 and count2 to be zero,
172 or for them to be equal but nonzero. */
173 if (count1 == 0 && count2 == 0)
174 {
175 ncases = 2;
176 status |= do_crypt_gensalt(prefix, rbytes1, 0, sref[0]);
177 status |= do_crypt_gensalt(prefix, rbytes2, 0, sref[1]);
178 }
179 else if (count1 != 0 && count2 != 0 && count1 != count2)
180 {
181 ncases = 6;
182 status |= do_crypt_gensalt(prefix, rbytes1, 0, sref[0]);
183 status |= do_crypt_gensalt(prefix, rbytes2, 0, sref[1]);
184 status |= do_crypt_gensalt(prefix, rbytes1, count1, sref[2]);
185 status |= do_crypt_gensalt(prefix, rbytes2, count1, sref[3]);
186 status |= do_crypt_gensalt(prefix, rbytes1, count2, sref[4]);
187 status |= do_crypt_gensalt(prefix, rbytes2, count2, sref[5]);
188 }
189 else
190 {
191 printf ("ERROR: %zu/%s: inappropriate count1=%lu count2=%lu\n",
192 i, prefix, count1, count2);
193 status = 1;
194 continue;
195 }
196
197 /* At this point, sref[0..ncases] are filled with setting
198 strings corresponding to different combinations of salt and
199 cost. If we reuse those strings as prefixes for crypt_gensalt,
200 none of the additional information should affect the output. */
201 for (int j = 0; j < ncases; j++)
202 {
203 if (sref[j][0] == '*')
204 continue; /* initial crypt_gensalt call failed */
205 if (count1 == 0 && count2 == 0)
206 {
207 status |= do_crypt_gensalt(sref[j], rbytes1, 0, stst);
208 status |= do_check_equal(stst, sref[0],
209 prefix, rbytes1, 0, sref[j]);
210
211 status |= do_crypt_gensalt(sref[j], rbytes2, 0, stst);
212 status |= do_check_equal(stst, sref[1],
213 prefix, rbytes2, 0, sref[j]);
214 }
215 else
216 {
217 status |= do_crypt_gensalt(sref[j], rbytes1, 0, stst);
218 status |= do_check_equal(stst, sref[0],
219 prefix, rbytes1, 0, sref[j]);
220
221 status |= do_crypt_gensalt(sref[j], rbytes2, 0, stst);
222 status |= do_check_equal(stst, sref[1],
223 prefix, rbytes2, 0, sref[j]);
224
225 status |= do_crypt_gensalt(sref[j], rbytes1, count1, stst);
226 status |= do_check_equal(stst, sref[2],
227 prefix, rbytes1, count1, sref[j]);
228
229 status |= do_crypt_gensalt(sref[j], rbytes2, count1, stst);
230 status |= do_check_equal(stst, sref[3],
231 prefix, rbytes2, count1, sref[j]);
232
233 status |= do_crypt_gensalt(sref[j], rbytes1, count2, stst);
234 status |= do_check_equal(stst, sref[4],
235 prefix, rbytes1, count2, sref[j]);
236
237 status |= do_crypt_gensalt(sref[j], rbytes2, count2, stst);
238 status |= do_check_equal(stst, sref[5],
239 prefix, rbytes2, count2, sref[j]);
240 }
241 }
242
243 }
244
245 return status;
246 }