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: 2022-01-29 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sat, 29 Jan 2022 07:14:32 +0000 (55 minutes ago)

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