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