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);
588 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_DO_CTRACPKM_OMAC, ERR_R_EVP_LIB);
591 result = gost_grasshopper_cipher_do_ctracpkm(ctx, out, in, inl);
593 /* As in and out can be the same pointer, process decrypted here */
594 if (!EVP_CIPHER_CTX_encrypting(ctx))
595 EVP_DigestSignUpdate(c->omac_ctx, out, inl);
600 * Fixed 128-bit IV implementation make shift regiser redundant.
602 static void gost_grasshopper_cnt_next(gost_grasshopper_cipher_ctx * ctx,
603 grasshopper_w128_t * iv,
604 grasshopper_w128_t * buf)
606 grasshopper_w128_t tmp;
607 memcpy(&tmp, iv, 16);
608 grasshopper_encrypt_block(&ctx->encrypt_round_keys, &tmp,
613 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX *ctx, unsigned char *out,
614 const unsigned char *in, size_t inl)
616 gost_grasshopper_cipher_ctx *c = (gost_grasshopper_cipher_ctx *)
617 EVP_CIPHER_CTX_get_cipher_data(ctx);
618 const unsigned char *in_ptr = in;
619 unsigned char *out_ptr = out;
620 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
621 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
622 int num = EVP_CIPHER_CTX_num(ctx);
626 /* process partial block if any */
628 for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
629 j++, i++, in_ptr++, out_ptr++) {
630 *out_ptr = buf[j] ^ (*in_ptr);
632 if (j == GRASSHOPPER_BLOCK_SIZE) {
633 EVP_CIPHER_CTX_set_num(ctx, 0);
635 EVP_CIPHER_CTX_set_num(ctx, (int)j);
640 for (; i + GRASSHOPPER_BLOCK_SIZE <
642 i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
643 GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
645 * block cipher current iv
648 gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
649 (grasshopper_w128_t *) buf);
652 * xor next block of input text with it and output it
657 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
658 out_ptr[j] = buf[j] ^ in_ptr[j];
662 /* Process rest of buffer */
664 gost_grasshopper_cnt_next(c, (grasshopper_w128_t *) iv,
665 (grasshopper_w128_t *) buf);
666 for (j = 0; i < inl; j++, i++) {
667 out_ptr[j] = buf[j] ^ in_ptr[j];
669 EVP_CIPHER_CTX_set_num(ctx, (int)j);
671 EVP_CIPHER_CTX_set_num(ctx, 0);
677 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX *ctx, unsigned char *out,
678 const unsigned char *in, size_t inl)
680 gost_grasshopper_cipher_ctx *c =
681 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
682 const unsigned char *in_ptr = in;
683 unsigned char *out_ptr = out;
684 unsigned char *buf = EVP_CIPHER_CTX_buf_noconst(ctx);
685 unsigned char *iv = EVP_CIPHER_CTX_iv_noconst(ctx);
686 bool encrypting = (bool) EVP_CIPHER_CTX_encrypting(ctx);
687 int num = EVP_CIPHER_CTX_num(ctx);
691 /* process partial block if any */
693 for (j = (size_t)num, i = 0; j < GRASSHOPPER_BLOCK_SIZE && i < inl;
694 j++, i++, in_ptr++, out_ptr++) {
696 buf[j + GRASSHOPPER_BLOCK_SIZE] = *in_ptr;
698 *out_ptr = buf[j] ^ (*in_ptr);
700 buf[j + GRASSHOPPER_BLOCK_SIZE] = *out_ptr;
703 if (j == GRASSHOPPER_BLOCK_SIZE) {
704 memcpy(iv, buf + GRASSHOPPER_BLOCK_SIZE, GRASSHOPPER_BLOCK_SIZE);
705 EVP_CIPHER_CTX_set_num(ctx, 0);
707 EVP_CIPHER_CTX_set_num(ctx, (int)j);
712 for (; i + GRASSHOPPER_BLOCK_SIZE <
714 i += GRASSHOPPER_BLOCK_SIZE, in_ptr +=
715 GRASSHOPPER_BLOCK_SIZE, out_ptr += GRASSHOPPER_BLOCK_SIZE) {
717 * block cipher current iv
719 grasshopper_encrypt_block(&c->encrypt_round_keys,
720 (grasshopper_w128_t *) iv,
721 (grasshopper_w128_t *) buf, &c->buffer);
723 * xor next block of input text with it and output it
729 memcpy(iv, in_ptr, GRASSHOPPER_BLOCK_SIZE);
731 for (j = 0; j < GRASSHOPPER_BLOCK_SIZE; j++) {
732 out_ptr[j] = buf[j] ^ in_ptr[j];
735 /* Next iv is next block of cipher text */
737 memcpy(iv, out_ptr, GRASSHOPPER_BLOCK_SIZE);
741 /* Process rest of buffer */
743 grasshopper_encrypt_block(&c->encrypt_round_keys,
744 (grasshopper_w128_t *) iv,
745 (grasshopper_w128_t *) buf, &c->buffer);
747 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, in_ptr, inl - i);
749 for (j = 0; i < inl; j++, i++) {
750 out_ptr[j] = buf[j] ^ in_ptr[j];
752 EVP_CIPHER_CTX_set_num(ctx, (int)j);
754 memcpy(buf + GRASSHOPPER_BLOCK_SIZE, out_ptr, j);
757 EVP_CIPHER_CTX_set_num(ctx, 0);
763 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX *ctx)
765 struct GRASSHOPPER_CIPHER_PARAMS *params;
766 gost_grasshopper_cipher_ctx *c =
767 (gost_grasshopper_cipher_ctx *) EVP_CIPHER_CTX_get_cipher_data(ctx);
772 params = &gost_cipher_params[c->type];
774 gost_grasshopper_cipher_destroy(c);
775 if (params->destroy_cipher != NULL) {
776 params->destroy_cipher(c);
779 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
784 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX *ctx, ASN1_TYPE *params)
786 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
787 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
789 /* CMS implies 256kb section_size */
790 ctr->section_size = 256*1024;
792 return gost2015_set_asn1_params(params, EVP_CIPHER_CTX_original_iv(ctx), 8,
798 GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX
802 if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CTR_MODE) {
803 gost_grasshopper_cipher_ctx_ctr *ctr = EVP_CIPHER_CTX_get_cipher_data(ctx);
806 unsigned char iv[16];
808 if (gost2015_get_asn1_params(params, 16, iv, 8, ctr->kdf_seed) == 0) {
812 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, iv_len);
813 memcpy((unsigned char *)EVP_CIPHER_CTX_original_iv(ctx), iv, iv_len);
815 /* CMS implies 256kb section_size */
816 ctr->section_size = 256*1024;
822 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg,
826 case EVP_CTRL_RAND_KEY:{
828 ((unsigned char *)ptr, EVP_CIPHER_CTX_key_length(ctx)) <= 0) {
829 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, GOST_R_RNG_ERROR);
834 case EVP_CTRL_KEY_MESH:{
835 gost_grasshopper_cipher_ctx_ctr *c =
836 EVP_CIPHER_CTX_get_cipher_data(ctx);
837 if ((c->c.type != GRASSHOPPER_CIPHER_CTRACPKM &&
838 c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
840 || (arg % GRASSHOPPER_BLOCK_SIZE))
842 c->section_size = arg;
845 #ifdef EVP_CTRL_TLS1_2_TLSTREE
846 case EVP_CTRL_TLS1_2_TLSTREE:
848 unsigned char newkey[32];
849 int mode = EVP_CIPHER_CTX_mode(ctx);
850 static const unsigned char zeroseq[8];
851 gost_grasshopper_cipher_ctx_ctr *ctr_ctx = NULL;
852 gost_grasshopper_cipher_ctx *c = NULL;
854 unsigned char adjusted_iv[16];
855 unsigned char seq[8];
857 if (mode != EVP_CIPH_CTR_MODE)
860 ctr_ctx = (gost_grasshopper_cipher_ctx_ctr *)
861 EVP_CIPHER_CTX_get_cipher_data(ctx);
865 if (EVP_CIPHER_CTX_encrypting(ctx)) {
867 * OpenSSL increments seq after mac calculation.
868 * As we have Mac-Then-Encrypt, we need decrement it here on encryption
869 * to derive the key correctly.
871 if (memcmp(seq, zeroseq, 8) != 0)
875 if (seq[j] != 0) {seq[j]--; break;}
880 if (gost_tlstree(NID_grasshopper_cbc, c->master_key.k.b, newkey,
881 (const unsigned char *)seq) > 0) {
882 memset(adjusted_iv, 0, 16);
883 memcpy(adjusted_iv, EVP_CIPHER_CTX_original_iv(ctx), 8);
884 for(j=7,carry=0; j>=0; j--)
886 int adj_byte = adjusted_iv[j]+seq[j]+carry;
887 carry = (adj_byte > 255) ? 1 : 0;
888 adjusted_iv[j] = adj_byte & 0xFF;
890 EVP_CIPHER_CTX_set_num(ctx, 0);
891 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), adjusted_iv, 16);
893 gost_grasshopper_cipher_key(c, newkey);
900 case EVP_CTRL_AEAD_GET_TAG:
901 case EVP_CTRL_AEAD_SET_TAG:
904 unsigned char *tag = ptr;
906 gost_grasshopper_cipher_ctx *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
907 if (c->c.type != GRASSHOPPER_CIPHER_MGM)
910 if (taglen > KUZNYECHIK_MAC_MAX_SIZE) {
911 CRYPTOCOMerr(CRYPTOCOM_F_GOST_GRASSHOPPER_CIPHER_CTL,
912 CRYPTOCOM_R_INVALID_TAG_LENGTH);
916 if (type == EVP_CTRL_AEAD_GET_TAG)
917 memcpy(tag, c->final_tag, taglen);
919 memcpy(c->final_tag, tag, taglen);
924 case EVP_CTRL_PROCESS_UNPROTECTED:
926 gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx);
927 ASN1_OBJECT *cmsmacobj = NULL;
928 if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
930 cmsmacobj = OBJ_txt2obj(OID_GOST_CMS_MAC, 1);
931 if (cmsmacobj == NULL) {
932 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
935 if (arg == 0) /*Decrypting*/ {
936 STACK_OF(X509_ATTRIBUTE) *x = ptr;
937 ASN1_OCTET_STRING *osExpectedMac = X509at_get0_data_by_OBJ(x,
938 cmsmacobj, -3, V_ASN1_OCTET_STRING);
939 ASN1_OBJECT_free(cmsmacobj);
941 if (ptr == NULL || osExpectedMac ==NULL || osExpectedMac->length != KUZNYECHIK_MAC_MAX_SIZE)
944 memcpy(c->tag, osExpectedMac->data, osExpectedMac->length);
947 STACK_OF(X509_ATTRIBUTE) *x = ptr;
948 return (X509at_add1_attr_by_OBJ(&x, cmsmacobj,
949 V_ASN1_OCTET_STRING, c->tag, KUZNYECHIK_MAC_MAX_SIZE) == NULL) ? -1 : 1;
953 case EVP_CTRL_COPY: {
954 EVP_CIPHER_CTX *out = ptr;
956 gost_grasshopper_cipher_ctx_ctr *out_cctx = EVP_CIPHER_CTX_get_cipher_data(out);
957 gost_grasshopper_cipher_ctx_ctr *in_cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
959 if (in_cctx->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC)
962 if (in_cctx->omac_ctx == out_cctx->omac_ctx) {
963 out_cctx->omac_ctx = EVP_MD_CTX_new();
964 if (out_cctx->omac_ctx == NULL) {
965 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE);
969 return EVP_MD_CTX_copy(out_cctx->omac_ctx, in_cctx->omac_ctx);
972 GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL,
973 GOST_R_UNSUPPORTED_CIPHER_CTL_COMMAND);
979 GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_create(int
983 return EVP_CIPHER_meth_new(cipher_type, block_size /* block_size */ ,
984 GRASSHOPPER_KEY_SIZE /* key_size */ );
987 const int cipher_gost_grasshopper_setup(EVP_CIPHER *cipher, uint8_t mode,
988 int iv_size, bool padding, int extra_flags)
990 unsigned long flags = (unsigned long)(mode
991 | ((!padding) ? EVP_CIPH_NO_PADDING : 0)
992 | ((iv_size > 0) ? EVP_CIPH_CUSTOM_IV : 0)
993 | EVP_CIPH_RAND_KEY | EVP_CIPH_ALWAYS_CALL_INIT
996 return EVP_CIPHER_meth_set_iv_length(cipher, iv_size)
997 && EVP_CIPHER_meth_set_flags(cipher, flags)
998 && EVP_CIPHER_meth_set_cleanup(cipher, gost_grasshopper_cipher_cleanup)
999 && EVP_CIPHER_meth_set_set_asn1_params(cipher,
1000 gost_grasshopper_set_asn1_parameters)
1001 && EVP_CIPHER_meth_set_get_asn1_params(cipher,
1002 gost_grasshopper_get_asn1_parameters)
1003 && EVP_CIPHER_meth_set_ctrl(cipher, gost_grasshopper_cipher_ctl)
1004 && EVP_CIPHER_meth_set_do_cipher(cipher, gost_grasshopper_cipher_do);
1007 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper(uint8_t mode,
1010 EVP_CIPHER **cipher;
1011 struct GRASSHOPPER_CIPHER_PARAMS *params;
1013 cipher = &gost_grasshopper_ciphers[num];
1015 if (*cipher == NULL) {
1016 grasshopper_init_cipher_func init_cipher;
1017 int nid, block_size, ctx_size, iv_size, extra_flags;
1020 params = &gost_cipher_params[num];
1023 init_cipher = params->init_cipher;
1024 block_size = params->block_size;
1025 ctx_size = params->ctx_size;
1026 iv_size = params->iv_size;
1027 padding = params->padding;
1028 extra_flags = params->extra_flags;
1030 *cipher = cipher_gost_grasshopper_create(nid, block_size);
1031 if (*cipher == NULL) {
1035 if (!cipher_gost_grasshopper_setup(*cipher, mode, iv_size, padding, extra_flags)
1036 || !EVP_CIPHER_meth_set_init(*cipher, init_cipher)
1037 || !EVP_CIPHER_meth_set_impl_ctx_size(*cipher, ctx_size)) {
1038 EVP_CIPHER_meth_free(*cipher);
1046 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ecb()
1048 return cipher_gost_grasshopper(EVP_CIPH_ECB_MODE, GRASSHOPPER_CIPHER_ECB);
1051 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cbc()
1053 return cipher_gost_grasshopper(EVP_CIPH_CBC_MODE, GRASSHOPPER_CIPHER_CBC);
1056 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ofb()
1058 return cipher_gost_grasshopper(EVP_CIPH_OFB_MODE, GRASSHOPPER_CIPHER_OFB);
1061 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_cfb()
1063 return cipher_gost_grasshopper(EVP_CIPH_CFB_MODE, GRASSHOPPER_CIPHER_CFB);
1066 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctr()
1068 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE, GRASSHOPPER_CIPHER_CTR);
1071 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm()
1073 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1074 GRASSHOPPER_CIPHER_CTRACPKM);
1077 const GRASSHOPPER_INLINE EVP_CIPHER *cipher_gost_grasshopper_ctracpkm_omac()
1079 return cipher_gost_grasshopper(EVP_CIPH_CTR_MODE,
1080 GRASSHOPPER_CIPHER_CTRACPKMOMAC);
1083 void cipher_gost_grasshopper_destroy(void)
1085 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB]);
1086 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_ECB] = NULL;
1087 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC]);
1088 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CBC] = NULL;
1089 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB]);
1090 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_OFB] = NULL;
1091 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB]);
1092 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CFB] = NULL;
1093 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR]);
1094 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTR] = NULL;
1095 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM]);
1096 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKM] = NULL;
1097 EVP_CIPHER_meth_free(gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC]);
1098 gost_grasshopper_ciphers[GRASSHOPPER_CIPHER_CTRACPKMOMAC] = NULL;