Coverage Report

Created: 2026-06-02 06:36

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