blob: 72e37e485033c78d14b65b03523a101fcedf5d42 [file] [log] [blame]
Denis Vlasenko2211d522008-11-10 18:52:35 +00001/* SHA256 and SHA512-based Unix crypt implementation.
2 * Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>.
3 */
4
5/* Prefix for optional rounds specification. */
Denys Vlasenko5dcc6602010-12-01 13:57:25 +01006static const char str_rounds[] ALIGN1 = "rounds=%u$";
Denis Vlasenko2211d522008-11-10 18:52:35 +00007
8/* Maximum salt string length. */
9#define SALT_LEN_MAX 16
10/* Default number of rounds if not explicitly specified. */
11#define ROUNDS_DEFAULT 5000
12/* Minimum number of rounds. */
13#define ROUNDS_MIN 1000
14/* Maximum number of rounds. */
15#define ROUNDS_MAX 999999999
16
17static char *
18NOINLINE
19sha_crypt(/*const*/ char *key_data, /*const*/ char *salt_data)
20{
21 void (*sha_begin)(void *ctx) FAST_FUNC;
Denys Vlasenko5dcc6602010-12-01 13:57:25 +010022 void (*sha_hash)(void *ctx, const void *buffer, size_t len) FAST_FUNC;
23 void (*sha_end)(void *ctx, void *resbuf) FAST_FUNC;
Denis Vlasenko2211d522008-11-10 18:52:35 +000024 int _32or64;
25
26 char *result, *resptr;
27
28 /* btw, sha256 needs [32] and uint32_t only */
Denis Vlasenko3470f922008-11-13 13:25:18 +000029 struct {
30 unsigned char alt_result[64];
31 unsigned char temp_result[64];
32 union {
33 sha256_ctx_t x;
34 sha512_ctx_t y;
35 } ctx;
36 union {
37 sha256_ctx_t x;
38 sha512_ctx_t y;
39 } alt_ctx;
40 } L __attribute__((__aligned__(__alignof__(uint64_t))));
41#define alt_result (L.alt_result )
42#define temp_result (L.temp_result)
43#define ctx (L.ctx )
44#define alt_ctx (L.alt_ctx )
Denis Vlasenko2211d522008-11-10 18:52:35 +000045 unsigned salt_len;
46 unsigned key_len;
47 unsigned cnt;
48 unsigned rounds;
49 char *cp;
Denis Vlasenko2211d522008-11-10 18:52:35 +000050
51 /* Analyze salt, construct already known part of result */
52 cnt = strlen(salt_data) + 1 + 43 + 1;
Denys Vlasenko16e7f692017-01-15 20:59:32 +010053 _32or64 = 32;
54 if (salt_data[1] == '6') { /* sha512 */
55 _32or64 *= 2; /*64*/
Denis Vlasenko2211d522008-11-10 18:52:35 +000056 cnt += 43;
Denys Vlasenko16e7f692017-01-15 20:59:32 +010057 }
Denis Vlasenko2211d522008-11-10 18:52:35 +000058 result = resptr = xzalloc(cnt); /* will provide NUL terminator */
59 *resptr++ = '$';
Denys Vlasenko16e7f692017-01-15 20:59:32 +010060 *resptr++ = salt_data[1];
Denis Vlasenko2211d522008-11-10 18:52:35 +000061 *resptr++ = '$';
62 rounds = ROUNDS_DEFAULT;
63 salt_data += 3;
64 if (strncmp(salt_data, str_rounds, 7) == 0) {
65 /* 7 == strlen("rounds=") */
66 char *endp;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +000067 cnt = bb_strtou(salt_data + 7, &endp, 10);
Denis Vlasenko2211d522008-11-10 18:52:35 +000068 if (*endp == '$') {
69 salt_data = endp + 1;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +000070 rounds = cnt;
Denis Vlasenko2211d522008-11-10 18:52:35 +000071 if (rounds < ROUNDS_MIN)
72 rounds = ROUNDS_MIN;
73 if (rounds > ROUNDS_MAX)
74 rounds = ROUNDS_MAX;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +000075 /* add "rounds=NNNNN$" to result */
76 resptr += sprintf(resptr, str_rounds, rounds);
Denis Vlasenko2211d522008-11-10 18:52:35 +000077 }
78 }
79 salt_len = strchrnul(salt_data, '$') - salt_data;
80 if (salt_len > SALT_LEN_MAX)
81 salt_len = SALT_LEN_MAX;
82 /* xstrdup assures suitable alignment; also we will use it
83 as a scratch space later. */
84 salt_data = xstrndup(salt_data, salt_len);
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +000085 /* add "salt$" to result */
Denis Vlasenko2211d522008-11-10 18:52:35 +000086 strcpy(resptr, salt_data);
87 resptr += salt_len;
88 *resptr++ = '$';
89 /* key data doesn't need much processing */
90 key_len = strlen(key_data);
91 key_data = xstrdup(key_data);
92
93 /* Which flavor of SHAnnn ops to use? */
94 sha_begin = (void*)sha256_begin;
95 sha_hash = (void*)sha256_hash;
96 sha_end = (void*)sha256_end;
Denys Vlasenko16e7f692017-01-15 20:59:32 +010097 if (_32or64 != 32) {
Denis Vlasenko2211d522008-11-10 18:52:35 +000098 sha_begin = (void*)sha512_begin;
99 sha_hash = (void*)sha512_hash;
100 sha_end = (void*)sha512_end;
Denis Vlasenko2211d522008-11-10 18:52:35 +0000101 }
102
103 /* Add KEY, SALT. */
104 sha_begin(&ctx);
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100105 sha_hash(&ctx, key_data, key_len);
106 sha_hash(&ctx, salt_data, salt_len);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000107
108 /* Compute alternate SHA sum with input KEY, SALT, and KEY.
109 The final result will be added to the first context. */
110 sha_begin(&alt_ctx);
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100111 sha_hash(&alt_ctx, key_data, key_len);
112 sha_hash(&alt_ctx, salt_data, salt_len);
113 sha_hash(&alt_ctx, key_data, key_len);
114 sha_end(&alt_ctx, alt_result);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000115
116 /* Add result of this to the other context. */
117 /* Add for any character in the key one byte of the alternate sum. */
118 for (cnt = key_len; cnt > _32or64; cnt -= _32or64)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100119 sha_hash(&ctx, alt_result, _32or64);
120 sha_hash(&ctx, alt_result, cnt);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000121
122 /* Take the binary representation of the length of the key and for every
123 1 add the alternate sum, for every 0 the key. */
124 for (cnt = key_len; cnt != 0; cnt >>= 1)
125 if ((cnt & 1) != 0)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100126 sha_hash(&ctx, alt_result, _32or64);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000127 else
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100128 sha_hash(&ctx, key_data, key_len);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000129
130 /* Create intermediate result. */
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100131 sha_end(&ctx, alt_result);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000132
133 /* Start computation of P byte sequence. */
134 /* For every character in the password add the entire password. */
135 sha_begin(&alt_ctx);
136 for (cnt = 0; cnt < key_len; ++cnt)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100137 sha_hash(&alt_ctx, key_data, key_len);
138 sha_end(&alt_ctx, temp_result);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000139
140 /* NB: past this point, raw key_data is not used anymore */
141
142 /* Create byte sequence P. */
143#define p_bytes key_data /* reuse the buffer as it is of the key_len size */
144 cp = p_bytes; /* was: ... = alloca(key_len); */
145 for (cnt = key_len; cnt >= _32or64; cnt -= _32or64) {
146 cp = memcpy(cp, temp_result, _32or64);
147 cp += _32or64;
148 }
149 memcpy(cp, temp_result, cnt);
150
151 /* Start computation of S byte sequence. */
152 /* For every character in the password add the entire password. */
153 sha_begin(&alt_ctx);
154 for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100155 sha_hash(&alt_ctx, salt_data, salt_len);
156 sha_end(&alt_ctx, temp_result);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000157
158 /* NB: past this point, raw salt_data is not used anymore */
159
160 /* Create byte sequence S. */
161#define s_bytes salt_data /* reuse the buffer as it is of the salt_len size */
162 cp = s_bytes; /* was: ... = alloca(salt_len); */
163 for (cnt = salt_len; cnt >= _32or64; cnt -= _32or64) {
164 cp = memcpy(cp, temp_result, _32or64);
165 cp += _32or64;
166 }
167 memcpy(cp, temp_result, cnt);
168
169 /* Repeatedly run the collected hash value through SHA to burn
170 CPU cycles. */
171 for (cnt = 0; cnt < rounds; ++cnt) {
172 sha_begin(&ctx);
173
174 /* Add key or last result. */
175 if ((cnt & 1) != 0)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100176 sha_hash(&ctx, p_bytes, key_len);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000177 else
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100178 sha_hash(&ctx, alt_result, _32or64);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000179 /* Add salt for numbers not divisible by 3. */
180 if (cnt % 3 != 0)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100181 sha_hash(&ctx, s_bytes, salt_len);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000182 /* Add key for numbers not divisible by 7. */
183 if (cnt % 7 != 0)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100184 sha_hash(&ctx, p_bytes, key_len);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000185 /* Add key or last result. */
186 if ((cnt & 1) != 0)
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100187 sha_hash(&ctx, alt_result, _32or64);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000188 else
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100189 sha_hash(&ctx, p_bytes, key_len);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000190
Denys Vlasenko5dcc6602010-12-01 13:57:25 +0100191 sha_end(&ctx, alt_result);
Denis Vlasenko2211d522008-11-10 18:52:35 +0000192 }
193
Denis Vlasenko2211d522008-11-10 18:52:35 +0000194 /* Append encrypted password to result buffer */
195//TODO: replace with something like
196// bb_uuencode(cp, src, length, bb_uuenc_tbl_XXXbase64);
197#define b64_from_24bit(B2, B1, B0, N) \
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200198do { \
199 unsigned w = ((B2) << 16) | ((B1) << 8) | (B0); \
200 resptr = to64(resptr, w, N); \
Denis Vlasenko2211d522008-11-10 18:52:35 +0000201} while (0)
Denys Vlasenko16e7f692017-01-15 20:59:32 +0100202 if (_32or64 == 32) { /* sha256 */
Denis Vlasenko3470f922008-11-13 13:25:18 +0000203 unsigned i = 0;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000204 while (1) {
Denis Vlasenko3470f922008-11-13 13:25:18 +0000205 unsigned j = i + 10;
206 unsigned k = i + 20;
207 if (j >= 30) j -= 30;
208 if (k >= 30) k -= 30;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000209 b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4);
Denis Vlasenko3470f922008-11-13 13:25:18 +0000210 if (k == 29)
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000211 break;
Denis Vlasenkoa9acbe62008-11-24 13:25:20 +0000212 i = k + 1;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000213 }
Denis Vlasenko6bd54d42008-11-13 12:55:11 +0000214 b64_from_24bit(0, alt_result[31], alt_result[30], 3);
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000215 /* was:
Denis Vlasenko2211d522008-11-10 18:52:35 +0000216 b64_from_24bit(alt_result[0], alt_result[10], alt_result[20], 4);
217 b64_from_24bit(alt_result[21], alt_result[1], alt_result[11], 4);
218 b64_from_24bit(alt_result[12], alt_result[22], alt_result[2], 4);
219 b64_from_24bit(alt_result[3], alt_result[13], alt_result[23], 4);
220 b64_from_24bit(alt_result[24], alt_result[4], alt_result[14], 4);
221 b64_from_24bit(alt_result[15], alt_result[25], alt_result[5], 4);
222 b64_from_24bit(alt_result[6], alt_result[16], alt_result[26], 4);
223 b64_from_24bit(alt_result[27], alt_result[7], alt_result[17], 4);
224 b64_from_24bit(alt_result[18], alt_result[28], alt_result[8], 4);
225 b64_from_24bit(alt_result[9], alt_result[19], alt_result[29], 4);
226 b64_from_24bit(0, alt_result[31], alt_result[30], 3);
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000227 */
Denis Vlasenko2211d522008-11-10 18:52:35 +0000228 } else {
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000229 unsigned i = 0;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000230 while (1) {
Denis Vlasenko3470f922008-11-13 13:25:18 +0000231 unsigned j = i + 21;
232 unsigned k = i + 42;
233 if (j >= 63) j -= 63;
234 if (k >= 63) k -= 63;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000235 b64_from_24bit(alt_result[i], alt_result[j], alt_result[k], 4);
Denis Vlasenko3470f922008-11-13 13:25:18 +0000236 if (j == 20)
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000237 break;
Denis Vlasenko3470f922008-11-13 13:25:18 +0000238 i = j + 1;
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000239 }
240 b64_from_24bit(0, 0, alt_result[63], 2);
241 /* was:
Denis Vlasenko2211d522008-11-10 18:52:35 +0000242 b64_from_24bit(alt_result[0], alt_result[21], alt_result[42], 4);
243 b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4);
244 b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4);
245 b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4);
246 b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4);
247 b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4);
248 b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4);
249 b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4);
250 b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4);
251 b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4);
252 b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4);
253 b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4);
254 b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4);
255 b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4);
256 b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4);
257 b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4);
258 b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4);
259 b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4);
260 b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4);
261 b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4);
262 b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4);
263 b64_from_24bit(0, 0, alt_result[63], 2);
Denis Vlasenko6b1e3d72008-11-13 12:23:46 +0000264 */
Denis Vlasenko2211d522008-11-10 18:52:35 +0000265 }
266 /* *resptr = '\0'; - xzalloc did it */
267#undef b64_from_24bit
268
269 /* Clear the buffer for the intermediate result so that people
270 attaching to processes or reading core dumps cannot get any
271 information. */
Denis Vlasenko3470f922008-11-13 13:25:18 +0000272 memset(&L, 0, sizeof(L)); /* [alt]_ctx and XXX_result buffers */
Denis Vlasenko2211d522008-11-10 18:52:35 +0000273 memset(key_data, 0, key_len); /* also p_bytes */
274 memset(salt_data, 0, salt_len); /* also s_bytes */
275 free(key_data);
276 free(salt_data);
277#undef p_bytes
278#undef s_bytes
279
280 return result;
Denis Vlasenko3470f922008-11-13 13:25:18 +0000281#undef alt_result
282#undef temp_result
283#undef ctx
284#undef alt_ctx
Denis Vlasenko2211d522008-11-10 18:52:35 +0000285}