1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Steffan Esser <sesser@php.net> |
16 : | Sara Golemon <pollita@php.net> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: hash_sha.c 276986 2009-03-10 23:40:06Z helly $ */
21 :
22 : #include "php_hash.h"
23 : #include "php_hash_sha.h"
24 :
25 : static const unsigned char PADDING[128] =
26 : {
27 : 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
35 : };
36 :
37 : /* {{{ SHAEncode32
38 : Encodes input (php_hash_uint32) into output (unsigned char). Assumes len is
39 : a multiple of 4.
40 : */
41 : static void SHAEncode32(unsigned char *output, php_hash_uint32 *input, unsigned int len)
42 38 : {
43 : unsigned int i, j;
44 :
45 332 : for (i = 0, j = 0; j < len; i++, j += 4) {
46 294 : output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
47 294 : output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
48 294 : output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
49 294 : output[j + 3] = (unsigned char) (input[i] & 0xff);
50 : }
51 38 : }
52 : /* }}} */
53 :
54 :
55 : /* {{{ SHADecode32
56 : Decodes input (unsigned char) into output (php_hash_uint32). Assumes len is
57 : a multiple of 4.
58 : */
59 : static void SHADecode32(php_hash_uint32 *output, const unsigned char *input, unsigned int len)
60 31456 : {
61 : unsigned int i, j;
62 :
63 534752 : for (i = 0, j = 0; j < len; i++, j += 4)
64 503296 : output[i] = ((php_hash_uint32) input[j + 3]) | (((php_hash_uint32) input[j + 2]) << 8) |
65 : (((php_hash_uint32) input[j + 1]) << 16) | (((php_hash_uint32) input[j]) << 24);
66 31456 : }
67 : /* }}} */
68 :
69 : const php_hash_ops php_hash_sha1_ops = {
70 : (php_hash_init_func_t) PHP_SHA1Init,
71 : (php_hash_update_func_t) PHP_SHA1Update,
72 : (php_hash_final_func_t) PHP_SHA1Final,
73 : (php_hash_copy_func_t) php_hash_copy,
74 : 20,
75 : 64,
76 : sizeof(PHP_SHA1_CTX)
77 : };
78 :
79 : /* sha224/sha256 */
80 :
81 : const php_hash_ops php_hash_sha256_ops = {
82 : (php_hash_init_func_t) PHP_SHA256Init,
83 : (php_hash_update_func_t) PHP_SHA256Update,
84 : (php_hash_final_func_t) PHP_SHA256Final,
85 : (php_hash_copy_func_t) php_hash_copy,
86 : 32,
87 : 64,
88 : sizeof(PHP_SHA256_CTX)
89 : };
90 :
91 : const php_hash_ops php_hash_sha224_ops = {
92 : (php_hash_init_func_t) PHP_SHA224Init,
93 : (php_hash_update_func_t) PHP_SHA224Update,
94 : (php_hash_final_func_t) PHP_SHA224Final,
95 : (php_hash_copy_func_t) php_hash_copy,
96 : 28,
97 : 64,
98 : sizeof(PHP_SHA224_CTX)
99 : };
100 :
101 : #define ROTR32(b,x) ((x >> b) | (x << (32 - b)))
102 : #define ROTR64(b,x) ((x >> b) | (x << (64 - b)))
103 : #define SHR(b, x) (x >> b)
104 :
105 : /* Ch */
106 : #define SHA256_F0(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
107 : /* Maj */
108 : #define SHA256_F1(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
109 : /* SUM0 */
110 : #define SHA256_F2(x) (ROTR32( 2,(x)) ^ ROTR32(13,(x)) ^ ROTR32(22,(x)))
111 : /* SUM1 */
112 : #define SHA256_F3(x) (ROTR32( 6,(x)) ^ ROTR32(11,(x)) ^ ROTR32(25,(x)))
113 : /* OM0 */
114 : #define SHA256_F4(x) (ROTR32( 7,(x)) ^ ROTR32(18,(x)) ^ SHR( 3,(x)))
115 : /* OM1 */
116 : #define SHA256_F5(x) (ROTR32(17,(x)) ^ ROTR32(19,(x)) ^ SHR(10,(x)))
117 :
118 : static const php_hash_uint32 SHA256_K[64] = {
119 : 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
120 : 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
121 : 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
122 : 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
123 : 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
124 : 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
125 : 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
126 : 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
127 :
128 : /* {{{ PHP_SHA256Init
129 : * SHA256 initialization. Begins an SHA256 operation, writing a new context.
130 : */
131 : PHP_HASH_API void PHP_SHA256Init(PHP_SHA256_CTX * context)
132 28 : {
133 28 : context->count[0] = context->count[1] = 0;
134 : /* Load magic initialization constants.
135 : */
136 28 : context->state[0] = 0x6a09e667;
137 28 : context->state[1] = 0xbb67ae85;
138 28 : context->state[2] = 0x3c6ef372;
139 28 : context->state[3] = 0xa54ff53a;
140 28 : context->state[4] = 0x510e527f;
141 28 : context->state[5] = 0x9b05688c;
142 28 : context->state[6] = 0x1f83d9ab;
143 28 : context->state[7] = 0x5be0cd19;
144 28 : }
145 : /* }}} */
146 :
147 : /* {{{ SHA256Transform
148 : * SHA256 basic transformation. Transforms state based on block.
149 : */
150 : static void SHA256Transform(php_hash_uint32 state[8], const unsigned char block[64])
151 31456 : {
152 31456 : php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3];
153 31456 : php_hash_uint32 e = state[4], f = state[5], g = state[6], h = state[7];
154 : php_hash_uint32 x[16], T1, T2, W[64];
155 : int i;
156 :
157 31456 : SHADecode32(x, block, 64);
158 :
159 : /* Schedule */
160 534752 : for(i = 0; i < 16; i++) {
161 503296 : W[i] = x[i];
162 : }
163 1541344 : for(i = 16; i < 64; i++) {
164 1509888 : W[i] = SHA256_F5(W[i-2]) + W[i-7] + SHA256_F4(W[i-15]) + W[i-16];
165 : }
166 :
167 2044640 : for (i = 0; i < 64; i++) {
168 2013184 : T1 = h + SHA256_F3(e) + SHA256_F0(e,f,g) + SHA256_K[i] + W[i];
169 2013184 : T2 = SHA256_F2(a) + SHA256_F1(a,b,c);
170 2013184 : h = g; g = f; f = e; e = d + T1;
171 2013184 : d = c; c = b; b = a; a = T1 + T2;
172 : }
173 :
174 31456 : state[0] += a;
175 31456 : state[1] += b;
176 31456 : state[2] += c;
177 31456 : state[3] += d;
178 31456 : state[4] += e;
179 31456 : state[5] += f;
180 31456 : state[6] += g;
181 31456 : state[7] += h;
182 :
183 : /* Zeroize sensitive information. */
184 31456 : memset((unsigned char*) x, 0, sizeof(x));
185 31456 : }
186 : /* }}} */
187 :
188 : /* {{{ PHP_SHA224Init
189 : * SHA224 initialization. Begins an SHA224 operation, writing a new context.
190 : */
191 : PHP_HASH_API void PHP_SHA224Init(PHP_SHA224_CTX * context)
192 10 : {
193 10 : context->count[0] = context->count[1] = 0;
194 : /* Load magic initialization constants.
195 : */
196 10 : context->state[0] = 0xc1059ed8;
197 10 : context->state[1] = 0x367cd507;
198 10 : context->state[2] = 0x3070dd17;
199 10 : context->state[3] = 0xf70e5939;
200 10 : context->state[4] = 0xffc00b31;
201 10 : context->state[5] = 0x68581511;
202 10 : context->state[6] = 0x64f98fa7;
203 10 : context->state[7] = 0xbefa4fa4;
204 10 : }
205 : /* }}} */
206 :
207 : /* {{{ PHP_SHA224Update
208 : SHA224 block update operation. Continues an SHA224 message-digest
209 : operation, processing another message block, and updating the
210 : context.
211 : */
212 : PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX * context, const unsigned char *input, unsigned int inputLen)
213 29 : {
214 : unsigned int i, index, partLen;
215 :
216 : /* Compute number of bytes mod 64 */
217 29 : index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
218 :
219 : /* Update number of bits */
220 29 : if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
221 0 : context->count[1]++;
222 : }
223 29 : context->count[1] += ((php_hash_uint32) inputLen >> 29);
224 :
225 29 : partLen = 64 - index;
226 :
227 : /* Transform as many times as possible.
228 : */
229 29 : if (inputLen >= partLen) {
230 14 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
231 14 : SHA256Transform(context->state, context->buffer);
232 :
233 15638 : for (i = partLen; i + 63 < inputLen; i += 64) {
234 15624 : SHA256Transform(context->state, &input[i]);
235 : }
236 :
237 14 : index = 0;
238 : } else {
239 15 : i = 0;
240 : }
241 :
242 : /* Buffer remaining input */
243 29 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
244 29 : }
245 : /* }}} */
246 :
247 : /* {{{ PHP_SHA224Final
248 : SHA224 finalization. Ends an SHA224 message-digest operation, writing the
249 : the message digest and zeroizing the context.
250 : */
251 : PHP_HASH_API void PHP_SHA224Final(unsigned char digest[28], PHP_SHA224_CTX * context)
252 10 : {
253 : unsigned char bits[8];
254 : unsigned int index, padLen;
255 :
256 : /* Save number of bits */
257 10 : bits[7] = (unsigned char) (context->count[0] & 0xFF);
258 10 : bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
259 10 : bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
260 10 : bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
261 10 : bits[3] = (unsigned char) (context->count[1] & 0xFF);
262 10 : bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
263 10 : bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
264 10 : bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
265 :
266 : /* Pad out to 56 mod 64.
267 : */
268 10 : index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
269 10 : padLen = (index < 56) ? (56 - index) : (120 - index);
270 10 : PHP_SHA224Update(context, PADDING, padLen);
271 :
272 : /* Append length (before padding) */
273 10 : PHP_SHA224Update(context, bits, 8);
274 :
275 : /* Store state in digest */
276 10 : SHAEncode32(digest, context->state, 28);
277 :
278 : /* Zeroize sensitive information.
279 : */
280 10 : memset((unsigned char*) context, 0, sizeof(*context));
281 10 : }
282 : /* }}} */
283 :
284 : /* {{{ PHP_SHA256Update
285 : SHA256 block update operation. Continues an SHA256 message-digest
286 : operation, processing another message block, and updating the
287 : context.
288 : */
289 : PHP_HASH_API void PHP_SHA256Update(PHP_SHA256_CTX * context, const unsigned char *input, unsigned int inputLen)
290 98 : {
291 : unsigned int i, index, partLen;
292 :
293 : /* Compute number of bytes mod 64 */
294 98 : index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
295 :
296 : /* Update number of bits */
297 98 : if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
298 0 : context->count[1]++;
299 : }
300 98 : context->count[1] += ((php_hash_uint32) inputLen >> 29);
301 :
302 98 : partLen = 64 - index;
303 :
304 : /* Transform as many times as possible.
305 : */
306 98 : if (inputLen >= partLen) {
307 58 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
308 58 : SHA256Transform(context->state, context->buffer);
309 :
310 15818 : for (i = partLen; i + 63 < inputLen; i += 64) {
311 15760 : SHA256Transform(context->state, &input[i]);
312 : }
313 :
314 58 : index = 0;
315 : } else {
316 40 : i = 0;
317 : }
318 :
319 : /* Buffer remaining input */
320 98 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
321 98 : }
322 : /* }}} */
323 :
324 : /* {{{ PHP_SHA256Final
325 : SHA256 finalization. Ends an SHA256 message-digest operation, writing the
326 : the message digest and zeroizing the context.
327 : */
328 : PHP_HASH_API void PHP_SHA256Final(unsigned char digest[32], PHP_SHA256_CTX * context)
329 28 : {
330 : unsigned char bits[8];
331 : unsigned int index, padLen;
332 :
333 : /* Save number of bits */
334 28 : bits[7] = (unsigned char) (context->count[0] & 0xFF);
335 28 : bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
336 28 : bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
337 28 : bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
338 28 : bits[3] = (unsigned char) (context->count[1] & 0xFF);
339 28 : bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
340 28 : bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
341 28 : bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
342 :
343 : /* Pad out to 56 mod 64.
344 : */
345 28 : index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
346 28 : padLen = (index < 56) ? (56 - index) : (120 - index);
347 28 : PHP_SHA256Update(context, PADDING, padLen);
348 :
349 : /* Append length (before padding) */
350 28 : PHP_SHA256Update(context, bits, 8);
351 :
352 : /* Store state in digest */
353 28 : SHAEncode32(digest, context->state, 32);
354 :
355 : /* Zeroize sensitive information.
356 : */
357 28 : memset((unsigned char*) context, 0, sizeof(*context));
358 28 : }
359 : /* }}} */
360 :
361 : /* sha384/sha512 */
362 :
363 : /* Ch */
364 : #define SHA512_F0(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
365 : /* Maj */
366 : #define SHA512_F1(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
367 : /* SUM0 */
368 : #define SHA512_F2(x) (ROTR64(28, x) ^ ROTR64(34, x) ^ ROTR64(39, x))
369 : /* SUM1 */
370 : #define SHA512_F3(x) (ROTR64(14, x) ^ ROTR64(18, x) ^ ROTR64(41, x))
371 : /* OM0 */
372 : #define SHA512_F4(x) (ROTR64( 1, x) ^ ROTR64( 8, x) ^ SHR(7, x))
373 : /* OM1 */
374 : #define SHA512_F5(x) (ROTR64(19, x) ^ ROTR64(61, x) ^ SHR(6, x))
375 :
376 : static const php_hash_uint64 SHA512_K[128] = {
377 : L64(0x428a2f98d728ae22), L64(0x7137449123ef65cd), L64(0xb5c0fbcfec4d3b2f), L64(0xe9b5dba58189dbbc),
378 : L64(0x3956c25bf348b538), L64(0x59f111f1b605d019), L64(0x923f82a4af194f9b), L64(0xab1c5ed5da6d8118),
379 : L64(0xd807aa98a3030242), L64(0x12835b0145706fbe), L64(0x243185be4ee4b28c), L64(0x550c7dc3d5ffb4e2),
380 : L64(0x72be5d74f27b896f), L64(0x80deb1fe3b1696b1), L64(0x9bdc06a725c71235), L64(0xc19bf174cf692694),
381 : L64(0xe49b69c19ef14ad2), L64(0xefbe4786384f25e3), L64(0x0fc19dc68b8cd5b5), L64(0x240ca1cc77ac9c65),
382 : L64(0x2de92c6f592b0275), L64(0x4a7484aa6ea6e483), L64(0x5cb0a9dcbd41fbd4), L64(0x76f988da831153b5),
383 : L64(0x983e5152ee66dfab), L64(0xa831c66d2db43210), L64(0xb00327c898fb213f), L64(0xbf597fc7beef0ee4),
384 : L64(0xc6e00bf33da88fc2), L64(0xd5a79147930aa725), L64(0x06ca6351e003826f), L64(0x142929670a0e6e70),
385 : L64(0x27b70a8546d22ffc), L64(0x2e1b21385c26c926), L64(0x4d2c6dfc5ac42aed), L64(0x53380d139d95b3df),
386 : L64(0x650a73548baf63de), L64(0x766a0abb3c77b2a8), L64(0x81c2c92e47edaee6), L64(0x92722c851482353b),
387 : L64(0xa2bfe8a14cf10364), L64(0xa81a664bbc423001), L64(0xc24b8b70d0f89791), L64(0xc76c51a30654be30),
388 : L64(0xd192e819d6ef5218), L64(0xd69906245565a910), L64(0xf40e35855771202a), L64(0x106aa07032bbd1b8),
389 : L64(0x19a4c116b8d2d0c8), L64(0x1e376c085141ab53), L64(0x2748774cdf8eeb99), L64(0x34b0bcb5e19b48a8),
390 : L64(0x391c0cb3c5c95a63), L64(0x4ed8aa4ae3418acb), L64(0x5b9cca4f7763e373), L64(0x682e6ff3d6b2b8a3),
391 : L64(0x748f82ee5defb2fc), L64(0x78a5636f43172f60), L64(0x84c87814a1f0ab72), L64(0x8cc702081a6439ec),
392 : L64(0x90befffa23631e28), L64(0xa4506cebde82bde9), L64(0xbef9a3f7b2c67915), L64(0xc67178f2e372532b),
393 : L64(0xca273eceea26619c), L64(0xd186b8c721c0c207), L64(0xeada7dd6cde0eb1e), L64(0xf57d4f7fee6ed178),
394 : L64(0x06f067aa72176fba), L64(0x0a637dc5a2c898a6), L64(0x113f9804bef90dae), L64(0x1b710b35131c471b),
395 : L64(0x28db77f523047d84), L64(0x32caab7b40c72493), L64(0x3c9ebe0a15c9bebc), L64(0x431d67c49c100d4c),
396 : L64(0x4cc5d4becb3e42b6), L64(0x597f299cfc657e2a), L64(0x5fcb6fab3ad6faec), L64(0x6c44198c4a475817) };
397 :
398 : /* {{{ SHAEncode64
399 : Encodes input (php_hash_uint64) into output (unsigned char). Assumes len is
400 : a multiple of 8.
401 : */
402 : static void SHAEncode64(unsigned char *output, php_hash_uint64 *input, unsigned int len)
403 38 : {
404 : unsigned int i, j;
405 :
406 312 : for (i = 0, j = 0; j < len; i++, j += 8) {
407 274 : output[j] = (unsigned char) ((input[i] >> 56) & 0xff);
408 274 : output[j + 1] = (unsigned char) ((input[i] >> 48) & 0xff);
409 274 : output[j + 2] = (unsigned char) ((input[i] >> 40) & 0xff);
410 274 : output[j + 3] = (unsigned char) ((input[i] >> 32) & 0xff);
411 274 : output[j + 4] = (unsigned char) ((input[i] >> 24) & 0xff);
412 274 : output[j + 5] = (unsigned char) ((input[i] >> 16) & 0xff);
413 274 : output[j + 6] = (unsigned char) ((input[i] >> 8) & 0xff);
414 274 : output[j + 7] = (unsigned char) (input[i] & 0xff);
415 : }
416 38 : }
417 : /* }}} */
418 :
419 :
420 : /* {{{ SHADecode64
421 : Decodes input (unsigned char) into output (php_hash_uint64). Assumes len is
422 : a multiple of 8.
423 : */
424 : static void SHADecode64(php_hash_uint64 *output, const unsigned char *input, unsigned int len)
425 15744 : {
426 : unsigned int i, j;
427 :
428 267648 : for (i = 0, j = 0; j < len; i++, j += 8)
429 251904 : output[i] =
430 : ((php_hash_uint64) input[j + 7]) | (((php_hash_uint64) input[j + 6]) << 8) |
431 : (((php_hash_uint64) input[j + 5]) << 16) | (((php_hash_uint64) input[j + 4]) << 24) |
432 : (((php_hash_uint64) input[j + 3]) << 32) | (((php_hash_uint64) input[j + 2]) << 40) |
433 : (((php_hash_uint64) input[j + 1]) << 48) | (((php_hash_uint64) input[j]) << 56);
434 15744 : }
435 : /* }}} */
436 :
437 : /* {{{ PHP_SHA384Init
438 : * SHA384 initialization. Begins an SHA384 operation, writing a new context.
439 : */
440 : PHP_HASH_API void PHP_SHA384Init(PHP_SHA384_CTX * context)
441 15 : {
442 15 : context->count[0] = context->count[1] = 0;
443 : /* Load magic initialization constants.
444 : */
445 15 : context->state[0] = L64(0xcbbb9d5dc1059ed8);
446 15 : context->state[1] = L64(0x629a292a367cd507);
447 15 : context->state[2] = L64(0x9159015a3070dd17);
448 15 : context->state[3] = L64(0x152fecd8f70e5939);
449 15 : context->state[4] = L64(0x67332667ffc00b31);
450 15 : context->state[5] = L64(0x8eb44a8768581511);
451 15 : context->state[6] = L64(0xdb0c2e0d64f98fa7);
452 15 : context->state[7] = L64(0x47b5481dbefa4fa4);
453 15 : }
454 : /* }}} */
455 :
456 : /* {{{ SHA512Transform
457 : * SHA512 basic transformation. Transforms state based on block.
458 : * SHA384 uses the exact same algorithm
459 : */
460 : static void SHA512Transform(php_hash_uint64 state[8], const unsigned char block[128])
461 15744 : {
462 15744 : php_hash_uint64 a = state[0], b = state[1], c = state[2], d = state[3];
463 15744 : php_hash_uint64 e = state[4], f = state[5], g = state[6], h = state[7];
464 : php_hash_uint64 x[16], T1, T2, W[80];
465 : int i;
466 :
467 15744 : SHADecode64(x, block, 128);
468 :
469 : /* Schedule */
470 267648 : for(i = 0; i < 16; i++) {
471 251904 : W[i] = x[i];
472 : }
473 1023360 : for(i = 16; i < 80; i++) {
474 1007616 : W[i] = SHA512_F5(W[i-2]) + W[i-7] + SHA512_F4(W[i-15]) + W[i-16];
475 : }
476 :
477 1275264 : for (i = 0; i < 80; i++) {
478 1259520 : T1 = h + SHA512_F3(e) + SHA512_F0(e,f,g) + SHA512_K[i] + W[i];
479 1259520 : T2 = SHA512_F2(a) + SHA512_F1(a,b,c);
480 1259520 : h = g; g = f; f = e; e = d + T1;
481 1259520 : d = c; c = b; b = a; a = T1 + T2;
482 : }
483 :
484 15744 : state[0] += a;
485 15744 : state[1] += b;
486 15744 : state[2] += c;
487 15744 : state[3] += d;
488 15744 : state[4] += e;
489 15744 : state[5] += f;
490 15744 : state[6] += g;
491 15744 : state[7] += h;
492 :
493 : /* Zeroize sensitive information. */
494 15744 : memset((unsigned char*) x, 0, sizeof(x));
495 15744 : }
496 : /* }}} */
497 :
498 : /* {{{ PHP_SHA384Update
499 : SHA384 block update operation. Continues an SHA384 message-digest
500 : operation, processing another message block, and updating the
501 : context.
502 : */
503 : PHP_HASH_API void PHP_SHA384Update(PHP_SHA384_CTX * context, const unsigned char *input, unsigned int inputLen)
504 48 : {
505 : unsigned int i, index, partLen;
506 :
507 : /* Compute number of bytes mod 128 */
508 48 : index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
509 :
510 : /* Update number of bits */
511 48 : if ((context->count[0] += ((php_hash_uint64) inputLen << 3)) < ((php_hash_uint64) inputLen << 3)) {
512 0 : context->count[1]++;
513 : }
514 48 : context->count[1] += ((php_hash_uint64) inputLen >> 61);
515 :
516 48 : partLen = 128 - index;
517 :
518 : /* Transform as many times as possible.
519 : */
520 48 : if (inputLen >= partLen) {
521 21 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
522 21 : SHA512Transform(context->state, context->buffer);
523 :
524 7832 : for (i = partLen; i + 127 < inputLen; i += 128) {
525 7811 : SHA512Transform(context->state, &input[i]);
526 : }
527 :
528 21 : index = 0;
529 : } else {
530 27 : i = 0;
531 : }
532 :
533 : /* Buffer remaining input */
534 48 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
535 48 : }
536 : /* }}} */
537 :
538 : /* {{{ PHP_SHA384Final
539 : SHA384 finalization. Ends an SHA384 message-digest operation, writing the
540 : the message digest and zeroizing the context.
541 : */
542 : PHP_HASH_API void PHP_SHA384Final(unsigned char digest[48], PHP_SHA384_CTX * context)
543 15 : {
544 : unsigned char bits[16];
545 : unsigned int index, padLen;
546 :
547 : /* Save number of bits */
548 15 : bits[15] = (unsigned char) (context->count[0] & 0xFF);
549 15 : bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
550 15 : bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
551 15 : bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
552 15 : bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
553 15 : bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
554 15 : bits[9] = (unsigned char) ((context->count[0] >> 48) & 0xFF);
555 15 : bits[8] = (unsigned char) ((context->count[0] >> 56) & 0xFF);
556 15 : bits[7] = (unsigned char) (context->count[1] & 0xFF);
557 15 : bits[6] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
558 15 : bits[5] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
559 15 : bits[4] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
560 15 : bits[3] = (unsigned char) ((context->count[1] >> 32) & 0xFF);
561 15 : bits[2] = (unsigned char) ((context->count[1] >> 40) & 0xFF);
562 15 : bits[1] = (unsigned char) ((context->count[1] >> 48) & 0xFF);
563 15 : bits[0] = (unsigned char) ((context->count[1] >> 56) & 0xFF);
564 :
565 : /* Pad out to 112 mod 128.
566 : */
567 15 : index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
568 15 : padLen = (index < 112) ? (112 - index) : (240 - index);
569 15 : PHP_SHA384Update(context, PADDING, padLen);
570 :
571 : /* Append length (before padding) */
572 15 : PHP_SHA384Update(context, bits, 16);
573 :
574 : /* Store state in digest */
575 15 : SHAEncode64(digest, context->state, 48);
576 :
577 : /* Zeroize sensitive information.
578 : */
579 15 : memset((unsigned char*) context, 0, sizeof(*context));
580 15 : }
581 : /* }}} */
582 :
583 : const php_hash_ops php_hash_sha384_ops = {
584 : (php_hash_init_func_t) PHP_SHA384Init,
585 : (php_hash_update_func_t) PHP_SHA384Update,
586 : (php_hash_final_func_t) PHP_SHA384Final,
587 : (php_hash_copy_func_t) php_hash_copy,
588 : 48,
589 : 128,
590 : sizeof(PHP_SHA384_CTX)
591 : };
592 :
593 : /* {{{ PHP_SHA512Init
594 : * SHA512 initialization. Begins an SHA512 operation, writing a new context.
595 : */
596 : PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX * context)
597 23 : {
598 23 : context->count[0] = context->count[1] = 0;
599 : /* Load magic initialization constants.
600 : */
601 23 : context->state[0] = L64(0x6a09e667f3bcc908);
602 23 : context->state[1] = L64(0xbb67ae8584caa73b);
603 23 : context->state[2] = L64(0x3c6ef372fe94f82b);
604 23 : context->state[3] = L64(0xa54ff53a5f1d36f1);
605 23 : context->state[4] = L64(0x510e527fade682d1);
606 23 : context->state[5] = L64(0x9b05688c2b3e6c1f);
607 23 : context->state[6] = L64(0x1f83d9abfb41bd6b);
608 23 : context->state[7] = L64(0x5be0cd19137e2179);
609 23 : }
610 : /* }}} */
611 :
612 : /* {{{ PHP_SHA512Update
613 : SHA512 block update operation. Continues an SHA512 message-digest
614 : operation, processing another message block, and updating the
615 : context.
616 : */
617 : PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX * context, const unsigned char *input, unsigned int inputLen)
618 79 : {
619 : unsigned int i, index, partLen;
620 :
621 : /* Compute number of bytes mod 128 */
622 79 : index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
623 :
624 : /* Update number of bits */
625 79 : if ((context->count[0] += ((php_hash_uint64) inputLen << 3)) < ((php_hash_uint64) inputLen << 3)) {
626 0 : context->count[1]++;
627 : }
628 79 : context->count[1] += ((php_hash_uint64) inputLen >> 61);
629 :
630 79 : partLen = 128 - index;
631 :
632 : /* Transform as many times as possible.
633 : */
634 79 : if (inputLen >= partLen) {
635 40 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
636 40 : SHA512Transform(context->state, context->buffer);
637 :
638 7912 : for (i = partLen; i + 127 < inputLen; i += 128) {
639 7872 : SHA512Transform(context->state, &input[i]);
640 : }
641 :
642 40 : index = 0;
643 : } else {
644 39 : i = 0;
645 : }
646 :
647 : /* Buffer remaining input */
648 79 : memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
649 79 : }
650 : /* }}} */
651 :
652 : /* {{{ PHP_SHA512Final
653 : SHA512 finalization. Ends an SHA512 message-digest operation, writing the
654 : the message digest and zeroizing the context.
655 : */
656 : PHP_HASH_API void PHP_SHA512Final(unsigned char digest[64], PHP_SHA512_CTX * context)
657 23 : {
658 : unsigned char bits[16];
659 : unsigned int index, padLen;
660 :
661 : /* Save number of bits */
662 23 : bits[15] = (unsigned char) (context->count[0] & 0xFF);
663 23 : bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
664 23 : bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
665 23 : bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
666 23 : bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
667 23 : bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
668 23 : bits[9] = (unsigned char) ((context->count[0] >> 48) & 0xFF);
669 23 : bits[8] = (unsigned char) ((context->count[0] >> 56) & 0xFF);
670 23 : bits[7] = (unsigned char) (context->count[1] & 0xFF);
671 23 : bits[6] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
672 23 : bits[5] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
673 23 : bits[4] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
674 23 : bits[3] = (unsigned char) ((context->count[1] >> 32) & 0xFF);
675 23 : bits[2] = (unsigned char) ((context->count[1] >> 40) & 0xFF);
676 23 : bits[1] = (unsigned char) ((context->count[1] >> 48) & 0xFF);
677 23 : bits[0] = (unsigned char) ((context->count[1] >> 56) & 0xFF);
678 :
679 : /* Pad out to 112 mod 128.
680 : */
681 23 : index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
682 23 : padLen = (index < 112) ? (112 - index) : (240 - index);
683 23 : PHP_SHA512Update(context, PADDING, padLen);
684 :
685 : /* Append length (before padding) */
686 23 : PHP_SHA512Update(context, bits, 16);
687 :
688 : /* Store state in digest */
689 23 : SHAEncode64(digest, context->state, 64);
690 :
691 : /* Zeroize sensitive information.
692 : */
693 23 : memset((unsigned char*) context, 0, sizeof(*context));
694 23 : }
695 : /* }}} */
696 :
697 : const php_hash_ops php_hash_sha512_ops = {
698 : (php_hash_init_func_t) PHP_SHA512Init,
699 : (php_hash_update_func_t) PHP_SHA512Update,
700 : (php_hash_final_func_t) PHP_SHA512Final,
701 : (php_hash_copy_func_t) php_hash_copy,
702 : 64,
703 : 128,
704 : sizeof(PHP_SHA512_CTX)
705 : };
706 :
707 : /*
708 : * Local variables:
709 : * tab-width: 4
710 : * c-basic-offset: 4
711 : * End:
712 : * vim600: sw=4 ts=4 fdm=marker
713 : * vim<600: sw=4 ts=4
714 : */
|