xref: /PHP-8.0/ext/hash/hash_haval.c (revision 2b5de6f8)
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   | http://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   | Author: Sara Golemon <pollita@php.net>                               |
14   +----------------------------------------------------------------------+
15 */
16 
17 #include "php_hash.h"
18 #include "php_hash_haval.h"
19 
20 static const unsigned char PADDING[128] ={
21 	1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
29 
30 static const uint32_t D0[8] = {
31 	0x243F6A88, 0x85A308D3, 0x13198A2E, 0x03707344, 0xA4093822, 0x299F31D0, 0x082EFA98, 0xEC4E6C89 };
32 
33 static const uint32_t K2[32] = {
34 	0x452821E6, 0x38D01377, 0xBE5466CF, 0x34E90C6C, 0xC0AC29B7, 0xC97C50DD, 0x3F84D5B5, 0xB5470917,
35 	0x9216D5D9, 0x8979FB1B, 0xD1310BA6, 0x98DFB5AC, 0x2FFD72DB, 0xD01ADFB7, 0xB8E1AFED, 0x6A267E96,
36 	0xBA7C9045, 0xF12C7F99, 0x24A19947, 0xB3916CF7, 0x0801F2E2, 0x858EFC16, 0x636920D8, 0x71574E69,
37 	0xA458FEA3, 0xF4933D7E, 0x0D95748F, 0x728EB658, 0x718BCD58, 0x82154AEE, 0x7B54A41D, 0xC25A59B5 };
38 
39 static const uint32_t K3[32] = {
40 	0x9C30D539, 0x2AF26013, 0xC5D1B023, 0x286085F0, 0xCA417918, 0xB8DB38EF, 0x8E79DCB0, 0x603A180E,
41 	0x6C9E0E8B, 0xB01E8A3E, 0xD71577C1, 0xBD314B27, 0x78AF2FDA, 0x55605C60, 0xE65525F3, 0xAA55AB94,
42 	0x57489862, 0x63E81440, 0x55CA396A, 0x2AAB10B6, 0xB4CC5C34, 0x1141E8CE, 0xA15486AF, 0x7C72E993,
43 	0xB3EE1411, 0x636FBC2A, 0x2BA9C55D, 0x741831F6, 0xCE5C3E16, 0x9B87931E, 0xAFD6BA33, 0x6C24CF5C };
44 
45 static const uint32_t K4[32] = {
46 	0x7A325381, 0x28958677, 0x3B8F4898, 0x6B4BB9AF, 0xC4BFE81B, 0x66282193, 0x61D809CC, 0xFB21A991,
47 	0x487CAC60, 0x5DEC8032, 0xEF845D5D, 0xE98575B1, 0xDC262302, 0xEB651B88, 0x23893E81, 0xD396ACC5,
48 	0x0F6D6FF3, 0x83F44239, 0x2E0B4482, 0xA4842004, 0x69C8F04A, 0x9E1F9B5E, 0x21C66842, 0xF6E96C9A,
49 	0x670C9C61, 0xABD388F0, 0x6A51A0D2, 0xD8542F68, 0x960FA728, 0xAB5133A3, 0x6EEF0B6C, 0x137A3BE4 };
50 
51 static const uint32_t K5[32] = {
52 	0xBA3BF050, 0x7EFB2A98, 0xA1F1651D, 0x39AF0176, 0x66CA593E, 0x82430E88, 0x8CEE8619, 0x456F9FB4,
53 	0x7D84A5C3, 0x3B8B5EBE, 0xE06F75D8, 0x85C12073, 0x401A449F, 0x56C16AA6, 0x4ED3AA62, 0x363F7706,
54 	0x1BFEDF72, 0x429B023D, 0x37D0D724, 0xD00A1248, 0xDB0FEAD3, 0x49F1C09B, 0x075372C9, 0x80991B7B,
55 	0x25D479D8, 0xF6E8DEF7, 0xE3FE501A, 0xB6794C3B, 0x976CE0BD, 0x04C006BA, 0xC1A94FB6, 0x409F60C4 };
56 
57 static const short I2[32] = {	 5, 14, 26, 18, 11, 28,  7, 16,  0, 23, 20, 22,  1, 10,  4,  8,
58 						30,  3, 21,  9, 17, 24, 29,  6, 19, 12, 15, 13,  2, 25, 31, 27 };
59 
60 static const short I3[32] = {	19,  9,  4, 20, 28, 17,  8, 22, 29, 14, 25, 12, 24, 30, 16, 26,
61 						31, 15,  7,  3,  1,  0, 18, 27, 13,  6, 21, 10, 23, 11,  5,  2 };
62 
63 static const short I4[32] = {	24,  4,  0, 14,  2,  7, 28, 23, 26,  6, 30, 20, 18, 25, 19,  3,
64 						22, 11, 31, 21,  8, 27, 12,  9,  1, 29,  5, 15, 17, 10, 16, 13 };
65 
66 static const short I5[32] = {	27,  3, 21, 26, 17, 11, 20, 29, 19,  0, 12,  7, 13,  8, 31, 10,
67 						 5,  9, 14, 30, 18,  6, 28, 24,  2, 23, 16, 22,  4,  1, 25, 15 };
68 
69 static const short M0[32] = {	0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1,
70 						0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1 };
71 
72 static const short M1[32] = {	1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2,
73 						1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2 };
74 
75 static const short M2[32] = {	2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3,
76 						2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3 };
77 
78 static const short M3[32] = {	3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4,
79 						3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4 };
80 
81 static const short M4[32] = {	4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5,
82 						4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5 };
83 
84 static const short M5[32] = {	5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6,
85 						5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7, 6 };
86 
87 static const short M6[32] = {	6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7,
88 						6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0, 7 };
89 
90 static const short M7[32] = {	7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0,
91 						7, 6, 5, 4, 3, 2, 1, 0, 7, 6, 5, 4, 3, 2, 1, 0 };
92 
93 /* {{{ Encode
94    Encodes input (uint32_t) into output (unsigned char). Assumes len is
95    a multiple of 4.
96  */
Encode(unsigned char * output,uint32_t * input,unsigned int len)97 static void Encode(unsigned char *output, uint32_t *input, unsigned int len)
98 {
99 	unsigned int i, j;
100 
101 	for (i = 0, j = 0; j < len; i++, j += 4) {
102 		output[j] = (unsigned char) (input[i] & 0xff);
103 		output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
104 		output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
105 		output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
106 	}
107 }
108 /* }}} */
109 
110 /* {{{ Decode
111    Decodes input (unsigned char) into output (uint32_t). Assumes len is
112    a multiple of 4.
113  */
Decode(uint32_t * output,const unsigned char * input,unsigned int len)114 static void Decode(uint32_t *output, const unsigned char *input, unsigned int len)
115 {
116 	unsigned int i, j;
117 
118 	for (i = 0, j = 0; j < len; i++, j += 4) {
119 		output[i] = ((uint32_t) input[j]) | (((uint32_t) input[j + 1]) << 8) |
120 			(((uint32_t) input[j + 2]) << 16) | (((uint32_t) input[j + 3]) << 24);
121 	}
122 }
123 /* }}} */
124 
125 #define F1(x6,x5,x4,x3,x2,x1,x0)	( ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ ((x0) & (x1)) ^ (x0) )
126 #define F2(x6,x5,x4,x3,x2,x1,x0)	( ((x1) & (x2) & (x3)) ^ ((x2) & (x4) & (x5)) ^ ((x1) & (x2)) ^ ((x1) & (x4)) ^ \
127 									  ((x2) & (x6)) ^ ((x3) & (x5)) ^ ((x4) & (x5)) ^ ((x0) & (x2)) ^ (x0) )
128 #define F3(x6,x5,x4,x3,x2,x1,x0)	( ((x1) & (x2) & (x3)) ^ ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ ((x0) & (x3)) ^ (x0) )
129 #define F4(x6,x5,x4,x3,x2,x1,x0)	( ((x1) & (x2) & (x3)) ^ ((x2) & (x4) & (x5)) ^ ((x3) & (x4) & (x6)) ^ \
130 									  ((x1) & (x4)) ^ ((x2) & (x6)) ^ ((x3) & (x4)) ^ ((x3) & (x5)) ^ \
131 									  ((x3) & (x6)) ^ ((x4) & (x5)) ^ ((x4) & (x6)) ^ ((x0) & (x4)) ^ (x0) )
132 #define F5(x6,x5,x4,x3,x2,x1,x0)	( ((x1) & (x4)) ^ ((x2) & (x5)) ^ ((x3) & (x6)) ^ \
133 									  ((x0) & (x1) & (x2) & (x3)) ^ ((x0) & (x5)) ^ (x0) )
134 
135 #define ROTR(x,n)	(((x) >> (n)) | ((x) << (32 - (n))))
136 
137 
138 /* {{{ PHP_3HAVALTransform */
PHP_3HAVALTransform(uint32_t state[8],const unsigned char block[128])139 static void PHP_3HAVALTransform(uint32_t state[8], const unsigned char block[128])
140 {
141 	uint32_t E[8];
142 	uint32_t x[32];
143 	int i;
144 
145 	Decode(x, block, 128);
146 
147 	for(i = 0; i < 8; i++) {
148 		E[i] = state[i];
149 	}
150 
151 	for(i = 0; i < 32; i++) {
152 		E[7 - (i % 8)] = ROTR(F1(E[M1[i]],E[M0[i]],E[M3[i]],E[M5[i]],E[M6[i]],E[M2[i]],E[M4[i]]),7) + ROTR(E[M7[i]],11) + x[i];
153 	}
154 	for(i = 0; i < 32; i++) {
155 		E[7 - (i % 8)] = ROTR(F2(E[M4[i]],E[M2[i]],E[M1[i]],E[M0[i]],E[M5[i]],E[M3[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
156 	}
157 	for(i = 0; i < 32; i++) {
158 		E[7 - (i % 8)] = ROTR(F3(E[M6[i]],E[M1[i]],E[M2[i]],E[M3[i]],E[M4[i]],E[M5[i]],E[M0[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
159 	}
160 
161 	/* Update digest */
162 	for(i = 0; i < 8; i++) {
163 		state[i] += E[i];
164 	}
165 
166 	/* Zeroize sensitive information. */
167 	ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
168 }
169 /* }}} */
170 
171 /* {{{ PHP_4HAVALTransform */
PHP_4HAVALTransform(uint32_t state[8],const unsigned char block[128])172 static void PHP_4HAVALTransform(uint32_t state[8], const unsigned char block[128])
173 {
174 	uint32_t E[8];
175 	uint32_t x[32];
176 	int i;
177 
178 	Decode(x, block, 128);
179 
180 	for(i = 0; i < 8; i++) {
181 		E[i] = state[i];
182 	}
183 
184 	for(i = 0; i < 32; i++) {
185 		E[7 - (i % 8)] = ROTR(F1(E[M2[i]],E[M6[i]],E[M1[i]],E[M4[i]],E[M5[i]],E[M3[i]],E[M0[i]]),7) + ROTR(E[M7[i]],11) + x[i];
186 	}
187 	for(i = 0; i < 32; i++) {
188 		E[7 - (i % 8)] = ROTR(F2(E[M3[i]],E[M5[i]],E[M2[i]],E[M0[i]],E[M1[i]],E[M6[i]],E[M4[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
189 	}
190 	for(i = 0; i < 32; i++) {
191 		E[7 - (i % 8)] = ROTR(F3(E[M1[i]],E[M4[i]],E[M3[i]],E[M6[i]],E[M0[i]],E[M2[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
192 	}
193 	for(i = 0; i < 32; i++) {
194 		E[7 - (i % 8)] = ROTR(F4(E[M6[i]],E[M4[i]],E[M0[i]],E[M5[i]],E[M2[i]],E[M1[i]],E[M3[i]]),7) + ROTR(E[M7[i]],11) + x[I4[i]] + K4[i];
195 	}
196 
197 	/* Update digest */
198 	for(i = 0; i < 8; i++) {
199 		state[i] += E[i];
200 	}
201 
202 	/* Zeroize sensitive information. */
203 	ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
204 }
205 /* }}} */
206 
207 /* {{{ PHP_5HAVALTransform */
PHP_5HAVALTransform(uint32_t state[8],const unsigned char block[128])208 static void PHP_5HAVALTransform(uint32_t state[8], const unsigned char block[128])
209 {
210 	uint32_t E[8];
211 	uint32_t x[32];
212 	int i;
213 
214 	Decode(x, block, 128);
215 
216 	for(i = 0; i < 8; i++) {
217 		E[i] = state[i];
218 	}
219 	for(i = 0; i < 32; i++) {
220 		E[7 - (i % 8)] = ROTR(F1(E[M3[i]],E[M4[i]],E[M1[i]],E[M0[i]],E[M5[i]],E[M2[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[i];
221 	}
222 	for(i = 0; i < 32; i++) {
223 		E[7 - (i % 8)] = ROTR(F2(E[M6[i]],E[M2[i]],E[M1[i]],E[M0[i]],E[M3[i]],E[M4[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I2[i]] + K2[i];
224 	}
225 	for(i = 0; i < 32; i++) {
226 		E[7 - (i % 8)] = ROTR(F3(E[M2[i]],E[M6[i]],E[M0[i]],E[M4[i]],E[M3[i]],E[M1[i]],E[M5[i]]),7) + ROTR(E[M7[i]],11) + x[I3[i]] + K3[i];
227 	}
228 	for(i = 0; i < 32; i++) {
229 		E[7 - (i % 8)] = ROTR(F4(E[M1[i]],E[M5[i]],E[M3[i]],E[M2[i]],E[M0[i]],E[M4[i]],E[M6[i]]),7) + ROTR(E[M7[i]],11) + x[I4[i]] + K4[i];
230 	}
231 	for(i = 0; i < 32; i++) {
232 		E[7 - (i % 8)] = ROTR(F5(E[M2[i]],E[M5[i]],E[M0[i]],E[M6[i]],E[M4[i]],E[M3[i]],E[M1[i]]),7) + ROTR(E[M7[i]],11) + x[I5[i]] + K5[i];
233 	}
234 
235 	/* Update digest */
236 	for(i = 0; i < 8; i++) {
237 		state[i] += E[i];
238 	}
239 
240 	/* Zeroize sensitive information. */
241 	ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
242 }
243 /* }}} */
244 
245 #define PHP_HASH_HAVAL_INIT(p,b) \
246 const php_hash_ops php_hash_##p##haval##b##_ops = { \
247 	"haval" #b "," #p, \
248 	(php_hash_init_func_t) PHP_##p##HAVAL##b##Init, \
249 	(php_hash_update_func_t) PHP_HAVALUpdate, \
250 	(php_hash_final_func_t) PHP_HAVAL##b##Final, \
251 	php_hash_copy, \
252 	php_hash_serialize, \
253 	php_hash_unserialize, \
254 	PHP_HAVAL_SPEC, \
255 	((b) / 8), 128, sizeof(PHP_HAVAL_CTX), 1 }; \
256 PHP_HASH_API void PHP_##p##HAVAL##b##Init(PHP_HAVAL_CTX *context) \
257 {	int i; context->count[0] = 	context->count[1] = 	0; \
258 	for(i = 0; i < 8; i++) context->state[i] = D0[i]; \
259 	context->passes = p;	context->output = b; \
260 	context->Transform = PHP_##p##HAVALTransform; }
261 
262 PHP_HASH_HAVAL_INIT(3,128)
263 PHP_HASH_HAVAL_INIT(3,160)
264 PHP_HASH_HAVAL_INIT(3,192)
265 PHP_HASH_HAVAL_INIT(3,224)
266 PHP_HASH_HAVAL_INIT(3,256)
267 
268 PHP_HASH_HAVAL_INIT(4,128)
269 PHP_HASH_HAVAL_INIT(4,160)
270 PHP_HASH_HAVAL_INIT(4,192)
271 PHP_HASH_HAVAL_INIT(4,224)
272 PHP_HASH_HAVAL_INIT(4,256)
273 
274 PHP_HASH_HAVAL_INIT(5,128)
275 PHP_HASH_HAVAL_INIT(5,160)
276 PHP_HASH_HAVAL_INIT(5,192)
277 PHP_HASH_HAVAL_INIT(5,224)
278 PHP_HASH_HAVAL_INIT(5,256)
279 
280 /* {{{ PHP_HAVALUpdate */
PHP_HAVALUpdate(PHP_HAVAL_CTX * context,const unsigned char * input,size_t inputLen)281 PHP_HASH_API void PHP_HAVALUpdate(PHP_HAVAL_CTX *context, const unsigned char *input, size_t inputLen)
282 {
283 	unsigned int i, index, partLen;
284 
285 	/* Compute number of bytes mod 128 */
286 	index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
287 	/* Update number of bits */
288 	if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
289 		context->count[1]++;
290 	}
291 	context->count[1] += ((uint32_t) inputLen >> 29);
292 
293 	partLen = 128 - index;
294 
295 	/* Transform as many times as possible.
296 	 */
297 	if (inputLen >= partLen) {
298 		memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
299 		context->Transform(context->state, context->buffer);
300 
301 		for (i = partLen; i + 127 < inputLen; i += 128) {
302 			context->Transform(context->state, &input[i]);
303 		}
304 
305 		index = 0;
306 	} else {
307 		i = 0;
308 	}
309 
310 	/* Buffer remaining input */
311 	memcpy((unsigned char*) &context->buffer[index], (unsigned char*) &input[i], inputLen - i);
312 }
313 /* }}} */
314 
315 #define PHP_HASH_HAVAL_VERSION	0x01
316 
317 /* {{{ PHP_HAVAL128Final */
PHP_HAVAL128Final(unsigned char * digest,PHP_HAVAL_CTX * context)318 PHP_HASH_API void PHP_HAVAL128Final(unsigned char *digest, PHP_HAVAL_CTX * context)
319 {
320 	unsigned char bits[10];
321 	unsigned int index, padLen;
322 
323 	/* Version, Passes, and Digest Length */
324 	bits[0] =	(PHP_HASH_HAVAL_VERSION & 0x07) |
325 				((context->passes & 0x07) << 3) |
326 				((context->output & 0x03) << 6);
327 	bits[1] = (context->output >> 2);
328 
329 	/* Save number of bits */
330 	Encode(bits + 2, context->count, 8);
331 
332 	/* Pad out to 118 mod 128.
333 	 */
334 	index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
335 	padLen = (index < 118) ? (118 - index) : (246 - index);
336 	PHP_HAVALUpdate(context, PADDING, padLen);
337 
338 	/* Append version, passes, digest length, and message length */
339 	PHP_HAVALUpdate(context, bits, 10);
340 
341 	/* Store state in digest */
342 	context->state[3] += (context->state[7] & 0xFF000000) |
343 						 (context->state[6] & 0x00FF0000) |
344 						 (context->state[5] & 0x0000FF00) |
345 						 (context->state[4] & 0x000000FF);
346 
347 	context->state[2] += (((context->state[7] & 0x00FF0000) |
348 						   (context->state[6] & 0x0000FF00) |
349 						   (context->state[5] & 0x000000FF)) << 8) |
350 						  ((context->state[4] & 0xFF000000) >> 24);
351 
352 	context->state[1] += (((context->state[7] & 0x0000FF00) |
353 						   (context->state[6] & 0x000000FF)) << 16) |
354 						 (((context->state[5] & 0xFF000000) |
355 						   (context->state[4] & 0x00FF0000)) >> 16);
356 
357 	context->state[0] +=  ((context->state[7] & 0x000000FF) << 24) |
358 						 (((context->state[6] & 0xFF000000) |
359 						   (context->state[5] & 0x00FF0000) |
360 						   (context->state[4] & 0x0000FF00)) >> 8);
361 
362 	Encode(digest, context->state, 16);
363 
364 	/* Zeroize sensitive information.
365 	 */
366 	ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
367 }
368 /* }}} */
369 
370 /* {{{ PHP_HAVAL160Final */
PHP_HAVAL160Final(unsigned char * digest,PHP_HAVAL_CTX * context)371 PHP_HASH_API void PHP_HAVAL160Final(unsigned char *digest, PHP_HAVAL_CTX * context)
372 {
373 	unsigned char bits[10];
374 	unsigned int index, padLen;
375 
376 	/* Version, Passes, and Digest Length */
377 	bits[0] =	(PHP_HASH_HAVAL_VERSION & 0x07) |
378 				((context->passes & 0x07) << 3) |
379 				((context->output & 0x03) << 6);
380 	bits[1] = (context->output >> 2);
381 
382 	/* Save number of bits */
383 	Encode(bits + 2, context->count, 8);
384 
385 	/* Pad out to 118 mod 128.
386 	 */
387 	index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
388 	padLen = (index < 118) ? (118 - index) : (246 - index);
389 	PHP_HAVALUpdate(context, PADDING, padLen);
390 
391 	/* Append version, passes, digest length, and message length */
392 	PHP_HAVALUpdate(context, bits, 10);
393 
394 	/* Store state in digest */
395 	context->state[4] += ((context->state[7] & 0xFE000000) |
396 						  (context->state[6] & 0x01F80000) |
397 						  (context->state[5] & 0x0007F000)) >> 12;
398 
399 	context->state[3] += ((context->state[7] & 0x01F80000) |
400 						  (context->state[6] & 0x0007F000) |
401 						  (context->state[5] & 0x00000FC0)) >> 6;
402 
403 	context->state[2] +=  (context->state[7] & 0x0007F000) |
404 						  (context->state[6] & 0x00000FC0) |
405 						  (context->state[5] & 0x0000003F);
406 
407 	context->state[1] += ROTR((context->state[7] & 0x00000FC0) |
408 							  (context->state[6] & 0x0000003F) |
409 							  (context->state[5] & 0xFE000000), 25);
410 
411 	context->state[0] += ROTR((context->state[7] & 0x0000003F) |
412 							  (context->state[6] & 0xFE000000) |
413 							  (context->state[5] & 0x01F80000), 19);
414 
415 	Encode(digest, context->state, 20);
416 
417 	/* Zeroize sensitive information.
418 	 */
419 	ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
420 }
421 /* }}} */
422 
423 /* {{{ PHP_HAVAL192Final */
PHP_HAVAL192Final(unsigned char * digest,PHP_HAVAL_CTX * context)424 PHP_HASH_API void PHP_HAVAL192Final(unsigned char *digest, PHP_HAVAL_CTX * context)
425 {
426 	unsigned char bits[10];
427 	unsigned int index, padLen;
428 
429 	/* Version, Passes, and Digest Length */
430 	bits[0] =	(PHP_HASH_HAVAL_VERSION & 0x07) |
431 				((context->passes & 0x07) << 3) |
432 				((context->output & 0x03) << 6);
433 	bits[1] = (context->output >> 2);
434 
435 	/* Save number of bits */
436 	Encode(bits + 2, context->count, 8);
437 
438 	/* Pad out to 118 mod 128.
439 	 */
440 	index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
441 	padLen = (index < 118) ? (118 - index) : (246 - index);
442 	PHP_HAVALUpdate(context, PADDING, padLen);
443 
444 	/* Append version, passes, digest length, and message length */
445 	PHP_HAVALUpdate(context, bits, 10);
446 
447 	/* Store state in digest */
448 	context->state[5] += ((context->state[7] & 0xFC000000) | (context->state[6] & 0x03E00000)) >> 21;
449 	context->state[4] += ((context->state[7] & 0x03E00000) | (context->state[6] & 0x001F0000)) >> 16;
450 	context->state[3] += ((context->state[7] & 0x001F0000) | (context->state[6] & 0x0000FC00)) >> 10;
451 	context->state[2] += ((context->state[7] & 0x0000FC00) | (context->state[6] & 0x000003E0)) >>  5;
452 	context->state[1] +=  (context->state[7] & 0x000003E0) | (context->state[6] & 0x0000001F);
453 	context->state[0] += ROTR((context->state[7] & 0x0000001F) | (context->state[6] & 0xFC000000), 26);
454 	Encode(digest, context->state, 24);
455 
456 	/* Zeroize sensitive information.
457 	 */
458 	ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
459 }
460 /* }}} */
461 
462 /* {{{ PHP_HAVAL224Final */
PHP_HAVAL224Final(unsigned char * digest,PHP_HAVAL_CTX * context)463 PHP_HASH_API void PHP_HAVAL224Final(unsigned char *digest, PHP_HAVAL_CTX * context)
464 {
465 	unsigned char bits[10];
466 	unsigned int index, padLen;
467 
468 	/* Version, Passes, and Digest Length */
469 	bits[0] =	(PHP_HASH_HAVAL_VERSION & 0x07) |
470 				((context->passes & 0x07) << 3) |
471 				((context->output & 0x03) << 6);
472 	bits[1] = (context->output >> 2);
473 
474 	/* Save number of bits */
475 	Encode(bits + 2, context->count, 8);
476 
477 	/* Pad out to 118 mod 128.
478 	 */
479 	index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
480 	padLen = (index < 118) ? (118 - index) : (246 - index);
481 	PHP_HAVALUpdate(context, PADDING, padLen);
482 
483 	/* Append version, passes, digest length, and message length */
484 	PHP_HAVALUpdate(context, bits, 10);
485 
486 	/* Store state in digest */
487 	context->state[6] +=  context->state[7]        & 0x0000000F;
488 	context->state[5] += (context->state[7] >>  4) & 0x0000001F;
489 	context->state[4] += (context->state[7] >>  9) & 0x0000000F;
490 	context->state[3] += (context->state[7] >> 13) & 0x0000001F;
491 	context->state[2] += (context->state[7] >> 18) & 0x0000000F;
492 	context->state[1] += (context->state[7] >> 22) & 0x0000001F;
493 	context->state[0] += (context->state[7] >> 27) & 0x0000001F;
494 	Encode(digest, context->state, 28);
495 
496 	/* Zeroize sensitive information.
497 	 */
498 	ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
499 }
500 /* }}} */
501 
502 /* {{{ PHP_HAVAL256Final */
PHP_HAVAL256Final(unsigned char * digest,PHP_HAVAL_CTX * context)503 PHP_HASH_API void PHP_HAVAL256Final(unsigned char *digest, PHP_HAVAL_CTX * context)
504 {
505 	unsigned char bits[10];
506 	unsigned int index, padLen;
507 
508 	/* Version, Passes, and Digest Length */
509 	bits[0] =	(PHP_HASH_HAVAL_VERSION & 0x07) |
510 				((context->passes & 0x07) << 3) |
511 				((context->output & 0x03) << 6);
512 	bits[1] = (context->output >> 2);
513 
514 	/* Save number of bits */
515 	Encode(bits + 2, context->count, 8);
516 
517 	/* Pad out to 118 mod 128.
518 	 */
519 	index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
520 	padLen = (index < 118) ? (118 - index) : (246 - index);
521 	PHP_HAVALUpdate(context, PADDING, padLen);
522 
523 	/* Append version, passes, digest length, and message length */
524 	PHP_HAVALUpdate(context, bits, 10);
525 
526 	/* Store state in digest */
527 	Encode(digest, context->state, 32);
528 
529 	/* Zeroize sensitive information.
530 	 */
531 	ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
532 }
533 /* }}} */
534