Coverage Report

Created: 2025-06-13 06:43

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