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>
20 #include "e_gost_err.h"
22 enum GRASSHOPPER_CIPHER_TYPE {
23 GRASSHOPPER_CIPHER_ECB = 0,
24 GRASSHOPPER_CIPHER_CBC,
25 GRASSHOPPER_CIPHER_OFB,
26 GRASSHOPPER_CIPHER_CFB,
27 GRASSHOPPER_CIPHER_CTR
30 static EVP_CIPHER* gost_grasshopper_ciphers[5] = {
31 [GRASSHOPPER_CIPHER_ECB] = NULL,
32 [GRASSHOPPER_CIPHER_CBC] = NULL,
33 [GRASSHOPPER_CIPHER_OFB] = NULL,
34 [GRASSHOPPER_CIPHER_CFB] = NULL,
35 [GRASSHOPPER_CIPHER_CTR] = NULL
38 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c);
39 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c);
41 struct GRASSHOPPER_CIPHER_PARAMS {
43 grasshopper_init_cipher_func init_cipher;
44 grasshopper_do_cipher_func do_cipher;
45 grasshopper_destroy_cipher_func destroy_cipher;
52 static struct GRASSHOPPER_CIPHER_PARAMS gost_cipher_params[5] = {
53 [GRASSHOPPER_CIPHER_ECB] = {
55 gost_grasshopper_cipher_init_ecb,
56 gost_grasshopper_cipher_do_ecb,
59 sizeof(gost_grasshopper_cipher_ctx),
63 [GRASSHOPPER_CIPHER_CBC] = {
65 gost_grasshopper_cipher_init_cbc,
66 gost_grasshopper_cipher_do_cbc,
69 sizeof(gost_grasshopper_cipher_ctx),
73 [GRASSHOPPER_CIPHER_OFB] = {
75 gost_grasshopper_cipher_init_ofb,
76 gost_grasshopper_cipher_do_ofb,
77 gost_grasshopper_cipher_destroy_ofb,
79 sizeof(gost_grasshopper_cipher_ctx_ofb),
83 [GRASSHOPPER_CIPHER_CFB] = {
85 gost_grasshopper_cipher_init_cfb,
86 gost_grasshopper_cipher_do_cfb,
89 sizeof(gost_grasshopper_cipher_ctx),
93 [GRASSHOPPER_CIPHER_CTR] = {
95 gost_grasshopper_cipher_init_ctr,
96 gost_grasshopper_cipher_do_ctr,
97 gost_grasshopper_cipher_destroy_ctr,
99 sizeof(gost_grasshopper_cipher_ctx),
105 /* Set 256 bit key into context */
106 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k) {
107 for (int i = 0; i < 2; i++) {
108 grasshopper_copy128(&c->key.k.k[i], (const grasshopper_w128_t*) (k + i * 16));
110 grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
111 grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
114 /* Cleans up key from context */
115 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c) {
116 for (int i = 0; i < 2; i++) {
117 grasshopper_zero128(&c->key.k.k[i]);
119 for (int i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
120 grasshopper_zero128(&c->encrypt_round_keys.k[i]);
122 for (int i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
123 grasshopper_zero128(&c->decrypt_round_keys.k[i]);
125 grasshopper_zero128(&c->buffer);
128 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ofb(gost_grasshopper_cipher_ctx* c) {
129 gost_grasshopper_cipher_ctx_ofb* ctx = (gost_grasshopper_cipher_ctx_ofb*) c;
131 grasshopper_zero128(&ctx->buffer1);
134 static GRASSHOPPER_INLINE void gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx* c) {
135 gost_grasshopper_cipher_ctx_ctr* ctx = (gost_grasshopper_cipher_ctx_ctr*) c;
137 grasshopper_zero128(&ctx->iv_buffer);
138 grasshopper_zero128(&ctx->partial_buffer);
143 static int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
144 const unsigned char* iv, int enc) {
145 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_cipher_data(ctx);
147 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
148 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_cipher_data(ctx));
152 gost_grasshopper_cipher_key(c, key);
156 memcpy((unsigned char*) EVP_CIPHER_CTX_original_iv(ctx), iv,
157 EVP_CIPHER_CTX_iv_length(ctx));
160 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
161 EVP_CIPHER_CTX_original_iv(ctx),
162 EVP_CIPHER_CTX_iv_length(ctx));
164 grasshopper_zero128(&c->buffer);
169 static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
170 const unsigned char* iv,
172 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_cipher_data(ctx);
173 c->type = GRASSHOPPER_CIPHER_ECB;
174 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
177 static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key,
178 const unsigned char* iv,
180 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_cipher_data(ctx);
181 c->type = GRASSHOPPER_CIPHER_CBC;
182 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
185 static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
186 const unsigned char* iv,
188 gost_grasshopper_cipher_ctx_ofb* c = EVP_CIPHER_CTX_cipher_data(ctx);
190 c->c.type = GRASSHOPPER_CIPHER_OFB;
192 grasshopper_zero128(&c->buffer1);
194 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
197 static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key,
198 const unsigned char* iv,
200 gost_grasshopper_cipher_ctx* c = EVP_CIPHER_CTX_cipher_data(ctx);
201 c->type = GRASSHOPPER_CIPHER_CFB;
202 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
205 static GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key,
206 const unsigned char* iv,
208 gost_grasshopper_cipher_ctx_ctr* c = EVP_CIPHER_CTX_cipher_data(ctx);
210 c->c.type = GRASSHOPPER_CIPHER_CTR;
212 grasshopper_zero128(&c->iv_buffer);
213 grasshopper_zero128(&c->partial_buffer);
217 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
220 static 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_cipher_data(ctx);
223 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
225 return params->do_cipher(ctx, out, in, inl);
228 static 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_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 static 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_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 static int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
285 const unsigned char* in, size_t inl) {
286 gost_grasshopper_cipher_ctx_ctr* c = (gost_grasshopper_cipher_ctx_ctr*) EVP_CIPHER_CTX_cipher_data(ctx);
287 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
288 const unsigned char* current_in = in;
289 unsigned char* current_out = out;
290 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
291 grasshopper_w128_t* currentInputBlock;
292 grasshopper_w128_t* currentOutputBlock;
295 memcpy(&c->iv_buffer, iv, 8);
298 for (size_t i = 0; i < blocks; i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out += GRASSHOPPER_BLOCK_SIZE) {
299 currentInputBlock = (grasshopper_w128_t*) current_in;
300 currentOutputBlock = (grasshopper_w128_t*) current_out;
301 memcpy(c->iv_buffer.b + 8, &c->counter, 8);
302 grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, currentOutputBlock, &c->c.buffer);
303 grasshopper_append128(currentOutputBlock, currentInputBlock);
308 lasted = inl - blocks * GRASSHOPPER_BLOCK_SIZE;
310 currentInputBlock = (grasshopper_w128_t*) current_in;
311 currentOutputBlock = (grasshopper_w128_t*) current_out;
312 memcpy(c->iv_buffer.b + 8, &c->counter, 8);
313 grasshopper_encrypt_block(&c->c.encrypt_round_keys, &c->iv_buffer, &c->partial_buffer, &c->c.buffer);
314 for (size_t i = 0; i < lasted; i++) {
315 currentOutputBlock->b[i] = c->partial_buffer.b[i] ^ currentInputBlock->b[i];
323 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx_ofb* ctx, grasshopper_w128_t* iv,
324 grasshopper_w128_t* buf) {
325 memcpy(&ctx->buffer1, iv, 16);
326 ctx->g = ctx->buffer1.b[0] | (ctx->buffer1.b[1] << 8) | (ctx->buffer1.b[2] << 16) |
327 ((uint32_t) ctx->buffer1.b[3] << 24);
328 ctx->g += 0x01010101;
329 ctx->buffer1.b[0] = (unsigned char) (ctx->g & 0xff);
330 ctx->buffer1.b[1] = (unsigned char) ((ctx->g >> 8) & 0xff);
331 ctx->buffer1.b[2] = (unsigned char) ((ctx->g >> 16) & 0xff);
332 ctx->buffer1.b[3] = (unsigned char) ((ctx->g >> 24) & 0xff);
333 ctx->g = ctx->buffer1.b[4] | (ctx->buffer1.b[5] << 8) | (ctx->buffer1.b[6] << 16) |
334 ((uint32_t) ctx->buffer1.b[7] << 24);
336 ctx->g += 0x01010104;
337 if (ctx->go > ctx->g) { /* overflow */
340 ctx->buffer1.b[4] = (unsigned char) (ctx->g & 0xff);
341 ctx->buffer1.b[5] = (unsigned char) ((ctx->g >> 8) & 0xff);
342 ctx->buffer1.b[6] = (unsigned char) ((ctx->g >> 16) & 0xff);
343 ctx->buffer1.b[7] = (unsigned char) ((ctx->g >> 24) & 0xff);
344 ctx->g = ctx->buffer1.b[8] | (ctx->buffer1.b[9] << 8) | (ctx->buffer1.b[10] << 16) |
345 ((uint32_t) ctx->buffer1.b[11] << 24);
347 ctx->g += 0x01010107;
348 if (ctx->go > ctx->g) { /* overflow */
351 ctx->buffer1.b[8] = (unsigned char) (ctx->g & 0xff);
352 ctx->buffer1.b[9] = (unsigned char) ((ctx->g >> 8) & 0xff);
353 ctx->buffer1.b[10] = (unsigned char) ((ctx->g >> 16) & 0xff);
354 ctx->buffer1.b[11] = (unsigned char) ((ctx->g >> 24) & 0xff);
355 ctx->g = ctx->buffer1.b[12] | (ctx->buffer1.b[13] << 8) | (ctx->buffer1.b[14] << 16) |
356 ((uint32_t) ctx->buffer1.b[15] << 24);
358 ctx->g += 0x01010110;
359 if (ctx->go > ctx->g) { /* overflow */
362 ctx->buffer1.b[12] = (unsigned char) (ctx->g & 0xff);
363 ctx->buffer1.b[13] = (unsigned char) ((ctx->g >> 8) & 0xff);
364 ctx->buffer1.b[14] = (unsigned char) ((ctx->g >> 16) & 0xff);
365 ctx->buffer1.b[15] = (unsigned char) ((ctx->g >> 24) & 0xff);
366 memcpy(iv, &ctx->buffer1, 16);
367 grasshopper_encrypt_block(&ctx->c.encrypt_round_keys, &ctx->buffer1, buf, &ctx->c.buffer);
370 static int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
371 const unsigned char* in, size_t inl) {
372 gost_grasshopper_cipher_ctx_ofb* c = (gost_grasshopper_cipher_ctx_ofb*) EVP_CIPHER_CTX_cipher_data(ctx);
373 const unsigned char* in_ptr = in;
374 unsigned char* out_ptr = out;
375 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
376 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
377 int num = EVP_CIPHER_CTX_num(ctx);
381 /* process partial block if any */
383 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
384 j++, i++, in_ptr++, out_ptr++) {
385 *out_ptr = buf[j] ^ (*in_ptr);
387 if (j == GRASSHOPPER_BLOCK_SIZE) {
388 EVP_CIPHER_CTX_set_num(ctx, 0);
390 EVP_CIPHER_CTX_set_num(ctx, (int) j);
395 for (; i + GRASSHOPPER_BLOCK_SIZE <
396 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
398 * block cipher current iv
401 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
404 * xor next block of input text with it and output it
409 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
410 out_ptr[j] = buf[j] ^ in_ptr[j];
414 /* Process rest of buffer */
416 gost_grasshopper_cnt_next(c, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf);
417 for (j = 0; i < inl; j++, i++) {
418 out_ptr[j] = buf[j] ^ in_ptr[j];
420 EVP_CIPHER_CTX_set_num(ctx, (int) j);
422 EVP_CIPHER_CTX_set_num(ctx, 0);
428 static int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
429 const unsigned char* in, size_t inl) {
430 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_cipher_data(ctx);
431 const unsigned char* in_ptr = in;
432 unsigned char* out_ptr = out;
433 unsigned char* buf = EVP_CIPHER_CTX_buf_noconst(ctx);
434 unsigned char* iv = EVP_CIPHER_CTX_iv_noconst(ctx);
435 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
436 int num = EVP_CIPHER_CTX_num(ctx);
440 /* process partial block if any */
442 for (j = (size_t) num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl; j++, i++, in_ptr++, out_ptr++) {
444 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
446 *out_ptr = buf[j] ^ (*in_ptr);
448 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
451 if (j == GRASSHOPPER_BLOCK_SIZE) {
452 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
453 EVP_CIPHER_CTX_set_num(ctx, 0);
455 EVP_CIPHER_CTX_set_num(ctx, (int) j);
460 for (; i + GRASSHOPPER_BLOCK_SIZE <
461 inl; i += GRASSHOPPER_BLOCK_SIZE, in_ptr += GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
463 * block cipher current iv
465 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
468 * xor next block of input text with it and output it
474 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
476 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
477 out_ptr[j] = buf[j] ^ in_ptr[j];
480 /* Next iv is next block of cipher text */
482 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
486 /* Process rest of buffer */
488 grasshopper_encrypt_block(&c->encrypt_round_keys, (grasshopper_w128_t*) iv, (grasshopper_w128_t*) buf,
491 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
493 for (j = 0; i < inl; j++, i++) {
494 out_ptr[j] = buf[j] ^ in_ptr[j];
496 EVP_CIPHER_CTX_set_num(ctx, (int) j);
498 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
501 EVP_CIPHER_CTX_set_num(ctx, 0);
507 static int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx) {
508 gost_grasshopper_cipher_ctx* c = (gost_grasshopper_cipher_ctx*) EVP_CIPHER_CTX_cipher_data(ctx);
509 struct GRASSHOPPER_CIPHER_PARAMS* params = &gost_cipher_params[c->type];
511 gost_grasshopper_cipher_destroy(c);
512 if (params->destroy_cipher != NULL) {
513 params->destroy_cipher(c);
516 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
521 static int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
523 unsigned char* buf = NULL;
524 unsigned char* p = NULL;
525 ASN1_OCTET_STRING* os = NULL;
527 os = ASN1_OCTET_STRING_new();
529 if (!os || !ASN1_OCTET_STRING_set(os, buf, len)) {
531 GOSTerr(GOST_F_GOST89_SET_ASN1_PARAMETERS, ERR_R_MALLOC_FAILURE);
536 ASN1_TYPE_set(params, V_ASN1_SEQUENCE, os);
540 static GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params) {
543 if (ASN1_TYPE_get(params) != V_ASN1_SEQUENCE) {
550 static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr) {
552 case EVP_CTRL_RAND_KEY: {
553 if (RAND_bytes((unsigned char*) ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
554 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_RNG_ERROR);
560 GOSTerr(GOST_F_GOST_CIPHER_CTL, GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
566 static GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size) {
567 return EVP_CIPHER_meth_new(cipher_type,
568 block_size /* block_size */,
569 GRASSHOPPER_KEY_SIZE /* key_size */);
572 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding) {
573 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size) &&
574 EVP_CIPHER_meth_set_flags(cipher, (unsigned long) (
576 ((!padding) ? EVP_CIPH_NO_PADDING : 0) |
577 ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0) |
579 EVP_CIPH_ALWAYS_CALL_INIT)
581 EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup) &&
582 EVP_CIPHER_meth_set_set_asn1_params(cipher, gost_grasshopper_set_asn1_parameters) &&
583 EVP_CIPHER_meth_set_get_asn1_params(cipher, gost_grasshopper_get_asn1_parameters) &&
584 EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl) &&
585 EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
588 static const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num) {
590 struct GRASSHOPPER_CIPHER_PARAMS* params;
592 cipher = &gost_grasshopper_ciphers[num];
594 if (*cipher == NULL) {
595 params = &gost_cipher_params[num];
597 int nid = params->nid;
598 grasshopper_init_cipher_func init_cipher = params->init_cipher;
599 int block_size = params->block_size;
600 int ctx_size = params->ctx_size;
601 int iv_size = params->iv_size;
602 bool padding = params->padding;
604 *cipher = cipher_gost_grasshopper_create(nid, block_size);
605 if (*cipher == NULL) {
609 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding) ||
610 !EVP_CIPHER_meth_set_init(*cipher, init_cipher) ||
611 !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
612 EVP_CIPHER_meth_free(*cipher);
620 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ecb() {
621 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
624 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cbc() {
625 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
628 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ofb() {
629 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
632 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_cfb() {
633 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
636 const GRASSHOPPER_INLINE EVP_CIPHER* cipher_gost_grasshopper_ctr() {
637 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
640 #if defined(__cplusplus)