3 * This file is distributed under the same license as OpenSSL
6 #include "gost_grasshopper_cipher.h"
7 #include "gost_grasshopper_defines.h"
8 #include "gost_grasshopper_math.h"
9 #include "gost_grasshopper_core.h"
10 #include "gost_gost2015.h"
12 #include <openssl/evp.h>
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
18 #include "e_gost_err.h"
20 enum GRASSHOPPER_CIPHER_TYPE {
21 GRASSHOPPER_CIPHER_ECB = 0,
22 GRASSHOPPER_CIPHER_CBC,
23 GRASSHOPPER_CIPHER_OFB,
24 GRASSHOPPER_CIPHER_CFB,
25 GRASSHOPPER_CIPHER_CTR,
26 GRASSHOPPER_CIPHER_CTRACPKM,
27 GRASSHOPPER_CIPHER_CTRACPKMOMAC,
30 static EVP_CIPHER *gost_grasshopper_ciphers[7] = {
31 NULL, NULL, NULL, NULL, NULL, NULL, NULL
34 static GRASSHOPPER_INLINE void
35 gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx * c);
37 struct GRASSHOPPER_CIPHER_PARAMS {
39 grasshopper_init_cipher_func init_cipher;
40 grasshopper_do_cipher_func do_cipher;
41 grasshopper_destroy_cipher_func destroy_cipher;
49 static struct GRASSHOPPER_CIPHER_PARAMS gost_cipher_params[7] = {
52 gost_grasshopper_cipher_init_ecb,
53 gost_grasshopper_cipher_do_ecb,
56 sizeof(gost_grasshopper_cipher_ctx),
63 gost_grasshopper_cipher_init_cbc,
64 gost_grasshopper_cipher_do_cbc,
67 sizeof(gost_grasshopper_cipher_ctx),
74 gost_grasshopper_cipher_init_ofb,
75 gost_grasshopper_cipher_do_ofb,
78 sizeof(gost_grasshopper_cipher_ctx),
85 gost_grasshopper_cipher_init_cfb,
86 gost_grasshopper_cipher_do_cfb,
89 sizeof(gost_grasshopper_cipher_ctx),
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 NID_kuznyechik_ctr_acpkm,
107 gost_grasshopper_cipher_init_ctracpkm,
108 gost_grasshopper_cipher_do_ctracpkm,
109 gost_grasshopper_cipher_destroy_ctr,
111 sizeof(gost_grasshopper_cipher_ctx_ctr),
117 NID_kuznyechik_ctr_acpkm_omac,
118 gost_grasshopper_cipher_init_ctracpkm_omac,
119 gost_grasshopper_cipher_do_ctracpkm_omac,
120 gost_grasshopper_cipher_destroy_ctr,
122 sizeof(gost_grasshopper_cipher_ctx_ctr),
125 EVP_CIPH_FLAG_CUSTOM_CIPHER|EVP_CIPH_FLAG_CIPHER_WITH_MAC|EVP_CIPH_CUSTOM_COPY
129 /* first 256 bit of D from draft-irtf-cfrg-re-keying-12 */
130 static const unsigned char ACPKM_D_2018[] = {
131 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* 64 bit */
132 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, /* 128 bit */
133 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
134 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* 256 bit */
137 static void acpkm_next(gost_grasshopper_cipher_ctx * c)
139 unsigned char newkey[GRASSHOPPER_KEY_SIZE];
140 const int J = GRASSHOPPER_KEY_SIZE / GRASSHOPPER_BLOCK_SIZE;
143 for (n = 0; n < J; n++) {
144 const unsigned char *D_n = &ACPKM_D_2018[n * GRASSHOPPER_BLOCK_SIZE];
146 grasshopper_encrypt_block(&c->encrypt_round_keys,
147 (grasshopper_w128_t *) D_n,
148 (grasshopper_w128_t *) & newkey[n *
149 GRASSHOPPER_BLOCK_SIZE],
152 gost_grasshopper_cipher_key(c, newkey);
155 /* Set 256 bit key into context */
156 GRASSHOPPER_INLINE void
157 gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx * c, const uint8_t *k)
160 for (i = 0; i < 2; i++) {
161 grasshopper_copy128(&c->key.k.k[i],
162 (const grasshopper_w128_t *)(k + i * 16));
165 grasshopper_set_encrypt_key(&c->encrypt_round_keys, &c->key);
166 grasshopper_set_decrypt_key(&c->decrypt_round_keys, &c->key);
169 /* Set master 256-bit key to be used in TLSTREE calculation into context */
170 GRASSHOPPER_INLINE void
171 gost_grasshopper_master_key(gost_grasshopper_cipher_ctx * c, const uint8_t *k)
174 for (i = 0; i < 2; i++) {
175 grasshopper_copy128(&c->master_key.k.k[i],
176 (const grasshopper_w128_t *)(k + i * 16));
180 /* Cleans up key from context */
181 GRASSHOPPER_INLINE void
182 gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx * c)
185 for (i = 0; i < 2; i++) {
186 grasshopper_zero128(&c->key.k.k[i]);
187 grasshopper_zero128(&c->master_key.k.k[i]);
189 for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
190 grasshopper_zero128(&c->encrypt_round_keys.k[i]);
192 for (i = 0; i < GRASSHOPPER_ROUND_KEYS_COUNT; i++) {
193 grasshopper_zero128(&c->decrypt_round_keys.k[i]);
195 grasshopper_zero128(&c->buffer);
198 static GRASSHOPPER_INLINE void
199 gost_grasshopper_cipher_destroy_ctr(gost_grasshopper_cipher_ctx * c)
201 gost_grasshopper_cipher_ctx_ctr *ctx =
202 (gost_grasshopper_cipher_ctx_ctr *) c;
205 EVP_MD_CTX_free(ctx->omac_ctx);
207 grasshopper_zero128(&ctx->partial_buffer);
210 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX *ctx,
211 const unsigned char *key,
212 const unsigned char *iv, int enc)
214 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
216 if (EVP_CIPHER_CTX_get_app_data(ctx) == NULL) {
217 EVP_CIPHER_CTX_set_app_data(ctx, EVP_CIPHER_CTX_get_cipher_data(ctx));
218 if (enc && c->type == GRASSHOPPER_CIPHER_CTRACPKM) {
219 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
220 if (init_zero_kdf_seed(ctr->kdf_seed) == 0)
226 gost_grasshopper_cipher_key(c, key);
227 gost_grasshopper_master_key(c, key);
231 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
232 EVP_CIPHER_CTX_iv_length(ctx));
235 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
236 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
238 grasshopper_zero128(&c->buffer);
243 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX *ctx, const unsigned char
244 *key, const unsigned char
247 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
248 c->type = GRASSHOPPER_CIPHER_ECB;
249 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
252 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char
253 *key, const unsigned char
256 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
257 c->type = GRASSHOPPER_CIPHER_CBC;
258 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
261 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX *ctx, const unsigned char
262 *key, const unsigned char
265 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
266 c->type = GRASSHOPPER_CIPHER_OFB;
267 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
270 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX *ctx, const unsigned char
271 *key, const unsigned char
274 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
275 c->type = GRASSHOPPER_CIPHER_CFB;
276 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
279 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX *ctx, const unsigned char
280 *key, const unsigned char
283 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
285 c->c.type = GRASSHOPPER_CIPHER_CTR;
286 EVP_CIPHER_CTX_set_num(ctx, 0);
288 grasshopper_zero128(&c->partial_buffer);
290 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
293 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX
295 char *key, const unsigned
298 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
300 /* NB: setting type makes EVP do_cipher callback useless */
301 c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
302 EVP_CIPHER_CTX_set_num(ctx, 0);
303 c->section_size = 4096;
305 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
308 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm_omac(EVP_CIPHER_CTX
310 char *key, const unsigned
313 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
315 /* NB: setting type makes EVP do_cipher callback useless */
316 c->c.type = GRASSHOPPER_CIPHER_CTRACPKMOMAC;
317 EVP_CIPHER_CTX_set_num(ctx, 0);
318 c->section_size = 4096;
321 unsigned char cipher_key[32];
322 c->omac_ctx = EVP_MD_CTX_new();
324 if (c->omac_ctx == NULL) {
325 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_INIT_CTRACPKM_OMAC, ERR_R_MALLOC_FAILURE);
329 if (gost2015_acpkm_omac_init(NID_kuznyechik_mac, enc, key,
330 c->omac_ctx, cipher_key, c->kdf_seed) != 1) {
331 EVP_MD_CTX_free(c->omac_ctx);
336 return gost_grasshopper_cipher_init(ctx, cipher_key, iv, enc);
339 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
342 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX *ctx,
344 const unsigned char *in,
347 gost_grasshopper_cipher_ctx *c =
348 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
349 struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
351 return params->do_cipher(ctx, out, in, inl);
354 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
355 const unsigned char *in, size_t inl)
357 gost_grasshopper_cipher_ctx *c =
358 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
359 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
360 const unsigned char *current_in = in;
361 unsigned char *current_out = out;
362 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
365 for (i = 0; i < blocks;
366 i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
367 GRASSHOPPER_BLOCK_SIZE) {
369 grasshopper_encrypt_block(&c->encrypt_round_keys,
370 (grasshopper_w128_t *) current_in,
371 (grasshopper_w128_t *) current_out,
374 grasshopper_decrypt_block(&c->decrypt_round_keys,
375 (grasshopper_w128_t *) current_in,
376 (grasshopper_w128_t *) current_out,
384 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
385 const unsigned char *in, size_t inl)
387 gost_grasshopper_cipher_ctx *c =
388 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
389 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
390 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
391 const unsigned char *current_in = in;
392 unsigned char *current_out = out;
393 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
395 grasshopper_w128_t *currentBlock;
397 currentBlock = (grasshopper_w128_t *) iv;
399 for (i = 0; i < blocks;
400 i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
401 GRASSHOPPER_BLOCK_SIZE) {
402 grasshopper_w128_t *currentInputBlock = (grasshopper_w128_t *) current_in;
403 grasshopper_w128_t *currentOutputBlock = (grasshopper_w128_t *) current_out;
405 grasshopper_append128(currentBlock, currentInputBlock);
406 grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock,
407 currentOutputBlock, &c->buffer);
408 grasshopper_copy128(currentBlock, currentOutputBlock);
410 grasshopper_w128_t tmp;
412 grasshopper_copy128(&tmp, currentInputBlock);
413 grasshopper_decrypt_block(&c->decrypt_round_keys,
414 currentInputBlock, currentOutputBlock,
416 grasshopper_append128(currentOutputBlock, currentBlock);
417 grasshopper_copy128(currentBlock, &tmp);
424 void inc_counter(unsigned char *counter, size_t counter_bytes)
426 unsigned int n = counter_bytes;
439 /* increment counter (128-bit int) by 1 */
440 static void ctr128_inc(unsigned char *counter)
442 inc_counter(counter, 16);
445 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
446 const unsigned char *in, size_t inl)
448 gost_grasshopper_cipher_ctx_ctr *c = (gost_grasshopper_cipher_ctx_ctr *)
449 EVP_CIPHER_CTX_get_cipher_data(ctx);
450 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
451 const unsigned char *current_in = in;
452 unsigned char *current_out = out;
453 grasshopper_w128_t *currentInputBlock;
454 grasshopper_w128_t *currentOutputBlock;
455 unsigned int n = EVP_CIPHER_CTX_num(ctx);
459 grasshopper_w128_t *iv_buffer;
460 grasshopper_w128_t tmp;
462 while (n && lasted) {
463 *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
465 n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
467 EVP_CIPHER_CTX_set_num(ctx, n);
468 blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
470 iv_buffer = (grasshopper_w128_t *) iv;
473 for (i = 0; i < blocks; i++) {
474 currentInputBlock = (grasshopper_w128_t *) current_in;
475 currentOutputBlock = (grasshopper_w128_t *) current_out;
476 grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
477 &c->partial_buffer, &c->c.buffer);
478 grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock);
479 grasshopper_copy128(currentOutputBlock, &tmp);
480 ctr128_inc(iv_buffer->b);
481 current_in += GRASSHOPPER_BLOCK_SIZE;
482 current_out += GRASSHOPPER_BLOCK_SIZE;
483 lasted -= GRASSHOPPER_BLOCK_SIZE;
487 currentInputBlock = (grasshopper_w128_t *) current_in;
488 currentOutputBlock = (grasshopper_w128_t *) current_out;
489 grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
490 &c->partial_buffer, &c->c.buffer);
491 for (i = 0; i < lasted; i++) {
492 currentOutputBlock->b[i] =
493 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
495 EVP_CIPHER_CTX_set_num(ctx, i);
496 ctr128_inc(iv_buffer->b);
502 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
503 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
504 ctx, unsigned int *num)
506 if (!ctx->section_size || (*num < ctx->section_size))
509 *num &= GRASSHOPPER_BLOCK_MASK;
512 /* If meshing is not configured via ctrl (setting section_size)
513 * this function works exactly like plain ctr */
514 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
516 const unsigned char *in, size_t inl)
518 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
519 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
520 unsigned int num = EVP_CIPHER_CTX_num(ctx);
521 size_t blocks, i, lasted = inl;
522 grasshopper_w128_t tmp;
524 while ((num & GRASSHOPPER_BLOCK_MASK) && lasted) {
525 *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
529 blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
532 for (i = 0; i < blocks; i++) {
533 apply_acpkm_grasshopper(c, &num);
534 grasshopper_encrypt_block(&c->c.encrypt_round_keys,
535 (grasshopper_w128_t *) iv,
536 (grasshopper_w128_t *) & c->partial_buffer,
538 grasshopper_plus128(&tmp, &c->partial_buffer,
539 (grasshopper_w128_t *) in);
540 grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
542 in += GRASSHOPPER_BLOCK_SIZE;
543 out += GRASSHOPPER_BLOCK_SIZE;
544 num += GRASSHOPPER_BLOCK_SIZE;
545 lasted -= GRASSHOPPER_BLOCK_SIZE;
550 apply_acpkm_grasshopper(c, &num);
551 grasshopper_encrypt_block(&c->c.encrypt_round_keys,
552 (grasshopper_w128_t *) iv,
553 &c->partial_buffer, &c->c.buffer);
554 for (i = 0; i < lasted; i++)
555 out[i] = c->partial_buffer.b[i] ^ in[i];
559 EVP_CIPHER_CTX_set_num(ctx, num);
564 int gost_grasshopper_cipher_do_ctracpkm_omac(EVP_CIPHER_CTX *ctx,
566 const unsigned char *in, size_t inl)
569 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
570 /* As in and out can be the same pointer, process unencrypted here */
571 if (EVP_CIPHER_CTX_encrypting(ctx))
572 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
574 if (in == NULL && inl == 0) { /* Final call */
575 return gost2015_final_call(ctx, c->omac_ctx, KUZNYECHIK_MAC_MAX_SIZE, c->tag, gost_grasshopper_cipher_do_ctracpkm);
579 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_CTRACPKM_OMAC, ERR_R_EVP_LIB);
582 result = gost_grasshopper_cipher_do_ctracpkm(ctx, out, in, inl);
584 /* As in and out can be the same pointer, process decrypted here */
585 if (!EVP_CIPHER_CTX_encrypting(ctx))
586 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
591 * Fixed 128-bit IV implementation make shift regiser redundant.
593 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx * ctx,
594 grasshopper_w128_t * iv,
595 grasshopper_w128_t * buf)
597 grasshopper_w128_t tmp;
598 memcpy(&tmp, iv, 16);
599 grasshopper_encrypt_block(&ctx->encrypt_round_keys, &tmp,
604 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
605 const unsigned char *in, size_t inl)
607 gost_grasshopper_cipher_ctx *c = (gost_grasshopper_cipher_ctx *)
608 EVP_CIPHER_CTX_get_cipher_data(ctx);
609 const unsigned char *in_ptr = in;
610 unsigned char *out_ptr = out;
611 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
612 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
613 int num = EVP_CIPHER_CTX_num(ctx);
617 /* process partial block if any */
619 for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
620 j++, i++, in_ptr++, out_ptr++) {
621 *out_ptr = buf[j] ^ (*in_ptr);
623 if (j == GRASSHOPPER_BLOCK_SIZE) {
624 EVP_CIPHER_CTX_set_num(ctx, 0);
626 EVP_CIPHER_CTX_set_num(ctx, (int)j);
631 for (; i + GRASSHOPPER_BLOCK_SIZE <
633 i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
634 GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
636 * block cipher current iv
639 gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
640 (grasshopper_w128_t *) buf);
643 * xor next block of input text with it and output it
648 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
649 out_ptr[j] = buf[j] ^ in_ptr[j];
653 /* Process rest of buffer */
655 gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
656 (grasshopper_w128_t *) buf);
657 for (j = 0; i < inl; j++, i++) {
658 out_ptr[j] = buf[j] ^ in_ptr[j];
660 EVP_CIPHER_CTX_set_num(ctx, (int)j);
662 EVP_CIPHER_CTX_set_num(ctx, 0);
668 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
669 const unsigned char *in, size_t inl)
671 gost_grasshopper_cipher_ctx *c =
672 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
673 const unsigned char *in_ptr = in;
674 unsigned char *out_ptr = out;
675 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
676 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
677 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
678 int num = EVP_CIPHER_CTX_num(ctx);
682 /* process partial block if any */
684 for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
685 j++, i++, in_ptr++, out_ptr++) {
687 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
689 *out_ptr = buf[j] ^ (*in_ptr);
691 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
694 if (j == GRASSHOPPER_BLOCK_SIZE) {
695 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
696 EVP_CIPHER_CTX_set_num(ctx, 0);
698 EVP_CIPHER_CTX_set_num(ctx, (int)j);
703 for (; i + GRASSHOPPER_BLOCK_SIZE <
705 i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
706 GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
708 * block cipher current iv
710 grasshopper_encrypt_block(&c->encrypt_round_keys,
711 (grasshopper_w128_t *) iv,
712 (grasshopper_w128_t *) buf, &c->buffer);
714 * xor next block of input text with it and output it
720 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
722 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
723 out_ptr[j] = buf[j] ^ in_ptr[j];
726 /* Next iv is next block of cipher text */
728 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
732 /* Process rest of buffer */
734 grasshopper_encrypt_block(&c->encrypt_round_keys,
735 (grasshopper_w128_t *) iv,
736 (grasshopper_w128_t *) buf, &c->buffer);
738 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
740 for (j = 0; i < inl; j++, i++) {
741 out_ptr[j] = buf[j] ^ in_ptr[j];
743 EVP_CIPHER_CTX_set_num(ctx, (int)j);
745 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
748 EVP_CIPHER_CTX_set_num(ctx, 0);
754 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx)
756 struct GRASSHOPPER_CIPHER_PARAMS *params;
757 gost_grasshopper_cipher_ctx *c =
758 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
763 params = &gost_cipher_params[c->type];
765 gost_grasshopper_cipher_destroy(c);
766 if (params->destroy_cipher != NULL) {
767 params->destroy_cipher(c);
770 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
775 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
777 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
778 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
780 /* CMS implies 256kb section_size */
781 ctr->section_size = 256*1024;
783 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 8,
789 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX
793 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
794 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
797 unsigned char iv[16];
799 if (gost2015_get_asn1_params(params, 16, iv, 8, ctr->kdf_seed) == 0) {
803 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, iv_len);
804 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, iv_len);
806 /* CMS implies 256kb section_size */
807 ctr->section_size = 256*1024;
813 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
816 case EVP_CTRL_RAND_KEY:{
818 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
819 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
824 case EVP_CTRL_KEY_MESH:{
825 gost_grasshopper_cipher_ctx_ctr *c =
826 EVP_CIPHER_CTX_get_cipher_data(ctx);
827 if ((c->c.type != GRASSHOPPER_CIPHER_CTRACPKM &&
828 c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
830 || (arg % GRASSHOPPER_BLOCK_SIZE))
832 c->section_size = arg;
835 #ifdef EVP_CTRL_TLS1_2_TLSTREE
836 case EVP_CTRL_TLS1_2_TLSTREE:
838 unsigned char newkey[32];
839 int mode = EVP_CIPHER_CTX_mode(ctx);
840 static const unsigned char zeroseq[8];
841 gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
842 gost_grasshopper_cipher_ctx *c = NULL;
844 unsigned char adjusted_iv[16];
845 unsigned char seq[8];
847 if (mode != EVP_CIPH_CTR_MODE)
850 ctr_ctx = (gost_grasshopper_cipher_ctx_ctr *)
851 EVP_CIPHER_CTX_get_cipher_data(ctx);
855 if (EVP_CIPHER_CTX_encrypting(ctx)) {
857 * OpenSSL increments seq after mac calculation.
858 * As we have Mac-Then-Encrypt, we need decrement it here on encryption
859 * to derive the key correctly.
861 if (memcmp(seq, zeroseq, 8) != 0)
865 if (seq[j] != 0) {seq[j]--; break;}
870 if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
871 (const unsigned char *)seq) > 0) {
872 memset(adjusted_iv, 0, 16);
873 memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 8);
874 for(j=7,carry=0; j>=0; j--)
876 int adj_byte = adjusted_iv[j]+seq[j]+carry;
877 carry = (adj_byte > 255) ? 1 : 0;
878 adjusted_iv[j] = adj_byte & 0xFF;
880 EVP_CIPHER_CTX_set_num(ctx, 0);
881 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
883 gost_grasshopper_cipher_key(c, newkey);
890 case EVP_CTRL_AEAD_GET_TAG:
891 case EVP_CTRL_AEAD_SET_TAG:
894 unsigned char *tag = ptr;
896 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
897 if (c->c.type != GRASSHOPPER_CIPHER_MGM)
900 if (taglen > KUZNYECHIK_MAC_MAX_SIZE) {
901 CRYPTOCOMerr(CRYPTOCOM_F_GOST_GRASSHOPPER_CIPHER_CTL,
902 CRYPTOCOM_R_INVALID_TAG_LENGTH);
906 if (type == EVP_CTRL_AEAD_GET_TAG)
907 memcpy(tag, c->final_tag, taglen);
909 memcpy(c->final_tag, tag, taglen);
914 case EVP_CTRL_PROCESS_UNPROTECTED:
916 STACK_OF(X509_ATTRIBUTE) *x = ptr;
917 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
919 if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
922 return gost2015_process_unprotected_attributes(x, arg, KUZNYECHIK_MAC_MAX_SIZE, c->tag);
925 case EVP_CTRL_COPY: {
926 EVP_CIPHER_CTX *out = ptr;
928 gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
929 gost_grasshopper_cipher_ctx_ctr *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
931 if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
934 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
935 out_cctx->omac_ctx = EVP_MD_CTX_new();
936 if (out_cctx->omac_ctx == NULL) {
937 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
941 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
944 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
945 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
951 GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
955 return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
956 GRASSHOPPER_KEY_SIZE /* key_size */ );
959 const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
960 int iv_size, bool padding, int extra_flags)
962 unsigned long flags = (unsigned long)(mode
963 | ((!padding) ? EVP_CIPH_NO_PADDING : 0)
964 | ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0)
965 | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT
968 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
969 && EVP_CIPHER_meth_set_flags(cipher, flags)
970 && EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup)
971 && EVP_CIPHER_meth_set_set_asn1_params(cipher,
972 gost_grasshopper_set_asn1_parameters)
973 && EVP_CIPHER_meth_set_get_asn1_params(cipher,
974 gost_grasshopper_get_asn1_parameters)
975 && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
976 && EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
979 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper(uint8_t mode,
983 struct GRASSHOPPER_CIPHER_PARAMS *params;
985 cipher = &gost_grasshopper_ciphers[num];
987 if (*cipher == NULL) {
988 grasshopper_init_cipher_func init_cipher;
989 int nid, block_size, ctx_size, iv_size, extra_flags;
992 params = &gost_cipher_params[num];
995 init_cipher = params->init_cipher;
996 block_size = params->block_size;
997 ctx_size = params->ctx_size;
998 iv_size = params->iv_size;
999 padding = params->padding;
1000 extra_flags = params->extra_flags;
1002 *cipher = cipher_gost_grasshopper_create(nid, block_size);
1003 if (*cipher == NULL) {
1007 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding, extra_flags)
1008 || !EVP_CIPHER_meth_set_init(*cipher, init_cipher)
1009 || !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
1010 EVP_CIPHER_meth_free(*cipher);
1018 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ecb()
1020 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
1023 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cbc()
1025 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
1028 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ofb()
1030 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
1033 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cfb()
1035 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
1038 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctr()
1040 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
1043 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm()
1045 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1046 GRASSHOPPER_CIPHER_CTRACPKM);
1049 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm_omac()
1051 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1052 GRASSHOPPER_CIPHER_CTRACPKMOMAC);
1055 void cipher_gost_grasshopper_destroy(void)
1057 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
1058 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
1059 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
1060 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
1061 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
1062 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
1063 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
1064 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
1065 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
1066 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
1067 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM]);
1068 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
1069 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC]);
1070 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC] = NULL;