LTP GCOV extension - code coverage report
Current view: directory - ext/hash - hash_sha.c
Test: PHP Code Coverage
Date: 2007-04-10 Instrumented lines: 214
Code covered: 34.6 % Executed lines: 74
Legend: not executed executed

       1                 : /*
       2                 :   +----------------------------------------------------------------------+
       3                 :   | PHP Version 5                                                        |
       4                 :   +----------------------------------------------------------------------+
       5                 :   | Copyright (c) 1997-2007 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,v 1.10.2.3.2.2 2007/01/08 22:29:25 nlopess Exp $ */
      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               2 : {
      43                 :         unsigned int i, j;
      44                 : 
      45              18 :         for (i = 0, j = 0; j < len; i++, j += 4) {
      46              16 :                 output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
      47              16 :                 output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
      48              16 :                 output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
      49              16 :                 output[j + 3] = (unsigned char) (input[i] & 0xff);
      50                 :         }
      51               2 : }
      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               2 : {
      61                 :         unsigned int i, j;
      62                 : 
      63              34 :         for (i = 0, j = 0; j < len; i++, j += 4)
      64              32 :                 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               2 : }
      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                 :         20,
      74                 :         64,
      75                 :         sizeof(PHP_SHA1_CTX)
      76                 : };
      77                 : 
      78                 : #ifdef PHP_HASH_SHA1_NOT_IN_CORE
      79                 : 
      80                 : PHP_HASH_API void make_sha1_digest(char *sha1str, unsigned char *digest)
      81                 : {
      82                 :         php_hash_bin2hex(sha1str, digest, 20);
      83                 :         sha1str[40] = '\0';
      84                 : }
      85                 : 
      86                 : /* {{{ proto string sha1(string str [, bool raw_output])
      87                 :    Calculate the sha1 hash of a string */
      88                 : PHP_FUNCTION(sha1)
      89                 : {
      90                 :         char *arg;
      91                 :         int arg_len;
      92                 :         zend_bool raw_output = 0;
      93                 :         char sha1str[41];
      94                 :         PHP_SHA1_CTX context;
      95                 :         unsigned char digest[20];
      96                 :         
      97                 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
      98                 :                 return;
      99                 :         }
     100                 : 
     101                 :         sha1str[0] = '\0';
     102                 :         PHP_SHA1Init(&context);
     103                 :         PHP_SHA1Update(&context, arg, arg_len);
     104                 :         PHP_SHA1Final(digest, &context);
     105                 :         if (raw_output) {
     106                 :                 RETURN_STRINGL(digest, 20, 1);
     107                 :         } else {
     108                 :                 make_sha1_digest(sha1str, digest);
     109                 :                 RETVAL_STRING(sha1str, 1);
     110                 :         }
     111                 : 
     112                 : }
     113                 : 
     114                 : /* }}} */
     115                 : 
     116                 : /* {{{ proto string sha1_file(string filename [, bool raw_output])
     117                 :    Calculate the sha1 hash of given filename */
     118                 : PHP_FUNCTION(sha1_file)
     119                 : {
     120                 :         char          *arg;
     121                 :         int           arg_len;
     122                 :         zend_bool raw_output = 0;
     123                 :         char          sha1str[41];
     124                 :         unsigned char buf[1024];
     125                 :         unsigned char digest[20];
     126                 :         PHP_SHA1_CTX   context;
     127                 :         int           n;
     128                 :         php_stream    *stream;
     129                 : 
     130                 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
     131                 :                 return;
     132                 :         }
     133                 :         
     134                 :         stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL);
     135                 :         if (!stream) {
     136                 :                 RETURN_FALSE;
     137                 :         }
     138                 : 
     139                 :         PHP_SHA1Init(&context);
     140                 : 
     141                 :         while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     142                 :                 PHP_SHA1Update(&context, buf, n);
     143                 :         }
     144                 : 
     145                 :         PHP_SHA1Final(digest, &context);
     146                 : 
     147                 :         php_stream_close(stream);
     148                 : 
     149                 :         if (n<0) {
     150                 :                 RETURN_FALSE;
     151                 :         }
     152                 : 
     153                 :         if (raw_output) {
     154                 :                 RETURN_STRINGL(digest, 20, 1);
     155                 :         } else {
     156                 :                 make_sha1_digest(sha1str, digest);
     157                 :                 RETVAL_STRING(sha1str, 1);
     158                 :         }
     159                 : }
     160                 : /* }}} */
     161                 : 
     162                 : /* F, G, H and I are basic SHA1 functions.
     163                 :  */
     164                 : #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
     165                 : #define G(x, y, z) ((x) ^ (y) ^ (z))
     166                 : #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
     167                 : #define I(x, y, z) ((x) ^ (y) ^ (z))
     168                 : 
     169                 : /* ROTATE_LEFT rotates x left n bits.
     170                 :  */
     171                 : #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
     172                 : 
     173                 : /* W[i]
     174                 :  */
     175                 : #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
     176                 :         (x[i&15]=ROTATE_LEFT(tmp, 1)) )  
     177                 : 
     178                 : /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
     179                 :  */
     180                 : #define FF(a, b, c, d, e, w) { \
     181                 :  (e) += F ((b), (c), (d)) + (w) + (php_hash_uint32)(0x5A827999); \
     182                 :  (e) += ROTATE_LEFT ((a), 5); \
     183                 :  (b) = ROTATE_LEFT((b), 30); \
     184                 :   }
     185                 : #define GG(a, b, c, d, e, w) { \
     186                 :  (e) += G ((b), (c), (d)) + (w) + (php_hash_uint32)(0x6ED9EBA1); \
     187                 :  (e) += ROTATE_LEFT ((a), 5); \
     188                 :  (b) = ROTATE_LEFT((b), 30); \
     189                 :   }
     190                 : #define HH(a, b, c, d, e, w) { \
     191                 :  (e) += H ((b), (c), (d)) + (w) + (php_hash_uint32)(0x8F1BBCDC); \
     192                 :  (e) += ROTATE_LEFT ((a), 5); \
     193                 :  (b) = ROTATE_LEFT((b), 30); \
     194                 :   }
     195                 : #define II(a, b, c, d, e, w) { \
     196                 :  (e) += I ((b), (c), (d)) + (w) + (php_hash_uint32)(0xCA62C1D6); \
     197                 :  (e) += ROTATE_LEFT ((a), 5); \
     198                 :  (b) = ROTATE_LEFT((b), 30); \
     199                 :   }
     200                 :                                             
     201                 : 
     202                 : /* {{{ PHP_SHA1Init
     203                 :  * SHA1 initialization. Begins an SHA1 operation, writing a new context.
     204                 :  */
     205                 : PHP_HASH_API void PHP_SHA1Init(PHP_SHA1_CTX * context)
     206                 : {
     207                 :         context->count[0] = context->count[1] = 0;
     208                 :         /* Load magic initialization constants.
     209                 :          */
     210                 :         context->state[0] = 0x67452301;
     211                 :         context->state[1] = 0xefcdab89;
     212                 :         context->state[2] = 0x98badcfe;
     213                 :         context->state[3] = 0x10325476;
     214                 :         context->state[4] = 0xc3d2e1f0;
     215                 : }
     216                 : /* }}} */
     217                 : 
     218                 : /* {{{ SHA1Transform
     219                 :  * SHA1 basic transformation. Transforms state based on block.
     220                 :  */
     221                 : static void SHA1Transform(php_hash_uint32 state[5], const unsigned char block[64])
     222                 : {
     223                 :         php_hash_uint32 a = state[0], b = state[1], c = state[2];
     224                 :         php_hash_uint32 d = state[3], e = state[4], x[16], tmp;
     225                 : 
     226                 :         SHADecode32(x, block, 64);
     227                 : 
     228                 :         /* Round 1 */
     229                 :         FF(a, b, c, d, e, x[0]);   /* 1 */
     230                 :         FF(e, a, b, c, d, x[1]);   /* 2 */
     231                 :         FF(d, e, a, b, c, x[2]);   /* 3 */
     232                 :         FF(c, d, e, a, b, x[3]);   /* 4 */
     233                 :         FF(b, c, d, e, a, x[4]);   /* 5 */
     234                 :         FF(a, b, c, d, e, x[5]);   /* 6 */
     235                 :         FF(e, a, b, c, d, x[6]);   /* 7 */
     236                 :         FF(d, e, a, b, c, x[7]);   /* 8 */
     237                 :         FF(c, d, e, a, b, x[8]);   /* 9 */
     238                 :         FF(b, c, d, e, a, x[9]);   /* 10 */
     239                 :         FF(a, b, c, d, e, x[10]);  /* 11 */
     240                 :         FF(e, a, b, c, d, x[11]);  /* 12 */
     241                 :         FF(d, e, a, b, c, x[12]);  /* 13 */
     242                 :         FF(c, d, e, a, b, x[13]);  /* 14 */
     243                 :         FF(b, c, d, e, a, x[14]);  /* 15 */
     244                 :         FF(a, b, c, d, e, x[15]);  /* 16 */
     245                 :         FF(e, a, b, c, d, W(16));  /* 17 */
     246                 :         FF(d, e, a, b, c, W(17));  /* 18 */
     247                 :         FF(c, d, e, a, b, W(18));  /* 19 */
     248                 :         FF(b, c, d, e, a, W(19));  /* 20 */
     249                 : 
     250                 :         /* Round 2 */
     251                 :         GG(a, b, c, d, e, W(20));  /* 21 */
     252                 :         GG(e, a, b, c, d, W(21));  /* 22 */
     253                 :         GG(d, e, a, b, c, W(22));  /* 23 */
     254                 :         GG(c, d, e, a, b, W(23));  /* 24 */
     255                 :         GG(b, c, d, e, a, W(24));  /* 25 */
     256                 :         GG(a, b, c, d, e, W(25));  /* 26 */
     257                 :         GG(e, a, b, c, d, W(26));  /* 27 */
     258                 :         GG(d, e, a, b, c, W(27));  /* 28 */
     259                 :         GG(c, d, e, a, b, W(28));  /* 29 */
     260                 :         GG(b, c, d, e, a, W(29));  /* 30 */
     261                 :         GG(a, b, c, d, e, W(30));  /* 31 */
     262                 :         GG(e, a, b, c, d, W(31));  /* 32 */
     263                 :         GG(d, e, a, b, c, W(32));  /* 33 */
     264                 :         GG(c, d, e, a, b, W(33));  /* 34 */
     265                 :         GG(b, c, d, e, a, W(34));  /* 35 */
     266                 :         GG(a, b, c, d, e, W(35));  /* 36 */
     267                 :         GG(e, a, b, c, d, W(36));  /* 37 */
     268                 :         GG(d, e, a, b, c, W(37));  /* 38 */
     269                 :         GG(c, d, e, a, b, W(38));  /* 39 */
     270                 :         GG(b, c, d, e, a, W(39));  /* 40 */
     271                 : 
     272                 :         /* Round 3 */
     273                 :         HH(a, b, c, d, e, W(40));  /* 41 */
     274                 :         HH(e, a, b, c, d, W(41));  /* 42 */
     275                 :         HH(d, e, a, b, c, W(42));  /* 43 */
     276                 :         HH(c, d, e, a, b, W(43));  /* 44 */
     277                 :         HH(b, c, d, e, a, W(44));  /* 45 */
     278                 :         HH(a, b, c, d, e, W(45));  /* 46 */
     279                 :         HH(e, a, b, c, d, W(46));  /* 47 */
     280                 :         HH(d, e, a, b, c, W(47));  /* 48 */
     281                 :         HH(c, d, e, a, b, W(48));  /* 49 */
     282                 :         HH(b, c, d, e, a, W(49));  /* 50 */
     283                 :         HH(a, b, c, d, e, W(50));  /* 51 */
     284                 :         HH(e, a, b, c, d, W(51));  /* 52 */
     285                 :         HH(d, e, a, b, c, W(52));  /* 53 */
     286                 :         HH(c, d, e, a, b, W(53));  /* 54 */
     287                 :         HH(b, c, d, e, a, W(54));  /* 55 */
     288                 :         HH(a, b, c, d, e, W(55));  /* 56 */
     289                 :         HH(e, a, b, c, d, W(56));  /* 57 */
     290                 :         HH(d, e, a, b, c, W(57));  /* 58 */
     291                 :         HH(c, d, e, a, b, W(58));  /* 59 */
     292                 :         HH(b, c, d, e, a, W(59));  /* 60 */
     293                 : 
     294                 :         /* Round 4 */
     295                 :         II(a, b, c, d, e, W(60));  /* 61 */
     296                 :         II(e, a, b, c, d, W(61));  /* 62 */
     297                 :         II(d, e, a, b, c, W(62));  /* 63 */
     298                 :         II(c, d, e, a, b, W(63));  /* 64 */
     299                 :         II(b, c, d, e, a, W(64));  /* 65 */
     300                 :         II(a, b, c, d, e, W(65));  /* 66 */
     301                 :         II(e, a, b, c, d, W(66));  /* 67 */
     302                 :         II(d, e, a, b, c, W(67));  /* 68 */
     303                 :         II(c, d, e, a, b, W(68));  /* 69 */
     304                 :         II(b, c, d, e, a, W(69));  /* 70 */
     305                 :         II(a, b, c, d, e, W(70));  /* 71 */
     306                 :         II(e, a, b, c, d, W(71));  /* 72 */
     307                 :         II(d, e, a, b, c, W(72));  /* 73 */
     308                 :         II(c, d, e, a, b, W(73));  /* 74 */
     309                 :         II(b, c, d, e, a, W(74));  /* 75 */
     310                 :         II(a, b, c, d, e, W(75));  /* 76 */
     311                 :         II(e, a, b, c, d, W(76));  /* 77 */
     312                 :         II(d, e, a, b, c, W(77));  /* 78 */
     313                 :         II(c, d, e, a, b, W(78));  /* 79 */
     314                 :         II(b, c, d, e, a, W(79));  /* 80 */
     315                 : 
     316                 :         state[0] += a;
     317                 :         state[1] += b;
     318                 :         state[2] += c;
     319                 :         state[3] += d;
     320                 :         state[4] += e;
     321                 : 
     322                 :         /* Zeroize sensitive information. */
     323                 :         memset((unsigned char*) x, 0, sizeof(x));
     324                 : }
     325                 : /* }}} */
     326                 : 
     327                 : /* {{{ PHP_SHA1Update
     328                 :    SHA1 block update operation. Continues an SHA1 message-digest
     329                 :    operation, processing another message block, and updating the
     330                 :    context.
     331                 :  */
     332                 : PHP_HASH_API void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
     333                 :                            unsigned int inputLen)
     334                 : {
     335                 :         unsigned int i, index, partLen;
     336                 : 
     337                 :         /* Compute number of bytes mod 64 */
     338                 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
     339                 : 
     340                 :         /* Update number of bits */
     341                 :         if ((context->count[0] += ((php_hash_uint32) inputLen << 3))
     342                 :                 < ((php_hash_uint32) inputLen << 3))
     343                 :                 context->count[1]++;
     344                 :         context->count[1] += ((php_hash_uint32) inputLen >> 29);
     345                 : 
     346                 :         partLen = 64 - index;
     347                 : 
     348                 :         /* Transform as many times as possible.
     349                 :          */
     350                 :         if (inputLen >= partLen) {
     351                 :                 memcpy
     352                 :                         ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     353                 :                 SHA1Transform(context->state, context->buffer);
     354                 : 
     355                 :                 for (i = partLen; i + 63 < inputLen; i += 64)
     356                 :                         SHA1Transform(context->state, &input[i]);
     357                 : 
     358                 :                 index = 0;
     359                 :         } else
     360                 :                 i = 0;
     361                 : 
     362                 :         /* Buffer remaining input */
     363                 :         memcpy
     364                 :                 ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
     365                 :                  inputLen - i);
     366                 : }
     367                 : /* }}} */
     368                 : 
     369                 : /* {{{ PHP_SHA1Final
     370                 :    SHA1 finalization. Ends an SHA1 message-digest operation, writing the
     371                 :    the message digest and zeroizing the context.
     372                 :  */
     373                 : PHP_HASH_API void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
     374                 : {
     375                 :         unsigned char bits[8];
     376                 :         unsigned int index, padLen;
     377                 : 
     378                 :         /* Save number of bits */
     379                 :         bits[7] = context->count[0] & 0xFF;
     380                 :         bits[6] = (context->count[0] >> 8) & 0xFF;
     381                 :         bits[5] = (context->count[0] >> 16) & 0xFF;
     382                 :         bits[4] = (context->count[0] >> 24) & 0xFF;
     383                 :         bits[3] = context->count[1] & 0xFF;
     384                 :         bits[2] = (context->count[1] >> 8) & 0xFF;
     385                 :         bits[1] = (context->count[1] >> 16) & 0xFF;
     386                 :         bits[0] = (context->count[1] >> 24) & 0xFF;
     387                 :         
     388                 :         /* Pad out to 56 mod 64.
     389                 :          */
     390                 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
     391                 :         padLen = (index < 56) ? (56 - index) : (120 - index);
     392                 :         PHP_SHA1Update(context, PADDING, padLen);
     393                 : 
     394                 :         /* Append length (before padding) */
     395                 :         PHP_SHA1Update(context, bits, 8);
     396                 : 
     397                 :         /* Store state in digest */
     398                 :         SHAEncode32(digest, context->state, 20);
     399                 : 
     400                 :         /* Zeroize sensitive information.
     401                 :          */
     402                 :         memset((unsigned char*) context, 0, sizeof(*context));
     403                 : }
     404                 : /* }}} */
     405                 : 
     406                 : #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
     407                 : 
     408                 : /* sha256 */
     409                 : 
     410                 : const php_hash_ops php_hash_sha256_ops = {
     411                 :         (php_hash_init_func_t) PHP_SHA256Init,
     412                 :         (php_hash_update_func_t) PHP_SHA256Update,
     413                 :         (php_hash_final_func_t) PHP_SHA256Final,
     414                 :         32,
     415                 :         64,
     416                 :         sizeof(PHP_SHA256_CTX)
     417                 : };
     418                 : 
     419                 : #define ROTR32(b,x)             ((x >> b) | (x << (32 - b)))
     420                 : #define ROTR64(b,x)             ((x >> b) | (x << (64 - b)))
     421                 : #define SHR(b, x)               (x >> b)
     422                 : 
     423                 : /* Ch */
     424                 : #define SHA256_F0(x,y,z)        (((x) & (y)) ^ ((~(x)) & (z)))
     425                 : /* Maj */
     426                 : #define SHA256_F1(x,y,z)        (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
     427                 : /* SUM0 */
     428                 : #define SHA256_F2(x)            (ROTR32( 2,(x)) ^ ROTR32(13,(x)) ^ ROTR32(22,(x)))
     429                 : /* SUM1 */
     430                 : #define SHA256_F3(x)            (ROTR32( 6,(x)) ^ ROTR32(11,(x)) ^ ROTR32(25,(x)))
     431                 : /* OM0 */
     432                 : #define SHA256_F4(x)            (ROTR32( 7,(x)) ^ ROTR32(18,(x)) ^ SHR( 3,(x)))
     433                 : /* OM1 */
     434                 : #define SHA256_F5(x)            (ROTR32(17,(x)) ^ ROTR32(19,(x)) ^ SHR(10,(x)))
     435                 : 
     436                 : static const php_hash_uint32 SHA256_K[64] = {
     437                 :         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
     438                 :         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
     439                 :         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
     440                 :         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
     441                 :         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
     442                 :         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
     443                 :         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
     444                 :         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
     445                 : 
     446                 : /* {{{ PHP_SHA256Init
     447                 :  * SHA256 initialization. Begins an SHA256 operation, writing a new context.
     448                 :  */
     449                 : PHP_HASH_API void PHP_SHA256Init(PHP_SHA256_CTX * context)
     450               2 : {
     451               2 :         context->count[0] = context->count[1] = 0;
     452                 :         /* Load magic initialization constants.
     453                 :          */
     454               2 :         context->state[0] = 0x6a09e667;
     455               2 :         context->state[1] = 0xbb67ae85;
     456               2 :         context->state[2] = 0x3c6ef372;
     457               2 :         context->state[3] = 0xa54ff53a;
     458               2 :         context->state[4] = 0x510e527f;
     459               2 :         context->state[5] = 0x9b05688c;
     460               2 :         context->state[6] = 0x1f83d9ab;
     461               2 :         context->state[7] = 0x5be0cd19;
     462               2 : }
     463                 : /* }}} */
     464                 : 
     465                 : /* {{{ SHA256Transform
     466                 :  * SHA256 basic transformation. Transforms state based on block.
     467                 :  */
     468                 : static void SHA256Transform(php_hash_uint32 state[8], const unsigned char block[64])
     469               2 : {
     470               2 :         php_hash_uint32 a = state[0], b = state[1], c = state[2], d = state[3];
     471               2 :         php_hash_uint32 e = state[4], f = state[5], g = state[6], h = state[7];
     472                 :         php_hash_uint32 x[16], T1, T2, W[64];
     473                 :         int i;
     474                 : 
     475               2 :         SHADecode32(x, block, 64);
     476                 : 
     477                 :         /* Schedule */
     478              34 :         for(i = 0; i < 16; i++) {
     479              32 :                 W[i] = x[i];
     480                 :         }
     481              98 :         for(i = 16; i < 64; i++) {
     482              96 :                 W[i] = SHA256_F5(W[i-2]) + W[i-7] + SHA256_F4(W[i-15]) + W[i-16];
     483                 :         }
     484                 : 
     485             130 :         for (i = 0; i < 64; i++) {
     486             128 :                 T1 = h + SHA256_F3(e) + SHA256_F0(e,f,g) + SHA256_K[i] + W[i];
     487             128 :                 T2 = SHA256_F2(a) + SHA256_F1(a,b,c);
     488             128 :                 h = g; g = f; f = e; e = d + T1;
     489             128 :                 d = c; c = b; b = a; a = T1 + T2;
     490                 :         }
     491                 : 
     492               2 :         state[0] += a;
     493               2 :         state[1] += b;
     494               2 :         state[2] += c;
     495               2 :         state[3] += d;
     496               2 :         state[4] += e;
     497               2 :         state[5] += f;
     498               2 :         state[6] += g;
     499               2 :         state[7] += h;
     500                 : 
     501                 :         /* Zeroize sensitive information. */
     502               2 :         memset((unsigned char*) x, 0, sizeof(x));
     503               2 : }
     504                 : /* }}} */
     505                 : 
     506                 : /* {{{ PHP_SHA256Update
     507                 :    SHA256 block update operation. Continues an SHA256 message-digest
     508                 :    operation, processing another message block, and updating the
     509                 :    context.
     510                 :  */
     511                 : PHP_HASH_API void PHP_SHA256Update(PHP_SHA256_CTX * context, const unsigned char *input, unsigned int inputLen)
     512               6 : {
     513                 :         unsigned int i, index, partLen;
     514                 : 
     515                 :         /* Compute number of bytes mod 64 */
     516               6 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
     517                 : 
     518                 :         /* Update number of bits */
     519               6 :         if ((context->count[0] += ((php_hash_uint32) inputLen << 3)) < ((php_hash_uint32) inputLen << 3)) {
     520               0 :                 context->count[1]++;
     521                 :         }
     522               6 :         context->count[1] += ((php_hash_uint32) inputLen >> 29);
     523                 : 
     524               6 :         partLen = 64 - index;
     525                 : 
     526                 :         /* Transform as many times as possible.
     527                 :          */
     528               6 :         if (inputLen >= partLen) {
     529               2 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     530               2 :                 SHA256Transform(context->state, context->buffer);
     531                 : 
     532               2 :                 for (i = partLen; i + 63 < inputLen; i += 64) {
     533               0 :                         SHA256Transform(context->state, &input[i]);
     534                 :                 }
     535                 : 
     536               2 :                 index = 0;
     537                 :         } else {
     538               4 :                 i = 0;
     539                 :         }
     540                 : 
     541                 :         /* Buffer remaining input */
     542               6 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     543               6 : }
     544                 : /* }}} */
     545                 : 
     546                 : /* {{{ PHP_SHA256Final
     547                 :    SHA256 finalization. Ends an SHA256 message-digest operation, writing the
     548                 :    the message digest and zeroizing the context.
     549                 :  */
     550                 : PHP_HASH_API void PHP_SHA256Final(unsigned char digest[32], PHP_SHA256_CTX * context)
     551               2 : {
     552                 :         unsigned char bits[8];
     553                 :         unsigned int index, padLen;
     554                 : 
     555                 :         /* Save number of bits */
     556               2 :         bits[7] = (unsigned char) (context->count[0] & 0xFF);
     557               2 :         bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     558               2 :         bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     559               2 :         bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     560               2 :         bits[3] = (unsigned char) (context->count[1] & 0xFF);
     561               2 :         bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     562               2 :         bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     563               2 :         bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     564                 :         
     565                 :         /* Pad out to 56 mod 64.
     566                 :          */
     567               2 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
     568               2 :         padLen = (index < 56) ? (56 - index) : (120 - index);
     569               2 :         PHP_SHA256Update(context, PADDING, padLen);
     570                 : 
     571                 :         /* Append length (before padding) */
     572               2 :         PHP_SHA256Update(context, bits, 8);
     573                 : 
     574                 :         /* Store state in digest */
     575               2 :         SHAEncode32(digest, context->state, 32);
     576                 : 
     577                 :         /* Zeroize sensitive information.
     578                 :          */
     579               2 :         memset((unsigned char*) context, 0, sizeof(*context));
     580               2 : }
     581                 : /* }}} */
     582                 : 
     583                 : /* sha384/sha512 */
     584                 : 
     585                 : /* Ch */
     586                 : #define SHA512_F0(x,y,z)                (((x) & (y)) ^ ((~(x)) & (z)))
     587                 : /* Maj */
     588                 : #define SHA512_F1(x,y,z)                (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
     589                 : /* SUM0 */
     590                 : #define SHA512_F2(x)                    (ROTR64(28, x) ^ ROTR64(34, x) ^ ROTR64(39, x))
     591                 : /* SUM1 */
     592                 : #define SHA512_F3(x)                    (ROTR64(14, x) ^ ROTR64(18, x) ^ ROTR64(41, x))
     593                 : /* OM0 */
     594                 : #define SHA512_F4(x)                    (ROTR64( 1, x) ^ ROTR64( 8, x) ^ SHR(7, x))
     595                 : /* OM1 */
     596                 : #define SHA512_F5(x)                    (ROTR64(19, x) ^ ROTR64(61, x) ^ SHR(6, x))
     597                 : 
     598                 : static const php_hash_uint64 SHA512_K[128] = {
     599                 :         L64(0x428a2f98d728ae22), L64(0x7137449123ef65cd), L64(0xb5c0fbcfec4d3b2f), L64(0xe9b5dba58189dbbc),
     600                 :         L64(0x3956c25bf348b538), L64(0x59f111f1b605d019), L64(0x923f82a4af194f9b), L64(0xab1c5ed5da6d8118),
     601                 :         L64(0xd807aa98a3030242), L64(0x12835b0145706fbe), L64(0x243185be4ee4b28c), L64(0x550c7dc3d5ffb4e2),
     602                 :         L64(0x72be5d74f27b896f), L64(0x80deb1fe3b1696b1), L64(0x9bdc06a725c71235), L64(0xc19bf174cf692694),
     603                 :         L64(0xe49b69c19ef14ad2), L64(0xefbe4786384f25e3), L64(0x0fc19dc68b8cd5b5), L64(0x240ca1cc77ac9c65),
     604                 :         L64(0x2de92c6f592b0275), L64(0x4a7484aa6ea6e483), L64(0x5cb0a9dcbd41fbd4), L64(0x76f988da831153b5),
     605                 :         L64(0x983e5152ee66dfab), L64(0xa831c66d2db43210), L64(0xb00327c898fb213f), L64(0xbf597fc7beef0ee4),
     606                 :         L64(0xc6e00bf33da88fc2), L64(0xd5a79147930aa725), L64(0x06ca6351e003826f), L64(0x142929670a0e6e70),
     607                 :         L64(0x27b70a8546d22ffc), L64(0x2e1b21385c26c926), L64(0x4d2c6dfc5ac42aed), L64(0x53380d139d95b3df),
     608                 :         L64(0x650a73548baf63de), L64(0x766a0abb3c77b2a8), L64(0x81c2c92e47edaee6), L64(0x92722c851482353b),
     609                 :         L64(0xa2bfe8a14cf10364), L64(0xa81a664bbc423001), L64(0xc24b8b70d0f89791), L64(0xc76c51a30654be30),
     610                 :         L64(0xd192e819d6ef5218), L64(0xd69906245565a910), L64(0xf40e35855771202a), L64(0x106aa07032bbd1b8),
     611                 :         L64(0x19a4c116b8d2d0c8), L64(0x1e376c085141ab53), L64(0x2748774cdf8eeb99), L64(0x34b0bcb5e19b48a8),
     612                 :         L64(0x391c0cb3c5c95a63), L64(0x4ed8aa4ae3418acb), L64(0x5b9cca4f7763e373), L64(0x682e6ff3d6b2b8a3),
     613                 :         L64(0x748f82ee5defb2fc), L64(0x78a5636f43172f60), L64(0x84c87814a1f0ab72), L64(0x8cc702081a6439ec),
     614                 :         L64(0x90befffa23631e28), L64(0xa4506cebde82bde9), L64(0xbef9a3f7b2c67915), L64(0xc67178f2e372532b),
     615                 :         L64(0xca273eceea26619c), L64(0xd186b8c721c0c207), L64(0xeada7dd6cde0eb1e), L64(0xf57d4f7fee6ed178),
     616                 :         L64(0x06f067aa72176fba), L64(0x0a637dc5a2c898a6), L64(0x113f9804bef90dae), L64(0x1b710b35131c471b),
     617                 :         L64(0x28db77f523047d84), L64(0x32caab7b40c72493), L64(0x3c9ebe0a15c9bebc), L64(0x431d67c49c100d4c),
     618                 :         L64(0x4cc5d4becb3e42b6), L64(0x597f299cfc657e2a), L64(0x5fcb6fab3ad6faec), L64(0x6c44198c4a475817) };
     619                 : 
     620                 : /* {{{ SHAEncode64
     621                 :    Encodes input (php_hash_uint64) into output (unsigned char). Assumes len is
     622                 :    a multiple of 8.
     623                 :  */
     624                 : static void SHAEncode64(unsigned char *output, php_hash_uint64 *input, unsigned int len)
     625               0 : {
     626                 :         unsigned int i, j;
     627                 : 
     628               0 :         for (i = 0, j = 0; j < len; i++, j += 8) {
     629               0 :                 output[j] = (unsigned char) ((input[i] >> 56) & 0xff);
     630               0 :                 output[j + 1] = (unsigned char) ((input[i] >> 48) & 0xff);
     631               0 :                 output[j + 2] = (unsigned char) ((input[i] >> 40) & 0xff);
     632               0 :                 output[j + 3] = (unsigned char) ((input[i] >> 32) & 0xff);
     633               0 :                 output[j + 4] = (unsigned char) ((input[i] >> 24) & 0xff);
     634               0 :                 output[j + 5] = (unsigned char) ((input[i] >> 16) & 0xff);
     635               0 :                 output[j + 6] = (unsigned char) ((input[i] >> 8) & 0xff);
     636               0 :                 output[j + 7] = (unsigned char) (input[i] & 0xff);
     637                 :         }
     638               0 : }
     639                 : /* }}} */
     640                 : 
     641                 : 
     642                 : /* {{{ SHADecode64
     643                 :    Decodes input (unsigned char) into output (php_hash_uint64). Assumes len is
     644                 :    a multiple of 8.
     645                 :  */
     646                 : static void SHADecode64(php_hash_uint64 *output, const unsigned char *input, unsigned int len)
     647               0 : {
     648                 :         unsigned int i, j;
     649                 : 
     650               0 :         for (i = 0, j = 0; j < len; i++, j += 8)
     651               0 :                 output[i] = 
     652                 :                         ((php_hash_uint64) input[j + 7]) | (((php_hash_uint64) input[j + 6]) << 8) |
     653                 :                         (((php_hash_uint64) input[j + 5]) << 16) | (((php_hash_uint64) input[j + 4]) << 24) |
     654                 :                         (((php_hash_uint64) input[j + 3]) << 32) | (((php_hash_uint64) input[j + 2]) << 40) |
     655                 :                         (((php_hash_uint64) input[j + 1]) << 48) | (((php_hash_uint64) input[j]) << 56);
     656               0 : }
     657                 : /* }}} */
     658                 : 
     659                 : /* {{{ PHP_SHA384Init
     660                 :  * SHA384 initialization. Begins an SHA384 operation, writing a new context.
     661                 :  */
     662                 : PHP_HASH_API void PHP_SHA384Init(PHP_SHA384_CTX * context)
     663               0 : {
     664               0 :         context->count[0] = context->count[1] = 0;
     665                 :         /* Load magic initialization constants.
     666                 :          */
     667               0 :         context->state[0] = L64(0xcbbb9d5dc1059ed8);
     668               0 :         context->state[1] = L64(0x629a292a367cd507);
     669               0 :         context->state[2] = L64(0x9159015a3070dd17);
     670               0 :         context->state[3] = L64(0x152fecd8f70e5939);
     671               0 :         context->state[4] = L64(0x67332667ffc00b31);
     672               0 :         context->state[5] = L64(0x8eb44a8768581511);
     673               0 :         context->state[6] = L64(0xdb0c2e0d64f98fa7);
     674               0 :         context->state[7] = L64(0x47b5481dbefa4fa4);
     675               0 : }
     676                 : /* }}} */
     677                 : 
     678                 : /* {{{ SHA512Transform
     679                 :  * SHA512 basic transformation. Transforms state based on block.
     680                 :  * SHA384 uses the exact same algorithm
     681                 :  */
     682                 : static void SHA512Transform(php_hash_uint64 state[8], const unsigned char block[128])
     683               0 : {
     684               0 :         php_hash_uint64 a = state[0], b = state[1], c = state[2], d = state[3];
     685               0 :         php_hash_uint64 e = state[4], f = state[5], g = state[6], h = state[7];
     686                 :         php_hash_uint64 x[16], T1, T2, W[80];
     687                 :         int i;
     688                 : 
     689               0 :         SHADecode64(x, block, 128);
     690                 : 
     691                 :         /* Schedule */
     692               0 :         for(i = 0; i < 16; i++) {
     693               0 :                 W[i] = x[i];
     694                 :         }
     695               0 :         for(i = 16; i < 80; i++) {
     696               0 :                 W[i] = SHA512_F5(W[i-2]) + W[i-7] + SHA512_F4(W[i-15]) + W[i-16];
     697                 :         }
     698                 : 
     699               0 :         for (i = 0; i < 80; i++) {
     700               0 :                 T1 = h + SHA512_F3(e) + SHA512_F0(e,f,g) + SHA512_K[i] + W[i];
     701               0 :                 T2 = SHA512_F2(a) + SHA512_F1(a,b,c);
     702               0 :                 h = g; g = f; f = e; e = d + T1;
     703               0 :                 d = c; c = b; b = a; a = T1 + T2;
     704                 :         }
     705                 : 
     706               0 :         state[0] += a;
     707               0 :         state[1] += b;
     708               0 :         state[2] += c;
     709               0 :         state[3] += d;
     710               0 :         state[4] += e;
     711               0 :         state[5] += f;
     712               0 :         state[6] += g;
     713               0 :         state[7] += h;
     714                 : 
     715                 :         /* Zeroize sensitive information. */
     716               0 :         memset((unsigned char*) x, 0, sizeof(x));
     717               0 : }
     718                 : /* }}} */
     719                 : 
     720                 : /* {{{ PHP_SHA384Update
     721                 :    SHA384 block update operation. Continues an SHA384 message-digest
     722                 :    operation, processing another message block, and updating the
     723                 :    context.
     724                 :  */
     725                 : PHP_HASH_API void PHP_SHA384Update(PHP_SHA384_CTX * context, const unsigned char *input, unsigned int inputLen)
     726               0 : {
     727                 :         unsigned int i, index, partLen;
     728                 : 
     729                 :         /* Compute number of bytes mod 128 */
     730               0 :         index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
     731                 : 
     732                 :         /* Update number of bits */
     733               0 :         if ((context->count[0] += ((php_hash_uint64) inputLen << 3)) < ((php_hash_uint64) inputLen << 3)) {
     734               0 :                 context->count[1]++;
     735                 :         }
     736               0 :         context->count[1] += ((php_hash_uint64) inputLen >> 61);
     737                 : 
     738               0 :         partLen = 128 - index;
     739                 : 
     740                 :         /* Transform as many times as possible.
     741                 :          */
     742               0 :         if (inputLen >= partLen) {
     743               0 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     744               0 :                 SHA512Transform(context->state, context->buffer);
     745                 : 
     746               0 :                 for (i = partLen; i + 127 < inputLen; i += 128) {
     747               0 :                         SHA512Transform(context->state, &input[i]);
     748                 :                 }
     749                 : 
     750               0 :                 index = 0;
     751                 :         } else {
     752               0 :                 i = 0;
     753                 :         }
     754                 : 
     755                 :         /* Buffer remaining input */
     756               0 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     757               0 : }
     758                 : /* }}} */
     759                 : 
     760                 : /* {{{ PHP_SHA384Final
     761                 :    SHA384 finalization. Ends an SHA384 message-digest operation, writing the
     762                 :    the message digest and zeroizing the context.
     763                 :  */
     764                 : PHP_HASH_API void PHP_SHA384Final(unsigned char digest[48], PHP_SHA384_CTX * context)
     765               0 : {
     766                 :         unsigned char bits[16];
     767                 :         unsigned int index, padLen;
     768                 : 
     769                 :         /* Save number of bits */
     770               0 :         bits[15] = (unsigned char) (context->count[0] & 0xFF);
     771               0 :         bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     772               0 :         bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     773               0 :         bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     774               0 :         bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
     775               0 :         bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
     776               0 :         bits[9]  = (unsigned char) ((context->count[0] >> 48) & 0xFF);
     777               0 :         bits[8]  = (unsigned char) ((context->count[0] >> 56) & 0xFF);
     778               0 :         bits[7]  = (unsigned char) (context->count[1] & 0xFF);
     779               0 :         bits[6]  = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     780               0 :         bits[5]  = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     781               0 :         bits[4]  = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     782               0 :         bits[3]  = (unsigned char) ((context->count[1] >> 32) & 0xFF);
     783               0 :         bits[2]  = (unsigned char) ((context->count[1] >> 40) & 0xFF);
     784               0 :         bits[1]  = (unsigned char) ((context->count[1] >> 48) & 0xFF);
     785               0 :         bits[0]  = (unsigned char) ((context->count[1] >> 56) & 0xFF);
     786                 :         
     787                 :         /* Pad out to 112 mod 128.
     788                 :          */
     789               0 :         index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
     790               0 :         padLen = (index < 112) ? (112 - index) : (240 - index);
     791               0 :         PHP_SHA384Update(context, PADDING, padLen);
     792                 : 
     793                 :         /* Append length (before padding) */
     794               0 :         PHP_SHA384Update(context, bits, 16);
     795                 : 
     796                 :         /* Store state in digest */
     797               0 :         SHAEncode64(digest, context->state, 48);
     798                 : 
     799                 :         /* Zeroize sensitive information.
     800                 :          */
     801               0 :         memset((unsigned char*) context, 0, sizeof(*context));
     802               0 : }
     803                 : /* }}} */
     804                 : 
     805                 : const php_hash_ops php_hash_sha384_ops = {
     806                 :         (php_hash_init_func_t) PHP_SHA384Init,
     807                 :         (php_hash_update_func_t) PHP_SHA384Update,
     808                 :         (php_hash_final_func_t) PHP_SHA384Final,
     809                 :         48,
     810                 :         128,
     811                 :         sizeof(PHP_SHA384_CTX)
     812                 : };
     813                 : 
     814                 : /* {{{ PHP_SHA512Init
     815                 :  * SHA512 initialization. Begins an SHA512 operation, writing a new context.
     816                 :  */
     817                 : PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX * context)
     818               0 : {
     819               0 :         context->count[0] = context->count[1] = 0;
     820                 :         /* Load magic initialization constants.
     821                 :          */
     822               0 :         context->state[0] = L64(0x6a09e667f3bcc908);
     823               0 :         context->state[1] = L64(0xbb67ae8584caa73b);
     824               0 :         context->state[2] = L64(0x3c6ef372fe94f82b);
     825               0 :         context->state[3] = L64(0xa54ff53a5f1d36f1);
     826               0 :         context->state[4] = L64(0x510e527fade682d1);
     827               0 :         context->state[5] = L64(0x9b05688c2b3e6c1f);
     828               0 :         context->state[6] = L64(0x1f83d9abfb41bd6b);
     829               0 :         context->state[7] = L64(0x5be0cd19137e2179);
     830               0 : }
     831                 : /* }}} */
     832                 : 
     833                 : /* {{{ PHP_SHA512Update
     834                 :    SHA512 block update operation. Continues an SHA512 message-digest
     835                 :    operation, processing another message block, and updating the
     836                 :    context.
     837                 :  */
     838                 : PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX * context, const unsigned char *input, unsigned int inputLen)
     839               0 : {
     840                 :         unsigned int i, index, partLen;
     841                 : 
     842                 :         /* Compute number of bytes mod 128 */
     843               0 :         index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
     844                 : 
     845                 :         /* Update number of bits */
     846               0 :         if ((context->count[0] += ((php_hash_uint64) inputLen << 3)) < ((php_hash_uint64) inputLen << 3)) {
     847               0 :                 context->count[1]++;
     848                 :         }
     849               0 :         context->count[1] += ((php_hash_uint64) inputLen >> 61);
     850                 : 
     851               0 :         partLen = 128 - index;
     852                 : 
     853                 :         /* Transform as many times as possible.
     854                 :          */
     855               0 :         if (inputLen >= partLen) {
     856               0 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     857               0 :                 SHA512Transform(context->state, context->buffer);
     858                 : 
     859               0 :                 for (i = partLen; i + 127 < inputLen; i += 128) {
     860               0 :                         SHA512Transform(context->state, &input[i]);
     861                 :                 }
     862                 : 
     863               0 :                 index = 0;
     864                 :         } else {
     865               0 :                 i = 0;
     866                 :         }
     867                 : 
     868                 :         /* Buffer remaining input */
     869               0 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     870               0 : }
     871                 : /* }}} */
     872                 : 
     873                 : /* {{{ PHP_SHA512Final
     874                 :    SHA512 finalization. Ends an SHA384 message-digest operation, writing the
     875                 :    the message digest and zeroizing the context.
     876                 :  */
     877                 : PHP_HASH_API void PHP_SHA512Final(unsigned char digest[48], PHP_SHA512_CTX * context)
     878               0 : {
     879                 :         unsigned char bits[16];
     880                 :         unsigned int index, padLen;
     881                 : 
     882                 :         /* Save number of bits */
     883               0 :         bits[15] = (unsigned char) (context->count[0] & 0xFF);
     884               0 :         bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     885               0 :         bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     886               0 :         bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     887               0 :         bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
     888               0 :         bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
     889               0 :         bits[9]  = (unsigned char) ((context->count[0] >> 48) & 0xFF);
     890               0 :         bits[8]  = (unsigned char) ((context->count[0] >> 56) & 0xFF);
     891               0 :         bits[7]  = (unsigned char) (context->count[1] & 0xFF);
     892               0 :         bits[6]  = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     893               0 :         bits[5]  = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     894               0 :         bits[4]  = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     895               0 :         bits[3]  = (unsigned char) ((context->count[1] >> 32) & 0xFF);
     896               0 :         bits[2]  = (unsigned char) ((context->count[1] >> 40) & 0xFF);
     897               0 :         bits[1]  = (unsigned char) ((context->count[1] >> 48) & 0xFF);
     898               0 :         bits[0]  = (unsigned char) ((context->count[1] >> 56) & 0xFF);
     899                 : 
     900                 :         /* Pad out to 112 mod 128.
     901                 :          */
     902               0 :         index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
     903               0 :         padLen = (index < 112) ? (112 - index) : (240 - index);
     904               0 :         PHP_SHA512Update(context, PADDING, padLen);
     905                 : 
     906                 :         /* Append length (before padding) */
     907               0 :         PHP_SHA512Update(context, bits, 16);
     908                 : 
     909                 :         /* Store state in digest */
     910               0 :         SHAEncode64(digest, context->state, 64);
     911                 : 
     912                 :         /* Zeroize sensitive information.
     913                 :          */
     914               0 :         memset((unsigned char*) context, 0, sizeof(*context));
     915               0 : }
     916                 : /* }}} */
     917                 : 
     918                 : const php_hash_ops php_hash_sha512_ops = {
     919                 :         (php_hash_init_func_t) PHP_SHA512Init,
     920                 :         (php_hash_update_func_t) PHP_SHA512Update,
     921                 :         (php_hash_final_func_t) PHP_SHA512Final,
     922                 :         64,
     923                 :         128,
     924                 :         sizeof(PHP_SHA512_CTX)
     925                 : };
     926                 : 
     927                 : /*
     928                 :  * Local variables:
     929                 :  * tab-width: 4
     930                 :  * c-basic-offset: 4
     931                 :  * End:
     932                 :  * vim600: sw=4 ts=4 fdm=marker
     933                 :  * vim<600: sw=4 ts=4
     934                 :  */

Generated by: LTP GCOV extension version 1.5