Coverage Report

Created: 2026-06-13 07:01

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