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