Skip to content

Commit 05f847d

Browse files
committed
Merge PR #430 gost-crypto
Merge branch 'master' of github.com:manuelm/php-src * 'master' of github.com:manuelm/php-src: fix failing tests fix indention Add support for CryptoPro S-box for GOST
2 parents c7b1d76 + 7dbb1bb commit 05f847d

File tree

8 files changed

+210
-30
lines changed

8 files changed

+210
-30
lines changed

ext/hash/hash.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ PHP_MINIT_FUNCTION(hash)
986986
php_hash_register_algo("snefru", &php_hash_snefru_ops);
987987
php_hash_register_algo("snefru256", &php_hash_snefru_ops);
988988
php_hash_register_algo("gost", &php_hash_gost_ops);
989+
php_hash_register_algo("gost-crypto", &php_hash_gost_crypto_ops);
989990
php_hash_register_algo("adler32", &php_hash_adler32_ops);
990991
php_hash_register_algo("crc32", &php_hash_crc32_ops);
991992
php_hash_register_algo("crc32b", &php_hash_crc32b_ops);

ext/hash/hash_gost.c

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,33 @@
2727
* derived from gost_compress() by Markku-Juhani Saarinen <[email protected]>
2828
*/
2929

30-
#define round(k1, k2) \
30+
#define round(tables, k1, k2) \
3131
t = (k1) + r; \
3232
l ^= tables[0][t & 0xff] ^ tables[1][(t >> 8) & 0xff] ^ \
3333
tables[2][(t >> 16) & 0xff] ^ tables[3][t >> 24]; \
3434
t = (k2) + l; \
3535
r ^= tables[0][t & 0xff] ^ tables[1][(t >> 8) & 0xff] ^ \
3636
tables[2][(t >> 16) & 0xff] ^ tables[3][t >> 24];
3737

38-
#define R(key, h, i, t, l, r) \
38+
#define R(tables, key, h, i, t, l, r) \
3939
r = h[i]; \
4040
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]) \
5757
t = r; \
5858
r = l; \
5959
l = t; \
@@ -194,10 +194,10 @@
194194
(v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ v[4] ^ (v[5] >> 16) ^ v[5] ^ \
195195
(v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16) ^ v[7];
196196

197-
#define PASS \
197+
#define PASS(tables) \
198198
X(w, u, v); \
199199
P(key, w); \
200-
R(key, h, i, t, l, r); \
200+
R((tables), key, h, i, t, l, r); \
201201
S(s, l, r); \
202202
if (i != 6) { \
203203
A(u, l, r); \
@@ -207,16 +207,16 @@
207207
AA(v, l, r); \
208208
}
209209

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])
211211
{
212212
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;
214214

215-
memcpy(u, state, sizeof(u));
215+
memcpy(u, context->state, sizeof(u));
216216
memcpy(v, data, sizeof(v));
217217

218218
for (i = 0; i < 8; i += 2) {
219-
PASS;
219+
PASS(*context->tables);
220220
}
221221
SHIFT12(u, m, s);
222222
SHIFT16(h, v, u);
@@ -237,12 +237,19 @@ static inline void GostTransform(PHP_GOST_CTX *context, const unsigned char inpu
237237
temp = ((context->state[i + 8] < data[i]) || (context->state[i + 8] < save)) ? 1 : 0;
238238
}
239239

240-
Gost(context->state, data);
240+
Gost(context, data);
241241
}
242242

243243
PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *context)
244244
{
245245
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;
246253
}
247254

248255
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)
288295
}
289296

290297
memcpy(l, context->count, sizeof(context->count));
291-
Gost(context->state, l);
298+
Gost(context, l);
292299
memcpy(l, &context->state[8], sizeof(l));
293-
Gost(context->state, l);
300+
Gost(context, l);
294301

295302
for (i = 0, j = 0; j < 32; i++, j += 4) {
296303
digest[j] = (unsigned char) (context->state[i] & 0xff);
@@ -312,6 +319,16 @@ const php_hash_ops php_hash_gost_ops = {
312319
sizeof(PHP_GOST_CTX)
313320
};
314321

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+
315332
/*
316333
* Local variables:
317334
* tab-width: 4

ext/hash/php_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern const php_hash_ops php_hash_4tiger160_ops;
8080
extern const php_hash_ops php_hash_4tiger192_ops;
8181
extern const php_hash_ops php_hash_snefru_ops;
8282
extern const php_hash_ops php_hash_gost_ops;
83+
extern const php_hash_ops php_hash_gost_crypto_ops;
8384
extern const php_hash_ops php_hash_adler32_ops;
8485
extern const php_hash_ops php_hash_crc32_ops;
8586
extern const php_hash_ops php_hash_crc32b_ops;

ext/hash/php_hash_gost.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef struct {
2929
php_hash_uint32 count[2];
3030
unsigned char length;
3131
unsigned char buffer[32];
32+
const php_hash_uint32 (*tables)[4][256];
3233
} PHP_GOST_CTX;
3334

3435
PHP_HASH_API void PHP_GOSTInit(PHP_GOST_CTX *);

0 commit comments

Comments
 (0)