Coverage Report

Created: 2025-11-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/hash/hash_snefru.c
Line
Count
Source
1
/*
2
  +----------------------------------------------------------------------+
3
  | Copyright (c) The PHP Group                                          |
4
  +----------------------------------------------------------------------+
5
  | This source file is subject to version 3.01 of the PHP license,      |
6
  | that is bundled with this package in the file LICENSE, and is        |
7
  | available through the world-wide-web at the following url:           |
8
  | https://www.php.net/license/3_01.txt                                 |
9
  | If you did not receive a copy of the PHP license and are unable to   |
10
  | obtain it through the world-wide-web, please send a note to          |
11
  | license@php.net so we can mail you a copy immediately.               |
12
  +----------------------------------------------------------------------+
13
  | Authors: Michael Wallner <mike@php.net>                              |
14
  |          Sara Golemon <pollita@php.net>                              |
15
  +----------------------------------------------------------------------+
16
*/
17
18
#include "php_hash.h"
19
#include "php_hash_snefru.h"
20
#include "php_hash_snefru_tables.h"
21
22
#define round(L, C, N, SB)  \
23
14.9M
  SBE = SB[C & 0xff]; \
24
14.9M
  L ^= SBE; \
25
14.9M
  N ^= SBE
26
27
#ifndef DBG_SNEFRU
28
#define DBG_SNEFRU 0
29
#endif
30
31
#if DBG_SNEFRU
32
void ph(uint32_t h[16])
33
{
34
  int i;
35
  for (i = 0; i < 16; i++)
36
    printf ("%08lx", h[i]); printf("\n");
37
}
38
#endif
39
40
static inline void Snefru(uint32_t input[16])
41
29.2k
{
42
29.2k
  static const int shifts[4] = {16, 8, 16, 24};
43
29.2k
  int b, index, rshift, lshift;
44
29.2k
  const uint32_t *t0,*t1;
45
29.2k
  uint32_t SBE,B00,B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15;
46
47
29.2k
  B00 = input[0];
48
29.2k
  B01 = input[1];
49
29.2k
  B02 = input[2];
50
29.2k
  B03 = input[3];
51
29.2k
  B04 = input[4];
52
29.2k
  B05 = input[5];
53
29.2k
  B06 = input[6];
54
29.2k
  B07 = input[7];
55
29.2k
  B08 = input[8];
56
29.2k
  B09 = input[9];
57
29.2k
  B10 = input[10];
58
29.2k
  B11 = input[11];
59
29.2k
  B12 = input[12];
60
29.2k
  B13 = input[13];
61
29.2k
  B14 = input[14];
62
29.2k
  B15 = input[15];
63
64
262k
  for (index = 0; index < 8; index++) {
65
233k
    t0 = tables[2*index+0];
66
233k
    t1 = tables[2*index+1];
67
1.16M
    for (b = 0; b < 4; b++) {
68
935k
      round(B15, B00, B01, t0);
69
935k
      round(B00, B01, B02, t0);
70
935k
      round(B01, B02, B03, t1);
71
935k
      round(B02, B03, B04, t1);
72
935k
      round(B03, B04, B05, t0);
73
935k
      round(B04, B05, B06, t0);
74
935k
      round(B05, B06, B07, t1);
75
935k
      round(B06, B07, B08, t1);
76
935k
      round(B07, B08, B09, t0);
77
935k
      round(B08, B09, B10, t0);
78
935k
      round(B09, B10, B11, t1);
79
935k
      round(B10, B11, B12, t1);
80
935k
      round(B11, B12, B13, t0);
81
935k
      round(B12, B13, B14, t0);
82
935k
      round(B13, B14, B15, t1);
83
935k
      round(B14, B15, B00, t1);
84
85
935k
      rshift = shifts[b];
86
935k
      lshift = 32-rshift;
87
88
935k
      B00 = (B00 >> rshift) | (B00 << lshift);
89
935k
      B01 = (B01 >> rshift) | (B01 << lshift);
90
935k
      B02 = (B02 >> rshift) | (B02 << lshift);
91
935k
      B03 = (B03 >> rshift) | (B03 << lshift);
92
935k
      B04 = (B04 >> rshift) | (B04 << lshift);
93
935k
      B05 = (B05 >> rshift) | (B05 << lshift);
94
935k
      B06 = (B06 >> rshift) | (B06 << lshift);
95
935k
      B07 = (B07 >> rshift) | (B07 << lshift);
96
935k
      B08 = (B08 >> rshift) | (B08 << lshift);
97
935k
      B09 = (B09 >> rshift) | (B09 << lshift);
98
935k
      B10 = (B10 >> rshift) | (B10 << lshift);
99
935k
      B11 = (B11 >> rshift) | (B11 << lshift);
100
935k
      B12 = (B12 >> rshift) | (B12 << lshift);
101
935k
      B13 = (B13 >> rshift) | (B13 << lshift);
102
935k
      B14 = (B14 >> rshift) | (B14 << lshift);
103
935k
      B15 = (B15 >> rshift) | (B15 << lshift);
104
935k
    }
105
233k
  }
106
29.2k
  input[0] ^= B15;
107
29.2k
  input[1] ^= B14;
108
29.2k
  input[2] ^= B13;
109
29.2k
  input[3] ^= B12;
110
29.2k
  input[4] ^= B11;
111
29.2k
  input[5] ^= B10;
112
29.2k
  input[6] ^= B09;
113
29.2k
  input[7] ^= B08;
114
#if DBG_SNEFRU
115
  ph(input);
116
#endif
117
29.2k
}
118
119
static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char input[32])
120
29.1k
{
121
29.1k
  int i, j;
122
123
262k
  for (i = 0, j = 0; i < 32; i += 4, ++j) {
124
233k
    context->state[8+j] = ((unsigned)input[i] << 24) | ((unsigned)input[i+1] << 16) |
125
233k
                ((unsigned)input[i+2] << 8) | (unsigned)input[i+3];
126
233k
  }
127
29.1k
  Snefru(context->state);
128
29.1k
  ZEND_SECURE_ZERO(&context->state[8], sizeof(uint32_t) * 8);
129
29.1k
}
130
131
PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
132
54
{
133
54
  memset(context, 0, sizeof(*context));
134
54
}
135
136
static const uint32_t MAX32 = 0xffffffffLU;
137
138
PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *context, const unsigned char *input, size_t len)
139
45
{
140
45
  if ((MAX32 - context->count[1]) < (len * 8)) {
141
23
    context->count[0]++;
142
23
    context->count[1] = MAX32 - context->count[1];
143
23
    context->count[1] = ((uint32_t) len * 8) - context->count[1];
144
23
  } else {
145
22
    context->count[1] += (uint32_t) len * 8;
146
22
  }
147
148
45
  if (context->length + len < 32) {
149
3
    memcpy(&context->buffer[context->length], input, len);
150
3
    context->length += (unsigned char)len;
151
42
  } else {
152
42
    size_t i = 0, r = (context->length + len) % 32;
153
154
42
    if (context->length) {
155
22
      i = 32 - context->length;
156
22
      memcpy(&context->buffer[context->length], input, i);
157
22
      SnefruTransform(context, context->buffer);
158
22
    }
159
160
29.1k
    for (; i + 32 <= len; i += 32) {
161
29.1k
      SnefruTransform(context, input + i);
162
29.1k
    }
163
164
42
    memcpy(context->buffer, input + i, r);
165
42
    ZEND_SECURE_ZERO(&context->buffer[r], 32 - r);
166
42
    context->length = (unsigned char)r;
167
42
  }
168
45
}
169
170
PHP_HASH_API void PHP_SNEFRUFinal(unsigned char digest[32], PHP_SNEFRU_CTX *context)
171
45
{
172
45
  uint32_t i, j;
173
174
45
  if (context->length) {
175
39
    SnefruTransform(context, context->buffer);
176
39
  }
177
178
45
  context->state[14] = context->count[0];
179
45
  context->state[15] = context->count[1];
180
45
  Snefru(context->state);
181
182
405
  for (i = 0, j = 0; j < 32; i++, j += 4) {
183
360
    digest[j] = (unsigned char) ((context->state[i] >> 24) & 0xff);
184
360
    digest[j + 1] = (unsigned char) ((context->state[i] >> 16) & 0xff);
185
360
    digest[j + 2] = (unsigned char) ((context->state[i] >> 8) & 0xff);
186
360
    digest[j + 3] = (unsigned char) (context->state[i] & 0xff);
187
360
  }
188
189
45
  ZEND_SECURE_ZERO(context, sizeof(*context));
190
45
}
191
192
static hash_spec_result php_snefru_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
193
54
{
194
54
  PHP_SNEFRU_CTX *ctx = (PHP_SNEFRU_CTX *) hash->context;
195
54
  hash_spec_result r = HASH_SPEC_FAILURE;
196
54
  if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
197
53
    && (r = php_hash_unserialize_spec(hash, zv, PHP_SNEFRU_SPEC)) == HASH_SPEC_SUCCESS
198
46
    && ctx->length < sizeof(ctx->buffer)) {
199
45
    return HASH_SPEC_SUCCESS;
200
45
  }
201
202
9
    return r != HASH_SPEC_SUCCESS ? r : CONTEXT_VALIDATION_FAILURE;
203
54
}
204
205
const php_hash_ops php_hash_snefru_ops = {
206
  "snefru",
207
  (php_hash_init_func_t) PHP_SNEFRUInit,
208
  (php_hash_update_func_t) PHP_SNEFRUUpdate,
209
  (php_hash_final_func_t) PHP_SNEFRUFinal,
210
  php_hash_copy,
211
  php_hash_serialize,
212
  php_snefru_unserialize,
213
  PHP_SNEFRU_SPEC,
214
  32,
215
  32,
216
  sizeof(PHP_SNEFRU_CTX),
217
  1
218
};