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