Coverage Report

Created: 2026-01-18 06:47

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