-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkupyna.c
361 lines (325 loc) · 7.78 KB
/
kupyna.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdint.h>
#include <main/php.h>
#include <Zend/zend.h>
#include <ext/hash/php_hash.h>
#include <ext/standard/info.h>
#include <ext/standard/md5.h>
#include "php_kupyna.h"
#include "kupyna256.h"
#include "kupyna512.h"
#include "kupyna_kmac.h"
#if PHP_VERSION_ID >= 80000
#define TSRMLS_CC
#endif
static void hash_kupyna256_init(
void* context
#if PHP_VERSION_ID >= 80100
, ZEND_ATTRIBUTE_UNUSED HashTable* args
#endif
)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna256_ctx_t* ctx = (struct kupyna256_ctx_t*)((char*)context + offset);
kupyna256_init(ctx);
}
static void hash_kupyna384_init(
void* context
#if PHP_VERSION_ID >= 80100
, ZEND_ATTRIBUTE_UNUSED HashTable* args
#endif
)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna512_ctx_t* ctx = (struct kupyna512_ctx_t*)((char*)context + offset);
kupyna384_init(ctx);
}
static void hash_kupyna512_init(
void* context
#if PHP_VERSION_ID >= 80100
, ZEND_ATTRIBUTE_UNUSED HashTable* args
#endif
)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna512_ctx_t* ctx = (struct kupyna512_ctx_t*)((char*)context + offset);
kupyna512_init(ctx);
}
static void hash_kupyna256_update(
void* context,
const unsigned char* buf,
#if PHP_VERSION_ID >= 70400
size_t count
#else
unsigned int count
#endif
)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna256_ctx_t* ctx = (struct kupyna256_ctx_t*)((char*)context + offset);
kupyna256_update(ctx, buf, count);
}
static void hash_kupyna384_update(
void* context,
const unsigned char* buf,
#if PHP_VERSION_ID >= 70400
size_t count
#else
unsigned int count
#endif
)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna512_ctx_t* ctx = (struct kupyna512_ctx_t*)((char*)context + offset);
kupyna384_update(ctx, buf, count);
}
static void hash_kupyna512_update(
void* context,
const unsigned char* buf,
#if PHP_VERSION_ID >= 70400
size_t count
#else
unsigned int count
#endif
)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna512_ctx_t* ctx = (struct kupyna512_ctx_t*)((char*)context + offset);
kupyna512_update(ctx, buf, count);
}
static void hash_kupyna256_final(unsigned char* digest, void* context)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna256_ctx_t* ctx = (struct kupyna256_ctx_t*)((char*)context + offset);
kupyna256_final(ctx, digest);
}
static void hash_kupyna384_final(unsigned char* digest, void* context)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna512_ctx_t* ctx = (struct kupyna512_ctx_t*)((char*)context + offset);
kupyna384_final(ctx, digest);
}
static void hash_kupyna512_final(unsigned char* digest, void* context)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
struct kupyna512_ctx_t* ctx = (struct kupyna512_ctx_t*)((char*)context + offset);
kupyna512_final(ctx, digest);
}
const php_hash_ops kupyna256_hash_ops = {
#if PHP_VERSION_ID >= 80000
"kupyna256",
#endif
hash_kupyna256_init,
hash_kupyna256_update,
hash_kupyna256_final,
#if PHP_VERSION_ID >= 50300
(php_hash_copy_func_t)php_hash_copy,
#endif
#if PHP_VERSION_ID >= 80000
NULL,
NULL,
NULL,
#endif
32,
64,
(sizeof(struct kupyna256_ctx_t) + 15)
#if PHP_VERSION_ID >= 70400
, 1
#endif
};
const php_hash_ops kupyna384_hash_ops = {
#if PHP_VERSION_ID >= 80000
"kupyna384",
#endif
hash_kupyna384_init,
hash_kupyna384_update,
hash_kupyna384_final,
#if PHP_VERSION_ID >= 50300
(php_hash_copy_func_t)php_hash_copy,
#endif
#if PHP_VERSION_ID >= 80000
NULL,
NULL,
NULL,
#endif
48,
128,
(sizeof(struct kupyna512_ctx_t) + 15)
#if PHP_VERSION_ID >= 70400
, 1
#endif
};
const php_hash_ops kupyna512_hash_ops = {
#if PHP_VERSION_ID >= 80000
"kupyna512",
#endif
hash_kupyna512_init,
hash_kupyna512_update,
hash_kupyna512_final,
#if PHP_VERSION_ID >= 50300
(php_hash_copy_func_t)php_hash_copy,
#endif
#if PHP_VERSION_ID >= 80000
NULL,
NULL,
NULL,
#endif
64,
128,
(sizeof(struct kupyna512_ctx_t) + 15)
#if PHP_VERSION_ID >= 70400
, 1
#endif
};
static PHP_MINIT_FUNCTION(kupyna)
{
php_hash_register_algo("kupyna256", &kupyna256_hash_ops);
php_hash_register_algo("kupyna384", &kupyna384_hash_ops);
php_hash_register_algo("kupyna512", &kupyna512_hash_ops);
return SUCCESS;
}
static PHP_MINFO_FUNCTION(kupyna)
{
php_info_print_table_start();
php_info_print_table_row(2, "DSTU 7564:2014 hash functions", "enabled");
php_info_print_table_row(2, "Version", PHP_KUPYNA_EXTVER);
php_info_print_table_end();
}
static PHP_FUNCTION(kupyna256_kmac)
{
char* key;
char* data;
#if PHP_MAJOR_VERSION < 7
int key_len;
int data_len;
#else
size_t key_len;
size_t data_len;
#endif
uint8_t mac[32];
char result[65];
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, &data, &data_len)) {
RETURN_NULL();
}
if (key_len < 32) {
struct kupyna256_ctx_t ctx;
kupyna256_init(&ctx);
kupyna256_update(&ctx, (uint8_t*)key, key_len);
kupyna256_final(&ctx, mac);
kupyna256_kmac(mac, (const uint8_t*)data, (size_t)data_len, mac);
}
else {
kupyna256_kmac((const uint8_t*)key, (const uint8_t*)data, (size_t)data_len, mac);
}
make_digest_ex(result, mac, 32);
#if PHP_MAJOR_VERSION < 7
RETURN_STRINGL(result, 64, 1);
#else
RETURN_STRINGL(result, 64);
#endif
}
static PHP_FUNCTION(kupyna384_kmac)
{
char* key;
char* data;
#if PHP_MAJOR_VERSION < 7
int key_len;
int data_len;
#else
size_t key_len;
size_t data_len;
#endif
uint8_t mac[48];
char result[97];
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, &data, &data_len)) {
RETURN_NULL();
}
if (key_len < 48) {
struct kupyna512_ctx_t ctx;
kupyna384_init(&ctx);
kupyna384_update(&ctx, (uint8_t*)key, key_len);
kupyna384_final(&ctx, mac);
kupyna384_kmac(mac, (const uint8_t*)data, (size_t)data_len, mac);
}
else {
kupyna384_kmac((const uint8_t*)key, (const uint8_t*)data, (size_t)data_len, mac);
}
make_digest_ex(result, mac, 48);
#if PHP_MAJOR_VERSION < 7
RETURN_STRINGL(result, 96, 1);
#else
RETURN_STRINGL(result, 96);
#endif
}
static PHP_FUNCTION(kupyna512_kmac)
{
char* key;
char* data;
#if PHP_MAJOR_VERSION < 7
int key_len;
int data_len;
#else
size_t key_len;
size_t data_len;
#endif
uint8_t mac[64];
char result[129];
if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &key, &key_len, &data, &data_len)) {
RETURN_NULL();
}
if (key_len < 64) {
struct kupyna512_ctx_t ctx;
kupyna512_init(&ctx);
kupyna512_update(&ctx, (uint8_t*)key, key_len);
kupyna512_final(&ctx, mac);
kupyna512_kmac(mac, (const uint8_t*)data, (size_t)data_len, mac);
}
else {
kupyna512_kmac((const uint8_t*)key, (const uint8_t*)data, (size_t)data_len, mac);
}
make_digest_ex(result, mac, 64);
#if PHP_MAJOR_VERSION < 7
RETURN_STRINGL(result, 128, 1);
#else
RETURN_STRINGL(result, 128);
#endif
}
ZEND_BEGIN_ARG_INFO(arginfo_kmac, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, data)
ZEND_END_ARG_INFO()
static const zend_function_entry kupyna_functions[] = {
PHP_FE(kupyna256_kmac, arginfo_kmac)
PHP_FE(kupyna384_kmac, arginfo_kmac)
PHP_FE(kupyna512_kmac, arginfo_kmac)
PHP_FE_END
};
static
#if ZEND_MODULE_API_NO > 20060613
const
#endif
zend_module_dep kupyna_deps[] = {
ZEND_MOD_REQUIRED("hash")
ZEND_MOD_END
};
zend_module_entry kupyna_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
kupyna_deps,
PHP_KUPYNA_EXTNAME,
kupyna_functions,
PHP_MINIT(kupyna),
NULL,
NULL,
NULL,
PHP_MINFO(kupyna),
PHP_KUPYNA_EXTVER,
NO_MODULE_GLOBALS,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_KUPYNA
ZEND_GET_MODULE(kupyna)
#endif