-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
stribog.c
165 lines (152 loc) · 3.23 KB
/
stribog.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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <main/php.h>
#include <Zend/zend.h>
#include <ext/hash/php_hash.h>
#include <ext/standard/info.h>
#include "php_stribog.h"
#include "gost3411-2012.h"
static void stribog256_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;
void *ctx = (char*)context + offset;
GOST34112012Init(ctx, 256);
}
static void stribog512_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;
void *ctx = (char*)context + offset;
GOST34112012Init(ctx, 512);
}
static void stribog_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;
void *ctx = (char*)context + offset;
offset = (((size_t)buf + 15) & ~0x0F) - (size_t)buf;
if (!offset) {
GOST34112012Update(ctx, buf, count);
}
else {
ALIGN(16) unsigned char tmp[15];
assert(offset < 16);
if (count > offset) {
memcpy(tmp, buf, offset);
GOST34112012Update(ctx, tmp, offset);
GOST34112012Update(ctx, buf + offset, count - offset);
}
else {
memcpy(tmp, buf, count);
GOST34112012Update(ctx, tmp, count);
}
}
}
static void stribog_final(unsigned char* digest, void* context)
{
size_t offset = (((size_t)context + 15) & ~0x0F) - (size_t)context;
void *ctx = (char*)context + offset;
GOST34112012Final(ctx, digest);
}
const php_hash_ops stribog256_hash_ops = {
#if PHP_VERSION_ID >= 80000
"stribog256",
#endif
stribog256_init,
stribog_update,
stribog_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(GOST34112012Context) + 15)
#if PHP_VERSION_ID >= 70400
, 1
#endif
};
const php_hash_ops stribog512_hash_ops = {
#if PHP_VERSION_ID >= 80000
"stribog512",
#endif
stribog512_init,
stribog_update,
stribog_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,
64,
(sizeof(GOST34112012Context) + 15)
#if PHP_VERSION_ID >= 70400
, 1
#endif
};
static PHP_MINIT_FUNCTION(stribog)
{
php_hash_register_algo("stribog256", &stribog256_hash_ops);
php_hash_register_algo("stribog512", &stribog512_hash_ops);
php_hash_register_algo("stribog", &stribog512_hash_ops);
return SUCCESS;
}
static PHP_MINFO_FUNCTION(stribog)
{
php_info_print_table_start();
php_info_print_table_row(2, "GOST R 34.11-2012 hash functions", "enabled");
php_info_print_table_row(2, "Version", PHP_STRIBOG_EXTVER);
php_info_print_table_end();
}
static
#if ZEND_MODULE_API_NO > 20060613
const
#endif
zend_module_dep stribog_deps[] = {
ZEND_MOD_REQUIRED("hash")
ZEND_MOD_END
};
zend_module_entry stribog_module_entry = {
STANDARD_MODULE_HEADER_EX,
NULL,
stribog_deps,
PHP_STRIBOG_EXTNAME,
NULL,
PHP_MINIT(stribog),
NULL,
NULL,
NULL,
PHP_MINFO(stribog),
PHP_STRIBOG_EXTVER,
NO_MODULE_GLOBALS,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_STRIBOG
ZEND_GET_MODULE(stribog)
#endif