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_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
7.92k
  aa = a; \
33
7.92k
  bb = b; \
34
7.92k
  cc = c;
35
36
#define round(a,b,c,x,mul) \
37
242k
  c ^= x; \
38
242k
  a -= t1[(unsigned char)(c)] ^ \
39
242k
    t2[(unsigned char)(((uint32_t)(c))>>(2*8))] ^ \
40
242k
    t3[(unsigned char)((c)>>(4*8))] ^ \
41
242k
    t4[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(2*8))] ; \
42
242k
  b += t4[(unsigned char)(((uint32_t)(c))>>(1*8))] ^ \
43
242k
    t3[(unsigned char)(((uint32_t)(c))>>(3*8))] ^ \
44
242k
    t2[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(1*8))] ^ \
45
242k
    t1[(unsigned char)(((uint32_t)((c)>>(4*8)))>>(3*8))]; \
46
242k
  b *= mul;
47
48
#define pass(a,b,c,mul) \
49
30.3k
  round(a,b,c,x0,mul) \
50
30.3k
  round(b,c,a,x1,mul) \
51
30.3k
  round(c,a,b,x2,mul) \
52
30.3k
  round(a,b,c,x3,mul) \
53
30.3k
  round(b,c,a,x4,mul) \
54
30.3k
  round(c,a,b,x5,mul) \
55
30.3k
  round(a,b,c,x6,mul) \
56
30.3k
  round(b,c,a,x7,mul)
57
58
#define key_schedule \
59
22.4k
  x0 -= x7 ^ L64(0xA5A5A5A5A5A5A5A5); \
60
22.4k
  x1 ^= x0; \
61
22.4k
  x2 += x1; \
62
22.4k
  x3 -= x2 ^ ((~x1)<<19); \
63
22.4k
  x4 ^= x3; \
64
22.4k
  x5 += x4; \
65
22.4k
  x6 -= x5 ^ ((~x4)>>23); \
66
22.4k
  x7 ^= x6; \
67
22.4k
  x0 += x7; \
68
22.4k
  x1 -= x0 ^ ((~x7)<<19); \
69
22.4k
  x2 ^= x1; \
70
22.4k
  x3 += x2; \
71
22.4k
  x4 -= x3 ^ ((~x2)>>23); \
72
22.4k
  x5 ^= x4; \
73
22.4k
  x6 += x5; \
74
22.4k
  x7 -= x6 ^ L64(0x0123456789ABCDEF);
75
76
#define feedforward \
77
7.92k
  a ^= aa; \
78
7.92k
  b -= bb; \
79
7.92k
  c += cc;
80
81
#define compress(passes) \
82
7.92k
  save_abc \
83
7.92k
  pass(a,b,c,5) \
84
7.92k
  key_schedule \
85
7.92k
  pass(c,a,b,7) \
86
7.92k
  key_schedule \
87
7.92k
  pass(b,c,a,9) \
88
14.4k
  for(pass_no=0; pass_no<passes; pass_no++) { \
89
6.55k
    key_schedule \
90
6.55k
    pass(a,b,c,9) \
91
6.55k
    tmpa=a; a=c; c=b; b=tmpa; \
92
6.55k
  } \
93
7.92k
  feedforward
94
95
#define split_ex(str) \
96
7.92k
  x0=str[0]; x1=str[1]; x2=str[2]; x3=str[3]; \
97
7.92k
  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
7.92k
# define split split_ex
111
#endif
112
113
7.92k
#define tiger_compress(passes, str, state) \
114
7.92k
{ \
115
7.92k
  register uint64_t a, b, c, tmpa, x0, x1, x2, x3, x4, x5, x6, x7; \
116
7.92k
  uint64_t aa, bb, cc; \
117
7.92k
  unsigned int pass_no; \
118
7.92k
  \
119
7.92k
  a = state[0]; \
120
7.92k
  b = state[1]; \
121
7.92k
  c = state[2]; \
122
7.92k
  \
123
7.92k
  split(str); \
124
7.92k
  \
125
7.92k
  compress(passes); \
126
7.92k
  \
127
7.92k
  state[0] = a; \
128
7.92k
  state[1] = b; \
129
7.92k
  state[2] = c; \
130
7.92k
}
131
/* }}} */
132
133
static inline void TigerFinalize(PHP_TIGER_CTX *context)
134
37
{
135
37
  context->passed += (uint64_t) context->length << 3;
136
137
37
  context->buffer[context->length++] = 0x1;
138
37
  if (context->length % 8) {
139
29
    memset(&context->buffer[context->length], 0, 8-context->length%8);
140
29
    context->length += 8-context->length%8;
141
29
  }
142
143
37
  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
26
  } else {
148
26
    memset(&context->buffer[context->length], 0, 56 - context->length);
149
26
  }
150
151
37
#ifndef WORDS_BIGENDIAN
152
37
  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
37
  tiger_compress(context->passes, ((uint64_t *) context->buffer), context->state);
164
37
}
165
166
static inline void TigerDigest(unsigned char *digest_str, unsigned int digest_len, PHP_TIGER_CTX *context)
167
37
{
168
37
  unsigned int i;
169
170
781
  for (i = 0; i < digest_len; ++i) {
171
744
    digest_str[i] = (unsigned char) ((context->state[i/8] >> (8 * (i%8))) & 0xff);
172
744
  }
173
37
}
174
175
PHP_HASH_API void PHP_3TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
176
18
{
177
18
  memset(context, 0, sizeof(*context));
178
18
  context->state[0] = L64(0x0123456789ABCDEF);
179
18
  context->state[1] = L64(0xFEDCBA9876543210);
180
18
  context->state[2] = L64(0xF096A5B4C3B2E187);
181
18
}
182
183
PHP_HASH_API void PHP_4TIGERInit(PHP_TIGER_CTX *context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
184
26
{
185
26
  memset(context, 0, sizeof(*context));
186
26
  context->passes = 1;
187
26
  context->state[0] = L64(0x0123456789ABCDEF);
188
26
  context->state[1] = L64(0xFEDCBA9876543210);
189
26
  context->state[2] = L64(0xF096A5B4C3B2E187);
190
26
}
191
192
PHP_HASH_API void PHP_TIGERUpdate(PHP_TIGER_CTX *context, const unsigned char *input, size_t len)
193
37
{
194
37
  if (context->length + len < 64) {
195
4
    memcpy(&context->buffer[context->length], input, len);
196
4
    context->length += len;
197
33
  } else {
198
33
    size_t i = 0, r = (context->length + len) % 64;
199
200
33
    if (context->length) {
201
8
      i = 64 - context->length;
202
8
      memcpy(&context->buffer[context->length], input, i);
203
8
      tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
204
8
      ZEND_SECURE_ZERO(context->buffer, 64);
205
8
      context->passed += 512;
206
8
    }
207
208
7.90k
    for (; i + 64 <= len; i += 64) {
209
7.87k
      memcpy(context->buffer, &input[i], 64);
210
7.87k
      tiger_compress(context->passes, ((const uint64_t *) context->buffer), context->state);
211
7.87k
      context->passed += 512;
212
7.87k
    }
213
33
    ZEND_SECURE_ZERO(&context->buffer[r], 64-r);
214
33
    memcpy(context->buffer, &input[i], r);
215
33
    context->length = r;
216
33
  }
217
37
}
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
34
{
228
34
  TigerFinalize(context);
229
34
  TigerDigest(digest, 20, context);
230
34
  ZEND_SECURE_ZERO(context, sizeof(*context));
231
34
}
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
38
    && ctx->length < sizeof(ctx->buffer)) {
247
37
    return HASH_SPEC_SUCCESS;
248
37
  }
249
250
7
    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);