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