Coverage Report

Created: 2026-09-14 06:25

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
10.9k
  aa = a; \
33
10.9k
  bb = b; \
34
10.9k
  cc = c;
35
36
#define round(a,b,c,x,mul) \
37
338k
  c ^= x; \
38
338k
  a -= t1[(unsigned char)(c)] ^ \
39
338k
    t2[(unsigned char)(((uint32_t)(c))>>(2*8))] ^ \
40
338k
    t3[(unsigned char)((c)>>(4*8))] ^ \
41
338k
    t4[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(2*8))] ; \
42
338k
  b += t4[(unsigned char)(((uint32_t)(c))>>(1*8))] ^ \
43
338k
    t3[(unsigned char)(((uint32_t)(c))>>(3*8))] ^ \
44
338k
    t2[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(1*8))] ^ \
45
338k
    t1[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(3*8))]; \
46
338k
  b *= mul;
47
48
#define pass(a,b,c,mul) \
49
42.3k
  round(a,b,c,x0,mul) \
50
42.3k
  round(b,c,a,x1,mul) \
51
42.3k
  round(c,a,b,x2,mul) \
52
42.3k
  round(a,b,c,x3,mul) \
53
42.3k
  round(b,c,a,x4,mul) \
54
42.3k
  round(c,a,b,x5,mul) \
55
42.3k
  round(a,b,c,x6,mul) \
56
42.3k
  round(b,c,a,x7,mul)
57
58
#define key_schedule \
59
31.4k
  x0 -= x7 ^ L64(0xA5A5A5A5A5A5A5A5); \
60
31.4k
  x1 ^= x0; \
61
31.4k
  x2 += x1; \
62
31.4k
  x3 -= x2 ^ ((~x1)<<19); \
63
31.4k
  x4 ^= x3; \
64
31.4k
  x5 += x4; \
65
31.4k
  x6 -= x5 ^ ((~x4)>>23); \
66
31.4k
  x7 ^= x6; \
67
31.4k
  x0 += x7; \
68
31.4k
  x1 -= x0 ^ ((~x7)<<19); \
69
31.4k
  x2 ^= x1; \
70
31.4k
  x3 += x2; \
71
31.4k
  x4 -= x3 ^ ((~x2)>>23); \
72
31.4k
  x5 ^= x4; \
73
31.4k
  x6 += x5; \
74
31.4k
  x7 -= x6 ^ L64(0x0123456789ABCDEF);
75
76
#define feedforward \
77
10.9k
  a ^= aa; \
78
10.9k
  b -= bb; \
79
10.9k
  c += cc;
80
81
#define compress(passes) \
82
10.9k
  save_abc \
83
10.9k
  pass(a,b,c,5) \
84
10.9k
  key_schedule \
85
10.9k
  pass(c,a,b,7) \
86
10.9k
  key_schedule \
87
10.9k
  pass(b,c,a,9) \
88
20.5k
  for(pass_no=0; pass_no<passes; pass_no++) { \
89
9.65k
    key_schedule \
90
9.65k
    pass(a,b,c,9) \
91
9.65k
    tmpa=a; a=c; c=b; b=tmpa; \
92
9.65k
  } \
93
10.9k
  feedforward
94
95
#define split_ex(str) \
96
10.9k
  x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3]; \
97
10.9k
  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
10.9k
# define split split_ex
111
#endif
112
113
10.9k
#define tiger_compress(passes, str, state) \
114
10.9k
{ \
115
10.9k
  register uint64_t a, b, c, tmpa, x0, x1, x2, x3, x4, x5, x6, x7; \
116
10.9k
  uint64_t aa, bb, cc; \
117
10.9k
  unsigned int pass_no; \
118
10.9k
  \
119
10.9k
  a = state[0]; \
120
10.9k
  b = state[1]; \
121
10.9k
  c = state[2]; \
122
10.9k
  \
123
10.9k
  split(str); \
124
10.9k
  \
125
10.9k
  compress(passes); \
126
10.9k
  \
127
10.9k
  state[0] = a; \
128
10.9k
  state[1] = b; \
129
10.9k
  state[2] = c; \
130
10.9k
}
131
/* }}} */
132
133
static inline void TigerFinalize(PHP_TIGER_CTX *context)
134
40
{
135
40
  context->passed += (uint64_t) context->length << 3;
136
137
40
  context->buffer[context->length++] = 0x1;
138
40
  if (context->length % 8) {
139
30
    memset(&context->buffer[context->length], 0, 8-context->length%8);
140
30
    context->length += 8-context->length%8;
141
30
  }
142
143
40
  if (context->length > 56) {
144
11
    memset(&context->buffer[context->length], 0, 64 - context->length);
145
11
    tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
146
11
    memset(context->buffer, 0, 56);
147
29
  } else {
148
29
    memset(&context->buffer[context->length], 0, 56 - context->length);
149
29
  }
150
151
40
#ifndef WORDS_BIGENDIAN
152
40
  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
40
  tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
164
40
}
165
166
static inline void TigerDigest(unsigned char *digest_str, unsigned int digest_len, PHP_TIGER_CTX *context)
167
40
{
168
40
  unsigned int i;
169
170
836
  for (i = 0; i < digest_len; ++i) {
171
796
    digest_str[i] = (unsigned char) ((context->state[i/8] >> (8 * (i%8))) & 0xff);
172
796
  }
173
40
}
174
175
PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
176
19
{
177
19
  memset(context, 0, sizeof(*context));
178
19
  context->state[0] = L64(0x0123456789ABCDEF);
179
19
  context->state[1] = L64(0xFEDCBA9876543210);
180
19
  context->state[2] = L64(0xF096A5B4C3B2E187);
181
19
}
182
183
PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
184
29
{
185
29
  memset(context, 0, sizeof(*context));
186
29
  context->passes = 1;
187
29
  context->state[0] = L64(0x0123456789ABCDEF);
188
29
  context->state[1] = L64(0xFEDCBA9876543210);
189
29
  context->state[2] = L64(0xF096A5B4C3B2E187);
190
29
}
191
192
PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len)
193
40
{
194
40
  if (context->length + len < 64) {
195
6
    memcpy(&context->buffer[context->length], input, len);
196
6
    context->length += len;
197
34
  } else {
198
34
    size_t i = 0, r = (context->length + len) % 64;
199
200
34
    if (context->length) {
201
6
      i = 64 - context->length;
202
6
      memcpy(&context->buffer[context->length], input, i);
203
6
      tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
204
6
      ZEND_SECURE_ZERO(context->buffer, 64);
205
6
      context->passed += 512;
206
6
    }
207
208
10.8k
    for (; i + 64 <= len; i += 64) {
209
10.8k
      memcpy(context->buffer, &input[i], 64);
210
10.8k
      tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
211
10.8k
      context->passed += 512;
212
10.8k
    }
213
34
    ZEND_SECURE_ZERO(&context->buffer[r], 64-r);
214
34
    memcpy(context->buffer, &input[i], r);
215
34
    context->length = r;
216
34
  }
217
40
}
218
219
PHP_HASH_API void PHP_TIGER128Final(unsigned char digest[16], PHP_TIGER_CTX *context)
220
2
{
221
2
  TigerFinalize(context);
222
2
  TigerDigest(digest, 16, context);
223
2
  ZEND_SECURE_ZERO(context, sizeof(*context));
224
2
}
225
226
PHP_HASH_API void PHP_TIGER160Final(unsigned char digest[20], PHP_TIGER_CTX *context)
227
37
{
228
37
  TigerFinalize(context);
229
37
  TigerDigest(digest, 20, context);
230
37
  ZEND_SECURE_ZERO(context, sizeof(*context));
231
37
}
232
233
PHP_HASH_API void PHP_TIGER192Final(unsigned char digest[24], PHP_TIGER_CTX *context)
234
1
{
235
1
  TigerFinalize(context);
236
1
  TigerDigest(digest, 24, context);
237
1
  ZEND_SECURE_ZERO(context, sizeof(*context));
238
1
}
239
240
static hash_spec_result php_tiger_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
241
48
{
242
48
  PHP_TIGER_CTX *ctx = (PHP_TIGER_CTX *) hash->context;
243
48
  hash_spec_result r = HASH_SPEC_FAILURE;
244
48
  if (magic == PHP_HASH_SERIALIZE_MAGIC_SPEC
245
46
    && (r = php_hash_unserialize_spec(hash, zv, PHP_TIGER_SPEC)) == HASH_SPEC_SUCCESS
246
41
    && ctx->length < sizeof(ctx->buffer)) {
247
40
    return HASH_SPEC_SUCCESS;
248
40
  }
249
250
8
    return r != HASH_SPEC_SUCCESS ? r : CONTEXT_VALIDATION_FAILURE;
251
48
}
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
    0 \
268
  }
269
270
PHP_HASH_TIGER_OPS(3, 128);
271
PHP_HASH_TIGER_OPS(3, 160);
272
PHP_HASH_TIGER_OPS(3, 192);
273
PHP_HASH_TIGER_OPS(4, 128);
274
PHP_HASH_TIGER_OPS(4, 160);
275
PHP_HASH_TIGER_OPS(4, 192);