PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/hash - hash_ripemd.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 327 331 98.8 %
Date: 2019-05-06 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 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             :   | Author: Sara Golemon <pollita@php.net>                               |
      16             :   +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* Heavily borrowed from md5.c & sha1.c of PHP archival fame
      20             :    Note that ripemd laughs in the face of logic and uses
      21             :    little endian byte ordering */
      22             : 
      23             : #include "php_hash.h"
      24             : #include "php_hash_ripemd.h"
      25             : 
      26             : const php_hash_ops php_hash_ripemd128_ops = {
      27             :         (php_hash_init_func_t) PHP_RIPEMD128Init,
      28             :         (php_hash_update_func_t) PHP_RIPEMD128Update,
      29             :         (php_hash_final_func_t) PHP_RIPEMD128Final,
      30             :         (php_hash_copy_func_t) php_hash_copy,
      31             :         16,
      32             :         64,
      33             :         sizeof(PHP_RIPEMD128_CTX),
      34             :         1
      35             : };
      36             : 
      37             : const php_hash_ops php_hash_ripemd160_ops = {
      38             :         (php_hash_init_func_t) PHP_RIPEMD160Init,
      39             :         (php_hash_update_func_t) PHP_RIPEMD160Update,
      40             :         (php_hash_final_func_t) PHP_RIPEMD160Final,
      41             :         (php_hash_copy_func_t) php_hash_copy,
      42             :         20,
      43             :         64,
      44             :         sizeof(PHP_RIPEMD160_CTX),
      45             :         1
      46             : };
      47             : 
      48             : const php_hash_ops php_hash_ripemd256_ops = {
      49             :         (php_hash_init_func_t) PHP_RIPEMD256Init,
      50             :         (php_hash_update_func_t) PHP_RIPEMD256Update,
      51             :         (php_hash_final_func_t) PHP_RIPEMD256Final,
      52             :         (php_hash_copy_func_t) php_hash_copy,
      53             :         32,
      54             :         64,
      55             :         sizeof(PHP_RIPEMD256_CTX),
      56             :         1
      57             : };
      58             : 
      59             : const php_hash_ops php_hash_ripemd320_ops = {
      60             :         (php_hash_init_func_t) PHP_RIPEMD320Init,
      61             :         (php_hash_update_func_t) PHP_RIPEMD320Update,
      62             :         (php_hash_final_func_t) PHP_RIPEMD320Final,
      63             :         (php_hash_copy_func_t) php_hash_copy,
      64             :         40,
      65             :         64,
      66             :         sizeof(PHP_RIPEMD320_CTX),
      67             :         1
      68             : };
      69             : 
      70             : /* {{{ PHP_RIPEMD128Init
      71             :  * ripemd128 initialization. Begins a ripemd128 operation, writing a new context.
      72             :  */
      73          27 : PHP_HASH_API void PHP_RIPEMD128Init(PHP_RIPEMD128_CTX * context)
      74             : {
      75          27 :         context->count[0] = context->count[1] = 0;
      76             :         /* Load magic initialization constants.
      77             :          */
      78          27 :         context->state[0] = 0x67452301;
      79          27 :         context->state[1] = 0xEFCDAB89;
      80          27 :         context->state[2] = 0x98BADCFE;
      81          27 :         context->state[3] = 0x10325476;
      82          27 : }
      83             : /* }}} */
      84             : 
      85             : /* {{{ PHP_RIPEMD256Init
      86             :  * ripemd256 initialization. Begins a ripemd256 operation, writing a new context.
      87             :  */
      88          27 : PHP_HASH_API void PHP_RIPEMD256Init(PHP_RIPEMD256_CTX * context)
      89             : {
      90          27 :         context->count[0] = context->count[1] = 0;
      91             :         /* Load magic initialization constants.
      92             :          */
      93          27 :         context->state[0] = 0x67452301;
      94          27 :         context->state[1] = 0xEFCDAB89;
      95          27 :         context->state[2] = 0x98BADCFE;
      96          27 :         context->state[3] = 0x10325476;
      97          27 :         context->state[4] = 0x76543210;
      98          27 :         context->state[5] = 0xFEDCBA98;
      99          27 :         context->state[6] = 0x89ABCDEF;
     100          27 :         context->state[7] = 0x01234567;
     101          27 : }
     102             : /* }}} */
     103             : 
     104             : /* {{{ PHP_RIPEMD160Init
     105             :  * ripemd160 initialization. Begins a ripemd160 operation, writing a new context.
     106             :  */
     107          34 : PHP_HASH_API void PHP_RIPEMD160Init(PHP_RIPEMD160_CTX * context)
     108             : {
     109          34 :         context->count[0] = context->count[1] = 0;
     110             :         /* Load magic initialization constants.
     111             :          */
     112          34 :         context->state[0] = 0x67452301;
     113          34 :         context->state[1] = 0xEFCDAB89;
     114          34 :         context->state[2] = 0x98BADCFE;
     115          34 :         context->state[3] = 0x10325476;
     116          34 :         context->state[4] = 0xC3D2E1F0;
     117          34 : }
     118             : /* }}} */
     119             : 
     120             : /* {{{ PHP_RIPEMD320Init
     121             :  * ripemd320 initialization. Begins a ripemd320 operation, writing a new context.
     122             :  */
     123          27 : PHP_HASH_API void PHP_RIPEMD320Init(PHP_RIPEMD320_CTX * context)
     124             : {
     125          27 :         context->count[0] = context->count[1] = 0;
     126             :         /* Load magic initialization constants.
     127             :          */
     128          27 :         context->state[0] = 0x67452301;
     129          27 :         context->state[1] = 0xEFCDAB89;
     130          27 :         context->state[2] = 0x98BADCFE;
     131          27 :         context->state[3] = 0x10325476;
     132          27 :         context->state[4] = 0xC3D2E1F0;
     133          27 :         context->state[5] = 0x76543210;
     134          27 :         context->state[6] = 0xFEDCBA98;
     135          27 :         context->state[7] = 0x89ABCDEF;
     136          27 :         context->state[8] = 0x01234567;
     137          27 :         context->state[9] = 0x3C2D1E0F;
     138          27 : }
     139             : /* }}} */
     140             : 
     141             : /* Basic ripemd function */
     142             : #define F0(x,y,z)               ((x) ^ (y) ^ (z))
     143             : #define F1(x,y,z)               (((x) & (y)) | ((~(x)) & (z)))
     144             : #define F2(x,y,z)               (((x) | (~(y))) ^ (z))
     145             : #define F3(x,y,z)               (((x) & (z)) | ((y) & (~(z))))
     146             : #define F4(x,y,z)               ((x) ^ ((y) | (~(z))))
     147             : 
     148             : static const uint32_t K_values[5]  = { 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E };    /* 128, 256, 160, 320 */
     149             : static const uint32_t KK_values[4] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x00000000 };                /* 128 & 256 */
     150             : static const uint32_t KK160_values[5] = { 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000 }; /* 160 & 320 */
     151             : 
     152             : #define K(n)  K_values[ (n) >> 4]
     153             : #define KK(n) KK_values[(n) >> 4]
     154             : #define KK160(n) KK160_values[(n) >> 4]
     155             : 
     156             : static const unsigned char R[80] = {
     157             :          0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
     158             :          7,  4, 13,  1, 10,  6, 15,  3, 12,  0,  9,  5,  2, 14, 11,  8,
     159             :          3, 10, 14,  4,  9, 15,  8,  1,  2,  7,  0,  6, 13, 11,  5, 12,
     160             :          1,  9, 11, 10,  0,  8, 12,  4, 13,  3,  7, 15, 14,  5,  6,  2,
     161             :          4,  0,  5,  9,  7, 12,  2, 10, 14,  1,  3,  8, 11,  6, 15, 13 };
     162             : 
     163             : static const unsigned char RR[80] = {
     164             :          5, 14,  7,  0,  9,  2, 11,  4, 13,  6, 15,  8,  1, 10,  3, 12,
     165             :          6, 11,  3,  7,  0, 13,  5, 10, 14, 15,  8, 12,  4,  9,  1,  2,
     166             :         15,  5,  1,  3,  7, 14,  6,  9, 11,  8, 12,  2, 10,  0,  4, 13,
     167             :          8,  6,  4,  1,  3, 11, 15,  0,  5, 12,  2, 13,  9,  7, 10, 14,
     168             :         12, 15, 10,  4,  1,  5,  8,  7,  6,  2, 13, 14,  0,  3,  9, 11 };
     169             : 
     170             : static const unsigned char S[80] = {
     171             :         11, 14, 15, 12,  5,  8,  7,  9, 11, 13, 14, 15,  6,  7,  9,  8,
     172             :          7,  6,  8, 13, 11,  9,  7, 15,  7, 12, 15,  9, 11,  7, 13, 12,
     173             :         11, 13,  6,  7, 14,  9, 13, 15, 14,  8, 13,  6,  5, 12,  7,  5,
     174             :         11, 12, 14, 15, 14, 15,  9,  8,  9, 14,  5,  6,  8,  6,  5, 12,
     175             :          9, 15,  5, 11,  6,  8, 13, 12,  5, 12, 13, 14, 11,  8,  5,  6 };
     176             : 
     177             : static const unsigned char SS[80] = {
     178             :          8,  9,  9, 11, 13, 15, 15,  5,  7,  7,  8, 11, 14, 14, 12,  6,
     179             :          9, 13, 15,  7, 12,  8,  9, 11,  7,  7, 12,  7,  6, 15, 13, 11,
     180             :          9,  7, 15, 11,  8,  6,  6, 14, 12, 13,  5, 14, 13, 13,  7,  5,
     181             :         15,  5,  8, 11, 14, 14,  6, 14,  6,  9, 12,  9, 12,  5, 15,  8,
     182             :          8,  5, 12,  9, 12,  5, 14,  6,  8, 13,  6,  5, 15, 13, 11, 11 };
     183             : 
     184             : #define ROLS(j, x)      (((x) << S[j])  | ((x) >> (32 - S[j])))
     185             : #define ROLSS(j, x)     (((x) << SS[j]) | ((x) >> (32 - SS[j])))
     186             : #define ROL(n, x)       (((x) << n) | ((x) >> (32 - n)))
     187             : 
     188             : /* {{{ RIPEMDDecode
     189             :    Decodes input (unsigned char) into output (uint32_t). Assumes len is
     190             :    a multiple of 4.
     191             :  */
     192       62684 : static void RIPEMDDecode(uint32_t *output, const unsigned char *input, unsigned int len)
     193             : {
     194             :         unsigned int i, j;
     195             : 
     196     1065628 :         for (i = 0, j = 0; j < len; i++, j += 4)
     197     4011776 :                 output[i] = ((uint32_t) input[j + 0]) | (((uint32_t) input[j + 1]) << 8) |
     198     3008832 :                         (((uint32_t) input[j + 2]) << 16) | (((uint32_t) input[j + 3]) << 24);
     199       62684 : }
     200             : /* }}} */
     201             : 
     202             : /* {{{ RIPEMD128Transform
     203             :  * ripemd128 basic transformation. Transforms state based on block.
     204             :  */
     205       15667 : static void RIPEMD128Transform(uint32_t state[4], const unsigned char block[64])
     206             : {
     207       15667 :         uint32_t a  = state[0], b  = state[1], c  = state[2], d  = state[3];
     208       15667 :         uint32_t aa = state[0], bb = state[1], cc = state[2], dd = state[3];
     209             :         uint32_t tmp, x[16];
     210             :         int j;
     211             : 
     212       15667 :         RIPEMDDecode(x, block, 64);
     213             : 
     214      266339 :         for(j = 0; j < 16; j++) {
     215      250672 :                 tmp = ROLS( j, a  + F0(b,  c,  d)  + x[R[j]]  + K(j));
     216      250672 :                 a = d; d = c; c = b; b = tmp;
     217      250672 :                 tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK(j));
     218      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     219             :         }
     220             : 
     221      266339 :         for(j = 16; j < 32; j++) {
     222      250672 :                 tmp = ROLS( j, a  + F1(b,  c,  d)  + x[R[j]]  + K(j));
     223      250672 :                 a = d; d = c; c = b; b = tmp;
     224      250672 :                 tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK(j));
     225      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     226             :         }
     227             : 
     228      266339 :         for(j = 32; j < 48; j++) {
     229      250672 :                 tmp = ROLS( j, a  + F2(b,  c,  d)  + x[R[j]]  + K(j));
     230      250672 :                 a = d; d = c; c = b; b = tmp;
     231      250672 :                 tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK(j));
     232      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     233             :         }
     234             : 
     235      266339 :         for(j = 48; j < 64; j++) {
     236      250672 :                 tmp = ROLS( j, a  + F3(b,  c,  d)  + x[R[j]]  + K(j));
     237      250672 :                 a = d; d = c; c = b; b = tmp;
     238      250672 :                 tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK(j));
     239      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     240             :         }
     241             : 
     242       15667 :         tmp = state[1] + c + dd;
     243       15667 :         state[1] = state[2] + d + aa;
     244       15667 :         state[2] = state[3] + a + bb;
     245       15667 :         state[3] = state[0] + b + cc;
     246       15667 :         state[0] = tmp;
     247             : 
     248       15667 :         tmp = 0;
     249       15667 :         ZEND_SECURE_ZERO(x, sizeof(x));
     250       15667 : }
     251             : /* }}} */
     252             : 
     253             : /* {{{ PHP_RIPEMD128Update
     254             :    ripemd128 block update operation. Continues a ripemd128 message-digest
     255             :    operation, processing another message block, and updating the
     256             :    context.
     257             :  */
     258          84 : PHP_HASH_API void PHP_RIPEMD128Update(PHP_RIPEMD128_CTX * context, const unsigned char *input, size_t inputLen)
     259             : {
     260             :         unsigned int i, index, partLen;
     261             : 
     262             :         /* Compute number of bytes mod 64 */
     263          84 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
     264             : 
     265             :         /* Update number of bits */
     266          84 :         if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
     267           0 :                 context->count[1]++;
     268             :         }
     269          84 :         context->count[1] += ((uint32_t) inputLen >> 29);
     270             : 
     271          84 :         partLen = 64 - index;
     272             : 
     273             :         /* Transform as many times as possible.
     274             :          */
     275          84 :         if (inputLen >= partLen) {
     276          43 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     277          43 :                 RIPEMD128Transform(context->state, context->buffer);
     278             : 
     279       15667 :                 for (i = partLen; i + 63 < inputLen; i += 64) {
     280       15624 :                         RIPEMD128Transform(context->state, &input[i]);
     281             :                 }
     282             : 
     283          43 :                 index = 0;
     284             :         } else {
     285          41 :                 i = 0;
     286             :         }
     287             : 
     288             :         /* Buffer remaining input */
     289          84 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     290          84 : }
     291             : /* }}} */
     292             : 
     293             : /* {{{ RIPEMD256Transform
     294             :  * ripemd256 basic transformation. Transforms state based on block.
     295             :  */
     296       15667 : static void RIPEMD256Transform(uint32_t state[8], const unsigned char block[64])
     297             : {
     298       15667 :         uint32_t a  = state[0], b  = state[1], c  = state[2], d  = state[3];
     299       15667 :         uint32_t aa = state[4], bb = state[5], cc = state[6], dd = state[7];
     300             :         uint32_t tmp, x[16];
     301             :         int j;
     302             : 
     303       15667 :         RIPEMDDecode(x, block, 64);
     304             : 
     305      266339 :         for(j = 0; j < 16; j++) {
     306      250672 :                 tmp = ROLS( j, a  + F0(b,  c,  d)  + x[R[j]]  + K(j));
     307      250672 :                 a = d; d = c; c = b; b = tmp;
     308      250672 :                 tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK(j));
     309      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     310             :         }
     311       15667 :         tmp = a; a = aa; aa = tmp;
     312             : 
     313      266339 :         for(j = 16; j < 32; j++) {
     314      250672 :                 tmp = ROLS( j, a  + F1(b,  c,  d)  + x[R[j]]  + K(j));
     315      250672 :                 a = d; d = c; c = b; b = tmp;
     316      250672 :                 tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK(j));
     317      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     318             :         }
     319       15667 :         tmp = b; b = bb; bb = tmp;
     320             : 
     321      266339 :         for(j = 32; j < 48; j++) {
     322      250672 :                 tmp = ROLS( j, a  + F2(b,  c,  d)  + x[R[j]]  + K(j));
     323      250672 :                 a = d; d = c; c = b; b = tmp;
     324      250672 :                 tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK(j));
     325      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     326             :         }
     327       15667 :         tmp = c; c = cc; cc = tmp;
     328             : 
     329      266339 :         for(j = 48; j < 64; j++) {
     330      250672 :                 tmp = ROLS( j, a  + F3(b,  c,  d)  + x[R[j]]  + K(j));
     331      250672 :                 a = d; d = c; c = b; b = tmp;
     332      250672 :                 tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK(j));
     333      250672 :                 aa = dd; dd = cc; cc = bb; bb = tmp;
     334             :         }
     335       15667 :         tmp = d; d = dd; dd = tmp;
     336             : 
     337       15667 :         state[0] += a;
     338       15667 :         state[1] += b;
     339       15667 :         state[2] += c;
     340       15667 :         state[3] += d;
     341       15667 :         state[4] += aa;
     342       15667 :         state[5] += bb;
     343       15667 :         state[6] += cc;
     344       15667 :         state[7] += dd;
     345             : 
     346       15667 :         tmp = 0;
     347       15667 :         ZEND_SECURE_ZERO(x, sizeof(x));
     348       15667 : }
     349             : /* }}} */
     350             : 
     351             : /* {{{ PHP_RIPEMD256Update
     352             :    ripemd256 block update operation. Continues a ripemd256 message-digest
     353             :    operation, processing another message block, and updating the
     354             :    context.
     355             :  */
     356          84 : PHP_HASH_API void PHP_RIPEMD256Update(PHP_RIPEMD256_CTX * context, const unsigned char *input, size_t inputLen)
     357             : {
     358             :         unsigned int i, index, partLen;
     359             : 
     360             :         /* Compute number of bytes mod 64 */
     361          84 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
     362             : 
     363             :         /* Update number of bits */
     364          84 :         if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
     365           0 :                 context->count[1]++;
     366             :         }
     367          84 :         context->count[1] += ((uint32_t) inputLen >> 29);
     368             : 
     369          84 :         partLen = 64 - index;
     370             : 
     371             :         /* Transform as many times as possible.
     372             :          */
     373          84 :         if (inputLen >= partLen) {
     374          43 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     375          43 :                 RIPEMD256Transform(context->state, context->buffer);
     376             : 
     377       15667 :                 for (i = partLen; i + 63 < inputLen; i += 64) {
     378       15624 :                         RIPEMD256Transform(context->state, &input[i]);
     379             :                 }
     380             : 
     381          43 :                 index = 0;
     382             :         } else {
     383          41 :                 i = 0;
     384             :         }
     385             : 
     386             :         /* Buffer remaining input */
     387          84 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     388          84 : }
     389             : /* }}} */
     390             : 
     391             : /* {{{ RIPEMD160Transform
     392             :  * ripemd160 basic transformation. Transforms state based on block.
     393             :  */
     394       15683 : static void RIPEMD160Transform(uint32_t state[5], const unsigned char block[64])
     395             : {
     396       15683 :         uint32_t a  = state[0], b  = state[1], c  = state[2], d  = state[3], e  = state[4];
     397       15683 :         uint32_t aa = state[0], bb = state[1], cc = state[2], dd = state[3], ee = state[4];
     398             :         uint32_t tmp, x[16];
     399             :         int j;
     400             : 
     401       15683 :         RIPEMDDecode(x, block, 64);
     402             : 
     403      266611 :         for(j = 0; j < 16; j++) {
     404      250928 :                 tmp = ROLS( j, a  + F0(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     405      250928 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     406      250928 :                 tmp = ROLSS(j, aa + F4(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     407      250928 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     408             :         }
     409             : 
     410      266611 :         for(j = 16; j < 32; j++) {
     411      250928 :                 tmp = ROLS( j, a  + F1(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     412      250928 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     413      250928 :                 tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     414      250928 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     415             :         }
     416             : 
     417      266611 :         for(j = 32; j < 48; j++) {
     418      250928 :                 tmp = ROLS( j, a  + F2(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     419      250928 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     420      250928 :                 tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     421      250928 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     422             :         }
     423             : 
     424      266611 :         for(j = 48; j < 64; j++) {
     425      250928 :                 tmp = ROLS( j, a  + F3(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     426      250928 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     427      250928 :                 tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     428      250928 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     429             :         }
     430             : 
     431      266611 :         for(j = 64; j < 80; j++) {
     432      250928 :                 tmp = ROLS( j, a  + F4(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     433      250928 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     434      250928 :                 tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     435      250928 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     436             :         }
     437             : 
     438       15683 :         tmp = state[1] + c + dd;
     439       15683 :         state[1] = state[2] + d + ee;
     440       15683 :         state[2] = state[3] + e + aa;
     441       15683 :         state[3] = state[4] + a + bb;
     442       15683 :         state[4] = state[0] + b + cc;
     443       15683 :         state[0] = tmp;
     444             : 
     445       15683 :         tmp = 0;
     446       15683 :         ZEND_SECURE_ZERO(x, sizeof(x));
     447       15683 : }
     448             : /* }}} */
     449             : 
     450             : /* {{{ PHP_RIPEMD160Update
     451             :    ripemd160 block update operation. Continues a ripemd160 message-digest
     452             :    operation, processing another message block, and updating the
     453             :    context.
     454             :  */
     455         117 : PHP_HASH_API void PHP_RIPEMD160Update(PHP_RIPEMD160_CTX * context, const unsigned char *input, size_t inputLen)
     456             : {
     457             :         unsigned int i, index, partLen;
     458             : 
     459             :         /* Compute number of bytes mod 64 */
     460         117 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
     461             : 
     462             :         /* Update number of bits */
     463         117 :         if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
     464           0 :                 context->count[1]++;
     465             :         }
     466         117 :         context->count[1] += ((uint32_t) inputLen >> 29);
     467             : 
     468         117 :         partLen = 64 - index;
     469             : 
     470             :         /* Transform as many times as possible.
     471             :          */
     472         117 :         if (inputLen >= partLen) {
     473          54 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     474          54 :                 RIPEMD160Transform(context->state, context->buffer);
     475             : 
     476       15683 :                 for (i = partLen; i + 63 < inputLen; i += 64) {
     477       15629 :                         RIPEMD160Transform(context->state, &input[i]);
     478             :                 }
     479             : 
     480          54 :                 index = 0;
     481             :         } else {
     482          63 :                 i = 0;
     483             :         }
     484             : 
     485             :         /* Buffer remaining input */
     486         117 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     487         117 : }
     488             : /* }}} */
     489             : 
     490             : /* {{{ RIPEMD320Transform
     491             :  * ripemd320 basic transformation. Transforms state based on block.
     492             :  */
     493       15667 : static void RIPEMD320Transform(uint32_t state[10], const unsigned char block[64])
     494             : {
     495       15667 :         uint32_t a  = state[0], b  = state[1], c  = state[2], d  = state[3], e  = state[4];
     496       15667 :         uint32_t aa = state[5], bb = state[6], cc = state[7], dd = state[8], ee = state[9];
     497             :         uint32_t tmp, x[16];
     498             :         int j;
     499             : 
     500       15667 :         RIPEMDDecode(x, block, 64);
     501             : 
     502      266339 :         for(j = 0; j < 16; j++) {
     503      250672 :                 tmp = ROLS( j, a  + F0(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     504      250672 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     505      250672 :                 tmp = ROLSS(j, aa + F4(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     506      250672 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     507             :         }
     508       15667 :         tmp = b; b = bb; bb = tmp;
     509             : 
     510      266339 :         for(j = 16; j < 32; j++) {
     511      250672 :                 tmp = ROLS( j, a  + F1(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     512      250672 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     513      250672 :                 tmp = ROLSS(j, aa + F3(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     514      250672 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     515             :         }
     516       15667 :         tmp = d; d = dd; dd = tmp;
     517             : 
     518      266339 :         for(j = 32; j < 48; j++) {
     519      250672 :                 tmp = ROLS( j, a  + F2(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     520      250672 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     521      250672 :                 tmp = ROLSS(j, aa + F2(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     522      250672 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     523             :         }
     524       15667 :         tmp = a; a = aa; aa = tmp;
     525             : 
     526      266339 :         for(j = 48; j < 64; j++) {
     527      250672 :                 tmp = ROLS( j, a  + F3(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     528      250672 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     529      250672 :                 tmp = ROLSS(j, aa + F1(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     530      250672 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     531             :         }
     532       15667 :         tmp = c; c = cc; cc = tmp;
     533             : 
     534      266339 :         for(j = 64; j < 80; j++) {
     535      250672 :                 tmp = ROLS( j, a  + F4(b,  c,  d)  + x[R[j]]  + K(j)) + e;
     536      250672 :                 a = e; e = d; d = ROL(10, c); c = b; b = tmp;
     537      250672 :                 tmp = ROLSS(j, aa + F0(bb, cc, dd) + x[RR[j]] + KK160(j)) + ee;
     538      250672 :                 aa = ee; ee = dd; dd = ROL(10, cc); cc = bb; bb = tmp;
     539             :         }
     540       15667 :         tmp = e; e = ee; ee = tmp;
     541             : 
     542       15667 :         state[0] += a;
     543       15667 :         state[1] += b;
     544       15667 :         state[2] += c;
     545       15667 :         state[3] += d;
     546       15667 :         state[4] += e;
     547       15667 :         state[5] += aa;
     548       15667 :         state[6] += bb;
     549       15667 :         state[7] += cc;
     550       15667 :         state[8] += dd;
     551       15667 :         state[9] += ee;
     552             : 
     553       15667 :         tmp = 0;
     554       15667 :         ZEND_SECURE_ZERO(x, sizeof(x));
     555       15667 : }
     556             : /* }}} */
     557             : 
     558             : /* {{{ PHP_RIPEMD320Update
     559             :    ripemd320 block update operation. Continues a ripemd320 message-digest
     560             :    operation, processing another message block, and updating the
     561             :    context.
     562             :  */
     563          84 : PHP_HASH_API void PHP_RIPEMD320Update(PHP_RIPEMD320_CTX * context, const unsigned char *input, size_t inputLen)
     564             : {
     565             :         unsigned int i, index, partLen;
     566             : 
     567             :         /* Compute number of bytes mod 64 */
     568          84 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
     569             : 
     570             :         /* Update number of bits */
     571          84 :         if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
     572           0 :                 context->count[1]++;
     573             :         }
     574          84 :         context->count[1] += ((uint32_t) inputLen >> 29);
     575             : 
     576          84 :         partLen = 64 - index;
     577             : 
     578             :         /* Transform as many times as possible.
     579             :          */
     580          84 :         if (inputLen >= partLen) {
     581          43 :                 memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
     582          43 :                 RIPEMD320Transform(context->state, context->buffer);
     583             : 
     584       15667 :                 for (i = partLen; i + 63 < inputLen; i += 64) {
     585       15624 :                         RIPEMD320Transform(context->state, &input[i]);
     586             :                 }
     587             : 
     588          43 :                 index = 0;
     589             :         } else {
     590          41 :                 i = 0;
     591             :         }
     592             : 
     593             :         /* Buffer remaining input */
     594          84 :         memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
     595          84 : }
     596             : /* }}} */
     597             : 
     598             : static const unsigned char PADDING[64] =
     599             : {
     600             :         0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     601             :         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     602             :         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     603             : };
     604             : 
     605             : /* {{{ RIPEMDEncode
     606             :    Encodes input (uint32_t) into output (unsigned char). Assumes len is
     607             :    a multiple of 4.
     608             :  */
     609         110 : static void RIPEMDEncode(unsigned char *output, uint32_t *input, unsigned int len)
     610             : {
     611             :         unsigned int i, j;
     612             : 
     613         842 :         for (i = 0, j = 0; j < len; i++, j += 4) {
     614         732 :                 output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
     615         732 :                 output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
     616         732 :                 output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
     617         732 :                 output[j + 0] = (unsigned char) (input[i] & 0xff);
     618             :         }
     619         110 : }
     620             : /* }}} */
     621             : 
     622             : /* {{{ PHP_RIPEMD128Final
     623             :    ripemd128 finalization. Ends a ripemd128 message-digest operation, writing the
     624             :    the message digest and zeroizing the context.
     625             :  */
     626          26 : PHP_HASH_API void PHP_RIPEMD128Final(unsigned char digest[16], PHP_RIPEMD128_CTX * context)
     627             : {
     628             :         unsigned char bits[8];
     629             :         unsigned int index, padLen;
     630             : 
     631             :         /* Save number of bits */
     632          26 :         bits[0] = (unsigned char) (context->count[0] & 0xFF);
     633          26 :         bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     634          26 :         bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     635          26 :         bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     636          26 :         bits[4] = (unsigned char) (context->count[1] & 0xFF);
     637          26 :         bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     638          26 :         bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     639          26 :         bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     640             : 
     641             :         /* Pad out to 56 mod 64.
     642             :          */
     643          26 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
     644          26 :         padLen = (index < 56) ? (56 - index) : (120 - index);
     645          26 :         PHP_RIPEMD128Update(context, PADDING, padLen);
     646             : 
     647             :         /* Append length (before padding) */
     648          26 :         PHP_RIPEMD128Update(context, bits, 8);
     649             : 
     650             :         /* Store state in digest */
     651          26 :         RIPEMDEncode(digest, context->state, 16);
     652             : 
     653             :         /* Zeroize sensitive information.
     654             :          */
     655          26 :         ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
     656          26 : }
     657             : /* }}} */
     658             : 
     659             : /* {{{ PHP_RIPEMD256Final
     660             :    ripemd256 finalization. Ends a ripemd256 message-digest operation, writing the
     661             :    the message digest and zeroizing the context.
     662             :  */
     663          26 : PHP_HASH_API void PHP_RIPEMD256Final(unsigned char digest[32], PHP_RIPEMD256_CTX * context)
     664             : {
     665             :         unsigned char bits[8];
     666             :         unsigned int index, padLen;
     667             : 
     668             :         /* Save number of bits */
     669          26 :         bits[0] = (unsigned char) (context->count[0] & 0xFF);
     670          26 :         bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     671          26 :         bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     672          26 :         bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     673          26 :         bits[4] = (unsigned char) (context->count[1] & 0xFF);
     674          26 :         bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     675          26 :         bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     676          26 :         bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     677             : 
     678             :         /* Pad out to 56 mod 64.
     679             :          */
     680          26 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
     681          26 :         padLen = (index < 56) ? (56 - index) : (120 - index);
     682          26 :         PHP_RIPEMD256Update(context, PADDING, padLen);
     683             : 
     684             :         /* Append length (before padding) */
     685          26 :         PHP_RIPEMD256Update(context, bits, 8);
     686             : 
     687             :         /* Store state in digest */
     688          26 :         RIPEMDEncode(digest, context->state, 32);
     689             : 
     690             :         /* Zeroize sensitive information.
     691             :          */
     692          26 :         ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
     693          26 : }
     694             : /* }}} */
     695             : 
     696             : /* {{{ PHP_RIPEMD160Final
     697             :    ripemd160 finalization. Ends a ripemd160 message-digest operation, writing the
     698             :    the message digest and zeroizing the context.
     699             :  */
     700          32 : PHP_HASH_API void PHP_RIPEMD160Final(unsigned char digest[20], PHP_RIPEMD160_CTX * context)
     701             : {
     702             :         unsigned char bits[8];
     703             :         unsigned int index, padLen;
     704             : 
     705             :         /* Save number of bits */
     706          32 :         bits[0] = (unsigned char) (context->count[0] & 0xFF);
     707          32 :         bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     708          32 :         bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     709          32 :         bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     710          32 :         bits[4] = (unsigned char) (context->count[1] & 0xFF);
     711          32 :         bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     712          32 :         bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     713          32 :         bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     714             : 
     715             :         /* Pad out to 56 mod 64.
     716             :          */
     717          32 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
     718          32 :         padLen = (index < 56) ? (56 - index) : (120 - index);
     719          32 :         PHP_RIPEMD160Update(context, PADDING, padLen);
     720             : 
     721             :         /* Append length (before padding) */
     722          32 :         PHP_RIPEMD160Update(context, bits, 8);
     723             : 
     724             :         /* Store state in digest */
     725          32 :         RIPEMDEncode(digest, context->state, 20);
     726             : 
     727             :         /* Zeroize sensitive information.
     728             :          */
     729          32 :         ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
     730          32 : }
     731             : /* }}} */
     732             : 
     733             : /* {{{ PHP_RIPEMD320Final
     734             :    ripemd320 finalization. Ends a ripemd320 message-digest operation, writing the
     735             :    the message digest and zeroizing the context.
     736             :  */
     737          26 : PHP_HASH_API void PHP_RIPEMD320Final(unsigned char digest[40], PHP_RIPEMD320_CTX * context)
     738             : {
     739             :         unsigned char bits[8];
     740             :         unsigned int index, padLen;
     741             : 
     742             :         /* Save number of bits */
     743          26 :         bits[0] = (unsigned char) (context->count[0] & 0xFF);
     744          26 :         bits[1] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
     745          26 :         bits[2] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
     746          26 :         bits[3] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
     747          26 :         bits[4] = (unsigned char) (context->count[1] & 0xFF);
     748          26 :         bits[5] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
     749          26 :         bits[6] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
     750          26 :         bits[7] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
     751             : 
     752             :         /* Pad out to 56 mod 64.
     753             :          */
     754          26 :         index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
     755          26 :         padLen = (index < 56) ? (56 - index) : (120 - index);
     756          26 :         PHP_RIPEMD320Update(context, PADDING, padLen);
     757             : 
     758             :         /* Append length (before padding) */
     759          26 :         PHP_RIPEMD320Update(context, bits, 8);
     760             : 
     761             :         /* Store state in digest */
     762          26 :         RIPEMDEncode(digest, context->state, 40);
     763             : 
     764             :         /* Zeroize sensitive information.
     765             :          */
     766          26 :         ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
     767          26 : }
     768             : /* }}} */

Generated by: LCOV version 1.10

Generated at Mon, 06 May 2019 17:58:14 +0000 (998 days ago)

Copyright © 2005-2022 The PHP Group
All rights reserved.