sha512-fix für crypt
Hanno Böck

Hanno Böck commited on 2009-12-22 17:31:42
Zeige 3 geänderte Dateien mit 1805 Einfügungen und 1 Löschungen.

... ...
@@ -3,8 +3,9 @@ AUX 70_mod_php5.conf-apache2 490 RMD160 745bdb5db622577f473703d5ee8dc7f3c66f8f0c
3 3
 AUX 70_mod_php5.conf-apache2-r1 374 RMD160 ca06cdc9d1a3dc4129a60c938ee3a1b542497fbb SHA1 4733066b6324c5870e716485484c44c7c26a9ff1 SHA256 815c1ca23c9afe8479568ceaac4057eb91ea5444fbd863866b29adb4eea2c82b
4 4
 AUX 70_mod_php5_concurr.conf-apache2 414 RMD160 1783b6c830119f021c3fb6cb35a631f43c4fa70c SHA1 09f9076f35bc84994fc8c687b4befc0400475f1a SHA256 ccc3bc073eafc83f98049679a411801e80f84620bd51c37c36de2b9ee9492aa2
5 5
 AUX 70_mod_php5_concurr.conf-apache2-r1 376 RMD160 4f7de5c0784b6191450b5c1a7de7ad941620e199 SHA1 c42a23bd7a1d3e8c7e0ac906f50f180116349f76 SHA256 c05f499d9c8927391c586b94716a9f59d63767165552ea527ec7ff63c36eaa40
6
+AUX php-5.3.1-sha512.diff 58046 RMD160 1479068ed76c041427cdd85f18911b76f5d9bf02 SHA1 6745a2ea314554ddf3550e9ebaa192bc2ed04c8a SHA256 f152e27055e26817448f60b88d8d0738cc3def6d6f10c2d7fbbd84e5a096a344
6 7
 AUX php5-ldvs 22 RMD160 5846dab2745b68a88175dd4e72d0b8cf4756dd46 SHA1 592398c92575adb14ec972847ce2aca28a7b9c2c SHA256 b79d0e52b1b3b4543b31ad45525ae1c2814a27ea8e676772ab10bf6fb12dfe79
7 8
 DIST php-5.3.1.tar.bz2 10457046 RMD160 01183a9f752ce5982a7b91dcd43721fd1b9cd88b SHA1 be08ca9337a4962cd4acf8709cd695e1e31c1149 SHA256 9803ce0d6eb2ae072f0149158f5921135b47b633ef5632b4688b30a23be20dba
8 9
 DIST php-patchset-5.3.1-r0.tar.bz2 7301 RMD160 2582efa0b514d9c744a403b84b9ea185f1d8fd6e SHA1 9177c25ae67c301fbe038ae1cc7d55a6a16bb06f SHA256 2dbd8743d309fffda3e2896256d26631d2522eab3ba759c81192d8cc5950bb1f
9 10
 DIST suhosin-patch-5.3.1RC1-0.9.8.patch.gz 38250 RMD160 cfb60da9b4142b0d3fd0f7e193fe2419ba9a5d31 SHA1 d3e8f83f81311a5f382b545cbd745dcedd5f3c93 SHA256 46a3aff061b4d6c7f3721aa65824177c36821631247a502bc77e120559a37721
10
-EBUILD php-5.3.1.ebuild 16776 RMD160 4f27ca6d444cde333fea23ebc929b182c07c4644 SHA1 6ecf35cb6411ce09fb29df54721ac6c8df3cf3f4 SHA256 000b8f2c56aaf04ea5bba008eee82ad105c342f4ee8d276c7e9220d3e5d5fc7c
11
+EBUILD php-5.3.1.ebuild 16816 RMD160 932192368c8d50c6b9b0f0fa0fdc72c3958c7237 SHA1 a1c7de3f071982526bcd224b907887b5ff011e63 SHA256 20247583d9640161710683100a0b9fe4f3ab49eaff7d923259e519ad75f11c85
... ...
@@ -0,0 +1,1801 @@
1
+Index: ext/standard/crypt_sha512.c
2
+===================================================================
3
+--- ext/standard/crypt_sha512.c	(Revision 0)
4
++++ ext/standard/crypt_sha512.c	(Revision 291899)
5
+@@ -0,0 +1,831 @@
6
++/* SHA512-based Unix crypt implementation.
7
++   Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>.  */
8
++/* Windows VC++ port by Pierre Joye <pierre@php.net> */
9
++
10
++#ifndef PHP_WIN32
11
++# include <endian.h>
12
++# include "php.h"
13
++# include "php_main.h"
14
++#endif
15
++
16
++#include <errno.h>
17
++#include <limits.h>
18
++#ifdef PHP_WIN32
19
++# include "win32/php_stdint.h"
20
++# include "win32/php_stdbool.h"
21
++# define __alignof__ __alignof
22
++# define alloca _alloca
23
++#else
24
++# if HAVE_INTTYPES_H
25
++#  include <inttypes.h>
26
++# elif HAVE_STDINT_H
27
++#  include <stdint.h>
28
++# endif
29
++# include <stdbool.h>
30
++#endif
31
++
32
++#include <stdio.h>
33
++#include <stdlib.h>
34
++
35
++#ifdef PHP_WIN32
36
++# include <string.h>
37
++#else
38
++# include <sys/param.h>
39
++# include <sys/types.h>
40
++# if HAVE_STRING_H
41
++#  define __USE_GNU 
42
++#  include <string.h>
43
++# else
44
++#  include <strings.h>
45
++# endif
46
++#endif
47
++#if 0
48
++#ifndef stpncpy
49
++char * stpncpy(char *dst, const char *src, size_t len)
50
++{
51
++	size_t n = strlen(src);
52
++	if (n > len) {
53
++		n = len;
54
++	}
55
++	return strncpy(dst, src, len) + n;
56
++}
57
++#endif
58
++
59
++#ifndef mempcpy
60
++void * mempcpy(void * dst, const void * src, size_t len)
61
++{
62
++	return (((char *)memcpy(dst, src, len)) + len);
63
++}
64
++#endif
65
++#endif
66
++
67
++#ifndef MIN
68
++# define MIN(a, b) (((a) < (b)) ? (a) : (b))
69
++#endif
70
++#ifndef MAX
71
++# define MAX(a, b) (((a) > (b)) ? (a) : (b))
72
++#endif
73
++
74
++/* Structure to save state of computation between the single steps.  */
75
++struct sha512_ctx
76
++{
77
++	uint64_t H[8];
78
++
79
++	uint64_t total[2];
80
++	uint64_t buflen;
81
++	char buffer[256];	/* NB: always correctly aligned for uint64_t.  */
82
++};
83
++
84
++
85
++#if PHP_WIN32 || (__BYTE_ORDER == __LITTLE_ENDIAN)
86
++# define SWAP(n) \
87
++  (((n) << 56)					\
88
++   | (((n) & 0xff00) << 40)			\
89
++   | (((n) & 0xff0000) << 24)			\
90
++   | (((n) & 0xff000000) << 8)			\
91
++   | (((n) >> 8) & 0xff000000)			\
92
++   | (((n) >> 24) & 0xff0000)			\
93
++   | (((n) >> 40) & 0xff00)			\
94
++   | ((n) >> 56))
95
++#else
96
++# define SWAP(n) (n)
97
++#endif
98
++
99
++/* This array contains the bytes used to pad the buffer to the next
100
++   64-byte boundary.  (FIPS 180-2:5.1.2)  */
101
++static const unsigned char fillbuf[128] = { 0x80, 0 /* , 0, 0, ...  */ };
102
++
103
++/* Constants for SHA512 from FIPS 180-2:4.2.3.  */
104
++static const uint64_t K[80] = {
105
++	UINT64_C (0x428a2f98d728ae22), UINT64_C (0x7137449123ef65cd),
106
++	UINT64_C (0xb5c0fbcfec4d3b2f), UINT64_C (0xe9b5dba58189dbbc),
107
++	UINT64_C (0x3956c25bf348b538), UINT64_C (0x59f111f1b605d019),
108
++	UINT64_C (0x923f82a4af194f9b), UINT64_C (0xab1c5ed5da6d8118),
109
++	UINT64_C (0xd807aa98a3030242), UINT64_C (0x12835b0145706fbe),
110
++	UINT64_C (0x243185be4ee4b28c), UINT64_C (0x550c7dc3d5ffb4e2),
111
++	UINT64_C (0x72be5d74f27b896f), UINT64_C (0x80deb1fe3b1696b1),
112
++	UINT64_C (0x9bdc06a725c71235), UINT64_C (0xc19bf174cf692694),
113
++	UINT64_C (0xe49b69c19ef14ad2), UINT64_C (0xefbe4786384f25e3),
114
++	UINT64_C (0x0fc19dc68b8cd5b5), UINT64_C (0x240ca1cc77ac9c65),
115
++	UINT64_C (0x2de92c6f592b0275), UINT64_C (0x4a7484aa6ea6e483),
116
++	UINT64_C (0x5cb0a9dcbd41fbd4), UINT64_C (0x76f988da831153b5),
117
++	UINT64_C (0x983e5152ee66dfab), UINT64_C (0xa831c66d2db43210),
118
++	UINT64_C (0xb00327c898fb213f), UINT64_C (0xbf597fc7beef0ee4),
119
++	UINT64_C (0xc6e00bf33da88fc2), UINT64_C (0xd5a79147930aa725),
120
++	UINT64_C (0x06ca6351e003826f), UINT64_C (0x142929670a0e6e70),
121
++	UINT64_C (0x27b70a8546d22ffc), UINT64_C (0x2e1b21385c26c926),
122
++	UINT64_C (0x4d2c6dfc5ac42aed), UINT64_C (0x53380d139d95b3df),
123
++	UINT64_C (0x650a73548baf63de), UINT64_C (0x766a0abb3c77b2a8),
124
++	UINT64_C (0x81c2c92e47edaee6), UINT64_C (0x92722c851482353b),
125
++	UINT64_C (0xa2bfe8a14cf10364), UINT64_C (0xa81a664bbc423001),
126
++	UINT64_C (0xc24b8b70d0f89791), UINT64_C (0xc76c51a30654be30),
127
++	UINT64_C (0xd192e819d6ef5218), UINT64_C (0xd69906245565a910),
128
++	UINT64_C (0xf40e35855771202a), UINT64_C (0x106aa07032bbd1b8),
129
++	UINT64_C (0x19a4c116b8d2d0c8), UINT64_C (0x1e376c085141ab53),
130
++	UINT64_C (0x2748774cdf8eeb99), UINT64_C (0x34b0bcb5e19b48a8),
131
++	UINT64_C (0x391c0cb3c5c95a63), UINT64_C (0x4ed8aa4ae3418acb),
132
++	UINT64_C (0x5b9cca4f7763e373), UINT64_C (0x682e6ff3d6b2b8a3),
133
++	UINT64_C (0x748f82ee5defb2fc), UINT64_C (0x78a5636f43172f60),
134
++	UINT64_C (0x84c87814a1f0ab72), UINT64_C (0x8cc702081a6439ec),
135
++	UINT64_C (0x90befffa23631e28), UINT64_C (0xa4506cebde82bde9),
136
++	UINT64_C (0xbef9a3f7b2c67915), UINT64_C (0xc67178f2e372532b),
137
++	UINT64_C (0xca273eceea26619c), UINT64_C (0xd186b8c721c0c207),
138
++	UINT64_C (0xeada7dd6cde0eb1e), UINT64_C (0xf57d4f7fee6ed178),
139
++	UINT64_C (0x06f067aa72176fba), UINT64_C (0x0a637dc5a2c898a6),
140
++	UINT64_C (0x113f9804bef90dae), UINT64_C (0x1b710b35131c471b),
141
++	UINT64_C (0x28db77f523047d84), UINT64_C (0x32caab7b40c72493),
142
++	UINT64_C (0x3c9ebe0a15c9bebc), UINT64_C (0x431d67c49c100d4c),
143
++	UINT64_C (0x4cc5d4becb3e42b6), UINT64_C (0x597f299cfc657e2a),
144
++	UINT64_C (0x5fcb6fab3ad6faec), UINT64_C (0x6c44198c4a475817)
145
++  };
146
++
147
++
148
++/* Process LEN bytes of BUFFER, accumulating context into CTX.
149
++   It is assumed that LEN % 128 == 0.  */
150
++static void
151
++sha512_process_block(const void *buffer, size_t len, struct sha512_ctx *ctx) {
152
++	const uint64_t *words = buffer;
153
++	size_t nwords = len / sizeof(uint64_t);
154
++	uint64_t a = ctx->H[0];
155
++	uint64_t b = ctx->H[1];
156
++	uint64_t c = ctx->H[2];
157
++	uint64_t d = ctx->H[3];
158
++	uint64_t e = ctx->H[4];
159
++	uint64_t f = ctx->H[5];
160
++	uint64_t g = ctx->H[6];
161
++	uint64_t h = ctx->H[7];
162
++
163
++  /* First increment the byte count.  FIPS 180-2 specifies the possible
164
++	 length of the file up to 2^128 bits.  Here we only compute the
165
++	 number of bytes.  Do a double word increment.  */
166
++	ctx->total[0] += len;
167
++	if (ctx->total[0] < len) {
168
++		++ctx->total[1];
169
++	}
170
++
171
++	/* Process all bytes in the buffer with 128 bytes in each round of
172
++	 the loop.  */
173
++	while (nwords > 0) {
174
++		uint64_t W[80];
175
++		uint64_t a_save = a;
176
++		uint64_t b_save = b;
177
++		uint64_t c_save = c;
178
++		uint64_t d_save = d;
179
++		uint64_t e_save = e;
180
++		uint64_t f_save = f;
181
++		uint64_t g_save = g;
182
++		uint64_t h_save = h;
183
++		unsigned int t;
184
++
185
++/* Operators defined in FIPS 180-2:4.1.2.  */
186
++#define Ch(x, y, z) ((x & y) ^ (~x & z))
187
++#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
188
++#define S0(x) (CYCLIC (x, 28) ^ CYCLIC (x, 34) ^ CYCLIC (x, 39))
189
++#define S1(x) (CYCLIC (x, 14) ^ CYCLIC (x, 18) ^ CYCLIC (x, 41))
190
++#define R0(x) (CYCLIC (x, 1) ^ CYCLIC (x, 8) ^ (x >> 7))
191
++#define R1(x) (CYCLIC (x, 19) ^ CYCLIC (x, 61) ^ (x >> 6))
192
++
193
++		/* It is unfortunate that C does not provide an operator for
194
++		   cyclic rotation.  Hope the C compiler is smart enough.  */
195
++#define CYCLIC(w, s) ((w >> s) | (w << (64 - s)))
196
++
197
++		/* Compute the message schedule according to FIPS 180-2:6.3.2 step 2.  */
198
++		for (t = 0; t < 16; ++t) {
199
++			W[t] = SWAP (*words);
200
++			++words;
201
++		}
202
++
203
++		for (t = 16; t < 80; ++t) {
204
++			W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
205
++		}
206
++
207
++		/* The actual computation according to FIPS 180-2:6.3.2 step 3.  */
208
++		for (t = 0; t < 80; ++t) {
209
++			uint64_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
210
++			uint64_t T2 = S0 (a) + Maj (a, b, c);
211
++			h = g;
212
++			g = f;
213
++			f = e;
214
++			e = d + T1;
215
++			d = c;
216
++			c = b;
217
++			b = a;
218
++			a = T1 + T2;
219
++		}
220
++
221
++		/* Add the starting values of the context according to FIPS 180-2:6.3.2
222
++		step 4.  */
223
++		a += a_save;
224
++		b += b_save;
225
++		c += c_save;
226
++		d += d_save;
227
++		e += e_save;
228
++		f += f_save;
229
++		g += g_save;
230
++		h += h_save;
231
++
232
++		/* Prepare for the next round.  */
233
++		nwords -= 16;
234
++	}
235
++
236
++	/* Put checksum in context given as argument.  */
237
++	ctx->H[0] = a;
238
++	ctx->H[1] = b;
239
++	ctx->H[2] = c;
240
++	ctx->H[3] = d;
241
++	ctx->H[4] = e;
242
++	ctx->H[5] = f;
243
++	ctx->H[6] = g;
244
++	ctx->H[7] = h;
245
++}
246
++
247
++
248
++/* Initialize structure containing state of computation.
249
++   (FIPS 180-2:5.3.3)  */
250
++static void sha512_init_ctx (struct sha512_ctx *ctx) {
251
++	ctx->H[0] = UINT64_C (0x6a09e667f3bcc908);
252
++	ctx->H[1] = UINT64_C (0xbb67ae8584caa73b);
253
++	ctx->H[2] = UINT64_C (0x3c6ef372fe94f82b);
254
++	ctx->H[3] = UINT64_C (0xa54ff53a5f1d36f1);
255
++	ctx->H[4] = UINT64_C (0x510e527fade682d1);
256
++	ctx->H[5] = UINT64_C (0x9b05688c2b3e6c1f);
257
++	ctx->H[6] = UINT64_C (0x1f83d9abfb41bd6b);
258
++	ctx->H[7] = UINT64_C (0x5be0cd19137e2179);
259
++
260
++	ctx->total[0] = ctx->total[1] = 0;
261
++	ctx->buflen = 0;
262
++}
263
++
264
++
265
++/* Process the remaining bytes in the internal buffer and the usual
266
++	prolog according to the standard and write the result to RESBUF.
267
++
268
++	IMPORTANT: On some systems it is required that RESBUF is correctly
269
++	aligned for a 32 bits value. */
270
++static void * sha512_finish_ctx (struct sha512_ctx *ctx, void *resbuf) {
271
++	/* Take yet unprocessed bytes into account.  */
272
++	uint64_t bytes = ctx->buflen;
273
++	size_t pad;
274
++	unsigned int i;
275
++
276
++	/* Now count remaining bytes.  */
277
++	ctx->total[0] += bytes;
278
++	if (ctx->total[0] < bytes) {
279
++		++ctx->total[1];
280
++	}
281
++
282
++	pad = bytes >= 112 ? 128 + 112 - (size_t)bytes : 112 - (size_t)bytes;
283
++	memcpy(&ctx->buffer[bytes], fillbuf, pad);
284
++
285
++	/* Put the 128-bit file length in *bits* at the end of the buffer.  */
286
++	*(uint64_t *) &ctx->buffer[bytes + pad + 8] = SWAP(ctx->total[0] << 3);
287
++	*(uint64_t *) &ctx->buffer[bytes + pad] = SWAP((ctx->total[1] << 3) |
288
++						(ctx->total[0] >> 61));
289
++
290
++	/* Process last bytes.  */
291
++	sha512_process_block(ctx->buffer, (size_t)(bytes + pad + 16), ctx);
292
++
293
++	/* Put result from CTX in first 64 bytes following RESBUF.  */
294
++	for (i = 0; i < 8; ++i) {
295
++		((uint64_t *) resbuf)[i] = SWAP(ctx->H[i]);
296
++	}
297
++
298
++	return resbuf;
299
++}
300
++
301
++static void
302
++sha512_process_bytes(const void *buffer, size_t len, struct sha512_ctx *ctx) {
303
++	/* When we already have some bits in our internal buffer concatenate
304
++	 both inputs first.  */
305
++	if (ctx->buflen != 0) {
306
++		size_t left_over = (size_t)ctx->buflen;
307
++		size_t add = (size_t)(256 - left_over > len ? len : 256 - left_over);
308
++
309
++		memcpy(&ctx->buffer[left_over], buffer, add);
310
++		ctx->buflen += add;
311
++
312
++		if (ctx->buflen > 128) {
313
++			sha512_process_block(ctx->buffer, ctx->buflen & ~127, ctx);
314
++
315
++			ctx->buflen &= 127;
316
++			/* The regions in the following copy operation cannot overlap.  */
317
++			memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~127],
318
++					(size_t)ctx->buflen);
319
++		}
320
++
321
++		buffer = (const char *) buffer + add;
322
++		len -= add;
323
++	}
324
++
325
++	/* Process available complete blocks.  */
326
++	if (len >= 128) {
327
++#if !_STRING_ARCH_unaligned
328
++/* To check alignment gcc has an appropriate operator.  Other
329
++   compilers don't.  */
330
++# if __GNUC__ >= 2
331
++#  define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint64_t) != 0)
332
++# else
333
++#  define UNALIGNED_P(p) (((uintptr_t) p) % sizeof(uint64_t) != 0)
334
++# endif
335
++		if (UNALIGNED_P(buffer))
336
++			while (len > 128) {
337
++				sha512_process_block(memcpy(ctx->buffer, buffer, 128), 128, ctx);
338
++				buffer = (const char *) buffer + 128;
339
++				len -= 128;
340
++			}
341
++		else
342
++#endif
343
++		{
344
++		  sha512_process_block(buffer, len & ~127, ctx);
345
++		  buffer = (const char *) buffer + (len & ~127);
346
++		  len &= 127;
347
++		}
348
++	}
349
++
350
++  /* Move remaining bytes into internal buffer.  */
351
++	if (len > 0) {
352
++		size_t left_over = (size_t)ctx->buflen;
353
++
354
++		memcpy(&ctx->buffer[left_over], buffer, len);
355
++		left_over += len;
356
++		if (left_over >= 128) {
357
++			sha512_process_block(ctx->buffer, 128, ctx);
358
++			left_over -= 128;
359
++			memcpy(ctx->buffer, &ctx->buffer[128], left_over);
360
++		}
361
++		ctx->buflen = left_over;
362
++	}
363
++}
364
++
365
++
366
++/* Define our magic string to mark salt for SHA512 "encryption"
367
++   replacement.  */
368
++static const char sha512_salt_prefix[] = "$6$";
369
++
370
++/* Prefix for optional rounds specification.  */
371
++static const char sha512_rounds_prefix[] = "rounds=";
372
++
373
++/* Maximum salt string length.  */
374
++#define SALT_LEN_MAX 16
375
++/* Default number of rounds if not explicitly specified.  */
376
++#define ROUNDS_DEFAULT 5000
377
++/* Minimum number of rounds.  */
378
++#define ROUNDS_MIN 1000
379
++/* Maximum number of rounds.  */
380
++#define ROUNDS_MAX 999999999
381
++
382
++/* Table with characters for base64 transformation.  */
383
++static const char b64t[64] =
384
++"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
385
++
386
++
387
++char *
388
++php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen) {
389
++#ifdef PHP_WIN32
390
++	__declspec(align(64)) unsigned char alt_result[64];
391
++	__declspec(align(64)) unsigned char temp_result[64];
392
++#else
393
++	unsigned char alt_result[64]
394
++		__attribute__ ((__aligned__ (__alignof__ (uint64_t))));
395
++	unsigned char temp_result[64]
396
++		__attribute__ ((__aligned__ (__alignof__ (uint64_t))));
397
++#endif
398
++	struct sha512_ctx ctx;
399
++	struct sha512_ctx alt_ctx;
400
++	size_t salt_len;
401
++	size_t key_len;
402
++	size_t cnt;
403
++	char *cp;
404
++	char *copied_key = NULL;
405
++	char *copied_salt = NULL;
406
++	char *p_bytes;
407
++	char *s_bytes;
408
++	/* Default number of rounds.  */
409
++	size_t rounds = ROUNDS_DEFAULT;
410
++	bool rounds_custom = false;
411
++
412
++	/* Find beginning of salt string.  The prefix should normally always
413
++	 be present.  Just in case it is not.  */
414
++	if (strncmp(sha512_salt_prefix, salt, sizeof(sha512_salt_prefix) - 1) == 0) {
415
++		/* Skip salt prefix.  */
416
++		salt += sizeof(sha512_salt_prefix) - 1;
417
++	}
418
++
419
++	if (strncmp(salt, sha512_rounds_prefix, sizeof(sha512_rounds_prefix) - 1) == 0) {
420
++		const char *num = salt + sizeof(sha512_rounds_prefix) - 1;
421
++		char *endp;
422
++		unsigned long int srounds = strtoul(num, &endp, 10);
423
++
424
++		if (*endp == '$') {
425
++			salt = endp + 1;
426
++			rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX));
427
++			rounds_custom = true;
428
++		}
429
++	}
430
++
431
++	salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX);
432
++	key_len = strlen(key);
433
++
434
++	if ((key - (char *) 0) % __alignof__ (uint64_t) != 0) {
435
++		char *tmp = (char *) alloca (key_len + __alignof__ (uint64_t));
436
++		key = copied_key =
437
++		memcpy(tmp + __alignof__(uint64_t) - (tmp - (char *) 0) % __alignof__(uint64_t), key, key_len);
438
++	}
439
++
440
++	if ((salt - (char *) 0) % __alignof__ (uint64_t) != 0) {
441
++		char *tmp = (char *) alloca(salt_len + __alignof__(uint64_t));
442
++
443
++		salt = copied_salt = memcpy(tmp + __alignof__(uint64_t) - (tmp - (char *) 0) % __alignof__(uint64_t), salt, salt_len);
444
++	}
445
++
446
++	/* Prepare for the real work.  */
447
++	sha512_init_ctx(&ctx);
448
++
449
++	/* Add the key string.  */
450
++	sha512_process_bytes(key, key_len, &ctx);
451
++
452
++	/* The last part is the salt string.  This must be at most 16
453
++	 characters and it ends at the first `$' character (for
454
++	 compatibility with existing implementations).  */
455
++	sha512_process_bytes(salt, salt_len, &ctx);
456
++
457
++
458
++	/* Compute alternate SHA512 sum with input KEY, SALT, and KEY.  The
459
++	 final result will be added to the first context.  */
460
++	sha512_init_ctx(&alt_ctx);
461
++
462
++	/* Add key.  */
463
++	sha512_process_bytes(key, key_len, &alt_ctx);
464
++
465
++	/* Add salt.  */
466
++	sha512_process_bytes(salt, salt_len, &alt_ctx);
467
++
468
++	/* Add key again.  */
469
++	sha512_process_bytes(key, key_len, &alt_ctx);
470
++
471
++	/* Now get result of this (64 bytes) and add it to the other
472
++	 context.  */
473
++	sha512_finish_ctx(&alt_ctx, alt_result);
474
++
475
++	/* Add for any character in the key one byte of the alternate sum.  */
476
++	for (cnt = key_len; cnt > 64; cnt -= 64) {
477
++		sha512_process_bytes(alt_result, 64, &ctx);
478
++	}
479
++	sha512_process_bytes(alt_result, cnt, &ctx);
480
++
481
++	/* Take the binary representation of the length of the key and for every
482
++	 1 add the alternate sum, for every 0 the key.  */
483
++	for (cnt = key_len; cnt > 0; cnt >>= 1) {
484
++		if ((cnt & 1) != 0) {
485
++			sha512_process_bytes(alt_result, 64, &ctx);
486
++		} else {
487
++			sha512_process_bytes(key, key_len, &ctx);
488
++		}
489
++	}
490
++
491
++	/* Create intermediate result.  */
492
++	sha512_finish_ctx(&ctx, alt_result);
493
++
494
++	/* Start computation of P byte sequence.  */
495
++	sha512_init_ctx(&alt_ctx);
496
++
497
++	/* For every character in the password add the entire password.  */
498
++	for (cnt = 0; cnt < key_len; ++cnt) {
499
++		sha512_process_bytes(key, key_len, &alt_ctx);
500
++	}
501
++
502
++	/* Finish the digest.  */
503
++	sha512_finish_ctx(&alt_ctx, temp_result);
504
++
505
++	/* Create byte sequence P.  */
506
++	cp = p_bytes = alloca(key_len);
507
++	for (cnt = key_len; cnt >= 64; cnt -= 64) {
508
++		cp = mempcpy((void *) cp, (const void *)temp_result, 64);
509
++	}
510
++
511
++	memcpy(cp, temp_result, cnt);
512
++
513
++	/* Start computation of S byte sequence.  */
514
++	sha512_init_ctx(&alt_ctx);
515
++
516
++	/* For every character in the password add the entire password.  */
517
++	for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) {
518
++		sha512_process_bytes(salt, salt_len, &alt_ctx);
519
++	}
520
++
521
++	/* Finish the digest.  */
522
++	sha512_finish_ctx(&alt_ctx, temp_result);
523
++
524
++	/* Create byte sequence S.  */
525
++	cp = s_bytes = alloca(salt_len);
526
++	for (cnt = salt_len; cnt >= 64; cnt -= 64) {
527
++		cp = mempcpy(cp, temp_result, 64);
528
++	}
529
++	memcpy(cp, temp_result, cnt);
530
++
531
++	/* Repeatedly run the collected hash value through SHA512 to burn
532
++	 CPU cycles.  */
533
++	for (cnt = 0; cnt < rounds; ++cnt) {
534
++		/* New context.  */
535
++		sha512_init_ctx(&ctx);
536
++
537
++		/* Add key or last result.  */
538
++		if ((cnt & 1) != 0) {
539
++			sha512_process_bytes(p_bytes, key_len, &ctx);
540
++		} else {
541
++			sha512_process_bytes(alt_result, 64, &ctx);
542
++		}
543
++
544
++		/* Add salt for numbers not divisible by 3.  */
545
++		if (cnt % 3 != 0) {
546
++			sha512_process_bytes(s_bytes, salt_len, &ctx);
547
++		}
548
++
549
++		/* Add key for numbers not divisible by 7.  */
550
++		if (cnt % 7 != 0) {
551
++			sha512_process_bytes(p_bytes, key_len, &ctx);
552
++		}
553
++
554
++		/* Add key or last result.  */
555
++		if ((cnt & 1) != 0) {
556
++			sha512_process_bytes(alt_result, 64, &ctx);
557
++		} else {
558
++			sha512_process_bytes(p_bytes, key_len, &ctx);
559
++		}
560
++
561
++		/* Create intermediate result.  */
562
++		sha512_finish_ctx(&ctx, alt_result);
563
++	}
564
++
565
++	/* Now we can construct the result string.  It consists of three
566
++	 parts.  */
567
++	cp = stpncpy(buffer, sha512_salt_prefix, MAX(0, buflen));
568
++	buflen -= sizeof(sha512_salt_prefix) - 1;
569
++
570
++	if (rounds_custom) {
571
++#ifdef PHP_WIN32
572
++	  int n = _snprintf(cp, MAX(0, buflen), "%s%u$", sha512_rounds_prefix, rounds);
573
++#else
574
++	  int n = snprintf(cp, MAX(0, buflen), "%s%zu$", sha512_rounds_prefix, rounds);
575
++#endif
576
++	  cp += n;
577
++	  buflen -= n;
578
++	}
579
++
580
++	cp = stpncpy(cp, salt, MIN((size_t) MAX(0, buflen), salt_len));
581
++	buflen -= (int) MIN((size_t) MAX(0, buflen), salt_len);
582
++
583
++	if (buflen > 0) {
584
++		*cp++ = '$';
585
++		--buflen;
586
++	}
587
++
588
++#define b64_from_24bit(B2, B1, B0, N)                    \
589
++  do {									                 \
590
++	unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0);	 \
591
++	int n = (N);							             \
592
++	while (n-- > 0 && buflen > 0)					     \
593
++	  {									                 \
594
++	*cp++ = b64t[w & 0x3f];						         \
595
++	--buflen;							                 \
596
++	w >>= 6;							                 \
597
++	  }									                 \
598
++  } while (0)
599
++
600
++	b64_from_24bit(alt_result[0], alt_result[21], alt_result[42], 4);
601
++	b64_from_24bit(alt_result[22], alt_result[43], alt_result[1], 4);
602
++	b64_from_24bit(alt_result[44], alt_result[2], alt_result[23], 4);
603
++	b64_from_24bit(alt_result[3], alt_result[24], alt_result[45], 4);
604
++	b64_from_24bit(alt_result[25], alt_result[46], alt_result[4], 4);
605
++	b64_from_24bit(alt_result[47], alt_result[5], alt_result[26], 4);
606
++	b64_from_24bit(alt_result[6], alt_result[27], alt_result[48], 4);
607
++	b64_from_24bit(alt_result[28], alt_result[49], alt_result[7], 4);
608
++	b64_from_24bit(alt_result[50], alt_result[8], alt_result[29], 4);
609
++	b64_from_24bit(alt_result[9], alt_result[30], alt_result[51], 4);
610
++	b64_from_24bit(alt_result[31], alt_result[52], alt_result[10], 4);
611
++	b64_from_24bit(alt_result[53], alt_result[11], alt_result[32], 4);
612
++	b64_from_24bit(alt_result[12], alt_result[33], alt_result[54], 4);
613
++	b64_from_24bit(alt_result[34], alt_result[55], alt_result[13], 4);
614
++	b64_from_24bit(alt_result[56], alt_result[14], alt_result[35], 4);
615
++	b64_from_24bit(alt_result[15], alt_result[36], alt_result[57], 4);
616
++	b64_from_24bit(alt_result[37], alt_result[58], alt_result[16], 4);
617
++	b64_from_24bit(alt_result[59], alt_result[17], alt_result[38], 4);
618
++	b64_from_24bit(alt_result[18], alt_result[39], alt_result[60], 4);
619
++	b64_from_24bit(alt_result[40], alt_result[61], alt_result[19], 4);
620
++	b64_from_24bit(alt_result[62], alt_result[20], alt_result[41], 4);
621
++	b64_from_24bit(0, 0, alt_result[63], 2);
622
++
623
++	if (buflen <= 0) {
624
++		errno = ERANGE;
625
++		buffer = NULL;
626
++	} else {
627
++		*cp = '\0';		/* Terminate the string.  */
628
++	}
629
++
630
++	/* Clear the buffer for the intermediate result so that people
631
++	 attaching to processes or reading core dumps cannot get any
632
++	 information.  We do it in this way to clear correct_words[]
633
++	 inside the SHA512 implementation as well.  */
634
++	sha512_init_ctx(&ctx);
635
++	sha512_finish_ctx(&ctx, alt_result);
636
++	memset(temp_result, '\0', sizeof(temp_result));
637
++	memset(p_bytes, '\0', key_len);
638
++	memset(s_bytes, '\0', salt_len);
639
++	memset(&ctx, '\0', sizeof(ctx));
640
++	memset(&alt_ctx, '\0', sizeof(alt_ctx));
641
++	if (copied_key != NULL) {
642
++		memset(copied_key, '\0', key_len);
643
++	}
644
++	if (copied_salt != NULL) {
645
++		memset(copied_salt, '\0', salt_len);
646
++	}
647
++
648
++	return buffer;
649
++}
650
++
651
++
652
++/* This entry point is equivalent to the `crypt' function in Unix
653
++   libcs.  */
654
++char *
655
++php_sha512_crypt(const char *key, const char *salt) {
656
++	/* We don't want to have an arbitrary limit in the size of the
657
++	 password.  We can compute an upper bound for the size of the
658
++	 result in advance and so we can prepare the buffer we pass to
659
++	 `sha512_crypt_r'.  */
660
++	static char *buffer;
661
++	static int buflen;
662
++	int needed = (int)(sizeof(sha512_salt_prefix) - 1
663
++		+ sizeof(sha512_rounds_prefix) + 9 + 1
664
++		+ strlen(salt) + 1 + 86 + 1);
665
++
666
++	if (buflen < needed) {
667
++		char *new_buffer = (char *) realloc(buffer, needed);
668
++		if (new_buffer == NULL) {
669
++			return NULL;
670
++		}
671
++
672
++		buffer = new_buffer;
673
++		buflen = needed;
674
++	}
675
++
676
++	return php_sha512_crypt_r (key, salt, buffer, buflen);
677
++}
678
++
679
++#ifdef TEST
680
++static const struct {
681
++	const char *input;
682
++	const char result[64];
683
++} tests[] =
684
++	{
685
++	/* Test vectors from FIPS 180-2: appendix C.1.  */
686
++	{ "abc",
687
++	  "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31"
688
++	  "\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a"
689
++	  "\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd"
690
++	  "\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f" },
691
++	/* Test vectors from FIPS 180-2: appendix C.2.  */
692
++	{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
693
++	  "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
694
++	  "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14\x3f"
695
++	  "\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88\x90\x18"
696
++	  "\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4\xb5\x43\x3a"
697
++	  "\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b\x87\x4b\xe9\x09" },
698
++	/* Test vectors from the NESSIE project.  */
699
++	{ "",
700
++	  "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80\x07"
701
++	  "\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c\xe9\xce"
702
++	  "\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87\x7e\xec\x2f"
703
++	  "\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a\xf9\x27\xda\x3e" },
704
++	{ "a",
705
++	  "\x1f\x40\xfc\x92\xda\x24\x16\x94\x75\x09\x79\xee\x6c\xf5\x82\xf2"
706
++	  "\xd5\xd7\xd2\x8e\x18\x33\x5d\xe0\x5a\xbc\x54\xd0\x56\x0e\x0f\x53"
707
++	  "\x02\x86\x0c\x65\x2b\xf0\x8d\x56\x02\x52\xaa\x5e\x74\x21\x05\x46"
708
++	  "\xf3\x69\xfb\xbb\xce\x8c\x12\xcf\xc7\x95\x7b\x26\x52\xfe\x9a\x75" },
709
++	{ "message digest",
710
++	  "\x10\x7d\xbf\x38\x9d\x9e\x9f\x71\xa3\xa9\x5f\x6c\x05\x5b\x92\x51"
711
++	  "\xbc\x52\x68\xc2\xbe\x16\xd6\xc1\x34\x92\xea\x45\xb0\x19\x9f\x33"
712
++	  "\x09\xe1\x64\x55\xab\x1e\x96\x11\x8e\x8a\x90\x5d\x55\x97\xb7\x20"
713
++	  "\x38\xdd\xb3\x72\xa8\x98\x26\x04\x6d\xe6\x66\x87\xbb\x42\x0e\x7c" },
714
++	{ "abcdefghijklmnopqrstuvwxyz",
715
++	  "\x4d\xbf\xf8\x6c\xc2\xca\x1b\xae\x1e\x16\x46\x8a\x05\xcb\x98\x81"
716
++	  "\xc9\x7f\x17\x53\xbc\xe3\x61\x90\x34\x89\x8f\xaa\x1a\xab\xe4\x29"
717
++	  "\x95\x5a\x1b\xf8\xec\x48\x3d\x74\x21\xfe\x3c\x16\x46\x61\x3a\x59"
718
++	  "\xed\x54\x41\xfb\x0f\x32\x13\x89\xf7\x7f\x48\xa8\x79\xc7\xb1\xf1" },
719
++	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
720
++	  "\x20\x4a\x8f\xc6\xdd\xa8\x2f\x0a\x0c\xed\x7b\xeb\x8e\x08\xa4\x16"
721
++	  "\x57\xc1\x6e\xf4\x68\xb2\x28\xa8\x27\x9b\xe3\x31\xa7\x03\xc3\x35"
722
++	  "\x96\xfd\x15\xc1\x3b\x1b\x07\xf9\xaa\x1d\x3b\xea\x57\x78\x9c\xa0"
723
++	  "\x31\xad\x85\xc7\xa7\x1d\xd7\x03\x54\xec\x63\x12\x38\xca\x34\x45" },
724
++	{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
725
++	  "\x1e\x07\xbe\x23\xc2\x6a\x86\xea\x37\xea\x81\x0c\x8e\xc7\x80\x93"
726
++	  "\x52\x51\x5a\x97\x0e\x92\x53\xc2\x6f\x53\x6c\xfc\x7a\x99\x96\xc4"
727
++	  "\x5c\x83\x70\x58\x3e\x0a\x78\xfa\x4a\x90\x04\x1d\x71\xa4\xce\xab"
728
++	  "\x74\x23\xf1\x9c\x71\xb9\xd5\xa3\xe0\x12\x49\xf0\xbe\xbd\x58\x94" },
729
++	{ "123456789012345678901234567890123456789012345678901234567890"
730
++	  "12345678901234567890",
731
++	  "\x72\xec\x1e\xf1\x12\x4a\x45\xb0\x47\xe8\xb7\xc7\x5a\x93\x21\x95"
732
++	  "\x13\x5b\xb6\x1d\xe2\x4e\xc0\xd1\x91\x40\x42\x24\x6e\x0a\xec\x3a"
733
++	  "\x23\x54\xe0\x93\xd7\x6f\x30\x48\xb4\x56\x76\x43\x46\x90\x0c\xb1"
734
++	  "\x30\xd2\xa4\xfd\x5d\xd1\x6a\xbb\x5e\x30\xbc\xb8\x50\xde\xe8\x43" }
735
++  };
736
++#define ntests (sizeof (tests) / sizeof (tests[0]))
737
++
738
++
739
++static const struct
740
++{
741
++	const char *salt;
742
++	const char *input;
743
++	const char *expected;
744
++} tests2[] = {
745
++	{ "$6$saltstring", "Hello world!",
746
++	"$6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJu"
747
++	"esI68u4OTLiBFdcbYEdFCoEOfaS35inz1"},
748
++	{ "$6$rounds=10000$saltstringsaltstring", "Hello world!",
749
++	"$6$rounds=10000$saltstringsaltst$OW1/O6BYHV6BcXZu8QVeXbDWra3Oeqh0sb"
750
++	"HbbMCVNSnCM/UrjmM0Dp8vOuZeHBy/YTBmSK6H9qs/y3RnOaw5v." },
751
++	{ "$6$rounds=5000$toolongsaltstring", "This is just a test",
752
++	"$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQ"
753
++	"zQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0" },
754
++	{ "$6$rounds=1400$anotherlongsaltstring",
755
++	"a very much longer text to encrypt.  This one even stretches over more"
756
++	"than one line.",
757
++	"$6$rounds=1400$anotherlongsalts$POfYwTEok97VWcjxIiSOjiykti.o/pQs.wP"
758
++	"vMxQ6Fm7I6IoYN3CmLs66x9t0oSwbtEW7o7UmJEiDwGqd8p4ur1" },
759
++	{ "$6$rounds=77777$short",
760
++	"we have a short salt string but not a short password",
761
++	"$6$rounds=77777$short$WuQyW2YR.hBNpjjRhpYD/ifIw05xdfeEyQoMxIXbkvr0g"
762
++	"ge1a1x3yRULJ5CCaUeOxFmtlcGZelFl5CxtgfiAc0" },
763
++	{ "$6$rounds=123456$asaltof16chars..", "a short string",
764
++	"$6$rounds=123456$asaltof16chars..$BtCwjqMJGx5hrJhZywWvt0RLE8uZ4oPwc"
765
++	"elCjmw2kSYu.Ec6ycULevoBK25fs2xXgMNrCzIMVcgEJAstJeonj1" },
766
++	{ "$6$rounds=10$roundstoolow", "the minimum number is still observed",
767
++	"$6$rounds=1000$roundstoolow$kUMsbe306n21p9R.FRkW3IGn.S9NPN0x50YhH1x"
768
++	"hLsPuWGsUSklZt58jaTfF4ZEQpyUNGc0dqbpBYYBaHHrsX." },
769
++};
770
++#define ntests2 (sizeof (tests2) / sizeof (tests2[0]))
771
++
772
++
773
++int main (void) {
774
++	struct sha512_ctx ctx;
775
++	char sum[64];
776
++	int result = 0;
777
++	int cnt;
778
++	int i;
779
++	char buf[1000];
780
++	static const char expected[64] =
781
++		"\xe7\x18\x48\x3d\x0c\xe7\x69\x64\x4e\x2e\x42\xc7\xbc\x15\xb4\x63"
782
++		"\x8e\x1f\x98\xb1\x3b\x20\x44\x28\x56\x32\xa8\x03\xaf\xa9\x73\xeb"
783
++		"\xde\x0f\xf2\x44\x87\x7e\xa6\x0a\x4c\xb0\x43\x2c\xe5\x77\xc3\x1b"
784
++		"\xeb\x00\x9c\x5c\x2c\x49\xaa\x2e\x4e\xad\xb2\x17\xad\x8c\xc0\x9b";
785
++
786
++	for (cnt = 0; cnt < (int) ntests; ++cnt) {
787
++		sha512_init_ctx (&ctx);
788
++		sha512_process_bytes (tests[cnt].input, strlen (tests[cnt].input), &ctx);
789
++		sha512_finish_ctx (&ctx, sum);
790
++		if (memcmp (tests[cnt].result, sum, 64) != 0) {
791
++			printf ("test %d run %d failed\n", cnt, 1);
792
++			result = 1;
793
++		}
794
++
795
++		sha512_init_ctx (&ctx);
796
++		for (i = 0; tests[cnt].input[i] != '\0'; ++i) {
797
++			sha512_process_bytes (&tests[cnt].input[i], 1, &ctx);
798
++		}
799
++		sha512_finish_ctx (&ctx, sum);
800
++		if (memcmp (tests[cnt].result, sum, 64) != 0) {
801
++			printf ("test %d run %d failed\n", cnt, 2);
802
++			result = 1;
803
++		}
804
++	}
805
++
806
++	/* Test vector from FIPS 180-2: appendix C.3.  */
807
++
808
++	memset (buf, 'a', sizeof (buf));
809
++	sha512_init_ctx (&ctx);
810
++	for (i = 0; i < 1000; ++i) {
811
++		sha512_process_bytes (buf, sizeof (buf), &ctx);
812
++	}
813
++
814
++	sha512_finish_ctx (&ctx, sum);
815
++	if (memcmp (expected, sum, 64) != 0) {
816
++		printf ("test %d failed\n", cnt);
817
++		result = 1;
818
++	}
819
++
820
++	for (cnt = 0; cnt < ntests2; ++cnt) {
821
++		char *cp = php_sha512_crypt(tests2[cnt].input, tests2[cnt].salt);
822
++
823
++		if (strcmp (cp, tests2[cnt].expected) != 0) {
824
++			printf ("test %d: expected \"%s\", got \"%s\"\n",
825
++					cnt, tests2[cnt].expected, cp);
826
++			result = 1;
827
++		}
828
++	}
829
++
830
++	if (result == 0) {
831
++		puts ("all tests OK");
832
++	}
833
++
834
++	return result;
835
++}
836
++#endif
837
+Index: ext/standard/crypt_sha256.c
838
+===================================================================
839
+--- ext/standard/crypt_sha256.c	(Revision 0)
840
++++ ext/standard/crypt_sha256.c	(Revision 291899)
841
+@@ -0,0 +1,754 @@
842
++/* SHA256-based Unix crypt implementation.
843
++   Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>.  */
844
++/* Windows VC++ port by Pierre Joye <pierre@php.net> */
845
++
846
++#ifndef PHP_WIN32
847
++# include <endian.h>
848
++# include "php.h"
849
++# include "php_main.h"
850
++#endif
851
++
852
++#include <errno.h>
853
++#include <limits.h>
854
++
855
++#ifdef PHP_WIN32
856
++# include "win32/php_stdint.h"
857
++# include "win32/php_stdbool.h"
858
++# define __alignof__ __alignof
859
++# define alloca _alloca
860
++#else
861
++# if HAVE_INTTYPES_H
862
++#  include <inttypes.h>
863
++# elif HAVE_STDINT_H
864
++#  include <stdint.h>
865
++# endif
866
++# include <stdbool.h>
867
++#endif
868
++
869
++#include <stdio.h>
870
++#include <stdlib.h>
871
++
872
++#ifdef PHP_WIN32
873
++# include <string.h>
874
++#else
875
++# include <sys/param.h>
876
++# include <sys/types.h>
877
++# if HAVE_STRING_H
878
++//#  define __USE_GNU 1
879
++#  include <string.h>
880
++# else
881
++#  include <strings.h>
882
++# endif
883
++#endif
884
++
885
++#ifndef HAVE_STRPNCPY 
886
++char * stpncpy(char *dst, const char *src, size_t len)
887
++{
888
++	size_t n = strlen(src);
889
++	if (n > len) {
890
++		n = len;
891
++	}
892
++	return strncpy(dst, src, len) + n;
893
++}
894
++#endif
895
++
896
++#ifndef HAVE_MEMPCPY 
897
++void * mempcpy(void * dst, const void * src, size_t len)
898
++{
899
++	return (((char *)memcpy(dst, src, len)) + len);
900
++}
901
++#endif
902
++
903
++#ifndef MIN
904
++# define MIN(a, b) (((a) < (b)) ? (a) : (b))
905
++#endif
906
++#ifndef MAX
907
++# define MAX(a, b) (((a) > (b)) ? (a) : (b))
908
++#endif
909
++
910
++/* Structure to save state of computation between the single steps.  */
911
++struct sha256_ctx {
912
++	uint32_t H[8];
913
++
914
++	uint32_t total[2];
915
++	uint32_t buflen;
916
++	char buffer[128]; /* NB: always correctly aligned for uint32_t.  */
917
++};
918
++
919
++#if PHP_WIN32 || (__BYTE_ORDER == __LITTLE_ENDIAN)
920
++# define SWAP(n) \
921
++    (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
922
++#else
923
++# define SWAP(n) (n)
924
++#endif
925
++
926
++/* This array contains the bytes used to pad the buffer to the next
927
++   64-byte boundary.  (FIPS 180-2:5.1.1)  */
928
++static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
929
++
930
++
931
++/* Constants for SHA256 from FIPS 180-2:4.2.2.  */
932
++static const uint32_t K[64] = {
933
++	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
934
++	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
935
++	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
936
++	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
937
++	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
938
++	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
939
++	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
940
++	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
941
++	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
942
++	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
943
++	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
944
++	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
945
++	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
946
++	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
947
++	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
948
++	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
949
++};
950
++
951
++
952
++/* Process LEN bytes of BUFFER, accumulating context into CTX.
953
++   It is assumed that LEN % 64 == 0.  */
954
++static void sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx) {
955
++	const uint32_t *words = buffer;
956
++	size_t nwords = len / sizeof (uint32_t);
957
++	unsigned int t;
958
++
959
++	uint32_t a = ctx->H[0];
960
++	uint32_t b = ctx->H[1];
961
++	uint32_t c = ctx->H[2];
962
++	uint32_t d = ctx->H[3];
963
++	uint32_t e = ctx->H[4];
964
++	uint32_t f = ctx->H[5];
965
++	uint32_t g = ctx->H[6];
966
++	uint32_t h = ctx->H[7];
967
++
968
++	/* First increment the byte count.  FIPS 180-2 specifies the possible
969
++	 length of the file up to 2^64 bits.  Here we only compute the
970
++	 number of bytes.  Do a double word increment.  */
971
++	ctx->total[0] += len;
972
++	if (ctx->total[0] < len) {
973
++		++ctx->total[1];
974
++	}
975
++
976
++	/* Process all bytes in the buffer with 64 bytes in each round of
977
++	 the loop.  */
978
++	while (nwords > 0) {
979
++		uint32_t W[64];
980
++		uint32_t a_save = a;
981
++		uint32_t b_save = b;
982
++		uint32_t c_save = c;
983
++		uint32_t d_save = d;
984
++		uint32_t e_save = e;
985
++		uint32_t f_save = f;
986
++		uint32_t g_save = g;
987
++		uint32_t h_save = h;
988
++
989
++	/* Operators defined in FIPS 180-2:4.1.2.  */
990
++#define Ch(x, y, z) ((x & y) ^ (~x & z))
991
++#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
992
++#define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
993
++#define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
994
++#define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
995
++#define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
996
++
997
++	/* It is unfortunate that C does not provide an operator for
998
++	cyclic rotation.  Hope the C compiler is smart enough.  */
999
++#define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
1000
++
1001
++		/* Compute the message schedule according to FIPS 180-2:6.2.2 step 2.  */
1002
++		for (t = 0; t < 16; ++t) {
1003
++			W[t] = SWAP (*words);
1004
++			++words;
1005
++		}
1006
++		for (t = 16; t < 64; ++t)
1007
++			W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
1008
++
1009
++		/* The actual computation according to FIPS 180-2:6.2.2 step 3.  */
1010
++		for (t = 0; t < 64; ++t) {
1011
++			uint32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
1012
++			uint32_t T2 = S0 (a) + Maj (a, b, c);
1013
++			h = g;
1014
++			g = f;
1015
++			f = e;
1016
++			e = d + T1;
1017
++			d = c;
1018
++			c = b;
1019
++			b = a;
1020
++			a = T1 + T2;
1021
++		}
1022
++
1023
++		/* Add the starting values of the context according to FIPS 180-2:6.2.2
1024
++		step 4.  */
1025
++		a += a_save;
1026
++		b += b_save;
1027
++		c += c_save;
1028
++		d += d_save;
1029
++		e += e_save;
1030
++		f += f_save;
1031
++		g += g_save;
1032
++		h += h_save;
1033
++
1034
++		/* Prepare for the next round.  */
1035
++		nwords -= 16;
1036
++	}
1037
++
1038
++	/* Put checksum in context given as argument.  */
1039
++	ctx->H[0] = a;
1040
++	ctx->H[1] = b;
1041
++	ctx->H[2] = c;
1042
++	ctx->H[3] = d;
1043
++	ctx->H[4] = e;
1044
++	ctx->H[5] = f;
1045
++	ctx->H[6] = g;
1046
++	ctx->H[7] = h;
1047
++}
1048
++
1049
++
1050
++/* Initialize structure containing state of computation.
1051
++   (FIPS 180-2:5.3.2)  */
1052
++static void sha256_init_ctx(struct sha256_ctx *ctx) {
1053
++	ctx->H[0] = 0x6a09e667;
1054
++	ctx->H[1] = 0xbb67ae85;
1055
++	ctx->H[2] = 0x3c6ef372;
1056
++	ctx->H[3] = 0xa54ff53a;
1057
++	ctx->H[4] = 0x510e527f;
1058
++	ctx->H[5] = 0x9b05688c;
1059
++	ctx->H[6] = 0x1f83d9ab;
1060
++	ctx->H[7] = 0x5be0cd19;
1061
++
1062
++	ctx->total[0] = ctx->total[1] = 0;
1063
++	ctx->buflen = 0;
1064
++}
1065
++
1066
++
1067
++/* Process the remaining bytes in the internal buffer and the usual
1068
++   prolog according to the standard and write the result to RESBUF.
1069
++
1070
++   IMPORTANT: On some systems it is required that RESBUF is correctly
1071
++   aligned for a 32 bits value.  */
1072
++static void * sha256_finish_ctx(struct sha256_ctx *ctx, void *resbuf) {
1073
++	/* Take yet unprocessed bytes into account.  */
1074
++	uint32_t bytes = ctx->buflen;
1075
++	size_t pad;
1076
++	unsigned int i;
1077
++
1078
++	/* Now count remaining bytes.  */
1079
++	ctx->total[0] += bytes;
1080
++	if (ctx->total[0] < bytes) {
1081
++		++ctx->total[1];
1082
++	}
1083
++
1084
++	pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
1085
++	memcpy(&ctx->buffer[bytes], fillbuf, pad);
1086
++
1087
++	/* Put the 64-bit file length in *bits* at the end of the buffer.  */
1088
++	*(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
1089
++	*(uint32_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
1090
++						  (ctx->total[0] >> 29));
1091
++
1092
++	/* Process last bytes.  */
1093
++	sha256_process_block(ctx->buffer, bytes + pad + 8, ctx);
1094
++
1095
++	/* Put result from CTX in first 32 bytes following RESBUF.  */
1096
++	for (i = 0; i < 8; ++i) {
1097
++		((uint32_t *) resbuf)[i] = SWAP(ctx->H[i]);
1098
++	}
1099
++
1100
++	return resbuf;
1101
++}
1102
++
1103
++
1104
++static void sha256_process_bytes(const void *buffer, size_t len, struct sha256_ctx *ctx) {
1105
++	/* When we already have some bits in our internal buffer concatenate
1106
++	 both inputs first.  */
1107
++	if (ctx->buflen != 0) {
1108
++		size_t left_over = ctx->buflen;
1109
++		size_t add = 128 - left_over > len ? len : 128 - left_over;
1110
++
1111
++		  memcpy(&ctx->buffer[left_over], buffer, add);
1112
++		  ctx->buflen += add;
1113
++
1114
++		if (ctx->buflen > 64) {
1115
++			sha256_process_block(ctx->buffer, ctx->buflen & ~63, ctx);
1116
++			ctx->buflen &= 63;
1117
++			/* The regions in the following copy operation cannot overlap.  */
1118
++			memcpy(ctx->buffer, &ctx->buffer[(left_over + add) & ~63], ctx->buflen);
1119
++		}
1120
++
1121
++		buffer = (const char *) buffer + add;
1122
++		len -= add;
1123
++	}
1124
++
1125
++	/* Process available complete blocks.  */
1126
++	if (len >= 64) {
1127
++/* To check alignment gcc has an appropriate operator.  Other
1128
++compilers don't.  */
1129
++#if __GNUC__ >= 2
1130
++# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
1131
++#else
1132
++# define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint32_t) != 0)
1133
++#endif
1134
++		if (UNALIGNED_P (buffer))
1135
++			while (len > 64) {
1136
++				sha256_process_block(memcpy(ctx->buffer, buffer, 64), 64, ctx);
1137
++				buffer = (const char *) buffer + 64;
1138
++				len -= 64;
1139
++			} else {
1140
++				sha256_process_block(buffer, len & ~63, ctx);
1141
++				buffer = (const char *) buffer + (len & ~63);
1142
++				len &= 63;
1143
++			}
1144
++	}
1145
++
1146
++	/* Move remaining bytes into internal buffer.  */
1147
++	if (len > 0) {
1148
++		size_t left_over = ctx->buflen;
1149
++
1150
++		memcpy(&ctx->buffer[left_over], buffer, len);
1151
++		left_over += len;
1152
++		if (left_over >= 64) {
1153
++			sha256_process_block(ctx->buffer, 64, ctx);
1154
++			left_over -= 64;
1155
++			memcpy(ctx->buffer, &ctx->buffer[64], left_over);
1156
++		}
1157
++		ctx->buflen = left_over;
1158
++	}
1159
++}
1160
++
1161
++
1162
++/* Define our magic string to mark salt for SHA256 "encryption"
1163
++   replacement.  */
1164
++static const char sha256_salt_prefix[] = "$5$";
1165
++
1166
++/* Prefix for optional rounds specification.  */
1167
++static const char sha256_rounds_prefix[] = "rounds=";
1168
++
1169
++/* Maximum salt string length.  */
1170
++#define SALT_LEN_MAX 16
1171
++/* Default number of rounds if not explicitly specified.  */
1172
++#define ROUNDS_DEFAULT 5000
1173
++/* Minimum number of rounds.  */
1174
++#define ROUNDS_MIN 1000
1175
++/* Maximum number of rounds.  */
1176
++#define ROUNDS_MAX 999999999
1177
++
1178
++/* Table with characters for base64 transformation.  */
1179
++static const char b64t[64] =
1180
++"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
1181
++
1182
++char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
1183
++{
1184
++#ifdef PHP_WIN32
1185
++	__declspec(align(32)) unsigned char alt_result[32];
1186
++	__declspec(align(32)) unsigned char temp_result[32];
1187
++#else
1188
++	unsigned char alt_result[32]
1189
++	__attribute__ ((__aligned__ (__alignof__ (uint32_t))));
1190
++	unsigned char temp_result[32]
1191
++	__attribute__ ((__aligned__ (__alignof__ (uint32_t))));
1192
++#endif
1193
++
1194
++	struct sha256_ctx ctx;
1195
++	struct sha256_ctx alt_ctx;
1196
++	size_t salt_len;
1197
++	size_t key_len;
1198
++	size_t cnt;
1199
++	char *cp;
1200
++	char *copied_key = NULL;
1201
++	char *copied_salt = NULL;
1202
++	char *p_bytes;
1203
++	char *s_bytes;
1204
++	/* Default number of rounds.  */
1205
++	size_t rounds = ROUNDS_DEFAULT;
1206
++	bool rounds_custom = false;
1207
++
1208
++	/* Find beginning of salt string.  The prefix should normally always
1209
++	be present.  Just in case it is not.  */
1210
++	if (strncmp(sha256_salt_prefix, salt, sizeof(sha256_salt_prefix) - 1) == 0) {
1211
++		/* Skip salt prefix.  */
1212
++		salt += sizeof(sha256_salt_prefix) - 1;
1213
++	}
1214
++
1215
++	if (strncmp(salt, sha256_rounds_prefix, sizeof(sha256_rounds_prefix) - 1) == 0) {
1216
++		const char *num = salt + sizeof(sha256_rounds_prefix) - 1;
1217
++		char *endp;
1218
++		unsigned long int srounds = strtoul(num, &endp, 10);
1219
++		if (*endp == '$') {
1220
++			salt = endp + 1;
1221
++			rounds = MAX(ROUNDS_MIN, MIN(srounds, ROUNDS_MAX));
1222
++			rounds_custom = true;
1223
++		}
1224
++	}
1225
++
1226
++	salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX);
1227
++	key_len = strlen(key);
1228
++
1229
++	if ((key - (char *) 0) % __alignof__ (uint32_t) != 0) {
1230
++		char *tmp = (char *) alloca(key_len + __alignof__(uint32_t));
1231
++		key = copied_key = memcpy(tmp + __alignof__(uint32_t) - (tmp - (char *) 0) % __alignof__(uint32_t), key, key_len);
1232
++	}
1233
++
1234
++	if ((salt - (char *) 0) % __alignof__(uint32_t) != 0) {
1235
++		char *tmp = (char *) alloca(salt_len + __alignof__(uint32_t));
1236
++		salt = copied_salt =
1237
++		memcpy(tmp + __alignof__(uint32_t) - (tmp - (char *) 0) % __alignof__ (uint32_t), salt, salt_len);
1238
++	}
1239
++
1240
++	/* Prepare for the real work.  */
1241
++	sha256_init_ctx(&ctx);
1242
++
1243
++	/* Add the key string.  */
1244
++	sha256_process_bytes(key, key_len, &ctx);
1245
++
1246
++	/* The last part is the salt string.  This must be at most 16
1247
++	 characters and it ends at the first `$' character (for
1248
++	 compatibility with existing implementations).  */
1249
++	sha256_process_bytes(salt, salt_len, &ctx);
1250
++
1251
++
1252
++	/* Compute alternate SHA256 sum with input KEY, SALT, and KEY.  The
1253
++	 final result will be added to the first context.  */
1254
++	sha256_init_ctx(&alt_ctx);
1255
++
1256
++	/* Add key.  */
1257
++	sha256_process_bytes(key, key_len, &alt_ctx);
1258
++
1259
++	/* Add salt.  */
1260
++	sha256_process_bytes(salt, salt_len, &alt_ctx);
1261
++
1262
++	/* Add key again.  */
1263
++	sha256_process_bytes(key, key_len, &alt_ctx);
1264
++
1265
++	/* Now get result of this (32 bytes) and add it to the other
1266
++	 context.  */
1267
++	sha256_finish_ctx(&alt_ctx, alt_result);
1268
++
1269
++	/* Add for any character in the key one byte of the alternate sum.  */
1270
++	for (cnt = key_len; cnt > 32; cnt -= 32) {
1271
++		sha256_process_bytes(alt_result, 32, &ctx);
1272
++	}
1273
++	sha256_process_bytes(alt_result, cnt, &ctx);
1274
++
1275
++	/* Take the binary representation of the length of the key and for every
1276
++	1 add the alternate sum, for every 0 the key.  */
1277
++	for (cnt = key_len; cnt > 0; cnt >>= 1) {
1278
++		if ((cnt & 1) != 0) {
1279
++			sha256_process_bytes(alt_result, 32, &ctx);
1280
++		} else {
1281
++			sha256_process_bytes(key, key_len, &ctx);
1282
++		}
1283
++	}
1284
++
1285
++	/* Create intermediate result.  */
1286
++	sha256_finish_ctx(&ctx, alt_result);
1287
++
1288
++	/* Start computation of P byte sequence.  */
1289
++	sha256_init_ctx(&alt_ctx);
1290
++
1291
++	/* For every character in the password add the entire password.  */
1292
++	for (cnt = 0; cnt < key_len; ++cnt) {
1293
++		sha256_process_bytes(key, key_len, &alt_ctx);
1294
++	}
1295
++
1296
++	/* Finish the digest.  */
1297
++	sha256_finish_ctx(&alt_ctx, temp_result);
1298
++
1299
++	/* Create byte sequence P.  */
1300
++	cp = p_bytes = alloca(key_len);
1301
++	for (cnt = key_len; cnt >= 32; cnt -= 32) {
1302
++		cp = mempcpy((void *)cp, (const void *)temp_result, 32);
1303
++	}
1304
++	memcpy(cp, temp_result, cnt);
1305
++
1306
++	/* Start computation of S byte sequence.  */
1307
++	sha256_init_ctx(&alt_ctx);
1308
++
1309
++	/* For every character in the password add the entire password.  */
1310
++	for (cnt = 0; cnt < 16 + alt_result[0]; ++cnt) {
1311
++		sha256_process_bytes(salt, salt_len, &alt_ctx);
1312
++	}
1313
++
1314
++	/* Finish the digest.  */
1315
++	sha256_finish_ctx(&alt_ctx, temp_result);
1316
++
1317
++	/* Create byte sequence S.  */
1318
++	cp = s_bytes = alloca(salt_len);
1319
++	for (cnt = salt_len; cnt >= 32; cnt -= 32) {
1320
++		cp = mempcpy(cp, temp_result, 32);
1321
++	}
1322
++	memcpy(cp, temp_result, cnt);
1323
++
1324
++	/* Repeatedly run the collected hash value through SHA256 to burn
1325
++	CPU cycles.  */
1326
++	for (cnt = 0; cnt < rounds; ++cnt) {
1327
++		/* New context.  */
1328
++		sha256_init_ctx(&ctx);
1329
++
1330
++		/* Add key or last result.  */
1331
++		if ((cnt & 1) != 0) {
1332
++			sha256_process_bytes(p_bytes, key_len, &ctx);
1333
++		} else {
1334
++			sha256_process_bytes(alt_result, 32, &ctx);
1335
++		}
1336
++
1337
++		/* Add salt for numbers not divisible by 3.  */
1338
++		if (cnt % 3 != 0) {
1339
++			sha256_process_bytes(s_bytes, salt_len, &ctx);
1340
++		}
1341
++
1342
++		/* Add key for numbers not divisible by 7.  */
1343
++		if (cnt % 7 != 0) {
1344
++			sha256_process_bytes(p_bytes, key_len, &ctx);
1345
++		}
1346
++
1347
++		/* Add key or last result.  */
1348
++		if ((cnt & 1) != 0) {
1349
++			sha256_process_bytes(alt_result, 32, &ctx);
1350
++		} else {
1351
++			sha256_process_bytes(p_bytes, key_len, &ctx);
1352
++		}
1353
++
1354
++		/* Create intermediate result.  */
1355
++		sha256_finish_ctx(&ctx, alt_result);
1356
++	}
1357
++
1358
++	/* Now we can construct the result string.  It consists of three
1359
++	parts.  */
1360
++	cp = stpncpy(buffer, sha256_salt_prefix, MAX(0, buflen));
1361
++	buflen -= sizeof(sha256_salt_prefix) - 1;
1362
++
1363
++	if (rounds_custom) {
1364
++#ifdef PHP_WIN32
1365
++		int n = _snprintf(cp, MAX(0, buflen), "%s%u$", sha256_rounds_prefix, rounds);
1366
++#else
1367
++		int n = snprintf(cp, MAX(0, buflen), "%s%zu$", sha256_rounds_prefix, rounds);
1368
++#endif
1369
++		cp += n;
1370
++		buflen -= n;
1371
++	}
1372
++
1373
++	cp = stpncpy(cp, salt, MIN ((size_t) MAX (0, buflen), salt_len));
1374
++	buflen -= MIN((size_t) MAX (0, buflen), salt_len);
1375
++
1376
++	if (buflen > 0) {
1377
++		*cp++ = '$';
1378
++		--buflen;
1379
++	}
1380
++
1381
++#define b64_from_24bit(B2, B1, B0, N)					      \
1382
++  do {									      \
1383
++    unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0);			      \
1384
++    int n = (N);							      \
1385
++    while (n-- > 0 && buflen > 0)					      \
1386
++      {									      \
1387
++	*cp++ = b64t[w & 0x3f];						      \
1388
++	--buflen;							      \
1389
++	w >>= 6;							      \
1390
++      }									      \
1391
++  } while (0)
1392
++
1393
++	b64_from_24bit(alt_result[0], alt_result[10], alt_result[20], 4);
1394
++	b64_from_24bit(alt_result[21], alt_result[1], alt_result[11], 4);
1395
++	b64_from_24bit(alt_result[12], alt_result[22], alt_result[2], 4);
1396
++	b64_from_24bit(alt_result[3], alt_result[13], alt_result[23], 4);
1397
++	b64_from_24bit(alt_result[24], alt_result[4], alt_result[14], 4);
1398
++	b64_from_24bit(alt_result[15], alt_result[25], alt_result[5], 4);
1399
++	b64_from_24bit(alt_result[6], alt_result[16], alt_result[26], 4);
1400
++	b64_from_24bit(alt_result[27], alt_result[7], alt_result[17], 4);
1401
++	b64_from_24bit(alt_result[18], alt_result[28], alt_result[8], 4);
1402
++	b64_from_24bit(alt_result[9], alt_result[19], alt_result[29], 4);
1403
++	b64_from_24bit(0, alt_result[31], alt_result[30], 3);
1404
++	if (buflen <= 0) {
1405
++		errno = ERANGE;
1406
++		buffer = NULL;
1407
++	} else
1408
++		*cp = '\0';		/* Terminate the string.  */
1409
++
1410
++	/* Clear the buffer for the intermediate result so that people
1411
++     attaching to processes or reading core dumps cannot get any
1412
++     information.  We do it in this way to clear correct_words[]
1413
++     inside the SHA256 implementation as well.  */
1414
++	sha256_init_ctx(&ctx);
1415
++	sha256_finish_ctx(&ctx, alt_result);
1416
++	memset(temp_result, '\0', sizeof(temp_result));
1417
++	memset(p_bytes, '\0', key_len);
1418
++	memset(s_bytes, '\0', salt_len);
1419
++	memset(&ctx, '\0', sizeof(ctx));
1420
++	memset(&alt_ctx, '\0', sizeof(alt_ctx));
1421
++
1422
++	if (copied_key != NULL) {
1423
++		memset(copied_key, '\0', key_len);
1424
++
1425
++	}
1426
++	if (copied_salt != NULL) {
1427
++		memset(copied_salt, '\0', salt_len);
1428
++	}
1429
++
1430
++	return buffer;
1431
++}
1432
++
1433
++
1434
++/* This entry point is equivalent to the `crypt' function in Unix
1435
++   libcs.  */
1436
++char * php_sha256_crypt(const char *key, const char *salt)
1437
++{
1438
++	/* We don't want to have an arbitrary limit in the size of the
1439
++	password.  We can compute an upper bound for the size of the
1440
++	result in advance and so we can prepare the buffer we pass to
1441
++	`sha256_crypt_r'.  */
1442
++	static char *buffer;
1443
++	static int buflen;
1444
++	int needed = (sizeof(sha256_salt_prefix) - 1
1445
++			+ sizeof(sha256_rounds_prefix) + 9 + 1
1446
++			+ strlen(salt) + 1 + 43 + 1);
1447
++
1448
++	if (buflen < needed) {
1449
++		char *new_buffer = (char *) realloc(buffer, needed);
1450
++		if (new_buffer == NULL) {
1451
++			return NULL;
1452
++		}
1453
++
1454
++		buffer = new_buffer;
1455
++		buflen = needed;
1456
++	}
1457
++
1458
++	return php_sha256_crypt_r(key, salt, buffer, buflen);
1459
++}
1460
++
1461
++
1462
++#ifdef TEST
1463
++static const struct
1464
++{
1465
++	const char *input;
1466
++	const char result[32];
1467
++} tests[] =
1468
++	{
1469
++	/* Test vectors from FIPS 180-2: appendix B.1.  */
1470
++	{ "abc",
1471
++	"\xba\x78\x16\xbf\x8f\x01\xcf\xea\x41\x41\x40\xde\x5d\xae\x22\x23"
1472
++	"\xb0\x03\x61\xa3\x96\x17\x7a\x9c\xb4\x10\xff\x61\xf2\x00\x15\xad" },
1473
++	/* Test vectors from FIPS 180-2: appendix B.2.  */
1474
++	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1475
++	"\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
1476
++	"\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
1477
++	/* Test vectors from the NESSIE project.  */
1478
++	{ "",
1479
++	"\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9\x24"
1480
++	"\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52\xb8\x55" },
1481
++	{ "a",
1482
++	"\xca\x97\x81\x12\xca\x1b\xbd\xca\xfa\xc2\x31\xb3\x9a\x23\xdc\x4d"
1483
++	"\xa7\x86\xef\xf8\x14\x7c\x4e\x72\xb9\x80\x77\x85\xaf\xee\x48\xbb" },
1484
++	{ "message digest",
1485
++	"\xf7\x84\x6f\x55\xcf\x23\xe1\x4e\xeb\xea\xb5\xb4\xe1\x55\x0c\xad"
1486
++	"\x5b\x50\x9e\x33\x48\xfb\xc4\xef\xa3\xa1\x41\x3d\x39\x3c\xb6\x50" },
1487
++	{ "abcdefghijklmnopqrstuvwxyz",
1488
++	"\x71\xc4\x80\xdf\x93\xd6\xae\x2f\x1e\xfa\xd1\x44\x7c\x66\xc9\x52"
1489
++	"\x5e\x31\x62\x18\xcf\x51\xfc\x8d\x9e\xd8\x32\xf2\xda\xf1\x8b\x73" },
1490
++	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
1491
++	"\x24\x8d\x6a\x61\xd2\x06\x38\xb8\xe5\xc0\x26\x93\x0c\x3e\x60\x39"
1492
++	"\xa3\x3c\xe4\x59\x64\xff\x21\x67\xf6\xec\xed\xd4\x19\xdb\x06\xc1" },
1493
++	{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
1494
++	"\xdb\x4b\xfc\xbd\x4d\xa0\xcd\x85\xa6\x0c\x3c\x37\xd3\xfb\xd8\x80"
1495
++	"\x5c\x77\xf1\x5f\xc6\xb1\xfd\xfe\x61\x4e\xe0\xa7\xc8\xfd\xb4\xc0" },
1496
++	{ "123456789012345678901234567890123456789012345678901234567890"
1497
++	"12345678901234567890",
1498
++	"\xf3\x71\xbc\x4a\x31\x1f\x2b\x00\x9e\xef\x95\x2d\xd8\x3c\xa8\x0e"
1499
++	"\x2b\x60\x02\x6c\x8e\x93\x55\x92\xd0\xf9\xc3\x08\x45\x3c\x81\x3e" }
1500
++  };
1501
++#define ntests (sizeof (tests) / sizeof (tests[0]))
1502
++
1503
++
1504
++static const struct
1505
++{
1506
++	const char *salt;
1507
++	const char *input;
1508
++	const char *expected;
1509
++} tests2[] =
1510
++{
1511
++	{ "$5$saltstring", "Hello world!",
1512
++	"$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5" },
1513
++	{ "$5$rounds=10000$saltstringsaltstring", "Hello world!",
1514
++	"$5$rounds=10000$saltstringsaltst$3xv.VbSHBb41AL9AvLeujZkZRBAwqFMz2."
1515
++	"opqey6IcA" },
1516
++	{ "$5$rounds=5000$toolongsaltstring", "This is just a test",
1517
++	"$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8"
1518
++	"mGRcvxa5" },
1519
++	{ "$5$rounds=1400$anotherlongsaltstring",
1520
++	"a very much longer text to encrypt.  This one even stretches over more"
1521
++	"than one line.",
1522
++	"$5$rounds=1400$anotherlongsalts$Rx.j8H.h8HjEDGomFU8bDkXm3XIUnzyxf12"
1523
++	"oP84Bnq1" },
1524
++	{ "$5$rounds=77777$short",
1525
++	"we have a short salt string but not a short password",
1526
++	"$5$rounds=77777$short$JiO1O3ZpDAxGJeaDIuqCoEFysAe1mZNJRs3pw0KQRd/" },
1527
++	{ "$5$rounds=123456$asaltof16chars..", "a short string",
1528
++	"$5$rounds=123456$asaltof16chars..$gP3VQ/6X7UUEW3HkBn2w1/Ptq2jxPyzV/"
1529
++	"cZKmF/wJvD" },
1530
++	{ "$5$rounds=10$roundstoolow", "the minimum number is still observed",
1531
++	"$5$rounds=1000$roundstoolow$yfvwcWrQ8l/K0DAWyuPMDNHpIVlTQebY9l/gL97"
1532
++	"2bIC" },
1533
++};
1534
++#define ntests2 (sizeof (tests2) / sizeof (tests2[0]))
1535
++
1536
++
1537
++int main(void) {
1538
++	struct sha256_ctx ctx;
1539
++	char sum[32];
1540
++	int result = 0;
1541
++	int cnt, i;
1542
++	char buf[1000];
1543
++	static const char expected[32] =
1544
++	"\xcd\xc7\x6e\x5c\x99\x14\xfb\x92\x81\xa1\xc7\xe2\x84\xd7\x3e\x67"
1545
++	"\xf1\x80\x9a\x48\xa4\x97\x20\x0e\x04\x6d\x39\xcc\xc7\x11\x2c\xd0";
1546
++
1547
++	for (cnt = 0; cnt < (int) ntests; ++cnt) {
1548
++		sha256_init_ctx(&ctx);
1549
++		sha256_process_bytes(tests[cnt].input, strlen(tests[cnt].input), &ctx);
1550
++		sha256_finish_ctx(&ctx, sum);
1551
++		if (memcmp(tests[cnt].result, sum, 32) != 0) {
1552
++			printf("test %d run %d failed\n", cnt, 1);
1553
++			result = 1;
1554
++		}
1555
++
1556
++		sha256_init_ctx(&ctx);
1557
++		for (i = 0; tests[cnt].input[i] != '\0'; ++i) {
1558
++			sha256_process_bytes(&tests[cnt].input[i], 1, &ctx);
1559
++		}
1560
++		sha256_finish_ctx(&ctx, sum);
1561
++		if (memcmp(tests[cnt].result, sum, 32) != 0) {
1562
++			printf("test %d run %d failed\n", cnt, 2);
1563
++			result = 1;
1564
++		}
1565
++	}
1566
++
1567
++	/* Test vector from FIPS 180-2: appendix B.3.  */
1568
++
1569
++	memset(buf, 'a', sizeof(buf));
1570
++	sha256_init_ctx(&ctx);
1571
++	for (i = 0; i < 1000; ++i) {
1572
++		sha256_process_bytes (buf, sizeof (buf), &ctx);
1573
++	}
1574
++
1575
++	sha256_finish_ctx(&ctx, sum);
1576
++
1577
++	if (memcmp(expected, sum, 32) != 0) {
1578
++		printf("test %d failed\n", cnt);
1579
++		result = 1;
1580
++	}
1581
++
1582
++	for (cnt = 0; cnt < ntests2; ++cnt) {
1583
++		char *cp = php_sha256_crypt(tests2[cnt].input, tests2[cnt].salt);
1584
++		if (strcmp(cp, tests2[cnt].expected) != 0) {
1585
++			printf("test %d: expected \"%s\", got \"%s\"\n", cnt, tests2[cnt].expected, cp);
1586
++			result = 1;
1587
++		}
1588
++	}
1589
++
1590
++	if (result == 0)
1591
++	puts("all tests OK");
1592
++
1593
++	return result;
1594
++}
1595
++#endif
1596
+Index: ext/standard/config.w32
1597
+===================================================================
1598
+--- ext/standard/config.w32	(Revision 291898)
1599
++++ ext/standard/config.w32	(Revision 291899)
1600
+@@ -9,8 +9,8 @@
1601
+ CHECK_HEADER_ADD_INCLUDE("timelib_config.h", "CFLAGS_STANDARD", "ext/date/lib");
1602
+ 
1603
+ EXTENSION("standard", "array.c base64.c basic_functions.c browscap.c \
1604
+-	crc32.c crypt.c \
1605
+-	crypt_freesec.c crypt_blowfish.c php_crypt_r.c \
1606
++	crc32.c crypt.c crypt_freesec.c crypt_blowfish.c crypt_sha256.c \
1607
++	crypt_sha512.c  php_crypt_r.c \
1608
+ 	cyr_convert.c datetime.c dir.c dl.c dns.c dns_win32.c exec.c \
1609
+ 	file.c filestat.c formatted_print.c fsock.c head.c html.c image.c \
1610
+ 	info.c iptc.c lcg.c link_win32.c mail.c math.c md5.c metaphone.c microtime.c \
1611
+Index: ext/standard/php_crypt_r.h
1612
+===================================================================
1613
+--- ext/standard/php_crypt_r.h	(Revision 291898)
1614
++++ ext/standard/php_crypt_r.h	(Revision 291899)
1615
+@@ -49,6 +49,8 @@
1616
+ extern char * php_md5_crypt_r(const char *pw, const char *salt, char *out);
1617
+ extern char * php_crypt_blowfish_rn(__CONST char *key, __CONST char *setting,
1618
+ 	char *output, int size);
1619
++extern char * php_sha512_crypt_r (const char *key, const char *salt, char *buffer, int buflen);
1620
++extern char * php_sha256_crypt_r (const char *key, const char *salt, char *buffer, int buflen);
1621
+ 
1622
+ #ifdef __cplusplus
1623
+ }
1624
+Index: ext/standard/crypt.c
1625
+===================================================================
1626
+--- ext/standard/crypt.c	(Revision 291898)
1627
++++ ext/standard/crypt.c	(Revision 291899)
1628
+@@ -82,6 +82,12 @@
1629
+ #define PHP_MAX_SALT_LEN 60
1630
+ #endif
1631
+ 
1632
++#if PHP_SHA512_CRYPT
1633
++#undef PHP_MAX_SALT_LEN
1634
++#define PHP_MAX_SALT_LEN 123
1635
++#endif
1636
++
1637
++
1638
+ /* If the configure-time checks fail, we provide DES.
1639
+  * XXX: This is a hack. Fix the real problem! */
1640
+ 
1641
+@@ -163,6 +169,7 @@
1642
+ 		php_to64(&salt[0], PHP_CRYPT_RAND, 2);
1643
+ 		salt[2] = '\0';
1644
+ #endif
1645
++		salt_in_len = strlen(salt);
1646
+ 	}
1647
+ 
1648
+ /* Windows (win32/crypt) has a stripped down version of libxcrypt and 
1649
+@@ -175,7 +182,36 @@
1650
+ 			char output[MD5_HASH_MAX_LEN];
1651
+ 
1652
+ 			RETURN_STRING(php_md5_crypt_r(str, salt, output), 1);
1653
+-		} else  if (
1654
++		} else if (salt[0]=='$' && salt[1]=='6' && salt[2]=='$') {
1655
++			const char sha512_salt_prefix[] = "$6$";
1656
++			const char sha512_rounds_prefix[] = "rounds=";
1657
++			char *output;
1658
++			int needed = (sizeof(sha512_salt_prefix) - 1
1659
++						+ sizeof(sha512_rounds_prefix) + 9 + 1
1660
++						+ strlen(salt) + 1 + 43 + 1);
1661
++			output = emalloc(needed * sizeof(char *));
1662
++			salt[salt_in_len] = '\0';
1663
++
1664
++			php_sha512_crypt_r(str, salt, output, needed);
1665
++
1666
++			RETVAL_STRING(output, 1);
1667
++			memset(output, 0, PHP_MAX_SALT_LEN + 1);
1668
++			efree(output);
1669
++		} else if (salt[0]=='$' && salt[1]=='5' && salt[2]=='$') {
1670
++			const char sha256_salt_prefix[] = "$5$";
1671
++			const char sha256_rounds_prefix[] = "rounds=";
1672
++			char *output;
1673
++			int needed = (sizeof(sha256_salt_prefix) - 1
1674
++						+ sizeof(sha256_rounds_prefix) + 9 + 1
1675
++						+ strlen(salt) + 1 + 43 + 1);
1676
++			output = emalloc(needed * sizeof(char *));
1677
++			salt[salt_in_len] = '\0';
1678
++			php_sha256_crypt_r(str, salt, output, needed);
1679
++
1680
++			RETVAL_STRING(output, 1);
1681
++			memset(output, 0, PHP_MAX_SALT_LEN + 1);
1682
++			efree(output);
1683
++		} else if (
1684
+ 				salt[0] == '$' &&
1685
+ 				salt[1] == '2' &&
1686
+ 				salt[2] == 'a' &&
1687
+Index: ext/standard/config.m4
1688
+===================================================================
1689
+--- ext/standard/config.m4	(Revision 291898)
1690
++++ ext/standard/config.m4	(Revision 291899)
1691
+@@ -172,6 +172,65 @@
1692
+   ac_cv_crypt_blowfish=no
1693
+ ])])
1694
+ 
1695
++AC_CACHE_CHECK(for SHA512 crypt, ac_cv_crypt_SHA512,[
1696
++AC_TRY_RUN([
1697
++#if HAVE_UNISTD_H
1698
++#include <unistd.h>
1699
++#endif
1700
++
1701
++#if HAVE_CRYPT_H
1702
++#include <crypt.h>
1703
++#endif
1704
++
1705
++main() {
1706
++#if HAVE_CRYPT
1707
++    char salt[30], answer[80];
1708
++    
1709
++    salt[0]='$'; salt[1]='6'; salt[2]='$'; salt[3]='$'; salt[4]='b'; salt[5]='a'; salt[6]='r'; salt[7]='\0';
1710
++    strcpy(answer, salt);
1711
++    strcpy(&answer[29],"$6$$QMXjqd7rHQZPQ1yHsXkQqC1FBzDiVfTHXL.LaeDAeVV.IzMaV9VU4MQ8kPuZa2SOP1A0RPm772EaFYjpEJtdu.");
1712
++    exit (strcmp((char *)crypt("foo",salt),answer));
1713
++#else
1714
++	exit(0);
1715
++#endif
1716
++}],[
1717
++  ac_cv_crypt_SHA512=yes
1718
++],[
1719
++  ac_cv_crypt_SHA512=no
1720
++],[
1721
++  ac_cv_crypt_SHA512=no
1722
++])])
1723
++
1724
++AC_CACHE_CHECK(for SHA256 crypt, ac_cv_crypt_SHA256,[
1725
++AC_TRY_RUN([
1726
++#if HAVE_UNISTD_H
1727
++#include <unistd.h>
1728
++#endif
1729
++
1730
++#if HAVE_CRYPT_H
1731
++#include <crypt.h>
1732
++#endif
1733
++
1734
++main() {
1735
++#if HAVE_CRYPT
1736
++    char salt[30], answer[80];
1737
++    salt[0]='$'; salt[1]='5'; salt[2]='$'; salt[3]='$'; salt[4]='s'; salt[5]='a'; salt[6]='l'; salt[7]='t';  salt[8]='s'; salt[9]='t'; salt[10]='r'; salt[11]='i'; salt[12]='n'; salt[13]='g'; salt[14]='\0';    
1738
++    strcat(salt,"");
1739
++    strcpy(answer, salt);
1740
++    strcpy(&answer[29], "$5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5");
1741
++    exit (strcmp((char *)crypt("foo",salt),answer));
1742
++#else
1743
++	exit(0);
1744
++#endif
1745
++}],[
1746
++  ac_cv_crypt_SHA256=yes
1747
++],[
1748
++  ac_cv_crypt_SHA256=no
1749
++],[
1750
++  ac_cv_crypt_SHA256=no
1751
++])])
1752
++
1753
++
1754
+ dnl
1755
+ dnl If one of them is missing, use our own implementation, portable code is then possible
1756
+ dnl
1757
+@@ -181,8 +240,10 @@
1758
+   AC_DEFINE_UNQUOTED(PHP_BLOWFISH_CRYPT, 1, [Whether the system supports BlowFish salt])
1759
+   AC_DEFINE_UNQUOTED(PHP_EXT_DES_CRYPT, 1, [Whether the system supports extended DES salt])
1760
+   AC_DEFINE_UNQUOTED(PHP_MD5_CRYPT, 1, [Whether the system supports extended DES salt])
1761
++  AC_DEFINE_UNQUOTED(PHP_SHA512_CRYPT, 1, [Whether the system supports SHA512 salt])
1762
++  AC_DEFINE_UNQUOTED(PHP_SHA256_CRYPT, 1, [Whether the system supports SHA256 salt])
1763
+ 
1764
+-  PHP_ADD_SOURCES(PHP_EXT_DIR(standard), crypt_freesec.c crypt_blowfish.c php_crypt_r.c)
1765
++  PHP_ADD_SOURCES(PHP_EXT_DIR(standard), crypt_freesec.c crypt_blowfish.c crypt_sha512.c crypt_sha256.c php_crypt_r.c)
1766
+ else
1767
+   if test "$ac_cv_crypt_des" = "yes"; then
1768
+     ac_result=1
1769
+@@ -211,13 +272,31 @@
1770
+   fi
1771
+   AC_DEFINE_UNQUOTED(PHP_EXT_DES_CRYPT, $ac_result, [Whether the system supports extended DES salt])
1772
+ 
1773
++  if test "$ac_cv_crypt_sha512" = "yes"; then
1774
++    ac_result=1
1775
++    ac_crypt_sha512=1
1776
++  else
1777
++    ac_result=0
1778
++    ac_crypt_sha512=0
1779
++  fi
1780
++  AC_DEFINE_UNQUOTED(PHP_EXT_SHA512_CRYPT, $ac_result, [Whether the system supports SHA512 salt])
1781
++
1782
++  if test "$ac_cv_crypt_sha256" = "yes"; then
1783
++    ac_result=1
1784
++    ac_crypt_sha256=1
1785
++  else
1786
++    ac_result=0
1787
++    ac_crypt_sha256=0
1788
++  fi
1789
++  AC_DEFINE_UNQUOTED(PHP_EXT_SHA256_CRYPT, $ac_result, [Whether the system supports SHA256 salt])
1790
++
1791
+   AC_DEFINE_UNQUOTED(PHP_USE_PHP_CRYPT_R, 0, [Whether PHP has to use its own crypt_r for blowfish, des and ext des])
1792
+ fi
1793
+ 
1794
+ dnl
1795
+ dnl Check for available functions
1796
+ dnl
1797
+-AC_CHECK_FUNCS(getcwd getwd asinh acosh atanh log1p hypot glob strfmon nice fpclass isinf isnan)
1798
++AC_CHECK_FUNCS(getcwd getwd asinh acosh atanh log1p hypot glob strfmon nice fpclass isinf isnan mempcpy strpncpy)
1799
+ AC_FUNC_FNMATCH	
1800
+ 
1801
+ divert(5)dnl
... ...
@@ -134,6 +134,8 @@ src_unpack() {
134 134
 
135 135
 	cd "${S}"
136 136
 
137
+	epatch "${FILESDIR}/${P}-sha512.diff"
138
+
137 139
 	# Concurrent PHP Apache2 modules support
138 140
 	if use apache2 ; then
139 141
 		if use concurrentmodphp ; then
140 142