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