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;
213 grasshopper_zero128(&c->iv_buffer);
214 grasshopper_zero128(&c->partial_buffer);
216 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
219 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
220 const unsigned char* in, size_t inl) {
221 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
222 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
224 return params->do_cipher(ctx, out, in, inl);
227 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
228 const unsigned char* in, size_t inl) {
229 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
230 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
231 const unsigned char* current_in = in;
232 unsigned char* current_out = out;
233 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
236 for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
238 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) current_in,
239 (grasshopper_w128_t*) current_out,
242 grasshopper_decrypt_block(&c->decrypt_round_keys, (grasshopper_w128_t*) current_in,
243 (grasshopper_w128_t*) current_out,
251 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
252 const unsigned char* in, size_t inl) {
253 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
254 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
255 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
256 const unsigned char* current_in = in;
257 unsigned char* current_out = out;
258 grasshopper_w128_t* currentInputBlock;
259 grasshopper_w128_t* currentOutputBlock;
260 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
262 grasshopper_w128_t* currentBlock;
264 currentBlock = (grasshopper_w128_t*) iv;
266 for (i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
267 currentInputBlock = (grasshopper_w128_t*) current_in;
268 currentOutputBlock = (grasshopper_w128_t*) current_out;
270 grasshopper_append128(currentBlock, currentInputBlock);
271 grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock, currentOutputBlock, &c->buffer);
272 grasshopper_copy128(currentBlock, currentOutputBlock);
274 grasshopper_decrypt_block(&c->decrypt_round_keys, currentInputBlock, currentOutputBlock, &c->buffer);
275 grasshopper_append128(currentOutputBlock, currentBlock);
276 grasshopper_copy128(currentBlock, currentInputBlock);
283 void inc_counter(unsigned char* counter, size_t counter_bytes)
286 unsigned int n = counter_bytes;
297 /* increment counter (128-bit int) by 1 */
298 static void ctr128_inc(unsigned char *counter)
300 inc_counter(counter, 16);
303 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
304 const unsigned char* in, size_t inl) {
305 gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_get_cipher_data(ctx);
306 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
307 const unsigned char* current_in = in;
308 unsigned char* current_out = out;
309 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
310 grasshopper_w128_t* currentInputBlock;
311 grasshopper_w128_t* currentOutputBlock;
315 memcpy(&c->iv_buffer, iv, 8);
318 for (i = 0; i < blocks; i++) {
319 currentInputBlock = (grasshopper_w128_t*) current_in;
320 currentOutputBlock = (grasshopper_w128_t*) current_out;
321 grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, currentOutputBlock, &c->c.buffer);
322 grasshopper_append128(currentOutputBlock, currentInputBlock);
323 ctr128_inc(c->iv_buffer.b);
324 current_in += GRASSHOPPER_BLOCK_SIZE;
325 current_out += GRASSHOPPER_BLOCK_SIZE;
329 lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
331 currentInputBlock = (grasshopper_w128_t*) current_in;
332 currentOutputBlock = (grasshopper_w128_t*) current_out;
333 grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, &c->partial_buffer, &c->c.buffer);
334 for (i = 0; i < lasted; i++) {
335 currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
337 ctr128_inc(c->iv_buffer.b);
343 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb* ctx, grasshopper_w128_t* iv,
344 grasshopper_w128_t* buf) {
345 memcpy(&ctx->buffer1, iv, 16);
346 ctx->g = ctx->buffer1.b[0] | (ctx->buffer1.b[1] << 8) | (ctx->buffer1.b[2] << 16) |
347 ((uint32_t) ctx->buffer1.b[3] << 24);
348 ctx->g += 0x01010101;
349 ctx->buffer1.b[0] = (unsigned char) (ctx->g & 0xff);
350 ctx->buffer1.b[1] = (unsigned char) ((ctx->g >> 8) & 0xff);
351 ctx->buffer1.b[2] = (unsigned char) ((ctx->g >> 16) & 0xff);
352 ctx->buffer1.b[3] = (unsigned char) ((ctx->g >> 24) & 0xff);
353 ctx->g = ctx->buffer1.b[4] | (ctx->buffer1.b[5] << 8) | (ctx->buffer1.b[6] << 16) |
354 ((uint32_t) ctx->buffer1.b[7] << 24);
356 ctx->g += 0x01010104;
357 if (ctx->go > ctx->g) { /* overflow */
360 ctx->buffer1.b[4] = (unsigned char) (ctx->g & 0xff);
361 ctx->buffer1.b[5] = (unsigned char) ((ctx->g >> 8) & 0xff);
362 ctx->buffer1.b[6] = (unsigned char) ((ctx->g >> 16) & 0xff);
363 ctx->buffer1.b[7] = (unsigned char) ((ctx->g >> 24) & 0xff);
364 ctx->g = ctx->buffer1.b[8] | (ctx->buffer1.b[9] << 8) | (ctx->buffer1.b[10] << 16) |
365 ((uint32_t) ctx->buffer1.b[11] << 24);
367 ctx->g += 0x01010107;
368 if (ctx->go > ctx->g) { /* overflow */
371 ctx->buffer1.b[8] = (unsigned char) (ctx->g & 0xff);
372 ctx->buffer1.b[9] = (unsigned char) ((ctx->g >> 8) & 0xff);
373 ctx->buffer1.b[10] = (unsigned char) ((ctx->g >> 16) & 0xff);
374 ctx->buffer1.b[11] = (unsigned char) ((ctx->g >> 24) & 0xff);
375 ctx->g = ctx->buffer1.b[12] | (ctx->buffer1.b[13] << 8) | (ctx->buffer1.b[14] << 16) |
376 ((uint32_t) ctx->buffer1.b[15] << 24);
378 ctx->g += 0x01010110;
379 if (ctx->go > ctx->g) { /* overflow */
382 ctx->buffer1.b[12] = (unsigned char) (ctx->g & 0xff);
383 ctx->buffer1.b[13] = (unsigned char) ((ctx->g >> 8) & 0xff);
384 ctx->buffer1.b[14] = (unsigned char) ((ctx->g >> 16) & 0xff);
385 ctx->buffer1.b[15] = (unsigned char) ((ctx->g >> 24) & 0xff);
386 memcpy(iv, &ctx->buffer1, 16);
387 grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
390 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
391 const unsigned char* in, size_t inl) {
392 gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_get_cipher_data(ctx);
393 const unsigned char* in_ptr = in;
394 unsigned char* out_ptr = out;
395 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
396 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
397 int num = EVP_CIPHER_CTX_num(ctx);
401 /* process partial block if any */
403 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
404 j++, i++, in_ptr++, out_ptr++) {
405 *out_ptr = buf[j] ^ (*in_ptr);
407 if (j == GRASSHOPPER_BLOCK_SIZE) {
408 EVP_CIPHER_CTX_set_num(ctx, 0);
410 EVP_CIPHER_CTX_set_num(ctx, (int) j);
415 for (; i + GRASSHOPPER_BLOCK_SIZE <
416 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
418 * block cipher current iv
421 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
424 * xor next block of input text with it and output it
429 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
430 out_ptr[j] = buf[j] ^ in_ptr[j];
434 /* Process rest of buffer */
436 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
437 for (j = 0; i < inl; j++, i++) {
438 out_ptr[j] = buf[j] ^ in_ptr[j];
440 EVP_CIPHER_CTX_set_num(ctx, (int) j);
442 EVP_CIPHER_CTX_set_num(ctx, 0);
448 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
449 const unsigned char* in, size_t inl) {
450 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
451 const unsigned char* in_ptr = in;
452 unsigned char* out_ptr = out;
453 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
454 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
455 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
456 int num = EVP_CIPHER_CTX_num(ctx);
460 /* process partial block if any */
462 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl; j++, i++, in_ptr++, out_ptr++) {
464 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
466 *out_ptr = buf[j] ^ (*in_ptr);
468 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
471 if (j == GRASSHOPPER_BLOCK_SIZE) {
472 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
473 EVP_CIPHER_CTX_set_num(ctx, 0);
475 EVP_CIPHER_CTX_set_num(ctx, (int) j);
480 for (; i + GRASSHOPPER_BLOCK_SIZE <
481 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
483 * block cipher current iv
485 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
488 * xor next block of input text with it and output it
494 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
496 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
497 out_ptr[j] = buf[j] ^ in_ptr[j];
500 /* Next iv is next block of cipher text */
502 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
506 /* Process rest of buffer */
508 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
511 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
513 for (j = 0; i < inl; j++, i++) {
514 out_ptr[j] = buf[j] ^ in_ptr[j];
516 EVP_CIPHER_CTX_set_num(ctx, (int) j);
518 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
521 EVP_CIPHER_CTX_set_num(ctx, 0);
527 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
528 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_get_cipher_data(ctx);
529 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
531 gost_grasshopper_cipher_destroy(c);
532 if (params->destroy_cipher != NULL) {
533 params->destroy_cipher(c);
536 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
541 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
543 unsigned char* buf = NULL;
544 ASN1_OCTET_STRING* os = NULL;
546 os = ASN1_OCTET_STRING_new();
548 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
550 GOSTerr(GOST_F_GOST_GRASSHOPPER_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
555 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
559 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
562 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
569 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
571 case EVP_CTRL_RAND_KEY: {
572 if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
573 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
579 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
585 GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
586 return EVP_CIPHER_meth_new(cipher_type,
587 block_size /* block_size */,
588 GRASSHOPPER_KEY_SIZE /* key_size */);
591 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding) {
592 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size) &&
593 EVP_CIPHER_meth_set_flags(cipher, (unsigned long) (
595 ((!padding) ? EVP_CIPH_NO_PADDING : 0) |
596 ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0) |
598 EVP_CIPH_ALWAYS_CALL_INIT)
600 EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup) &&
601 EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_grasshopper_set_asn1_parameters) &&
602 EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_grasshopper_get_asn1_parameters) &&
603 EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl) &&
604 EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
607 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
609 struct GRASSHOPPER_CIPHER_PARAMS* params;
611 cipher = &gost_grasshopper_ciphers[num];
613 if (*cipher == NULL) {
614 params = &gost_cipher_params[num];
616 int nid = params->nid;
617 grasshopper_init_cipher_func init_cipher = params->init_cipher;
618 int block_size = params->block_size;
619 int ctx_size = params->ctx_size;
620 int iv_size = params->iv_size;
621 bool padding = params->padding;
623 *cipher = cipher_gost_grasshopper_create(nid, block_size);
624 if (*cipher == NULL) {
628 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding) ||
629 !EVP_CIPHER_meth_set_init(*cipher, init_cipher) ||
630 !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
631 EVP_CIPHER_meth_free(*cipher);
639 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ecb() {
640 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
643 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cbc() {
644 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
647 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ofb() {
648 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
651 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cfb() {
652 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
655 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctr() {
656 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
659 void cipher_gost_grasshopper_destroy(void)
661 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
662 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
663 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
664 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
665 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
666 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
667 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
668 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
669 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
670 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
673 #if defined(__cplusplus)