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));
221 gost_grasshopper_cipher_key(c, key);
222 gost_grasshopper_master_key(c, key);
226 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv,
227 EVP_CIPHER_CTX_iv_length(ctx));
230 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx),
231 EVP_CIPHER_CTX_original_iv(ctx), EVP_CIPHER_CTX_iv_length(ctx));
233 grasshopper_zero128(&c->buffer);
238 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX *ctx, const unsigned char
239 *key, const unsigned char
242 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
243 c->type = GRASSHOPPER_CIPHER_ECB;
244 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
247 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX *ctx, const unsigned char
248 *key, const unsigned char
251 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
252 c->type = GRASSHOPPER_CIPHER_CBC;
253 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
256 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX *ctx, const unsigned char
257 *key, const unsigned char
260 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
261 c->type = GRASSHOPPER_CIPHER_OFB;
262 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
265 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX *ctx, const unsigned char
266 *key, const unsigned char
269 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
270 c->type = GRASSHOPPER_CIPHER_CFB;
271 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
274 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX *ctx, const unsigned char
275 *key, const unsigned char
278 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
280 c->c.type = GRASSHOPPER_CIPHER_CTR;
281 EVP_CIPHER_CTX_set_num(ctx, 0);
283 grasshopper_zero128(&c->partial_buffer);
285 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
288 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX
290 char *key, const unsigned
293 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
295 /* NB: setting type makes EVP do_cipher callback useless */
296 c->c.type = GRASSHOPPER_CIPHER_CTRACPKM;
297 EVP_CIPHER_CTX_set_num(ctx, 0);
298 c->section_size = 4096;
300 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
303 GRASSHOPPER_INLINE int gost_grasshopper_cipher_init_ctracpkm_omac(EVP_CIPHER_CTX
305 char *key, const unsigned
308 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
310 /* NB: setting type makes EVP do_cipher callback useless */
311 c->c.type = GRASSHOPPER_CIPHER_CTRACPKMOMAC;
312 EVP_CIPHER_CTX_set_num(ctx, 0);
313 c->section_size = 4096;
316 unsigned char keys[64];
317 const EVP_MD *md = EVP_get_digestbynid(NID_kuznyechik_mac);
324 if (RAND_bytes(c->kdf_seed, 8) != 1)
328 if (gost_kdftree2012_256(keys, 64, key, 32, (const unsigned char *)"kdf tree", 8, c->kdf_seed, 8, 1) <= 0)
331 c->omac_ctx = EVP_MD_CTX_new();
332 mac_key = EVP_PKEY_new_mac_key(NID_kuznyechik_mac, NULL, keys+32, 32);
334 if (mac_key == NULL || c->omac_ctx == NULL) {
335 EVP_PKEY_free(mac_key);
339 if (EVP_DigestInit_ex(c->omac_ctx, md, NULL) <= 0 ||
340 EVP_DigestSignInit(c->omac_ctx, NULL, md, NULL, mac_key) <= 0) {
341 EVP_PKEY_free(mac_key);
344 EVP_PKEY_free(mac_key);
346 return gost_grasshopper_cipher_init(ctx, keys, iv, enc);
348 return gost_grasshopper_cipher_init(ctx, key, iv, enc);
351 GRASSHOPPER_INLINE int gost_grasshopper_cipher_do(EVP_CIPHER_CTX *ctx,
353 const unsigned char *in,
356 gost_grasshopper_cipher_ctx *c =
357 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
358 struct GRASSHOPPER_CIPHER_PARAMS *params = &gost_cipher_params[c->type];
360 return params->do_cipher(ctx, out, in, inl);
363 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX *ctx, unsigned char *out,
364 const unsigned char *in, size_t inl)
366 gost_grasshopper_cipher_ctx *c =
367 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
368 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
369 const unsigned char *current_in = in;
370 unsigned char *current_out = out;
371 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
374 for (i = 0; i < blocks;
375 i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
376 GRASSHOPPER_BLOCK_SIZE) {
378 grasshopper_encrypt_block(&c->encrypt_round_keys,
379 (grasshopper_w128_t *) current_in,
380 (grasshopper_w128_t *) current_out,
383 grasshopper_decrypt_block(&c->decrypt_round_keys,
384 (grasshopper_w128_t *) current_in,
385 (grasshopper_w128_t *) current_out,
393 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX *ctx, unsigned char *out,
394 const unsigned char *in, size_t inl)
396 gost_grasshopper_cipher_ctx *c =
397 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
398 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
399 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
400 const unsigned char *current_in = in;
401 unsigned char *current_out = out;
402 size_t blocks = inl / GRASSHOPPER_BLOCK_SIZE;
404 grasshopper_w128_t *currentBlock;
406 currentBlock = (grasshopper_w128_t *) iv;
408 for (i = 0; i < blocks;
409 i++, current_in += GRASSHOPPER_BLOCK_SIZE, current_out +=
410 GRASSHOPPER_BLOCK_SIZE) {
411 grasshopper_w128_t *currentInputBlock = (grasshopper_w128_t *) current_in;
412 grasshopper_w128_t *currentOutputBlock = (grasshopper_w128_t *) current_out;
414 grasshopper_append128(currentBlock, currentInputBlock);
415 grasshopper_encrypt_block(&c->encrypt_round_keys, currentBlock,
416 currentOutputBlock, &c->buffer);
417 grasshopper_copy128(currentBlock, currentOutputBlock);
419 grasshopper_w128_t tmp;
421 grasshopper_copy128(&tmp, currentInputBlock);
422 grasshopper_decrypt_block(&c->decrypt_round_keys,
423 currentInputBlock, currentOutputBlock,
425 grasshopper_append128(currentOutputBlock, currentBlock);
426 grasshopper_copy128(currentBlock, &tmp);
433 void inc_counter(unsigned char *counter, size_t counter_bytes)
435 unsigned int n = counter_bytes;
448 /* increment counter (128-bit int) by 1 */
449 static void ctr128_inc(unsigned char *counter)
451 inc_counter(counter, 16);
454 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX *ctx, unsigned char *out,
455 const unsigned char *in, size_t inl)
457 gost_grasshopper_cipher_ctx_ctr *c = (gost_grasshopper_cipher_ctx_ctr *)
458 EVP_CIPHER_CTX_get_cipher_data(ctx);
459 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
460 const unsigned char *current_in = in;
461 unsigned char *current_out = out;
462 grasshopper_w128_t *currentInputBlock;
463 grasshopper_w128_t *currentOutputBlock;
464 unsigned int n = EVP_CIPHER_CTX_num(ctx);
468 grasshopper_w128_t *iv_buffer;
469 grasshopper_w128_t tmp;
471 while (n && lasted) {
472 *(current_out++) = *(current_in++) ^ c->partial_buffer.b[n];
474 n = (n + 1) % GRASSHOPPER_BLOCK_SIZE;
476 EVP_CIPHER_CTX_set_num(ctx, n);
477 blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
479 iv_buffer = (grasshopper_w128_t *) iv;
482 for (i = 0; i < blocks; i++) {
483 currentInputBlock = (grasshopper_w128_t *) current_in;
484 currentOutputBlock = (grasshopper_w128_t *) current_out;
485 grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
486 &c->partial_buffer, &c->c.buffer);
487 grasshopper_plus128(&tmp, &c->partial_buffer, currentInputBlock);
488 grasshopper_copy128(currentOutputBlock, &tmp);
489 ctr128_inc(iv_buffer->b);
490 current_in += GRASSHOPPER_BLOCK_SIZE;
491 current_out += GRASSHOPPER_BLOCK_SIZE;
492 lasted -= GRASSHOPPER_BLOCK_SIZE;
496 currentInputBlock = (grasshopper_w128_t *) current_in;
497 currentOutputBlock = (grasshopper_w128_t *) current_out;
498 grasshopper_encrypt_block(&c->c.encrypt_round_keys, iv_buffer,
499 &c->partial_buffer, &c->c.buffer);
500 for (i = 0; i < lasted; i++) {
501 currentOutputBlock->b[i] =
502 c->partial_buffer.b[i] ^ currentInputBlock->b[i];
504 EVP_CIPHER_CTX_set_num(ctx, i);
505 ctr128_inc(iv_buffer->b);
511 #define GRASSHOPPER_BLOCK_MASK (GRASSHOPPER_BLOCK_SIZE - 1)
512 static inline void apply_acpkm_grasshopper(gost_grasshopper_cipher_ctx_ctr *
513 ctx, unsigned int *num)
515 if (!ctx->section_size || (*num < ctx->section_size))
518 *num &= GRASSHOPPER_BLOCK_MASK;
521 /* If meshing is not configured via ctrl (setting section_size)
522 * this function works exactly like plain ctr */
523 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX *ctx,
525 const unsigned char *in, size_t inl)
527 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
528 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
529 unsigned int num = EVP_CIPHER_CTX_num(ctx);
530 size_t blocks, i, lasted = inl;
531 grasshopper_w128_t tmp;
533 while ((num & GRASSHOPPER_BLOCK_MASK) && lasted) {
534 *out++ = *in++ ^ c->partial_buffer.b[num & GRASSHOPPER_BLOCK_MASK];
538 blocks = lasted / GRASSHOPPER_BLOCK_SIZE;
541 for (i = 0; i < blocks; i++) {
542 apply_acpkm_grasshopper(c, &num);
543 grasshopper_encrypt_block(&c->c.encrypt_round_keys,
544 (grasshopper_w128_t *) iv,
545 (grasshopper_w128_t *) & c->partial_buffer,
547 grasshopper_plus128(&tmp, &c->partial_buffer,
548 (grasshopper_w128_t *) in);
549 grasshopper_copy128((grasshopper_w128_t *) out, &tmp);
551 in += GRASSHOPPER_BLOCK_SIZE;
552 out += GRASSHOPPER_BLOCK_SIZE;
553 num += GRASSHOPPER_BLOCK_SIZE;
554 lasted -= GRASSHOPPER_BLOCK_SIZE;
559 apply_acpkm_grasshopper(c, &num);
560 grasshopper_encrypt_block(&c->c.encrypt_round_keys,
561 (grasshopper_w128_t *) iv,
562 &c->partial_buffer, &c->c.buffer);
563 for (i = 0; i < lasted; i++)
564 out[i] = c->partial_buffer.b[i] ^ in[i];
568 EVP_CIPHER_CTX_set_num(ctx, num);
573 int gost_grasshopper_cipher_do_ctracpkm_omac(EVP_CIPHER_CTX *ctx,
575 const unsigned char *in, size_t inl)
578 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
579 /* As in and out can be the same pointer, process unencrypted here */
580 if (EVP_CIPHER_CTX_encrypting(ctx))
581 EVP_DigestSignUpdate(c->omac_ctx, in, inl);
583 if (in == NULL && inl == 0) { /* Final call */
584 return gost2015_final_call(ctx, c->omac_ctx, KUZNYECHIK_MAC_MAX_SIZE, c->tag, gost_grasshopper_cipher_do_ctracpkm);
587 result = gost_grasshopper_cipher_do_ctracpkm(ctx, out, in, inl);
589 /* As in and out can be the same pointer, process decrypted here */
590 if (!EVP_CIPHER_CTX_encrypting(ctx))
591 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
596 * Fixed 128-bit IV implementation make shift regiser redundant.
598 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx * ctx,
599 grasshopper_w128_t * iv,
600 grasshopper_w128_t * buf)
602 grasshopper_w128_t tmp;
603 memcpy(&tmp, iv, 16);
604 grasshopper_encrypt_block(&ctx->encrypt_round_keys, &tmp,
609 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
610 const unsigned char *in, size_t inl)
612 gost_grasshopper_cipher_ctx *c = (gost_grasshopper_cipher_ctx *)
613 EVP_CIPHER_CTX_get_cipher_data(ctx);
614 const unsigned char *in_ptr = in;
615 unsigned char *out_ptr = out;
616 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
617 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
618 int num = EVP_CIPHER_CTX_num(ctx);
622 /* process partial block if any */
624 for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
625 j++, i++, in_ptr++, out_ptr++) {
626 *out_ptr = buf[j] ^ (*in_ptr);
628 if (j == GRASSHOPPER_BLOCK_SIZE) {
629 EVP_CIPHER_CTX_set_num(ctx, 0);
631 EVP_CIPHER_CTX_set_num(ctx, (int)j);
636 for (; i + GRASSHOPPER_BLOCK_SIZE <
638 i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
639 GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
641 * block cipher current iv
644 gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
645 (grasshopper_w128_t *) buf);
648 * xor next block of input text with it and output it
653 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
654 out_ptr[j] = buf[j] ^ in_ptr[j];
658 /* Process rest of buffer */
660 gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
661 (grasshopper_w128_t *) buf);
662 for (j = 0; i < inl; j++, i++) {
663 out_ptr[j] = buf[j] ^ in_ptr[j];
665 EVP_CIPHER_CTX_set_num(ctx, (int)j);
667 EVP_CIPHER_CTX_set_num(ctx, 0);
673 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
674 const unsigned char *in, size_t inl)
676 gost_grasshopper_cipher_ctx *c =
677 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
678 const unsigned char *in_ptr = in;
679 unsigned char *out_ptr = out;
680 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
681 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
682 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
683 int num = EVP_CIPHER_CTX_num(ctx);
687 /* process partial block if any */
689 for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
690 j++, i++, in_ptr++, out_ptr++) {
692 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
694 *out_ptr = buf[j] ^ (*in_ptr);
696 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
699 if (j == GRASSHOPPER_BLOCK_SIZE) {
700 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
701 EVP_CIPHER_CTX_set_num(ctx, 0);
703 EVP_CIPHER_CTX_set_num(ctx, (int)j);
708 for (; i + GRASSHOPPER_BLOCK_SIZE <
710 i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
711 GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
713 * block cipher current iv
715 grasshopper_encrypt_block(&c->encrypt_round_keys,
716 (grasshopper_w128_t *) iv,
717 (grasshopper_w128_t *) buf, &c->buffer);
719 * xor next block of input text with it and output it
725 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
727 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
728 out_ptr[j] = buf[j] ^ in_ptr[j];
731 /* Next iv is next block of cipher text */
733 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
737 /* Process rest of buffer */
739 grasshopper_encrypt_block(&c->encrypt_round_keys,
740 (grasshopper_w128_t *) iv,
741 (grasshopper_w128_t *) buf, &c->buffer);
743 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
745 for (j = 0; i < inl; j++, i++) {
746 out_ptr[j] = buf[j] ^ in_ptr[j];
748 EVP_CIPHER_CTX_set_num(ctx, (int)j);
750 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
753 EVP_CIPHER_CTX_set_num(ctx, 0);
759 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx)
761 struct GRASSHOPPER_CIPHER_PARAMS *params;
762 gost_grasshopper_cipher_ctx *c =
763 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
768 params = &gost_cipher_params[c->type];
770 gost_grasshopper_cipher_destroy(c);
771 if (params->destroy_cipher != NULL) {
772 params->destroy_cipher(c);
775 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
780 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
782 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
783 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
785 /* CMS implies 256kb section_size */
786 ctr->section_size = 256*1024;
788 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 8,
794 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX
798 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
799 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
802 unsigned char iv[16];
804 if (gost2015_get_asn1_params(params, 16, iv, 8, ctr->kdf_seed) == 0) {
808 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, iv_len);
809 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, iv_len);
811 /* CMS implies 256kb section_size */
812 ctr->section_size = 256*1024;
818 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg,
822 case EVP_CTRL_RAND_KEY:{
824 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
825 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
830 case EVP_CTRL_KEY_MESH:{
831 gost_grasshopper_cipher_ctx_ctr *c =
832 EVP_CIPHER_CTX_get_cipher_data(ctx);
833 if ((c->c.type != GRASSHOPPER_CIPHER_CTRACPKM &&
834 c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
836 || (arg % GRASSHOPPER_BLOCK_SIZE))
838 c->section_size = arg;
841 #ifdef EVP_CTRL_TLS1_2_TLSTREE
842 case EVP_CTRL_TLS1_2_TLSTREE:
844 unsigned char newkey[32];
845 int mode = EVP_CIPHER_CTX_mode(ctx);
846 static const unsigned char zeroseq[8];
847 gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
848 gost_grasshopper_cipher_ctx *c = NULL;
850 unsigned char adjusted_iv[16];
851 unsigned char seq[8];
853 if (mode != EVP_CIPH_CTR_MODE)
856 ctr_ctx = (gost_grasshopper_cipher_ctx_ctr *)
857 EVP_CIPHER_CTX_get_cipher_data(ctx);
861 if (EVP_CIPHER_CTX_encrypting(ctx)) {
863 * OpenSSL increments seq after mac calculation.
864 * As we have Mac-Then-Encrypt, we need decrement it here on encryption
865 * to derive the key correctly.
867 if (memcmp(seq, zeroseq, 8) != 0)
871 if (seq[j] != 0) {seq[j]--; break;}
876 if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
877 (const unsigned char *)seq) > 0) {
878 memset(adjusted_iv, 0, 16);
879 memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 8);
880 for(j=7,carry=0; j>=0; j--)
882 int adj_byte = adjusted_iv[j]+seq[j]+carry;
883 carry = (adj_byte > 255) ? 1 : 0;
884 adjusted_iv[j] = adj_byte & 0xFF;
886 EVP_CIPHER_CTX_set_num(ctx, 0);
887 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
889 gost_grasshopper_cipher_key(c, newkey);
896 case EVP_CTRL_AEAD_GET_TAG:
897 case EVP_CTRL_AEAD_SET_TAG:
900 unsigned char *tag = ptr;
902 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
903 if (c->c.type != GRASSHOPPER_CIPHER_MGM)
906 if (taglen > KUZNYECHIK_MAC_MAX_SIZE) {
907 CRYPTOCOMerr(CRYPTOCOM_F_GOST_GRASSHOPPER_CIPHER_CTL,
908 CRYPTOCOM_R_INVALID_TAG_LENGTH);
912 if (type == EVP_CTRL_AEAD_GET_TAG)
913 memcpy(tag, c->final_tag, taglen);
915 memcpy(c->final_tag, tag, taglen);
920 case EVP_CTRL_PROCESS_UNPROTECTED:
922 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
923 ASN1_OBJECT *cmsmacobj = NULL;
924 if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
926 cmsmacobj = OBJ_txt2obj(OID_GOST_CMS_MAC, 1);
927 if (cmsmacobj == NULL) {
928 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
931 if (arg == 0) /*Decrypting*/ {
932 STACK_OF(X509_ATTRIBUTE) *x = ptr;
933 ASN1_OCTET_STRING *osExpectedMac = X509at_get0_data_by_OBJ(x,
934 cmsmacobj, -3, V_ASN1_OCTET_STRING);
935 ASN1_OBJECT_free(cmsmacobj);
937 if (ptr == NULL || osExpectedMac ==NULL || osExpectedMac->length != KUZNYECHIK_MAC_MAX_SIZE)
940 memcpy(c->tag, osExpectedMac->data, osExpectedMac->length);
943 STACK_OF(X509_ATTRIBUTE) *x = ptr;
944 return (X509at_add1_attr_by_OBJ(&x, cmsmacobj,
945 V_ASN1_OCTET_STRING, c->tag, KUZNYECHIK_MAC_MAX_SIZE) == NULL) ? -1 : 1;
949 case EVP_CTRL_COPY: {
950 EVP_CIPHER_CTX *out = ptr;
952 gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
953 gost_grasshopper_cipher_ctx_ctr *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
955 if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
958 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
959 out_cctx->omac_ctx = EVP_MD_CTX_new();
960 if (out_cctx->omac_ctx == NULL) {
961 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
965 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
968 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
969 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
975 GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
979 return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
980 GRASSHOPPER_KEY_SIZE /* key_size */ );
983 const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
984 int iv_size, bool padding, int extra_flags)
986 unsigned long flags = (unsigned long)(mode
987 | ((!padding) ? EVP_CIPH_NO_PADDING : 0)
988 | ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0)
989 | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT
992 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
993 && EVP_CIPHER_meth_set_flags(cipher, flags)
994 && EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup)
995 && EVP_CIPHER_meth_set_set_asn1_params(cipher,
996 gost_grasshopper_set_asn1_parameters)
997 && EVP_CIPHER_meth_set_get_asn1_params(cipher,
998 gost_grasshopper_get_asn1_parameters)
999 && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
1000 && EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
1003 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper(uint8_t mode,
1006 EVP_CIPHER **cipher;
1007 struct GRASSHOPPER_CIPHER_PARAMS *params;
1009 cipher = &gost_grasshopper_ciphers[num];
1011 if (*cipher == NULL) {
1012 grasshopper_init_cipher_func init_cipher;
1013 int nid, block_size, ctx_size, iv_size, extra_flags;
1016 params = &gost_cipher_params[num];
1019 init_cipher = params->init_cipher;
1020 block_size = params->block_size;
1021 ctx_size = params->ctx_size;
1022 iv_size = params->iv_size;
1023 padding = params->padding;
1024 extra_flags = params->extra_flags;
1026 *cipher = cipher_gost_grasshopper_create(nid, block_size);
1027 if (*cipher == NULL) {
1031 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding, extra_flags)
1032 || !EVP_CIPHER_meth_set_init(*cipher, init_cipher)
1033 || !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
1034 EVP_CIPHER_meth_free(*cipher);
1042 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ecb()
1044 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
1047 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cbc()
1049 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
1052 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ofb()
1054 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
1057 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cfb()
1059 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
1062 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctr()
1064 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
1067 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm()
1069 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1070 GRASSHOPPER_CIPHER_CTRACPKM);
1073 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm_omac()
1075 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1076 GRASSHOPPER_CIPHER_CTRACPKMOMAC);
1079 void cipher_gost_grasshopper_destroy(void)
1081 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
1082 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
1083 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
1084 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
1085 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
1086 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
1087 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
1088 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
1089 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
1090 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
1091 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM]);
1092 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
1093 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC]);
1094 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC] = NULL;