summaryrefslogtreecommitdiff
path: root/plugins/Kuzne4ikCrypt/src/GOST/block_chipher.h
blob: 869a0e1fd9064ebe2ce56099b3eaf97823e93c7b (plain)
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
362
363
364
365
366
367
368
/** @file 
 * @brief Îáúÿâëåíèå ôóíêöèé ðåàëèçóþùèõ ðåæèìû ðàáîòû áëî÷íûõ àëãîðèòìîâ
 *
 * @copyright InfoTeCS. All rights reserved.
 */

#ifndef BLOCK_CHIPHER_H
#define BLOCK_CHIPHER_H

#define DLL_IMPORT

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ ECB àëãîðèòìà "êóçíå÷èê" */
#define kEcb14ContextLen 0x28

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ ECB àëãîðèòìà 28147-89 */
#define kEcb89ContextLen 0x28

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ CBC àëãîðèòìà "êóçíå÷èê" */
#define kCbc14ContextLen 0x38

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ CBC àëãîðèòìà 28147-89 */
#define kCbc89ContextLen 0x38

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ CRT àëãîðèòìà "êóçíå÷èê" */
#define kCrt14ContextLen 0x30

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ CRT àëãîðèòìà 28147-89 */
#define kCrt89ContextLen 0x30

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ OFB àëãîðèòìà "êóçíå÷èê" */
#define kOfb14ContextLen 0x40

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ OFB àëãîðèòìà 28147-89 */
#define kOfb89ContextLen 0x40

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ CFB àëãîðèòìà "êóçíå÷èê" */
#define kCfb14ContextLen 0x40

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà øèôðîâàíèÿ CFB àëãîðèòìà 28147-89 */
#define kCfb89ContextLen 0x40

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà ôîðìèðîâàíèÿ èìèòîâñòàâêè àëãîðèòìà "êóçíå÷èê" */
#define kImit14ContextLen 0x68

/** @brief Ðàçìåð êîíòåêñòà äëÿ ðåæèìà ôîðìèðîâàíèÿ èìèòîâñòàâêè àëãîðèòìà 28147-89 */
#define kImit89ContextLen 0x68

/** @brief Ðàçìåð áëîêà àëãîðèòìà "êóçíå÷èê" */
#define kBlockLen14 16

/** @brief Ðàçìåð áëîêà àëãîðèòìà 28147-89 */
#define kBlockLen89 8

/** @brief Ðàçìåð êëþ÷à àëãîðèòìà 28147-89 */
#define kKeyLen89 32

#ifdef __cplusplus
extern "C" {
#endif

/* *** Ðåæèìû øèôðîâàíèÿ ***
 * Ðåæèìû øèôðîâàíèÿ ðàáîòàþò ñ èñïîëüçîâàíèåì áàçîâîãî 
 * êðèïòîãðàôè÷åñêîãî ïðåîáðàçîâàíèÿ
 */

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà øèôðîâàíèÿ â ðåæèìå ECB äëÿ àëãîðèòìà "êóçíå÷èê"
 *
 * @param[in] key êëþ÷
 * @param[out] ctx êîíòåêñò cbc
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_ecb_14(unsigned char *key, void* ctx);

/** @brief Óäàëåíèå êîíòåêñòà ecb
 *
 * @param[in] ctx êîíòåêñò ecb
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
void DLL_IMPORT free_ecb(void* ctx);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà øèôðîâàíèÿ â ðåæèìå CBC äëÿ àëãîðèòìà "êóçíå÷èê"
 *
 * @param[in] key êëþ÷
 * @param[out] ctx êîíòåêñò cbc
 * @param[in] iv ñèíõðîïîñûëêà
 * @param[in] ivLength äëèííà ñèíõðîïîñûëêè
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT  init_cbc_14(unsigned char *key, void* ctx, unsigned char *iv, size_t ivLength);

/** @brief Óäàëåíèå êîíòåêñòà cbc
 *
 * @param[in] ctx êîíòåêñò cbc
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
void DLL_IMPORT free_cbc(void* ctx);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà øèôðîâàíèÿ â ðåæèìå CRT äëÿ àëãîðèòìà "êóçíå÷èê"
 *
 * @param[in] key êëþ÷
 * @param[out] ctx êîíòåêñò crt
 * @param[in] iv ñèíõðîïîñûëêà
 * @param[in] length äëèííà ñèíõðîïîñûëêè
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_crt_14(unsigned char* key, unsigned char *iv, size_t length, void *ctx);

/** @brief Óäàëåíèå êîíòåêñòà crt
 *
 * @param[in] ctx êîíòåêñò crt
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
void DLL_IMPORT free_crt(void* ctx);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà øèôðîâàíèÿ â ðåæèìå OFB äëÿ àëãîðèòìà "êóçíå÷èê"
 *
 * @param[in] key êëþ÷
 * @param[out] ctx êîíòåêñò ofb
 * @param[in] s ïàðàìåòð S
 * @param[in] iv ñèíõðîïîñûëêà
 * @param[in] ivLength äëèííà ñèíõðîïîñûëêè
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_ofb_14(unsigned char *key, void *ctx, size_t s, unsigned char *iv, size_t ivLength);


/** @brief Óäàëåíèå êîíòåêñòà ofb
 *
 * @param[in] ctx êîíòåêñò ofb
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
void DLL_IMPORT free_ofb(void* ctx);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà øèôðîâàíèÿ â ðåæèìå CFB äëÿ àëãîðèòìà "êóçíå÷èê"
 *
 * @param[in] key êëþ÷
 * @param[out] ctx êîíòåêñò cfb
 * @param[in] s ïàðàìåòð S
 * @param[in] iv ñèíõðîïîñûëêà
 * @param[in] ivLength äëèííà ñèíõðîïîñûëêè
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_cfb_14(unsigned char *key, void *ctx, size_t s, unsigned char *iv, size_t ivLength);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà øèôðîâàíèÿ â ðåæèìå CFB äëÿ àëãîðèòìà 28147-89
 *
 * @param[in] key êëþ÷
 * @param[out] ctx êîíòåêñò cfb
 * @param[in] s ïàðàìåòð S
 * @param[in] iv ñèíõðîïîñûëêà
 * @param[in] ivLength äëèííà ñèíõðîïîñûëêè
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_cfb_89(unsigned char *key, void *ctx, size_t s, unsigned char *iv, size_t ivLength);

/** @brief Óäàëåíèå êîíòåêñòà cfb
 *
 * @param[in] ctx êîíòåêñò cfb
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
void DLL_IMPORT free_cfb(void* ctx);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà èìòîâñòàâêè äëÿ àëãîðèòìà "êóçíå÷èê"
 *
 * @param[out] ctx êîíòåêñò èìèòîâñòàâêè
 * @param[in] key êëþ÷
 * @param[in] s ïàðàìåòð S
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_imit_14(unsigned char *key, size_t s, void *ctx);

/** @brief Èíèöèàëèçàöèÿ êîíòåêñòà èìòîâñòàâêè äëÿ àëãîðèòìà 28147-89
 *
 * @param[in] key êëþ÷
 * @param[in] s ïàðàìåòð S
 * @param[out] ctx êîíòåêñò èìèòîâñòàâêè
 * @param[in] print ôóíêöèÿ ëîãèðîâàíèÿ
 * @param[in] print_uint ôóíêöèÿ ëîãèðîâàíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT init_imit_89(unsigned char *key, size_t s, void *ctx);

/** @brief Óäàëåíèå êîíòåêñòà èìèòîâñòàâêè
 *
 * @param[in] ctx êîíòåêñò èìèòîâñòàâêè
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
void DLL_IMPORT free_imit(void* ctx);

/** @brief Âûïîëíåíèå çàøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ïðîñòîé çàìåíû äëÿ äàííûõ êðàòíûõ ðàçìåðó áëîêà
 *
 * @param[in] ctx êîíòåêñò ECB
 * @param[in] indata îòêðûòûé òåêñò
 * @param[out] outdata çàøèôðîâàííûé òåêñò
 * @param[in] length äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT encrypt_ecb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t length);

/** @brief Âûïîëíåíèå ðàñøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ïðîñòîé çàìåíû äëÿ äàííûõ êðàòíûõ ðàçìåðó áëîêà
 *
 * @param[in] ctx êîíòåêñò ECB
 * @param[in] indata îòêðûòûé òåêñò
 * @param[out] outdata çàøèôðîâàííûé òåêñò
 * @param[in] length äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT decrypt_ecb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t length);

/** @brief Âûïîëíåíèå çàøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ïðîñòîé çàìåíû ñ çàöåïëåíèåì äëÿ äàííûõ êðàòíûõ ðàçìåðó áëîêà
 *
 * @param[in] ctx êîíòåêñò CBC
 * @param[in] indata îòêðûòûé òåêñò
 * @param[out] outdata çàøèôðîâàííûé òåêñò
 * @param[in] length äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT encrypt_cbc(void *ctx, unsigned char *indata, unsigned char *outdata, size_t length);

/** @brief Âûïîëíåíèå ðñøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ïðîñòîé çàìåíû ñ çàöåïëåíèåì äëÿ äàííûõ êðàòíûõ ðàçìåðó áëîêà
 *
 * @param[in] ctx êîíòåêñò CBC
 * @param[in] indata çàøèôðîâàííûé òåêñò
 * @param[out] outdata ðàñøèôðîâàííûé òåêñò
 * @param[in] length äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT decrypt_cbc(void *ctx, unsigned char *indata, unsigned char *outdata, size_t length);

/** @brief Âûïîëíåíèå øèôðîâàíèÿ (çàøèôðîâàíèÿ èëè ðàñøèôðîâàíèÿ) â ðåæèìå ãàììèðîâàíèÿ
 *
 * @param[in] ctx êîíòåêñò CRT
 * @param[in] indata âõîäíîå ñîîáùåíèå
 * @param[out] outdata ðåçóëüòàò
 * @param[in] length äëèííà ñîîáùåíèÿ
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT crypt_crt(void *ctx, unsigned char *indata, unsigned char *outdata, size_t length);

/** @brief Âûïîëíåíèå øàãîâîé øèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ãàììèðîâàíèÿ ñ îáðàòíîé ñâÿçüþ
 *
 * @param[in] ctx êîíòåêñò OFB
 * @param[in] indata âõîäíîé áëîê
 * @param[out] outdata ðåçóëüòàò ïðåîáðàçîâàíèÿ
 * @param[in] inlength äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT crypt_ofb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t inlength);

/** @brief Âûïîëíåíèå çàøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ãàììèðîâàíèÿ ñ îáðàòíîé ñâÿçüþ
 *
 * @param[in] ctx êîíòåêñò OFB
 * @param[in] indata îòêðûòûé òåêñò
 * @param[out] outdata çàøèôðîâàííûé òåêñò
 * @param[in] inlength äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT encrypt_ofb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t inlength);

/** @brief Âûïîëíåíèå ðàñøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ãàììèðîâàíèÿ ñ îáðàòíîé ñâÿçüþ
 *
 * @param[in] ctx êîíòåêñò OFB
 * @param[in] indata çàøèôðîâàííûé òåêñò
 * @param[out] outdata ðàñøèôðîâàííûé òåêñò
 * @param[in] inlength äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT decrypt_ofb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t inlength);

/** @brief Âûïîëíåíèå çàøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ãàììèðîâàíèÿ ñ îáðàòíîé ñâÿçüþ ïî øèôðòåêñòó
 *
 * @param[in] ctx êîíòåêñò CFB
 * @param[in] indata îòêðûòûé òåêñò
 * @param[out] outdata çàøèôðîâàííûé òåêñò
 * @param[in] inlength äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT encrypt_cfb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t inlength);

/** @brief Âûïîëíåíèå ðàñøèôðîâàíèÿ èíôîðìàöèè â ðåæèìå ãàììèðîâàíèÿ ñ îáðàòíîé ñâÿçüþ ïî øèôðòåêñòó
 *
 * @param[in] ctx êîíòåêñò CFB
 * @param[in] indata çàøèôðîâàííûé òåêñò
 * @param[out] outdata ðàñøèôðîâàííûé òåêñò
 * @param[in] inlength äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT decrypt_cfb(void *ctx, unsigned char *indata, unsigned char *outdata, size_t inlength);

/** @brief Âûïîëíåíèå âû÷èñëåíèÿ èìèòîâñòàâêè ïî äàííûì êðàòíûì ðàçìåðó áëîêà
 *
 * @param[in] ctx êîíòåêñò èìèòîâñòàâêè
 * @param[in] indata îòêðûòûé òåêñò
 * @param[in] length äëèííà òåêñòà
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT imit(void *ctx, unsigned char *indata, size_t length);

/** @brief Çàâåðøåíèå âûðîáîòêè èìèòîâñòàâêè
 *
 * @param[in] ctx êîíòåêñò èìèòîâñòàâêè
 * @param[out] value
 * @return 0 åñëè âñå ïðåîáðàçîâàíèå ïðîøëî óñïåøíî
 * @return -1 åñëè ïðîèçîøëà îøèáêà
 */
int DLL_IMPORT done_imit(void *ctx, unsigned char *value);

/** @brief Äîïîëíåíèå äàííûõ äî ðàçìåðà áëîêà. 
 *
 * @param[in] data ñîîáùåíèå. Ïàìÿòü ïîä äàííûå data äîëæíà áûòü âûäåëåíà, äîñòàòî÷íàÿ äëÿ äîïîëíåíèÿ.
 * @param[in] length ðàçìåð ñîîáùåíèÿ
 * @param[in] blockLen äëèííà áëîêà
 * @return ðàçìåð ñîîáùåíèÿ
 */
int DLL_IMPORT padd(unsigned char *data, size_t length, size_t blockLen);

/** @brief Óäàëåíèå äîïîëíåííûõ äàííûõ. Ïðè îøèáêàõ âîçâðàùàåòñÿ çíà÷åíèå -1
 *
 * @param[in] data ñîîáùåíèå
 * @param[in] length ðàçìåð ñîîáùåíèÿ
 * @return ðàçìåð ñîîáùåíèÿ
 */
int DLL_IMPORT unpadd(unsigned char *data, size_t length);

#ifdef __cplusplus
}
#endif

#endif