3 * This file is distributed under the same license as OpenSSL
6 #if defined(__cplusplus)
10 #include "gost_grasshopper_cipher.h"
11 #include "gost_grasshopper_defines.h"
12 #include "gost_grasshopper_math.h"
13 #include "gost_grasshopper_core.h"
15 #include <openssl/evp.h>
16 #include <openssl/rand.h>
17 #include <openssl/err.h>
21 #include "e_gost_err.h"
23 enum GRASSHOPPER_CIPHER_TYPE {
24 GRASSHOPPER_CIPHER_ECB = 0,
25 GRASSHOPPER_CIPHER_CBC,
26 GRASSHOPPER_CIPHER_OFB,
27 GRASSHOPPER_CIPHER_CFB,
28 GRASSHOPPER_CIPHER_CTR
31 static EVP_CIPHER* gost_grasshopper_ciphers[5] = {
32 [GRASSHOPPER_CIPHER_ECB] = NULL,
33 [GRASSHOPPER_CIPHER_CBC] = NULL,
34 [GRASSHOPPER_CIPHER_OFB] = NULL,
35 [GRASSHOPPER_CIPHER_CFB] = NULL,
36 [GRASSHOPPER_CIPHER_CTR] = NULL
39 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c);
40 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c);
42 struct GRASSHOPPER_CIPHER_PARAMS {
44 grasshopper_init_cipher_func init_cipher;
45 grasshopper_do_cipher_func do_cipher;
46 grasshopper_destroy_cipher_func destroy_cipher;
53 static struct GRASSHOPPER_CIPHER_PARAMS gost_cipher_params[5] = {
54 [GRASSHOPPER_CIPHER_ECB] = {
56 gost_grasshopper_cipher_init_ecb,
57 gost_grasshopper_cipher_do_ecb,
60 sizeof(gost_grasshopper_cipher_ctx),
64 [GRASSHOPPER_CIPHER_CBC] = {
66 gost_grasshopper_cipher_init_cbc,
67 gost_grasshopper_cipher_do_cbc,
70 sizeof(gost_grasshopper_cipher_ctx),
74 [GRASSHOPPER_CIPHER_OFB] = {
76 gost_grasshopper_cipher_init_ofb,
77 gost_grasshopper_cipher_do_ofb,
78 gost_grasshopper_cipher_destroy_ofb,
80 sizeof(gost_grasshopper_cipher_ctx_ofb),
84 [GRASSHOPPER_CIPHER_CFB] = {
86 gost_grasshopper_cipher_init_cfb,
87 gost_grasshopper_cipher_do_cfb,
90 sizeof(gost_grasshopper_cipher_ctx),
94 [GRASSHOPPER_CIPHER_CTR] = {
96 gost_grasshopper_cipher_init_ctr,
97 gost_grasshopper_cipher_do_ctr,
98 gost_grasshopper_cipher_destroy_ctr,
100 sizeof(gost_grasshopper_cipher_ctx_ctr),
106 /* Set 256 bit key into context */
107 GRASSHOPPER_INLINE void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k) {
109 for (i = 0; i < 2; i++) {
110 grasshopper_copy128(&c->key.k.k[i], (const grasshopper_w128_t*) (k + i * 16));
112 grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
113 grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
116 /* Cleans up key from context */
117 GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c) {
119 for (i = 0; i < 2; i++) {
120 grasshopper_zero128(&c->key.k.k[i]);
122 for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
123 grasshopper_zero128(&c->encrypt_round_keys.k[i]);
125 for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
126 grasshopper_zero128(&c->decrypt_round_keys.k[i]);
128 grasshopper_zero128(&c->buffer);
131 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c) {
132 gost_grasshopper_cipher_ctx_ofb* ctx = (gost_grasshopper_cipher_ctx_ofb*) c;
134 grasshopper_zero128(&ctx->buffer1);
137 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c) {
138 gost_grasshopper_cipher_ctx_ctr* ctx = (gost_grasshopper_cipher_ctx_ctr*) c;
140 grasshopper_zero128(&ctx->iv_buffer);
141 grasshopper_zero128(&ctx->partial_buffer);
144 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
145 const unsigned char* iv, int enc) {
146 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
148 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
149 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
153 gost_grasshopper_cipher_key(c, key);
157 memcpy((unsigned char*) EVP_CIPHER_CTX_original_iv(ctx), iv,
158 EVP_CIPHER_CTX_iv_length(ctx));
161 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
162 EVP_CIPHER_CTX_original_iv(ctx),
163 EVP_CIPHER_CTX_iv_length(ctx));
165 grasshopper_zero128(&c->buffer);
170 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
171 const unsigned char* iv,
173 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
174 c->type = GRASSHOPPER_CIPHER_ECB;
175 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
178 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key,
179 const unsigned char* iv,
181 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
182 c->type = GRASSHOPPER_CIPHER_CBC;
183 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
186 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
187 const unsigned char* iv,
189 gost_grasshopper_cipher_ctx_ofb* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
191 c->c.type = GRASSHOPPER_CIPHER_OFB;
193 grasshopper_zero128(&c->buffer1);
195 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
198 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
199 const unsigned char* iv,
201 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
202 c->type = GRASSHOPPER_CIPHER_CFB;
203 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
206 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key,
207 const unsigned char* iv,
209 gost_grasshopper_cipher_ctx_ctr* c = EVP_CIPHER_CTX_get_cipher_data(ctx);
211 c->c.type = GRASSHOPPER_CIPHER_CTR;
214 grasshopper_zero128(&c->iv_buffer);
215 grasshopper_zero128(&c->partial_buffer);
217 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
220 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
221 const unsigned char* in, size_t inl) {
222 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
223 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
225 return params->do_cipher(ctx, out, in, inl);
228 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
229 const unsigned char* in, size_t inl) {
230 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
231 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
232 const unsigned char* current_in = in;
233 unsigned char* current_out = out;
234 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
237 for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
239 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) current_in,
240 (grasshopper_w128_t*) current_out,
243 grasshopper_decrypt_block(&c->decrypt_round_keys, (grasshopper_w128_t*) current_in,
244 (grasshopper_w128_t*) current_out,
252 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
253 const unsigned char* in, size_t inl) {
254 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
255 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
256 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
257 const unsigned char* current_in = in;
258 unsigned char* current_out = out;
259 grasshopper_w128_t* currentInputBlock;
260 grasshopper_w128_t* currentOutputBlock;
261 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
263 grasshopper_w128_t* currentBlock;
265 currentBlock = (grasshopper_w128_t*) iv;
267 for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
268 currentInputBlock = (grasshopper_w128_t*) current_in;
269 currentOutputBlock = (grasshopper_w128_t*) current_out;
271 grasshopper_append128(currentBlock, currentInputBlock);
272 grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock, currentOutputBlock, &c->buffer);
273 grasshopper_copy128(currentBlock, currentOutputBlock);
275 grasshopper_decrypt_block(&c->decrypt_round_keys, currentInputBlock, currentOutputBlock, &c->buffer);
276 grasshopper_append128(currentOutputBlock, currentBlock);
277 grasshopper_copy128(currentBlock, currentInputBlock);
284 void inc_counter(unsigned char* counter, size_t counter_bytes)
287 unsigned int n = counter_bytes;
298 /* increment counter (128-bit int) by 1 */
299 static void ctr128_inc(unsigned char *counter)
301 inc_counter(counter, 16);
304 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
305 const unsigned char* in, size_t inl) {
306 gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
307 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
308 const unsigned char* current_in = in;
309 unsigned char* current_out = out;
310 grasshopper_w128_t* currentInputBlock;
311 grasshopper_w128_t* currentOutputBlock;
312 unsigned int n = ctx->num;
317 *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
319 n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
322 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
324 memcpy(&c->iv_buffer, iv, 8);
327 for (i = 0; i < blocks; i++) {
328 currentInputBlock = (grasshopper_w128_t*) current_in;
329 currentOutputBlock = (grasshopper_w128_t*) current_out;
330 grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, currentOutputBlock, &c->c.buffer);
331 grasshopper_append128(currentOutputBlock, currentInputBlock);
332 ctr128_inc(c->iv_buffer.b);
333 current_in += GRASSHOPPER_BLOCK_SIZE;
334 current_out += GRASSHOPPER_BLOCK_SIZE;
338 lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
340 currentInputBlock = (grasshopper_w128_t*) current_in;
341 currentOutputBlock = (grasshopper_w128_t*) current_out;
342 grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, &c->partial_buffer, &c->c.buffer);
343 for (i = 0; i < lasted; i++) {
344 currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
347 ctr128_inc(c->iv_buffer.b);
353 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb* ctx, grasshopper_w128_t* iv,
354 grasshopper_w128_t* buf) {
355 memcpy(&ctx->buffer1, iv, 16);
356 ctx->g = ctx->buffer1.b[0] | (ctx->buffer1.b[1] << 8) | (ctx->buffer1.b[2] << 16) |
357 ((uint32_t) ctx->buffer1.b[3] << 24);
358 ctx->g += 0x01010101;
359 ctx->buffer1.b[0] = (unsigned char) (ctx->g & 0xff);
360 ctx->buffer1.b[1] = (unsigned char) ((ctx->g >> 8) & 0xff);
361 ctx->buffer1.b[2] = (unsigned char) ((ctx->g >> 16) & 0xff);
362 ctx->buffer1.b[3] = (unsigned char) ((ctx->g >> 24) & 0xff);
363 ctx->g = ctx->buffer1.b[4] | (ctx->buffer1.b[5] << 8) | (ctx->buffer1.b[6] << 16) |
364 ((uint32_t) ctx->buffer1.b[7] << 24);
366 ctx->g += 0x01010104;
367 if (ctx->go > ctx->g) { /* overflow */
370 ctx->buffer1.b[4] = (unsigned char) (ctx->g & 0xff);
371 ctx->buffer1.b[5] = (unsigned char) ((ctx->g >> 8) & 0xff);
372 ctx->buffer1.b[6] = (unsigned char) ((ctx->g >> 16) & 0xff);
373 ctx->buffer1.b[7] = (unsigned char) ((ctx->g >> 24) & 0xff);
374 ctx->g = ctx->buffer1.b[8] | (ctx->buffer1.b[9] << 8) | (ctx->buffer1.b[10] << 16) |
375 ((uint32_t) ctx->buffer1.b[11] << 24);
377 ctx->g += 0x01010107;
378 if (ctx->go > ctx->g) { /* overflow */
381 ctx->buffer1.b[8] = (unsigned char) (ctx->g & 0xff);
382 ctx->buffer1.b[9] = (unsigned char) ((ctx->g >> 8) & 0xff);
383 ctx->buffer1.b[10] = (unsigned char) ((ctx->g >> 16) & 0xff);
384 ctx->buffer1.b[11] = (unsigned char) ((ctx->g >> 24) & 0xff);
385 ctx->g = ctx->buffer1.b[12] | (ctx->buffer1.b[13] << 8) | (ctx->buffer1.b[14] << 16) |
386 ((uint32_t) ctx->buffer1.b[15] << 24);
388 ctx->g += 0x01010110;
389 if (ctx->go > ctx->g) { /* overflow */
392 ctx->buffer1.b[12] = (unsigned char) (ctx->g & 0xff);
393 ctx->buffer1.b[13] = (unsigned char) ((ctx->g >> 8) & 0xff);
394 ctx->buffer1.b[14] = (unsigned char) ((ctx->g >> 16) & 0xff);
395 ctx->buffer1.b[15] = (unsigned char) ((ctx->g >> 24) & 0xff);
396 memcpy(iv, &ctx->buffer1, 16);
397 grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
400 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
401 const unsigned char* in, size_t inl) {
402 gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_get_cipher_data(ctx);
403 const unsigned char* in_ptr = in;
404 unsigned char* out_ptr = out;
405 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
406 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
407 int num = EVP_CIPHER_CTX_num(ctx);
411 /* process partial block if any */
413 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
414 j++, i++, in_ptr++, out_ptr++) {
415 *out_ptr = buf[j] ^ (*in_ptr);
417 if (j == GRASSHOPPER_BLOCK_SIZE) {
418 EVP_CIPHER_CTX_set_num(ctx, 0);
420 EVP_CIPHER_CTX_set_num(ctx, (int) j);
425 for (; i + GRASSHOPPER_BLOCK_SIZE <
426 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
428 * block cipher current iv
431 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
434 * xor next block of input text with it and output it
439 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
440 out_ptr[j] = buf[j] ^ in_ptr[j];
444 /* Process rest of buffer */
446 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
447 for (j = 0; i < inl; j++, i++) {
448 out_ptr[j] = buf[j] ^ in_ptr[j];
450 EVP_CIPHER_CTX_set_num(ctx, (int) j);
452 EVP_CIPHER_CTX_set_num(ctx, 0);
458 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
459 const unsigned char* in, size_t inl) {
460 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
461 const unsigned char* in_ptr = in;
462 unsigned char* out_ptr = out;
463 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
464 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
465 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
466 int num = EVP_CIPHER_CTX_num(ctx);
470 /* process partial block if any */
472 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl; j++, i++, in_ptr++, out_ptr++) {
474 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
476 *out_ptr = buf[j] ^ (*in_ptr);
478 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
481 if (j == GRASSHOPPER_BLOCK_SIZE) {
482 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
483 EVP_CIPHER_CTX_set_num(ctx, 0);
485 EVP_CIPHER_CTX_set_num(ctx, (int) j);
490 for (; i + GRASSHOPPER_BLOCK_SIZE <
491 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
493 * block cipher current iv
495 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
498 * xor next block of input text with it and output it
504 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
506 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
507 out_ptr[j] = buf[j] ^ in_ptr[j];
510 /* Next iv is next block of cipher text */
512 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
516 /* Process rest of buffer */
518 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
521 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
523 for (j = 0; i < inl; j++, i++) {
524 out_ptr[j] = buf[j] ^ in_ptr[j];
526 EVP_CIPHER_CTX_set_num(ctx, (int) j);
528 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
531 EVP_CIPHER_CTX_set_num(ctx, 0);
537 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
538 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
539 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
541 gost_grasshopper_cipher_destroy(c);
542 if (params->destroy_cipher != NULL) {
543 params->destroy_cipher(c);
546 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
551 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
553 unsigned char* buf = NULL;
554 ASN1_OCTET_STRING* os = NULL;
556 os = ASN1_OCTET_STRING_new();
558 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
560 GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
565 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
569 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
572 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
579 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
581 case EVP_CTRL_RAND_KEY: {
582 if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
583 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
589 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
595 GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
596 return EVP_CIPHER_meth_new(cipher_type,
597 block_size /* block_size */,
598 GRASSHOPPER_KEY_SIZE /* key_size */);
601 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding) {
602 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size) &&
603 EVP_CIPHER_meth_set_flags(cipher, (unsigned long) (
605 ((!padding) ? EVP_CIPH_NO_PADDING : 0) |
606 ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0) |
608 EVP_CIPH_ALWAYS_CALL_INIT)
610 EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup) &&
611 EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_grasshopper_set_asn1_parameters) &&
612 EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_grasshopper_get_asn1_parameters) &&
613 EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl) &&
614 EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
617 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
619 struct GRASSHOPPER_CIPHER_PARAMS* params;
621 cipher = &gost_grasshopper_ciphers[num];
623 if (*cipher == NULL) {
624 params = &gost_cipher_params[num];
626 int nid = params->nid;
627 grasshopper_init_cipher_func init_cipher = params->init_cipher;
628 int block_size = params->block_size;
629 int ctx_size = params->ctx_size;
630 int iv_size = params->iv_size;
631 bool padding = params->padding;
633 *cipher = cipher_gost_grasshopper_create(nid, block_size);
634 if (*cipher == NULL) {
638 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding) ||
639 !EVP_CIPHER_meth_set_init(*cipher, init_cipher) ||
640 !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
641 EVP_CIPHER_meth_free(*cipher);
649 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ecb() {
650 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
653 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cbc() {
654 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
657 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ofb() {
658 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
661 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cfb() {
662 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
665 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctr() {
666 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
669 void cipher_gost_grasshopper_destroy(void)
671 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
672 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
673 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
674 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
675 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
676 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
677 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
678 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
679 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
680 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
683 #if defined(__cplusplus)