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_snefru.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 104 107 97.2 %
Date: 2019-05-06 Functions: 5 5 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             :   | Authors: Michael Wallner <mike@php.net>                              |
      16             :   |          Sara Golemon <pollita@php.net>                              |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : #include "php_hash.h"
      21             : #include "php_hash_snefru.h"
      22             : #include "php_hash_snefru_tables.h"
      23             : 
      24             : #define round(L, C, N, SB)      \
      25             :         SBE = SB[C & 0xff]; \
      26             :         L ^= SBE; \
      27             :         N ^= SBE
      28             : 
      29             : #ifndef DBG_SNEFRU
      30             : #define DBG_SNEFRU 0
      31             : #endif
      32             : 
      33             : #if DBG_SNEFRU
      34             : void ph(uint32_t h[16])
      35             : {
      36             :         int i;
      37             :         for (i = 0; i < 16; i++)
      38             :                 printf ("%08lx", h[i]); printf("\n");
      39             : }
      40             : #endif
      41             : 
      42          93 : static inline void Snefru(uint32_t input[16])
      43             : {
      44             :         static int shifts[4] = {16, 8, 16, 24};
      45             :         int b, index, rshift, lshift;
      46             :         const uint32_t *t0,*t1;
      47             :         uint32_t SBE,B00,B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15;
      48             : 
      49          93 :         B00 = input[0];
      50          93 :         B01 = input[1];
      51          93 :         B02 = input[2];
      52          93 :         B03 = input[3];
      53          93 :         B04 = input[4];
      54          93 :         B05 = input[5];
      55          93 :         B06 = input[6];
      56          93 :         B07 = input[7];
      57          93 :         B08 = input[8];
      58          93 :         B09 = input[9];
      59          93 :         B10 = input[10];
      60          93 :         B11 = input[11];
      61          93 :         B12 = input[12];
      62          93 :         B13 = input[13];
      63          93 :         B14 = input[14];
      64          93 :         B15 = input[15];
      65             : 
      66         837 :         for (index = 0; index < 8; index++) {
      67         744 :                 t0 = tables[2*index+0];
      68         744 :                 t1 = tables[2*index+1];
      69        3720 :                 for (b = 0; b < 4; b++) {
      70        2976 :                         round(B15, B00, B01, t0);
      71        2976 :                         round(B00, B01, B02, t0);
      72        2976 :                         round(B01, B02, B03, t1);
      73        2976 :                         round(B02, B03, B04, t1);
      74        2976 :                         round(B03, B04, B05, t0);
      75        2976 :                         round(B04, B05, B06, t0);
      76        2976 :                         round(B05, B06, B07, t1);
      77        2976 :                         round(B06, B07, B08, t1);
      78        2976 :                         round(B07, B08, B09, t0);
      79        2976 :                         round(B08, B09, B10, t0);
      80        2976 :                         round(B09, B10, B11, t1);
      81        2976 :                         round(B10, B11, B12, t1);
      82        2976 :                         round(B11, B12, B13, t0);
      83        2976 :                         round(B12, B13, B14, t0);
      84        2976 :                         round(B13, B14, B15, t1);
      85        2976 :                         round(B14, B15, B00, t1);
      86             : 
      87        2976 :                         rshift = shifts[b];
      88        2976 :                         lshift = 32-rshift;
      89             : 
      90        2976 :                         B00 = (B00 >> rshift) | (B00 << lshift);
      91        2976 :                         B01 = (B01 >> rshift) | (B01 << lshift);
      92        2976 :                         B02 = (B02 >> rshift) | (B02 << lshift);
      93        2976 :                         B03 = (B03 >> rshift) | (B03 << lshift);
      94        2976 :                         B04 = (B04 >> rshift) | (B04 << lshift);
      95        2976 :                         B05 = (B05 >> rshift) | (B05 << lshift);
      96        2976 :                         B06 = (B06 >> rshift) | (B06 << lshift);
      97        2976 :                         B07 = (B07 >> rshift) | (B07 << lshift);
      98        2976 :                         B08 = (B08 >> rshift) | (B08 << lshift);
      99        2976 :                         B09 = (B09 >> rshift) | (B09 << lshift);
     100        2976 :                         B10 = (B10 >> rshift) | (B10 << lshift);
     101        2976 :                         B11 = (B11 >> rshift) | (B11 << lshift);
     102        2976 :                         B12 = (B12 >> rshift) | (B12 << lshift);
     103        2976 :                         B13 = (B13 >> rshift) | (B13 << lshift);
     104        2976 :                         B14 = (B14 >> rshift) | (B14 << lshift);
     105        2976 :                         B15 = (B15 >> rshift) | (B15 << lshift);
     106             :                 }
     107             :         }
     108          93 :         input[0] ^= B15;
     109          93 :         input[1] ^= B14;
     110          93 :         input[2] ^= B13;
     111          93 :         input[3] ^= B12;
     112          93 :         input[4] ^= B11;
     113          93 :         input[5] ^= B10;
     114          93 :         input[6] ^= B09;
     115          93 :         input[7] ^= B08;
     116             : #if DBG_SNEFRU
     117             :         ph(input);
     118             : #endif
     119          93 : }
     120             : 
     121          59 : static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char input[32])
     122             : {
     123             :         int i, j;
     124             : 
     125         531 :         for (i = 0, j = 0; i < 32; i += 4, ++j) {
     126        1888 :                 context->state[8+j] =        ((input[i] & 0xff) << 24) | ((input[i+1] & 0xff) << 16) |
     127        1416 :                                                                 ((input[i+2] & 0xff) << 8) | (input[i+3] & 0xff);
     128             :         }
     129          59 :         Snefru(context->state);
     130          59 :         ZEND_SECURE_ZERO(&context->state[8], sizeof(uint32_t) * 8);
     131          59 : }
     132             : 
     133          36 : PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context)
     134             : {
     135          36 :         memset(context, 0, sizeof(*context));
     136          36 : }
     137             : 
     138             : static const uint32_t MAX32 = 0xffffffffLU;
     139             : 
     140          42 : PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *context, const unsigned char *input, size_t len)
     141             : {
     142          42 :         if ((MAX32 - context->count[1]) < (len * 8)) {
     143           0 :                 context->count[0]++;
     144           0 :                 context->count[1] = MAX32 - context->count[1];
     145           0 :                 context->count[1] = ((uint32_t) len * 8) - context->count[1];
     146             :         } else {
     147          42 :                 context->count[1] += (uint32_t) len * 8;
     148             :         }
     149             : 
     150          42 :         if (context->length + len < 32) {
     151          13 :                 memcpy(&context->buffer[context->length], input, len);
     152          13 :                 context->length += (unsigned char)len;
     153             :         } else {
     154          29 :                 size_t i = 0, r = (context->length + len) % 32;
     155             : 
     156          29 :                 if (context->length) {
     157           4 :                         i = 32 - context->length;
     158           4 :                         memcpy(&context->buffer[context->length], input, i);
     159           4 :                         SnefruTransform(context, context->buffer);
     160             :                 }
     161             : 
     162          59 :                 for (; i + 32 <= len; i += 32) {
     163          30 :                         SnefruTransform(context, input + i);
     164             :                 }
     165             : 
     166          29 :                 memcpy(context->buffer, input + i, r);
     167          29 :                 ZEND_SECURE_ZERO(&context->buffer[r], 32 - r);
     168          29 :                 context->length = (unsigned char)r;
     169             :         }
     170          42 : }
     171             : 
     172          34 : PHP_HASH_API void PHP_SNEFRUFinal(unsigned char digest[32], PHP_SNEFRU_CTX *context)
     173             : {
     174             :         uint32_t i, j;
     175             : 
     176          34 :         if (context->length) {
     177          25 :                 SnefruTransform(context, context->buffer);
     178             :         }
     179             : 
     180          34 :         context->state[14] = context->count[0];
     181          34 :         context->state[15] = context->count[1];
     182          34 :         Snefru(context->state);
     183             : 
     184         306 :         for (i = 0, j = 0; j < 32; i++, j += 4) {
     185         272 :                 digest[j] = (unsigned char) ((context->state[i] >> 24) & 0xff);
     186         272 :                 digest[j + 1] = (unsigned char) ((context->state[i] >> 16) & 0xff);
     187         272 :                 digest[j + 2] = (unsigned char) ((context->state[i] >> 8) & 0xff);
     188         272 :                 digest[j + 3] = (unsigned char) (context->state[i] & 0xff);
     189             :         }
     190             : 
     191          34 :         ZEND_SECURE_ZERO(context, sizeof(*context));
     192          34 : }
     193             : 
     194             : const php_hash_ops php_hash_snefru_ops = {
     195             :         (php_hash_init_func_t) PHP_SNEFRUInit,
     196             :         (php_hash_update_func_t) PHP_SNEFRUUpdate,
     197             :         (php_hash_final_func_t) PHP_SNEFRUFinal,
     198             :         (php_hash_copy_func_t) php_hash_copy,
     199             :         32,
     200             :         32,
     201             :         sizeof(PHP_SNEFRU_CTX),
     202             :         1
     203             : };

Generated by: LCOV version 1.10

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

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