Coverage Report

Created: 2026-10-28 06:20

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
20.5M
  SBE = SB[C & 0xff]; \
22
20.5M
  L ^= SBE; \
23
20.5M
  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
40.1k
{
40
40.1k
  static const int shifts[4] = {16, 8, 16, 24};
41
40.1k
  int b, index, rshift, lshift;
42
40.1k
  const uint32_t *t0,*t1;
43
40.1k
  uint32_t SBE,B00,B01,B02,B03,B04,B05,B06,B07,B08,B09,B10,B11,B12,B13,B14,B15;
44
45
40.1k
  B00 = input[0];
46
40.1k
  B01 = input[1];
47
40.1k
  B02 = input[2];
48
40.1k
  B03 = input[3];
49
40.1k
  B04 = input[4];
50
40.1k
  B05 = input[5];
51
40.1k
  B06 = input[6];
52
40.1k
  B07 = input[7];
53
40.1k
  B08 = input[8];
54
40.1k
  B09 = input[9];
55
40.1k
  B10 = input[10];
56
40.1k
  B11 = input[11];
57
40.1k
  B12 = input[12];
58
40.1k
  B13 = input[13];
59
40.1k
  B14 = input[14];
60
40.1k
  B15 = input[15];
61
62
361k
  for (index = 0; index < 8; index++) {
63
321k
    t0 = tables[2*index+0];
64
321k
    t1 = tables[2*index+1];
65
1.60M
    for (b = 0; b < 4; b++) {
66
1.28M
      round(B15, B00, B01, t0);
67
1.28M
      round(B00, B01, B02, t0);
68
1.28M
      round(B01, B02, B03, t1);
69
1.28M
      round(B02, B03, B04, t1);
70
1.28M
      round(B03, B04, B05, t0);
71
1.28M
      round(B04, B05, B06, t0);
72
1.28M
      round(B05, B06, B07, t1);
73
1.28M
      round(B06, B07, B08, t1);
74
1.28M
      round(B07, B08, B09, t0);
75
1.28M
      round(B08, B09, B10, t0);
76
1.28M
      round(B09, B10, B11, t1);
77
1.28M
      round(B10, B11, B12, t1);
78
1.28M
      round(B11, B12, B13, t0);
79
1.28M
      round(B12, B13, B14, t0);
80
1.28M
      round(B13, B14, B15, t1);
81
1.28M
      round(B14, B15, B00, t1);
82
83
1.28M
      rshift = shifts[b];
84
1.28M
      lshift = 32-rshift;
85
86
1.28M
      B00 = (B00 >> rshift) | (B00 << lshift);
87
1.28M
      B01 = (B01 >> rshift) | (B01 << lshift);
88
1.28M
      B02 = (B02 >> rshift) | (B02 << lshift);
89
1.28M
      B03 = (B03 >> rshift) | (B03 << lshift);
90
1.28M
      B04 = (B04 >> rshift) | (B04 << lshift);
91
1.28M
      B05 = (B05 >> rshift) | (B05 << lshift);
92
1.28M
      B06 = (B06 >> rshift) | (B06 << lshift);
93
1.28M
      B07 = (B07 >> rshift) | (B07 << lshift);
94
1.28M
      B08 = (B08 >> rshift) | (B08 << lshift);
95
1.28M
      B09 = (B09 >> rshift) | (B09 << lshift);
96
1.28M
      B10 = (B10 >> rshift) | (B10 << lshift);
97
1.28M
      B11 = (B11 >> rshift) | (B11 << lshift);
98
1.28M
      B12 = (B12 >> rshift) | (B12 << lshift);
99
1.28M
      B13 = (B13 >> rshift) | (B13 << lshift);
100
1.28M
      B14 = (B14 >> rshift) | (B14 << lshift);
101
1.28M
      B15 = (B15 >> rshift) | (B15 << lshift);
102
1.28M
    }
103
321k
  }
104
40.1k
  input[0] ^= B15;
105
40.1k
  input[1] ^= B14;
106
40.1k
  input[2] ^= B13;
107
40.1k
  input[3] ^= B12;
108
40.1k
  input[4] ^= B11;
109
40.1k
  input[5] ^= B10;
110
40.1k
  input[6] ^= B09;
111
40.1k
  input[7] ^= B08;
112
#if DBG_SNEFRU
113
  ph(input);
114
#endif
115
40.1k
}
116
117
static inline void SnefruTransform(PHP_SNEFRU_CTX *context, const unsigned char input[32])
118
40.1k
{
119
40.1k
  int i, j;
120
121
360k
  for (i = 0, j = 0; i < 32; i += 4, ++j) {
122
320k
    context->state[8+j] = ((unsigned)input[i] << 24) | ((unsigned)input[i+1] << 16) |
123
320k
                ((unsigned)input[i+2] << 8) | (unsigned)input[i+3];
124
320k
  }
125
40.1k
  Snefru(context->state);
126
40.1k
  ZEND_SECURE_ZERO(&context->state[8], sizeof(uint32_t) * 8);
127
40.1k
}
128
129
PHP_HASH_API void PHP_SNEFRUInit(PHP_SNEFRU_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
130
59
{
131
59
  memset(context, 0, sizeof(*context));
132
59
}
133
134
PHP_HASH_API void PHP_SNEFRUUpdate(PHP_SNEFRU_CTX *context, const unsigned char *input, size_t len)
135
37
{
136
37
  uint64_t count = (((uint64_t) context->count[0] << 32) | context->count[1]) + (uint64_t) len * 8;
137
138
37
  context->count[0] = (uint32_t) (count >> 32);
139
37
  context->count[1] = (uint32_t) count;
140
141
37
  if (context->length + len < 32) {
142
2
    memcpy(&context->buffer[context->length], input, len);
143
2
    context->length += (unsigned char)len;
144
35
  } else {
145
35
    size_t i = 0, r = (context->length + len) % 32;
146
147
35
    if (context->length) {
148
17
      i = 32 - context->length;
149
17
      memcpy(&context->buffer[context->length], input, i);
150
17
      SnefruTransform(context, context->buffer);
151
17
    }
152
153
40.1k
    for (; i + 32 <= len; i += 32) {
154
40.0k
      SnefruTransform(context, input + i);
155
40.0k
    }
156
157
35
    memcpy(context->buffer, input + i, r);
158
35
    ZEND_SECURE_ZERO(&context->buffer[r], 32 - r);
159
35
    context->length = (unsigned char)r;
160
35
  }
161
37
}
162
163
PHP_HASH_API void PHP_SNEFRUFinal(unsigned char digest[32], PHP_SNEFRU_CTX *context)
164
37
{
165
37
  uint32_t i, j;
166
167
37
  if (context->length) {
168
27
    SnefruTransform(context, context->buffer);
169
27
  }
170
171
37
  context->state[14] = context->count[0];
172
37
  context->state[15] = context->count[1];
173
37
  Snefru(context->state);
174
175
333
  for (i = 0, j = 0; j < 32; i++, j += 4) {
176
296
    digest[j] = (unsigned char) ((context->state[i] >> 24) & 0xff);
177
296
    digest[j + 1] = (unsigned char) ((context->state[i] >> 16) & 0xff);
178
296
    digest[j + 2] = (unsigned char) ((context->state[i] >> 8) & 0xff);
179
296
    digest[j + 3] = (unsigned char) (context->state[i] & 0xff);
180
296
  }
181
182
37
  ZEND_SECURE_ZERO(context, sizeof(*context));
183
37
}
184
185
static hash_spec_result php_snefru_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
186
59
{
187
59
  PHP_SNEFRU_CTX *ctx = (PHP_SNEFRU_CTX *) hash->context;
188
59
  hash_spec_result r = HASH_SPEC_FAILURE;
189
59
  if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
190
41
    && (r = php_hash_unserialize_spec(hash, zv, PHP_SNEFRU_SPEC)) == HASH_SPEC_SUCCESS
191
38
    && ctx->length < sizeof(ctx->buffer)) {
192
37
    return HASH_SPEC_SUCCESS;
193
37
  }
194
195
22
    return r != HASH_SPEC_SUCCESS ? r : CONTEXT_VALIDATION_FAILURE;
196
59
}
197
198
const php_hash_ops php_hash_snefru_ops = {
199
  "snefru",
200
  (php_hash_init_func_t) PHP_SNEFRUInit,
201
  (php_hash_update_func_t) PHP_SNEFRUUpdate,
202
  (php_hash_final_func_t) PHP_SNEFRUFinal,
203
  php_hash_copy,
204
  php_hash_serialize,
205
  php_snefru_unserialize,
206
  PHP_SNEFRU_SPEC,
207
  32,
208
  32,
209
  sizeof(PHP_SNEFRU_CTX),
210
  1,
211
  0
212
};