27
27
* derived from gost_compress() by Markku-Juhani Saarinen <[email protected] >
28
28
*/
29
29
30
- #define round (k1 , k2 ) \
30
+ #define round (tables , k1 , k2 ) \
31
31
t = (k1) + r; \
32
32
l ^= tables[0][t & 0xff] ^ tables[1][(t >> 8) & 0xff] ^ \
33
33
tables[2][(t >> 16) & 0xff] ^ tables[3][t >> 24]; \
34
34
t = (k2) + l; \
35
35
r ^= tables[0][t & 0xff] ^ tables[1][(t >> 8) & 0xff] ^ \
36
36
tables[2][(t >> 16) & 0xff] ^ tables[3][t >> 24];
37
37
38
- #define R (key , h , i , t , l , r ) \
38
+ #define R (tables , key , h , i , t , l , r ) \
39
39
r = h[i]; \
40
40
l = h[i + 1]; \
41
- round(key[0], key[1]) \
42
- round(key[2], key[3]) \
43
- round(key[4], key[5]) \
44
- round(key[6], key[7]) \
45
- round(key[0], key[1]) \
46
- round(key[2], key[3]) \
47
- round(key[4], key[5]) \
48
- round(key[6], key[7]) \
49
- round(key[0], key[1]) \
50
- round(key[2], key[3]) \
51
- round(key[4], key[5]) \
52
- round(key[6], key[7]) \
53
- round(key[7], key[6]) \
54
- round(key[5], key[4]) \
55
- round(key[3], key[2]) \
56
- round(key[1], key[0]) \
41
+ round(tables, key[0], key[1]) \
42
+ round(tables, key[2], key[3]) \
43
+ round(tables, key[4], key[5]) \
44
+ round(tables, key[6], key[7]) \
45
+ round(tables, key[0], key[1]) \
46
+ round(tables, key[2], key[3]) \
47
+ round(tables, key[4], key[5]) \
48
+ round(tables, key[6], key[7]) \
49
+ round(tables, key[0], key[1]) \
50
+ round(tables, key[2], key[3]) \
51
+ round(tables, key[4], key[5]) \
52
+ round(tables, key[6], key[7]) \
53
+ round(tables, key[7], key[6]) \
54
+ round(tables, key[5], key[4]) \
55
+ round(tables, key[3], key[2]) \
56
+ round(tables, key[1], key[0]) \
57
57
t = r; \
58
58
r = l; \
59
59
l = t; \
194
194
(v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ v[4] ^ (v[5] >> 16) ^ v[5] ^ \
195
195
(v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16) ^ v[7];
196
196
197
- #define PASS \
197
+ #define PASS ( tables ) \
198
198
X(w, u, v); \
199
199
P(key, w); \
200
- R(key, h, i, t, l, r); \
200
+ R((tables), key, h, i, t, l, r); \
201
201
S(s, l, r); \
202
202
if (i != 6) { \
203
203
A(u, l, r); \
207
207
AA(v, l, r); \
208
208
}
209
209
210
- static inline void Gost (php_hash_uint32 state [ 8 ] , php_hash_uint32 data [8 ])
210
+ static inline void Gost (PHP_GOST_CTX * context , php_hash_uint32 data [8 ])
211
211
{
212
212
int i ;
213
- php_hash_uint32 l , r , t , key [8 ], u [8 ], v [8 ], w [8 ], s [8 ], * h = state , * m = data ;
213
+ php_hash_uint32 l , r , t , key [8 ], u [8 ], v [8 ], w [8 ], s [8 ], * h = context -> state , * m = data ;
214
214
215
- memcpy (u , state , sizeof (u ));
215
+ memcpy (u , context -> state , sizeof (u ));
216
216
memcpy (v , data , sizeof (v ));
217
217
218
218
for (i = 0 ; i < 8 ; i += 2 ) {
219
- PASS ;
219
+ PASS ( * context -> tables ) ;
220
220
}
221
221
SHIFT12 (u , m , s );
222
222
SHIFT16 (h , v , u );
@@ -237,12 +237,19 @@ static inline void GostTransform(PHP_GOST_CTX *context, const unsigned char inpu
237
237
temp = ((context -> state [i + 8 ] < data [i ]) || (context -> state [i + 8 ] < save )) ? 1 : 0 ;
238
238
}
239
239
240
- Gost (context -> state , data );
240
+ Gost (context , data );
241
241
}
242
242
243
243
PHP_HASH_API void PHP_GOSTInit (PHP_GOST_CTX * context )
244
244
{
245
245
memset (context , 0 , sizeof (* context ));
246
+ context -> tables = & tables_test ;
247
+ }
248
+
249
+ PHP_HASH_API void PHP_GOSTInitCrypto (PHP_GOST_CTX * context )
250
+ {
251
+ PHP_GOSTInit (context );
252
+ context -> tables = & tables_crypto ;
246
253
}
247
254
248
255
static const php_hash_uint32 MAX32 = 0xffffffffLU ;
@@ -288,9 +295,9 @@ PHP_HASH_API void PHP_GOSTFinal(unsigned char digest[32], PHP_GOST_CTX *context)
288
295
}
289
296
290
297
memcpy (l , context -> count , sizeof (context -> count ));
291
- Gost (context -> state , l );
298
+ Gost (context , l );
292
299
memcpy (l , & context -> state [8 ], sizeof (l ));
293
- Gost (context -> state , l );
300
+ Gost (context , l );
294
301
295
302
for (i = 0 , j = 0 ; j < 32 ; i ++ , j += 4 ) {
296
303
digest [j ] = (unsigned char ) (context -> state [i ] & 0xff );
@@ -312,6 +319,16 @@ const php_hash_ops php_hash_gost_ops = {
312
319
sizeof (PHP_GOST_CTX )
313
320
};
314
321
322
+ const php_hash_ops php_hash_gost_crypto_ops = {
323
+ (php_hash_init_func_t ) PHP_GOSTInitCrypto ,
324
+ (php_hash_update_func_t ) PHP_GOSTUpdate ,
325
+ (php_hash_final_func_t ) PHP_GOSTFinal ,
326
+ (php_hash_copy_func_t ) php_hash_copy ,
327
+ 32 ,
328
+ 32 ,
329
+ sizeof (PHP_GOST_CTX )
330
+ };
331
+
315
332
/*
316
333
* Local variables:
317
334
* tab-width: 4
0 commit comments