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 | Authors: Michael Wallner <mike@php.net> |
16 | Sara Golemon <pollita@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #include "php_hash.h"
21 #include "php_hash_gost.h"
22 #include "php_hash_gost_tables.h"
23
24 /* {{{ Gost()
25 * derived from gost_compress() by Markku-Juhani Saarinen <mjos@ssh.fi>
26 */
27
28 #define round(tables, k1, k2) \
29 t = (k1) + r; \
30 l ^= tables[0][t & 0xff] ^ tables[1][(t >> 8) & 0xff] ^ \
31 tables[2][(t >> 16) & 0xff] ^ tables[3][t >> 24]; \
32 t = (k2) + l; \
33 r ^= tables[0][t & 0xff] ^ tables[1][(t >> 8) & 0xff] ^ \
34 tables[2][(t >> 16) & 0xff] ^ tables[3][t >> 24];
35
36 #define R(tables, key, h, i, t, l, r) \
37 r = h[i]; \
38 l = h[i + 1]; \
39 round(tables, key[0], key[1]) \
40 round(tables, key[2], key[3]) \
41 round(tables, key[4], key[5]) \
42 round(tables, key[6], key[7]) \
43 round(tables, key[0], key[1]) \
44 round(tables, key[2], key[3]) \
45 round(tables, key[4], key[5]) \
46 round(tables, key[6], key[7]) \
47 round(tables, key[0], key[1]) \
48 round(tables, key[2], key[3]) \
49 round(tables, key[4], key[5]) \
50 round(tables, key[6], key[7]) \
51 round(tables, key[7], key[6]) \
52 round(tables, key[5], key[4]) \
53 round(tables, key[3], key[2]) \
54 round(tables, key[1], key[0]) \
55 t = r; \
56 r = l; \
57 l = t; \
58
59 #define X(w, u, v) \
60 w[0] = u[0] ^ v[0]; \
61 w[1] = u[1] ^ v[1]; \
62 w[2] = u[2] ^ v[2]; \
63 w[3] = u[3] ^ v[3]; \
64 w[4] = u[4] ^ v[4]; \
65 w[5] = u[5] ^ v[5]; \
66 w[6] = u[6] ^ v[6]; \
67 w[7] = u[7] ^ v[7];
68
69 #define P(key, w) \
70 key[0] = (w[0] & 0x000000ff) | ((w[2] & 0x000000ff) << 8) | \
71 ((w[4] & 0x000000ff) << 16) | ((w[6] & 0x000000ff) << 24); \
72 key[1] = ((w[0] & 0x0000ff00) >> 8) | (w[2] & 0x0000ff00) | \
73 ((w[4] & 0x0000ff00) << 8) | ((w[6] & 0x0000ff00) << 16); \
74 key[2] = ((w[0] & 0x00ff0000) >> 16) | ((w[2] & 0x00ff0000) >> 8) | \
75 (w[4] & 0x00ff0000) | ((w[6] & 0x00ff0000) << 8); \
76 key[3] = ((w[0] & 0xff000000) >> 24) | ((w[2] & 0xff000000) >> 16) | \
77 ((w[4] & 0xff000000) >> 8) | (w[6] & 0xff000000); \
78 key[4] = (w[1] & 0x000000ff) | ((w[3] & 0x000000ff) << 8) | \
79 ((w[5] & 0x000000ff) << 16) | ((w[7] & 0x000000ff) << 24); \
80 key[5] = ((w[1] & 0x0000ff00) >> 8) | (w[3] & 0x0000ff00) | \
81 ((w[5] & 0x0000ff00) << 8) | ((w[7] & 0x0000ff00) << 16); \
82 key[6] = ((w[1] & 0x00ff0000) >> 16) | ((w[3] & 0x00ff0000) >> 8) | \
83 (w[5] & 0x00ff0000) | ((w[7] & 0x00ff0000) << 8); \
84 key[7] = ((w[1] & 0xff000000) >> 24) | ((w[3] & 0xff000000) >> 16) | \
85 ((w[5] & 0xff000000) >> 8) | (w[7] & 0xff000000);
86
87 #define A(x, l, r) \
88 l = x[0] ^ x[2]; \
89 r = x[1] ^ x[3]; \
90 x[0] = x[2]; \
91 x[1] = x[3]; \
92 x[2] = x[4]; \
93 x[3] = x[5]; \
94 x[4] = x[6]; \
95 x[5] = x[7]; \
96 x[6] = l; \
97 x[7] = r;
98
99 #define AA(x, l, r) \
100 l = x[0]; \
101 r = x[2]; \
102 x[0] = x[4]; \
103 x[2] = x[6]; \
104 x[4] = l ^ r; \
105 x[6] = x[0] ^ r; \
106 l = x[1]; \
107 r = x[3]; \
108 x[1] = x[5]; \
109 x[3] = x[7]; \
110 x[5] = l ^ r; \
111 x[7] = x[1] ^ r;
112
113 #define C(x) \
114 x[0] ^= 0xff00ff00; \
115 x[1] ^= 0xff00ff00; \
116 x[2] ^= 0x00ff00ff; \
117 x[3] ^= 0x00ff00ff; \
118 x[4] ^= 0x00ffff00; \
119 x[5] ^= 0xff0000ff; \
120 x[6] ^= 0x000000ff; \
121 x[7] ^= 0xff00ffff;
122
123 #define S(s, l, r) \
124 s[i] = r; \
125 s[i + 1] = l;
126
127 #define SHIFT12(u, m, s) \
128 u[0] = m[0] ^ s[6]; \
129 u[1] = m[1] ^ s[7]; \
130 u[2] = m[2] ^ (s[0] << 16) ^ (s[0] >> 16) ^ (s[0] & 0xffff) ^ \
131 (s[1] & 0xffff) ^ (s[1] >> 16) ^ (s[2] << 16) ^ s[6] ^ (s[6] << 16) ^ \
132 (s[7] & 0xffff0000) ^ (s[7] >> 16); \
133 u[3] = m[3] ^ (s[0] & 0xffff) ^ (s[0] << 16) ^ (s[1] & 0xffff) ^ \
134 (s[1] << 16) ^ (s[1] >> 16) ^ (s[2] << 16) ^ (s[2] >> 16) ^ \
135 (s[3] << 16) ^ s[6] ^ (s[6] << 16) ^ (s[6] >> 16) ^ (s[7] & 0xffff) ^ \
136 (s[7] << 16) ^ (s[7] >> 16); \
137 u[4] = m[4] ^ \
138 (s[0] & 0xffff0000) ^ (s[0] << 16) ^ (s[0] >> 16) ^ \
139 (s[1] & 0xffff0000) ^ (s[1] >> 16) ^ (s[2] << 16) ^ (s[2] >> 16) ^ \
140 (s[3] << 16) ^ (s[3] >> 16) ^ (s[4] << 16) ^ (s[6] << 16) ^ \
141 (s[6] >> 16) ^(s[7] & 0xffff) ^ (s[7] << 16) ^ (s[7] >> 16); \
142 u[5] = m[5] ^ (s[0] << 16) ^ (s[0] >> 16) ^ (s[0] & 0xffff0000) ^ \
143 (s[1] & 0xffff) ^ s[2] ^ (s[2] >> 16) ^ (s[3] << 16) ^ (s[3] >> 16) ^ \
144 (s[4] << 16) ^ (s[4] >> 16) ^ (s[5] << 16) ^ (s[6] << 16) ^ \
145 (s[6] >> 16) ^ (s[7] & 0xffff0000) ^ (s[7] << 16) ^ (s[7] >> 16); \
146 u[6] = m[6] ^ s[0] ^ (s[1] >> 16) ^ (s[2] << 16) ^ s[3] ^ (s[3] >> 16) ^ \
147 (s[4] << 16) ^ (s[4] >> 16) ^ (s[5] << 16) ^ (s[5] >> 16) ^ s[6] ^ \
148 (s[6] << 16) ^ (s[6] >> 16) ^ (s[7] << 16); \
149 u[7] = m[7] ^ (s[0] & 0xffff0000) ^ (s[0] << 16) ^ (s[1] & 0xffff) ^ \
150 (s[1] << 16) ^ (s[2] >> 16) ^ (s[3] << 16) ^ s[4] ^ (s[4] >> 16) ^ \
151 (s[5] << 16) ^ (s[5] >> 16) ^ (s[6] >> 16) ^ (s[7] & 0xffff) ^ \
152 (s[7] << 16) ^ (s[7] >> 16);
153
154 #define SHIFT16(h, v, u) \
155 v[0] = h[0] ^ (u[1] << 16) ^ (u[0] >> 16); \
156 v[1] = h[1] ^ (u[2] << 16) ^ (u[1] >> 16); \
157 v[2] = h[2] ^ (u[3] << 16) ^ (u[2] >> 16); \
158 v[3] = h[3] ^ (u[4] << 16) ^ (u[3] >> 16); \
159 v[4] = h[4] ^ (u[5] << 16) ^ (u[4] >> 16); \
160 v[5] = h[5] ^ (u[6] << 16) ^ (u[5] >> 16); \
161 v[6] = h[6] ^ (u[7] << 16) ^ (u[6] >> 16); \
162 v[7] = h[7] ^ (u[0] & 0xffff0000) ^ (u[0] << 16) ^ (u[7] >> 16) ^ \
163 (u[1] & 0xffff0000) ^ (u[1] << 16) ^ (u[6] << 16) ^ (u[7] & 0xffff0000);
164
165 #define SHIFT61(h, v) \
166 h[0] = (v[0] & 0xffff0000) ^ (v[0] << 16) ^ (v[0] >> 16) ^ (v[1] >> 16) ^ \
167 (v[1] & 0xffff0000) ^ (v[2] << 16) ^ (v[3] >> 16) ^ (v[4] << 16) ^ \
168 (v[5] >> 16) ^ v[5] ^ (v[6] >> 16) ^ (v[7] << 16) ^ (v[7] >> 16) ^ \
169 (v[7] & 0xffff); \
170 h[1] = (v[0] << 16) ^ (v[0] >> 16) ^ (v[0] & 0xffff0000) ^ (v[1] & 0xffff) ^ \
171 v[2] ^ (v[2] >> 16) ^ (v[3] << 16) ^ (v[4] >> 16) ^ (v[5] << 16) ^ \
172 (v[6] << 16) ^ v[6] ^ (v[7] & 0xffff0000) ^ (v[7] >> 16); \
173 h[2] = (v[0] & 0xffff) ^ (v[0] << 16) ^ (v[1] << 16) ^ (v[1] >> 16) ^ \
174 (v[1] & 0xffff0000) ^ (v[2] << 16) ^ (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ \
175 (v[5] >> 16) ^ v[6] ^ (v[6] >> 16) ^ (v[7] & 0xffff) ^ (v[7] << 16) ^ \
176 (v[7] >> 16); \
177 h[3] = (v[0] << 16) ^ (v[0] >> 16) ^ (v[0] & 0xffff0000) ^ \
178 (v[1] & 0xffff0000) ^ (v[1] >> 16) ^ (v[2] << 16) ^ (v[2] >> 16) ^ v[2] ^ \
179 (v[3] << 16) ^ (v[4] >> 16) ^ v[4] ^ (v[5] << 16) ^ (v[6] << 16) ^ \
180 (v[7] & 0xffff) ^ (v[7] >> 16); \
181 h[4] = (v[0] >> 16) ^ (v[1] << 16) ^ v[1] ^ (v[2] >> 16) ^ v[2] ^ \
182 (v[3] << 16) ^ (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ (v[5] >> 16) ^ \
183 v[5] ^ (v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16); \
184 h[5] = (v[0] << 16) ^ (v[0] & 0xffff0000) ^ (v[1] << 16) ^ (v[1] >> 16) ^ \
185 (v[1] & 0xffff0000) ^ (v[2] << 16) ^ v[2] ^ (v[3] >> 16) ^ v[3] ^ \
186 (v[4] << 16) ^ (v[4] >> 16) ^ v[4] ^ (v[5] << 16) ^ (v[6] << 16) ^ \
187 (v[6] >> 16) ^ v[6] ^ (v[7] << 16) ^ (v[7] >> 16) ^ (v[7] & 0xffff0000); \
188 h[6] = v[0] ^ v[2] ^ (v[2] >> 16) ^ v[3] ^ (v[3] << 16) ^ v[4] ^ \
189 (v[4] >> 16) ^ (v[5] << 16) ^ (v[5] >> 16) ^ v[5] ^ (v[6] << 16) ^ \
190 (v[6] >> 16) ^ v[6] ^ (v[7] << 16) ^ v[7]; \
191 h[7] = v[0] ^ (v[0] >> 16) ^ (v[1] << 16) ^ (v[1] >> 16) ^ (v[2] << 16) ^ \
192 (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ v[4] ^ (v[5] >> 16) ^ v[5] ^ \
193 (v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16) ^ v[7];
194
195 #define PASS(tables) \
196 X(w, u, v); \
197 P(key, w); \
198 R((tables), key, h, i, t, l, r); \
199 S(s, l, r); \
200 if (i != 6) { \
201 A(u, l, r); \
202 if (i == 2) { \
203 C(u); \
204 } \
205 AA(v, l, r); \
206 }
207
Gost(PHP_GOST_CTX * context,uint32_t data[8])208 static inline void Gost(PHP_GOST_CTX *context, uint32_t data[8])
209 {
210 int i;
211 uint32_t l, r, t, key[8], u[8], v[8], w[8], s[8], *h = context->state, *m = data;
212
213 memcpy(u, context->state, sizeof(u));
214 memcpy(v, data, sizeof(v));
215
216 for (i = 0; i < 8; i += 2) {
217 PASS(*context->tables);
218 }
219 SHIFT12(u, m, s);
220 SHIFT16(h, v, u);
221 SHIFT61(h, v);
222 }
223 /* }}} */
224
GostTransform(PHP_GOST_CTX * context,const unsigned char input[32])225 static inline void GostTransform(PHP_GOST_CTX *context, const unsigned char input[32])
226 {
227 int i, j;
228 uint32_t data[8], temp = 0;
229
230 for (i = 0, j = 0; i < 8; ++i, j += 4) {
231 data[i] = ((uint32_t) input[j]) | (((uint32_t) input[j + 1]) << 8) |
232 (((uint32_t) input[j + 2]) << 16) | (((uint32_t) input[j + 3]) << 24);
233 context->state[i + 8] += data[i] + temp;
234 temp = context->state[i + 8] < data[i] ? 1 : (context->state[i + 8] == data[i] ? temp : 0);
235 }
236
237 Gost(context, data);
238 }
239
PHP_GOSTInit(PHP_GOST_CTX * context)240 PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *context)
241 {
242 memset(context, 0, sizeof(*context));
243 context->tables = &tables_test;
244 }
245
PHP_GOSTInitCrypto(PHP_GOST_CTX * context)246 PHP_HASH_API void PHP_GOSTInitCrypto(PHP_GOST_CTX *context)
247 {
248 PHP_GOSTInit(context);
249 context->tables = &tables_crypto;
250 }
251
252 static const uint32_t MAX32 = 0xffffffffLU;
253
PHP_GOSTUpdate(PHP_GOST_CTX * context,const unsigned char * input,size_t len)254 PHP_HASH_API void PHP_GOSTUpdate(PHP_GOST_CTX *context, const unsigned char *input, size_t len)
255 {
256 if ((MAX32 - context->count[0]) < (len * 8)) {
257 context->count[1]++;
258 context->count[0] = MAX32 - context->count[0];
259 context->count[0] = (len * 8) - context->count[0];
260 } else {
261 context->count[0] += len * 8;
262 }
263
264 if (context->length + len < 32) {
265 memcpy(&context->buffer[context->length], input, len);
266 context->length += (unsigned char)len;
267 } else {
268 size_t i = 0, r = (context->length + len) % 32;
269
270 if (context->length) {
271 i = 32 - context->length;
272 memcpy(&context->buffer[context->length], input, i);
273 GostTransform(context, context->buffer);
274 }
275
276 for (; i + 32 <= len; i += 32) {
277 GostTransform(context, input + i);
278 }
279
280 memcpy(context->buffer, input + i, r);
281 ZEND_SECURE_ZERO(&context->buffer[r], 32 - r);
282 context->length = (unsigned char)r;
283 }
284 }
285
PHP_GOSTFinal(unsigned char digest[32],PHP_GOST_CTX * context)286 PHP_HASH_API void PHP_GOSTFinal(unsigned char digest[32], PHP_GOST_CTX *context)
287 {
288 uint32_t i, j, l[8] = {0};
289
290 if (context->length) {
291 GostTransform(context, context->buffer);
292 }
293
294 l[0] = context->count[0];
295 l[1] = context->count[1];
296 Gost(context, l);
297 memcpy(l, &context->state[8], sizeof(l));
298 Gost(context, l);
299
300 for (i = 0, j = 0; j < 32; i++, j += 4) {
301 digest[j] = (unsigned char) (context->state[i] & 0xff);
302 digest[j + 1] = (unsigned char) ((context->state[i] >> 8) & 0xff);
303 digest[j + 2] = (unsigned char) ((context->state[i] >> 16) & 0xff);
304 digest[j + 3] = (unsigned char) ((context->state[i] >> 24) & 0xff);
305 }
306
307 ZEND_SECURE_ZERO(context, sizeof(*context));
308 }
309
310 const php_hash_ops php_hash_gost_ops = {
311 (php_hash_init_func_t) PHP_GOSTInit,
312 (php_hash_update_func_t) PHP_GOSTUpdate,
313 (php_hash_final_func_t) PHP_GOSTFinal,
314 (php_hash_copy_func_t) php_hash_copy,
315 32,
316 32,
317 sizeof(PHP_GOST_CTX),
318 1
319 };
320
321 const php_hash_ops php_hash_gost_crypto_ops = {
322 (php_hash_init_func_t) PHP_GOSTInitCrypto,
323 (php_hash_update_func_t) PHP_GOSTUpdate,
324 (php_hash_final_func_t) PHP_GOSTFinal,
325 (php_hash_copy_func_t) php_hash_copy,
326 32,
327 32,
328 sizeof(PHP_GOST_CTX),
329 1
330 };
331