Coverage Report

Created: 2026-06-02 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/hash/hash_tiger.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_tiger.h"
18
#include "php_hash_tiger_tables.h"
19
20
#if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
21
# if defined(__LITTLE_ENDIAN__)
22
#  undef WORDS_BIGENDIAN
23
# else
24
#  if defined(__BIG_ENDIAN__)
25
#   define WORDS_BIGENDIAN
26
#  endif
27
# endif
28
#endif
29
30
/* {{{ */
31
#define save_abc \
32
11.7k
  aa = a; \
33
11.7k
  bb = b; \
34
11.7k
  cc = c;
35
36
#define round(a,b,c,x,mul) \
37
364k
  c ^= x; \
38
364k
  a -= t1[(unsigned char)(c)] ^ \
39
364k
    t2[(unsigned char)(((uint32_t)(c))>>(2*8))] ^ \
40
364k
    t3[(unsigned char)((c)>>(4*8))] ^ \
41
364k
    t4[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(2*8))] ; \
42
364k
  b += t4[(unsigned char)(((uint32_t)(c))>>(1*8))] ^ \
43
364k
    t3[(unsigned char)(((uint32_t)(c))>>(3*8))] ^ \
44
364k
    t2[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(1*8))] ^ \
45
364k
    t1[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(3*8))]; \
46
364k
  b *= mul;
47
48
#define pass(a,b,c,mul) \
49
45.5k
  round(a,b,c,x0,mul) \
50
45.5k
  round(b,c,a,x1,mul) \
51
45.5k
  round(c,a,b,x2,mul) \
52
45.5k
  round(a,b,c,x3,mul) \
53
45.5k
  round(b,c,a,x4,mul) \
54
45.5k
  round(c,a,b,x5,mul) \
55
45.5k
  round(a,b,c,x6,mul) \
56
45.5k
  round(b,c,a,x7,mul)
57
58
#define key_schedule \
59
33.8k
  x0 -= x7 ^ L64(0xA5A5A5A5A5A5A5A5); \
60
33.8k
  x1 ^= x0; \
61
33.8k
  x2 += x1; \
62
33.8k
  x3 -= x2 ^ ((~x1)<<19); \
63
33.8k
  x4 ^= x3; \
64
33.8k
  x5 += x4; \
65
33.8k
  x6 -= x5 ^ ((~x4)>>23); \
66
33.8k
  x7 ^= x6; \
67
33.8k
  x0 += x7; \
68
33.8k
  x1 -= x0 ^ ((~x7)<<19); \
69
33.8k
  x2 ^= x1; \
70
33.8k
  x3 += x2; \
71
33.8k
  x4 -= x3 ^ ((~x2)>>23); \
72
33.8k
  x5 ^= x4; \
73
33.8k
  x6 += x5; \
74
33.8k
  x7 -= x6 ^ L64(0x0123456789ABCDEF);
75
76
#define feedforward \
77
11.7k
  a ^= aa; \
78
11.7k
  b -= bb; \
79
11.7k
  c += cc;
80
81
#define compress(passes) \
82
11.7k
  save_abc \
83
11.7k
  pass(a,b,c,5) \
84
11.7k
  key_schedule \
85
11.7k
  pass(c,a,b,7) \
86
11.7k
  key_schedule \
87
11.7k
  pass(b,c,a,9) \
88
22.0k
  for(pass_no=0; pass_no<passes; pass_no++) { \
89
10.3k
    key_schedule \
90
10.3k
    pass(a,b,c,9) \
91
10.3k
    tmpa=a; a=c; c=b; b=tmpa; \
92
10.3k
  } \
93
11.7k
  feedforward
94
95
#define split_ex(str) \
96
11.7k
  x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3]; \
97
11.7k
  x4=str[4]; x5=str[5]; x6=str[6]; x7=str[7];
98
#ifdef WORDS_BIGENDIAN
99
# define split(str) \
100
  { \
101
    int i; \
102
    uint64_t tmp[8]; \
103
     \
104
    for (i = 0; i < 64; ++i) { \
105
      ((unsigned char *) tmp)[i^7] = ((unsigned char *) str)[i]; \
106
    } \
107
    split_ex(tmp); \
108
  }
109
#else
110
11.7k
# define split split_ex
111
#endif
112
113
11.7k
#define tiger_compress(passes, str, state) \
114
11.7k
{ \
115
11.7k
  register uint64_t a, b, c, tmpa, x0, x1, x2, x3, x4, x5, x6, x7; \
116
11.7k
  uint64_t aa, bb, cc; \
117
11.7k
  unsigned int pass_no; \
118
11.7k
  \
119
11.7k
  a = state[0]; \
120
11.7k
  b = state[1]; \
121
11.7k
  c = state[2]; \
122
11.7k
  \
123
11.7k
  split(str); \
124
11.7k
  \
125
11.7k
  compress(passes); \
126
11.7k
  \
127
11.7k
  state[0] = a; \
128
11.7k
  state[1] = b; \
129
11.7k
  state[2] = c; \
130
11.7k
}
131
/* }}} */
132
133
static inline void TigerFinalize(PHP_TIGER_CTX *context)
134
36
{
135
36
  context->passed += (uint64_t) context->length << 3;
136
137
36
  context->buffer[context->length++] = 0x1;
138
36
  if (context->length % 8) {
139
28
    memset(&context->buffer[context->length], 0, 8-context->length%8);
140
28
    context->length += 8-context->length%8;
141
28
  }
142
143
36
  if (context->length > 56) {
144
9
    memset(&context->buffer[context->length], 0, 64 - context->length);
145
9
    tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
146
9
    memset(context->buffer, 0, 56);
147
27
  } else {
148
27
    memset(&context->buffer[context->length], 0, 56 - context->length);
149
27
  }
150
151
36
#ifndef WORDS_BIGENDIAN
152
36
  memcpy(&context->buffer[56], &context->passed, sizeof(uint64_t));
153
#else
154
  context->buffer[56] = (unsigned char) (context->passed & 0xff);
155
  context->buffer[57] = (unsigned char) ((context->passed >> 8) & 0xff);
156
  context->buffer[58] = (unsigned char) ((context->passed >> 16) & 0xff);
157
  context->buffer[59] = (unsigned char) ((context->passed >> 24) & 0xff);
158
  context->buffer[60] = (unsigned char) ((context->passed >> 32) & 0xff);
159
  context->buffer[61] = (unsigned char) ((context->passed >> 40) & 0xff);
160
  context->buffer[62] = (unsigned char) ((context->passed >> 48) & 0xff);
161
  context->buffer[63] = (unsigned char) ((context->passed >> 56) & 0xff);
162
#endif
163
36
  tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
164
36
}
165
166
static inline void TigerDigest(unsigned char *digest_str, unsigned int digest_len, PHP_TIGER_CTX *context)
167
36
{
168
36
  unsigned int i;
169
170
760
  for (i = 0; i < digest_len; ++i) {
171
724
    digest_str[i] = (unsigned char) ((context->state[i/8] >> (8 * (i%8))) & 0xff);
172
724
  }
173
36
}
174
175
PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
176
20
{
177
20
  memset(context, 0, sizeof(*context));
178
20
  context->state[0] = L64(0x0123456789ABCDEF);
179
20
  context->state[1] = L64(0xFEDCBA9876543210);
180
20
  context->state[2] = L64(0xF096A5B4C3B2E187);
181
20
}
182
183
PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
184
24
{
185
24
  memset(context, 0, sizeof(*context));
186
24
  context->passes = 1;
187
24
  context->state[0] = L64(0x0123456789ABCDEF);
188
24
  context->state[1] = L64(0xFEDCBA9876543210);
189
24
  context->state[2] = L64(0xF096A5B4C3B2E187);
190
24
}
191
192
PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len)
193
36
{
194
36
  if (context->length + len < 64) {
195
4
    memcpy(&context->buffer[context->length], input, len);
196
4
    context->length += len;
197
32
  } else {
198
32
    size_t i = 0, r = (context->length + len) % 64;
199
200
32
    if (context->length) {
201
9
      i = 64 - context->length;
202
9
      memcpy(&context->buffer[context->length], input, i);
203
9
      tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
204
9
      ZEND_SECURE_ZERO(context->buffer, 64);
205
9
      context->passed += 512;
206
9
    }
207
208
11.7k
    for (; i + 64 <= len; i += 64) {
209
11.6k
      memcpy(context->buffer, &input[i], 64);
210
11.6k
      tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
211
11.6k
      context->passed += 512;
212
11.6k
    }
213
32
    ZEND_SECURE_ZERO(&context->buffer[r], 64-r);
214
32
    memcpy(context->buffer, &input[i], r);
215
32
    context->length = r;
216
32
  }
217
36
}
218
219
PHP_HASH_API void PHP_TIGER128Final(unsigned char digest[16], PHP_TIGER_CTX *context)
220
1
{
221
1
  TigerFinalize(context);
222
1
  TigerDigest(digest, 16, context);
223
1
  ZEND_SECURE_ZERO(context, sizeof(*context));
224
1
}
225
226
PHP_HASH_API void PHP_TIGER160Final(unsigned char digest[20], PHP_TIGER_CTX *context)
227
33
{
228
33
  TigerFinalize(context);
229
33
  TigerDigest(digest, 20, context);
230
33
  ZEND_SECURE_ZERO(context, sizeof(*context));
231
33
}
232
233
PHP_HASH_API void PHP_TIGER192Final(unsigned char digest[24], PHP_TIGER_CTX *context)
234
2
{
235
2
  TigerFinalize(context);
236
2
  TigerDigest(digest, 24, context);
237
2
  ZEND_SECURE_ZERO(context, sizeof(*context));
238
2
}
239
240
static hash_spec_result php_tiger_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
241
44
{
242
44
  PHP_TIGER_CTX *ctx = (PHP_TIGER_CTX *) hash->context;
243
44
  hash_spec_result r = HASH_SPEC_FAILURE;
244
44
  if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
245
42
    && (r = php_hash_unserialize_spec(hash, zv, PHP_TIGER_SPEC)) == HASH_SPEC_SUCCESS
246
37
    && ctx->length < sizeof(ctx->buffer)) {
247
36
    return HASH_SPEC_SUCCESS;
248
36
  }
249
250
8
    return r != HASH_SPEC_SUCCESS ? r : CONTEXT_VALIDATION_FAILURE;
251
44
}
252
253
#define PHP_HASH_TIGER_OPS(p, b) \
254
  const php_hash_ops php_hash_##p##tiger##b##_ops = { \
255
    "tiger" #b "," #p, \
256
    (php_hash_init_func_t) PHP_##p##TIGERInit, \
257
    (php_hash_update_func_t) PHP_TIGERUpdate, \
258
    (php_hash_final_func_t) PHP_TIGER##b##Final, \
259
    php_hash_copy, \
260
    php_hash_serialize, \
261
    php_tiger_unserialize, \
262
    PHP_TIGER_SPEC, \
263
    b/8, \
264
    64, \
265
    sizeof(PHP_TIGER_CTX), \
266
    1 \
267
  }
268
269
PHP_HASH_TIGER_OPS(3, 128);
270
PHP_HASH_TIGER_OPS(3, 160);
271
PHP_HASH_TIGER_OPS(3, 192);
272
PHP_HASH_TIGER_OPS(4, 128);
273
PHP_HASH_TIGER_OPS(4, 160);
274
PHP_HASH_TIGER_OPS(4, 192);